Skip to content
DoS Audit finding Intermediate

Netting and withdraw auction can be frozen permanently

A withdrawal queue processed by the netting and withdraw auction functions required every USDC transfer to succeed, so a user whose address is blocked by USDC could queue a withdrawal and make those functions revert permanently. An attacker could therefore freeze netting and auctions at a moment of their choosing, causing losses to beneficiaries.

joestakey Follow with bin2chen, hyh and 5 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2022-11-opyn-judging/issues/219

Found by

joestakey, bin2chen, hyh, libratus, KingNFT, Zarf, yixxas, cccz

Summary

An attacker can permanently block the auctions by using a blocked address to fail USDC transfers, which are now required for the auction to proceed.

Vulnerability Detail

Say Bob knows that one of his addresses is blocked by USDC. He has/can obtain CRAB, which he can transfer to this address.

As withdraw queue requires each transfer call to be successful, this will permanently freezes the functionality, i.e. all future auctions will be blocked.

Knowing that, Bob will block the auctions when it's beneficial to him the most.

Impact

netAtPrice() and withdrawAuction() will be blocked as long as Bob's withdrawal is queued. There is no way for the owner to manually alter this state.

As auction timing can have material impact on the beneficiaries, the inability to perform netting and withdraw auction will lead to losses for them as Bob will choose the moment to execute the attack to benefit himself at the expense of the participants.

Setting the severity to be high as this is permanent freeze of the core functionality fully controllable by the attacker only.

Code Snippet

netAtPrice() will be reverting at Bob's withdrawal:

https://github.com/sherlock-audit/2022-11-opyn/blob/main/crab-netting/src/CrabNetting.sol#L389-L419

Solidity
        // process withdraws and send usdc
        i = withdrawsIndex;
        while (crabQuantity > 0) {
            Receipt memory withdraw = withdraws[i];
            if (withdraw.amount == 0) {
                i++;
                continue;
            }
            if (withdraw.amount <= crabQuantity) {
                crabQuantity = crabQuantity - withdraw.amount;
                crabBalance[withdraw.sender] -= withdraw.amount;
                amountToSend = (withdraw.amount * _price) / 1e18;
                IERC20(usdc).transfer(withdraw.sender, amountToSend);

                emit CrabWithdrawn(withdraw.sender, withdraw.amount, amountToSend, i);

                delete withdraws[i];
                i++;
            } else {
                withdraws[i].amount = withdraw.amount - crabQuantity;
                crabBalance[withdraw.sender] -= crabQuantity;
                amountToSend = (crabQuantity * _price) / 1e18;
                IERC20(usdc).transfer(withdraw.sender, amountToSend);

                emit CrabWithdrawn(withdraw.sender, withdraw.amount, amountToSend, i);

                crabQuantity = 0;
            }
        }
        withdrawsIndex = i;
    }

withdrawAuction() similarly will fail on Bob's entry:

https://github.com/sherlock-audit/2022-11-opyn/blob/main/crab-netting/src/CrabNetting.sol#L687-L720

Solidity
        // step 5 pay all withdrawers and mark their withdraws as done
        uint256 remainingWithdraws = _p.crabToWithdraw;
        uint256 j = withdrawsIndex;
        uint256 usdcAmount;
        while (remainingWithdraws > 0) {
            Receipt memory withdraw = withdraws[j];
            if (withdraw.amount == 0) {
                j++;
                continue;
            }
            if (withdraw.amount <= remainingWithdraws) {
                // full usage
                remainingWithdraws -= withdraw.amount;
                crabBalance[withdraw.sender] -= withdraw.amount;

                // send proportional usdc
                usdcAmount = (((withdraw.amount * 1e18) / _p.crabToWithdraw) * usdcReceived) / 1e18;
                IERC20(usdc).transfer(withdraw.sender, usdcAmount);
                emit CrabWithdrawn(withdraw.sender, withdraw.amount, usdcAmount, j);
                delete withdraws[j];
                j++;
            } else {
                withdraws[j].amount -= remainingWithdraws;
                crabBalance[withdraw.sender] -= remainingWithdraws;

                // send proportional usdc
                usdcAmount = (((remainingWithdraws * 1e18) / _p.crabToWithdraw) * usdcReceived) / 1e18;
                IERC20(usdc).transfer(withdraw.sender, usdcAmount);
                emit CrabWithdrawn(withdraw.sender, remainingWithdraws, usdcAmount, j);

                remainingWithdraws = 0;
            }
        }
        withdrawsIndex = j;

netAtPrice() and withdrawAuction() unavailability and the whole withdrawal queue freeze will be permanent as withdrawsIndex can be changed only in netAtPrice() and withdrawAuction(), i.e. there is no way to skip any entry, including Bob's.

I.e. only Bob can unstuck the system by removing the withdrawal:

https://github.com/sherlock-audit/2022-11-opyn/blob/main/crab-netting/src/CrabNetting.sol#L319-L346

