Immunefi Audit Competitions
Plether | Audit Competition

#98557 [SC-Low] MED-28 fix introduces a new bug: `PositionProtectionBook` reads `getLatestPrice(MarkRefresh)`, so stop-loss / take-profit creation, replacement and protected opens revert after any delayed-order fill

Submitted on Nov 8th 2026 at 17:31:49 UTC by @VivaxiLabs for Audit Competition | Plether

Description

Brief/Intro

The MED-28 remediation makes PositionProtectionBook check new and replaced trigger prices against the accepted on-chain oracle observation. It relies on a synchronization guard, _acceptedMarkInstalled(), added in PR #120 (8191921). That guard explicitly accepts the state in which the Engine mark is newer than the accepted live basket (acceptedTime < markTime).

PR #124 (eadc5c5) then inserted an observation read before that guard: PletherOracle.getLatestPrice(PriceMode.MarkRefresh). That view enforces publish ordering and reverts with PletherOracle__PriceOutOfOrder whenever the accepted basket is older than engine.lastMarkTime().

That state follows every ordinary delayed-order fill. The Router installs each fill's signed historical tick as the Engine mark, while the accepted basket only moves on live paths (mark refresh, liquidation, LP settlement, protection triggers).

As a result, after any fill newer than the last live refresh, the following calls revert until somebody pays for a new live refresh, and the next fill blocks them again:

  • createPositionProtection
  • replacePositionProtection
  • commitOpenOrderWithProtection

The guard's permissive branch is therefore dead code on this path. Meanwhile, the Book's own attached-protection path (afterOrderTerminal) arms protections in exactly this state.

Cecuro MED-28 is marked Acknowledged / Addressed. Under the program's known-issue rule, this report shows that the fix at the frozen commit introduces a new bug. At 8191921, the same function (then _freshCachedProtectionMark) validated against the Engine mark behind the same guard, so this state was accepted. The revert only appears with the read added in #124. The recommended fix restores the #120 behaviour for this single state and keeps the #124 observation everywhere else.

Vulnerability Details

1. The observation read happens before the guard and enforces ordering. packages/perps/src/PositionProtectionBook.sol#L862-L886:

IPletherOracle.PriceSnapshot memory current = oracle.getLatestPrice(IPletherOracle.PriceMode.MarkRefresh); // L880 (added in #124)
currentMarkPrice = current.markPrice;
currentPublishTime = current.publishTime;
if (!_acceptedMarkInstalled()) {            // L883 (added in #120)
    revert PositionProtectionBook__OracleMarkAhead();
}

2. getLatestPrice(MarkRefresh) refuses an accepted basket older than the Engine mark. The call goes through _getLatestPriceSnapshotWithConfidence → _snapshotFromBasket(..., enforcePublishOrder = true), at packages/perps/src/PletherOracle.sol#L892-L896:

uint64 markTime = engine.lastMarkTime();
if (snapshot.publishTime < markTime) {
    revert PletherOracle__PriceOutOfOrder(snapshot.publishTime, markTime);
}

3. The guard that follows defines exactly that state as synchronized. PositionProtectionBook.sol#L888-L895:

(bool accepted, uint256 acceptedPrice, uint64 acceptedTime) = _activeOracle().getAcceptedBasketMark();
if (!accepted) { return true; }
uint64 markTime = ENGINE.lastMarkTime();
return acceptedTime < markTime || (acceptedTime == markTime && acceptedPrice == ENGINE.lastMarkPrice());

