FighterFarm::reRoll won’t work for nft id greater than 255 due to input limited to uint8
The reRoll function in FighterFarm takes a token ID as an 8-bit unsigned integer, so any fighter with an ID above 255 cannot be passed to the function. An attacker could not exploit this, but legitimate owners of higher-numbered fighters are unable to use the re-roll feature.
abhishek_thaku_r Follow with pontifex, Fulum and 26 others
Originally disclosed on Code4rena
Submitted by abhishek_thaku_r, also found by pontifex, Fulum, 0xDetermination, Greed, givn, stakog, offside0011, maxim371, ktg, alexzoid, immeas, sashik_eth, korok, Draiakoo, MrPotatoMagic, PoeAudits, Tychai0s, ahmedaghadi, kartik_giri_47538, iamandreiski, fnanni, 0xAlix2, klau5, dimulski, 0xShitgem, yotov721, kiqo, and swizz
FighterFarm::reRoll uses uint8 for nft id as input, which will stop people calling this function who owns id greater than 255. It will lead to not being able to use the reRoll to get random traits, which could have been better for there game performance.
Proof of Concept
Affect code can be seen here.
Adding code snippet below as well, for better clarity:
/// @notice Rolls a new fighter with random traits.
/// @param tokenId ID of the fighter being re-rolled.
/// @param fighterType The fighter type.
@> function reRoll(uint8 tokenId, uint8 fighterType) public {
require(msg.sender == ownerOf(tokenId));
require(numRerolls[tokenId] < maxRerollsAllowed[fighterType]);
require(_neuronInstance.balanceOf(msg.sender) >= rerollCost, "Not enough NRN for reroll");
_neuronInstance.approveSpender(msg.sender, rerollCost);
bool success = _neuronInstance.transferFrom(msg.sender, treasuryAddress, rerollCost);
if (success) {
numRerolls[tokenId] += 1;
uint256 dna = uint256(keccak256(abi.encode(msg.sender, tokenId, numRerolls[tokenId])));
(uint256 element, uint256 weight, uint256 newDna) = _createFighterBase(dna, fighterType);
fighters[tokenId].element = element;
fighters[tokenId].weight = weight;
fighters[tokenId].physicalAttributes = _aiArenaHelperInstance.createPhysicalAttributes(
newDna,
generation[fighterType],
fighters[tokenId].iconsType,
fighters[tokenId].dendroidBool
);
_tokenURIs[tokenId] = "";
}
}
If you notice the highlighted line (first line of function), it takes uint8 as input for tokenId parameter. Which will restrict users to call this function when they own nft id greater than 255.
Value will go out of bounds when user will input 256 or more.
Recommended Mitigation Steps
Use uint256 for nft id input to fix the issue.
- function reRoll(uint8 tokenId, uint8 fighterType) public {
+ function reRoll(uint256 tokenId, uint8 fighterType) public {
require(msg.sender == ownerOf(tokenId));
require(numRerolls[tokenId] < maxRerollsAllowed[fighterType]);
require(_neuronInstance.balanceOf(msg.sender) >= rerollCost, "Not enough NRN for reroll");
_neuronInstance.approveSpender(msg.sender, rerollCost);
bool success = _neuronInstance.transferFrom(msg.sender, treasuryAddress, rerollCost);
if (success) {
numRerolls[tokenId] += 1;
uint256 dna = uint256(keccak256(abi.encode(msg.sender, tokenId, numRerolls[tokenId])));
(uint256 element, uint256 weight, uint256 newDna) = _createFighterBase(dna, fighterType);
fighters[tokenId].element = element;
fighters[tokenId].weight = weight;
fighters[tokenId].physicalAttributes = _aiArenaHelperInstance.createPhysicalAttributes(
newDna,
generation[fighterType],
fighters[tokenId].iconsType,
fighters[tokenId].dendroidBool
);
_tokenURIs[tokenId] = "";
}
}
raymondfam (lookout) commented:
Unsigned integer type limitation indeed.
brandinho (AI Arena) confirmed
Fixed reRoll for fighters with tokenIds greater than 255. https://github.com/ArenaX-Labs/2024-02-ai-arena-mitigation/pull/1
Status: Mitigation confirmed. Full details in reports from niser93, d3e4, and fnanni.
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 tokenId parameter of reRoll is declared as uint8 instead of uint256, limiting valid IDs to 0–255.
Impact
Owners of fighters with token IDs greater than 255 cannot call reRoll to get new random traits, so the feature is unavailable to them.
What the attacker needed
Own a fighter with token ID above 255
The fix
The issue was fixed by changing the tokenId parameter type to uint256.
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.
A smart contract function accepts a number that is stored in a small box that can only hold values from 0 to 255. If a fighter's ID is 256 or higher, the value does not fit in that box, so the function cannot be called for that fighter. This is a usability bug, not a security hole, because it blocks a feature rather than letting someone steal or break something.
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.