NFT of NFT collection or NFT drop collection can be locked when calling _mint or mintCountTo function to mint it to a contract that does not support ERC721 protocol
The NFT minting functions use OpenZeppelin's _mint instead of _safeMint, so if an NFT is minted to a contract that does not implement the ERC721 receiver interface, the NFT becomes locked and cannot be retrieved. An attacker could exploit this by minting to such a contract, causing permanent loss of the NFT.
Submitted by rbserver, also found by 0xc0ffEE, 0xsolstars, berndartmueller, Bnke0x0, brgltd, cccz, CodingNameKiki, Deivitto, Diraco, Dravee, durianSausage, erictee, ignacio, IllIllI, joestakey, KIntern_NA, Lambda, LeoS, Noah3o6, oyc_109, ReyAdmirado, Rohan16, Rolezn, Sm4rty, Treasure-Seeker, zeesaw, and zkhorse
https://github.com/code-423n4/2022-08-foundation/blob/main/contracts/NFTCollection.sol#L262-L274
https://github.com/code-423n4/2022-08-foundation/blob/main/contracts/NFTDropCollection.sol#L171-L187
Impact
When calling the following _mint or mintCountTo function for minting an NFT of a NFT collection or NFT drop collection, the OpenZeppelin’s ERC721Upgradeable contract’s _mint function is used to mint the NFT to a receiver. If such receiver is a contract that does not support the ERC721 protocol, the NFT will be locked and cannot be retrieved.
https://github.com/code-423n4/2022-08-foundation/blob/main/contracts/NFTCollection.sol#L262-L274
function _mint(string calldata tokenCID) private onlyCreator returns (uint256 tokenId) {
require(bytes(tokenCID).length != 0, "NFTCollection: tokenCID is required");
require(!cidToMinted[tokenCID], "NFTCollection: NFT was already minted");
unchecked {
// Number of tokens cannot overflow 256 bits.
tokenId = ++latestTokenId;
require(maxTokenId == 0 || tokenId <= maxTokenId, "NFTCollection: Max token count has already been minted");
cidToMinted[tokenCID] = true;
_tokenCIDs[tokenId] = tokenCID;
_mint(msg.sender, tokenId);
emit Minted(msg.sender, tokenId, tokenCID, tokenCID);
}
}
https://github.com/code-423n4/2022-08-foundation/blob/main/contracts/NFTDropCollection.sol#L171-L187
function mintCountTo(uint16 count, address to) external onlyMinterOrAdmin returns (uint256 firstTokenId) {
require(count != 0, "NFTDropCollection: `count` must be greater than 0");
unchecked {
// If +1 overflows then +count would also overflow, unless count==0 in which case the loop would exceed gas limits
firstTokenId = latestTokenId + 1;
}
latestTokenId = latestTokenId + count;
require(latestTokenId <= maxTokenId, "NFTDropCollection: Exceeds max tokenId");
for (uint256 i = firstTokenId; i <= latestTokenId; ) {
_mint(to, i);
unchecked {
++i;
}
}
}
For reference, OpenZeppelin’s documentation for _mint states: “Usage of this method is discouraged, use _safeMint whenever possible”.
Proof of Concept
The following steps can occur when minting an NFT of a NFT collection or NFT drop collection.
- The
_mintormintCountTofunction is called withmsg.senderor thetoinput corresponding to a contract. - The OpenZeppelin’s
ERC721Upgradeablecontract’s_mintfunction is called withmsg.senderortoused in Step 1 as the receiver address. - Since calling the OpenZeppelin’s
ERC721Upgradeablecontract’s_mintfunction does not execute the same contract’s_checkOnERC721Receivedfunction, it is unknown if the receiving contract inherits from theIERC721ReceiverUpgradeableinterface and implements theonERC721Receivedfunction or not. It is possible that the receiving contract does not support the ERC721 protocol, which causes the minted NFT to be locked.
Tools Used
VSCode
Recommended Mitigation Steps
https://github.com/code-423n4/2022-08-foundation/blob/main/contracts/NFTCollection.sol#L271 can be changed to the following code.
_safeMint(msg.sender, tokenId);
Also, https://github.com/code-423n4/2022-08-foundation/blob/main/contracts/NFTDropCollection.sol#L182 can be changed to the following code.
_safeMint(to, i);
HardlyDifficult (Foundation) confirmed and commented:
Agree will fix.
Generally we are inclined to skip “safe” by default - it can introduce reentrancy & reverting risk and increase gas costs. When a user is making an action to buy or mint an NFT for themselves, it’s very clear that they are trying to acquire an NFT - so using safe to ensure that they support NFTs seems like a Low risk concern and we are inclined to avoid potential reentrancy/reverts and save costs for the common user paths.
However in this scenario the part that stood out as different is instead of minting for yourself (the msg.sender) we support minting to an arbitrary
toaddress, e.g. for an airdrop type use case. Here specifically it does seem that sending to a list of addresses could be error prone, where a contract address without 721 support was incorrectly captured. To guard against that scenario specifically we are moving forward with this change.Then for consistency we have decided to use safeMint for both collection types because the difference is nuanced.
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
What the attacker needed
Minting to a contract without ERC721 support
The fix
The minting functions should use _safeMint instead of _mint to ensure the receiver can handle ERC721 tokens.
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 send an NFT to a contract (a program on the blockchain), the contract needs a way to accept it. If it doesn't have that, the NFT gets stuck forever. This report shows that the minting function didn't check whether the receiving contract could handle NFTs, so it could lock them.
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.