Skip to content
Business logic Audit finding Advanced

Limit swap orders can be used to get a free look into the future

Limit swap orders could be cancelled after the trader learned the price at which they would execute, allowing a risk-free look at future prices. An attacker could submit a large limit order, observe oracle prices, and cancel if unprofitable, or submit a market order if profitable.

0xdeadbeef Follow with IllIllI and Nyx

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-04-gmx-judging/issues/241

Found by

0xdeadbeef, IllIllI, Nyx

Summary

Users can cancel their limit swap orders to get a free look into prices in future blocks

Vulnerability Detail

This is a part of the same issue that was described in the last contest. The sponsor fixed the bug for LimitDecrease and StopLossDecrease, but not for LimitSwap.

Any swap limit order submitted in block range N can't be executed until block range N+2, because the block range is forced to be after the submitted block range, and keepers can't execute until the price has been archived, which necessarily won't be until after block range N+1. Consider what happens when half of the oracle's block ranges are off from the other half, e.g.:

Text
    1 2 3 4 5 6 7 8 9 < block number
O1: A B B B B C C C D
02: A A B B B B C C C
^^ grouped oracle block ranges

At block 1, oracles in both groups (O1 and O2) are in the same block range A, and someone submits a large swap limit order (N). At block 6, oracles in O1 are in N+2, but oracles in O2 are still in N+1. This means that the swap limit order will execute at the median price of block 5 (since the earliest group to have archive prices at block 6 for N+1 will be O1) and market swap order submitted at block 6 in the other direction will execute at the median price of block 6 since O2 will be the first group to archive a price range that will contain block 6. By the end of block 5, the price for O1 is known, and the price that O2 will get at block 6 can be predicted with high probability (e.g. if the price has just gapped a few cents), so a trader will know whether the two orders will create a profit or not. If a profit is expected, they'll submit the market order at block 6. If a loss is expected, they'll cancel the swap limit order from block 1, and only have to cover gas fees.

Essentially the logic is that limit swap orders will use earlier prices, and market orders (with swaps) will use later prices, and since oracle block ranges aren't fixed, an attacker is able to know both prices before having their orders executed, and use large order sizes to capitalize on small price differences.

Impact

There is a lot of work involved in calculating statistics about block ranges for oracles and their processing time/queues, and ensuring one gets the prices essentially when the keepers do, but this is likely less work than co-located high frequency traders in traditional finance have to do, and if there's a risk free profit to be made, they'll put in the work to do it every single time, at the expense of all other traders.

Code Snippet

Market orders can use the current block, but limit orders must use the next block:

Solidity
// File: gmx-synthetics/contracts/order/SwapOrderUtils.sol : SwapOrderUtils.validateOracleBlockNumbers()   #1

57            if (orderType == Order.OrderType.MarketSwap) {
58 @>             OracleUtils.validateBlockNumberWithinRange(
59                    minOracleBlockNumbers,
60                    maxOracleBlockNumbers,
61                    orderUpdatedAtBlock
62                );
63                return;
64            }
65    
66            if (orderType == Order.OrderType.LimitSwap) {
67 @>             if (!minOracleBlockNumbers.areGreaterThan(orderUpdatedAtBlock)) {
68                    revert Errors.OracleBlockNumbersAreSmallerThanRequired(minOracleBlockNumbers, orderUpdatedAtBlock);
69                }
70                return;
71            }
72    
73            revert Errors.UnsupportedOrderType();
74:       }

https://github.com/sherlock-audit/2023-04-gmx/blob/main/gmx-synthetics/contracts/order/SwapOrderUtils.sol#L57-L74

Tool used

Manual Review

Recommendation

All orders should follow the same block range rules

Discussion

xvi10

fixed in https://github.com/gmx-io/gmx-synthetics/commit/c5fdc2952a8c982a7c215477124a627699c2009c

IllIllI000

https://github.com/gmx-io/gmx-synthetics/commit/c5fdc2952a8c982a7c215477124a627699c2009c The commit changes from a greater-than block number check to a greater-than-or-equal-to check. Array.areGreaterThan() is no longer called anywhere, so this specific issue is resolved. done

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

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 validation for limit swap orders required the oracle block numbers to be strictly greater than the order's update block, while market swaps could use the current block, creating a timing gap that let traders predict execution prices before committing.

Impact

A trader could make risk-free profits at the expense of other traders by exploiting the timing difference between limit and market orders.

What the attacker needed

Ability to submit limit and market swap orders; knowledge of oracle block ranges and timing

The fix

The issue was fixed by changing the block number check from greater-than to greater-than-or-equal-to, so limit orders follow the same block range rules as market orders.

How it was found

  1. Submit a large limit swap order in block range N.
  2. Wait until oracle prices for block range N+1 are known and the price for block range N+2 can be predicted.
  3. If a profit is expected, submit a market order in the opposite direction at block 6.
  4. If a loss is expected, cancel the limit swap order and only pay gas fees.

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 limit order is an instruction to trade only at a certain price or better. Here, the system let a trader place a limit order, wait until they could see the price it would get, and then cancel it if the trade would lose money. That gave them a free peek at future prices without any 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 0xdeadbeef

View all

9 more disclosed reports, best first.

More from GMX Update

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.