Incorrect liquidation mechanics either causes revert on liquidation due to insufficient seizeTokens or causes transition into bad debt
The liquidation mechanism has two flaws: partial liquidations can leave a position with debt below the minimum for partial liquidation but still above bad debt, causing full liquidation attempts to revert due to insufficient collateral to seize; alternatively, partial liquidations can push a position into bad debt, making the remaining debt unprofitable for liquidators. An attacker (or any liquidator) can trigger these states by performing partial liquidations, leaving the protocol with unclearable or loss-making positions.
Source: https://github.com/sherlock-audit/2024-12-numa-audit-judging/issues/101
Found by
t0x1c
Summary
The protocol's liquidation mechanics are fundamentally flawed in two distinct ways that emerge when performing liquidations on positions. Either:
-
The protocol reverts on liquidation of remaining debt due to insufficient collateral to seize, OR
-
The position transitions into bad debt status after a partial liquidation, making the remaining debt unprofitable for liquidators and worsening protocol's health.
Description
The protocol has two distinct broken liquidation paths that emerge when liquidating positions:
Prerequisite
The borrower's LTV should have worsened enough such that if the entire debt were to be liquidated, there wouldn't be enough collateral cTokens to seize after adding the liquidation incentive on top. Or in other words the liquidator would encounter a revert with error LIQUIDATE_SEIZE_TOO_MUCH if he tried to liquidate the entire debt.
Path 1: Insufficient seizeTokens--> ( coded as test_liquidationMechanics_path01 )
In this scenario:
-
Imagine that a position becomes liquidatable (has shortfall but not in badDebt). And the
borrowBalanceis aboveminBorrowAmountAllowPartialLiquidation. -
A liquidator attempts a liquidation. This will always be a partial liquidation due to one of these 3 reasons: a. The closeFactorMantissa setting constraints that
repayAmountis no more than 90% of theborrowBalance, even in the best of scenarios. b. Liquidator could choose a partial repayment based on their financial capacity. c. Liquidator could maliciously choose a partial repayment in order to carry out this attack. -
The liquidator is awarded a liquidationIncentive (for e.g., 12%) and is able to seize those collateral cTokens from the borrower.
-
Due to the above, every iteration of partial liquidation worsens the ratio of collateral cTokens to the remaining
borrowBalance( LTV increases with each iteration ). -
Eventually a state is arrived where
borrowBalanceis belowminBorrowAmountAllowPartialLiquidation. Now only full liquidations are allowed and hence every liquidation attempt will revert with errorLIQUIDATE_SEIZE_TOO_MUCH. Note that the debt is still not in the badDebt territory and henceliquidateBadDebt()can't be called yet which wouldn't have cared about awarding anyliquidationIncentive.
Path 2: Transition to Bad Debt--> ( coded as test_liquidationMechanics_path02 )
In the second scenario:
-
Steps 1-4 same as above.
-
Step 5: The worsening ratio with each partial liquidation iteration eventually pushes the debt into the badDebt territory where
borrowBalanceis greater than the remaining collateral. Although someone can callliquidateBadDebt(), it doesn't really offer any incentive to the liquidator. The protocol is already losing money at this point, even if someone cleans up the remaining borrowed balance.
Impact
The impact is severe:
- In Path 1, positions are left with debt that cannot be liquidated due to reverting transactions, leaving the protocol with unclearable bad positions
- In Path 2, positions transition into bad debt and will now be closed at a loss. The protocol's health is worse than before.
Proof of Concept
Add the 2 tests inside Vault.t.sol and run with FOUNDRY_PROFILE=lite forge test --mt test_liquidationMechanics -vv to see them pass:
function test_liquidationMechanics_path01() public {
// Initial setup
vm.startPrank(deployer);
vaultManager.setSellFee(1 ether); // no sell fee
comptroller._setCollateralFactor(cNuma, 0.85 ether); // 85% LTV allowed
vm.stopPrank();
uint collateralAmount = 25 ether;
uint borrowAmount = 20 ether; // 80% LTV to start
deal({token: address(rEth), to: userA, give: collateralAmount});
// First approve and enter the NUMA market
vm.startPrank(userA);
address[] memory t = new address[](1);
t[0] = address(cNuma);
comptroller.enterMarkets(t);
// Buy NUMA with rETH to use as collateral
rEth.approve(address(vault), collateralAmount);
uint numas = vault.buy(collateralAmount, 0, userA);
// Deposit NUMA as collateral
uint cNumaBefore = cNuma.balanceOf(userA);
numa.approve(address(cNuma), numas);
cNuma.mint(numas);
uint cNumas = cNuma.balanceOf(userA) - cNumaBefore;
emit log_named_decimal_uint("Numas deposited =", numas, 18);
emit log_named_decimal_uint("cNumas received =", cNumas, 18);
// Borrow rETH
cReth.borrow(borrowAmount);
uint initialBorrowBalance = cReth.borrowBalanceCurrent(userA);
emit log_named_decimal_uint("Initial borrow =", initialBorrowBalance, 18);
(, , uint shortfall, uint badDebt) = comptroller.getAccountLiquidityIsolate(userA, cNuma, cReth);
assertEq(shortfall, 0, "Unhealthy borrow");
vm.stopPrank();
// Make position liquidatable
vm.startPrank(deployer);
vaultManager.setSellFee(0.90 ether);
// Verify position is liquidatable
(, , shortfall, badDebt) = comptroller.getAccountLiquidityIsolate(userA, cNuma, cReth);
assertGt(shortfall, 0, "Position should be liquidatable");
assertEq(badDebt, 0, "Position shouldn't be in badDebt region");
emit log_named_decimal_uint("Shortfall =", shortfall, 18);
// Set liquidation incentive
comptroller._setLiquidationIncentive(1.12e18); // 12% premium
// Set close factor
comptroller._setCloseFactor(0.9e18); // 90%
vm.stopPrank();
// First liquidation attempt
vm.startPrank(userC); // liquidator
uint borrowBalance = cReth.borrowBalanceCurrent(userA);
uint repayAmount = (borrowBalance * 55) / 100; // repaying 55% of the debt
deal({token: address(rEth), to: userC, give: repayAmount});
rEth.approve(address(vault), repayAmount);
// This should succeed since there's enough collateral for the first liquidation
vault.liquidateLstBorrower(userA, repayAmount, false, false);
emit log_named_decimal_uint("First liquidation repaid =", repayAmount, 18);
uint remainingBorrow = cReth.borrowBalanceCurrent(userA);
emit log_named_decimal_uint("Remaining borrow =", remainingBorrow, 18);
// Only full liquidation allowed now
assertLt(remainingBorrow, vault.minBorrowAmountAllowPartialLiquidation(), "below minBorrowAmountAllowPartialLiquidation");
// Verify again the position is liquidatable but is not in the badDebt region
(, , shortfall, badDebt) = comptroller.getAccountLiquidityIsolate(userA, cNuma, cReth);
emit log_named_decimal_uint("Shortfall2 =", shortfall, 18);
emit log_named_decimal_uint("BadDebt2 =", badDebt, 18);
assertGt(shortfall, 0, "Position2 should be liquidatable");
assertEq(badDebt, 0, "Position2 shouldn't be in badDebt region");
vm.stopPrank();
// temporary hack required to allow full liquidation now since
// `borrowBalance < minBorrowAmountAllowPartialLiquidation`. Needs to be done due
// to existence of a different bug
vm.prank(deployer);
comptroller._setCloseFactor(1e18);
// Second liquidation attempt for remaining debt
vm.startPrank(userC); // liquidator
deal({token: address(rEth), to: userC, give: remainingBorrow});
rEth.approve(address(vault), remainingBorrow);
vm.expectRevert("LIQUIDATE_SEIZE_TOO_MUCH");
vault.liquidateLstBorrower(userA, remainingBorrow, false, false); // @audit-issue : no way to liquidate !
vm.stopPrank();
}
function test_liquidationMechanics_path02() public {
// Initial setup
vm.prank(deployer);
vaultManager.setSellFee(1 ether); // no sell fee
uint collateralAmount = 100 ether;
uint borrowAmount = 80 ether; // 80% LTV to start
deal({token: address(rEth), to: userA, give: collateralAmount});
// First approve and enter the NUMA market
vm.startPrank(userA);
address[] memory t = new address[](1);
t[0] = address(cNuma);
comptroller.enterMarkets(t);
// Buy NUMA with rETH to use as collateral
rEth.approve(address(vault), collateralAmount);
uint numas = vault.buy(collateralAmount, 0, userA);
// Deposit NUMA as collateral
uint cNumaBefore = cNuma.balanceOf(userA);
numa.approve(address(cNuma), numas);
cNuma.mint(numas);
uint cNumas = cNuma.balanceOf(userA) - cNumaBefore;
emit log_named_decimal_uint("Numas deposited =", numas, 18);
emit log_named_decimal_uint("cNumas received =", cNumas, 18);
// Borrow rETH
cReth.borrow(borrowAmount);
uint initialBorrowBalance = cReth.borrowBalanceCurrent(userA);
emit log_named_decimal_uint("Initial borrow =", initialBorrowBalance, 18);
(, , uint shortfall, uint badDebt) = comptroller.getAccountLiquidityIsolate(userA, cNuma, cReth);
assertEq(shortfall, 0, "Unhealthy borrow");
vm.stopPrank();
// Make position liquidatable by manipulating the sell fee
vm.startPrank(deployer);
vaultManager.setSellFee(0.87 ether); // price drop making position liquidatable
// Verify position is liquidatable
(, , shortfall, badDebt) = comptroller.getAccountLiquidityIsolate(userA, cNuma, cReth);
assertGt(shortfall, 0, "Position should be liquidatable");
assertEq(badDebt, 0, "Position shouldn't be in badDebt region");
emit log_named_decimal_uint("Shortfall =", shortfall, 18);
// Set liquidation incentive
comptroller._setLiquidationIncentive(1.12e18); // 12% premium
// Set close factor
comptroller._setCloseFactor(0.85e18); // 85%
vm.stopPrank();
// First liquidation attempt
vm.startPrank(userC); // liquidator
uint borrowBalance = cReth.borrowBalanceCurrent(userA);
uint repayAmount = (borrowBalance * 85) / 100; // 85% of the debt
deal({token: address(rEth), to: userC, give: repayAmount});
rEth.approve(address(vault), repayAmount);
// This should succeed since there's enough collateral for the first liquidation
vault.liquidateLstBorrower(userA, repayAmount, false, false);
emit log_named_decimal_uint("First liquidation repaid =", repayAmount, 18);
// Second liquidation attempt for remaining debt
uint remainingBorrow = cReth.borrowBalanceCurrent(userA);
emit log_named_decimal_uint("Remaining borrow =", remainingBorrow, 18);
// Verify the position again for badDebt
(, , shortfall, badDebt) = comptroller.getAccountLiquidityIsolate(userA, cNuma, cReth);
emit log_named_decimal_uint("Shortfall2 =", shortfall, 18);
emit log_named_decimal_uint("BadDebt2 =", badDebt, 18);
assertGt(badDebt, 0, "Position2 should be in badDebt region"); // @audit-issue : has become badDebt now; unprofitable for liquidators
vm.stopPrank();
}
Output:
Ran 2 tests for contracts/Test/Vault.t.sol:VaultTest
[PASS] test_liquidationMechanics_path01() (gas: 2288863)
Logs:
VAULT TEST
Numas deposited =: 176595.092400931043253778
cNumas received =: 0.000882975462004655
Initial borrow =: 20.000000000000000000
Shortfall =: 1.817974907058021698
redeem? 0
First liquidation repaid =: 11.000000000000000000
Remaining borrow =: 9.000000000000000000
Shortfall2 =: 1.289974907057879019
BadDebt2 =: 0.000000000000000000
[PASS] test_liquidationMechanics_path02() (gas: 1828185)
Logs:
VAULT TEST
Numas deposited =: 706380.369603724173015115
cNumas received =: 0.003531901848018620
Initial borrow =: 80.000000000000000000
Shortfall =: 1.264378335974950255
redeem? 0
First liquidation repaid =: 68.000000000000000000
Remaining borrow =: 12.000000000000000000
Shortfall2 =: 5.573919060292848876
BadDebt2 =: 5.235704273992472501
Suite result: ok. 2 passed; 0 failed; 0 skipped; finished in 52.60s (5.50s CPU time)
Mitigation
Add the following 2 checks:
-
If a partial liquidation attempt would result in the debt going into badDebt territory, then it should not be allowed. Full liquidations should be allowed in such cases with a reduced liquidation incentive applicable. The code should allow
repayAmount = borrowBalanceand bypass thecloseFactorMantissaconstraint ( or set it temporarily to1e18). -
If a full liquidation attempt ( possible when
borrowBalance < minBorrowAmountAllowPartialLiquidation) would result inseizeTokensto be greater than the cToken collateral balance of the borrower, then the liquidator should still be allowed to go ahead and be awarded all the available cTokens in borrower's balance.
This possibility of a reduced liquidation incentive should be properly documented so that liquidators know the risk in advance.
Discussion
tibthecat
I think it works like compound and as intended for the bad debt part which can be profitable only if there is way to swap collateral at profit on some LP.
About that comment in the dashboard:
_"Thank you for the detailed analysis.
For part 2, this was discussed in the "Counterproductive Incentives" section here. This is the liquidation design of Compound.
For part 1, if I'm understanding correctly, due to the existance of minBorrowAmountAllowPartialLiquidation, should be valid. Example: Collateral=100, Debt=95, incentive=12%, minBorrowAmountAllowPartialLiquidation=200. Then liquidators can only try to liquidate the whole position, which would fail due to not enough collateral (95*112% = 106.4 > 100).
Changing this to medium severity."_
I disagree because our goal IS that liquidator liquidate the maximum possible. Because of our maxProfitPerliquidation, we don't want liquidators to split their liquidations. We added that parameter (minBorrowAmountAllowPartialLiquidation) in case there would not be enough liquidity on the chain to perform the liquidation.
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 logic does not account for the fact that partial liquidations with a liquidation incentive worsen the collateral-to-debt ratio, and it lacks checks to allow full liquidation with reduced incentive when the remaining debt would otherwise become unliquidatable or bad debt.
Impact
Positions can become unliquidatable (reverting transactions) or transition into bad debt, causing the protocol to hold unclearable debt or realise losses.
What the attacker needed
A liquidatable position; borrow balance above minBorrowAmountAllowPartialLiquidation; liquidator performs partial liquidation
The fix
The mitigation suggests adding checks to prevent partial liquidations that would lead to bad debt, allowing full liquidation with reduced incentive, and permitting seizure of all available collateral when seizeTokens exceeds the borrower's balance.
How it was found
- Create a position that is liquidatable but not in bad debt, with borrow balance above the minimum for partial liquidation.
- Perform a partial liquidation, repaying part of the debt and seizing collateral with a liquidation incentive.
- Repeat partial liquidations until the remaining borrow balance falls below the minimum for partial liquidation (Path 1) or the position enters bad debt (Path 2).
- In Path 1, attempt a full liquidation of the remaining debt; it reverts with LIQUIDATE_SEIZE_TOO_MUCH because there is not enough collateral to seize after adding the incentive.
- In Path 2, the position is now in bad debt and can only be liquidated via liquidateBadDebt, which offers no incentive, so the protocol loses money.
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.
When a borrower's collateral is worth less than their debt, anyone can liquidate them: repay part of the debt and receive some collateral at a discount. This report shows that if you liquidate only part of the debt, the remaining debt can become impossible to liquidate (the system refuses because there isn't enough collateral to seize) or can turn into 'bad debt' where the collateral is worth less than the debt, so no one wants to liquidate it. That leaves the protocol stuck with losses.
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.