Division before multiplication incurs unnecessary precision loss
Interest accrual and invariant calculations in the protocol perform division before multiplication, so intermediate values are truncated and precision is lost. When liquidity is large, the scaled amount can round down to zero, making the computed values inaccurate.
Submitted by ladboy233, also found by Breeje
src/core/Pair.sol#L56 src/core/Pair.sol#L57 core/Lendgine.sol#L252
Proof of Concept
In the current codebase, FullMath.mulDiv is used, the function takes three parameters.
Basically FullMath.mulDIv(a, b, c) means a * b / c.
Then there are some operations which incur unnecessary precision loss because of division before multiplcation.
When accruing interest, the code below:
/// @notice Helper function for accruing lendgine interest
function _accrueInterest() private {
if (totalSupply == 0 || totalLiquidityBorrowed == 0) {
lastUpdate = block.timestamp;
return;
}
uint256 timeElapsed = block.timestamp - lastUpdate;
if (timeElapsed == 0) return;
uint256 _totalLiquidityBorrowed = totalLiquidityBorrowed; // SLOAD
uint256 totalLiquiditySupplied = totalLiquidity + _totalLiquidityBorrowed; // SLOAD
uint256 borrowRate = getBorrowRate(_totalLiquidityBorrowed, totalLiquiditySupplied);
uint256 dilutionLPRequested = (FullMath.mulDiv(borrowRate, _totalLiquidityBorrowed, 1e18) * timeElapsed) / 365 days;
uint256 dilutionLP = dilutionLPRequested > _totalLiquidityBorrowed ? _totalLiquidityBorrowed : dilutionLPRequested;
uint256 dilutionSpeculative = convertLiquidityToCollateral(dilutionLP);
totalLiquidityBorrowed = _totalLiquidityBorrowed - dilutionLP;
rewardPerPositionStored += FullMath.mulDiv(dilutionSpeculative, 1e18, totalPositionSize);
lastUpdate = block.timestamp;
emit AccrueInterest(timeElapsed, dilutionSpeculative, dilutionLP);
}
Note the line:
uint256 dilutionLPRequested = (FullMath.mulDiv(borrowRate, _totalLiquidityBorrowed, 1e18) * timeElapsed) / 365 days;
This basically equals to dilutionLPRequested = (borrowRate * totalLiquidityBorrowed / 1e18 * timeElapsed) / 365 days
The first part of division can greatly truncate the value borrowRate * totalLiquidityBorrowed / 1e18, the totalLiquidityBorrowed should be normalized and scaled by token precision when adding liqudity instead of division by 1e18 here.
Same preicision loss happens when computng the invariant
/// @inheritdoc IPair
function invariant(uint256 amount0, uint256 amount1, uint256 liquidity) public view override returns (bool) {
if (liquidity == 0) return (amount0 == 0 && amount1 == 0);
uint256 scale0 = FullMath.mulDiv(amount0, 1e18, liquidity) * token0Scale;
uint256 scale1 = FullMath.mulDiv(amount1, 1e18, liquidity) * token1Scale;
scale0 = (amount0 * 1e18 / liqudiity) * token0Scale
scale1 = (amount1 * 1e18 / liqudiity) * token1Scale
Whereas the amount0 and amount1 should be first be normalized by token0Scale and token1Scale and then divided by liquidity at last. If the liquidity is a larger number amount0 * 1e18 / liqudity is already truncated to 0.
Recommended Mitigation Steps
We recommend the protocol avoid divison before multiplication and always perform division operation at last.
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 code divides by 1e18 or by liquidity before multiplying by the time elapsed or token scale, so integer truncation happens too early.
Impact
Interest accrual and invariant checks could be computed inaccurately, and with large liquidity the scaled amount may truncate to zero.
What the attacker needed
None
The fix
The protocol should reorder the arithmetic so all multiplications happen before any division.
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.
Smart contracts store numbers as whole integers, so any division throws away the remainder. If you divide early and multiply later, that lost remainder gets multiplied too, producing a noticeably wrong result. Here the code divides before multiplying, so small amounts can shrink to zero.