Skip to content
Race condition Audit finding Advanced

mergeTranches() / refinancePartial() lack of nonReentrant

The mergeTranches and refinancePartial functions lack reentrancy protection, allowing an attacker to reenter during a callback and create duplicate loan records for the same NFT. This could let the attacker repay one loan, recover the NFT, use it to borrow other funds, and then repay the other loan to recover the NFT again.

bin2chen Follow

Originally disclosed on Code4rena

Submitted by bin2chen

In mergeTranches(), the method’s code implementation is as follows:

 function mergeTranches(uint256 _loanId, Loan memory _loan, uint256 _minTranche, uint256 _maxTranche)
        external
        returns (uint256, Loan memory)
    {
        _baseLoanChecks(_loanId, _loan);
        uint256 loanId = _getAndSetNewLoanId();
        Loan memory loanMergedTranches = _mergeTranches(loanId, _loan, _minTranche, _maxTranche);
        _loans[loanId] = loanMergedTranches.hash();
        delete _loans[_loanId];

        emit TranchesMerged(loanMergedTranches, _minTranche, _maxTranche);

        return (loanId, loanMergedTranches);
    }

As shown above, this method lacks reentrancy protection, which could allow reentrancy attacks to manipulate the _loans[].

Example: Suppose _loans[1] = {NFT = 1}

  1. Alice calls refinanceFromLoanExecutionData(_loans\[1],LoanExecutionData).

    • LoanExecutionData.ExecutionData.OfferExecution.LoanOffer.OfferValidator\[0].validator = CustomContract => for callback.
  2. refinanceFromLoanExecutionData() -> _processOffersFromExecutionData() -> _validateOfferExecution() -> _checkValidators() -> IOfferValidator(CustomContract).validateOffer().

  3. In IOfferValidator(CustomContract).validateOffer(), call MultiSourceLoan.mergeTranches(_loans[1]) -> pass without nonReentrant.

    • _loans\[3] = newLoan.hash()
  4. Return to refinanceFromLoanExecutionData(), will execute:

    • _loans\[2] = newOtherLoan.hash().

There will be _loans[2] and _loans[3], both containing NFT=1. Note: Both Loans ‘s lender are all himself:

  1. The user can repayLoan(_loans[2]) and get the NFT back.
  2. Use the NFT to borrow other people’s funds, e.g. to generate _loans[100].
  3. repayLoan(_loans[3]), get NFT back.

Recommended Mitigation

Add nonReentrant:

 function mergeTranches(uint256 _loanId, Loan memory _loan, uint256 _minTranche, uint256 _maxTranche)
        external
+       nonReentrant
        returns (uint256, Loan memory)
    {
        _baseLoanChecks(_loanId, _loan);
        uint256 loanId = _getAndSetNewLoanId();

    function refinancePartial(RenegotiationOffer calldata _renegotiationOffer, Loan memory _loan)
        external
+       nonReentrant
        returns (uint256, Loan memory)
    {

Assessed type

Context

0xend (Gondi) confirmed

Gondi mitigated:

Added nonReentrant.

Status: Mitigation confirmed. Full details in reports from oakcobalt, minhquanym and bin2chen.


The original report as published on Code4rena, by bin2chen. 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 functions mergeTranches and refinancePartial lack a reentrancy guard, allowing state changes to be interleaved with external calls.

Impact

An attacker could create duplicate loan records for the same NFT, repay one to recover the NFT, use it to borrow other funds, and then repay the other to recover the NFT again, potentially draining funds from other lenders.

What the attacker needed

Attacker controls a custom offer validator contract; attacker has a loan with that validator

The fix

The functions mergeTranches and refinancePartial were fixed by adding the nonReentrant modifier.

How it was found

  1. Alice calls refinanceFromLoanExecutionData with a loan whose offer validator is a custom contract that will call back.
  2. The call reaches _validateOfferExecution, which calls validateOffer on the custom contract.
  3. Inside validateOffer, the custom contract calls mergeTranches on the same loan, which succeeds because there is no nonReentrant guard.
  4. Control returns to refinanceFromLoanExecutionData, which then creates another loan record for the same NFT.
  5. Alice repays one of the duplicate loans to recover the NFT.
  6. Alice uses the NFT to borrow other funds, then repays the other duplicate loan to recover the NFT again.

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 an external contract, and that external contract calls back into the original contract before the first operation finishes. Here, the loan contract does not block such callbacks, so an attacker can create two loan records for the same NFT. They can then repay one loan to get the NFT back, use it to borrow more, and still have the second loan to repay and recover the NFT again.

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 bin2chen

View all

139 more disclosed reports, best first.

More from Gondi Invitational

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.