Precision loss in the calculation of the fee amounts and fee shares inside the _preparePool function of the MainHelper contract
The fee calculation in the _preparePool function performs division before multiplication, causing precision loss and underreporting of fee amounts. An attacker could exploit this to reduce fees owed to the pool.
Submitted by Matin
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:
uint256 amountInterest = bareIncrease
/ PRECISION_FACTOR_YEAR;
uint256 feeAmount = amountInterest
* globalPoolData[_poolToken].poolFee
/ PRECISION_FACTOR_E18;
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 we can clearly see that this pattern is used in the calculation of feeAmount at which the precision matters. Furthermore, the mentioned error will escalate, especially when the bareIncrease is bigger but close to the PRECISION_FACTOR_YEAR amount. The precision loss becomes more serious at lower discrepancies (such as 1.2 ~ 2 magnitudes of PRECISION_FACTOR_YEAR).
As for the Proof of Concept part, we can check this behavior precisely. You can run this code to see the difference between the results:
function test_precissionLoss() public {
uint x = (PRECISION_FACTOR_YEAR * 3)/ 2; // This number represents the `bareIncrease`
uint256 poolFee = 789600000000000000000000; // This number represents the `poolFee`
uint256 amountInterest = x
/ PRECISION_FACTOR_YEAR;
uint256 feeAmount1 = amountInterest
* poolFee
/ PRECISION_FACTOR_E18;
uint256 feeAmount2 = (x * poolFee) / (PRECISION_FACTOR_YEAR * PRECISION_FACTOR_E18);
console.log("Current Implementation ", feeAmount1);
console.log("Actual Implementation ", feeAmount2);
}
The result would be: (for 1.5 of PRECISION_FACTOR_YEAR):
Current Implementation 789600
Actual Implementation 1184400
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
Forge
Recommended Mitigation Steps
Consider modifying the fee shares calculation to prevent such precision loss and prioritize the multiplication over division:
uint256 amountInterest = bareIncrease
/ PRECISION_FACTOR_YEAR;
uint256 feeAmount = bareIncrease
* globalPoolData[_poolToken].poolFee
/ (PRECISION_FACTOR_E18 * PRECISION_FACTOR_YEAR);
Assessed type
Math
Mitigated 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 fee calculation performs integer division before multiplication, causing truncation and precision loss.
Impact
The pool receives less fee amount than intended, which could be exploited to reduce fees.
What the attacker needed
None
The fix
The fee calculation should be modified to multiply before dividing to prevent precision loss.
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 a computer divides whole numbers, it throws away any remainder. If you divide first and then multiply, you lose precision and get a smaller result than if you multiplied first and then divided. Here, the code divides before multiplying, so the calculated fee is lower than it should be.