Skip to content
Business logic Audit finding Intermediate

Loss of funds for traders due to accounting error in royalty calculations

The buy and sell functions in PrivatePool collected royalty fees from traders even when the royalty recipient address was zero, but did not transfer those fees to anyone. This caused traders to lose the royalty amount, which remained stuck in the pool contract.

AkshaySrivastav Follow with chaduke, bshramin and 13 others

Originally disclosed on Code4rena

Submitted by AkshaySrivastav, also found by chaduke, bshramin, adriro, saian, 0xRobocop, adriro, Koolex, tallo, rbserver, rvierdiiev, rvierdiiev, cryptonue, bin2chen, chaduke, and sashik_eth

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

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

Impact

The PrivatePool.buy and PrivatePool.sell functions intend to distribute royalty amount whenever NFTs are traded. The implementation of buy and sell looks like this:

    function buy(uint256[] calldata tokenIds, uint256[] calldata tokenWeights, MerkleMultiProof calldata proof)
        public
        payable
        returns (uint256 netInputAmount, uint256 feeAmount, uint256 protocolFeeAmount)
    {
        // ...

        // calculate the sale price (assume it's the same for each NFT even if weights differ)
        uint256 salePrice = (netInputAmount - feeAmount - protocolFeeAmount) / tokenIds.length;
        uint256 royaltyFeeAmount = 0;
        for (uint256 i = 0; i < tokenIds.length; i++) {
            // transfer the NFT to the caller
            ERC721(nft).safeTransferFrom(address(this), msg.sender, tokenIds[i]);

            if (payRoyalties) {
                // get the royalty fee for the NFT
                (uint256 royaltyFee,) = _getRoyalty(tokenIds[i], salePrice);

                // add the royalty fee to the total royalty fee amount
                royaltyFeeAmount += royaltyFee;
            }
        }

        // add the royalty fee amount to the net input aount
        netInputAmount += royaltyFeeAmount;

        // ...

        if (payRoyalties) {
            for (uint256 i = 0; i < tokenIds.length; i++) {
                // get the royalty fee for the NFT
                (uint256 royaltyFee, address recipient) = _getRoyalty(tokenIds[i], salePrice);

                // transfer the royalty fee to the recipient if it's greater than 0
                if (royaltyFee > 0 && recipient != address(0)) {
                    if (baseToken != address(0)) {
                        ERC20(baseToken).safeTransfer(recipient, royaltyFee);
                    } else {
                        recipient.safeTransferETH(royaltyFee);
                    }
                }
            }
        }

        // emit the buy event
        emit Buy(tokenIds, tokenWeights, netInputAmount, feeAmount, protocolFeeAmount, royaltyFeeAmount);
    }

    function sell(
        ...
    ) public returns (...) {
        // ...

        uint256 royaltyFeeAmount = 0;
        for (uint256 i = 0; i < tokenIds.length; i++) {
            // transfer each nft from the caller
            ERC721(nft).safeTransferFrom(msg.sender, address(this), tokenIds[i]);

            if (payRoyalties) {
                // calculate the sale price (assume it's the same for each NFT even if weights differ)
                uint256 salePrice = (netOutputAmount + feeAmount + protocolFeeAmount) / tokenIds.length;

                // get the royalty fee for the NFT
                (uint256 royaltyFee, address recipient) = _getRoyalty(tokenIds[i], salePrice);

                // tally the royalty fee amount
                royaltyFeeAmount += royaltyFee;

                // transfer the royalty fee to the recipient if it's greater than 0
                if (royaltyFee > 0 && recipient != address(0)) {
                    if (baseToken != address(0)) {
                        ERC20(baseToken).safeTransfer(recipient, royaltyFee);
                    } else {
                        recipient.safeTransferETH(royaltyFee);
                    }
                }
            }
        }

        // subtract the royalty fee amount from the net output amount
        netOutputAmount -= royaltyFeeAmount;

        if (baseToken == address(0)) {
            // transfer ETH to the caller
            msg.sender.safeTransferETH(netOutputAmount);

            // if the protocol fee is set then pay the protocol fee
            if (protocolFeeAmount > 0) factory.safeTransferETH(protocolFeeAmount);
        } else {
            // transfer base tokens to the caller
            ERC20(baseToken).transfer(msg.sender, netOutputAmount);

            // if the protocol fee is set then pay the protocol fee
            if (protocolFeeAmount > 0) ERC20(baseToken).safeTransfer(address(factory), protocolFeeAmount);
        }

        // ...
    }

It should be noted that while calculating royaltyFeeAmount the the recipient address returned from _getRoyalty function is ignored and the returned royaltyFee is added to the royaltyFeeAmount. This cumulative royalty amount is then collected from the trader.

