Lost fees due to precision loss in fees calculation
The fee calculation in a smart contract performs division before all multiplications, causing precision loss due to integer truncation. This results in lost fees, with the report estimating 1% to 7.5% of fees lost depending on parameters.
Submitted by kenzo, also found by 0v3rf10w
In fees calculation, division is being used in the midst of the calculation, not at the end of it. This leads to lost precision in fee amount (as solidity doesn’t save remainder of division). Division should happen at the end to maintain precision.
Impact
Lost fees. The exact amount depends on the parameters set and being tested. According to a few tests I ran, it seems that in normal usage, 1% of fees are lost. In some cases even 7.5% of fees.
Proof of Concept
Division in the midst of a calculation:
uint256 feePct = timeDiff * licenseFee / ONE_YEAR;
uint256 fee = startSupply * feePct / (BASE - feePct);
_mint(publisher, fee * (BASE - factory.ownerSplit()) / BASE);
_mint(Ownable(address(factory)).owner(), fee * factory.ownerSplit() / BASE);
Basket.sol#L140:#L145 It’s a little hard to share a POC script as it involves changing the .sol file so I tested it manually. But after moving the division to the end using the mitigation below, I saw 1%-7% increases in fees minted. Usually 1%.
Recommended Mitigation Steps
We want to firstly do all multiplication and lastly do all the division. So remove the usage of feePct and instead set fee to be:
uint256 fee = startSupply * licenseFee * timeDiff / ONE_YEAR / (BASE - licenseFee);
frank-beard (Kuiper) confirmed
0xleastwood (judge) commented:
Nice find! I think this qualifies as
mediumrisk due to the protocol regularly leaking value. This can be mitigated by performing division at the very end of the fee calculation.
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 integer division was performed before all multiplications were complete, causing truncation of intermediate results and loss of precision.
Impact
The protocol loses fees, with the report estimating 1% lost in normal usage and up to 7.5% in some cases.
What the attacker needed
None
The fix
The fix is to perform all multiplications first and do the division only at the end of the calculation.
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.
In Solidity, when you divide integers, any remainder is thrown away. If you divide early in a calculation, you lose precision and the final result is smaller than it should be. Here, the fee calculation divided too soon, so the protocol minted fewer fee tokens than intended.