Solidity
    /**
     * @notice withdraw Crab from queue
     * @param _amount Crab amount to dequeue
     */
    function dequeueCrab(uint256 _amount) external {
        require(!isAuctionLive, "auction is live");
        crabBalance[msg.sender] = crabBalance[msg.sender] - _amount;
        require(
            crabBalance[msg.sender] >= minCrabAmount || crabBalance[msg.sender] == 0,
            "remaining amount smaller than minimum, consider removing full balance"
        );
        // deQueue crab from the last, last in first out
        uint256 toRemove = _amount;
        uint256 lastIndexP1 = userWithdrawsIndex[msg.sender].length;
        for (uint256 i = lastIndexP1; i > 0; i--) {
            Receipt storage r = withdraws[userWithdrawsIndex[msg.sender][i - 1]];
            if (r.amount > toRemove) {
                r.amount -= toRemove;
                toRemove = 0;
                break;
            } else {
                toRemove -= r.amount;
                delete withdraws[userWithdrawsIndex[msg.sender][i - 1]];
            }
        }
        IERC20(crab).transfer(msg.sender, _amount);
        emit CrabDeQueued(msg.sender, _amount, crabBalance[msg.sender]);
    }

Tool used

Manual Review

Recommendation

Consider trying to transfer and skipping if there is any malfunction, for example:

https://github.com/sherlock-audit/2022-11-opyn/blob/main/crab-netting/src/CrabNetting.sol#L389-L419

Solidity
        // process withdraws and send usdc
        i = withdrawsIndex;
        while (crabQuantity > 0) {
            Receipt memory withdraw = withdraws[i];
            if (withdraw.amount == 0) {
                i++;
                continue;
            }
            if (withdraw.amount <= crabQuantity) {
                amountToSend = (withdraw.amount * _price) / 1e18;
-               IERC20(usdc).transfer(withdraw.sender, amountToSend);
+               try IERC20(usdc).transfer(withdraw.sender, amountToSend) {
                    crabQuantity = crabQuantity - withdraw.amount;
                    crabBalance[withdraw.sender] -= withdraw.amount;
                    emit CrabWithdrawn(withdraw.sender, withdraw.amount, amountToSend, i);

                    delete withdraws[i];                    
+               } catch {
+               }

                i++;
            } else {
                amountToSend = (crabQuantity * _price) / 1e18;
-               IERC20(usdc).transfer(withdraw.sender, amountToSend);
+               try IERC20(usdc).transfer(withdraw.sender, amountToSend) {
                    withdraws[i].amount = withdraw.amount - crabQuantity;
                    crabBalance[withdraw.sender] -= crabQuantity;
                    emit CrabWithdrawn(withdraw.sender, withdraw.amount, amountToSend, i);

                    crabQuantity = 0;                 
+               } catch {
+                   ++i;
+               }
            }
        }
        withdrawsIndex = i;
    }

This can be paired with introduction of the onlyOwner rescue function to handle the transfer manually, say for USDC ban case: auction operator transfers to self, swaps and return the funds to depositor in another form.

Notice that skipping the entry causes no harm for the withdrawer as dequeueCrab() can be run any time for it.

Discussion

sanandnarayan

https://github.com/opynfinance/squeeth-monorepo/pull/801

thec00n

The owner can repay Crabv2 to users and delete the withdraw in case the auction functions fail because of a blacklisted USDC address.

sanandnarayan

this is almost what the fix does. Instead of sending the crabV2 back, the blacklisted user can claim it / dequeue it themselves. And since we reject/delete the withdraw the auction can be resubmitted with a lesser amount / the next withdraw amounts could be used

thec00n

Ah yes indeed.

jacksanford1

Fix accepted

The original report as published on Sherlock, by joestakey. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 withdrawal loops call the USDC transfer without handling failure, so one reverting transfer blocks the whole queue and the index can only advance through those same functions.

Impact

Netting and withdraw auctions are permanently blocked while the entry is queued, and the attacker can time this to cause losses for beneficiaries; the text notes the owner cannot manually alter the state.

What the attacker needed

CRAB tokens; an address blocked by USDC; no special privileges

The fix

The fix wraps each USDC transfer in a try/catch so a failing entry is skipped, and the blocked user can later claim or dequeue their withdrawal themselves; the report says the fix was accepted.

How it was found

  1. Obtain CRAB tokens and send them to an address that USDC has blocked.
  2. Queue a withdrawal from that blocked address so it sits in the withdraws array.
  3. When netAtPrice() or withdrawAuction() runs, the USDC transfer to the blocked address reverts and the whole call fails.
  4. Repeat or leave the entry in place so the queue cannot advance, since only the owner of the entry can dequeue it.

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.

USDC is a stablecoin whose issuer can block specific addresses from sending or receiving it. This protocol pays queued withdrawals one by one, and if any single payment fails the whole function stops. By queueing a withdrawal from a blocked address, an attacker makes every later attempt to run the auction fail, and nobody else can remove that entry.

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.

More by joestakey

View all

10 more disclosed reports, best first.

More from Opyn Crab Netting

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.