The protocol's liquidity calculation used the price at the current tick instead of the actual current square-root price, causing incorrect liquidity deltas. This could make transactions revert when minimum amounts are close to desired amounts, and the judge noted it could make a portion of the protocol inaccessible.
pub fn adjust_position(
&mut self,
id: U256,
amount_0: U256,
amount_1: U256,
giving: bool,
) -> Result<(I256, I256), Revert> {
// calculate the delta using the amounts that we have here, guaranteeing
// that we don't dip below the amount that's supplied as the minimum.
let position = self.positions.positions.get(id);
let sqrt_ratio_x_96 = tick_math::get_sqrt_ratio_at_tick(self.get_cur_tick().as_i32())?;
let sqrt_ratio_a_x_96 = tick_math::get_sqrt_ratio_at_tick(position.lower.get().as_i32())?;
let sqrt_ratio_b_x_96 = tick_math::get_sqrt_ratio_at_tick(position.upper.get().as_i32())?;
let mut delta = sqrt_price_math::get_liquidity_for_amounts(
sqrt_ratio_x_96, // cur_tick
sqrt_ratio_a_x_96, // lower_tick
sqrt_ratio_b_x_96, // upper_tick
amount_0, // amount_0
amount_1, // amount_1
)?
.to_i128()
.map_or_else(|| Err(Error::LiquidityAmountTooWide), Ok)?;
if giving {
// If we're giving, then we need to take from the delta.
delta = -delta;
}
#[cfg(feature = "testing-dbg")]
dbg!((
"inside adjust_position",
current_test!(),
sqrt_ratio_x_96.to_string(),
sqrt_ratio_a_x_96.to_string(),
sqrt_ratio_b_x_96.to_string(),
amount_0.to_string(),
amount_1.to_string(),
delta
));
// [update_position] should also ensure that we don't do this on a pool that's not currently
// running
self.update_position(id, delta)
}
let sqrt_ratio_x_96 = tick_math::get_sqrt_ratio_at_tick(self.get_cur_tick().as_i32())?;
Instead of:
let sqrt_price_x_96 = self.sqrt_price.get();
This passes not the current price but the price of the current tick to the get_liquidity_for_amounts() function.
Proof of Concept
The following is a step-by-step PoC that explains the issue in detail. Let’s say there exists a pool with the following configuration:
token0 = X
token1 = FUSDC
There are many available positions; one of them with tick range [0, 5].
Current price of the pool lies between ticks 0 and 1 and is equals to 1.00005
The present calculation would result in an amount0 that would be ~899.98 or 900 which is 90% of amount0Desired and thus, is less than amount0Min. Therefore, this transaction would revert.
Severity
The protocol mentions “We should follow Uniswap V3’s math faithfully” as one of its core invariants. This must not break. However, as this report explains, this invariant stands broken.
If the amount0Desired and amount0Min (also applicable to amount1) are very close to each other, then the transaction to incrPositionC3AC7CAA() function reverts. So, the severity is High.
Tools Used
Desmos scientific calculator.
Recommended Mitigation Steps
Replace the following in the adjust_position() function below:
pub fn adjust_position(
&mut self,
id: U256,
amount_0: U256,
amount_1: U256,
giving: bool,
) -> Result<(I256, I256), Revert> {
// calculate the delta using the amounts that we have here, guaranteeing
// that we don't dip below the amount that's supplied as the minimum.
let position = self.positions.positions.get(id);
- let sqrt_ratio_x_96 = tick_math::get_sqrt_ratio_at_tick(self.get_cur_tick().as_i32())?;
+ let sqrt_ratio_x_96 = self.sqrt_price.get();
let sqrt_ratio_a_x_96 = tick_math::get_sqrt_ratio_at_tick(position.lower.get().as_i32())?;
let sqrt_ratio_b_x_96 = tick_math::get_sqrt_ratio_at_tick(position.upper.get().as_i32())?;
let mut delta = sqrt_price_math::get_liquidity_for_amounts(
sqrt_ratio_x_96, // cur_tick
sqrt_ratio_a_x_96, // lower_tick
sqrt_ratio_b_x_96, // upper_tick
amount_0, // amount_0
amount_1, // amount_1
)?
.to_i128()
.map_or_else(|| Err(Error::LiquidityAmountTooWide), Ok)?;
if giving {
// If we're giving, then we need to take from the delta.
delta = -delta;
}
#[cfg(feature = "testing-dbg")]
dbg!((
"inside adjust_position",
current_test!(),
sqrt_ratio_x_96.to_string(),
sqrt_ratio_a_x_96.to_string(),
sqrt_ratio_b_x_96.to_string(),
amount_0.to_string(),
amount_1.to_string(),
delta
));
// [update_position] should also ensure that we don't do this on a pool that's not currently
// running
self.update_position(id, delta)
}
While the submission is elaborate and goes into great detail, I do not believe that a tangible issue can manifest from calculating the square root price from the current tick as it should match the current price whenever this particular function is invoked. As such, I would advise a PoC to be introduced for this particular submission.
@0xsomeone - The issue is not a mere use of the price of current_tick instead of the current_sqrt_price. Rather, this report describes how the Superposition protocol uses the wrong Uniswap v3 liquidity math formula.
Difference between current_sqrt_price and price of the current_tick
Uniswap v3 wrote a blog describing the relationship between current_tick, current_tick_price, and current_sqrt_price. The blog states that:
This explains why using ticks can be less precise than sqrtPriceX96 in Uniswap v3.
Just like ticks can be in between initialized ticks, ticks can also be in between integers as well! The protocol itself reports the floor ⌊ic⌋ of the current tick, which the sqrtPriceX96 retains.
To simplify, getting the price of the current_tick would not be equal to getting the current_sqrt_price, as Uniswap v3 stores the current_tick as the floor of the tick derived through the current_sqrt_price:
Both parts lead to the wrong calculation of Delta Liquidity. Finally, this results in the wrong calculation of amount_0 and amount_1 when adjust_position() function calls update_position(&mut self, id: U256, delta: i128) function with the wrongly calculated delta:
let sqrt_ratio_x_96 = tick_math::get_sqrt_ratio_at_tick(self.get_cur_tick().as_i32())?;
This should be replaced with:
let sqrt_ratio_x_96 = self.sqrt_price.get();
With this fix, the correct liquidity formulas are used for both parts. This issue is valid as the sponsor has mentioned faithful use of Uniswap v3 math as a core invariant in the Readme of the audit. This report successfully shows that this core invariant stands broken.
@prapandey031 - I greatly appreciate the in-depth mathematical analysis of the issue; however, a simple deviation of the mathematical formulae implemented by Uniswap V3 is not adequate to consider this an HM risk submission.
I would advise you to quantify the precise error that would result from this flooring mechanism of the tick, as well as produce a PoC that demonstrates the issue (i.e., an execution of the same operations in Uniswap V3 and the SuperPosition system) to illustrate the vulnerability in practice.
While the error is clear, we need to have a quantifiable impact to assess it as an HM risk.
@0xsomeone - I would quantify the funds that Superposition looses due to the explained error as well as detail other impacts.
Below I present calculation PoCs to quantify the impact. I break the complete PoC into 2 parts corresponding to the 2 parts in my previous response.
I build this response on top of my previous response. In my previous response, I proved that Superposition uses wrong liquidity equations. I did this using the in-scope code snippets of Superposition and Uniswapv3 math equations. In this response I calculate values based on the correct and wrong equations to quantify the impact.
To confirm that the Uniswapv3 really uses the equations that I state above (although whatever equations stated above can be found in Uniswapv3 whitepaper; I took all of them from there) I provide the relevant code snippets from Uniswapv3-periphery repo:
addLiquidity(AddLiquidityParams memory params) which is similar to adjust_position() in Superposition:
Notice how Uniswapv3 uses current_sqrt_price instead of price of current_tick at marked (>) LOC above. This is the fix that I proposed in the original report.
getLiquidityForAmounts() which is similar to get_liquidity_for_amounts() in Superposition:
@prapandey031 - thank you for diving deeper into this particular submission! The original submission’s impact indicated that transactions would fail when the minimum amounts are close to the expected amounts; however, your latest feedback indicates that actual fund loss would occur.
Given that the submission must be judged on its original merits and its validity has been demonstrated, I will award this as a medium-severity submission that would cause a portion of the protocol to become inaccessible versus resulting in fund loss, as this is an aspect not described in the original report.
The original report as published on Code4rena, by prapandey031. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the code used the square-root ratio derived from the current tick instead of the stored current square-root price when computing liquidity.
Impact
Transactions to increase liquidity could revert when minimum amounts are close to desired amounts, and the judge noted it could make a portion of the protocol inaccessible.
What the attacker needed
A position in a pool; amount0Min close to amount0Desired
The fix
The code should use the stored current square-root price instead of the tick-derived price.
How it was found
A pool exists with token0, token1, and a position with tick range [0, 5].
The current price is between ticks 0 and 1, equal to 1.00005.
The position owner calls incrPositionC3AC7CAA with amount0Desired and amount0Min close to each other.
The call reaches adjust_position, which sets sqrt_ratio_x_96 from the current tick (0) instead of the actual square-root price.
get_liquidity_for_amounts uses this incorrect value, leading to a calculated amount0 that is below amount0Min.
The transaction reverts.
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.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
In automated market makers like Uniswap, the current price is stored as a square root value, and the 'tick' is a rounded-down version of that price. The code mistakenly used the rounded tick price instead of the precise square-root price when calculating how much liquidity a user gets. This small rounding error can cause the calculated amounts to fall below the user's minimum, making the transaction fail.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.