Skip to content
Race condition Audit finding Advanced

The attackers front-running repayloans so that the debt cannot be repaid

The repayLoan function checks the loan ID against a stored hash, but functions like mergeTranches can change that stored hash, causing repayLoan to revert. An attacker (e.g., the lender) can front-run a repayment with mergeTranches to make the loan impossible to repay, leading to liquidation and loss of collateral.

zhaojie Follow with minhquanym

Originally disclosed on Code4rena

Submitted by zhaojie, also found by minhquanym

The attackers make it impossible for borrowers to repay their debts, and the collateral is liquidated when the debts mature.

Proof of Concept

repayLoan needs to check the loanId; if the id is inconsistent it will revert.

    function repayLoan(LoanRepaymentData calldata _repaymentData) external override nonReentrant {
        uint256 loanId = _repaymentData.data.loanId;
        Loan calldata loan = _repaymentData.loan;
        .....
@>      _baseLoanChecks(loanId, loan);
        .....
    }

    function _baseLoanChecks(uint256 _loanId, Loan memory _loan) private view {
        if (_loan.hash() != _loans[_loanId]) {
            revert InvalidLoanError(_loanId);
        }
        if (_loan.startTime + _loan.duration < block.timestamp) {
            revert LoanExpiredError();
        }
    }

The problem is that _loans[_loanId] can change; for example, when mergeTranches deletes the old loanId and writes the new one:

    _loans[loanId] = loanMergedTranches.hash();
    delete _loans[_loanId];

An attacker can use the front-running attack method. When repayLoan is called, execute the mergeTranches function in advance, and make the id in _loans updated. In this case, the repayLoan execution will fail due to inconsistent _loanId.

If the attacker keeps using this attack, the borrower’s debt will not be repaid; eventually causing the collateral to be liquidated.

In addition to the mergeTranches function, the attacker can also call addNewTranche, and the borrower can also call the refinance-related function, again causing _loanId to be updated.

An attacker can also use the same method to attack refinance related functions, making refinance unable to execute. An attacker can also use the same method to attack the liquidateLoan function, making it impossible for debts to be cleared.

Tools Used

VScode

Recommended Mitigation Steps

Do not delete _loanId.

Assessed type

DoS

0xA5DF (judge) commented:

I have some doubts about severity, since this requires too many resources from the attacker (see here), and the addNewTranche() requires the lender’s signature (and when using mergeTranches() alone the attacker would eventually run out of tranches to merge).

0xend (Gondi) confirmed and commented:

I think this is low (agree with judge for those reasons).

0xA5DF (judge) decreased severity to Low and commented:

I think there are too many limitations on this one, and the motivation for the attacker isn’t very high - they’re not going to get the entire principal from this.

0xend (Gondi) commented:

Given the limit on tranches the attacker can only run this a handful of times.

zhaojie (warden) commented:

I think it’s a high risk, because anyone can be an attacker, so Lender can be an attacker.

If the lender does not want the borrower to repay the debt, the lender can use addNewTranche/mergeTranches and to attack repayLoans and make the borrower’s loan impossible to repay, especially when the loan is about to expire. This causes the borrower’s NFT to be loss, so it would have a high impact.

When _liquidateLoan, if _canClaim == true, the borrower can get the NFT directly:

    function _liquidateLoan....{
       ....
        if (_canClaim) {
            ERC721(_loan.nftCollateralAddress).transferFrom(
                address(this), _loan.tranche[0].lender, _loan.nftCollateralTokenId
            );
            emit LoanForeclosed(_loanId);

            liquidated = true;
        }
    ....
    }

     function liquidateLoan(uint256 _loanId, Loan calldata _loan)...  {
       .....
        (bool liquidated, bytes memory liquidation) = _liquidateLoan(
            _loanId, _loan, _loan.tranche.length == 1 && !getLoanManagerRegistry.isLoanManager(_loan.tranche[0].lender)
        );
       ......
     }

An attacker/lender can use mergeTranches to make _loan.tranche.length == 1. The key issue is that loanId will be reset.

0xA5DF (judge) increased severity to High and commented:

You’re right that the lender has a high motivation to execute this attack. You’re also right that when the borrower attempts to repay close to the expiry time this attack becomes feasible.

While some conditions are required in order for this to work, it still seems pretty likely to happen. Due to those reasons I’m reinstating high severity.

Side note: I think that a better mitigation would be to not allow functions that change the loanID to run near the expiry time.

0xend (Gondi) commented:

No specific PR here since it’s addressed when limiting addNewTranche to only be able to be called by the borrower and checking in refinancePartial that there’s at least one tranche being refinanced. This ends up limiting the number of times a loan can be locked by the lender (tranches are locked for some time after a refinance for future ones).


The original report as published on Code4rena, by zhaojie. 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 loan ID is mutable and functions like mergeTranches update the stored loan hash, so repayLoan's consistency check fails after such updates.

Impact

If the attacker keeps using this attack, the borrower’s debt will not be repaid; eventually causing the collateral to be liquidated.

What the attacker needed

Attacker can front-run transactions; loan has multiple tranches to merge; repayment attempted near expiry

The fix

The report recommends not deleting the loan ID, but the judge suggested not allowing functions that change the loan ID to run near expiry, and the protocol addressed it by limiting addNewTranche to the borrower and checking refinancePartial.

How it was found

  1. Attacker monitors for a repayLoan transaction.
  2. Attacker front-runs it by calling mergeTranches to update the loan ID.
  3. The repayLoan transaction then reverts due to inconsistent loan ID.

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 you repay a loan, the system checks that the loan details match what it has stored. But some actions can change the stored details, so the check fails and the repayment is rejected. An attacker can trigger such a change just before you repay, making it impossible for you to pay back and causing your collateral to be taken.

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 zhaojie

View all

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