Incorrect usage of typecasting in _getAmountsForLiquidity lets an attacker steal funds from the pool
The concentrated liquidity pool contract casts the results of its maths helpers from a 256-bit integer down to a 128-bit integer without checking for overflow. An attacker could craft mint parameters so the amount they must pay is truncated to a tiny value while they receive a huge amount of liquidity, which they could then burn to take other users' funds.
Submitted by broccoli
Impact
The _getAmountsForLiquidity function of ConcentratedLiquidityPool explicitly converts the result of DyDxMath.getDy and DyDxMath.getDx from type uint256 to type uint128. The explicit casting without checking whether the integer exceeds the maximum number (i.e., type(uint128).max) could cause incorrect results being used. Specifically, an attacker could exploit this bug to mint a large amount of liquidity but only pay a little of token0 or token1 to the pool and effectively steal other’s funds when burning his liquidity.
Proof of Concept
- Suppose that the current price is at the tick
500000, an attacker calls themintfunction with the following parameters:
mintParams.lower = 100000
mintParams.upper = 500000
mintParams.amount1Desired = (1 << 128) + 71914955423 # a carefully chosen number
mintParams.amount0Desired = 0
- Since the current price is equal to the upper price, we have
_liquidity = mintParams.amount1Desired * (1 << 96) // (priceUpper - priceLower)
= 4731732988155153573010127840
- The amounts of
token0andtoken1that the attacker has to pay is
amount0Actual = 0
amount1Actual = uint128(DyDxMath.getDy(_liquidity, priceLower, priceUpper, true))
= uint128(_liquidity * (priceUpper - priceLower) // (1 << 96)) # round up
= uint128(340282366920938463463374607456141861046) # exceed the max
= 24373649590 # truncated
- The attacker only pays
24373649590oftoken1to get4731732988155153573010127840of the liquidity, which he could burn to get moretoken1. As a result, the attacker is stealing the funds from the pool and could potentially drain it.
Referenced code:
- ConcentratedLiquidityPool.sol#L480
- concentratedPool/DyDxMath.sol#L15
- concentratedPool/DyDxMath.sol#L30
Recommended Mitigation Steps
Check whether the result of DyDxMath.getDy or DyDxMath.getDx exceeds type(uint128).max or not. If so, then revert the transaction. Or consider using the SafeCast library from OpenZeppelin instead.
sarangparikh22 (Sushi) disputed:
The example is wrong, you can’t add use upper tick as odd, correct the example and resubmit please.
@sarangparikh22 (Sushi), could you confirm whether the casting to uint128 is known to be safe? Are you unconvinced of the issue?
sarangparikh22 (Sushi) confirmed:
@alcueca (judge) I can confirm casting to uint128 is not safe, and will lead to overflow. However, the example mentioned is wrong.
Understood. I will uphold the severity 3 because the overflow happens in a critical function for the management of funds and an incorrect execution will likely lead to loss of funds.
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 the code explicitly downcasts the uint256 output of the DyDxMath helpers to uint128 without checking that it fits, so an oversized value is silently truncated.
Impact
An attacker could mint a large liquidity position for a tiny payment and then burn it to withdraw more than they paid, potentially draining the pool; the project confirmed the cast is unsafe but disputed the specific example.
What the attacker needed
Ability to call the pool's mint function; pool holds token1 liquidity to drain
The fix
The result of DyDxMath.getDy or DyDxMath.getDx should be checked against type(uint128).max and the transaction reverted if it exceeds it, or a safe casting library should be used instead.
How it was found
- Choose mint parameters whose upper tick equals the current price and whose desired token1 amount is a carefully chosen value just above the 128-bit limit.
- The pool computes the liquidity from that desired amount.
- The pool computes the token1 amount owed via DyDxMath.getDy and casts it to uint128, truncating the oversized result to a small number.
- The attacker pays only the truncated amount and receives the large liquidity position.
- The attacker burns the position to withdraw more token1 than they paid, taking funds from the pool.
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 numbers in fixed-size boxes. If a calculation produces a number too big for the smaller box, the extra digits are silently thrown away, so a huge amount can become a tiny one. Here the pool used the truncated tiny amount as the price the attacker had to pay, letting them buy far more of the pool's share than they paid for.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.