Skip to content
Smart contracts Audit finding Advanced

Unsafe cast in ConcentratedLiquidityPool.burn leads to attack

The burn function in ConcentratedLiquidityPool casts a user-supplied uint128 amount to a signed int128 without checking, so a large value like 2^128-1 becomes -1. This lets an attacker mint LP tokens for free and redeem an extremely large amount of pool tokens.

cmichel Follow

Originally disclosed on Code4rena

Submitted by cmichel, also found by broccoli

The ConcentratedLiquidityPool.burn function performs an unsafe cast of a uint128 type to a signed integer.

(uint256 amount0fees, uint256 amount1fees) = _updatePosition(msg.sender, lower, upper, -int128(amount));

Note that amount is chosen by the caller and when choosing amount = 2**128 - 1, this is interpreted as 0xFFFFFFFFF... = -1 as a signed integer. Thus -(-1)=1 adds 1 liquidity unit to the position

This allows an attacker to not only mint LP tokens for free but as this is the burn function it also redeems token0/1 amounts according to the unmodified uint128 amount which is an extremely large value.

POC

I created this POC that implements a hardhat test and shows how to steal the pool tokens.

Choosing the correct amount of liquidity to burn and lower, upper ticks is not straight-forward because of two competing constraints:

  1. the -int128(amount) must be less than MAX_TICK_LIQUIDITY (see _updatePosition). This drives the the amount up to its max value (as the max uint128 value is -1 => -(-1)=1 is very low)
  2. The redeemed amount0, amount1 values must be less than the current pool balance as the transfers would otherwise fail. This drives the amount down. However, by choosing a smart lower and upper tick range we can redeem fewer tokens for the same liquidity.

This example shows how to steal 99% of the token0 pool reserves:

Impact

An attacker can steal the pool tokens.

Recommended Mitigation Steps

Even though Solidity 0.8.x is used, type casts do not throw an error. A SafeCast library must be used everywhere a typecast is done.

sarangparikh22 (Sushi) confirmed

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

Root cause

Likely the code casts a uint128 to int128 without checking for overflow, so values above the signed maximum wrap to negative numbers.

Impact

An attacker could steal most of the pool's token0 reserves, as demonstrated by a proof of concept that steals 99%.

What the attacker needed

Access to the burn function; ability to choose amount and tick range

The fix

A SafeCast library should be used for all type casts to prevent unsafe conversions.

How it was found

  1. Choose an amount of liquidity to burn that is close to the maximum uint128 value.
  2. Select lower and upper tick values so that the redeemed token amounts stay below the pool balance.
  3. Call the burn function with these parameters to receive an extremely large amount of pool tokens.

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 function that burns liquidity mistakenly treats a very large unsigned number as a small negative number. Because of this, an attacker can pretend to burn a huge amount and receive a huge amount of the pool's tokens without actually providing them.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by cmichel

View all

196 more disclosed reports, best first.

More from Sushi Trident contest phase 2

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.