Skip to content
Business logic Audit finding Intermediate

Each generation should have 1 “Golden God” NFT, but there could be 0

The location of the rare 'Golden God' NFT is chosen pseudo-randomly, but the selection can place it in the last 10 entropy slots of a generation, which are never minted because minting stops at 10,000 NFTs. As a result, a generation could have zero Golden God NFTs, contrary to the intended design.

Kalogerone Follow with samuraii77, anonymousjoe and 4 others

Originally disclosed on Code4rena

Submitted by Kalogerone, also found by samuraii77, anonymousjoe, Decap, sl1, Shahil_Hussain, and yotov721

https://github.com/code-423n4/2024-07-traitforge/blob/main/contracts/EntropyGenerator/EntropyGenerator.sol#L206

https://github.com/code-423n4/2024-07-traitforge/blob/main/contracts/TraitForgeNft/TraitForgeNft.sol#L345

https://github.com/code-423n4/2024-07-traitforge/blob/main/contracts/EntropyGenerator/EntropyGenerator.sol#L10

Vulnerability Details

The location of the “Golden God” NFT with entropy of 999999 is calculated by the initializeAlphaIndices function in the EntropyGenerator contract.

    function initializeAlphaIndices() public whenNotPaused onlyOwner {
        uint256 hashValue = uint256(keccak256(abi.encodePacked(blockhash(block.number - 1), block.timestamp)));

        uint256 slotIndexSelection = (hashValue % 258) + 512;
        uint256 numberIndexSelection = hashValue % 13;

        slotIndexSelectionPoint = slotIndexSelection;
        numberIndexSelectionPoint = numberIndexSelection;
    }

It is confirmed by the devs/sponsors that TraitForgeNft contract is the only allowed caller of the initializeAlphaIndices. As we can see, the function is protected by a modifier and when the generation is getting incremented, TraitForgeNft is the caller.

TraitForgeNft.sol

    function _incrementGeneration() private {
        require(generationMintCounts[currentGeneration] >= maxTokensPerGen, "Generation limit not yet reached");
        currentGeneration++;
        generationMintCounts[currentGeneration] = 0;
        priceIncrement = priceIncrement + priceIncrementByGen;
@>      entropyGenerator.initializeAlphaIndices();
        emit GenerationIncremented(currentGeneration);
    }

Each generation is supposed to contain 1 “Golden God” NFT. According to the TraitForge docs:

Entropy is set into uint256 slots, with each slot able to contain a 78 digit number - or 13 concatenated entropies. Thus 770 slots are needed to contain 10,000 entropies.

Entropies are stored in the following array:

    uint256[770] private entropySlots; // Array to store entropy values

We have 770 slots with 13 entropies each, which means 770 * 13 = 10,010 total entropies generated.

The last 10 entropies are skipped when the generation ends, since there is a max mint value of 10,000 NFTs per generation and when it is reached, it increments to the next generation. There is a chance that the 999999 entropy is placed in the last entropy slot, in the last 10 entropies of that slot and is lost and not mintable. Since the generation has incremented by the TraitForgeNft contract, the action is not recoverable.

Impact

There is a chance that the “Golden God” NFT is completely lost and not mintable for a generation. This goes against the docs and the expectations of the game.

Proof of Concept

  1. 10,000 NFTs get minted in a generation, so it’s time to increment to the next generation.
  2. TraitForgeNft contract increments generation and calls the initializeAlphaIndices to set the location of the “Golden God”.
  3. “Golden God” entropy is set in the last slot (770th), in the last 10 entropy slots.
  4. The “Golden God” NFT is not mintable, as there needs to be more than 10,000 mints in the generation, which is not possible.

Recommended Mitigation Steps

Implement a check that ensures that the 999999 entropy is not set out of bounds.

    function initializeAlphaIndices() public whenNotPaused onlyOwner {
        uint256 hashValue = uint256(keccak256(abi.encodePacked(blockhash(block.number - 1), block.timestamp)));

        uint256 slotIndexSelection = (hashValue % 258) + 512;
        uint256 numberIndexSelection = hashValue % 13;

        slotIndexSelectionPoint = slotIndexSelection;
        numberIndexSelectionPoint = numberIndexSelection;

+       if (slotIndexSelection == 769 && numberIndexSelection > 3) {
+           initializeAlphaIndices();
+       }
    }

Assessed type

Math

Kalogerone (warden) commented:

@Koolex - I believe this issue is placed in the wrong group/duplicate of Issue #656. Issue #656 describes how there can be 0 Golden God NFT due to the forging, while my issue doesn’t involve forging at all. My issue describes how there can be 0 Golden God NFT even in the first generation which forging doesn’t affect, simply if the Golden God NFT slot gets placed in the last 10 slots during the entropy generation.

EDIT: My issue can even happen in the 1st generation while Issue #656 can’t.

Koolex (judge) commented:

@Kalogerone - Could you please provide a PoC for this?

Kalogerone (warden) commented:

@Koolex - I have created a foundry test file for my PoC so I could fuzz block numbers, because the location of the Golden God entropy relies on the block number, as shown in the report. I have created a tests.t.sol file inside the test folder.

