Overflow in geometric can dos swap
In the geometric swap logic, a value with 6 decimals is added to a value with 18 decimals, and the conversion multiplies the smaller value by 1e12, producing a 36-decimal number that overflows u128 for modest amounts. As a result, swaps in ask_exact_amount_out and bid_exact_amount_in revert, or users may receive the wrong number of tokens.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2025-09-dango-dex-judging/issues/263
Found by
ExtraCaterpillar
Summary
There are a few places in geometric where overflow happens during a swap. If you look at the ask_exact_amount_out function, inside the loop of this function, remaining ask amount in quote is converted into base amount. Then this base amount is compared with the available base order size at that price point. The final output of this comparison is saved into matched_amount. This matched_amount is of type Dec<u128, 6>. If the base token is 18 decimal token, this matched_amount will be 24 decimals since it adds 6 more decimals to it. After that this matched_amount is added with input_amount- input_amount.checked_add_assign(matched_amount)?;. Here the input_amount is of type Dec<u128, 18>. Since both matched_amount and input_amount are of different types, the checked_add_assign function converts matched_amount to type Dec<u128, 18> by multiplying 1e12, and then adds them. As a result the matched amount is now 36 decimals after multiplication by 1e12.
We know the max value of u128 is 340,282,366,920,938,463,463,374,607,431,768,211,455 or 340.282e36. The matched_amount already contains 36 decimals. If the matched_amount contains anything more than 340 base tokens, this input_amount will always overflow. this can happen in pool of base tokens whose price is low and has 18 decimals
Root Cause
Overflow during addition causes swaps to revert. It happens in ask_exact_amount_out and bid_exact_amount_in where the base asset is used inside checked_add_assign. The root cause is type Dec<u128, 6> is being added to type Dec<u128, 18> and the token decimal is 18, which leads to 36 decimal after addition
Internal Pre-conditions
_
External Pre-conditions
_
Attack Path
_
Impact
swaps will always revert in ask_exact_amount_out and bid_exact_amount_in or users will get wrong amount of tokens
PoC
Add this follwing test in geometric at the end of the file:
mod tests {
use std::ops::Sub;
use dango_oracle::OracleQuerier;
use grug::{coins, hash_map, Dec, Int, Timestamp, Udec128_24, Udec128_6};
use dango_types::{
constants::{eth, usdc},
oracle::PrecisionedPrice,
};
use super::*;
#[test]
fn test_overflow_ask_amount() {
let mut oracle_querier = OracleQuerier::new_mock(hash_map! {
eth::DENOM.clone() => PrecisionedPrice::new( // shib 18 decimal, consider eth as shib
Udec128::raw(grug::Int(1_17_500_000_000_000)), // .00001175
Udec128::raw(grug::Int(1_17_500_000_000_000)),
Timestamp::from_seconds(1730802926),
18,
),
usdc::DENOM.clone() => PrecisionedPrice::new(
Udec128::new_percent(100),
Udec128::new_percent(100),
Timestamp::from_seconds(1730802926),
6,
)
});
let reserve: CoinPair = coins!(
eth::DENOM.clone() => 1000000000_000_000_000_000_000_000,
usdc::DENOM.clone() => 11750_000_000,
).try_into().unwrap();
let output = Coin { denom: usdc::DENOM.clone(), amount: Int(150_000_000) };
let geometric_params = Geometric {
spacing: Udec128::raw(grug::Int(10_000_000_000_000_000)), // .01
ratio: Bounded::new_unchecked(Udec128::raw(grug::Int(1_000_000_000_000_000))), // .001
limit: 100,
};
let fee_rate = Udec128::raw(grug::Int(2_000_000_000_000_000)); // .2%
let swap_fee_rate = Bounded::new(fee_rate).unwrap();
let res = swap_exact_amount_out(&mut oracle_querier, reserve.first().denom, reserve.second().denom, &output, &reserve, geometric_params, swap_fee_rate);
println!("res: {:?}", res); // overflow error
assert!(res.is_err()); // true
let _value = res.unwrap(); // panics here
}
}
Run cargo test --package dango-dex --lib -- core::geometric::tests::test_overflow_ask_amount --exact --show-output. The test will pannic
Mitigation
In ask_exact_amount_out, set the type of input_amount as Dec<u128, 6>. Similarly, in bid_exact_amount_in, change the type of output_amount to Dec<u128, 6>
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely a decimal-scale mismatch: a Dec<u128, 6> matched amount is added to a Dec<u128, 18> input amount, and the implicit conversion multiplies by 1e12, giving a 36-decimal value that overflows u128.
Impact
Swaps will always revert in ask_exact_amount_out and bid_exact_amount_in, or users will get the wrong amount of tokens.
What the attacker needed
A pool with an 18-decimal base token; a low base token price; swap routed through geometric logic
The fix
The types should be aligned so that input_amount in ask_exact_amount_out and output_amount in bid_exact_amount_in use Dec<u128, 6> instead of Dec<u128, 18>.
How it was found
- Set up a mock oracle with an 18-decimal base token priced very low and a 6-decimal quote token.
- Build a reserve and geometric parameters, then call swap_exact_amount_out for a quote output.
- Observe that the call returns an overflow error and the test panics on unwrap.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A smart contract stores token amounts as whole numbers with a fixed number of decimal places. When two amounts with different decimal places are added, the code scales one of them up, and here that scaling made the number far too large for the fixed-size integer type, so the maths failed and the swap was rejected. This means ordinary trades on pools with an 18-decimal base token could fail.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.