Skip to content
DoS Audit finding Intermediate

`MarginDex::execute_limit_order` will always revert

The `execute_limit_order` function clears the limit order in storage before calling `_remove_limit_order`, which then reads the now-empty order and uses its zero address to fetch an empty list of order IDs, causing a revert. As a result, all calls to `execute_limit_order` always revert, preventing any limit orders from being executed.

Dug Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-06-unstoppable-judging/issues/111

Found by

Dug

Summary

All calls to the MarginDex's execute_limit_order will always revert due the limit order being cleared in storage ahead of a call to remove_limit_order.

Vulnerability Detail

The execute_limit_order function includes the following logic...

VYPER
    self.limit_orders[_uid] = empty(LimitOrder)
    
    trade: Trade = self._open_trade(
        limit_order.account,
        limit_order.position_token,
        limit_order.min_position_amount_out,
        limit_order.debt_token,
        limit_order.debt_amount,
        limit_order.margin_amount,
        limit_order.tp_orders,
        limit_order.sl_orders
    )

    self._remove_limit_order(_uid)

The limit order is being set to empty(LimitOrder) in storage. Then a call to self._remove_limit_order is called for the same _uid.

_remove_limit_order has the following logic...

VYPER
    order: LimitOrder = self.limit_orders[_uid]
    self.limit_orders[_uid] = empty(LimitOrder)

    uids: DynArray[bytes32, 1024] = self.limit_order_uids[order.account]
    for i in range(1024):
        if uids[i] == _uid:
            uids[i] = uids[len(uids) - 1]
            uids.pop()
            break
        if i == len(uids) - 1: // @audit - Reverts when uids is empty
            raise
    self.limit_order_uids[order.account] = uids

The newly-empty order is read from storage, and it's associated account is used fetch the limit_order_uids.

The issue is that this account is the zero address, which is the default value after the order was cleared in the preceding function. This will cause the uids array to be empty, and the loop will revert when i == len(uids) - 1.

Impact

This means that all calls to execute_limit_order will revert, and no limit orders can be executed, which is a major piece of functionality for the protocol.

Code Snippet

https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/MarginDex.vy#L568-L597

https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/MarginDex.vy#L619-L632

Tool used

Manual Review

Recommendation

execute_limit_order should not clear the limit order in storage before calling remove_limit_order. This will allow the limit_order_uids to be fetched correctly.

Discussion

Unstoppable-DeFi

https://github.com/Unstoppable-DeFi/unstoppable-dex-audit/pull/15

maarcweiss

Fixed by removing the code where the limit order was deleted: self.limit_orders[_uid] = empty(LimitOrder)

The original report as published on Sherlock, by Dug. 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 function clears the limit order in storage before calling `_remove_limit_order`, which then reads the cleared order and uses its zero address to access an empty list, causing a revert.

Impact

All calls to `execute_limit_order` will revert, so no limit orders can be executed, breaking a major protocol feature.

What the attacker needed

None

The fix

The fix is to not clear the limit order in storage before calling `_remove_limit_order`; the code that deletes the order was removed.

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 smart contract function that is supposed to execute limit orders always fails. The code deletes the order from storage before trying to remove it from a list, so when it looks up the list using the now-deleted order's account (which becomes zero), it finds nothing and crashes. This means users can never execute their limit orders.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by Dug

View all

7 more disclosed reports, best first.

More from Unstoppable

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.