requiredImprovementRate can not work as expected when previousInterestRate less than 10 due to precision loss
The buyout check in the NFT loan facilitator used integer division to compute the required improvement, so when the previous interest rate was below 10 and the required improvement rate was 100, rounding down let a new lender match the existing rate instead of improving it. An attacker could therefore buy out a loan without offering better terms.
Submitted by WatchPug, also found by CertoraInc and hickuphh3
NFTLoanFacilitator.sol#L167-L179
{
uint256 previousInterestRate = loan.perAnumInterestRate;
uint256 previousDurationSeconds = loan.durationSeconds;
require(interestRate <= previousInterestRate, 'NFTLoanFacilitator: rate too high');
require(durationSeconds >= previousDurationSeconds, 'NFTLoanFacilitator: duration too low');
require((previousLoanAmount * requiredImprovementRate / SCALAR) <= amountIncrease
|| previousDurationSeconds + (previousDurationSeconds * requiredImprovementRate / SCALAR) <= durationSeconds
|| (previousInterestRate != 0 // do not allow rate improvement if rate already 0
&& previousInterestRate - (previousInterestRate * requiredImprovementRate / SCALAR) >= interestRate),
"NFTLoanFacilitator: proposed terms must be better than existing terms");
}
The requiredImprovementRate represents the percentage of improvement required of at least one of the terms when buying out from a previous lender.
However, when previousInterestRate is less than 10 and requiredImprovementRate is 100, due to precision loss, the new interestRate is allowed to be the same as the previous one.
Making such an expected constraint absent.
Proof of Concept
- Alice
createLoan()withmaxPerAnumInterest= 10, receivedloanId= 1 - Bob
lend()withinterestRate= 9 forloanId= 1 - Charlie
lend()withinterestRate= 9 (and all the same other terms with Bob) and buys outloanId= 1
Charlie is expected to provide at least 10% better terms, but actually bought out Bob with the same terms.
Recommended Mitigation Steps
Consider using: OpenZeppelin/Math.sol#L39-L42
And change the check to:
(previousInterestRate != 0 // do not allow rate improvement if rate already 0
&& previousInterestRate - Math.ceilDiv(previousInterestRate * requiredImprovementRate, SCALAR) >= interestRate)
wilsoncusack (Backed Protocol) confirmed and resolved
Sponsor confirmed with fix.
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
Impact
A buyer could take over a loan without providing the required improvement in terms, weakening the protocol's buyout constraint.
What the attacker needed
Previous interest rate below 10; required improvement rate of 100; ability to lend and buy out a loan
The fix
The check should round the required improvement up (for example with a ceiling division helper) so small rates cannot round down to zero.
How it was found
- Alice creates a loan with a maximum annual interest rate of 10.
- Bob lends to that loan at an interest rate of 9.
- Charlie lends at the same rate of 9 with otherwise identical terms and buys out the loan.
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.
Smart contracts store numbers as whole integers, so dividing can throw away the remainder. Here the code calculated a required improvement by dividing, and for small interest rates the result rounded down to zero, meaning no improvement was actually required. That let someone take over a loan while offering exactly the same terms as the current lender.
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.