Skip to content
Race condition Audit finding Advanced

Borrower can be their own lender and steal funds from buyout due to reentrancy

A borrower could lend to their own loan and then, via a reentrant call during the buyout process, repay and close the loan before the lend ticket is transferred to the new lender. This allowed the borrower to keep the NFT, the loan amount, and accrued interest, effectively stealing funds from the new lender.

0xDjango Follow

Originally disclosed on Code4rena

Submitted by 0xDjango

NFTLoanFacilitator.sol#L214-L221 NFTLoanFacilitator.sol#L230-L250

If borrower lends their own loan, they can repay and close the loan before ownership of the lend ticket is transferred to the new lender. The borrower will keep the NFT + loan amount + accrued interest.

Proof of Concept

This exploit requires that the loanAssetContractAddress token transfers control to the receiver.

Steps of exploit:

  • Borrower creates loan with createLoan().
  • The same Borrower calls lend(), funding their own loan. The Borrower receives the lend ticket, and funds are transferred to themself.
  • A new lender attempts to buy out the loan. The original loan amount + accruedInterest are sent to the original lender (same person as borrower).
  • Due to lack of checks-effects-interactions pattern, the borrower is able to immediately call repayAndCloseLoan() before the lend ticket is transferred to the new lender.

The following code illustrates that the new lender sends funds to the original lender prior to receiving the lend ticket in return.

} else {
    ERC20(loan.loanAssetContractAddress).safeTransferFrom(
        msg.sender,
        currentLoanOwner,
        accumulatedInterest + previousLoanAmount
    );
}
ILendTicket(lendTicketContract).loanFacilitatorTransfer(currentLoanOwner, sendLendTicketTo, loanId);
The original lender/borrower calls the following `repayAndCloseLoan()` function so that they receive their collateral NFT from the protocol.

function repayAndCloseLoan(uint256 loanId) external override notClosed(loanId) {
    Loan storage loan = loanInfo[loanId];

    uint256 interest = _interestOwed(
        loan.loanAmount,
        loan.lastAccumulatedTimestamp,
        loan.perAnumInterestRate,
        loan.accumulatedInterest
    );
    address lender = IERC721(lendTicketContract).ownerOf(loanId);
    loan.closed = true;
    ERC20(loan.loanAssetContractAddress).safeTransferFrom(msg.sender, lender, interest + loan.loanAmount);
    IERC721(loan.collateralContractAddress).safeTransferFrom(
        address(this),
        IERC721(borrowTicketContract).ownerOf(loanId),
        loan.collateralTokenId
    );

    emit Repay(loanId, msg.sender, lender, interest, loan.loanAmount);
    emit Close(loanId);
}

Finally, the new lender receives the lend ticket that has no utility at this point. The borrower now possesses the NFT, original loan amount, and accrued interest.

Recommended Mitigation Steps

Move the line to transfer the lend ticket to the new lender above the line to transfer to funds to the original lender. Or, use reentrancyGuard from OpenZeppelin to remove the risk of reentrant calls completely.

If desired, also require that the lender cannot be the same account as the borrower of a loan.

wilsoncusack (Backed Protocol) confirmed and commented:

Borrower would need to convince lender to use an ERC20 with this malicious callback, but yes is legit.

malicious ERC20 -> transfers value to borrow ticket holder -> calls repay and close loan (would need funds available to do so, but still nets up)

wilsoncusack (Backed Protocol) commented:

Possibility of an ERC777 loan asset warrants this as high, I think. Even though the warden didn’t suggest that vector.

wilsoncusack (Backed Protocol) commented:

Scratch that, I think ERC777 not possible because our contract isn’t setup to receive them.

wilsoncusack (Backed Protocol) commented:

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

wilsoncusack (Backed Protocol) resolved

gzeon (judge) commented:

Sponsor confirmed.


The original report as published on Code4rena, by 0xDjango. 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 contract transfers funds to the current lender before transferring the lend ticket, and lacks a reentrancy guard, allowing the borrower (who is also the lender) to reenter and close the loan.

Impact

The borrower could steal the buyout funds from the new lender and retain the NFT, but the exploit requires a malicious ERC20 token with a callback, and the sponsor noted that ERC777 tokens may also enable it.

What the attacker needed

Borrower also acts as lender; Loan asset is a malicious ERC20 with callback; New lender buys out the loan

The fix

The fix is to transfer the lend ticket to the new lender before transferring funds to the original lender, and/or to use a reentrancy guard; the report was resolved.

How it was found

  1. Borrower creates a loan with createLoan().
  2. The same borrower calls lend() to fund their own loan, receiving the lend ticket and transferring funds to themselves.
  3. A new lender attempts to buy out the loan, sending the original loan amount plus accrued interest to the original lender (the borrower).
  4. During the fund transfer, the borrower reenters and calls repayAndCloseLoan() before the lend ticket is transferred to the new lender.
  5. The borrower receives the collateral NFT and keeps the loan amount plus interest; the new lender receives a useless lend ticket.

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 loan, a borrower puts up an NFT as collateral and a lender provides funds. Normally, if someone else buys the loan, they pay the original lender and get the right to collect repayments. Here, the borrower could also act as the original lender, and by exploiting the order in which the contract updates its records, they could close the loan early and keep both the NFT and the money.

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 0xDjango

View all

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