# Risk of overpayment due to race condition between repay and liquidateWithReplacement transactions

Race between repay and liquidateWithReplacement lets a borrower's repay tx land after the debt position's borrower is swapped, forcing them to pay another borrower's debt.

- Payout: Audit contest
- Severity: high
- Program: Size
- Found by: elhaj
- Disclosed: 2024-09-16
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-06-size-findings/issues/181
- Library page: https://bugbountylibrary.com/r/c4%3A2024-06-size%3AH-02
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:high tech:solidity technique:race-window vuln:access-control vuln:business-logic vuln:race-condition

**Target:** Size.sol repay and liquidateWithReplacement debt-position functions
**Root cause:** liquidateWithReplacement reassigns debtPosition.borrower while keeping the same debtPositionId and futureValue, and repay only validates debtPositionId (not borrower), so a stale repay tx pays the new borrower's debt.
**Steps:**
1. Borrower's position becomes liquidatable; borrower submits `repay(debtPositionId)` to avoid liquidation.
2. Keeper front-runs with `liquidateWithReplacement(debtPositionId, borrower=newBorrower, ...)`, which liquidates old borrower, sets `debtPosition.borrower = newBorrower`, keeps same ID/futureValue.
3. Borrower's `repay` then executes: `borrowAToken.transferFrom(msg.sender, address(this), debtPosition.futureValue)` and `repayDebt(debtPositionId, futureValue)` — paying the new borrower's debt.
4. Old borrower loses collateral to liquidation plus pays futureValue again (>2x loss).
**How found:** Noticed liquidateWithReplacement preserves debtPositionId and futureValue while only swapping borrower, then observed repay validates only the ID — so a repay racing a replacement pays the wrong party.
**Exploitation:** If liquidateWithReplacement were permissionless, attacker watches mempool for a victim's repay tx, front-runs with liquidateWithReplacement setting themselves as new borrower, receives victim's collateral plus borrowAToken issuance, then victim's repay clears attacker's debt.
**Insight:** When a state-mutating function preserves an identifier but changes its owner, check every consumer of that identifier for owner-agnostic authorization; repay-by-ID is a classic stale-owner payment vector.

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

**executeLiquidateWithReplacement reassigns borrower keeping same debtPositionId**

```
function executeLiquidateWithReplacement(State storage state, LiquidateWithReplacementParams calldata params)
        external
        returns (uint256 issuanceValue, uint256 liquidatorProfitCollateralToken, uint256 liquidatorProfitBorrowToken)
    {
      // some code ...
/*1*/>>    liquidatorProfitCollateralToken = state.executeLiquidate(LiquidateParams({debtPositionId: params.debtPositionId, minimumCollateralProfit: params.minimumCollateralProfit}));

        uint256 ratePerTenor = borrowOffer.getRatePerTenor(
            VariablePoolBorrowRateParams({
                variablePoolBorrowRate: state.oracle.variablePoolBorrowRate,
                variablePoolBorrowRateUpdatedAt: state.oracle.variablePoolBorrowRateUpdatedAt,
                variablePoolBorrowRateStaleRateInterval: state.oracle.variablePoolBorrowRateStaleRateInterval
            }),
            tenor
        );
        issuanceValue = Math.mulDivDown(debtPositionCopy.futureValue, PERCENT, PERCENT + ratePerTenor);
        liquidatorProfitBorrowToken = debtPositionCopy.futureValue - issuanceValue;

/*2*/>>      debtPosition.borrower = params.borrower;
/*3*/>>      debtPosition.futureValue = debtPositionCopy.futureValue;
        debtPosition.liquidityIndexAtRepayment = 0;

        emit Events.UpdateDebtPosition(params.debtPositionId, debtPosition.borrower, debtPosition.futureValue, debtPosition.liquidityIndexAtRepayment);
        // @note : in this case the borrower doesn't pay swapFees
          state.data.debtToken.mint(params.borrower, debtPosition.futureValue);
/*4*/>>   state.data.borrowAToken.transferFrom(address(this), params.borrower, issuanceValue);
          state.data.borrowAToken.transferFrom(address(this), state.feeConfig.feeRecipient, liquidatorProfitBorrowToken);
```

**executeRepay repays by debtPositionId only, no borrower check**

```
function executeRepay(State storage state, RepayParams calldata params) external {
        DebtPosition storage debtPosition = state.getDebtPosition(params.debtPositionId);

        state.data.borrowAToken.transferFrom(msg.sender, address(this), debtPosition.futureValue);
        debtPosition.liquidityIndexAtRepayment = state.data.borrowAToken.liquidityIndex();
        state.repayDebt(params.debtPositionId, debtPosition.futureValue);

        emit Events.Repay(params.debtPositionId);
```

