Dust orders can block order posting
Maker orders can be reduced below the minimum limit without a check, leaving dust positions in the order book. When a new order tries to match such a dust order, the quote amount can round down to zero, causing a ZeroCostTrade revert that blocks the incoming order.
montecristo Follow with 0xAsen, gesha17 and 4 others
Originally disclosed on Code4rena
Submitted by montecristo, also found by 0xAsen, gesha17, gizzy, holtzzx, newspacexyz, and solhhj
clob/CLOB.sol #L807-L849
When matching incoming orders, maker orders can be reduced below the minimum limit without checks, resulting in dust positions remaining in the order book. These dust orders can block new incoming orders, as matching them may revert with a ZeroCostTrade error if the quote amount rounds down to zero.
Description
When matching an incoming order, maker order’s amount can be set below minLimitOrderAmountInBase, as there is no min amount check:
833:@> bool orderRemoved = matchData.baseDelta == matchedBase;
834:
835: // Handle token accounting for maker.
836: if (takerOrder.side == Side.BUY) {
837: TransientMakerData.addQuoteToken(makerOrder.owner, matchData.quoteDelta);
838:
839: if (!orderRemoved) ds.metadata().baseTokenOpenInterest -= matchData.baseDelta;
840: } else {
841: TransientMakerData.addBaseToken(makerOrder.owner, matchData.baseDelta);
842:
843: if (!orderRemoved) ds.metadata().quoteTokenOpenInterest -= matchData.quoteDelta;
844: }
845:
846: if (orderRemoved) ds.removeOrderFromBook(makerOrder);
847:@> else makerOrder.amount -= matchData.baseDelta;
- In L833, order is removed only when matched amount is equal to maker order’s amount
- In L847, maker order’s amount is decreased by matched amount. The result amount can be dust, as there is no min amount check done afterwards.
As such, maker order’s amount can be a dust amount (up to lotSizeInBase config).
This means there can be a dust position in the order book.
What happens if this dust order is matched to another incoming order?
819: matchData.baseDelta = (matchedBase.min(takerOrder.amount) / lotSize) * lotSize;
820: matchData.quoteDelta = ds.getQuoteTokenAmount(matchedPrice, matchData.baseDelta);
- In L819,
matchData.baseDeltacan be as small aslotSize, sincemakeOrder.amountcan be down tolotSize - In L820,
matchData.quoteDeltacan be zero due to rounddown:
File: contracts/clob/types/Book.sol
471: function getQuoteTokenAmount(Book storage self, uint256 price, uint256 baseAmount)
472: internal
473: view
474: returns (uint256 quoteAmount)
475: {
476: return baseAmount * price / self.config().baseSize;
477: }
If matchData.quoteDelta is 0, the trade(or order posting) will revert with ZeroCostTrade error:
439: if (totalQuoteSent == 0 || totalBaseReceived == 0) revert ZeroCostTrade();
Impact
Dust positions can block incoming orders. An example is shown in the POC.
Recommended Mitigation Steps
Consider removing orders if the amount after matching is lower than minLimitOrderAmountInBase.
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
Likely the matching logic reduces a maker order's amount without checking whether the remainder is below the minimum limit, so dust orders stay in the book and can produce a zero quote amount on the next match.
Impact
Dust positions can block incoming orders, as matching them may revert with a ZeroCostTrade error.
What the attacker needed
A maker order partially matched; remainder below minimum limit; a new taker order matches dust
The fix
Orders should be removed if the amount after matching is lower than minLimitOrderAmountInBase.
How it was found
- A maker order is partially matched, leaving a remainder below the minimum limit.
- The remainder stays in the order book as a dust order.
- A new incoming order attempts to match the dust order.
- The base delta is as small as the lot size, and the quote delta rounds down to zero.
- The trade reverts with ZeroCostTrade, blocking the incoming order.
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 an order book, a 'maker' order waits to be matched and a 'taker' order comes in to trade against it. The code lets a maker order shrink to a tiny leftover amount (dust) without removing it. Later, when someone tries to trade against that tiny leftover, the calculated cost can round down to zero, and the system rejects the trade, so the new order cannot go through.
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.