Skip to content
Business logic Audit finding Intermediate

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

A borrower can front-run a liquidator's full-repayment transaction by repaying a tiny amount of debt, so the liquidator's repayment exceeds the remaining debt and the subtraction underflows, reverting the liquidation. This lets the borrower evade full liquidation, though the report notes the attacker must repeatedly front-run and pay gas.

0xbepresent Follow with Spearmint and Evo

Originally disclosed on Code4rena

Submitted by 0xbepresent, also found by Spearmint and Evo

https://github.com/code-423n4/2024-07-loopfi/blob/57871f64bdea450c1f04c9a53dc1a78223719164/src/CDPVault.sol#L509

https://github.com/code-423n4/2024-07-loopfi/blob/57871f64bdea450c1f04c9a53dc1a78223719164/src/CDPVault.sol#L713

Impact

In the CDPVault::liquidatePosition function, a liquidator can repay the total debt of a position to liquidate it, this can be achieved by calling the CDPVault::virtualDebt function to obtain the total debt and then call CDPVault::liquidatePosition. However, a malicious borrower can front-run this liquidation transaction by repaying a small amount (e.g., 1 wei) of the debt. This action causes the debt to be slightly less than the amount the liquidator intends to repay. Consequently, the subtraction operation in CDPVault#L713 will underflow, leading to a revert in the transaction. This will allow borrowers to evade total debt liquidation.

Proof of Concept

Consider the next scenario:

  1. Assume the initial debt is 80 ether. The position is liquidatable.
  2. Liquidator prepares to repay the full debt 80 ether to liquidate the position via CDPVault::liquidatePosition. Liquidator gets the full debt using CDPVault::virtualDebt function.
  3. Malicious borrower front-runs the transaction and repays 1 wei, the debt is now 80 ether - 1 wei.
  4. Liquidator’s transaction is executed but it attempts to repay 80 ether, causing an underflow when the newDebt is calculated CDPVault#L713 as (80 ether - 1 wei) - 80 ether.
File: CDPVault.sol
652:     function calcDecrease(
653:         uint256 amount,
654:         uint256 debt,
655:         uint256 cumulativeIndexNow,
656:         uint256 cumulativeIndexLastUpdate,
657:         uint128 cumulativeQuotaInterest
658:     )
659:         internal
660:         pure
661:         returns (uint256 newDebt, uint256 newCumulativeIndex, uint256 profit, uint128 newCumulativeQuotaInterest)
662:     {
663:         uint256 amountToRepay = amount;
...
...
713:>>>      newDebt = debt - amountToRepay;  // underflow
714:     }

The next test demonstrates how the malicious borrower can evade full liquidation by repaying small amounts of the debt.

    // File: CDPVault.t.sol
    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);
    }

Recommended Mitigation Steps

Implement a validation to prevent repayment if the position remains liquidable after increasing the repayment amount.

Assessed type

Under/Overflow

0xtj24 (LoopFi) acknowledged and commented:

All liquidators would have to take into account a possible repayment from other liquidators. He could split into 2 repayments for example.

The liquidatePosition already checks if an amount is too high for repayment. Also, in case of 1 wei, it is not economically feasible since it would just be better for the borrower to repay instead of spending gas for multiple txs to avoid liquidation, since it would have to constantly frontrunning it.

Koolex (judge) commented:

I see two issues here:

  • A malicious actor can DoS liquidators, not only one. Since liquidators race and eventually one will win. In this case, the malicious actor.
  • DoS could naturally happen if more than one liquidator race for liquidation and the sum of the amounts isn’t equal to the highest possible amount.

Please take into consideration, if the loan is too big, the costs of gas is relatively too small.


The original report as published on Code4rena, by 0xbepresent. 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 liquidation function subtracts the liquidator's repayment amount from the current debt without checking that the repayment does not exceed the debt, so a small prior repayment causes an underflow.

Impact

A borrower could repeatedly front-run liquidators to prevent full liquidation, but the report and comments note this requires ongoing gas expenditure and may not be economically feasible for tiny amounts.

What the attacker needed

A liquidatable position; ability to front-run the liquidator; gas to repeat front-running

The fix

The report recommends validating that a repayment does not leave the position liquidatable, and the protocol commented that liquidators should account for other repayments and split repayments.

How it was found

  1. Create a position with 110 ether collateral and 80 ether debt, then lower the spot price so the position is liquidatable.
  2. The liquidator reads the full debt via virtualDebt and prepares to repay that amount.
  3. The borrower front-runs and repays 1 wei of debt.
  4. The liquidator's transaction attempts to repay the original full amount, causing an arithmetic underflow and revert.

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.

In a lending vault, if a borrower's collateral falls below a threshold, anyone can repay their debt and take some collateral as a reward; this is called liquidation. The report shows that a borrower can send a tiny repayment just before the liquidator's transaction, making the liquidator's planned repayment slightly too large, which causes the vault's maths to fail and the whole liquidation to be cancelled. The borrower must keep doing this and pay transaction fees each time.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by 0xbepresent

View all

13 more disclosed reports, best first.

More from LoopFi

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.