Skip to content
Business logic Audit finding Advanced

Attacker can post-running attack to prevent LiquidityPool from hedging by orders of PerpMarket

The LiquidityPool's hedging mechanism can be blocked by an attacker who repeatedly opens or closes a small position immediately after the pool's delayed order is executed, causing the pool to submit a new order for only the current trade size and preventing it from submitting the accumulated queued size. This leaves the pool unhedged for subsequent trades.

KIntern_NA Follow

Originally disclosed on Code4rena

Submitted by KIntern_NA

The attacker can post-running attack to keep the LiquidityPool's can’t submit the orders of perpetual for hedging. It leads to every trade of the pool will not be hedged anymore.

Proof of concept

  1. LiquidityPool calls function _hedge every trade, and it triggers function _placeDelayedOrder.
function _placeDelayedOrder(int256 sizeDelta, bool isLiquidation) internal {
    IPerpsV2MarketBaseTypes.DelayedOrder memory order = perpMarket.delayedOrders(address(this));

    (,,,,, IPerpsV2MarketBaseTypes.Status status) =
        perpMarket.postTradeDetails(sizeDelta, 0, IPerpsV2MarketBaseTypes.OrderType.Delayed, address(this));

    int256 oldSize = order.sizeDelta;
    if (oldSize != 0 || isLiquidation || uint8(status) != 0) {
        queuedPerpSize += sizeDelta;
        return;
    }
    perpMarket.submitOffchainDelayedOrderWithTracking(sizeDelta, perpPriceImpactDelta, synthetixTrackingCode);

    emit SubmitDelayedOrder(sizeDelta);
}
  1. In this function, if (oldSize != 0 || isLiquidation || uint8(status) != 0), the pool will accumulate the variable queuedPerpSize and return. Else, the pool will submit a delay order of sizeDelta (the current size delta of the trade) to the Synthetix perpetual market.
  2. The current delay order of Pool will be executed by function executePerpOrders (LiquidityPool.sol#L704). After that, the order size of the pool will return to 0 and the pool can submit the other delayed order.
  3. However, when the pool can submit a new order, function _placeDelayedOrder just submit the order with sizeDelta of the current trade. And the order of queuedPerpSize can only submitted in function placeQueuedOrder (LiquidityPool.sol#L692), but it require the current order size of pool is 0:
function placeQueuedOrder() external requiresAuth nonReentrant {
    IPerpsV2MarketBaseTypes.DelayedOrder memory order = perpMarket.delayedOrders(address(this));

    require(order.sizeDelta == 0);
  1. Therefore, after the executePerpOrders call from the authority, attacker can post-run opening/closing a position, to trigger function _placeDelayedOrder, and make the pool submit the order of the current sizeDelta. Then the order size of the pool will be different from 0, and the pool can’t submit other delay orders, until the next executePerpOrders call. And all the sizes of trades after that will be accumulated into queuedPerpSize.
  2. Attacker can repeat post-running function executePerpOrders with a small trade, to keep the pool can’t submit the necessary order (with queuedPerpSize) for hedging. Then every trade will not be hedged.

Recommended Mitigation Steps

Function _placeDelayedOrder should submit the order of queuedPerpSize + sizeDelta instead of sizeDelta. You can take a look at the following example:

function _placeDelayedOrder(int256 sizeDelta, bool isLiquidation) internal {
    IPerpsV2MarketBaseTypes.DelayedOrder memory order = perpMarket.delayedOrders(address(this));

    (,,,,, IPerpsV2MarketBaseTypes.Status status) =
        perpMarket.postTradeDetails(queuedPerpSize + sizeDelta, 0, IPerpsV2MarketBaseTypes.OrderType.Delayed, address(this));

    int256 oldSize = order.sizeDelta;
    if (oldSize != 0 || isLiquidation || uint8(status) != 0) {
        queuedPerpSize += sizeDelta;
        return;
    }
    perpMarket.submitOffchainDelayedOrderWithTracking(queuedPerpSize + sizeDelta, perpPriceImpactDelta, synthetixTrackingCode);

    emit SubmitDelayedOrder(sizeDelta);
}

mubaris (Polynomial) acknowledged, but disagreed with severity and commented:

There’s a function to manually hedge the pool if it is not hedgePositions(), but I acknowledge the issue but disagree with the severity of this.

Dravee (judge) decreased severity to Medium and commented:

Griefing attack that has a counter. I believe Medium to be right.


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

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 `_placeDelayedOrder` function submits only the current trade's `sizeDelta` instead of including the accumulated `queuedPerpSize`, so a small trade can reset the pending order and block the queued hedge.

Impact

The pool's trades may remain unhedged, exposing it to price risk, though a manual hedging function exists as a countermeasure.

What the attacker needed

Attacker can trade on the pool; attacker can post-run transactions

How it was found

  1. The LiquidityPool calls `_hedge` on every trade, which triggers `_placeDelayedOrder`.
  2. If the pool already has a pending order or other conditions, it adds the size to `queuedPerpSize` and returns without submitting an order.
  3. After the authority calls `executePerpOrders`, the pool's order size becomes zero, allowing a new order.
  4. The attacker post-runs by opening or closing a small position, triggering `_placeDelayedOrder` to submit an order for only the current `sizeDelta`.
  5. This makes the pool's order size non-zero, so it cannot submit the queued order until the next `executePerpOrders` call.
  6. The attacker repeats this after each `executePerpOrders` to keep the pool from hedging the accumulated `queuedPerpSize`.

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 liquidity pool tries to protect itself from price swings by placing hedging orders on a perpetual futures market. The attacker can make a tiny trade right after the pool's hedging order is processed, which tricks the pool into placing a new order for only that tiny trade instead of the larger amount it needs to hedge. This stops the pool from properly hedging until someone manually fixes it.

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 KIntern_NA

View all

21 more disclosed reports, best first.

More from Polynomial Protocol contest

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.