Liquidations will leave dust when repaying expired maturities, making it impossible to clear bad debt putting the protocol at a risk of insolvency
A rounding error in the liquidation logic of a lending protocol causes the actual repaid amount to be less than the maximum allowed, leaving dust collateral that cannot be cleared. This prevents the bad debt from ever being fully cleared, risking protocol insolvency.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-07-exactly-stacking-contracts-judging/issues/69
The protocol has acknowledged this issue.
Found by
0x73696d616f
Summary
Market::liquidate() gets the maximum assets to liquidate from Auditor::checkLiquidation(). Then, when repaying maturities, when they have expired, it calculates how much principal of the maturity it has to liquidate to take into account the penalty rate and liquidate at most maxRepayAssets. However, there is rounding in this process that means the actual repay assets will be smaller than maxRepayAssets, which means the liquidatee will have dust collateral. Thus, it will be impossible to clear the bad debt, even if liquidated again later.
Root Cause
In Market::liquidate(), it calculates the actual repay has:
uint256 debt = position + position.mulWadDown((block.timestamp - maturity) * penaltyRate);
actualRepay = debt > maxAssets ? maxAssets.mulDivDown(position, debt) : maxAssets;
When debt > maxAssets, it will round down in the calculation, liquidating less assets than the maximum and leaving the liquidatee with some dust making it impossible to clear the bad debt.
Internal pre-conditions
- Liquidatee has more debt than collateral and expired maturities.
External pre-conditions
None.
Attack Path
- Liquidatee borrows at maturity and lets it expire, not repaying the debt until it grows bigger than the collateral.
- Liquidator liquidates but it is never possible to fully clear the collateral so
clearBadDebt()will never be called.
Impact
When there are expired maturities and the debt is bigger than the collateral clearBadDebt() will not be called, not even if liquidated successively.
PoC
Place the following test in Market.t.sol. Notice how it reverts due to ZERO_WITHDRAW as the actualRepayAssets is zero.
function test_POC_ClearBadDebt_Impossible_DueToDust() external {
uint256 maxVal = type(uint256).max;
market.deposit(10000 ether, address(this));
vm.startPrank(BOB);
market.deposit(100 ether, BOB);
market.borrowAtMaturity(4 weeks, 60e18, maxVal, BOB, BOB);
vm.stopPrank();
vm.startPrank(ALICE);
market.deposit(10, ALICE);
market.borrowAtMaturity(4 weeks, 1, maxVal, ALICE, ALICE);
vm.stopPrank();
skip(20 weeks);
(uint256 coll, uint256 debt) = market.accountSnapshot(BOB);
console.log(coll);
console.log(debt);
vm.startPrank(ALICE);
market.liquidate(BOB, 100_000 ether, market);
vm.stopPrank();
(coll, debt) = market.accountSnapshot(BOB);
console.log(coll);
console.log(debt);
vm.startPrank(ALICE);
market.liquidate(BOB, 100_000 ether, market);
vm.stopPrank();
(coll, debt) = market.accountSnapshot(BOB);
console.log(coll);
console.log(debt);
vm.startPrank(ALICE);
vm.expectRevert(); // ZERO WITHDRAW
market.liquidate(BOB, 100_000 ether, market);
vm.stopPrank();
}
Mitigation
The mentioned rounding error is hard to deal with, it's easier to set some threshold to clear bad debt so rounding errors can be disregarded.
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
Impact
When there are expired maturities and the debt is bigger than the collateral, clearBadDebt() will not be called, not even if liquidated successively, potentially leading to protocol insolvency.
What the attacker needed
Liquidatee has more debt than collateral; Maturities have expired
The fix
The rounding error is hard to deal with; it's easier to set a threshold to clear bad debt so rounding errors can be disregarded.
How it was found
- Liquidatee borrows at maturity and lets it expire, not repaying until debt exceeds collateral.
- Liquidator calls liquidate, but due to rounding, a small amount of collateral remains.
- Repeated liquidations still leave dust, so clearBadDebt() is never called.
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 debt exceeds their collateral, a liquidator can repay part of the debt to seize collateral. Due to a rounding mistake, the liquidator repays slightly less than needed, leaving a tiny amount of collateral that can never be cleared. This means the bad debt stays on the books, which could make the protocol insolvent.
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.