Liquidity providers may lose funds when adding liquidity
When adding liquidity to a two-token pool, the contract mints LP tokens using the smaller of the two proportional shares, so any imbalance between the deposited token amounts is left in the pool as a donation. A liquidity provider who deposits at a skewed ratio therefore receives fewer LP tokens than their deposit is worth and loses part of their funds on withdrawal.
Jeiwan Follow with minhtrng, minhquanym and 25 others
Originally disclosed on Code4rena
Submitted by Jeiwan, also found by minhtrng, minhquanym, HE1M, wait, hansfriese, BAHOZ, unforgiven, 0xxm, Junnon, bytehat, UNCHAIN, carlitox477, RaymondFam, Chom, CRYP70, 9svR6w, mauricio1802, 141345, hihen, caventa, koxuan, obront, nicobevi, shung, cccz, Bobface, and chaduke
Liquidity providers may lose a portion of provided liquidity in either of the pair tokens. While the minLpTokenAmount protects from slippage when adding liquidity, it doesn’t protect from providing liquidity at different K.
Proof of Concept
The Pair contract is designed to receive liquidity from liquidity providers (Pair.sol#L63). First liquidity provider in a pool may provide arbitrary token amounts and set the initial price (Pair.sol#L425-L426), but all other liquidity providers must provide liquidity proportionally to current pool reserves (Pair.sol#L420-L423). Since a pool is made of two tokens and liquidity is provided in both tokens, there’s a possibility for a discrepancy: token amounts may be provided in different proportions. When this happens, the smaller of the proportions is chosen to calculate the amount of LP tokens minted (Pair.sol#L420-L423):
// calculate amount of lp tokens as a fraction of existing reserves
uint256 baseTokenShare = (baseTokenAmount * lpTokenSupply) / baseTokenReserves();
uint256 fractionalTokenShare = (fractionalTokenAmount * lpTokenSupply) / fractionalTokenReserves();
return Math.min(baseTokenShare, fractionalTokenShare);
As a result, the difference in proportions will create an excess of tokens that won’t be redeemable for the amount of LP tokens minted. The excess of tokens gets, basically, donated to the pool: it’ll be shared among all liquidity providers of the pool. While the minLpTokenAmount argument of the add function (Pair.sol#L63) allows liquidity providers to set the minimal amount of LP tokens they want to receive, it doesn’t allow them to minimize the disproportion of token amounts or avoid it at all.
// test/Pair/unit.Add.t.sol
function testLockOfFunds_AUDIT() public {
address alice = address(0x31337);
address bob = address(0x12345);
vm.label(alice, "alice");
vm.label(bob, "bob");
deal(address(usd), alice, 100e18, true);
deal(address(usd), bob, 100e18, true);
deal(address(p), alice, 100e18, true);
deal(address(p), bob, 100e18, true);
// Alice is the first liquidity provider.
vm.startPrank(alice);
usd.approve(address(p), type(uint256).max);
p.add(10 ether, 10 ether, 0);
vm.stopPrank();
// Bob provides liquidity to the pool and sets the minimal LP amount.
// The token amounts are deposited in different proportions, thus the smaller
// one will be chosen to calculate the amount of LP tokens Bob will receive.
vm.startPrank(bob);
usd.approve(address(p), type(uint256).max);
uint256 minLPAmount = 1e18;
uint256 bobLPAmount = p.add(1.2 ether, 1 ether, minLPAmount);
vm.stopPrank();
// Bob has received the minimal LP amount he wanted.
assertEq(bobLPAmount, minLPAmount);
// However, after removing all his liquidity from the pool...
(uint256 bobUSDBefore, uint256 bobFracBefore) = (usd.balanceOf(bob), p.balanceOf(bob));
vm.prank(bob);
p.remove(minLPAmount, 0, 0);
(uint256 bobUSDAfter, uint256 bobFracAfter) = (usd.balanceOf(bob), p.balanceOf(bob));
// ... Bob received less USD than he deposited.
assertEq(bobUSDAfter - bobUSDBefore, 1.018181818181818181 ether);
assertEq(bobFracAfter - bobFracBefore, 1.000000000000000000 ether);
}
Recommended Mitigation Steps
In the add function, consider calculating optimal token amounts based on the amounts specified by user, current pool reserves, and the minimal LP tokens amount specified by user. As a reference, consider this piece from the Uniswap V2 Router: UniswapV2Router02.sol#L45-L60.
outdoteth (Caviar) confirmed and commented:
Fixed in: https://github.com/outdoteth/caviar/pull/2
By allowing a user to specify a
minPriceandmaxPricethat they are willing to LP at along with theminLpTokenAmountthat they would like to receive. The price calculation is based on this: https://github.com/outdoteth/caviar/blob/main/src/Pair.sol#L471
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
The add function mints LP tokens from the minimum of the two proportional shares without adjusting the deposited amounts to the pool's current ratio, so the surplus side is silently absorbed by the pool.
Impact
A liquidity provider could lose a portion of the deposited tokens because the excess is shared among all pool participants; the minLpTokenAmount check does not prevent this.
What the attacker needed
A deployed two-token liquidity pool; caller supplies both tokens to add liquidity
The fix
The add function was fixed by letting users specify a minimum and maximum price they are willing to provide liquidity at, alongside the minimum LP token amount.
How it was found
- The first provider seeds the pool with equal amounts of both tokens, setting the initial price.
- A second provider adds liquidity with token amounts in a different proportion and sets a minimum LP token amount.
- The contract mints LP tokens based on the smaller proportional share, so the minimum is met.
- The provider removes all their liquidity and receives less of one token than they deposited.
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 liquidity pool holds two tokens and gives out shares (LP tokens) to people who deposit both. The contract works out how many shares to give by comparing each token's deposit against the pool's existing balance and taking the smaller result. If you deposit the two tokens in a lopsided ratio, the extra amount of the larger side is effectively given to the pool and you cannot get it back.
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.