Skip to content
Smart contracts Audit finding Intermediate

Attackers can bypass `tokensPerMint` and mint lots of tokens in a transaction

The NFT minting contract enforced a per-transaction mint limit, but the safe mint callback let a contract re-enter the mint function and pass the limit check repeatedly. An attacker could therefore mint far more tokens than the limit in a single transaction.

0xSmartContract Follow with GimelSec

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2022-10-nftport-judging/issues/85

Found by

0xSmartContract, GimelSec

Summary

Attackers can bypass tokensPerMint, allowing them to mint over tokensPerMint per transaction.

Vulnerability Detail

NFTCollection has tokensPerMint that enforces the maximum number of tokens the user can mint per transaction. But attackers can bypass tokensPerMint by reentrancy attack to call mint() or presaleMint() multiple times in a transaction.

For example, we assume that tokensPerMint is 5 and we have a large amount of availableSupply():

  1. Alice (attacker) first creates an AttackerContract to call mint(5), then L318 will check that amount <= _deploymentConfig.tokensPerMint and call _safeMint() on L321.
  2. But _safeMint() has a callback that it will call onERC721Received() if to address is a contract.
  3. Now _safeMint() calls onERC721Received() on the AttackerContract, and the AttackerContract re-enter mint(5) again. mint() function will pass the check of tokensPerMint on L318 again. Finally, Alice will mint 5+5 tokens in a transaction.

Impact

Attackers can re-enter mint() function again and again to bypass the check of tokensPerMint, and mint lots of tokens in a transaction.

Code Snippet

https://github.com/sherlock-audit/2022-10-nftport/blob/main/evm-minting-master/contracts/templates/NFTCollection.sol#L317-L322

Tool used

Manual Review

Recommendation

If the protocol wants to check to address, keep _safeMint() and add ReentrancyGuard.sol on mint() and presaleMint() to prevent reentrancy attack.

Or just replace _safeMint() with _mint().

Discussion

hyperspacebunny

I believe this only applies to free mints since msg.value will be 0 when called from the attacker's contract and mint() will revert due to the paymentProvided modifier.

hyperspacebunny

Fixed in https://github.com/nftport/evm-minting-sherlock-fixes/pull/14

rayn731

Fixed, the fix uses reentrancy guard to prevent the bypass.

The original report as published on Sherlock, by 0xSmartContract. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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

Impact

An attacker could mint many more tokens than the per-transaction limit in a single transaction; a commenter noted this may only apply to free mints because paid mints would revert when no payment is sent.

What the attacker needed

Attacker deploys a contract; mint is free or payment check passes

The fix

The issue was fixed by adding a reentrancy guard to the mint and presale mint functions.

How it was found

  1. The attacker deploys a contract that calls the mint function with the maximum allowed amount.
  2. The mint function checks the amount against the per-transaction limit and proceeds to safe mint.
  3. Safe mint calls the receiver callback on the attacker's contract.
  4. The attacker's contract re-enters the mint function with the maximum amount again.
  5. The limit check passes again, so the attacker mints more than the limit in one transaction.

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 can call back into the minting contract while it is still finishing the first mint. Because the limit is only checked at the start of each call, the attacker's contract can keep calling mint again and again. This is called reentrancy: a function is re-entered before it has finished.

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 0xSmartContract

View all

6 more disclosed reports, best first.

More from NFTPort

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.