get_fee_growth_inside in tick.rs should allow for underflow / overflow but doesn’t
The fee growth calculation in tick.rs uses checked subtraction, so it reverts on underflow instead of wrapping as Uniswap v3 does. This can cause operations like liquidation to revert unexpectedly, potentially making positions permanently inaccessible.
SBSecurity Follow with DadeKuma, ZanyBonzy and 5 others
Originally disclosed on Code4rena
Submitted by SBSecurity, also found by DadeKuma, ZanyBonzy, aldarion, peanuts, 0xhashiman, Tricko, and Q7
When operations need to calculate the Superposition position’s fee growth, it uses a similar function implemented by uniswap v3.
However, according to this known issue, Uniswap/v3-core#573, the contract implicitly relies on underflow/overflow when calculating the fee growth if underflow is prevented, some operations that depend on fee growth will revert.
Proof of Concept
It can be observed that the current implementation of tick::get_fee_growth_inside does not allow underflow/overflow to happen when calculating fee_growth_inside_0 and fee_growth_inside_1 because the contract uses checked math with additional overflow/underflow checks; which will perform subtraction that returns none instead of wrapping around on underflow in tick.rs.
pub fn get_fee_growth_inside(
&mut self,
lower_tick: i32,
upper_tick: i32,
cur_tick: i32,
fee_growth_global_0: &U256,
fee_growth_global_1: &U256,
) -> Result<(U256, U256), Error> {
// the fee growth inside this tick is the total fee
// growth, minus the fee growth outside this tick
let lower = self.ticks.get(lower_tick);
let upper = self.ticks.get(upper_tick);
let (fee_growth_below_0, fee_growth_below_1) = if cur_tick >= lower_tick {
#[cfg(feature = "testing-dbg")]
dbg!((
"cur_tick >= lower_tick",
current_test!(),
lower.fee_growth_outside_0.get().to_string(),
lower.fee_growth_outside_1.get().to_string()
));
(
lower.fee_growth_outside_0.get(),
lower.fee_growth_outside_1.get(),
)
} else {
#[cfg(feature = "testing-dbg")]
dbg!((
"cur_tick < lower_tick",
current_test!(),
fee_growth_global_0,
fee_growth_global_1,
lower.fee_growth_outside_0.get().to_string(),
lower.fee_growth_outside_1.get().to_string()
));
(
fee_growth_global_0
.checked_sub(lower.fee_growth_outside_0.get())
.ok_or(Error::FeeGrowthSubTick)?,
fee_growth_global_1
.checked_sub(lower.fee_growth_outside_1.get())
.ok_or(Error::FeeGrowthSubTick)?,
)
};
let (fee_growth_above_0, fee_growth_above_1) = if cur_tick < upper_tick {
#[cfg(feature = "testing-dbg")]
dbg!((
"cur_tick < upper_tick",
current_test!(),
upper.fee_growth_outside_0.get().to_string(),
upper.fee_growth_outside_1.get().to_string()
));
(
upper.fee_growth_outside_0.get(),
upper.fee_growth_outside_1.get(),
)
} else {
#[cfg(feature = "testing-dbg")]
dbg!((
"cur_tick >= upper_tick",
current_test!(),
fee_growth_global_0,
fee_growth_global_1,
upper.fee_growth_outside_0.get(),
upper.fee_growth_outside_1.get()
));
(
fee_growth_global_0
.checked_sub(upper.fee_growth_outside_0.get())
.ok_or(Error::FeeGrowthSubTick)?,
fee_growth_global_1
.checked_sub(upper.fee_growth_outside_1.get())
.ok_or(Error::FeeGrowthSubTick)?,
)
};
#[cfg(feature = "testing-dbg")] // REMOVEME
{
if *fee_growth_global_0 < fee_growth_below_0 {
dbg!((
"fee_growth_global_0 < fee_growth_below_0",
current_test!(),
fee_growth_global_0.to_string(),
fee_growth_below_0.to_string()
));
}
let fee_growth_global_0 = fee_growth_global_0.checked_sub(fee_growth_below_0).unwrap();
if fee_growth_global_0 < fee_growth_above_0 {
dbg!((
"fee_growth_global_0 < fee_growth_above_0",
current_test!(),
fee_growth_global_0.to_string(),
fee_growth_above_0.to_string()
));
}
}
#[cfg(feature = "testing-dbg")]
dbg!((
"final stage checked sub below",
current_test!(),
fee_growth_global_0
.checked_sub(fee_growth_below_0)
.and_then(|x| x.checked_sub(fee_growth_above_0))
));
Ok((
fee_growth_global_0
.checked_sub(fee_growth_below_0)
.and_then(|x| x.checked_sub(fee_growth_above_0))
.ok_or(Error::FeeGrowthSubTick)?,
fee_growth_global_1
.checked_sub(fee_growth_below_1)
.and_then(|x| x.checked_sub(fee_growth_above_1))
.ok_or(Error::FeeGrowthSubTick)?,
))
}
Especially the last lines where we have fee_growth_global_0 - fee_growth_below_0 - fee_growth_above_0 and fee_growth_global_1 - fee_growth_below_1 - fee_growth_above_1.
In case either one of the calculation underflows we will receive Error::FeeGrowthSubTick instead of being wrapped and continue working.
Uniswap allows such underflows to happen because PositionLibrary works with Solidity version < 0.8. Otherwise, this will impact crucial operations that rely on this call, such as liquidation, and will revert unexpectedly. This behavior is quite often, especially for pools that use lower fees.
Furthermore, one of the Main Invariants in the README mentioned that Superposition should follow the UniswapV3 math faithfully, which is not the case here, violating the invariant.
https://github.com/code-423n4/2024-08-superposition?tab=readme-ov-file#main-invariants
Tools Used
H-04 from Particle Leverage AMM.
Recommended Mitigation Steps
Use unsafe/unchecked math when calculating the fee growths.
Assessed type
Under/Overflow
af-afk (Superposition) confirmed
The submission and its duplicates properly identify that the
tick.rsfile fails to permit underflows to occur when calculating fees which might result in inaccessible funds. I have personally observed this vulnerability in production, and I am inclined to agree with a high-risk severity rating as positions resulting in the underflow would effectively become permanently inaccessible.
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
Impact
Operations that depend on fee growth, such as liquidation, could revert unexpectedly, and affected positions may become permanently inaccessible.
What the attacker needed
Pools with lower fees; positions that trigger underflow
The fix
Use unsafe/unchecked math when calculating the fee growths.
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.
In some smart contracts, numbers are allowed to wrap around when they go below zero, like a car odometer rolling over. This report says a function that calculates fees uses a safer method that stops with an error instead of wrapping, which can break important actions like liquidations.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.