Skip to content
Smart contracts Audit finding Advanced

Reentrancy at _requestLoan allows requesting a loan without supplying collateral

The _requestLoan function makes an external call to the collateral contract before updating the contract's state, allowing a malicious borrower to reenter and request a loan without supplying collateral. The borrower can then remove the collateral and still have a valid loan, potentially stealing lender funds.

kenzo Follow

Originally disclosed on Code4rena

Submitted by kenzo, also found by WatchPug, GimelSec, Czar102, and AuditsAreUS

https://github.com/code-423n4/2022-04-abranft/blob/main/contracts/NFTPair.sol#L218

https://github.com/code-423n4/2022-04-abranft/blob/main/contracts/NFTPairWithOracle.sol#L238

_requestLoan makes an external call to the collateral contract before updating the NFTPair contract state.

Impact

If the ERC721 collateral has a afterTokenTransfer hook, The NFTPair contract can be reentered, and a loan can be requested without the borrower supplying the collateral. Someone can then lend for the loan while the collateral is missing from the contract. Therefore the malicious borrower received the loan without supplying collateral - so lender’s funds can be stolen. The issue is present in both NFTPair and NFTPairWithOracle.

Proof of Concept

Assume the NFT contract has an afterTokenTransfer hook which calls back to the malicious borrower. POC in short: borrower will call requestLoan with skim==false, then during the hook will reenter requestLoan with skim==true, then call removeCollateral to get his collateral back, then the first requestLoan will resume and initiate the loan, although the collateral is not in the contract any more.

POC in long: the borrower will do the following:

  • Call requestLoan with skim==false.
  • requestLoan will call collateral.transferFrom().
  • The collateral will be transferred to the NFTPair. Afterwards, the ERC721 contract will execute the afterTokenTransfer hook, and hand control back to Malbo (malicious borrower).
  • Malbo will call requestLoan again with skim==true.
  • As the first request’s details have not been updated yet, the tokenId status is still LOAN_INITIAL, and the require statement of the loan status will pass.
  • The NFT has already been transfered to the contract, so the require statement of token ownership will pass.
  • requestLoan (the second) will continue and set the loan details and status.
  • After it finishes, still within the afterTokenTransfer hook, Malbo will call removeCollateral. His call will succeed as the loan is in status requested. So the collateral will get sent back to Malbo.
  • Now the afterTokenTransfer hook finishes.
  • The original requestLoan will resume operation at the point where all the loan details will be updated.
  • Therefore, the contract will mark the loan is valid, although the collateral is not in the contract anymore. A lender might then lend funds against the loan without Malbo needing to pay back.

Recommended Mitigation Steps

Move the external call to the end of the function to conform with checks-effects-interaction pattern.

cryptolyndon (AbraNFT) disputed and commented:

Not a bug. We do not use safeTransfer, and if the collateral contract cannot be trusted, then all bets are off.

0xean (judge) downgraded to medium severity and commented:

I am downgrading this to medium severity and do believe it should be fixed by the sponsor. Re-entrancy has proved to be a problem in many ways in the space and while the sponsor says they are trusting the collateral contract, I dont think this is a defensible stance from what can be easily mitigated by either re-ordering code to conform to well established patterns or by adding a modifier.

2 — Med: Assets not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements.


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

The function performs an external call (collateral transfer) before updating its internal state, violating the checks-effects-interactions pattern.

Impact

A malicious borrower could receive a loan without supplying collateral, potentially stealing lender funds, but the attack requires the collateral contract to have a callback hook and the sponsor disputes the bug, so the judge downgraded it to medium severity.

What the attacker needed

Collateral contract has afterTokenTransfer hook; malicious borrower controls the hook

The fix

The external call should be moved to the end of the function to follow the checks-effects-interactions pattern.

How it was found

  1. Call requestLoan with skim==false.
  2. The collateral is transferred to the NFTPair, triggering the afterTokenTransfer hook.
  3. In the hook, call requestLoan again with skim==true.
  4. The second requestLoan sets the loan details and status.
  5. Still in the hook, call removeCollateral to retrieve the collateral.
  6. The original requestLoan resumes and marks the loan as valid despite no collateral.

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 reentrancy attack happens when a contract calls another contract before finishing its own updates, letting the called contract call back in and exploit the incomplete state. Here, the borrower uses a callback from the NFT transfer to trick the system into thinking collateral is still there when it is not, so they get a loan without actually providing anything.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by kenzo

View all

53 more disclosed reports, best first.

More from AbraNFT contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.