Royalty recipients will not get fair share of royalties
Royalties for NFTs bought together are calculated by dividing the total sale price equally among all NFTs, ignoring each NFT's weight. This means recipients of more expensive NFTs receive less royalty than they should, while cheaper NFTs get more.
0xLanterns Follow with abiih, bshramin and 19 others
Originally disclosed on Code4rena
Submitted by 0xLanterns, also found by abiih, bshramin, adriro, CodingNameKiki, Bason, DishWasher, AkshaySrivastav, Voyvoda, savi0ur, aviggiano, MiloTruck, Nyx, RaymondFam, T1MOH, DadeKuma, ElKu, Dug, sashik_eth, yixxas, J4de, and Ruhum
Recipients of NFTs who accept royalties will not get their fair share of royalties. This is because royalties are calculated by dividing the sales price equally amongst all sold NFTs in that purchase. The issue with this is that it assumes all NFTs cost the same amount when it comes time to deal out royalties. If NFTs cost different amounts, then they should be getting an amount of royalties based on that weight relative to the other NFTs. The impact of this is that Royalties will not be distributed evenly at the expense of the more expensive NFT. Meaning that recipients of the expensive NFT will always receive less than they are owed. And the cheaper ones will get more than owed. In short, this is a loss of funds or misdistribution of funds.
Proof of Concept
The easiest way to test this will to be add this snippet into Milady.sol.
Using this to have access to ERC2981’s setRoyaltyInfo():
file: Milady.sol
function setRoyaltyInfo(
uint256 _royaltyFeeRate,
address _royaltyRecipient
) public {
royaltyFeeRate = _royaltyFeeRate;
royaltyRecipient = _royaltyRecipient;
}
function supportsInterface(
bytes4 interfaceId
) public view override(ERC2981, ERC721) returns (bool) {
return super.supportsInterface(interfaceId);
}
function royaltyInfo(
uint256 id,
uint256 salePrice
) public view override returns (address, uint256) {
return super.royaltyInfo(id, salePrice);
}
function setRoyaltyInfo(uint256 id, address reciever, uint96 fee) public {
super._setTokenRoyalty(id, reciever, fee);
}
}
Then add this snippet to Fixture.sol:
file: Fixture.sol
GodsUnchained public gu = new GodsUnchained();
Then add this snippet to token-weights.json:
Changing the weights to represent the two NFT’s being bought in this case.
[
[
1,
1
],
[
2,
10
]
]
Lastly to test this, you need to add this test to Buy.t.sol:
// forge test --match-test test_unevenRoyalties --ffi
function test_unevenRoyalties() public {
// arrange
privatePool = new PrivatePool(
address(factory),
address(royaltyRegistry),
address(stolenNftOracle)
);
privatePool.initialize(
baseToken,
address(gu),
virtualBaseTokenReserves,
12e18,
changeFee,
feeRate,
generateMerkleRoot(),
true,
true
);
//> owner of nft's
address user1 = address(0xbeefbeef);
address user2 = address(0xfeebfeeb);
//> mint and push nft's one is 1x one is 10x
gu.mint(address(privatePool), 1);
tokenIds.push(1);
tokenWeights.push(1e18);
gu.mint(address(privatePool), 2);
tokenIds.push(2);
tokenWeights.push(10e18);
//> set fees. 1% for one user, 10% for the other
gu.setRoyaltyInfo(1, user1, 100);
gu.setRoyaltyInfo(2, user2, 1000);
//> set up
proofs = generateMerkleProofs(tokenIds, tokenWeights);
uint256 weightSum = privatePool.sumWeightsAndValidateProof(
tokenIds,
tokenWeights,
proofs
);
(uint256 netInputAmount, , ) = privatePool.buyQuote(weightSum);
//> buy
privatePool.buy{value: netInputAmount * 2}(
tokenIds,
tokenWeights,
proofs
);
//> assert that users got equal reserves. with different weights and royalty fees users should NOT be getting the same amount of royalties
//> the royalty fee of user2 is 10 times greater than user one. This shows that user2 is getting 10 times as much royaltys when user 2 should be getting 100 times as much as user one.
//> user 1: 1% of 1 eth = 0.01 eth
//> user 2: 10% of 10 eth = 1 eth
//> 0.01 eth * 100 = 1eth.
//> user 2 should be getting 100 times more royalties than user 1 but is only getting 10 times the amount.
assertEq(user1.balance * 10, address(user2).balance);
}
Tools Used
Foundry
Recommended Mitigation Steps
To address this issue, it is recommended that the weight of NFTs relative to other NFTs being purchased should be taken into consideration when calculating royalties.
outdoteth (Caviar) acknowledged, but disagreed with severity and commented:
It is commented in the code that it’s assumed all NFTs in the purchase are of the same price: https://github.com/code-423n4/2023-04-caviar/blob/main/src/PrivatePool.sol#L334
Assuming that the recipient is the same for each NFTs royalty payment (which it almost always is in practice), then this makes sense.
NFT 1 is worth 1 ETH NFT 2 is worth 2 ETH<
(1 + 2) / 2 = 1.5 ETH 1 / 2 + 2 / 2 = 1.5 ETH
The output is the same. The additional complexity of individually calculating each price is not worth it.
Alex the Entreprenerd (judge) decreased severity to Medium and commented:
I believe that the finding is valid because the EIP specifies that each NFT may have a different royalty, the contract is still fetching the specific royalty for each NFT id, leading me to believe that this will cause incorrect royalty payouts in specific cases in which a collection has different royalties based on the NFT id.
As a developer, I agree with the Sponsor with a nofix and believe in practice that this should not be an issue.
As a Judge, I believe the finding meets the requirements of improperly implementing an EIP, which can cause a loss of yield. For this reason, I think Medium Severity to be appropriate.
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 royalty calculation divides the total sale price equally by the number of NFTs instead of using each NFT's weight to determine its share.
What the attacker needed
Multiple NFTs bought in one transaction; NFTs have different weights/prices; royalty recipients differ or fees differ
The fix
The weight of each NFT relative to others in the purchase should be taken into account when calculating royalties.
How it was found
- Add royalty functions to the Milady contract to enable setting royalties.
- Deploy a test NFT contract in the fixture.
- Set token weights in the JSON file to represent two NFTs with different values.
- Write a test that mints two NFTs with different weights and royalty fees, then buys them together.
- Assert that the royalty recipients receive amounts proportional to their NFTs' weights and fees.
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 buy several NFTs in one transaction, the smart contract pays royalties to the creators. The contract splits the total royalty amount equally among all the NFTs, even if some are worth much more than others. This means the creator of an expensive NFT gets too little, and the creator of a cheap one gets too much.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.