# Liquidation residual debt and share accounting

Liquidation paths that leave negative collateral, leftover shares, stale rate state, or residual position after a partial fill

_Area: DeFi oracles, liquidations and flash loans. Seen in 4 disclosed reports; latest 2026-02-05. Updated 2026-11-14._

## How it works
Liquidation is supposed to leave the borrower's account in a clean, fully-accounted state: debt zeroed or reduced by exactly what was seized, collateral reduced by exactly what was paid out, and any shortfall either written off explicitly or socialized. The broken assumption is that a liquidation path "finishes" the position. In practice a path can reduce protocol-level totals while leaving the borrower's per-user state nonzero, leave collateral negative without socializing it, skip updating a rate accumulator, or treat a partial fill as a full close. Each time, the books no longer balance: sum of user balances diverges from actual funds, or a position that should be closed stays open and re-liquidatable.

## Where it shows up
- Liquidation code that subtracts leftover shares from pair/market totals but not from the borrower's share balance [E2](https://bugbountylibrary.com/r/c4%3A2022-08-frax%3AH-01.md).
- Paths allowing an account to end with negative collateral, with no reset-to-zero or socialization step [E1](https://bugbountylibrary.com/r/sherlock%3A2023-07-perennial%3AM-4.md).
- Partial/emergency liquidation that zeroes one accumulator (e.g. dailyRateCollateralBalance) but not the rate accumulator (accLoanRatePerSeconds) [E3](https://bugbountylibrary.com/r/sherlock%3A2024-02-leverage-contracts%3AM-2.md).
- "Full close" conditions based on position existence or array length rather than remaining amount == 0 [E4](https://bugbountylibrary.com/r/c4%3A2025-08-gte-perps-and-launchpad%3AM-15.md).
- Order types (IOC) used where full fill is assumed [E4](https://bugbountylibrary.com/r/c4%3A2025-08-gte-perps-and-launchpad%3AM-15.md).
- Zero-share or zero-amount liquidation calls that still mutate totals [E2](https://bugbountylibrary.com/r/c4%3A2022-08-frax%3AH-01.md).

## How to review
For every liquidation exit path, check the invariant: after the call, borrower debt and collateral are consistent with what was seized, and any shortfall is explicitly zeroed or socialized. Verify that every reduction of protocol-level totals has a matching reduction of the borrower's per-user balance [E2](https://bugbountylibrary.com/r/c4%3A2022-08-frax%3AH-01.md). Confirm no path can leave negative collateral unaddressed [E1](https://bugbountylibrary.com/r/sherlock%3A2023-07-perennial%3AM-4.md). Check that all accumulators touched by fee math are updated together [E3](https://bugbountylibrary.com/r/sherlock%3A2024-02-leverage-contracts%3AM-2.md). For close-out logic, require remaining amount == 0 (or a FOK order) before writing off margin [E4](https://bugbountylibrary.com/r/c4%3A2025-08-gte-perps-and-launchpad%3AM-15.md).

## How finders spotted it
- Reasoned about the invariant that sum of user collateral equals protocol funds, then checked whether liquidation can break it via negative collateral [E1](https://bugbountylibrary.com/r/sherlock%3A2023-07-perennial%3AM-4.md).
- Read liquidateClean and noticed leftover shares subtracted from pair totals but not from the user's borrowed shares [E2](https://bugbountylibrary.com/r/c4%3A2022-08-frax%3AH-01.md).
- Read the repay() fee path, saw partial emergency liquidation collect collateral without updating accLoanRatePerSeconds, then confirmed with a modified test and console.logs that collateralBalance stayed negative and the position remained liquidatable [E3](https://bugbountylibrary.com/r/sherlock%3A2024-02-leverage-contracts%3AM-2.md).
- Read the full-close condition in backstopLiquidate and noticed IOC semantics don't guarantee filling position.amount [E4](https://bugbountylibrary.com/r/c4%3A2025-08-gte-perps-and-launchpad%3AM-15.md).

## Variants seen
- Negative collateral never socialized, enabling a bank run [E1](https://bugbountylibrary.com/r/sherlock%3A2023-07-perennial%3AM-4.md).
- Leftover shares persisting, enabling repeated zero-share liquidations [E2](https://bugbountylibrary.com/r/c4%3A2022-08-frax%3AH-01.md).
- Stale rate accumulator causing double fees and unfair liquidation [E3](https://bugbountylibrary.com/r/sherlock%3A2024-02-leverage-contracts%3AM-2.md).
- Partial IOC fill treated as full close, leaving residual position and protocol loss [E4](https://bugbountylibrary.com/r/c4%3A2025-08-gte-perps-and-launchpad%3AM-15.md).

## Impact and severity
Medium to high. Outcomes ranged from locked lending pairs harming all lenders and borrowers [E2](https://bugbountylibrary.com/r/c4%3A2022-08-frax%3AH-01.md), to bank runs and loss for last withdrawers [E1](https://bugbountylibrary.com/r/sherlock%3A2023-07-perennial%3AM-4.md), unfair liquidation and excess fees [E3](https://bugbountylibrary.com/r/sherlock%3A2024-02-leverage-contracts%3AM-2.md), and direct protocol loss [E4](https://bugbountylibrary.com/r/c4%3A2025-08-gte-perps-and-launchpad%3AM-15.md). Impact scales with bad-debt size and repeatability of the no-op liquidation [E1](https://bugbountylibrary.com/r/sherlock%3A2023-07-perennial%3AM-4.md)[E2](https://bugbountylibrary.com/r/c4%3A2022-08-frax%3AH-01.md).

## Evidence
- E1: https://bugbountylibrary.com/r/sherlock%3A2023-07-perennial%3AM-4.md
- E2: https://bugbountylibrary.com/r/c4%3A2022-08-frax%3AH-01.md
- E3: https://bugbountylibrary.com/r/sherlock%3A2024-02-leverage-contracts%3AM-2.md
- E4: https://bugbountylibrary.com/r/c4%3A2025-08-gte-perps-and-launchpad%3AM-15.md
