Skip to content
Business logic Audit finding Intermediate

When price is within position’s range, deposit at TokenisableRange can cause loss of funds

The deposit function in TokenisableRange calculates fees based on the current pool price, which can change between transaction submission and execution. If the price moves outside the position's range, the user may be charged an excessive fee, potentially losing all of one token.

xuwinnie Follow

Originally disclosed on Code4rena

Submitted by xuwinnie

When slot0 price is within the range of tokenized position, function deposit needs to be called with both parameters, n0 and n1, greater than zero. However, if price moves outside the range during the transaction, user will be charged an excessive fee.

Proof of Concept

if ( fee0+fee1 > 0 && ( n0 > 0 || fee0 == 0) && ( n1 > 0 || fee1 == 0 ) ){
  address pool = V3_FACTORY.getPool(address(TOKEN0.token), address(TOKEN1.token), feeTier * 100);
  (uint160 sqrtPriceX96,,,,,,)  = IUniswapV3Pool(pool).slot0();
  (uint256 token0Amount, uint256 token1Amount) = LiquidityAmounts.getAmountsForLiquidity( sqrtPriceX96, TickMath.getSqrtRatioAtTick(lowerTick), TickMath.getSqrtRatioAtTick(upperTick), liquidity);
  if (token0Amount + fee0 > 0) newFee0 = n0 * fee0 / (token0Amount + fee0);
  if (token1Amount + fee1 > 0) newFee1 = n1 * fee1 / (token1Amount + fee1);
  fee0 += newFee0;
  fee1 += newFee1;
  n0   -= newFee0;
  n1   -= newFee1;
}

Suppose range is [120, 122] and current price is 121. Alice calls deposit with {n0: 100, n1:100}, if Price moves to 119 during execution (due to market fluctuations or malicious frontrunning), getAmountsForLiquidity will return 0 for token1Amount. As a result, newFee1 will be equal to n1, which means all the 100 token1 will be charged as fee.

(uint128 newLiquidity, uint256 added0, uint256 added1) = POS_MGR.increaseLiquidity(
  INonfungiblePositionManager.IncreaseLiquidityParams({
    tokenId: tokenId,
    amount0Desired: n0,
    amount1Desired: n1,
    amount0Min: n0 * 95 / 100,
    amount1Min: n1 * 95 / 100,
    deadline: block.timestamp
  })
);

Then, increaseLiquidity will succeed since amount1Min is now zero.

Recommended Mitigation Steps

Don’t use this to calculate fee:

if ( fee0+fee1 > 0 && ( n0 > 0 || fee0 == 0) && ( n1 > 0 || fee1 == 0 ) ){
  address pool = V3_FACTORY.getPool(address(TOKEN0.token), address(TOKEN1.token), feeTier * 100);
  (uint160 sqrtPriceX96,,,,,,)  = IUniswapV3Pool(pool).slot0();
  (uint256 token0Amount, uint256 token1Amount) = LiquidityAmounts.getAmountsForLiquidity( sqrtPriceX96, TickMath.getSqrtRatioAtTick(lowerTick), TickMath.getSqrtRatioAtTick(upperTick), liquidity);
  if (token0Amount + fee0 > 0) newFee0 = n0 * fee0 / (token0Amount + fee0);
  if (token1Amount + fee1 > 0) newFee1 = n1 * fee1 / (token1Amount + fee1);
  fee0 += newFee0;
  fee1 += newFee1;
  n0   -= newFee0;
  n1   -= newFee1;
}

Always use this:

  uint256 TOKEN0_PRICE = ORACLE.getAssetPrice(address(TOKEN0.token));
  uint256 TOKEN1_PRICE = ORACLE.getAssetPrice(address(TOKEN1.token));
  require (TOKEN0_PRICE > 0 && TOKEN1_PRICE > 0, "Invalid Oracle Price");
  // Calculate the equivalent liquidity amount of the non-yet compounded fees
  // Assume linearity for liquidity in same tick range; calculate feeLiquidity equivalent and consider it part of base liquidity
  feeLiquidity = newLiquidity * ( (fee0 * TOKEN0_PRICE / 10 ** TOKEN0.decimals) + (fee1 * TOKEN1_PRICE / 10 ** TOKEN1.decimals) )
                                / ( (added0   * TOKEN0_PRICE / 10 ** TOKEN0.decimals) + (added1   * TOKEN1_PRICE / 10 ** TOKEN1.decimals) );

Keref (Good Entry) disputed and commented:

Again this concurrency execution environment stuff. There is no price moving “during” execution.

xuwinnie (Warden) commented:

Again this concurrency execution environment stuff. There is no price moving “during” execution.

Hi @Keref, I guess there could be some misunderstanding. Here I mean when price is 121, user will need to submit the tx with {n0: 100, n1:100}, and price could move to 119 when tx gets executed. (something similar to slippage)

Keref (Good Entry) confirmed and commented:

Hi, sorry I misunderstood the report, accepted.

See PR#4

Good Entry Mitigated:

Remove complex fee clawing strategy. PR: https://github.com/GoodEntry-io/ge/pull/4

Status: Mitigation confirmed. Full details in reports from kutugu, xuwinnie and 3docSec.


The original report as published on Code4rena, by xuwinnie. 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.

How it worked

Impact

A user could lose a significant portion of their deposit, potentially all of one token, due to an excessive fee when the price moves outside the position's range during transaction execution.

What the attacker needed

Price within position range at submission; price moves outside range before execution

The fix

The fee calculation should use oracle prices instead of the pool's current price to avoid manipulation and price movement issues.

How it was found

  1. User calls deposit with both n0 and n1 greater than zero while the price is within the position's range.
  2. The price moves outside the range before the transaction is executed.
  3. The fee calculation uses the new price, causing one of the token amounts to be zero and the entire deposit of that token to be taken as fee.
  4. The increaseLiquidity call succeeds because the minimum amount check passes with zero.

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.

When you add money to a liquidity pool, the system charges a fee based on the current price. If the price changes before your transaction is processed, the fee calculation can go wrong and take too much of your deposit. This report shows how a user could lose all of one token because the fee was calculated using an outdated price.

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.

More by xuwinnie

View all

12 more disclosed reports, best first.

More from Good Entry

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.