Order double-linked list is broken because order.prevOrderId is not persisted
The order book's doubly linked list is broken because the previous-order pointer is written to a memory copy of the order rather than to storage. This corrupts the list on removal and can cause a denial of service when the book is full and the limit's tail order becomes invalid.
montecristo Follow with 0x1998, 0xAsen and 23 others
Originally disclosed on Code4rena
Submitted by montecristo, also found by 0x1998, 0xAsen, 0xdice91, 0xlookman, 0xPhantom, axelot, ayden, BenRai, boredpukar, DDEENNY, dhank, Drynooo, Egbe, gh0xt, JuggerNaut63, lodelux, Olugbenga, Ragnarok, Riceee, sedare64, surenyanoks, taticuvostru, touristS, volodya, and VulnViper
clob/types/Book.sol#L275-L289clob/types/Book.sol#L150-L154
order.prevOrderId is updated only in memory and not saved to storage, breaking the linked list. This causes order adding and removal issues, potentially leading to denial of service when the order book is full and the limit’s tailOrder becomes invalid.
Description
Orders are stored as a double-linked list in the book.
However, this linking is broken as order.prevOrderId is not actually stored in EVM storage:
File: contracts/clob/types/Book.sol
150: function addOrderToBook(Book storage self, Order memory order) internal {
151: Limit storage limit = _updateBookPostOrder(self, order);
152:
153:@> _updateLimitPostOrder(self, limit, order);
154: }
As we can see in the above code, linked list is updated in the end. And order is passed as memory type:
File: contracts/clob/types/Book.sol
275: function _updateLimitPostOrder(Book storage self, Limit storage limit, Order memory order) private {
276: limit.numOrders++;
277:
278: if (limit.headOrder.isNull()) {
279: limit.headOrder = order.id;
280: limit.tailOrder = order.id;
281: } else {
282: Order storage tailOrder = self.orders[limit.tailOrder];
283: tailOrder.nextOrderId = order.id;
284:@> order.prevOrderId = tailOrder.id;
285: limit.tailOrder = order.id;
286: }
287:
288: emit LimitOrderCreated(BookEventNonce.inc(), order.id, order.price, order.amount, order.side);
289: }
In L284, order.prevOrderId is updated. However, it is not stored in EVM storage because order is passed as memory in L275.
This completely breaks linked list in future operations. For example, when removing orders:
File: contracts/clob/types/Book.sol
320: OrderId prev = order.prevOrderId;
321: OrderId next = order.nextOrderId;
322:
323: if (!prev.isNull()) self.orders[prev].nextOrderId = next;
324: else limit.headOrder = next;
325:
326: if (!next.isNull()) self.orders[next].prevOrderId = prev;
327: else limit.tailOrder = prev;
prevwill be null in L320 becauseprevOrderIdwas not persisted- Thus, L323 will not be reached and L324 will always be reached. This means
prevandnextlinkage will be broken - In L327,
limit.tailOrderwill always be set to null if removed entry was the tail
Impact
Order adding/removal will be affected.
Especially, when order book is full, CLOB can face DoS because limit tailOrder is set to empty order due to broken linked list.
This DoS is demonstrated by a POC.
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
Root cause
The order is passed as a memory parameter, so assigning to its prevOrderId field updates only the temporary copy and never the stored order.
Impact
Order adding and removal are affected, and when the order book is full the CLOB could face a denial of service because the limit's tail order is set to an empty order.
What the attacker needed
Order book reaches its limit
The fix
The previous-order pointer should be written to the stored order rather than to the in-memory copy, so the linked list is persisted correctly.
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 keeps buy and sell orders in a line, where each order points to the one before and after it. Here the code updated the 'previous' pointer on a temporary copy of the order, so the saved version never learned who came before it. Later, removing an order used that missing pointer and damaged the line, which could stop the book working when it is full.
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.