Skip to content
Business logic Audit finding Intermediate

Discrepancy between nfts minted, price of nft when a generation changes & position of _incrementGeneration() inside _mintInternal() and _mintNewEntity()

The minting price calculation and generation increment logic are misaligned, causing users to pay the maximum price of the previous generation when minting the first NFT of a new generation, or to pay the starting price when they should pay more. This results in financial loss for either the user or the protocol.

Shubham Follow with ArsenLupin, ogKapten and 46 others

Originally disclosed on Code4rena

Submitted by Shubham, also found by ArsenLupin, ogKapten, zhaojohnson, Tomas0707, aldarion, dontonka, 0x3b (1, 2), eLSeR17, p0wd3r, Agontuk (1, 2), jesjupyter, samuraii77, emerald7017, Ruhum, LSHFGJ, FastChecker, 0xHash, anonymousjoe, TECHFUND-inc, Trident-Audits, hakunamatata, avoloder, Abdessamed, MrValioBg, VulnViper (1, 2, 3), PNS, perseus, shikhar229169, Dots, valy001, inzinko, 0x0bserver, dobrevaleri, Udsen, ABAIKUNANBAEV, eierina, StraawHaat, valentin_s2304, dimah7, gkrastenov, LeFy, dhank, denzi_, King_, rbserver, KupiaSec, Ryonen, 0xJoyBoy03, ZdravkoHr, 4rdiii (1, 2), dimulski, and 0xrex

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

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

Vulnerability details

The report covers two bugs:

  • User pays the maximum price of the last generation when minting the 1st nft of the next generation.
  • Wrong positioning of _incrementGeneration() will lead to the user paying the starting price of 1st nft of generation 1 even when the nft being minted is the 1st nft of a different generation.

Proof of Concept

1st Bug Scenario: Taking generation 1 into consideration, only direct minting can occur. No nft can be minted through forging.

File: TraitForgeNft.sol

  function calculateMintPrice() public view returns (uint256) {
    uint256 currentGenMintCount = generationMintCounts[currentGeneration];
    uint256 priceIncrease = priceIncrement * currentGenMintCount;
    uint256 price = startPrice + priceIncrease;
    return price;
  }
  • When the 1st nft is minted, the price to be paid is equal to startPrice (0.005 ether). _tokenIds & generationMintCounts is updated to 1. Meaning that one nft has been minted in the protocol.
  • To mint the 2nd nft, the price to be paid is 0.005 + (0.0000245 * 1) = 0.0050245 ether. _tokenIds & generationMintCounts is updated to 2. Meaning that as the first increment in price happens, the 2nd nft for the protocol has been minted.
  • Lets move forward to the end of generation 1 when _tokenIds and generationMintCounts has reached 9999.
  • The maximum nft that can be minted in a generation is 10000 defined by maxTokensPerGen.
  • The price that a user pays to mint the 10,000th nft is 0.005 + (0.0000245 * 9999) = 0.2499755 ether.
File: TraitForgeNft.sol

