Skip to content
Business logic Audit finding Intermediate

StopLoss/TakeProfit should be validated again for the new price in Trading.executeLimitOrder()

The executeLimitOrder function updates the trade price for stop orders but does not re-validate the stop-loss and take-profit levels against the new price. An attacker could have a limit order execute at a price that immediately triggers the take-profit, causing an unexpected loss for the user.

hansfriese Follow with bin2chen

Originally disclosed on Code4rena

Submitted by hansfriese, also found by bin2chen

The open price of a stop order might be changed during execution but it doesn’t validate StopLoss/TakeProfit for the changed price.

As a result, the executed market order might be closed immediately and there would be an unexpected loss for users.

Proof of Concept

As we can see from executeLimitOrder(), the open price might be changed to the current price for the stop order.

File: 2022-12-tigris\contracts\Trading.sol
480:     function executeLimitOrder(
481:         uint _id,
482:         PriceData calldata _priceData,
483:         bytes calldata _signature
484:     )
485:         external
486:     {
487:         unchecked {
488:             _checkDelay(_id, true);
489:             tradingExtension._checkGas();
490:             if (tradingExtension.paused()) revert TradingPaused();
491:             require(block.timestamp >= limitDelay[_id]);
492:             IPosition.Trade memory trade = position.trades(_id);
493:             uint _fee = _handleOpenFees(trade.asset, trade.margin*trade.leverage/1e18, trade.trader, trade.tigAsset, true);
494:             (uint256 _price, uint256 _spread) = tradingExtension.getVerifiedPrice(trade.asset, _priceData, _signature, 0);
495:             if (trade.orderType == 0) revert("5");
496:             if (_price > trade.price+trade.price*limitOrderPriceRange/DIVISION_CONSTANT || _price < trade.price-trade.price*limitOrderPriceRange/DIVISION_CONSTANT) revert("6"); //LimitNotMet
497:             if (trade.direction && trade.orderType == 1) {
498:                 if (trade.price < _price) revert("6"); //LimitNotMet
499:             } else if (!trade.direction && trade.orderType == 1) {
500:                 if (trade.price > _price) revert("6"); //LimitNotMet
501:             } else if (!trade.direction && trade.orderType == 2) {
502:                 if (trade.price < _price) revert("6"); //LimitNotMet
503:                 trade.price = _price;
504:             } else {
505:                 if (trade.price > _price) revert("6"); //LimitNotMet
506:                 trade.price = _price; //@audit check sl/tp
507:             }
508:             if(trade.direction) {
509:                 trade.price += trade.price * _spread / DIVISION_CONSTANT;
510:             } else {
511:                 trade.price -= trade.price * _spread / DIVISION_CONSTANT;
512:             }

But it doesn’t validate sl/tp again for the new price so the order might have an invalid sl/tp.

The new price wouldn’t satisfy the sl/tp requirements when the price was changed much from the original price due to the high slippage and the order might be closed immediately by sl or tp in this case.

Originally, the protocol validates stoploss only but I say to validate both of stoploss and takeprofit. (I submitted it as another issue to validate tp as well as sl).

Recommended Mitigation Steps

Recommend validating sl/tp for the new trade.price in Trading.executeLimitOrder().

TriHaz (Tigris Trade) disputed and commented:

The open price of a stop order might be changed during execution

Limit orders open price is guaranteed, so it will not be changed, so validating sl/tp again is not needed.

Alex the Entreprenerd (judge) commented:

@TriHaz can you please check the following line

504:             } else {
505:                 if (trade.price > _price) revert("6"); //LimitNotMet
506:                 trade.price = _price; //@audit check sl/tp
507:             }

and re-affirm your dispute?

Ultimately it looks like trade.price is changed to the new price from the feed, which is a “correct” price, but may not be a price the caller was originally willing to act on (not in range with SL / TP).

TriHaz (Tigris Trade) confirmed and commented:

Yes my review was not correct, the price for the stop orders are not guaranteed which makes this issue valid.

Alex the Entreprenerd (judge) commented:

The warden has shown how, due to a lack of check, limit orders that pass the logic check may be executed even though the validation for their Stop Loss / Take Profit may not be hit

Given the level of detail I believe the finding to be of Medium Severity.

GainsGoblin (Tigris Trade) resolved:

Mitigation: https://github.com/code-423n4/2022-12-tigris/pull/2#issuecomment-1419177423

Since this issue only affects TP and not SL, I only added a check for that.


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

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 code updates trade.price for stop orders without re-checking that the stop-loss and take-profit values are still valid relative to the new price.

Impact

The executed market order might be closed immediately, causing an unexpected loss for users.

What the attacker needed

A stop order with SL/TP; price moves significantly

The fix

The issue was fixed by adding a check for take-profit only, since the problem affects TP and not SL.

How it was found

  1. A user places a stop order with stop-loss and take-profit levels.
  2. The order is executed via executeLimitOrder and the price is updated to the current market price.
  3. The new price is far from the original, making the stop-loss or take-profit invalid.
  4. The position is closed immediately, causing an unexpected 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.

In trading, a stop-loss and take-profit are safety orders that automatically close a position at certain prices. This report says that when a special type of order (a stop order) is executed, the system updates the entry price but forgets to check that the safety orders still make sense for the new price. So the position could be closed right away at a loss.

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 hansfriese

View all

65 more disclosed reports, best first.

More from Tigris Trade 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.