Transferring quoteToken to the exchange pool contract will cause future liquidity providers to lose funds
The LP token minting calculation uses the contract's actual quoteToken balance instead of the internal reserve, allowing an attacker to donate quoteToken and inflate the balance so future liquidity providers receive fewer LP tokens and lose funds.
Submitted by WatchPug
In the current implementation, the amount of LP tokens to be minted when addLiquidity() is calculated based on the ratio between the amount of newly added quoteToken and the current wallet balance of quoteToken in the Exchange contract.
However, since anyone can transfer quoteToken to the contract, and make the balance of quoteToken to be larger than _internalBalances.quoteTokenReserveQty, existing liquidity providers can take advantage of this by donating quoteToken and make future liquidity providers receive fewer LP tokens than expected and lose funds.
liquidityTokenQty = calculateLiquidityTokenQtyForDoubleAssetEntry(
_totalSupplyOfLiquidityTokens,
quoteTokenQty,
_quoteTokenReserveQty // IERC20(quoteToken).balanceOf(address(this))
);
PoC
Given:
- The
Exchangepool is new; - Alice
addLiquidity()with1e18 baseTokenand1e18 quoteToken, recived1e18LP token; - Alice transfer
99e18 quoteTokento theExchangepool contract; - Bob
addLiquidity()with1e18 baseTokenand1e18 quoteToken; - Bob
removeLiquidity()with all the LP token in balance.
Expected Results: Bob recived 1e18 baseToken and >= 1e18 quoteToken.
Actual Results: Bob recived ~0.02e18 baseToken and ~1e18 quoteToken.
Alice can now removeLiquidity() and recive ~1.98e18 baseToken and ~100e18 quoteToken.
As a result, Bob suffers a fund loss of 0.98e18 baseToken.
Recommendation
Change to:
liquidityTokenQty = calculateLiquidityTokenQtyForDoubleAssetEntry(
_totalSupplyOfLiquidityTokens,
quoteTokenQty,
_internalBalances.quoteTokenReserveQty
);
0xean (ElasticSwap) confirmed and commented:
This does appear to be correct after attempting a POC. Thank you WatchPug!
Alex the Entreprenerd (judge) commented:
The warden identified a way to exploit the protocol math to devalue future liquidity provision at the advantage of early liquidity providers.
The exploit is extractive in nature, however, because this is reliably performable and effectively breaks the protocol’s goals and mechanics, I believe High Severity to be appropriate.
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 LP token minting calculation uses the contract's actual quoteToken balance instead of the internal reserve balance.
Impact
Future liquidity providers could lose funds when adding liquidity after an attacker donates quoteToken to the pool.
What the attacker needed
Attacker is an existing liquidity provider; Attacker can transfer quoteToken to the pool; Victim adds liquidity after the donation
How it was found
- Alice adds liquidity with 1e18 baseToken and 1e18 quoteToken, receiving 1e18 LP tokens.
- Alice transfers 99e18 quoteToken directly to the Exchange pool contract.
- Bob adds liquidity with 1e18 baseToken and 1e18 quoteToken.
- Bob removes all his LP tokens.
- Bob receives only ~0.02e18 baseToken and ~1e18 quoteToken, losing ~0.98e18 baseToken.
- Alice removes her liquidity and receives ~1.98e18 baseToken and ~100e18 quoteToken.
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, you get pool shares (LP tokens) based on how much you put in. The bug is that the pool counts all the money sitting in its account, not just the money it officially tracks. So someone can send extra money directly to the pool, making it look bigger, and the next person who adds money gets fewer shares than they should, losing out.
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.