An open or increase never lowers the liquidation reserve to its new target, so collateral the spec defines as PnL pledge is held as reserve, positions are liquidated early and the excess is charged to the trader or taken from LPs
The open/increase planner keeps the liquidation reserve at the larger of the previous reserve and the newly recomputed target, so after an increase at a favourable price the excess collateral stays locked as reserve instead of being credited to PnL pledge. This makes positions liquidatable while their spec equity is still above maintenance, overcharges LONGs at liquidation, and returns excess to SHORT traders at the expense of LPs.
Originally disclosed on Immunefi
- Disclosed after under an hour
#98795 [SC-Medium] an open or increase never lowers the liquidation reserve to its new target, so collateral the spec defines as PnL pledge is held as reserve, positions are liquidated early and the excess is charged to the trader or taken from LPs
Submitted on Nov 10th 2026 at 08:09:06 UTC by @farismaulana for Audit Competition | Plether
- Report ID: #98795
- Report Type: Smart Contract
- Report severity: Medium
- Target: https://github.com/immunefi-team/audit-comp-plether/tree/a82d4fefb02768e5b3bddcc97dee371eee24fea8/packages/perps/src
- Impacts:
- Griefing (e.g. no profit motive for an attacker, but damage to the users or the protocol)
Description
Brief/Intro
every open or same-side increase recomputes the liquidation reserve target from the resulting notional, and the spec says collateral beyond that target is PnL pledge. the planner instead keeps max(previous reserve, new target). after an increase at a price that lowers the target, the excess stays locked in the reserve.
health checks count only pledge and claim, so the trapped excess is invisible to them. the position becomes liquidatable while its spec equity is still above maintenance. at liquidation the excess raises the charge for a LONG, and for a SHORT liquidated with a loss above the pledge it goes back to the trader instead of to LPs. in the PoC a 1_000_000 USDG SHORT that adds 12 lots after a 10% move keeps 98.92 USDG of excess, is liquidatable at an equity of 883.76 against a spec equity of 982.68, and leaves LPs 98.92 USDG short.
Vulnerability Details
the spec defines the target after an open or increase and classifies everything above it (ACCOUNTING_SPEC.md#L806-L813):
After an open/increase, the target liquidation reserve is:
max(minBountyAssets, floor(resultingNotionalAssets * bountyBps / 10_000))Approved position collateral beyond that target is PnL pledge. A reduction recomputes the target and releases excess reserve; price movement alone never permits a liquidation charge to consume PnL pledge.
the open planner raises the new target back to the old reserve whenever the target is lower (CfdEnginePlanLib.sol#L551-L556):
delta.liquidationReserveBeforeAssets = snap.liquidationReserveAssets;
delta.liquidationReserveAfterAssets = delta.openState.liquidationReserveTargetAssets;
@> if (delta.liquidationReserveAfterAssets < delta.liquidationReserveBeforeAssets) {
@> delta.liquidationReserveAfterAssets = delta.liquidationReserveBeforeAssets;
}
uint256 reserveIncreaseAssets = delta.liquidationReserveAfterAssets - delta.liquidationReserveBeforeAssets;
and the settlement sidecar only ever moves collateral into the reserve on an open (CfdEngineSettlementSidecar.sol#L480-L484):
@> if (delta.liquidationReserveAfterAssets > delta.liquidationReserveBeforeAssets) {
clearinghouse.reclassifyPnlPledgeToLiquidationReserve(
delta.account, delta.liquidationReserveAfterAssets - delta.liquidationReserveBeforeAssets
);
}
the same open path follows the target in both directions for the other reserve it manages. when the VPI rebate reserve target falls in an open, the excess is released in that fill (CfdEnginePlanLib.sol#L348-L355, CfdEngineSettlementSidecar.sol#L436-L439). the liquidation reserve is the only one that ratchets.
the stored reserve therefore depends on the path, not on the position. a SHORT of 10_000 lots at 1.00 sets a 1_000 USDG reserve. after +12 lots at 0.90 the target is 0.1% * 10_012 * 100 * 0.90 = 901.08 USDG, but the reserve stays 1_000. an account that opens the same 10_012 lots directly at 0.90 holds exactly 901.08. the excess is bountyBps / 1e4 * (N * P1 - (N + K) * P2): 98.92 USDG per 1_000_000 notional after a 10% move, and it grows with notional, the size of the move and every further increase.
the excess then distorts three things.
- health ignores it. price health is computed on
pos.margin + traderClaimBalance(CfdEnginePlanLib.sol#L1461), so collateral that the spec classifies as pledge is missing from equity. in the PoC the SHORT is first liquidatable at an equity of 883.762256 against a maintenance of 883.765066. counting the excess as pledge, its equity is 982.682256 and it is not liquidatable. the LONG case is the same: 1_004.270282 against 1_004.274424, and 1_103.190282 with the excess. - the charge consumes it. the liquidation charge is capped at the stored reserve (CfdEnginePlanLib.sol#L1495-L1503). a LONG liquidated at its first liquidatable price pays 1_000 USDG where its target allows 901.08, so the trader loses 98.92 USDG of what the spec calls pledge to the keeper and LPs. that is exactly what "price movement alone never permits a liquidation charge to consume PnL pledge" rules out.
- bad debt returns it to the trader. price seizure is capped at the pledge, and whatever is left in the reserve after the charge is zeroed with no settlement debit (MarginClearinghouse.sol#L1279-L1285), so it stays in the trader's free settlement:
uint256 releasedLiquidationReserveAssets = liquidationReserveBalances[account];
if (releasedLiquidationReserveAssets > 0) {
@> liquidationReserveBalances[account] = 0;
emit MarginUnlocked(
account, IMarginClearinghouse.MarginBucket.LiquidationReserve, releasedLiquidationReserveAssets
);
}
when the loss exceeds the pledge, the excess that should have been seized as pledge is handed back to the trader and the write-off to LPs is larger by the same amount. in the PoC two SHORT accounts with the same size and the same pledge + reserve (119_198.70 USDG) are liquidated at the same price and pay the same 770.924 USDG charge. the ratchet account keeps 98.92 USDG more and the pool receives 98.92 USDG less (118_584.16 against 118_683.08). a SHORT liquidated after a 0.111% gap below its first liquidatable price already produces a 99.93 USDG write-off, with the full 98.92 excess returned to the trader.
Impact Details
- positions are liquidated while their equity, as the spec classifies it, is above maintenance. in the PoC the band is 98.92 USDG of equity per 1_000_000 notional after a 10% move, and the trader loses the position and pays the full liquidation charge.
- a LONG liquidated with the excess in its reserve pays 98.92 USDG more charge than its target allows, split between the keeper and LPs.
- a SHORT liquidated with a loss above its pledge keeps 98.92 USDG that the spec assigns to pledge, and LPs absorb the same amount as an extra write-off.
- every account that increases after a favourable move carries the excess, it compounds over repeated increases, and it persists until a reduction recomputes the target.
severity: Medium, Griefing (no profit motive for an attacker, but damage to the users or the protocol). no attacker is needed: ordinary increases put every such account in this state, the users lose positions and liquidation charge they should keep, and LPs absorb larger write-offs.
Recommendation
set the reserve to the recomputed target on every open, and release any excess to PnL pledge in the same fill, the same way the VPI rebate reserve is handled:
delta.liquidationReserveBeforeAssets = snap.liquidationReserveAssets;
delta.liquidationReserveAfterAssets = delta.openState.liquidationReserveTargetAssets;
- if (delta.liquidationReserveAfterAssets < delta.liquidationReserveBeforeAssets) {
- delta.liquidationReserveAfterAssets = delta.liquidationReserveBeforeAssets;
- }
- uint256 reserveIncreaseAssets = delta.liquidationReserveAfterAssets - delta.liquidationReserveBeforeAssets;
+ uint256 reserveIncreaseAssets = delta.liquidationReserveAfterAssets > delta.liquidationReserveBeforeAssets
+ ? delta.liquidationReserveAfterAssets - delta.liquidationReserveBeforeAssets
+ : 0;
and in CfdEngineSettlementSidecar move liquidationReserveBeforeAssets - liquidationReserveAfterAssets back to PnL pledge when the target is lower.
References
Proof of Concept
Proof of Concept
the test deploys the in-scope contracts through the existing BasePerpTest harness at release risk parameters, with the release Router minimum open notional of 1_000 USDG installed through the production timelock. only Pyth (MockPyth) and USDG (mock token) are mocked. every trade is committed through the production Router and executed by a keeper, marks use OrderRouter.updateMarkPrice, and liquidations use OrderRouter.executeLiquidation.
- create
packages/perps/test/perps/poc/LiquidationReserveRatchet.t.solwith the content below (no harness changes are needed) - from
packages/perps, run:
forge test --match-path test/perps/poc/LiquidationReserveRatchet.t.sol -vv
tests:
test_increaseKeepsReserveAboveTarget: the reserve stays 1_000 against a 901.08 target, and a control opening the same final position gets the targettest_negativeControl_increaseAtHigherPrice_reserveEqualsTarget: an increase that raises the target sets the reserve exactly to ittest_shortBadDebtLiquidation_excessReturnedToTrader: same size, same pledge + reserve, same charge; the trader keeps 98.92 more and LPs receive 98.92 lesstest_shortRealisticGap_lpShortfallEqualsExcess: early liquidation band and LP shortfall after a 0.111% gaptest_longLiquidation_extraCharge: the LONG pays 98.92 more charge, with the same early liquidation bandtest_reductionReleasesExcess: a reduction recomputes the target, and the same open fill releases the VPI reserve while keeping the liquidation reserve excess
// SPDX-License-Identifier: AGPL-3.0
pragma solidity 0.8.35;
import {BasePerpTest} from "../BasePerpTest.sol";
import {CfdTypes} from "@plether/perps/CfdTypes.sol";
import {OrderV3Types} from "@plether/perps/OrderV3Types.sol";
import {ICfdEngineTypes} from "@plether/perps/interfaces/ICfdEngineTypes.sol";
import {IOrderRouterAdminHost} from "@plether/perps/interfaces/IOrderRouterAdminHost.sol";
import {console2} from "forge-std/console2.sol";
/// @title PoC: an open/increase keeps the liquidation reserve at max(previous reserve, new target).
/// @notice CfdEnginePlanLib._planOpenReserveFunding (CfdEnginePlanLib.sol:551-556) sets the post-open reserve to the
/// recomputed target and then raises it back to the previous reserve whenever the target is lower. After a
/// same-side increase at a lower price the stored reserve therefore stays above the target that
/// ACCOUNTING_SPEC.md:806-811 defines, and the excess is never credited to PnL pledge.
/// @dev Local deploy of the repo contracts at a82d4fef (BasePerpTest) with release risk parameters
/// (DEPLOYMENT.md:126-165) and the release Router values minOpenNotionalAssets = 1,000 USDG and
/// adverseConfidenceMultiplierBps = 2,500 (DEPLOYMENT.md:147-148), installed through the production 72 h
/// OrderRouterAdmin timelock. Only Pyth (MockPyth) and USDG (MockSettlementToken) are mocked. Every trade goes
/// through the production Router FIFO (LegacyOrderRouterHarness.commitOrder -> production V3 commitOrder, then a
/// keeper calls OrderRouter.executeOrder). Marks use the permissionless OrderRouter.updateMarkPrice, liquidations
/// the permissionless OrderRouter.executeLiquidation, and the control's top-up the trader-only CfdEngine.addMargin.
contract LiquidationReserveRatchetTest is BasePerpTest {
address internal constant RATCHET = address(0xA11CE);
address internal constant CONTROL = address(0xC0C0);
address internal constant OTHER = address(0xB0B);
address internal constant KEEPER = address(0x4EE9);
uint256 internal constant P1 = 1e8; // 1.00
uint256 internal constant P2 = 0.9e8; // 0.90, a 10% fall
uint256 internal constant P_UP = 1.1e8; // negative control: increase at a higher price
uint256 internal constant DEEP_PRICE = 0.77e8; // common price that puts BOTH A/B accounts past their pledge
uint256 internal constant N_LOTS = 10_000; // 1,000,000 USDG notional at 1.00 (1 lot = 1e20 size = 100 tokens)
uint256 internal constant K_LOTS = 12; // 1,080 USDG at 0.90 >= release minOpenNotionalAssets (1,000 USDG)
uint256 internal constant SHORT_DEPOSIT = 150_000e6;
uint256 internal constant SHORT_MARGIN = 120_000e6; // survives the 10% adverse move before the increase
uint256 internal constant LONG_DEPOSIT = 20_000e6;
uint256 internal constant LONG_MARGIN = 6_000e6;
uint256 internal constant INCREASE_MARGIN = 100e6;
uint256 internal constant CONTROL_MARGIN = 5_000e6; // control opens small, then tops up to the same collateral
/// @dev Mirrors ICfdEngineSettlementSidecar.PriceLossWrittenOff (ICfdEngineSettlementSidecar.sol:25) for expectEmit.
event PriceLossWrittenOff(address indexed account, uint256 amountAssets);
struct LiqOutcome {
uint256 price;
uint256 reserveBefore;
uint256 pledgeBefore;
uint256 charge;
uint256 keeperBounty;
uint256 lpFee;
int256 equity;
uint256 maintenance;
uint256 traderFreeBefore;
uint256 traderFreeAfter;
uint256 traderSettlementBefore;
uint256 traderSettlementAfter;
uint256 poolCashIn;
uint256 keeperCredit;
uint256 writeOff;
uint256 reserveAfter;
}
// ---------------------------------------------------------------------
// Release parameters (DEPLOYMENT.md:126-165)
// ---------------------------------------------------------------------
function _riskParams() internal pure override returns (CfdTypes.RiskParams memory) {
return CfdTypes.RiskParams({
vpiFactor: 0.01e18,
maxSkewRatio: 0.4e18,
maintMarginBps: 10,
initMarginBps: 20,
fadMarginBps: 300,
baseCarryBps: 500,
minBountyAssets: 1e6,
bountyBps: 10,
keeperShareBps: 5000,
protocolShareBps: 0
});
}
/// @dev 10M USDG Junior depth so a 1M position fits maxSkewRatio 0.4 and the settlement buffer.
function _initialJuniorDeposit() internal pure override returns (uint256) {
return 10_000_000e6;
}
function setUp() public override {
super.setUp();
assertEq(engine.executionFeeBps(), 4, "release executionFeeBps");
// Release Router values (DEPLOYMENT.md:147-148) through the production 72 h timelock.
IOrderRouterAdminHost.RouterConfig memory config = _routerConfig();
config.minOpenNotionalAssets = 1_000e6;
config.adverseConfidenceMultiplierBps = 2_500;
_setRouterConfig(config);
assertEq(router.minOpenNotionalAssets(), 1_000e6, "release minOpenNotionalAssets");
_mark(P1);
}
// ---------------------------------------------------------------------
// 1. The increase keeps the reserve above the recomputed target
// ---------------------------------------------------------------------
/// @notice SHORT N lots at 1.00, then +K lots at 0.90. The stored reserve stays 1,000 USDG while the recomputed
/// target is 901.08 USDG. A control that opens the same final size at 0.90 gets exactly the target.
function test_increaseKeepsReserveAboveTarget() public {
_fundTrader(RATCHET, SHORT_DEPOSIT);
_fill(RATCHET, CfdTypes.Side.SHORT, N_LOTS, SHORT_MARGIN, false, P1);
uint256 r1 = clearinghouse.liquidationReserveAssets(RATCHET);
assertEq(r1, _target(N_LOTS, P1), "open at 1.00 sets reserve = target");
assertEq(r1, 1_000e6, "R1 = 1,000 USDG");
_mark(P2);
uint256 pledgeBefore = clearinghouse.pnlPledgeAssets(RATCHET);
_fill(RATCHET, CfdTypes.Side.SHORT, K_LOTS, INCREASE_MARGIN, false, P2);
uint256 pledgeAfter = clearinghouse.pnlPledgeAssets(RATCHET);
uint256 reserveAfter = clearinghouse.liquidationReserveAssets(RATCHET);
uint256 target = _target(N_LOTS + K_LOTS, P2);
assertEq(target, 901_080_000, "spec target after increase = 901.08 USDG");
assertEq(reserveAfter, r1, "increase leaves the reserve at the previous value");
assertGt(reserveAfter, target, "stored reserve > recomputed target");
uint256 excess = reserveAfter - target;
// Bound: (bountyBps / 1e4) * (N*P1 - (N+K)*P2) in USDG atoms.
assertEq(excess, ((N_LOTS * P1 - (N_LOTS + K_LOTS) * P2) * 10) / 10_000, "excess = 0.1% of notional drop");
assertEq(excess, 98_920_000, "excess = 98.92 USDG");
// The excess is not credited to PnL pledge: pledge grows by at most the new margin (less trade cost).
assertLe(pledgeAfter, pledgeBefore + INCREASE_MARGIN, "pledge not credited with the excess");
// Control: the same final size opened directly at 0.90.
_fundTrader(CONTROL, SHORT_DEPOSIT);
_fill(CONTROL, CfdTypes.Side.SHORT, N_LOTS + K_LOTS, CONTROL_MARGIN, false, P2);
uint256 controlReserve = clearinghouse.liquidationReserveAssets(CONTROL);
(uint256 sizeR,,,,,,) = engine.positions(RATCHET);
(uint256 sizeC,,,,,,) = engine.positions(CONTROL);
assertEq(sizeR, sizeC, "same final size");
assertEq(controlReserve, target, "control reserve == target");
console2.log("===== test_increaseKeepsReserveAboveTarget (SHORT, 1M notional, 10% fall) =====");
console2.log("reserve after open of 10,000 lots @1.00 (R1) ", r1);
console2.log("recomputed target, 10,012 lots @0.90 ", target);
console2.log("ratchet reserve after +12 lots @0.90 ", reserveAfter);
console2.log("control reserve, 10,012 lots opened @0.90 ", controlReserve);
console2.log("excess held as reserve instead of PnL pledge ", excess);
console2.log("ratchet pledge before/after increase ", pledgeBefore, pledgeAfter);
}
/// @notice Negative control from the candidate: an increase at a HIGHER price makes the target exceed the old
/// reserve, so max(before, target) == target and no excess exists.
function test_negativeControl_increaseAtHigherPrice_reserveEqualsTarget() public {
_fundTrader(RATCHET, SHORT_DEPOSIT);
_fill(RATCHET, CfdTypes.Side.SHORT, N_LOTS, SHORT_MARGIN, false, P1);
_mark(P_UP);
// The reserve now has to GROW by 101.32 USDG, and the planner funds a reserve increase only from the
// order's newly supplied pledge (CfdEnginePlanLib.sol:557-576; else LIQUIDATION_RESERVE_UNFUNDED = code 9).
// So this increase supplies 500 USDG of margin instead of INCREASE_MARGIN.
_fill(RATCHET, CfdTypes.Side.SHORT, K_LOTS, 500e6, false, P_UP);
uint256 reserve = clearinghouse.liquidationReserveAssets(RATCHET);
assertEq(reserve, _target(N_LOTS + K_LOTS, P_UP), "reserve == target when the target rises");
assertEq(reserve, 1_101_320_000, "target at 1.10 = 1,101.32 USDG");
console2.log("negative control: reserve after +12 lots @1.10 == target", reserve);
}
// ---------------------------------------------------------------------
// 2. SHORT bad-debt liquidation: the excess goes back to the trader, not to LPs
// ---------------------------------------------------------------------
/// @notice A/B at a common liquidation price past both accounts' pledge. Both have the same size and the same
/// total position collateral (pledge + reserve); only the reserve/pledge split differs. The ratchet account
/// gets the excess back as free settlement and the pool receives exactly the excess less.
function test_shortBadDebtLiquidation_excessReturnedToTrader() public {
uint256 snap = vm.snapshotState();
// A: ratchet path.
_fundTrader(RATCHET, SHORT_DEPOSIT);
_fill(RATCHET, CfdTypes.Side.SHORT, N_LOTS, SHORT_MARGIN, false, P1);
_mark(P2);
_fill(RATCHET, CfdTypes.Side.SHORT, K_LOTS, INCREASE_MARGIN, false, P2);
uint256 totalCollateral =
clearinghouse.pnlPledgeAssets(RATCHET) + clearinghouse.liquidationReserveAssets(RATCHET);
LiqOutcome memory a = _liquidate(RATCHET, DEEP_PRICE);
vm.revertToState(snap);
// B: control, same final size opened at 0.90, topped up to the same pledge + reserve.
_mark(P2);
_fundTrader(CONTROL, SHORT_DEPOSIT);
_fill(CONTROL, CfdTypes.Side.SHORT, N_LOTS + K_LOTS, CONTROL_MARGIN, false, P2);
_equalizeCollateral(CONTROL, totalCollateral);
LiqOutcome memory b = _liquidate(CONTROL, DEEP_PRICE);
uint256 target = _target(N_LOTS + K_LOTS, P2);
uint256 excess = a.reserveBefore - target;
_logOutcome("A: RATCHET (10,000 @1.00 then +12 @0.90)", a);
_logOutcome("B: CONTROL (10,012 @0.90, same pledge + reserve)", b);
assertEq(a.reserveBefore, 1_000e6, "A reserve = R1");
assertEq(b.reserveBefore, target, "B reserve = target");
assertEq(a.pledgeBefore + a.reserveBefore, b.pledgeBefore + b.reserveBefore, "same total collateral");
assertEq(b.pledgeBefore - a.pledgeBefore, excess, "B pledge = A pledge + excess");
assertEq(a.charge, b.charge, "same charge (same size, same price)");
assertEq(a.charge, ((N_LOTS + K_LOTS) * DEEP_PRICE * 10) / 10_000, "charge = notional(P_liq) * 10 bps");
assertLt(a.charge, target, "SHORT: charge at a lower price is below the target");
assertGt(a.writeOff, excess, "A: loss above pledge is written off (> excess)");
assertGt(b.writeOff, 0, "B: loss above pledge is written off");
assertEq(a.reserveAfter, 0, "reserve bucket zeroed");
// Trader side: the leftover reserve (incl. the excess) stays in the trader's free settlement.
uint256 aFreeGain = a.traderFreeAfter - a.traderFreeBefore;
uint256 bFreeGain = b.traderFreeAfter - b.traderFreeBefore;
assertApproxEqAbs(aFreeGain, a.reserveBefore - a.charge, 0.1e6, "A: free settlement gains R1 - charge");
assertApproxEqAbs(bFreeGain, b.reserveBefore - b.charge, 0.1e6, "B: free settlement gains target - charge");
assertApproxEqAbs(aFreeGain - bFreeGain, excess, 0.1e6, "trader keeps the excess vs control");
// LP side: the pool receives exactly the excess less.
assertApproxEqAbs(b.poolCashIn - a.poolCashIn, excess, 0.1e6, "pool receives excess less vs control");
console2.log("excess (A reserve - target) ", excess);
console2.log("trader free-settlement gain A - B ", aFreeGain - bFreeGain);
console2.log("pool cash received B - A (LP shortfall) ", b.poolCashIn - a.poolCashIn);
}
/// @notice Ratchet account alone, liquidated after a realistic gap below its first liquidatable price (about
/// 0.11%). The loss exceeds pledge + excess, so under the spec classification the excess would have been
/// seized as pledge; here it is unlocked to the trader. Also shows the premature-liquidation band: at the
/// first liquidatable price, equity + excess is still above maintenance.
function test_shortRealisticGap_lpShortfallEqualsExcess() public {
_fundTrader(RATCHET, SHORT_DEPOSIT);
_fill(RATCHET, CfdTypes.Side.SHORT, N_LOTS, SHORT_MARGIN, false, P1);
_mark(P2);
_fill(RATCHET, CfdTypes.Side.SHORT, K_LOTS, INCREASE_MARGIN, false, P2);
uint256 excess = clearinghouse.liquidationReserveAssets(RATCHET) - _target(N_LOTS + K_LOTS, P2);
uint256 pFirst = _firstLiquidatablePriceShort(RATCHET);
_assertPrematureBand(RATCHET, pFirst, excess);
// Price at which loss = pledge + excess + 1 USDG (SHORT loss = entry cost - lots * price).
uint256 pGap = (engine.positionEntryCostAssetAtoms(RATCHET) - clearinghouse.pnlPledgeAssets(RATCHET) - excess
- 1e6) / (N_LOTS + K_LOTS);
LiqOutcome memory o = _liquidate(RATCHET, pGap);
_logOutcome("RATCHET liquidated after a small gap", o);
assertGt(o.writeOff, excess, "write-off exceeds the excess");
uint256 released = o.reserveBefore - o.charge;
assertGt(released, excess, "released reserve includes the full excess");
assertApproxEqAbs(o.traderFreeAfter - o.traderFreeBefore, released, 0.1e6, "released reserve -> trader");
assertApproxEqAbs(o.poolCashIn, o.pledgeBefore + o.lpFee, 0.1e6, "pool cash = whole pledge + LP fee (+carry)");
// Spec counterfactual: pledge would be pledge + excess and the loss covers it, so LPs would seize
// min(loss, pledge + excess) = pledge + excess. Shortfall = min(excess, writeOff) = excess.
assertEq(o.writeOff < excess ? o.writeOff : excess, excess, "LP shortfall vs spec = excess");
console2.log("first liquidatable price (1e8) ", pFirst);
console2.log("liquidation price after gap (1e8) ", pGap);
console2.log("gap below first liquidatable price (0.01 bps) ", ((pFirst - pGap) * 1_000_000) / pFirst);
console2.log("excess ", excess);
console2.log("released reserve to trader (R1 - charge) ", released);
console2.log("of which spec-pledge excess (LP shortfall) ", excess);
}
// ---------------------------------------------------------------------
// 3. LONG liquidation: the excess is consumed as extra liquidation charge
// ---------------------------------------------------------------------
/// @notice LONG N at 1.00 (profits as the price falls), +K at 0.90, then the price rises back to the first
/// liquidatable price. The uncapped charge exceeds R1, so the charge consumes the whole stored reserve,
/// 98.92 USDG more than a reserve equal to the target allows. A/B at the same price and same collateral.
function test_longLiquidation_extraCharge() public {
uint256 snap = vm.snapshotState();
// A: ratchet LONG.
_fundTrader(RATCHET, LONG_DEPOSIT);
_fill(RATCHET, CfdTypes.Side.LONG, N_LOTS, LONG_MARGIN, false, P1);
_mark(P2);
_fill(RATCHET, CfdTypes.Side.LONG, K_LOTS, INCREASE_MARGIN, false, P2);
uint256 target = _target(N_LOTS + K_LOTS, P2);
uint256 r1 = clearinghouse.liquidationReserveAssets(RATCHET);
uint256 excess = r1 - target;
uint256 totalCollateral = clearinghouse.pnlPledgeAssets(RATCHET) + r1;
uint256 pFirst = _firstLiquidatablePriceLong(RATCHET);
// maintMarginBps == bountyBps == 10 at release, so maintenance == uncapped charge at this price.
uint256 uncappedCharge = _assertPrematureBand(RATCHET, pFirst, excess);
assertGt(uncappedCharge, r1, "uncapped charge > stored reserve");
LiqOutcome memory a = _liquidate(RATCHET, pFirst);
vm.revertToState(snap);
// B: control LONG, same size opened at 0.90 and topped up to the same pledge + reserve.
_mark(P2)
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 open planner clamps the post-open reserve to the previous reserve whenever the recomputed target is lower, and the settlement sidecar only ever moves collateral into the reserve, never back out.
Impact
Positions can be liquidated while their spec equity is above maintenance, LONGs pay more liquidation charge than their target allows, and SHORTs liquidated with a loss above their pledge keep excess that LPs then absorb as a larger write-off.
What the attacker needed
No attacker needed; ordinary same-side increase after a favourable move; position large enough to exceed min open notional
The fix
The reserve should be set to the recomputed target on every open and any excess released back to PnL pledge in the same fill, mirroring how the VPI rebate reserve is handled.
How it was found
- Open a SHORT of 10,000 lots at 1.00, which sets a 1,000 USDG reserve.
- Move the mark price down 10% to 0.90.
- Increase the same SHORT by 12 lots at 0.90.
- Observe the stored reserve stays 1,000 USDG while the recomputed target is 901.08 USDG.
- Compare with a control account that opens the same final size directly at 0.90 and gets exactly the target.
- Liquidate both accounts at a common deep price and compare the trader's free settlement and the pool's receipts.
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.
A trading protocol splits a trader's collateral into two buckets: a reserve kept aside to pay for liquidations, and a pledge that counts as the trader's own equity. The code is supposed to shrink the reserve when a position grows at a better price, but it only ever grows it, so money that should count as equity sits in the reserve instead. Because health checks ignore the reserve, the position looks weaker than it really is and can be liquidated too early.
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.