utilization for _getInterestRate() does not factor in interest
The utilization figure used to set the interest rate is computed before accrued interest is added to the total borrow amount, so the new interest rate is based on a stale, understated utilization. This can produce incorrect interest rates and may let utilization appear to exceed the maximum target threshold.
ItsNio Follow with ItsNio and SaeedAlipoor01988
Originally disclosed on Code4rena
Submitted by ItsNio, also found by ItsNio and SaeedAlipoor01988
The calculation for utilization in _getInterestRate() does not factor in the accrued interest. This leads to _accrueInfo.interestPerSecond being under-represented, and leading to incorrect interest rate calculation and potentially endangering conditions such as utilization > maximumTargetUtilization on line 124.
Proof of Concept
The calculation for utilization in the _getInterestRate() function for SGLCommon.sol occurs on lines 61-64 as a portion of the fullAssetAmount (which is also problematic) and the _totalBorrow.elastic. However, _totalBorrow.elastic is accrued by interest on line 99. This accrued amount is not factored into the calculation for utilization, which will be used to update the new interest rate, as purposed by the comment on line 111.
Recommended Mitigation Steps
Factor in the interest accrual into the utilization calculation:
...
// Accrue interest
extraAmount =
(uint256(_totalBorrow.elastic) *
_accrueInfo.interestPerSecond *
elapsedTime) /
1e18;
_totalBorrow.elastic += uint128(extraAmount);
+ uint256 fullAssetAmount = yieldBox.toAmount(
+ assetId,
+ _totalAsset.elastic,
+ false
+ ) + _totalBorrow.elastic;
//@audit utilization factors in accrual
+ utilization = fullAssetAmount == 0
+ ? 0
+ : (uint256(_totalBorrow.elastic) * UTILIZATION_PRECISION) /
+ fullAssetAmount;
...
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 utilization is calculated from the borrow total before the accrued interest is added to it, so the rate update uses a stale value.
Impact
Interest rates could be calculated incorrectly and utilization could appear to exceed the maximum target threshold, though the text does not show a concrete loss.
What the attacker needed
None
The fix
The utilization calculation should be moved after interest accrual so it includes the accrued amount.
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.
A lending contract sets its interest rate based on how much of the pool is borrowed (utilization). Before recalculating the rate, it adds newly earned interest to the amount owed, but it then works out utilization using the old figure, so the rate is set from out-of-date numbers. The result is that borrowers may be charged the wrong rate.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.