Skip to content
Race condition Audit finding Advanced

currentLoanOwner can manipulate loanInfo when any lenders try to buyout

The current loan owner can re-enter the lend function during a buyout because the loan asset token triggers a callback, allowing them to overwrite the loan terms with unfavourable values. A lender attempting to buy out the loan could end up with a manipulated loan that cannot be corrected.

rayn Follow

Originally disclosed on Code4rena

Submitted by rayn

NFTLoanFacilitator.sol#L205-L208 NFTLoanFacilitator.sol#L215-L218

If an attacker already calls lend() to lend to a loan, the attacker can manipulate loanInfo by reentrancy attack when any lenders try to buyout. The attacker can set bad values of lendInfo (e.g. very long duration, and 0 interest rate) that the lender who wants to buyout don’t expect.

Proof of Concept

An attacker lends a loan, and loanAssetContractAddress in loanInfo is ERC777 which is suffering from reentrancy attack. When a lender (victim) try to buyout the loan of the attacker:

  1. The victim called lend().

  2. In lend(), it always call ERC20(loanAssetContractAddress).safeTransfer to send accumulatedInterest + previousLoanAmount to currentLoanOwner (attacker).

  3. The transfer of loanAssetContractAddress ERC777 will call _callTokensReceived so that the attacker can call lend() again in reentrancy with parameters:

    • loanId: same loan Id
    • interestRate: set to bad value (e.g. 0)
    • amount: same amount
    • durationSeconds: set to bad value (e.g. a long durationSeconds)
    • sendLendTicketTo: same address of the attacker (currentLoanOwner)
  4. Now the variables in loanInfo are changed to bad value, and the victim will get the lend ticket but the loan term is manipulated, and can not set it back (because it requires a better term).

Tools Used

vim

Recommended Mitigation Steps

Use nonReentrant modifier on lend() to prevent reentrancy attack: OpenZeppelin/ReentrancyGuard.sol

wilsoncusack (Backed Protocol) acknowledged, but disagreed with High severity and commented:

We should mitigate, but will think on this.

wilsoncusack (Backed Protocol) confirmed and commented:

Not sure whether this should be Medium or High risk.

wilsoncusack (Backed Protocol) commented:

Thinking more, again we should definitely mitigate, but I think this is less severe because I do not think ERC777 tokens will work with our contract? The on received call will revert? So this would need to be a malicious ERC20 designed just for this.

wilsoncusack (Backed Protocol) resolved and commented:

er erc777 does work because reception ack is not needed in the normal case.

gzeon (judge) commented:

Sponsor confirmed with fix.


The original report as published on Code4rena, by rayn. 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

Root cause

Likely the lend function performs an external call to transfer tokens before updating state, and lacks a reentrancy guard, allowing the recipient to re-enter and modify the loan terms.

Impact

A lender attempting to buy out a loan could have the loan terms manipulated to unfavourable values, and the sponsor noted that ERC777 tokens may work, though they initially thought a malicious ERC20 might be needed.

What the attacker needed

Attacker is current loan owner; loan asset is ERC777 or malicious ERC20; victim attempts buyout

The fix

The lend function should be protected with a reentrancy guard, such as OpenZeppelin's nonReentrant modifier.

How it was found

  1. The victim calls lend() to buy out the loan.
  2. Inside lend(), the contract transfers accumulatedInterest + previousLoanAmount to the current loan owner (attacker).
  3. The transfer triggers a callback on the ERC777 token, allowing the attacker to re-enter lend().
  4. The attacker calls lend() again with the same loan ID, a zero interest rate, the same amount, a long duration, and the same recipient address.
  5. The loanInfo variables are overwritten with the bad values, and the victim receives the lend ticket but with manipulated terms that cannot be changed back.

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 someone tries to take over a loan by paying off the current owner, the contract sends the payment to the current owner. If the payment token has a callback feature, the current owner can use that callback to call the lend function again and change the loan's terms to something bad, like zero interest or a very long duration. The new lender is then stuck with those bad terms.

What is Race condition?

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.

More by rayn

View all

12 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.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.