Skip to content
Business logic Audit finding Intermediate

Removing only the tail order from a limit does not reduce tree size, allowing order book to grow indefinitely

The CLOB contract enforces a maximum number of price limits per side, but when the book is full it only removes the least competitive order from the worst limit, not the limit itself if it holds multiple orders. An attacker could repeatedly place orders at new prices to grow the order book without bound, bypassing the intended size limit.

montecristo Follow with 0x1998, 0xAura and 17 others

Originally disclosed on Code4rena

Submitted by montecristo, also found by 0x1998, 0xAura, 0xDetermination, 0xdice91, 0xlookman, ahahaHard1k, anonymousjoe, ayden, BenRai, dhank, dimulski, gesha17, Gosho, Merulez99, Neeloy, Ollam, Ragnarok, Soosh, and touristS

When the tree size limit is reached, the CLOB only removes the least competitive order (tail order) from a limit, but does not remove the limit itself if it contains multiple orders. As a result, the tree size does not decrease, allowing the order book to bypass the intended size restriction and grow without bound.

Description

When the tree size reaches the limit, CLOB will remove the most non-competitive order from the book:

File: contracts/clob/CLOB.sol

632:         // The book is full, pop the least competitive order (or revert if incoming is the least competitive)
633:         if (ds.askTree.size() == maxNumLimitsPerSide) {
634:             uint256 maxAskPrice = ds.getWorstAskPrice();
635:             if (newOrder.price >= maxAskPrice) revert MaxOrdersInBookPostNotCompetitive();
636:
637:@>           _removeNonCompetitiveOrder(ds, ds.orders[ds.askLimits[maxAskPrice].tailOrder]);
638:         }

The problem is that it only removes tailOrder from the limit. If a limit contains multiple orders, the limit will not be removed from the tree:

File: contracts/clob/types/Book.sol

307:@>       if (limit.numOrders == 1) {
308:             if (order.side == Side.BUY) {
309:@>               delete self.bidLimits[price];
310:                 self.bidTree.remove(price);
311:             } else {
312:@>               delete self.askLimits[price];
313:                 self.askTree.remove(price);
314:             }
315:             return;
316:         }

Since RedBlackTree.size() returns total number of nodes (i.e. limits), tree size will not be reduced even after removing the tail order.

Indeed, in order to keep the tree size limit, the code should remove all orders from the given limit, not just the tail.

Impact

If the worst limit has multiple orders:

  • Tree will not shrink after removing non-competitive order

So if the new order has a new limit (i.e. there are no existing orders with the given price):

  • Tree size will actually increase by 1 after adding a new limit order

Thus, for subsequent order postings:

  • Since tree size check is using == (CLOB.sol:633), subsequent order posting will bypass tree limit checking

As a result, the tree and the order book will grow indefinitely.

Recommended Mitigation Steps

Consider implementing _removeNonCompetitiveLimit instead of removing just the tail order.

View detailed Proof of Concept


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

Impact

The order book could grow without bound, bypassing the intended size restriction, which may lead to higher gas costs and degraded performance.

What the attacker needed

Ability to post orders; book already at max limits; worst limit has multiple orders

The fix

The code should remove the entire limit (all orders at that price) when the book is full, not just the tail order.

How it was found

  1. Fill the order book so that the tree size reaches the maximum number of limits per side.
  2. Ensure the worst limit contains more than one order.
  3. Post a new order at a new price that is more competitive than the worst limit.
  4. The contract removes only the tail order from the worst limit, leaving that limit in the tree.
  5. The tree size does not decrease, so the new limit increases the tree size by one.
  6. Repeat to keep adding new limits and bypass the size check.

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.

An order book is a list of buy and sell orders at different prices. The contract tries to keep the number of price levels (limits) under a maximum, but when it is full it only deletes one order from the worst price level instead of deleting the whole level. If that level still has other orders, the count of levels does not go down, so new price levels can keep being added and the book grows forever.

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 montecristo

View all

10 more disclosed reports, best first.

More from GTE Spot CLOB and Router

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.