[NAZ-M6] Unbounded loop in `_previewWithdraw() && _redeemMax()` Can Lead To DoS
Two functions in the auction and queue contracts loop over every order or token ID held by a user, performing checks, maths and external calls inside the loop. If enough orders accumulate, the transaction can run out of gas and revert, so the affected withdrawal and redemption calls could fail.
ctf_sec Follow with __141345__ and 0xNazgul
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2022-09-knox-judging/issues/102
Found by
ctf_sec, __141345__, 0xNazgul
Summary
There are some unbounded loops that can lead to DoS.
Vulnerability Detail
The loop inside of _previewWithdraw() goes through all orders in orderbook and checks if the data.buyer is the buyer passed in parameter. It does some checks, math and an external call to remove the data.id from the orderbook. With all of this happening in the loop and costing gas it may revert due to exceeding the block size gas limit. This is the same case for _redeemMax() but has more gas costly executions with transfer() being involved.
Impact
There are over thousands of orders that the loop has to go through, along with the massive amount of orders that are the buyers. Half way through the execution fails due to exceeding the block size gas limit.
Code Snippet
AuctionInternal.sol#L298, QueueInternal.sol#L140
Tool used
Manual Review
Recommendation
Consider avoiding all the actions executed in a single transaction, especially when calls are executed as part of a loop.
Discussion
0xCourtney
It might make sense to treat redeemMax as a separate issue since it's in a different contract and therefore relies on a different mechanism than the other functions mentioned. redeemMax implements a for-loop that iterates through all of the tokenId's held by an account. These tokenId's accumulate whenever a user deposits collateral into the queue during different epochs. The redeemMax function is called every time a user deposits, into the Queue or withdraws from the Vault. This makes it impossible to accumulate more than one new tokenId at a time.
This applies specifically to issues #15, and #102.
Evert0x
@0xCourtney would you consider 15 / 102 a different severity than the others? Based on your comment I assume this issue related to redeemMax is invalid but the others (24, 25, 82, 85) are valid. Is that correct?
0xCourtney
#25 is actually incorrect. We do check that size > minSize for the auction orders for _validateLimitOrder and _validateMarketOrder
#102 should be two separate issues, one for previewWithdraw another for redeemMax. The issue mentioned about redeemMax should be low or informational for the reasons stated above.
#15 likewise should be low or informational as its related to redeemMax
#24, #82, and #85 are acknowledged.
Evert0x
For #25 minSize is set in the AuctionProxy constructor but no > 0 check is done, but we consider that a low issue.
Evert0x
Just for confirmation I kept #102 the main issue, although it's referencing two separate issues. #24, #82 and #85 are duplicates of this. Rest of the issues are low/invalid
jacksanford1
Bringing in 0xCourtney comment from Discord:
All of the issues that are being reviewed have been linked in the judging repo issues. The only exception is issue#102. We acknowledged its a theoretical attack vector but will not fix it in this review.
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 functions iterate over an unbounded list of orders or token IDs while doing costly work and external calls inside the loop, so gas use grows with the list size.
Impact
With thousands of orders the loop can exceed the block gas limit and revert, blocking the withdrawal or redemption; the project acknowledged this as a theoretical attack vector but did not fix it in that review.
What the attacker needed
Many orders or token IDs for one user
The fix
The recommendation is to avoid doing all the work in a single transaction, especially calls inside a loop; the project said it would not fix it in this review.
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 can only do so much work before it runs out of gas, the fee budget for a transaction. Here a function walks through every order a user has, and if there are thousands, the transaction runs out of gas and fails. That means the user cannot complete the withdrawal or redemption.
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.