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.
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
LiquidityPoolcalls function_hedgeevery 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);
}
- In this function, if
(oldSize != 0 || isLiquidation || uint8(status) != 0), the pool will accumulate the variablequeuedPerpSizeand return. Else, the pool will submit a delay order ofsizeDelta(the current size delta of the trade) to the Synthetix perpetual market. - 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. - However, when the pool can submit a new order, function
_placeDelayedOrderjust submit the order withsizeDeltaof the current trade. And the order ofqueuedPerpSizecan only submitted in functionplaceQueuedOrder(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);
- Therefore, after the
executePerpOrderscall from the authority, attacker can post-run opening/closing a position, to trigger function_placeDelayedOrder, and make the pool submit the order of the currentsizeDelta. Then the order size of the pool will be different from 0, and the pool can’t submit other delay orders, until the nextexecutePerpOrderscall. And all the sizes of trades after that will be accumulated intoqueuedPerpSize. - Attacker can repeat post-running function
executePerpOrderswith a small trade, to keep the pool can’t submit the necessary order (withqueuedPerpSize) 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.
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
- The LiquidityPool calls `_hedge` on every trade, which triggers `_placeDelayedOrder`.
- If the pool already has a pending order or other conditions, it adds the size to `queuedPerpSize` and returns without submitting an order.
- After the authority calls `executePerpOrders`, the pool's order size becomes zero, allowing a new order.
- The attacker post-runs by opening or closing a small position, triggering `_placeDelayedOrder` to submit an order for only the current `sizeDelta`.
- This makes the pool's order size non-zero, so it cannot submit the queued order until the next `executePerpOrders` call.
- 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.