Skip to content
DoS Audit finding Intermediate

EthRouter.sell , EthRouter.deposit , and EthRouter.change functions can be DOS’ed for some ERC721 tokens

The EthRouter contract calls setApprovalForAll on ERC721 tokens every time sell, deposit, or change is used. For tokens like Axie that revert when setting the same approval twice, this causes the router functions to revert on subsequent calls, blocking users from selling or depositing those NFTs to the same pool.

rbserver Follow

Originally disclosed on Code4rena

Submitted by rbserver

https://github.com/code-423n4/2023-04-caviar/blob/main/src/EthRouter.sol#L152-L209

https://github.com/code-423n4/2023-04-caviar/blob/main/src/EthRouter.sol#L219-L248

https://github.com/code-423n4/2023-04-caviar/blob/main/src/EthRouter.sol#L254-L293

https://etherscan.io/address/0xf5b0a3efb8e8e4c201e2a935f110eaaf3ffecb8d#code#L672

Impact

The following EthRouter.sell, EthRouter.deposit, and EthRouter.change functions call the corresponding ERC721 tokens’ setApprovalForAll functions.

https://github.com/code-423n4/2023-04-caviar/blob/main/src/EthRouter.sol#L152-L209

    function sell(Sell[] calldata sells, uint256 minOutputAmount, uint256 deadline, bool payRoyalties) public {
        ...
        // loop through and execute the sells
        for (uint256 i = 0; i < sells.length; i++) {
            ...
            // approve the pair to transfer NFTs from the router
            ERC721(sells[i].nft).setApprovalForAll(sells[i].pool, true);
            ...
        }
        ...
    }

https://github.com/code-423n4/2023-04-caviar/blob/main/src/EthRouter.sol#L219-L248

    function deposit(
        address payable privatePool,
        address nft,
        uint256[] calldata tokenIds,
        uint256 minPrice,
        uint256 maxPrice,
        uint256 deadline
    ) public payable {
        ...
        // approve pair to transfer NFTs from router
        ERC721(nft).setApprovalForAll(privatePool, true);
        ...
    }

https://github.com/code-423n4/2023-04-caviar/blob/main/src/EthRouter.sol#L254-L293

    function change(Change[] calldata changes, uint256 deadline) public payable {
        ...
        // loop through and execute the changes
        for (uint256 i = 0; i < changes.length; i++) {
            Change memory _change = changes[i];
            ...
            // approve pair to transfer NFTs from router
            ERC721(_change.nft).setApprovalForAll(_change.pool, true);
            ...
        }
        ...
    }

For ERC721 tokens like Axie, which its setApprovalForAll function is shown below, calling their setApprovalForAll functions with the same msg.sender-_operator-_approved combination would revert because of requirements like require(_tokenOperator[msg.sender][_operator] != _approved).

For these ERC721 tokens, calling the EthRouter.sell, EthRouter.deposit, and EthRouter.change functions for the first time, which call such tokens’ setApprovalForAll functions for the first time, can succeed; however, calling the EthRouter.sell, EthRouter.deposit, and EthRouter.change functions again, which call such tokens’ setApprovalForAll functions with the same pool as _operator and true as _approved again, will revert. In this case, the EthRouter.sell, EthRouter.deposit, and EthRouter.change functions are DOS’ed for such ERC721 tokens.

https://etherscan.io/address/0xf5b0a3efb8e8e4c201e2a935f110eaaf3ffecb8d#code#L672

  function setApprovalForAll(address _operator, bool _approved) external whenNotPaused {
    require(_tokenOperator[msg.sender][_operator] != _approved);
    _tokenOperator[msg.sender][_operator] = _approved;
    ApprovalForAll(msg.sender, _operator, _approved);
  }

Proof of Concept

The following steps can occur for the described scenario.

  1. Alice calls the EthRouter.sell function to sell 1 Axie NFT to a private pool, which succeeds.
  2. Alice calls the EthRouter.sell function again to sell another Axie NFT to the same private pool. However, this function call’s execution of ERC721(sells[i].nft).setApprovalForAll(sells[i].pool, true) reverts because Axie’s require(_tokenOperator[msg.sender][_operator] != _approved) reverts.
  3. Bob tries to repeat Step 2 but his EthRouter.sell function call also reverts.
  4. Hence, the EthRouter.sell function is DOS’ed for selling any Axie NFTs to the same private pool for any users.

Tools Used

VS Code

Recommended Mitigation Steps

The EthRouter.sell, EthRouter.deposit, and EthRouter.change functions can be respectively updated to check if the EthRouter contract has approved the corresponding pool to spend any of the corresponding ERC721 tokens received by itself. If not, the corresponding ERC721’s setApprovalForAll function can be called; otherwise, the corresponding ERC721’s setApprovalForAll function should not be called.

outdoteth (Caviar) confirmed and mitigated:

Fixed in: https://github.com/outdoteth/caviar-private-pools/pull/7

Proposed fix is to skip the approval step if the pool has already been approved to transfer the NFTs from the EthRouter.

function _approveNfts(address nft, address target) internal {
    // check if the router is already approved to transfer NFTs from the caller
    if (ERC721(nft).isApprovedForAll(address(this), target)) return;

    // approve the target to transfer NFTs from the router
    ERC721(nft).setApprovalForAll(target, true);
}

Alex the Entreprenerd (judge) commented:

The Warden has shown how, the always re-approve pattern can cause reverts, this is contingent on the specific NFT used, however, AXIE is in my opinion a sufficiently relevant token for this finding to be valid.

Due to it’s reliance on token implementation I agree with Medium Severity.

Status: Mitigation confirmed. Full details in reports from rbserver, KrisApostolov, and rvierdiiev.


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

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 router unconditionally calls setApprovalForAll on the NFT contract for each operation, without checking if the approval already exists, which reverts for tokens that disallow redundant approvals.

Impact

For affected ERC721 tokens like Axie, the sell, deposit, and change functions become unusable for any user trying to interact with the same pool after the first approval, effectively denying service.

What the attacker needed

Use of an ERC721 token that reverts on redundant approvals…; The pool must have been previously approved by the router…

The fix

The router should check if the pool is already approved for the NFT and skip the setApprovalForAll call if so; this was fixed by the Caviar team.

How it was found

  1. Alice calls EthRouter.sell to sell one Axie NFT to a private pool, which succeeds.
  2. Alice calls EthRouter.sell again to sell another Axie NFT to the same private pool, but the call reverts because setApprovalForAll reverts.
  3. Bob tries the same and his call also reverts.
  4. As a result, the sell function is blocked for any Axie NFT sales to that pool.

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.

Some NFT contracts, like Axie, have a rule that you cannot approve the same operator twice in a row. The router always tries to approve the pool every time, so after the first successful sale, later attempts fail because the approval is already set. This means users cannot sell more of those NFTs to the same pool.

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 rbserver

View all

51 more disclosed reports, best first.

More from Caviar Private Pools

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.