Forger Entities can forge more times than intended
The forging count for a forger entity is checked before it is incremented, so an entity can list for forging one more time than its forge potential allows. An attacker could forge more times than intended, exceeding the limit set by the protocol.
AvantGard Follow with Tigerfrake, gajiknownnothing and 48 others
Originally disclosed on Code4rena
Submitted by AvantGard, also found by Tigerfrake, gajiknownnothing, zhaojohnson, robertodf99, Abdessamed, _karanel, 0x3b, p0wd3r, ZanyBonzy, almurhasan, Agontuk, oxwhite, samuraii77, D_Auditor, MinhTriet, peanuts, stanchev, Tamer, 0xb0k0, air_0x, anonymousjoe, Autosaida, MrValioBg, pfapostol, shikhar229169, hakunamatata, LeFy, dontonka, shui, VulnViper, jesjupyter, sl1, rndquu, Coinymous, cryptomoon, 0xvd, 0xlemon, onthehunt11, 0xPwned, Xcrypt, dyoff, nervouspika, perseus, Nihavent, LonelyWolfDemon, den-sosnowsky, Udsen, inzinko, dhank, hail_the_lord, turvy_fuzz, rbserver, yotov721, KupiaSec, al88nsk, Ryonen, klau5, nnez, ke1caM, shaflow2, shaka, ZdravkoHr, Beosin, and dimulski
Vulnerability details
As per the docs:
Entities’ Forge Potential is determined by the [5] digit in entropy Each entity may have the ability to forge 0-9 times depending on its Forge Potential.
This means an entity with forgePotential of 0 is infertile, an entity with forgePotential = 1 can forge 1 time an year, an entity with forgePotential = 2 can forge 2 times an year and so on.
The number of times an entity forges is tracked in the forgingCounts mapping. This mapping is incremented for both the forgerTokenId and mergerTokenId everytime a successful forging happens thru forgeWithListed.
The forgingCounts[] of mergerTokenIds is handled correctly. The increment of forgingCounts[] precedes the condition check:
forgingCounts[mergerTokenId]++;
require(mergerForgePotential > 0 &&
forgingCounts[mergerTokenId] <= mergerForgePotential,
'forgePotential insufficient'
);
However, Forger Entities forgerTokenId are able to exceed the forging limit due to increment of forgingCounts[] happening after the condition check. The check is made during the listForForging call, but the forgingCounts[] mapping is only incremented during forgeWithListed call. This allows the ‘Forger Entities’ to list one more time than intended.
This results in each Forger entity with non-zero forgingPotential having the ability to forge 2-10 times, instead of 1-9 times intended by the protocol.
Impact
As long as Forger Entities have a non-zero forgingPotential, the Entity is able to exceed the limit intended by forgingPotential.
Proof of Concept
Assume a Forger Entity of:
_tokenIds = 123
forgingPotential = 1 (extracted from the 5th digit of entropy).
Initial forgingCounts for tokenId 1 is 0.
First Listing:
- During listing:
forgingCounts[123] = 0 - Check:
forgingCounts[123] <= forgePotential (0 <= 1)=> Pass - During forging :
forgingCounts[123]++=> 1 (during forging)
Second Listing:
- During listing:
forgingCounts[123] = 1 - Check:
forgingCounts[123] <= forgePotential (1 <= 1)=> Pass - During forging:
forgingCounts[123]++=> 2
Third Listing: (Reverts only at the third attempt)
- During listing:
forgingCounts[123] = 2(during listing) - Check:
forgingCounts[123] < forgePotential (2 <= 1)=> Fail - During forging: forging does not proceed.
This shows a Forger Entity with a forgingPotential of 1 can forge 2 times. Similarly, a forger entity with forgingPotential of 9 can forge 10 times.
Tools Used
Foundry
Recommended Mitigation Steps
This can be fixed in different ways. The simplest way is to use the < instead of the <= in the listForForging condition check.
forgingCounts[tokenId] < forgePotential
require(
forgePotential > 0 && forgingCounts[tokenId] < forgePotential,
'Entity has reached its forging limit'
);
Assessed type
Invalid Validation
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
The forging count is incremented after the limit check, so the check uses a stale value and allows one extra forging.
Impact
Each forger entity with non-zero forging potential can forge one more time than intended, so entities can forge 2-10 times instead of 1-9 times.
What the attacker needed
A forger entity with non-zero forging potential
The fix
The check should use a strict less-than comparison instead of less-than-or-equal, so that the count cannot reach the potential before incrementing.
How it was found
- List a forger entity with forging potential 1 for forging (count is 0, check passes).
- Forge with that entity (count becomes 1).
- List the same entity again (count is 1, check passes because 1 <= 1).
- Forge again (count becomes 2).
- A third listing fails because count is 2 and 2 <= 1 is false.
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.
In this smart contract, each entity has a limit on how many times it can 'forge' (create new items). The code checks the limit when the entity is listed for forging, but only increases the count after the forging happens. This means the check uses an outdated count, allowing one extra forging. A beginner should understand that the order of checking and updating a counter matters.
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.