Division before Multiplication May Result In No Interest Being Accrued
The interest calculation in NFTVault._calculateAdditionalInterest() performs division before multiplication, so the per-second interest can round down to zero and no interest accrues. For tokens with few decimal places, this understates the interest owed.
Originally disclosed on Code4rena
Submitted by AuditsAreUS, also found by minhquanym
There is a division before multiplication bug in NFTVault._calculateAdditionalInterest() which may result in no interesting being accrued and will have significant rounding issues for tokens with small decimal places.
This issue occurs since an intermediate calculation of interestPerSec may round to zero and therefore the multiplication by elapsedTime may remain zero.
Furthermore, even if interestPerSec > 0 there will still be rounding errors as a result of doing division before multiplication and _calculatedInterest() will be understated.
This issue is significant as one divisor is 365 days = 30,758,400 (excluding the rate). Since many ERC20 tokens such as USDC and USDT only have 6 decimal places a numerator of less 30 * 10^6 will round to zero.
The rate also multiplies into the denominator. e.g. If the rate is 1% then the denominator will be equivalent to 1 / rate * 30 * 10^6 = 3,000 * 10^6.
Proof of Concept
The order of operations for the interest calculations
totalDebtAmount- MUL
settings.debtInterestApr.numerator - DIV
settings.debtInterestApr.denominator - DIV
365 days - MUL
elapsedTime
If the intermediate value of interestPerSec = 0 then the multiplication by elapsedTime will still be zero and no interested will be accrued.
Excerpt from NFTVault._calculateAdditionalInterest().
uint256 interestPerYear = (totalDebtAmount *
settings.debtInterestApr.numerator) /
settings.debtInterestApr.denominator;
uint256 interestPerSec = interestPerYear / 365 days;
return elapsedTime * interestPerSec;
Recommended Mitigation Steps
This issue may be resolved by performing the multiplication by elapsedTime before the division by the denominator or 365 days.
uint256 interestAccrued = (elapsedTime *
totalDebtAmount *
settings.debtInterestApr.numerator) /
settings.debtInterestApr.denominator /
365 days;
return interestAccrued;
spaghettieth (JPEG’d) confirmed
spaghettieth (JPEG’d) resolved and commented:
Fixed jpegd/core#8.
This report makes sense as a medium to me because it involves a calculation error that can lead to the protocol functioning incorrectly.
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 the annual rate denominator and by the number of seconds in a year before multiplying by elapsed time, causing integer truncation to zero.
Impact
For tokens with small decimal places, the interest accrued may be zero or understated, so the protocol could lose interest revenue.
What the attacker needed
Token with few decimal places; small debt amount or low interest rate
The fix
The issue was fixed by reordering the calculation to multiply by elapsed time before dividing by the denominator and 365 days.
How it was found
- The contract calculates interest per year by multiplying total debt by the APR numerator and dividing by the denominator.
- It then divides that yearly interest by 365 days to get interest per second.
- Finally it multiplies the per-second interest by elapsed time to get additional interest.
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 smart contract calculates interest, it divides numbers to get a per-second rate. If that division happens too early, the result can be zero because the numbers are small. Then multiplying by time still gives zero, so no interest is charged.