There is a large precision error in sqrt calculation of lp
The square root calculation used to determine LP token supply has a larger precision error than division, causing excess reserve tokens to remain in the contract when users add liquidity with imbalanced reserves. An attacker could monitor the contract and withdraw these accumulated excess reserves via removeLiquidityImbalanced.
Submitted by kutugu
Compared with div, there is a larger precision error in calculating lp through sqrt, so there should be a way to check whether there are excess tokens left when adding liquidity.
Proof of Concept
function testCalcLpTokenSupplyDiff() public {
uint256[] memory reserves = new uint256[](2);
reserves[0] = 1e24 + 1e4;
reserves[1] = 10000;
uint256 lp1 = this.calcLpTokenSupply(reserves, bytes(""));
reserves[0] = 1e24;
reserves[1] = 10000;
uint256 lp2 = this.calcLpTokenSupply(reserves, bytes(""));
assert(lp1 == lp2);
}
When reserve[0] is larger relative to reserve[1], the accuracy error is larger, and unlike div, the accuracy error is only 1, the accuracy error of sqrt is larger. When the user input the imbalance the amount will left excess reserve, searchers will monitor the contract after the excess reserve accumulation to a certain degree, will withdraw them by removeLiquidityImbalanced.
Recommended Mitigation Steps
Excess reserve tokens should be returned when the user adds liquidity
publiuss (Basin) acknowledged and commented:
This is a known issue. The documentation should be updated.
This was appended to the documentation here.
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 square root function used in LP token supply calculation introduces a larger rounding error than division, and the add-liquidity logic does not return the excess reserves to the user.
Impact
Excess reserve tokens could accumulate in the contract and be withdrawn by searchers via removeLiquidityImbalanced, potentially causing a loss to liquidity providers.
What the attacker needed
User adds liquidity with imbalanced reserves; attacker monitors contract for excess reserves
The fix
The project acknowledged the issue as known and updated the documentation, but the code was not changed; the recommended mitigation is to return excess reserve tokens when a user adds liquidity.
How it was found
- Set reserves to [1e24 + 1e4, 10000] and calculate LP token supply.
- Set reserves to [1e24, 10000] and calculate LP token supply.
- Observe that both calculations return the same LP token supply, meaning the extra 1e4 tokens are not accounted for.
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 liquidity to a pool, the pool gives you LP tokens representing your share. The pool calculates how many LP tokens to give using a square root function, which can round down and leave a tiny extra amount of your tokens in the pool. Over time, these extra tokens add up, and someone can later withdraw them for profit.