Division before multiplication could lead to users losing 50% in WithdrawalQueue
The withdrawal calculation divides before multiplying, causing precision loss. Users may receive up to 50% less than they should, with funds locked in the contract.
Originally disclosed on Code4rena
Submitted by minhquanym
In the _getAvailable() function, the calculation performs division before multiplication, which could result in precision loss. The consequence is that users may not be able to withdraw the amount they should receive, leaving some funds locked in the WithdrawalQueue.
// @audit division before multiplication
function _getAvailable(uint256 _tokenId) private view returns (uint256) {
return getShares[_tokenId] * _getWithdrawablePerShare() - getWithdrawn[_tokenId];
}
/// @notice Get the amount that can be withdrawn per share.
function _getWithdrawablePerShare() private view returns (uint256) {
return (_totalWithdrawn + _asset.balanceOf(address(this))) / getTotalShares;
}
Proof of Concept
Consider the following scenario:
getShares[_tokenId] = 1e8
getWithdrawn[_tokenId] = 0
_totalWithdrawn = 0
_asset.balanceOf(address(this)) = 1e9 (1000 USDC)
getTotalShares = 5e8 + 1
The current calculation will yield:
_getWithdrawablePerShare() = 1e9 / (5e8 + 1) = 1
_getAvailable() = 1e8 * 1 - 0 = 1e8 = 100000000
However, the users should actually receive:
getShares[_tokenId] * _asset.balanceOf(address(this)) / getTotalShares
= 1e8 * 1e9 / (5e8 + 1) = 199999999
As shown, the users lose almost 50% of what they should receive.
Recommended Mitigation Steps
Change the order of calculation to multiply before division.
Assessed type
Math
Change order in multiplication/division as suggested.
Status: Mitigation confirmed. Full details in reports from oakcobalt, minhquanym and bin2chen.
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 computes a per-share value by dividing first, then multiplies by the user's shares, so integer division truncates and loses precision.
Impact
Users could lose almost 50% of their entitled withdrawal amount, with the remainder locked in the WithdrawalQueue.
What the attacker needed
None
The fix
The order of calculation was changed to multiply before dividing, as confirmed by the mitigation.
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 use whole numbers, so division can discard remainders. If you divide before multiplying, you lose precision and get a smaller result. Here, that mistake meant users could withdraw much less than they were owed.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.