Skip to content
Smart contracts Audit finding Advanced

Duplicate NFTs Can Be Minted if payableToken Has a Callback Attached to it

The mintToken function pays the payableToken before minting and updating totalSupply, allowing a reentrant call to bypass the maxSupply check and mint duplicate NFTs when the last token is minted.

leastwood Follow

Originally disclosed on Code4rena

Submitted by leastwood

https://github.com/code-423n4/2022-03-joyn/blob/main/core-contracts/contracts/CoreCollection.sol#L139-L167

https://github.com/code-423n4/2022-03-joyn/blob/main/core-contracts/contracts/ERC721Payable.sol#L50-L56

Impact

The mintToken() function is called to mint unique tokens from an ERC721 collection. This function will either require users to provide a merkle proof to claim an airdropped token or pay a fee in the form of a payableToken. However, because the payableToken is paid before a token is minted, it may be possible to reenter the mintToken() function if there is a callback attached before or after the token transfer. Because totalSupply() has not been updated for the new token, a user is able to bypass the totalSupply() + amount <= maxSupply check. As a result, if the user mints the last token, they can reenter and mint duplicate NFTs as the way tokenId is generated will wrap around to the start again.

Proof of Concept

For the sake of this example, let’s say startingIndex = 0 and maxSupply = 100. tokenId is minted according to ((startingIndex + totalSupply()) % maxSupply) + 1. If we see that a user mints a token where totalSupply() = maxSupply - 1 = 99 and they reenter the function, then the next token to mint will actually be of index 1 as totalSupply() % maxSupply = 0. Calculating the first tokenId, we get ((0 + 0) % maxSupply) + 1 = 1 which is a duplicate of our example.

Recommended Mitigation Steps

Consider adding reentrancy protections to prevent users from abusing this behaviour. It may also be useful to follow the checks-effects pattern such that all external/state changing calls are made at the end.

sofianeOuafir (Joyn) confirmed and commented:

This is an issue we intend to investigate and fix if indeed it is an issue

deluca-mike (judge) commented:

This is a valid high risk issue. Also, for reference, the checks-effects-interactions (CEI) pattern suggests you, in this order:

  • perform checks that something can be done
  • perform the effects (update storage and emit events)
  • interact with other functions/contracts (since you may not be sure they will call out and re-enter)

The original report as published on Code4rena, by leastwood. 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

Likely the contract performs an external call to the payableToken before updating totalSupply, allowing reentrancy that bypasses the maxSupply check and causes tokenId generation to wrap around.

Impact

A user could mint duplicate NFTs, potentially exceeding the intended maximum supply and duplicating token IDs.

What the attacker needed

PayableToken has a callback; user mints the last token

The fix

The contract should be fixed by adding reentrancy protections and following the checks-effects-interactions pattern, updating state before external calls.

How it was found

  1. Set startingIndex to 0 and maxSupply to 100.
  2. Mint a token when totalSupply() equals maxSupply - 1 (99).
  3. Reenter the mintToken function before totalSupply is updated.
  4. Observe that the next tokenId wraps around to 1, duplicating an existing token.

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 mint an NFT, the contract first takes your payment and then creates the token. If the payment token can call back into the minting contract before the token count is updated, an attacker can mint again and get a duplicate token. This happens because the contract checks the supply limit before it updates the count.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by leastwood

View all

70 more disclosed reports, best first.

More from Joyn contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.