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():
- Alice (attacker) first creates an
AttackerContractto callmint(5), then L318 will check thatamount <= _deploymentConfig.tokensPerMintand call_safeMint()on L321. - But
_safeMint()has a callback that it will callonERC721Received()iftoaddress is a contract. - Now
_safeMint()callsonERC721Received()on theAttackerContract, and theAttackerContractre-entermint(5)again.mint()function will pass the check oftokensPerMinton 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
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.
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
- The attacker deploys a contract that calls the mint function with the maximum allowed amount.
- The mint function checks the amount against the per-transaction limit and proceeds to safe mint.
- Safe mint calls the receiver callback on the attacker's contract.
- The attacker's contract re-enters the mint function with the maximum amount again.
- 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.