Skip to content
Audit finding Advanced

Malicious royalty recipient can steal excess eth from buy orders

A malicious royalty recipient could reenter the buy function in EthRouter.sol and steal excess ether that was refunded to the router during a user's buy order. The attacker could drain all ether held by the router before the original transaction completed.

Voyvoda Follow with sashik_eth, Evo and 6 others

Originally disclosed on Code4rena

Submitted by Voyvoda, also found by sashik_eth, Evo, giovannidisiena, Kenshin, philogy, 0xRobocop, teawaterwire, and Ruhum

https://github.com/code-423n4/2023-04-caviar/blob/main/src/PrivatePool.sol#L268

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

Impact

Users that submit single or bulk Buy orders through EthRouter.sol can have their excess eth stolen by a malicious royalty recipient.

Proof of Concept

Introduction

The buy(...) function in PrivatePool.sol refunds excess ether back to EthRouter.sol and then pays a royalty amount to a royalty recipient. The order is the following:

// refund any excess ETH to the caller
if (msg.value > netInputAmount) msg.sender.safeTransferETH(msg.value - netInputAmount);
if (payRoyalties) {
    ...
else {
    recipient.safeTransferETH(royaltyFee);
}

This turns out to be dangerous since now buy(...) in EthRouter.sol can be reentered from the fallback function of a royalty recipient. In the fallback function the attacker would call buy in the EthRouter.sol with an empty Buy[] buys calldata, deadline=0 and payRoyalties = false which will skip the for loop in buy(...), since buys is empty, and would reach the following block of code:

// refund any surplus ETH to the caller
if (address(this).balance > 0) {
    msg.sender.safeTransferETH(address(this).balance);
}

Since now msg.sender is the royalty recipient he would receive all the ether that is currently residing in EthRouter.sol while the original buy(...) triggered by the user hasn’t yet finished.

Before supplying a PoC implementation in Foundry, there are a few caveats to be noted.

Firstly, this issue can be more easily reproduced by assuming that the malicious royalty recipient would come either from a single Buy order consisting of a single tokenId or multiple Buy orders where the tokenId with the malicious royalty recipient is the last tokenId in the array of the last Buy order.

In the case of the tokenId associated with the malicious royalty recipient being positioned NOT in last place in the tokenIds[] array in the last Buy order we would have to write a fallback function that after collecting all the ether in EthRouter.sol somehow extracts information of how much ether would be needed to successfully complete the rest of the buy(...) invocations (that will be called on the rest of the tokenIds[]) and sends that ether back to EthRouter.sol so that the whole transaction doesn’t revert due to EthRouter.sol being out of funds. In the presented PoC implementation it is assumed that tokenIds has a single token or the malicious royalty recipient is associated with the last tokenId in the last Buy if there are multiple Buy orders. In the case where tokenId is positioned not in last place a more sophisticated approach would be needed to steal the excess eth that involves inspecting the EthRouter.buy(...) while it resides in the transaction mempool and front-running a transaction that configures a fallback() function in the royalty recipient that would send the necessary amount of the stolen excess eth back to EthRouter.sol so that buy(...) doesn’t revert.

PoC implementation

See warden’s original submission for full details.

Note on severity

A severity rating of “High” was chosen due to the following:

  1. Although the current state of the NFT market mostly has adopted NFTs that have royalty payments directly to the creator, the authors of Caviar have acknowledged the ERC-2981 standard and it is assumed they are aware that royaltyInfo returns an arbitrary royalty recipient address.
  2. The PoC implementation in this report uses an already existing NFT project - Pixels1024 - deployed on the Polygon network that shows a use case where users are responsible for the creation of a given NFT from a collection and therefore the user-creator is assigned as a royalty recipient.
  3. It is possible that future projects adopting ERC-2981 could have novel and complex interactions between who creates and who receives royalties in a given collection, therefore, extra caution should be a priority when handling royaltyInfo requests and the current implementation is shown to have а notable vulnerability.

Tools Used

  1. Foundry
  2. ERC-2981 specification - https://eips.ethereum.org/EIPS/eip-2981
  3. 1024 Pixels NFT - repo; polygon;

Recommended Mitigation Steps

Rework buy in EthRouter.sol and PrivatePool.sol. Use reentrancy guard.

outdoteth (Caviar) acknowledged via duplicate issue #752

Alex the Entreprenerd (judge) decreased severity to Medium


The original report as published on Code4rena, by Voyvoda. 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

Likely the buy function in EthRouter.sol lacks a reentrancy guard and refunds surplus ether to msg.sender, allowing a malicious royalty recipient's fallback to reenter and drain the contract balance.

Impact

A malicious royalty recipient could steal excess ether from buy orders, but the attack is easiest when the malicious tokenId is last in the order; otherwise a more complex approach is needed.

What the attacker needed

Malicious royalty recipient; user submits buy order with excess ETH; malicious tokenId is last in order

The fix

Rework the buy functions in EthRouter.sol and PrivatePool.sol and add a reentrancy guard.

How it was found

  1. A user submits a buy order through EthRouter.sol with excess ether.
  2. PrivatePool.sol refunds excess ether to EthRouter.sol and then pays a royalty to the malicious recipient.
  3. The malicious royalty recipient's fallback function reenters EthRouter.sol's buy with empty buys, deadline=0, and payRoyalties=false.
  4. The reentrant call skips the loop and refunds the entire EthRouter.sol balance to the attacker.

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.

When someone buys an NFT, they might send more ether than needed, and the extra is refunded to a router contract. A malicious royalty recipient can call back into the router during that refund and take all the ether sitting there, including the extra from the original buyer.

More by Voyvoda

View all

2 more disclosed reports, best first.

More from Caviar Private Pools

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.