The value of LP token can be manipulated by the first minister, which allows the attacker to dilute future liquidity providers’ shares
The first liquidity provider of a pool can manipulate the ratio of reserves and the total supply of LP tokens so that later providers receive fewer LP tokens than they should due to rounding. An attacker can profit by taking a larger share of the pool when others add liquidity.
Submitted by WatchPug, also found by camden, danb, and hyh
For the first minter of an Exchange pool, the ratio of X/Y and the totalSupply of the LP token can be manipulated.
A sophisticated attacker can mint and burn all of the LP tokens but 1 Wei, and then artificially create a situation of rebasing up by transferring baseToken to the pool contract. Then addLiquidity() in singleAssetEntry mode.
Due to the special design of singleAssetEntry mode, the value of LP token can be inflated very quickly.
As a result, 1 Wei of LP token can be worthing a significate amount of baseToken and quoteToken.
Combine this with the precision loss when calculating the amount of LP tokens to be minted to the new liquidity provider, the attacker can turn the pool into a trap which will take a certain amount of cut for all future liquidity providers by minting fewer LP tokens to them.
} else {
// this user will set the initial pricing curve
require(
_baseTokenQtyDesired > 0,
"MathLib: INSUFFICIENT_BASE_QTY_DESIRED"
);
require(
_quoteTokenQtyDesired > 0,
"MathLib: INSUFFICIENT_QUOTE_QTY_DESIRED"
);
tokenQtys.baseTokenQty = _baseTokenQtyDesired;
tokenQtys.quoteTokenQty = _quoteTokenQtyDesired;
tokenQtys.liquidityTokenQty = sqrt(
_baseTokenQtyDesired * _quoteTokenQtyDesired
);
_internalBalances.baseTokenReserveQty += tokenQtys.baseTokenQty;
_internalBalances.quoteTokenReserveQty += tokenQtys.quoteTokenQty;
}
function calculateLiquidityTokenQtyForDoubleAssetEntry(
uint256 _totalSupplyOfLiquidityTokens,
uint256 _quoteTokenQty,
uint256 _quoteTokenReserveBalance
) public pure returns (uint256 liquidityTokenQty) {
liquidityTokenQty =
(_quoteTokenQty * _totalSupplyOfLiquidityTokens) /
_quoteTokenReserveBalance;
}
PoC
Given:
- The
Poolis newly created; - The market price of
baseTokenin terms ofquoteTokenis1.
The attacker can do the following steps in one tx:
addLiquidity()with2 Wei of baseTokenand100e18 quoteToken, received14142135623LP tokens;removeLiquidity()with14142135622LP tokens, the Pool state becomes:- totalSupply of LP tokens: 1 Wei
- baseTokenReserveQty: 1 Wei
- quoteTokenReserveQty: 7071067813 Wei
baseToken.transfer()7071067812 Wei to the Pool contract;addLiquidity()with no baseToken and50e18 quoteToken;swapBaseTokenForQuoteToken()with600000000000000 baseToken, the Pool state becomes:- totalSupply of LP tokens: 1 Wei
- quoteTokenReserveQty 591021750159032
- baseTokenReserveQty 600007071067801
baseToken.transfer()999399992928932200 Wei to the Pool contract;addLiquidity()with no baseToken and1e18 quoteToken, the Pool state becomes:- totalSupply of LP tokens: 1 Wei
- quoteTokenReserveQty: 1000000000000000013
- quoteTokenReserveQty: 985024641638342212
- baseTokenDecay: 0
From now on, addLiquidity() with less than 1e18 of baseToken and quoteToken will receive 0 LP token due to precision loss.
The amounts can be manipulated to higher numbers and cause most future liquidity providers to receive fewer LP tokens than expected, and the attacker will be able to profit from it as the attacker will take a larger share of the pool than expected.
Recommendation
Consider requiring a certain amount of minimal LP token amount (eg, 1e8) for the first minter and lock some of the first minter’s LP tokens by minting ~1% of the initial amount to the factory address.
0xean (ElasticSwap) confirmed, but disagreed with High severity and commented:
Thanks for the report, I don’t agree with the severity based on this
From now on, addLiquidity() with less than 1e18 of baseToken and quoteToken will receive 0 LP token due to precision loss.which in your example represents a user trying to add dust to the contract after the attack.
I think we will implement the minimum locked liquidity to avoid rounding errors, but this attack assumes users are adding dust to the contract and that they are totally unaware of the contract state which is incorrect. Users specific a min and a max token qty’s when adding liquidity.
Would recommend med. risk on this one if not low risk given the attack is on “dust” amounts of tokens.
Alex the Entreprenerd (judge) changed severity to Medium and commented:
I agree with the finding and remember coming across it when reading the Yearn V0.4.2 Audit by Trails of Bits.
Ultimately this is contingent on a donation, that will make each share more valuable, so it’s effectively a way to use rounding against making dust donations.
Technically this same idea can be extended to huge donations, however there are very dubious economic reasons as to why you’d do that (perhaps frontrunning a moderate deposit with the goal of using this method to earn that MEV).
Ultimately this is something that can happen anytime you have X shares and Y totalSupply If the total supply reaches greater units than the shares, then integer division will inevitably eat out some of those shares.
Have yet to see a long term solution to this rounding problem, however, a simple initial addition that mints 1e18 shares will require some economic commitment by the potential exploiters.
Agree with medium severity.
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 lack of a minimum initial liquidity and locked tokens allows the first minter to set an extreme ratio and total supply, causing rounding errors for subsequent deposits.
Impact
Future liquidity providers may receive fewer LP tokens than expected, and the attacker could profit by taking a larger share of the pool; however, the attack requires users to add small amounts without checking the pool state, and the judge noted it is contingent on donations and rounding.
What the attacker needed
Attacker is first liquidity provider; Victims add liquidity without checking state
How it was found
- Add liquidity with a tiny amount of base token and a large amount of quote token to receive LP tokens.
- Remove almost all LP tokens, leaving only 1 wei of LP tokens and small reserves.
- Transfer base token directly to the pool to inflate the value of the remaining LP token.
- Add liquidity with only quote token to further inflate the LP token value.
- Perform a swap to adjust reserves and then transfer more base token to the pool.
- Add liquidity again with only quote token, resulting in a state where future small deposits receive 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.
When you add money to a liquidity pool, you get pool tokens (LP tokens) representing your share. The first person to add money sets the initial exchange rate and total number of tokens. If they make the numbers very lopsided, later people who add money may get almost no tokens because of how the maths rounds down. This means the first person can end up owning most of the pool and profit when others join.
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.