function _mintInternal(address to, uint256 mintPrice) internal {
>   if (generationMintCounts[currentGeneration] >= maxTokensPerGen) {
      _incrementGeneration();
    }

>   _tokenIds++;
    uint256 newItemId = _tokenIds;
    _mint(to, newItemId);
    uint256 entropyValue = entropyGenerator.getNextEntropy();

    tokenCreationTimestamps[newItemId] = block.timestamp;
    tokenEntropy[newItemId] = entropyValue;
>   tokenGenerations[newItemId] = currentGeneration;
    generationMintCounts[currentGeneration]++;
    initialOwners[newItemId] = to;
    ...

Since generationMintCounts has not reached maxTokensPerGen, the _incrementGeneration() check is skipped. _tokenIds and generationMintCounts is updated to 10000.

Now comes the interesting part.

  • The next nft that is to be minted should come in generation 2 because 10000 nfts have already been minted at this point.

  • Also, the price for the user to pay to mint the 1st nft of generation 2 should be the startPrice plus the priceIncrementByGen that is 0.005 + 0.000005 = 0.005005 ether.

  • However, when the user calls mintToken(), the price he has to pay is 0.005 + (0.0000245 * 10000) = 0.25 ether (as mentioned in whitepaper).

    This states that to mint the 1st nft in generation 2, user has to pay the maximum price of generation 1.

  • When the call enters _mintInternal(), due to the first check, the generation is incremented from generation 1 to generation 2. _tokenIds is updated to 10001 & generationMintCounts to 1.

  • The price the user should pay be 0.005005 ether but instead, ends up paying 0.25 ether. User’s loss = 0.244995 ether.

2nd Bug Scenario:

Suppose we are at the end of generation 2. The generationMintCounts is 10000 and no minting has taken place through forging for generation 2. Meaning that nft count for generation 3 is zero.

If minting happens directly by calling the mintToken(), the same situation is repeated as explained in the scenario above & the user ends up paying the max price of generation 2 to mint the 1st nft of generation 3.

Now, if the generationMintCounts is at 9999 & _mintNewEntity() is called internally through forging, a different scenario takes place.

File: TraitForgeNft.sol

  function _mintNewEntity(
    address newOwner,
    uint256 entropy,
    uint256 gen
  ) private returns (uint256) {
    require(
      generationMintCounts[gen] < maxTokensPerGen,
      'Exceeds maxTokensPerGen'
    );

>   _tokenIds++;
    uint256 newTokenId = _tokenIds;
    _mint(newOwner, newTokenId);

    tokenCreationTimestamps[newTokenId] = block.timestamp;
    tokenEntropy[newTokenId] = entropy;
    tokenGenerations[newTokenId] = gen;
>   generationMintCounts[gen]++;
    initialOwners[newTokenId] = newOwner;

    if (
>     generationMintCounts[gen] >= maxTokensPerGen && gen == currentGeneration
    ) {
      _incrementGeneration();
    }
    ...
  • Since minting is happening through forging, the mint price is not calculated. _tokenIds becomes 20000 and generationMintCounts becomes 10000.
  • Immediately, the call enters the if condition and the generation is incremented.
  • generationMintCounts for generation 3 is reset to 0.
  • Now, if a user calls mintToken(), the price calculated will be 0.005 + (0.0000345 * 0) = 0.005 ether which the initial starting price of the 1st nft of generation 1. (priceIncrement for gen 3 would be = 0.0000245 + 0.000005 + 0.000005 = 0.0000345).

This scenario would lead to a loss to the protocol and benefit for the user.

Recommended Mitigation Steps

Fixing the bugs would require changes in the calculateMintPrice() and _mintInternal(). However, the protocol will not be able to claim the the last calculated price as intended by the whitepaper but it would then align with the number of nfts minted & the price the user pays.

File: TraitForgeNft.sol

  function calculateMintPrice() public view returns (uint256) {
+  if (generationMintCounts[currentGeneration] > 0) {
      uint256 currentGenMintCount = generationMintCounts[currentGeneration];
      uint256 priceIncrease = priceIncrement * currentGenMintCount;
      uint256 price = startPrice + priceIncrease;
      return price;
   }
+  else {
+     uint256 price = startPrice + ((currentGeneration - 1) * priceIncrementByGen );
+     return price;
   }
  }
File: TraitForgeNft.sol

  function _mintInternal(address to, uint256 mintPrice) internal {
-   if (generationMintCounts[currentGeneration] >= maxTokensPerGen) {
-     _incrementGeneration();
-   }

    _tokenIds++;
    uint256 newItemId = _tokenIds;
    _mint(to, newItemId);
    uint256 entropyValue = entropyGenerator.getNextEntropy();

    tokenCreationTimestamps[newItemId] = block.timestamp;
    tokenEntropy[newItemId] = entropyValue;
    tokenGenerations[newItemId] = currentGeneration;
    generationMintCounts[currentGeneration]++;
    initialOwners[newItemId] = to;

+   if (generationMintCounts[currentGeneration] >= maxTokensPerGen) {
+     _incrementGeneration();
+   }
    ...

Assessed type

Error

TForge1 (TraitForge) confirmed via duplicate Issue #210

Koolex (judge) decreased severity to Medium


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

Likely the generation increment is performed before or after the price calculation in a way that the price for the first NFT of a new generation uses the previous generation's mint count.

Impact

Users could overpay when minting the first NFT of a new generation, or the protocol could lose revenue when the first NFT is minted via forging, depending on the scenario.

What the attacker needed

Access to the minting function; Generation mint count reaches maxTokensPerGen

The fix

The price calculation should account for the generation change, and the generation increment should occur after updating the mint count, as shown in the recommended mitigation.

How it was found

  1. Mint NFTs until generationMintCounts reaches 9999 in generation 1.
  2. Mint the 10000th NFT; the price is calculated as startPrice + priceIncrement * 9999.
  3. Call mintToken() to mint the next NFT; the price is calculated as startPrice + priceIncrement * 10000, which is the maximum price of generation 1.
  4. The _mintInternal function increments the generation after the price is paid, so the user overpays for the first NFT of generation 2.

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 smart contract bug in an NFT minting system. The price of an NFT depends on how many have been minted in the current generation, but the code updates the generation counter at the wrong time. So when a new generation starts, the price is calculated using the old generation's count, making the first buyer overpay or underpay.

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 Shubham

View all

1 more disclosed report, 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.