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

Lender front-runs repayLoan with mergeTranches, changing the stored loan hash so repayment reverts and the borrower's NFT collateral gets liquidated.

- Payout: Audit contest
- Severity: high
- Program: Gondi
- Found by: zhaojie
- Disclosed: 2024-07-25
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-04-gondi-findings/issues/35
- Library page: https://bugbountylibrary.com/r/c4%3A2024-04-gondi%3AH-10
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:high tech:evm tech:solidity technique:chained vuln:business-logic vuln:dos vuln:liquidation

**Target:** Solidity lending contract repayLoan/mergeTranches
**Root cause:** repayLoan validates _loans[loanId] against the passed loan hash, but mergeTranches/addNewTranche delete and rewrite _loans[loanId], so a front-run state change makes the borrower's repayment revert with InvalidLoanError.
**Steps:**
1. Borrower submits `repayLoan(_repaymentData)` near loan expiry.
2. Attacker (lender) front-runs with `mergeTranches(...)` which executes `_loans[loanId] = loanMergedTranches.hash();` and `delete _loans[_loanId];`.
3. Borrower's tx executes `_baseLoanChecks`, `_loan.hash() != _loans[_loanId]` reverts `InvalidLoanError`.
4. Repeat until `startTime + duration < block.timestamp`, then loan expires and collateral is liquidated.
**How found:** Code reading of repayLoan's _baseLoanChecks comparing _loan.hash() to mutable _loans[_loanId], noticing mergeTranches deletes/rewrites that mapping entry.
**Exploitation:** Lender monitors mempool for repayLoan near expiry, front-runs with mergeTranches to invalidate the loan hash, blocking repayment until LoanExpiredError; then liquidateLoan forecloses the NFT collateral to the lender.
**Escalation:** Chained with liquidation: merging tranches to length 1 sets _canClaim true so the lender receives the NFT directly on foreclosure.
**Insight:** When a function validates a caller-supplied struct against mutable on-chain state, check whether any other permissionless or counterparty-callable function rewrites that state entry; a hash-equality check against a mutable mapping is front-runnable to grief repayment.

## Proof of concept (copied exactly from the original report)

**repayLoan checks loanId against stored hash**

```
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();
        }
    }
```

**mergeTranches deletes old loanId and writes new**

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