Coded PoC

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "forge-std/Test.sol";
import {console2} from "forge-std/Test.sol";
import {EntropyGenerator} from "../contracts/EntropyGenerator/EntropyGenerator.sol";
import {DevFund} from "../contracts/DevFund/DevFund.sol";
import {Airdrop} from "../contracts/Airdrop/Airdrop.sol";
import {TraitForgeNft} from "../contracts/TraitForgeNft/TraitForgeNft.sol";
import {EntityForging} from "../contracts/EntityForging/EntityForging.sol";
import {IEntityForging} from "../contracts/EntityForging/IEntityForging.sol";
import {NukeFund} from "../contracts/NukeFund/NukeFund.sol";
import {EntityTrading} from "../contracts/EntityTrading/EntityTrading.sol";
//import {IEntityTrading} from "../contracts/EntityTrading/IEntityTrading.sol";
import {TraitForgeNftMock} from "../contracts/TraitForgeNft/TraitForgeNftMock.sol";

contract ContractBTest is Test {
    address public owner = makeAddr("owner");
    address player1 = makeAddr("player1");
    address player2 = makeAddr("player2");

    EntityForging entityForging;
    TraitForgeNft traitForgeNft;
    EntropyGenerator entropyGenerator;
    Airdrop airdrop;
    DevFund devFund;
    NukeFund nukeFund;
    EntityTrading entityTrading;

    function setUp() public {
        vm.startPrank(owner);
        // Deploy TraitForgeNft contract
        traitForgeNft = new TraitForgeNft();

        // Deploy Airdrop contract
        airdrop = new Airdrop();
        traitForgeNft.setAirdropContract(address(airdrop));
        airdrop.transferOwnership(address(traitForgeNft));

        // Deploy entropyGenerator contract
        entropyGenerator = new EntropyGenerator(address(traitForgeNft));
        entropyGenerator.transferOwnership(address(traitForgeNft));

        traitForgeNft.setEntropyGenerator(address(entropyGenerator));

        // Deploy EntityForging contract
        entityForging = new EntityForging(address(traitForgeNft));
        traitForgeNft.setEntityForgingContract(address(entityForging));

        devFund = new DevFund();
        nukeFund = new NukeFund(address(traitForgeNft), address(airdrop), payable(address(devFund)), payable(owner));
        traitForgeNft.setNukeFundContract(payable(address(nukeFund)));

        entityTrading = new EntityTrading(address(traitForgeNft));
        entityTrading.setNukeFundAddress(payable(address(nukeFund)));

        nukeFund.setAgeMultplier(1);
        vm.stopPrank();

        vm.deal(player1, 10 ether);
        vm.deal(player2, 10 ether);

        // to skip whitelist
        vm.warp(block.timestamp + 3 days);
    }

    function testFuzzBlocks(uint256 blockn) public {
        // panic reverts
        vm.assume(blockn != 0);

        // blocks already found that provide the wanted outcome
        vm.assume(blockn != 2207465665814711110);
        vm.assume(blockn != 3307);
        vm.assume(blockn != 2774072794181196);

        vm.roll(blockn);

        vm.startPrank(address(traitForgeNft));
        entropyGenerator.initializeAlphaIndices();
        vm.stopPrank();

        uint256 slotIndexSelection = entropyGenerator.slotIndexSelectionPoint();

        assert(slotIndexSelection != 769);
    }

    function testBlock() public {
        vm.roll(3307);

        vm.startPrank(address(traitForgeNft));
        entropyGenerator.initializeAlphaIndices();

        uint256 slotIndexSelection = entropyGenerator.slotIndexSelectionPoint();
        uint256 numberIndexSelection = entropyGenerator.numberIndexSelectionPoint();

        assert(slotIndexSelection == 769 && numberIndexSelection > 2);

        console2.log(slotIndexSelection);
        console2.log(numberIndexSelection);
        vm.stopPrank();
    }
}

Explanation of the PoC code:

The testFuzzBlocks fuzz tests different block numbers and calls the entropyGenerator.initializeAlphaIndices() function which sets the position of the Golden God NFT. If we find the NFT placed in the last position possible (slotIndexSelection == 769 since 770 total slots, 0-769), we take the block.number and use it in the testBlock function, placing it inside the vm.roll() function.

Then, we confirm if the numberIndexSelection value is in the last 10 possible places (13 total places, 0-2 are the first 3 places, 3-12 are the last 10). If numberIndexSelection > 2, then the Golden God NFT is placed in the last 10 places possible and is not mintable. See report’s Vulnerability Details section for more detailed information on the reason why.


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

The function that selects the Golden God's position does not check whether the chosen slot and index fall within the first 10,000 mintable entropies, allowing it to land in the last 10 unused slots.

What the attacker needed

The generation must reach 10,000 mints; The random selection must land in the last 10 slots

The fix

The initializeAlphaIndices function should be modified to re-roll if the selected slot and index would place the Golden God in the last 10 entropies.

How it was found

  1. 10,000 NFTs are minted in a generation, triggering the increment to the next generation.
  2. The TraitForgeNft contract increments the generation and calls initializeAlphaIndices to set the Golden God location.
  3. The Golden God entropy is placed in the last slot (770th) and within the last 10 entropy positions.
  4. The Golden God NFT cannot be minted because that would require more than 10,000 mints in the generation, which is impossible.

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.

Each generation of NFTs is supposed to include one special 'Golden God' NFT. The code that picks where this NFT will appear can accidentally choose a position that is never reached because minting stops at 10,000. So the special NFT might never be created, which breaks the game's rules.

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.

More by Kalogerone

View all

9 more disclosed reports, best first.

More from TraitForge

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.