Adverary can DOS contract by making a large number of deposits/withdraws then removing them all
A user can create many empty deposit or withdrawal entries by repeatedly depositing and then dequeuing, which remain in the queue. Processing the queue must skip each empty entry, consuming gas, so a long enough queue can make auction processing exceed the block gas limit and become impossible.
indijanc Follow with rotcivegaf, hyh and 8 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2022-11-opyn-judging/issues/148
Found by
indijanc, rotcivegaf, hyh, chainNue, rvierdiiev, Met, 0x52, KingNFT, Jeiwan, adriro, yixxas
Summary
When a user dequeues a withdraw or deposit it leaves a blank entry in the withdraw/deposit. This entry must be read from memory and skipped when processing the withdraws/deposits which uses gas for each blank entry. An adversary could exploit this to DOS the contract. By making a large number of these blank deposits they could make it impossible to process any auction.
Vulnerability Detail
while (_quantity > 0) {
Receipt memory deposit = deposits[i];
if (deposit.amount == 0) {
i++;
continue;
}
if (deposit.amount <= _quantity) {
// deposit amount is lesser than quantity use it fully
_quantity = _quantity - deposit.amount;
usdBalance[deposit.sender] -= deposit.amount;
amountToSend = (deposit.amount * 1e18) / _price;
IERC20(crab).transfer(deposit.sender, amountToSend);
emit USDCDeposited(deposit.sender, deposit.amount, amountToSend, i, 0);
delete deposits[i];
i++;
} else {
// deposit amount is greater than quantity; use it partially
deposits[i].amount = deposit.amount - _quantity;
usdBalance[deposit.sender] -= _quantity;
amountToSend = (_quantity * 1e18) / _price;
IERC20(crab).transfer(deposit.sender, amountToSend);
emit USDCDeposited(deposit.sender, _quantity, amountToSend, i, 0);
_quantity = 0;
}
}
The code above processes deposits in the order they are submitted. An adversary can exploit this by withdrawing/depositing a large number of times then dequeuing them to create a larger number of blank deposits. Since these are all zero, it creates a fill or kill scenario. Either all of them are skipped or none. If the adversary makes the list long enough then it will be impossible to fill without going over block gas limit.
Impact
Contract can be permanently DOS'd
Code Snippet
https://github.com/sherlock-audit/2022-11-opyn/blob/main/crab-netting/src/CrabNetting.sol#L362-L386
Tool used
Manual Review
Recommendation
Two potential solutions. The first would be to limit the number of deposits/withdraws that can be processed in a single netting. The second would be to allow the owner to manually skip withdraws/deposits by calling an function that increments depositsIndex and withdrawsIndex.
Discussion
sanandnarayan
fix: https://github.com/opynfinance/squeeth-monorepo/pull/805
thec00n
Allows the owner to set the withdrawsIndex and depositIndex and so to skip the queue index forward if there is some issue with the queue elements. At least this should allow the auction functions to continue to work.
jacksanford1
FIx accepted
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 queue processing loop iterates over all entries, including empty ones left by dequeuing, without a bound on the number of entries processed, so an attacker can inflate the queue length to exceed the block gas limit.
Impact
The contract can be permanently DOS'd, making it impossible to process any auction.
What the attacker needed
Ability to make deposits and withdrawals; enough funds to create many entries
The fix
The fix allows the owner to set the withdrawsIndex and depositIndex to skip queue entries, and was accepted.
How it was found
- Make a large number of deposits and withdrawals.
- Dequeue them all, leaving blank entries in the queue.
- When the contract processes the queue, it must skip each blank entry, using gas.
- With enough blank entries, processing exceeds the block gas limit and cannot complete.
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 keeps a list of deposits and withdrawals. If someone adds many entries and then cancels them, the list still contains empty slots. When the contract later processes the list, it must look at every empty slot, which costs gas. If there are enough empty slots, the processing runs out of gas and cannot finish, so the contract is stuck.
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.