Sale contracts can be bricked if any other minter mints a token with an id that overlaps the sale
The sale contracts mint tokens with sequential IDs starting from a configurable value. If any other entity mints a token with an ID that falls within the sale's range, the buy function will revert because it tries to mint an already existing token, bricking the sale and potentially locking funds.
Submitted by adriro, also found by Ch_301, 0xRobocop, carrotsmuggler, neumo, HollaDieWaldfee, jonatascm, AkshaySrivastav, KingNFT, bin2chen, hihen, 0xA5DF, hansfriese, gasperpre, cccz, and lumoswiz
https://github.com/code-423n4/2022-12-escher/blob/main/src/minters/FixedPrice.sol#L66
https://github.com/code-423n4/2022-12-escher/blob/main/src/minters/OpenEdition.sol#L67
https://github.com/code-423n4/2022-12-escher/blob/main/src/minters/LPDA.sol#L74
In all of the three sale types of contracts, the buy function will mint tokens with sequential ids starting at a given configurable value.
If an external entity mints any of the token ids involved in the sale, then the buy procedure will fail since it will try to mint an already existing id. This can be the creator manually minting a token or another similar contract that creates a sale.
Taking the FixedPrice contract as the example, if any of the token ids between sale.currentId + 1 and sale.finalId is minted by an external entity, then the buy process will be bricked since it will try to mint an existing token id. This happens in lines 65-67:
https://github.com/code-423n4/2022-12-escher/blob/main/src/minters/FixedPrice.sol#L65-L67
for (uint48 x = sale_.currentId + 1; x <= newId; x++) {
nft.mint(msg.sender, x);
}
The implementation of the mint function (OZ contracts) requires that the token id doesn’t exist:
function _mint(address to, uint256 tokenId) internal virtual {
require(to != address(0), "ERC721: mint to the zero address");
require(!_exists(tokenId), "ERC721: token already minted");
...
Impact
If any of the scenarios previously described happens, then the contract will be bricked since it will try to mint an already existing token and will revert the transaction. There is no way to update the token ids in an ongoing sale, which means that the buy function will always fail for any call.
This will also cause a loss of funds in the case of the FixedPrice and LPDA contracts since those two require that all token ids are sold before funds can be withdrawn.
Proof of Concept
The following test reproduces the issue using the FixedPrice sale contract type.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "forge-std/Test.sol";
import {FixedPriceFactory} from "src/minters/FixedPriceFactory.sol";
import {FixedPrice} from "src/minters/FixedPrice.sol";
import {OpenEditionFactory} from "src/minters/OpenEditionFactory.sol";
import {OpenEdition} from "src/minters/OpenEdition.sol";
import {LPDAFactory} from "src/minters/LPDAFactory.sol";
import {LPDA} from "src/minters/LPDA.sol";
import {Escher721} from "src/Escher721.sol";
contract AuditTest is Test {
address deployer;
address creator;
address buyer;
FixedPriceFactory fixedPriceFactory;
OpenEditionFactory openEditionFactory;
LPDAFactory lpdaFactory;
function setUp() public {
deployer = makeAddr("deployer");
creator = makeAddr("creator");
buyer = makeAddr("buyer");
vm.deal(buyer, 1e18);
vm.startPrank(deployer);
fixedPriceFactory = new FixedPriceFactory();
openEditionFactory = new OpenEditionFactory();
lpdaFactory = new LPDAFactory();
vm.stopPrank();
}
function test_FixedPrice_buy_MintBrickedContract() public {
// Suppose there's another protocol or account that acts as a minter, it can even be another sale that overlaps ids.
address otherMinter = makeAddr("otherMinter");
// Setup NFT and create sale
vm.startPrank(creator);
Escher721 nft = new Escher721();
nft.initialize(creator, address(0), "Test NFT", "TNFT");
uint48 startId = 10;
uint48 finalId = 15;
uint96 price = 1;
FixedPrice.Sale memory sale = FixedPrice.Sale(
startId, // uint48 currentId;
finalId, // uint48 finalId;
address(nft), // address edition;
price, // uint96 price;
payable(creator), // address payable saleReceiver;
uint96(block.timestamp) // uint96 startTime;
);
FixedPrice fixedSale = FixedPrice(fixedPriceFactory.createFixedSale(sale));
nft.grantRole(nft.MINTER_ROLE(), address(fixedSale));
nft.grantRole(nft.MINTER_ROLE(), otherMinter);
vm.stopPrank();
// Now, other minter mints at least one of the ids included in the sale
vm.prank(otherMinter);
nft.mint(makeAddr("receiver"), startId + 2);
// Now buyer goes to sale and tries to buy the NFTs
vm.startPrank(buyer);
// The following will revert. The contract will be bricked since it will be impossible to advance with the ids from the sale.
uint256 amount = finalId - startId;
vm.expectRevert("ERC721: token already minted");
fixedSale.buy{value: price * amount}(amount);
vm.stopPrank();
}
}
Recommended Mitigation Steps
The quick solution would be to mint the tokens to the sale contract, but that will require a potentially high gas usage if the sale involves a large amount of tokens.
Other alternatives would be to “reserve” a range of tokens to a particular minter (and validate that each one mints over their own range) or to have a single minter role at a given time, so there’s just a single entity that can mint tokens at a given time.
mehtaculous (Escher) disputed, disagreed with severity and commented:
This is considered more of a Medium risk and a solution would be to have the ERC-721 contract manage the sequential IDs and have the sales contracts simply request minting of the next token ID.
berndartmueller (judge) decreased severity to Medium and commented:
Due to the external requirement of this finding, I’m downgrading it to Medium severity.
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 sale contracts assume they have exclusive control over the token ID range, but the NFT contract allows other minters to create tokens with any ID, so collisions cause minting to revert.
Impact
The sale contract becomes unusable because every buy attempt reverts, and in FixedPrice and LPDA sales, funds may be locked since all tokens must be sold before withdrawal.
What the attacker needed
Another minter has permission to mint; the other minter mints an ID in the sale range
The fix
The NFT contract should manage sequential IDs and sales contracts should request the next token ID, or token ID ranges should be reserved for each minter.
How it was found
- Deploy the NFT contract and create a fixed-price sale with a start ID and final ID.
- Grant the minter role to the sale contract and to another minter address.
- Have the other minter mint a token with an ID that falls within the sale's range.
- Attempt to buy the sale's tokens; the transaction reverts because the token ID already exists.
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.
A smart contract sells NFTs by creating them one by one with increasing numbers. If someone else creates an NFT with a number that the sale will later try to use, the sale's creation step fails and the whole buying process stops working. This can lock up money that buyers have paid.
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.