Skip to content
Business logic Audit finding Intermediate

Order will be executed with wrong slippage if by the time of execution, price crosses take profit

An order's direction is set at creation based on the current exchange rate relative to the take-profit price. If the price moves past the take-profit before execution, the order may be treated as a stop order, applying the stop slippage instead of the take-profit slippage, which could cause higher losses if stop slippage is larger.

LonWof-Demon Follow with ami, bughuntoor and 1 other

Originally disclosed on Sherlock

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

Found by

LonWof-Demon, ami, bughuntoor, covey0x07

Summary

An order's direction is determined by whether the current exchangeRate is below or above the takeProfit price

Solidity
        orders[existingOrderId] = Order({
            orderId: existingOrderId,
            takeProfit: takeProfit,
            stopPrice: stopPrice,
            amountIn: amountIn,
            tokenIn: tokenIn,
            tokenOut: tokenOut,
            recipient: recipient,
            takeProfitSlippage: takeProfitSlippage,
            feeBips: feeBips,
            stopSlippage: stopSlippage,
            direction: MASTER.getExchangeRate(tokenIn, tokenOut) > takeProfit //exchangeRate in/out > takeProfit
        });

However, this could be problematic, in the case where by the time the user's transaction is executed, the price moves beyond the takeProfit price and changes direction.

Solidity
    function checkInRange(
        Order memory order
    )
        internal
        view
        returns (bool inRange, bool takeProfit, uint256 exchangeRate)
    {
        exchangeRate = MASTER.getExchangeRate(order.tokenIn, order.tokenOut);
        if (order.direction) {
            //check for take profit price
            if (exchangeRate <= order.takeProfit) {
                return (true, true, exchangeRate);
            }
            //check for stop price
            if (exchangeRate >= order.stopPrice) {
                return (true, false, exchangeRate);
            }
        } else {
            //check for take profit price
            if (exchangeRate >= order.takeProfit) {
                return (true, true, exchangeRate);
            }
            //check for stop price
            if (exchangeRate <= order.stopPrice) {
                return (true, false, exchangeRate);
            }
        }
    }

In this case both of the takeProfit and stopPrice would be on the same side of the price, which would also make it instantly executable. The only problem is that it would be treated as a stop, rather than a takeProfit, which would result in using the stopSlippage , instead of the takeProfitSlippage.

In the case where the stopSlippage is higher than the takeProfit slippage, this would expose the user to a higher loss of funds than expected.

Root Cause

Wrong direction logic.

Attack Path

  1. Current WETH/ USDC price is $3000
  2. User creates an offer with tp $3100 and stopPrice $2800. takeProfitSlippage is 5% and stopPriceSlippage is 15%
  3. By the time the user's tx is executed, WETH's price goes above $3100. This changes the order's direction.
  4. Both of the tp price and stop price are below the current price.
  5. The order will be executed as a stop price, using the 15% slippage. This exposes the user to extra 10% loss.

Impact

Using wrong slippage parameter.

Affected Code

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

Mitigation

Allow the user to specify the direction.

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 LonWof-Demon. 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 order direction is determined at creation time using the current exchange rate, and not updated if the price moves past the take-profit before execution.

Impact

The order could be executed with the wrong slippage parameter, potentially causing a higher loss of funds than expected if stop slippage is greater than take-profit slippage.

What the attacker needed

User creates an order with take-profit and stop price; Price moves past take-profit before execution; Stop slippage is higher than take-profit slippage

The fix

The protocol team fixed this issue in the referenced PRs/commits.

How it was found

  1. Current WETH/USDC price is $3000
  2. User creates an offer with take-profit $3100 and stop price $2800, take-profit slippage 5% and stop slippage 15%
  3. By the time the user's transaction is executed, WETH's price goes above $3100, changing the order's direction
  4. Both the take-profit and stop prices are below the current price
  5. The order is executed as a stop price, using the 15% slippage, exposing the user to an extra 10% loss

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 sets up an order with a take-profit price (to lock in gains) and a stop price (to limit losses). The order's direction is fixed when created, based on the current price. If the price moves past the take-profit before the order executes, the order might be treated as a stop, using a different slippage setting that could be higher, leading to a larger loss than intended.

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 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.