accrue interest function is likely failed to accrue interest for token with low decimal
The interest accrual calculation divides by 365 days and a hardcoded scale factor before multiplying, causing the result to round down to zero for tokens with low decimals and short accrual intervals. An attacker could repeatedly call accrue to avoid paying interest, or interest would simply not accrue for small borrow amounts.
Submitted by ladboy233
Loss of precision is too high when accruing interest
Proof of Concept
When interest accrues, we are calling
uint256 interestAmount;
{
uint256 interestRate = IIRM(irm).getInterestRate(address(this), trancheIndex, totalDeposit, totalBorrow);
interestAmount = (trancheBorrowAmount_ * interestRate * timePassed) / 365 days / IRM_SCALE;
}
Note that the loss of precision is too high:
interestAmount = (trancheBorrowAmount_ * interestRate * timePassed) / 365 days / IRM_SCALE;
We are dividing 365 days and then divde by IRM_SCALE (which is 1e9)
Combing the fact that the IRM_SCALE is hardcoded to 1e9
Also, the time passed between two accruing is small,
Also, the underlying tokens have low decimals (6 decimals, even two decimals).
The interest accrued will be always rounded to 0.
In tests/MockERC20.sol,
The decimal is hardcoded to 18
If we change the hardcode decimal to 6 and we add the POC below in TestOmniToken.t.sol
function test_Accrue_POC_Low_decimal() public {
setUpBorrow();
(uint256 tda0, uint256 tba0,,) = oToken.tranches(0);
(uint256 tda1, uint256 tba1,,) = oToken.tranches(1);
(uint256 tda2, uint256 tba2,,) = oToken.tranches(2);
uint256 td = tda0 + tda1 + tda2;
uint256 borrowAmount = 10 * (10 ** uToken.decimals());
IOmniPool(pool).borrow(0, address(oToken), borrowAmount);
uint256 time_elapse = 120 seconds;
vm.warp(time_elapse);
uint256 interestAmount;
{
uint256 interestRate = irm.getInterestRate(address(oToken), 0, td, borrowAmount);
interestAmount = borrowAmount * interestRate * time_elapse / 365 days / oToken.IRM_SCALE();
}
uint256 feeInterestAmount = interestAmount * oToken.RESERVE_FEE() / oToken.FEE_SCALE();
interestAmount -= feeInterestAmount;
oToken.accrue();
vm.warp(time_elapse * 2);
oToken.accrue();
vm.warp(time_elapse * 4);
oToken.accrue();
}
We are now accruing the interest every two minutes
Before running the POC we import the
import "forge-std/console.sol";
In OmniToken.sol
and add the console.log to log the interest
{
uint256 interestRate = IIRM(irm).getInterestRate(address(this), trancheIndex, totalDeposit, totalBorrow);
// @audit
// this can be trancated to 0
interestAmount = (trancheBorrowAmount_ * interestRate * timePassed) / 365 days / IRM_SCALE;
console.log("interestAmount", interestAmount);
}
We then run the POC:
forge test -vv --match-test "test_Accrue_POC_Low_decimal"
The output is:
Running 1 test for src/tests/TestOmniToken.t.sol:TestOmniToken
[PASS] test_Accrue_POC_Low_decimal() (gas: 737833)
Logs:
interestAmount 0
interestAmount 0
interestAmount 0
This mean within every two minutes time elapse, the interest is round down to 0 and the accrue function is called in every borrow / withdraw / transfer, so the interest will be rounded down heavily.
Alternatively, user can keep calling accrue for every two minutes (or x minutes) to avoid paying interest.
Recommended Mitigation Steps
Modify the interest, do not do multiplication before division to avoid precision loss.
Do not hardcode IRM_SCALE and consider token decimals when accruing interest
allenjlee (BetaFinance) confirmed and commented:
We acknowledge this issue happens when values are small. I believe that the loss of precision causing 0 rounding is mainly from the 365 days value, which is equivalent to 31,536,000 seconds (a little over 7 decimals of precision loss). For the interest rate, let’s say — worst case — the lowest interest rate is 0.1%, which would be 3 decimals of precision loss as well. Therefore, there is a divisor of 31,536,000,000 every second, where the borrow amount must exceed this in the worst case. For tokens with greater than 11 decimals, there should be no issues. For tokens with less than 11 decimals, there will be issues.
For USDC, this means there must be greater than 31,536 USDC per tranche being actively borrowed otherwise the interest rounds to 0 (assuming
accrue()is called every second). If we relax this assumption, and sayaccrue()is called once every 10 seconds, the requirement becomes 3,153.60 USDC per tranche. Assuming it’s below the 31,536 USDC threshold, this would mean ~31.5 USDC is lost in interest every year.Similarly, for USDT (w/ 8 decimals) it will require greater than 315.36 (1 second) and 3.1536 (10 seconds) USDT per tranche being actively borrowed, otherwise interest rounds to 0.
For WBTC, this would also mean that we would need a 1% base interest rate for it to be 0.31536 (10 second block time) to compensate for no interest. The loss on this (assuming BTC price is $100K) would also be$300 in interest lost. Practically the value is small.
We think refactoring the code to address this issue would be quite challenging and introduce potential problems/edge cases and the changes outweigh the problems solved, so we will instead make it aware in our documentation that there is this limitation. We will also be mindful of setting the configurations for the asset IRMs. Thank you for bringing up this issue, we agree with the severity.
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 interest formula performs division before multiplication and uses a hardcoded scale factor, causing precision loss that rounds small interest amounts to zero.
Impact
Interest may round down to zero for tokens with low decimals and small borrow amounts, allowing users to avoid paying interest by calling accrue frequently; the team acknowledges the issue but considers the practical loss small and will document the limitation rather than refactor.
What the attacker needed
Token with low decimals (e.g. 6); small borrow amount; frequent accrue calls
The fix
The interest calculation should be refactored to avoid division before multiplication and to account for token decimals instead of using a hardcoded scale, though the team chose to document the limitation rather than change the code.
How it was found
- Change the mock token's decimals from 18 to 6.
- Borrow a small amount (10 tokens) from the pool.
- Advance time by 120 seconds and call accrue.
- Repeat accrual at 240 and 480 seconds.
- Observe that the logged interest amount is zero each time.
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 does maths with whole numbers. If it divides too early, the result can become zero because fractions are thrown away. Here, the code divides by a large number before multiplying, so for tokens with few decimal places and short time periods, the interest becomes zero and borrowers pay nothing.