On the create, replace and protected-open path, both permissive branches are unreachable because the read in step 1 reverts first:

  • !accepted hits PletherOracle__NoAcceptedBasket (PletherOracle.sol#L711-L712);
  • acceptedTime < markTime hits PriceOutOfOrder.

4. The rest of the codebase and docs treat "Engine mark ahead" as valid.

  • Attached-protection arming. afterOrderTerminal (PositionProtectionBook.sol#L596-L602) arms an attached protection right after its parent fill, in exactly this state: Engine mark = fill tick, newer than the accepted basket. It uses the same guard without the observation read, and records ENGINE.lastMarkTime().
  • Docs: what makes arming fail. WORKING_WITH_AI_AGENTS.md#L270-L272 documents arming failure only "if the accepted live basket is newer than the Engine mark or disagrees at the same timestamp".
  • Docs: what arming records. ORACLE_SYNCHRONIZATION.md#L55 says: "Protection arming records the Engine oracle time".
  • MED-28 remediation comment. It states that the guard "rejects arming when the accepted oracle mark is ahead of, or inconsistent with, the Engine mark". The state here is the opposite: the accepted mark is behind an Engine mark that is itself a signed, on-chain observation installed by the Router.

The MED-17 comment says creation and replacement "use the accepted current oracle observation". Using that observation is fine. The bug is that the read reverts when the observation is older than the already-installed Engine mark. MED-28's sentence "A qualifying mark must be installed before arming can proceed" is satisfied here, since by the guard's own definition the Engine mark is installed and qualifying.

5. This state is the protocol's steady state after fills.

  • OrderRouterExecutionSidecar._updateEngineMark (L1053-L1061) installs every strictly newer historical fill tick as the Engine mark.
  • Fills never write the accepted basket.
  • After a delayed-order execution, engine.lastMarkTime() > acceptedTime holds until the next live refresh. The trader's own fill is enough, and so is any other trader's.

6. All three entry points share the observation: _create (L229), _replace (L295) and _stageAttached (L463). No Book entry point accepts price update data.

The repository's AA client builds create-protection and replace-protection as a single Book call with no prior refresh (packages/perps-aa-client/src/protection.ts#L47-L55). That client is out of scope and is cited only as evidence of the intended integration path.

Step-by-step scenario. No attacker is required.

  1. t0: a live updateMarkPrice installs the accepted basket and the Engine mark at t0.
  2. ALICE commits an open at t0. It fills on the first post-commit tick at t0+2, and the Router installs the Engine mark at t0+2. The accepted basket stays at t0.
  3. ALICE calls createPositionProtection. It reverts with PletherOracle__PriceOutOfOrder(t0, t0+2), and ALICE remains unprotected.
  4. In the same state, BOB's commitOpenOrderWithProtection reverts the same way.
  5. Someone pays for a new live refresh at t0+4, and ALICE arms her protection. As soon as BOB's ordinary order fills at t0+6, ALICE can no longer tighten her stop: replacePositionProtection reverts with PriceOutOfOrder(t0+4, t0+6).
  6. Fuzzing over any fill tick within the default 15 s settlement window gives the same result.

Impact Details

Stop-loss and take-profit management is unavailable after every delayed-order fill, until the next live mark refresh. This covers creation, replacement and the attached-protection open. It typically hits a trader right after their own open fills, which is when they want to arm a stop.

The documented integration path fails with an oracle ordering error. To proceed, the user, or their wallet software, must first send a separate permissionless updateMarkPrice with a fresh signed Pyth payload, paying the Pyth fee and gas. A smart account can batch that refresh with the Book call atomically. A non-atomic flow is re-broken by any fill that lands in between.

No funds are taken, and positions can still be closed manually. The protection feature that the MED-28 remediation explicitly kept available does not work in the state that its own guard accepts.

This matches Low — "Contract fails to deliver promised returns, but doesn't lose value".

Recommendation

Keep the MED-28 observation read wherever it is meaningful, and use the installed Engine mark only in the state the guard already accepts: an accepted basket strictly older than the Engine mark. Every other case behaves exactly as today:

  • no accepted basket;
  • equal timestamps;
  • an accepted basket ahead of the Engine;
  • a stale accepted basket at equal time.
         ) {
             revert OrderRouter__ProtectionMarkTooStale();
         }
-        IPletherOracle.PriceSnapshot memory current = oracle.getLatestPrice(IPletherOracle.PriceMode.MarkRefresh);
-        currentMarkPrice = current.markPrice;
-        currentPublishTime = current.publishTime;
+        (bool accepted,, uint64 acceptedTime) = oracle.getAcceptedBasketMark();
+        if (accepted && acceptedTime < markTime) {
+            // A delayed-order fill installed a newer signed tick; the guard below accepts this state.
+            currentMarkPrice = cachedMarkPrice;
+            currentPublishTime = markTime;
+        } else {
+            IPletherOracle.PriceSnapshot memory current = oracle.getLatestPrice(IPletherOracle.PriceMode.MarkRefresh);
+            currentMarkPrice = current.markPrice;
+            currentPublishTime = current.publishTime;
+        }
         if (!_acceptedMarkInstalled()) {
             revert PositionProtectionBook__OracleMarkAhead();
         }

The Engine mark has already passed the freshness check a few lines above (ProtectionMarkTooStale), and it is newer than every accepted observation, so threshold validation against it is at least as current as the MED-28 design intends.

Verification of the fix (Forge 1.5.1, diff applied to src/PositionProtectionBook.sol):

  • In the PoC state, all three entry points succeed and the protection is Armed. The PoC's expectReverts no longer trigger, and the control still passes.
  • These MED-28 guarantees still hold: a take-profit already crossed at the installed mark reverts with OrderRouter__ProtectionTriggerAlreadyMet, and an accepted basket ahead of the Engine reverts with PositionProtectionBook__OracleMarkAhead.
  • These repository suites pass with the patch, 143/143 in total: spec/trader/PositionProtection.t.sol (104), spec/trader/OrderSettlementFloor.t.sol (17), spec/keeper/OrderRouterRiskOff.t.sol (14) and spec/keeper/PositionProtectionLiquidationBatch.t.sol (8).

References

Proof of Concept

Proof of Concept

The PoC runs inside the repository's own Foundry suite, at the frozen commit, with no in-scope contract modified. It reuses the repository's OracleSynchronizationTestBase, which deploys the real OrderRouter, its sidecars, CfdEngine, MarginClearinghouse, PletherOracle and the Router-created PositionProtectionBook. Only Pyth, which is out of scope, is the repository's MockPyth, driven with signed-style (publishTime, prevPublishTime) payloads.

Setup.

git clone --recurse-submodules https://github.com/immunefi-team/audit-comp-plether.git
cd audit-comp-plether
git checkout --detach a82d4fefb02768e5b3bddcc97dee371eee24fea8
git submodule update --init --recursive
# save the file below as packages/perps/test/perps/poc/L02_ProtectionObservationOrdering.t.sol
forge test --root packages/perps --match-path test/perps/poc/L02_ProtectionObservationOrdering.t.sol -vv

Expected output (Forge 1.5.1 as required by the program, fresh clone at the frozen commit, default 256 fuzz runs; first compile ≈ 1.5 min):

Ran 5 tests for test/perps/poc/L02_ProtectionObservationOrdering.t.sol:L02_ProtectionObservationOrderingPoC
[PASS] testFuzz_PoC_AnyNewerFillBlocksCreation(uint256) (runs: 256, μ: 3160085, ~: 3160108)
[PASS] test_Control_CreateSucceedsAfterExtraLiveRefresh() (gas: 3732813)
[PASS] test_PoC_CreateProtectionRevertsWhenEngineMarkIsAheadOfAcceptedBasket() (gas: 3170718)
[PASS] test_PoC_ProtectedOpenCommitRevertsInTheSameState() (gas: 4330868)
[PASS] test_PoC_ReplaceRevertsAfterAnotherTradersFill() (gas: 5515810)
Suite result: ok. 5 passed; 0 failed; 0 skipped; finished in 947.37ms (960.48ms CPU time)
Ran 1 test suite in 948.35ms (947.37ms CPU time): 5 tests passed, 0 failed, 0 skipped (5 total tests)

With FOUNDRY_FUZZ_RUNS=10000 ... --match-test testFuzz:

[PASS] testFuzz_PoC_AnyNewerFillBlocksCreation(uint256) (runs: 10000, μ: 3160084, ~: 3160108)

Test file: packages/perps/test/perps/poc/L02_ProtectionObservationOrdering.t.sol

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

import {OracleSynchronizationTestBase} from "../shared/OracleSynchronizationTestBase.sol";
import {CfdTypes} from "@plether/perps/CfdTypes.sol";
import {OrderV3Types} from "@plether/perps/OrderV3Types.sol";
import {IPletherOracle} from "@plether/perps/interfaces/IPletherOracle.sol";
import {IPositionProtectionActions} from "@plether/perps/interfaces/IPositionProtectionActions.sol";
import {IPositionProtectionViews} from "@plether/perps/interfaces/IPositionProtectionViews.sol";
import {PositionProtectionTypes} from "@plether/perps/interfaces/PositionProtectionTypes.sol";

/// @title  PoC — stop-loss / take-profit creation, replacement and protected opens revert after any delayed-order fill
/// @notice Uses the repository's own OracleSynchronizationTestBase (real OrderRouter, sidecars, CfdEngine,
///         MarginClearinghouse, PletherOracle and the Router-created PositionProtectionBook). Only Pyth is mocked
///         (out of scope), with signed-style payloads carrying (publishTime, prevPublishTime).
///         No in-scope contract is modified.
/// @dev    Run from the repository root at commit a82d4fefb02768e5b3bddcc97dee371eee24fea8:
///             forge test --root packages/perps --match-path test/perps/poc/L02_ProtectionObservationOrdering.t.sol -vv
///
///         State reached by ordinary usage: a live mark refresh installs the accepted basket and the Engine mark at
///         t0; a delayed order then fills on its first post-commit tick (t0 + 2) and the Router installs that tick
///         as the Engine mark. The accepted live basket stays at t0. `PositionProtectionBook._acceptedMarkInstalled()`
///         explicitly treats `acceptedTime < markTime` as synchronized, but `_freshProtectionObservation()` first
///         calls `getLatestPrice(MarkRefresh)`, which rejects any basket older than `engine.lastMarkTime()`.
contract L02_ProtectionObservationOrderingPoC is OracleSynchronizationTestBase {

    uint256 internal constant TAKE_PROFIT = 95_000_000; // ALICE LONG USD profits when the basket falls
    uint256 internal constant STOP_LOSS = 105_000_000;

    uint64 internal t0;

    function _book() internal view returns (address) {
        return address(router.positionProtectionBook());
    }

    function _params() internal pure returns (PositionProtectionTypes.PositionProtectionParams memory params) {
        params.takeProfitTriggerPrice = TAKE_PROFIT;
        params.stopLossTriggerPrice = STOP_LOSS;
        params.closeLimits = PositionProtectionTypes.CloseFinancialLimits({
            maxExecutionNotionalAssets: type(uint256).max,
            maxGrossAccountDebitAssets: type(uint256).max,
            maxActionChargeAssets: type(uint256).max,
            maxExplicitFeesAssets: type(uint256).max
        });
    }

    /// @dev Live refresh at t0, then ALICE's open fills on the first tick after its commit (t0 + 2).
    function _engineMarkAheadOfAcceptedBasket() internal {
        t0 = uint64(block.timestamp);
        router.updateMarkPrice{value: FEE}(_payload(t0, t0 - 1));
        _commit(ALICE, 0);
        _advance(t0 + 3);
        OrderV3Types.ExecutionResult memory fill = router.executeOrder{value: 2 * FEE}(1, _payload(t0 + 2, t0));
        assertEq(uint256(fill.status), uint256(OrderV3Types.LifecycleStatus.Executed), "ALICE position opened");

        (bool accepted,, uint64 acceptedTime) = pletherOracle.getAcceptedBasketMark();
        assertTrue(accepted);
        assertEq(acceptedTime, t0, "accepted live basket");
        assertEq(engine.lastMarkTime(), t0 + 2, "Engine mark advanced by the ordinary delayed-order fill");
    }

    function _boundedOpen(
        uint256 margin
    ) internal view returns (OrderV3Types.OrderRequest memory request) {
        request.clientOrderId = bytes32(uint256(router.nextCommitId()));
        request.side = CfdTypes.Side.LONG;
        request.sizeDelta = 10_000e18;
        request.marginDelta = margin;
        request.targetPrice = 1;
        request.isClose = false;
        request.bounds = OrderV3Types.ExecutionBounds({
            submitBy: uint64(block.timestamp + router.maxExecutionWindowSeconds()),
            executionWindowSeconds: uint32(router.maxExecutionWindowSeconds()),
            allowedExecutionModes: 1 | 2 | 4,
            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
        });
    }

    /// @notice 1. A trader who just opened cannot arm a stop-loss / take-profit.
    function test_PoC_CreateProtectionRevertsWhenEngineMarkIsAheadOfAcceptedBasket() public {
        _engineMarkAheadOfAcceptedBasket();

        IPositionProtectionActions book = IPositionProtectionActions(_book());
        PositionProtectionTypes.PositionProtectionParams memory params = _params();
        vm.prank(ALICE);
        vm.expectRevert(abi.encodeWithSelector(IPletherOracle.PletherOracle__PriceOutOfOrder.selector, t0, t0 + 2));
        book.createPositionProtection(params);
        assertEq(IPositionProtectionViews(_book()).activePositionProtectionId(ALICE), 0, "ALICE stays unprotected");
    }

    /// @notice 2. In the same state, another trader cannot open with an attached protection.
    function test_PoC_ProtectedOpenCommitRevertsInTheSameState() public {
        _engineMarkAheadOfAcceptedBasket();

        IPositionProtectionActions book = IPositionProtectionActions(_book());
        OrderV3Types.OrderRequest memory request = _boundedOpen(1000e6);
        PositionProtectionTypes.PositionProtectionParams memory params = _params();
        vm.prank(BOB);
        vm.expectRevert(abi.encodeWithSelector(IPletherOracle.PletherOracle__PriceOutOfOrder.selector, t0, t0 + 2));
        book.commitOpenOrderWithProtection(request, params);
    }

    /// @notice 3. An already-protected trader cannot tighten her stop after ANOTHER trader's ordinary fill.
    function test_PoC_ReplaceRevertsAfterAnotherTradersFill() public {
        _engineMarkAheadOfAcceptedBasket();
        _advance(t0 + 4);
        router.updateMarkPrice{value: FEE}(_payload(t0 + 4, t0 + 2)); // someone pays for an extra live refresh
        IPositionProtectionActions book = IPositionProtectionActions(_book());
        PositionProtectionTypes.PositionProtectionParams memory params = _params();
        vm.prank(ALICE);
        uint64 protectionId = book.createPositionProtection(params);

        _commit(BOB, 0);
        _advance(t0 + 7);
        router.executeOrder{value: 2 * FEE}(2, _payload(t0 + 6, t0 + 4)); // BOB's ordinary order fills
        assertEq(engine.lastMarkTime(), t0 + 6);

        PositionProtectionTypes.PositionProtectionParams memory tighter = _params();
        tighter.stopLossTriggerPrice = 102_000_000;
        vm.prank(ALICE);
        vm.expectRevert(abi.encodeWithSelector(IPletherOracle.PletherOracle__PriceOutOfOrder.selector, t0 + 4, t0 + 6));
        book.replacePositionProtection(protectionId, tighter);
    }

    /// @notice Any fill tick inside the default 15 s settlement window blocks creation.
    function testFuzz_PoC_AnyNewerFillBlocksCreation(
        uint256 tickSeed
    ) public {
        t0 = uint64(block.timestamp);
        router.updateMarkPrice{value: FEE}(_payload(t0, t0 - 1));
        _commit(ALICE, 0);
        uint64 tick = uint64(bound(tickSeed, 1, 15));
        _advance(t0 + tick + 1);
        router.executeOrder{value: 2 * FEE}(1, _payload(t0 + tick, t0));

        IPositionProtectionActions book = IPositionProtectionActions(_book());
        PositionProtectionTypes.PositionProtectionParams memory params = _params();
        vm.prank(ALICE);
        vm.expectRevert(abi.encodeWithSelector(IPletherOracle.PletherOracle__PriceOutOfOrder.selector, t0, t0 + tick));
        book.createPositionProtection(params);
    }

    /// @notice Control: the same creation succeeds only once someone pays for a NEW live refresh.
    function test_Control_CreateSucceedsAfterExtraLiveRefresh() public {
        _engineMarkAheadOfAcceptedBasket();
        _advance(t0 + 4);
        router.updateMarkPrice{value: FEE}(_payload(t0 + 4, t0 + 2));
        IPositionProtectionActions book = IPositionProtectionActions(_book());
        PositionProtectionTypes.PositionProtectionParams memory params = _params();
        vm.prank(ALICE);
        uint64 protectionId = book.createPositionProtection(params);
        assertGt(protectionId, 0);
    }

}

What each test proves.

  • test_PoC_CreateProtectionRevertsWhenEngineMarkIsAheadOfAcceptedBasket: right after her own open fills, ALICE cannot arm a stop-loss / take-profit. The call reverts with PriceOutOfOrder(t0, t0+2), and no protection exists.
  • test_PoC_ProtectedOpenCommitRevertsInTheSameState: in the same state, BOB cannot open with an attached protection.
  • test_PoC_ReplaceRevertsAfterAnotherTradersFill: an already-protected trader cannot tighten her stop after another trader's ordinary fill.
  • testFuzz_PoC_AnyNewerFillBlocksCreation: any fill tick from 1 to 15 s after the refresh blocks creation.
  • test_Control_CreateSucceedsAfterExtraLiveRefresh: the same call succeeds only after someone pays for an extra live refresh. The parameters are valid; only the observation ordering blocks them.