`Reserves` should not be considered part of the available liquidity while calculating the interest rate
The interest rate calculation used the entire asset balance as liquidity, including reserves, instead of subtracting reserves as documented. This made the calculated interest rate lower than expected, reducing the cost of borrowing for users.
Source: https://github.com/sherlock-audit/2022-08-sentiment-judging/tree/main/266-M
Found by
WATCHPUG
Summary
The implementation is different from the documentation regarding the interest rate formula.
Vulnerability Detail
The formula given in the docs:
Calculates Borrow rate per second:
$$ Borrow Rate Per Second = c3 \cdot (util \cdot c1 + util^{32} \cdot c1 + util^{64} \cdot c2) \div secsPerYear $$
where, $util = borrows \div (liquidity - reserves + borrows)$
$$ util=borrows \div (liquidity−reserves+borrows) $$
function getRateFactor() internal view returns (uint) {
return (block.timestamp == lastUpdated) ?
0 :
((block.timestamp - lastUpdated)*1e18)
.mulWadUp(
rateModel.getBorrowRatePerSecond(
asset.balanceOf(address(this)),
borrows
)
);
}
However, the current implementation is taking all the balance as the liquidity:
function getBorrowRatePerSecond(
uint liquidity,
uint borrows
)
external
view
returns (uint)
{
uint util = _utilization(liquidity, borrows);
return c3.mulDivDown(
(
util.mulWadDown(c1)
+ util.rpow(32, SCALE).mulWadDown(c1)
+ util.rpow(64, SCALE).mulWadDown(c2)
),
secsPerYear
);
}
function _utilization(uint liquidity, uint borrows)
internal
pure
returns (uint)
{
uint totalAssets = liquidity + borrows;
return (totalAssets == 0) ? 0 : borrows.divWadDown(totalAssets);
}
Impact
Per the docs, when calculating the interest rate, util is the ratio of available liquidity to the borrows, available liquidity should not include reserves.
The current implementation is using all the balance as the liquidity, this will make the interest rate lower than expectation.
PoC
Given:
asset.address(this) + borrows = 10000reserves = 1500, borrows = 7000
Expected result:
When calculating getRateFactor(), available liquidity should be asset.balanceOf(address(this)) - reserves = 1500, util = 7000 / 8500 = 0.82, getBorrowRatePerSecond() = 9114134329
Actual result:
When calculating getRateFactor(), asset.balanceOf(address(this)) = 3000, util = 0.7e18, getBorrowRatePerSecond() = 7763863430
The actual interest rate is only 7763863430 / 9114134329 = 85% of the expected rate.
Code Snippet
Tool used
Manual Review
Recommendation
The implementation of getRateFactor() can be updated to:
function getRateFactor() internal view returns (uint) {
return (block.timestamp == lastUpdated) ?
0 :
((block.timestamp - lastUpdated)*1e18)
.mulWadUp(
rateModel.getBorrowRatePerSecond(
asset.balanceOf(address(this)) - reserves,
borrows
)
);
}
Sentiment Team
Removed reserves from LToken and added an alternate mechanism to collect direct fees.
Lead Senior Watson
originationFee may result in the borrower account becoming liquidatable immediately (aka WP-M2).
Sentiment Team
Fixed as recommended. PR here.
Lead Senior Watson
riskEngine.isBorrowAllowed should be removed as it's no longer needed.
Sentiment Team
Pushed a commit to remove the redundant call to riskEngine. PR 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
The getRateFactor function passed the entire asset balance as liquidity to the rate model, without subtracting reserves as specified in the documentation.
Impact
The interest rate was lower than intended, which could reduce protocol revenue and incentivise excessive borrowing.
What the attacker needed
None
The fix
The implementation was fixed by subtracting reserves from the asset balance before calculating the interest rate.
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 a lending protocol, the interest rate depends on how much of the pool is borrowed. The formula should exclude reserves (funds set aside for safety) from the available liquidity. The code mistakenly included reserves, so the calculated interest rate was lower than it should be, meaning borrowers paid less interest.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.