# Liquidations will leave dust when repaying expired maturities, making it impossible to clear bad debt putting the protocol at a risk of insolvency

Rounding in Market.liquidate's penalty-adjusted repay calc leaves dust collateral on expired maturities, so clearBadDebt can never be triggered, risking insolvency.

- Payout: Audit contest
- Severity: medium
- Program: Exactly Stacking Contracts
- Found by: 0x73696d616f
- Disclosed: 2024-07-17
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-07-exactly-stacking-contracts-judging/issues/69
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-07-exactly-stacking-contracts%3AM-10
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:medium tech:solidity technique:source-review vuln:accounting-error vuln:liquidation vuln:precision-loss

**Target:** Market.sol liquidate() / Auditor.checkLiquidation
**Root cause:** When debt > maxAssets, actualRepay = maxAssets.mulDivDown(position, debt) rounds down, repaying less than maxAssets and leaving residual dust collateral that successive liquidations cannot fully clear.
**Steps:**
1. Deposit collateral and borrowAtMaturity with a maturity that will expire.
2. skip past maturity so penalty rate accrues and debt exceeds collateral.
3. Call `market.liquidate(BOB, 100_000 ether, market)` repeatedly.
4. Observe collateral never reaches zero; final liquidation reverts with ZERO_WITHDRAW because actualRepayAssets rounds to zero.
**How found:** Code reading of the actualRepay computation in Market::liquidate, spotting the mulDivDown rounding when debt > maxAssets, then writing a Foundry PoC to confirm dust remains and liquidation eventually reverts.
**Exploitation:** Liquidator repeatedly calls liquidate on an underwater expired-maturity position; each call repays slightly less than maxAssets due to mulDivDown rounding, leaving dust collateral. clearBadDebt() is never reachable, so bad debt persists and protocol absorbs insolvency.
**Insight:** When a liquidation caps repay by scaling with mulDivDown, check whether repeated max-repay liquidations can asymptotically approach but never reach zero collateral; dust below the rounding granularity blocks bad-debt-clearing paths that require full collateral removal.

## Proof of concept (copied exactly from the original report)

**Foundry PoC test showing liquidation dust reverts**

```
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();
}
```

