Skip to content
Business logic Audit finding Intermediate

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.

WatchPug Follow

Originally disclosed on Code4rena

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

  1. Alice createLoan() with maxPerAnumInterest = 10, received loanId = 1
  2. Bob lend() with interestRate = 9 for loanId = 1
  3. Charlie lend() with interestRate = 9 (and all the same other terms with Bob) and buys out loanId = 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

gzeon (judge) commented:

Sponsor confirmed with fix.


The original report as published on Code4rena, by WatchPug. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

  1. Alice creates a loan with a maximum annual interest rate of 10.
  2. Bob lends to that loan at an interest rate of 9.
  3. 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.

More by WatchPug

View all

197 more disclosed reports, best first.

More from Papr contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.