Limit orders are broken when there are price gaps
Limit orders on GMX fail to execute when the price moves past the trigger price in the block after submission, because the contract requires the primary and secondary oracle prices to straddle the trigger price. An attacker could not directly exploit this, but users with stop-loss orders may be unable to close positions during price gaps, leaving them exposed.
Source: https://github.com/sherlock-audit/2023-02-gmx-judging/issues/140
Found by
IllIllI
Summary
Limit orders fail to execute when the price becomes worse for the order in the block after the order is submitted.
Vulnerability Detail
Limit orders require that all oracle-provided prices come from blocks after the order has been submitted. In addition, limit orders's primary and secondary prices are required to straddle the trigger price.
Impact
If the primary/secondary prices go well past the trigger price (e.g. due to a price gap), then the order execution will revert, leaving the user exposed to their position, even if they had a valid stoploss set.
Code Snippet
Limit orders require prices to straddle the trigger price, and revert if they don't:
// File: gmx-synthetics/contracts/order/BaseOrderUtils.sol : BaseOrderUtils.setExactOrderPrice() #1
238 if (orderType == Order.OrderType.LimitIncrease ||
239 orderType == Order.OrderType.LimitDecrease ||
240 orderType == Order.OrderType.StopLossDecrease
241 ) {
242 uint256 primaryPrice = oracle.getPrimaryPrice(indexToken).pickPrice(shouldUseMaxPrice);
243 uint256 secondaryPrice = oracle.getSecondaryPrice(indexToken).pickPrice(shouldUseMaxPrice);
244
...
258 if (shouldValidateAscendingPrice) {
259 // check that the earlier price (primaryPrice) is smaller than the triggerPrice
260 // and that the later price (secondaryPrice) is larger than the triggerPrice
261 @> bool ok = primaryPrice <= triggerPrice && triggerPrice <= secondaryPrice;
262 if (!ok) {
263 revert InvalidOrderPrices(primaryPrice, secondaryPrice, triggerPrice, shouldValidateAscendingPrice);
264 }
...
270 } else {
271 // check that the earlier price (primaryPrice) is larger than the triggerPrice
272 // and that the later price (secondaryPrice) is smaller than the triggerPrice
273 @> bool ok = primaryPrice >= triggerPrice && triggerPrice >= secondaryPrice;
274 if (!ok) {
275 revert InvalidOrderPrices(primaryPrice, secondaryPrice, triggerPrice, shouldValidateAscendingPrice);
276 }
...
282: }
Tool used
Manual Review
Recommendation
Don't revert if both the primary and secondary prices are worse than the trigger price. Use the trigger price as the execution price.
Discussion
xvi10
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 contract requires the two oracle prices to straddle the trigger price, so when both are worse than the trigger, the order reverts instead of executing at the trigger price.
Impact
Users with limit or stop-loss orders may be unable to execute them during price gaps, leaving their positions unprotected.
What the attacker needed
A limit or stop-loss order; price gap past trigger
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 buy or sell only when the price reaches a certain level. This report found that if the price jumps past that level too quickly, the system refuses to carry out the order, so the user's stop-loss protection does not work as expected.
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.