However while performing the actual royalty transfer to the royalty recipient the returned recipient address is validated to not be equal to 0. The royalty is only paid when the recipient address is non-zero.

This inconsistency between royalty collection and royalty distribution can cause loss of funds to the traders. In the cases when royaltyFee is non-zero but recipient address is zero, the fee will be collected from traders but won’t be distributed to royalty recipient. Hence causing loss of funds to the traders.

As the creation of private pools is open to everyone, the likelihood of this vulnerability is high.

Proof of Concept

Consider this scenario:

  • A buyer initiates the buy call for an NFT.
  • The PrivatePool.buy function queries the _getRoyalty function which returns 10 WETH as the royaltyFee and 0x00 address as the royalty recipient.
  • This 10 WETH value will be added to the royaltyFeeAmount amount and will be collected from the buyer.
  • But since the recipient address is 0x00, the 10 WETH royalty amount will not be distributed.
  • The 10 WETH amount won’t be returned to the buyer either. It just simply stays inside the pool contract.
  • The buyer here suffered loss of 10 WETH.

A similar scenario is possible for the NFT sell flow.

Recommended Mitigation Steps

Consider collecting royalty amount from traders only when the royalty recipient is non-zero.

    if (royaltyFee > 0 && recipient != address(0)) {
        royaltyFeeAmount += royaltyFee;
    }

outdoteth (Caviar) confirmed and commented:

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

Proposed fix is to only increment the total royalty fee amount in sell() and buy() when the recipient address is not zero.

In sell():

// transfer the royalty fee to the recipient if it's greater than 0
if (royaltyFee > 0 && recipient != address(0)) {
    // tally the royalty fee amount
    royaltyFeeAmount += royaltyFee;

    if (baseToken != address(0)) {
        ERC20(baseToken).safeTransfer(recipient, royaltyFee);
    } else {
        recipient.safeTransferETH(royaltyFee);
    }
}

In buy():

if (royaltyFee > 0 && recipient != address(0)) {
    // add the royalty fee to the total royalty fee amount
    royaltyFeeAmount += royaltyFee;
}

Alex the Entreprenerd (judge) decreased severity to Medium and commented:

The Warden has shown how, due to a logic discrepancy, a non-zero royalty would be removed from total paid, but wouldn’t be transferred.

The behaviour is inconsistent, so the finding is valid.

Technically the tokens will be left in the pool, meaning the owner will be able to retrieve them.

Factually this would end up being an additional cost to the buyer, more so than a loss of funds.

Because the finding shows an inconsistent behavior, that doesn’t cause a loss of funds beside the royalty fee, I believe Medium Severity to be the most appropriate.

Alex the Entreprenerd (judge) commented:

At this time, with the information that I have available, the finding highlights the fact that the royalties may be paid, but not transferred to the recipient if the recipient is the address(0).

While the OZ implementation addresses this, I don’t believe older implementations would.

I also have to concede that having non-zero royalties sent to address(0) should not be common.

However, I maintain that the issue with the finding is not the address(0) per-se which would have been rated as Low, but the fact that in that case the behaviour is inconsistent with other cases, and that will cause a cost to the payer although the royalties will not be forwarded.

By contrast, if the royalties were sent to address(0) I would be arguing around the idea that the royalty recipient may wish to burn such tokens and that would have been within their rights to do so.

For the reasons above, am maintaining Medium Severity.

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


The original report as published on Code4rena, by AkshaySrivastav. 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 code increments the total royalty amount without checking that the royalty recipient is non-zero, while the actual transfer is skipped when the recipient is zero.

Impact

Traders could lose the royalty fee amount, which remains in the pool contract and may be retrievable by the pool owner, as the judge noted.

What the attacker needed

A private pool with royalties enabled; a royalty recipient set to the zero address; a trader buys or sells an NFT

The fix

The fix was to only add the royalty fee to the total collected amount when the recipient address is non-zero, as confirmed by the project.

How it was found

  1. A buyer calls the buy function for an NFT.
  2. The _getRoyalty function returns a non-zero royalty fee and the zero address as recipient.
  3. The royalty fee is added to the total royalty amount and collected from the buyer.
  4. Because the recipient is zero, the royalty is not transferred.
  5. The royalty amount remains in the pool contract, and the buyer loses that amount.

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 you trade an NFT through this pool, a royalty fee is added to the price you pay. The code adds this fee to the total you owe even if the royalty recipient is the zero address (a special address that cannot receive funds). So you pay extra, but the fee is never sent to anyone and stays in the pool, meaning you lose that money.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by AkshaySrivastav

View all

16 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.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.