Skip to content
Business logic Audit finding Intermediate

`StopLimit` order cannot be filled under certain condition

A StopLimit order that passed the minimum order size check at creation can fail to fill later if the token's price drops, because the resulting Bracket order re-checks the minimum size and reverts. This prevents the StopLimit order from being executed.

xiaoming90 Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-11-oku-judging/issues/449

Found by

xiaoming90

Summary

No response

Root Cause

No response

Internal pre-conditions

No response

External pre-conditions

No response

Attack Path

Assume that the MASTER.checkMinOrderSize is 100 USD. Assume that the current USDC price is 1.1 USD per USDC.

Bob creates a StopLimit order with order.tokenIn = USDC, order.amountIn = 100e6 (100 USDC), and order.stopLimitPrice = 100 USD. During the order creation, the MASTER.checkMinOrderSize function will be executed and the total USD value is 110 USD (100 USDC * 1.1 USD). Thus, the check will pass as it is over the minimum size of 100 USD.

https://github.com/sherlock-audit/2024-11-oku/blob/main/oku-custom-order-types/contracts/automatedTrigger/AutomationMaster.sol#L144

Solidity
File: AutomationMaster.sol
142:     ///@notice determine if a new order meets the minimum order size requirement
143:     ///Value of @param amountIn of @param tokenIn must meed the minimum USD value
144:     function checkMinOrderSize(IERC20 tokenIn, uint256 amountIn) external view override {
145:         uint256 currentPrice = oracles[tokenIn].currentValue();
146:         uint256 usdValue = (currentPrice * amountIn) /
147:             (10 ** ERC20(address(tokenIn)).decimals());
148: 
149:         require(usdValue > minOrderSize, "order too small");
150:     }

When the price of USDC drops from 1.1 to 0.9, Bob's StopLimit order will be in range, and the performUpkeep function will be executed to fill the order. A new bracket order will be created in Line 126 below, as per the instructions of Bob's StopLimit order, and the 100 USDC within the StopLimit order will be transferred to the newly created Bracket order.

https://github.com/sherlock-audit/2024-11-oku/blob/main/oku-custom-order-types/contracts/automatedTrigger/StopLimit.sol#L126

Solidity
File: StopLimit.sol
075:     function performUpkeep(
076:         bytes calldata performData
077:     ) external override nonReentrant {
..SNIP..
089:         //confirm order is in range to prevent improper fill
090:         (bool inRange, ) = checkInRange(order);
091:         require(inRange, "order ! in range");
..SNIP..
124: 
125:         //create bracket order
126:         BRACKET_CONTRACT.fillStopLimitOrder(
127:             swapPayload,
128:             order.takeProfit,
129:             order.stopPrice,
130:             order.amountIn,
..SNIP..
140:         );

The Bracket.fillStopLimitOrder function will execute Bracket._createOrder function internally. However, the issue is that when the Bracket._createOrder function is executed to create a new Bracket order, it will perform a minimum order size check again at Line 473 below. Since the total value of 100 USDC is only worth 90 USD, which is below the minimum order size of 100 USD. Thus, the transaction will revert. As a result, Bob's StopLimit cannot be filled due to the revert.

https://github.com/sherlock-audit/2024-11-oku/blob/main/oku-custom-order-types/contracts/automatedTrigger/Bracket.sol#L473

Solidity
File: Bracket.sol
444:     function _createOrder(
445:         uint256 takeProfit,
446:         uint256 stopPrice,
447:         uint256 amountIn,
448:         uint96 existingOrderId,
449:         IERC20 tokenIn,
450:         IERC20 tokenOut,
451:         address recipient,
452:         uint16 feeBips,
453:         uint16 takeProfitSlippage,
454:         uint16 stopSlippage
455:     ) internal {
..SNIP..
473:         MASTER.checkMinOrderSize(tokenIn, amountIn);

Impact

Medium. Loss of core functionality under certain conditions.

PoC

No response

Mitigation

Consider allowing the minimum order size check to be skipped if the order creation is initiated by the StopLimit contract when filling the StopLimit order. In this case, the Bracket order will be created without issues in the above described scenario.

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/gfx-labs/oku-custom-order-types/pull/1

The original report as published on Sherlock, by xiaoming90. 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

The minimum order size check is performed both when the StopLimit order is created and again when the resulting Bracket order is created, without accounting for price changes between the two events.

Impact

The StopLimit order cannot be filled, causing a loss of core functionality under certain price conditions.

What the attacker needed

StopLimit order created when token price is high; Token price drops below threshold; Order becomes in range

The fix

The minimum order size check should be skipped when the order creation is initiated by the StopLimit contract during filling.

How it was found

  1. Bob creates a StopLimit order with 100 USDC when USDC price is 1.1 USD, passing the minimum size check.
  2. USDC price drops to 0.9 USD, making the order in range.
  3. performUpkeep is called to fill the StopLimit order.
  4. The StopLimit contract calls Bracket.fillStopLimitOrder, which calls _createOrder.
  5. _createOrder calls checkMinOrderSize, which now fails because 100 USDC is worth only 90 USD.
  6. The transaction reverts, and the StopLimit order cannot be filled.

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 user creates a StopLimit order, which is an order that triggers when a price condition is met. The system checks that the order is large enough in dollar terms when it is created. Later, if the token's price falls, the order may become too small in dollar terms, and when the system tries to fill it, it checks again and rejects it, so the order never executes.

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 xiaoming90

View all

122 more disclosed reports, best first.

More from Oku's New Order Types Contract 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.