The calculation of LP token amount and deposit amount is wrong for edge cases
The report claims that the LP token minting and deposit calculation in Bin.sol uses reversed inequalities for the edge cases where the price is below the lower tick or above the upper tick, which would mint the wrong number of LP tokens and deposit the wrong amounts. The project responded that the code is correct and the white paper contains a typo, and the judge treated the documentation error as a medium severity issue.
Originally disclosed on Code4rena
Submitted by hansfriese
Bin.sol#L35
Bin.sol#L38
Impact
The wrong amount of LP tokens will be minted and the wrong amount of A/B tokens will be deposited.
Proof of Concept
According to the PDF document provided, the number of LP tokens newSupply is calculated using the Table 1 as below.
This is implemented in the function lpTokensFromDeltaReserve() of Bin.sol as below.
function lpTokensFromDeltaReserve(
Bin.Instance storage self,
uint256 _deltaA,
uint256 _deltaB,
int32 _activeLowerTick,
uint256 _existingReserveA,
uint256 _existingReserveB
) internal view returns (uint256 deltaAOptimal, uint256 deltaBOptimal, uint256 proRataLiquidity) {
deltaAOptimal = _deltaA;
deltaBOptimal = _deltaB;
bool noA = self.state.reserveA == 0;
bool noB = self.state.reserveB == 0;
if (self.state.lowerTick < _activeLowerTick || (!noA && noB)) {//@audit case p<p_l, the inequality should be opposite side
deltaBOptimal = 0;
proRataLiquidity = noA || self.state.totalSupply == 0 ? deltaAOptimal : Math.mulDiv(deltaAOptimal, self.state.totalSupply, self.state.reserveA, false);
} else if (self.state.lowerTick > _activeLowerTick || (noA && !noB)) {//@audit case p>p_u, the inequality should be opposite side
deltaAOptimal = 0;
proRataLiquidity = noB || self.state.totalSupply == 0 ? deltaBOptimal : Math.mulDiv(deltaBOptimal, self.state.totalSupply, self.state.reserveB, false);
} else {
if (_existingReserveA > 0) {
deltaBOptimal = Math.mulDiv(_existingReserveB, _deltaA, _existingReserveA, false);
}
if (deltaBOptimal > _deltaB && _existingReserveB > 0) {
deltaAOptimal = Math.mulDiv(_existingReserveA, _deltaB, _existingReserveB, false);
deltaBOptimal = _deltaB;
}
proRataLiquidity = (noA && noB) || self.state.totalSupply == 0
? Math.max(deltaAOptimal, deltaBOptimal)
: Math.min(Math.mulDiv(deltaAOptimal, self.state.totalSupply, self.state.reserveA, false), Math.mulDiv(deltaBOptimal, self.state.totalSupply, self.state.reserveB, false));
}
}
The implementation uses wrong inequalities for the edge cases $p\lt p_l$ and $p\gt p_u$.
Recommended Mitigation Steps
Reverse the inequalities at Bin.sol#L35 and Bin.sol#L38.
gte620v (Maverick) disagreed with severity and commented:
The code is correct and the pdf has a typo. We disagree with the finding that the code needs to be changed (that would severely break things). But we will update the pdf.
There is a bug in the white paper which would have been a critical issue. However, this bug was not replicated in the code base. After discussion with the C4 community, we are currently considering this situation to be a Medium severity issue.
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 written specification (white paper) had the inequalities reversed for the below-lower-tick and above-upper-tick cases, while the code itself was correct.
Impact
If the code had matched the white paper, the wrong amount of LP tokens would be minted and the wrong amounts of tokens deposited, but the project states the deployed code is correct so no such impact occurred.
What the attacker needed
None
The fix
The project said it would update the white paper rather than change the code.
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 lets people deposit two tokens and receive pool shares (LP tokens) in return. This report says the code that decides how many shares to give out uses the wrong comparison in two boundary situations, so users could get the wrong number of shares or deposit the wrong amounts. The project replied that the code is actually right and only the written specification has a mistake.
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.