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
clob/CLOB.sol#L600-L605clob/CLOB.sol#L633-L638
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:
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
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
- Fill the order book so that the tree size reaches the maximum number of limits per side.
- Ensure the worst limit contains more than one order.
- Post a new order at a new price that is more competitive than the worst limit.
- The contract removes only the tail order from the worst limit, leaving that limit in the tree.
- The tree size does not decrease, so the new limit increases the tree size by one.
- 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.