Royalty receiver can drain a private pool
The royalty fee calculation in the buy function calls _getRoyalty twice, with an untrusted external call in between. A malicious royalty receiver could change the royalty fee between the two calls, causing the pool to pay the full sale price to the royalty receiver while the buyer pays nothing to the pool, allowing the pool to be drained.
Voyvoda Follow with AkshaySrivastav, teddav and 2 others
Originally disclosed on Code4rena
Submitted by Voyvoda, also found by AkshaySrivastav, teddav, aviggiano, and Haipls
https://github.com/code-423n4/2023-04-caviar/blob/main/src/PrivatePool.sol#L237-L252
https://github.com/code-423n4/2023-04-caviar/blob/main/src/PrivatePool.sol#L267-L268
https://github.com/code-423n4/2023-04-caviar/blob/main/src/PrivatePool.sol#L274
Impact
Royalty fee calculation has a serious flaw in buy(...). Caviar’s private pools could be completely drained.
In the Caviar private pool, NFT royalties are being paid from the msg.sender to the NFT royalty receiver of each token in PrivatePool.buy and PrivatePool.sell:
https://github.com/code-423n4/2023-04-caviar/blob/main/src/PrivatePool.sol#L271-L285
#buy(uint256[],uint256[],MerkleMultiProof)
271: if (payRoyalties) {
...
274: (uint256 royaltyFee, address recipient) = _getRoyalty(tokenIds[i], salePrice);
...
278: if (baseToken != address(0)) {
279: ERC20(baseToken).safeTransfer(recipient, royaltyFee);
280: } else {
281: recipient.safeTransferETH(royaltyFee);
282: }
https://github.com/code-423n4/2023-04-caviar/blob/main/src/PrivatePool.sol#L328-L352
#sell(uint256[],uint256[],MerkleMultiProof,IStolenNftOracle.Message[])
329: for (uint256 i = 0; i < tokenIds.length; i++) {
...
333: if (payRoyalties) {
...
338: (uint256 royaltyFee, address recipient) = _getRoyalty(tokenIds[i], salePrice);
...
345: if (baseToken != address(0)) {
346: ERC20(baseToken).safeTransfer(recipient, royaltyFee);
347: } else {
348: recipient.safeTransferETH(royaltyFee);
349: }
In both functions, the amount needed to pay all royalties is taken from the msg.sender who is either the buyer or the seller depending on the context. In PrivatePool.sell, this amount is first paid by the pool and then taken from the msg.sender by simply reducing what they receive in return for the NFTs they are selling. A similar thing is done in PrivatePool.buy, but instead of reducing the output amount, the input amount of base tokens that the msg.sender (buyer) should pay to the pool is increased:
https://github.com/code-423n4/2023-04-caviar/blob/main/src/PrivatePool.sol#L251-L252
#buy(uint256[],uint256[],MerkleMultiProof)
251: // add the royalty fee amount to the net input aount
252: netInputAmount += royaltyFeeAmount;
https://github.com/code-423n4/2023-04-caviar/blob/main/src/PrivatePool.sol#L354-L355
#sell(uint256[],uint256[],MerkleMultiProof,IStolenNftOracle.Message[])
354: // subtract the royalty fee amount from the net output amount
355: netOutputAmount -= royaltyFeeAmount;
The difference between these two functions (that lies at the core of the problem) is that in PrivatePool.buy, the _getRoyalty function is called twice. The first time is to calculate the total amount of royalties to be paid, and the second time is to actually send each royalty fee to each recipient:
https://github.com/code-423n4/2023-04-caviar/blob/main/src/PrivatePool.sol#L242-L248
https://github.com/code-423n4/2023-04-caviar/blob/main/src/PrivatePool.sol#L273-L274
#buy(uint256[],uint256[],MerkleMultiProof)
242: if (payRoyalties) {
243: // get the royalty fee for the NFT
244: (uint256 royaltyFee,) = _getRoyalty(tokenIds[i], salePrice); // @audit _getRoyalty called 1st time
245:
246: // add the royalty fee to the total royalty fee amount
247: royaltyFeeAmount += royaltyFee;
248: }
...
273: // get the royalty fee for the NFT
274: (uint256 royaltyFee, address recipient) = _getRoyalty(tokenIds[i], salePrice); // @audit _getRoyalty called 2nd time
This is problematic because an attacker could potentially change the royalty fee between the two calls, due to the following untrusted external call:
https://github.com/code-423n4/2023-04-caviar/blob/main/src/PrivatePool.sol#L267-L268
#buy(uint256[],uint256[],MerkleMultiProof)
267: // refund any excess ETH to the caller
268: if (msg.value > netInputAmount) msg.sender.safeTransferETH(msg.value - netInputAmount); // @audit untrusted external call
If the msg.sender is a malicious contract that has control over the royaltyFee for the NFTs that are being bought, they can change it, for example, from 0 basis points (0%) to 10000 basis points (100%) in their receive() function.
// @audit An attacker can call this setter function between the two `_getRoyalty()` calls.
94: function _setTokenRoyalty(uint256 tokenId, address receiver, uint96 feeNumerator) internal virtual {
95: require(feeNumerator <= _feeDenominator(), "ERC2981: royalty fee will exceed salePrice");
96: require(receiver != address(0), "ERC2981: Invalid parameters");
97:
98: _tokenRoyaltyInfo[tokenId] = RoyaltyInfo(receiver, feeNumerator);
99: }
That way, the amount transferred by the msg.sender for royalties will be 0 because the total royaltyFeeAmount is calculated based on the first value (0%) but the actual sent amount to the receiver is determined by the second value (100%). This will result in the whole price paid for the NFT being returned to the royalty receiver, but being paid by the Pool instead of the msg.sender.
The msg.sender has therefore received the NFT but paid the whole price for it to the royalty receiver and 0 to the Pool. If the msg.sender is the royalty receiver, they will basically have spent 0 base tokens (not counting gas expenses) but received the NFT in their account. They can then sell it to the same private pool to exchange it for base tokens.
This is an extreme scenario, however, the developers have acknowledged ERC-2981 and that royaltyInfo(...) returns an arbitrary address. In the future we could see projects that have royalty payments that fluctuate such as increasing/decaying royalties over time article on eip 2981 or projects that delegate the creation of nfts to the users such as 1024pixels polygon, git repo and royalties are paid to each user rather to a single creator. In such cases invocation of _getRoyalty(...) twice with external calls that transfer assets in-between is a vulnerable pattern that is sure to introduce asset risks and calculation inaccuracies both for the users and protocol itself. Immediate remedy would be to simplify buy(...) in PrivatePool.sol to use only one for loop and call _getRoyalty(...) once.
PoC shows how the entire Pool’s base tokens can be drained by a single royalty receiver using a single NFT assuming that the royalty receiver has control over the royaltyFee.
Proof of Concept
See warden’s original submission for full Proof of Concept.
Tools Used
Foundry
Recommended Mitigation Steps
Ensure that the amount sent to the NFT royalty receivers in the second for loop in buy() is the same as the amount calculated in the first for loop.
Alex the Entreprenerd (judge) commented:
The Warden has shown how, because of reEntrancy and due to the same call being performed for royalties, a malicious royalty recipient can drain the pool of all funds.
I have considered downgrading the finding because of the conditionality of the royalty recipient being malicious, however, I don’t believe this can be considered an external condition, as any account able to change the royalty setting could willingly or unwillingly enable the attack.
For this reason I believe that the finding is of High Severity.
outdoteth (Caviar) confirmed via duplicate issue #593 and mitigated:
Fixed in: https://github.com/outdoteth/caviar-private-pools/pull/12.
Status: Mitigation confirmed. Full details in reports from rbserver, KrisApostolov, and rvierdiiev.
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 calls _getRoyalty twice with an external call in between, allowing the royalty fee to be changed between the calculation and the payment.
Impact
A malicious royalty receiver could drain the entire pool's base tokens by buying an NFT and then selling it back, assuming they control the royalty fee.
What the attacker needed
Attacker controls royalty fee for an NFT; attacker has an NFT in the private pool; attacker can deploy a malicious contract
The fix
The buy function should be simplified to use only one loop and call _getRoyalty once, ensuring the amount sent to royalty receivers matches the calculated amount.
How it was found
- Attacker deploys a malicious contract that controls the royalty fee for an NFT.
- Attacker calls buy on the private pool with that NFT, sending enough ETH to cover the sale price.
- During the refund external call, the attacker's contract changes the royalty fee from 0% to 100%.
- The pool then pays the full sale price as royalty to the attacker's contract, while the attacker paid nothing to the pool.
- Attacker sells the NFT back to the pool to receive base tokens, draining the 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.
In a smart contract, a function can call another contract, which can then call back into the original function. Here, the buy function calculates the royalty fee, then sends a refund to the buyer, and only later sends the royalty payment. A malicious buyer can use the refund step to change the royalty fee to 100%, so the pool pays the entire sale price as royalty while the buyer pays nothing to the pool. The attacker can then sell the NFT back to the pool to get the money.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.