First depositor can break minting of shares
The pair contract mints LP shares using a formula that rounds down, so an attacker who seeds the pool with a tiny amount and then donates a large amount directly to the pair can make each share extremely valuable. Later depositors adding less than the inflated reserve then receive zero LP tokens for their deposit.
minhquanym Follow with Apocalypto, 0xDecorativePineapple and 40 others
Originally disclosed on Code4rena
Submitted by minhquanym, also found by Apocalypto, 0xDecorativePineapple, Franfran, dipp, rjs, ak1, Tricko, Jeiwan, unforgiven, hansfriese, BAHOZ, unforgiven, bytehat, UNCHAIN, immeas, SamGMK, fs0c, Tointer, haku, Koolex, 141345, ElKu, rajatbeladiya, hihen, izhelyazkov, KingNFT, koxuan, 0x52, carrotsmuggler, yixxas, HE1M, supernova, cozzetti, rvierdiiev, SamGMK, aviggiano, seyni, lumoswiz, ladboy233, chaduke, cccz, and eyexploit
The attack vector and impact is the same as TOB-YEARN-003, where users may not receive shares in exchange for their deposits if the total asset amount has been manipulated through a large “donation”.
Proof of Concept
In Pair.add(), the amount of LP token minted is calculated as
function addQuote(uint256 baseTokenAmount, uint256 fractionalTokenAmount) public view returns (uint256) {
uint256 lpTokenSupply = lpToken.totalSupply();
if (lpTokenSupply > 0) {
// 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);
} else {
// if there is no liquidity then init
return Math.sqrt(baseTokenAmount * fractionalTokenAmount);
}
}
An attacker can exploit using these steps
- Create and add
1 wei baseToken - 1 wei quoteTokento the pair. At this moment, attacker is minted1 wei LP tokenbecausesqrt(1 * 1) = 1 - Transfer large amount of
baseTokenandquoteTokendirectly to the pair, such as1e9 baseToken - 1e9 quoteToken. Since no new LP token is minted,1 wei LP tokenworths1e9 baseToken - 1e9 quoteToken. - Normal users add liquidity to pool will receive
0LP token if they add less than1e9token because of rounding division.
baseTokenShare = (X * 1) / 1e9;
fractionalTokenShare = (Y * 1) / 1e9;
Recommended Mitigation Steps
- Uniswap V2 solved this problem by sending the first 1000 LP tokens to the zero address. The same can be done in this case i.e. when
lpTokenSupply == 0, send the first min liquidity LP tokens to the zero address to enable share dilution. - In
add(), ensure the number of LP tokens to be minted is non-zero:
require(lpTokenAmount != 0, "No LP minted");
outdoteth (Caviar) confirmed and commented:
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 share calculation divides by reserves that can be inflated by direct transfers without minting new shares, and the result is not checked to be non-zero.
Impact
Users could deposit tokens and receive no shares in return, losing the value of their deposit to the attacker's inflated share.
What the attacker needed
Attacker acts first on a new pair; attacker can transfer tokens directly to pair
The fix
It was fixed by sending the first minimum liquidity LP tokens to the zero address when supply is zero and requiring that the minted LP amount is non-zero.
How it was found
- Create a pair and add a minimal amount of each token, receiving one wei of LP token.
- Transfer a large amount of both tokens directly to the pair so the existing single share becomes very valuable.
- Observe that a normal user depositing less than the inflated reserve receives zero LP tokens due to rounding.
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 is a shared pot where people deposit two tokens and get shares (LP tokens) representing their portion. Shares are calculated by dividing the deposit by the pool's total reserves, and whole-number maths rounds down. If someone makes the pool's reserves huge while the number of shares stays tiny, a normal deposit divided by that huge reserve rounds to zero, so the depositor gets nothing back for their tokens.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.