Open c4-bot-3 opened 8 months ago
GalloDaSballo marked the issue as sufficient quality report
Worth checking as it has an example that looks somewhat realistic
GalloDaSballo marked the issue as primary issue
Perhaps @Foon256 can take a look
trust1995 marked the issue as satisfactory
trust1995 marked the issue as selected for report
For transparency and per conversation with the sponsors, see here for the Wise Lending team's mitigation.
Lines of code
https://github.com/code-423n4/2024-02-wise-lending/blob/main/contracts/MainHelper.sol#L525-L530
Vulnerability details
Impact
Low fee amounts and fee shares are calculated in the preparation part due to the precision loss.
Proof of Concept
Solidity rounds down the result of an integer division, and because of that, it is always recommended to multiply before dividing to avoid that precision loss. In the case of a prior division over multiplication, the final result may face serious precision loss as the first answer would face truncated precision and then multiplied to another integer. The problem arises in the pool's preparation part before applying the LASA algorithm. After cleaning up the pool, the next step is to update the pseudo amounts and adding the corresponding fee shares of that pool.
If we look deeply at the function
_updatePseudoTotalAmounts()
we can see the fee shares calculation procedure is presented as:we can see there is a hidden division before a multiplication that makes round down the whole expression. This is bad as the precision loss can be significant, which leads to the pool printing less
feeAmount
than actual. Also, it is better to mention that some protocols implement this method to have an integer part of the division (usually in time-related situations). But here we can clearly see that this pattern is used in the calculation offeeAmount
at which the precision matters. Furthermore, the mentioned error will escalate, especially when thebareIncrease
is bigger but close to thePRECISION_FACTOR_YEAR
amount. The precision loss becomes more serious at lower discrepancies (such as 1.2 ~ 2 magnitudes ofPRECISION_FACTOR_YEAR
).As the Proof of Concept part, we can check this behavior precisely.
You can run this code to see the difference between the results:
The result would be: (for 1.5 of PRECISION_FACTOR_YEAR)
Thus, we can see that the actual implementation produces less fee amount than the precise method. This test shows a big difference between the two calculated fee amounts in the LASA algorithm.
Tools Used
Manual Review + Forge
Recommended Mitigation Steps
Consider modifying the fee shares calculation to prevent such precision loss and prioritize the multiplication over division:
Assessed type
Math