Fighter created by mintFromMergingPool can have arbitrary weight and element
The claimRewards function in MergingPool passes user-supplied customAttributes directly to mintFromMergingPool without validating weight or element, allowing a winner to create a fighter with arbitrary values such as zero or the maximum uint256. This could let a fighter have an extreme weight that prevents knockback or an element outside the allowed set, affecting battle outcomes.
ktg Follow with visualbits, d3e4 and 48 others
Originally disclosed on Code4rena
Submitted by ktg, also found by visualbits, d3e4, vnavascues, dutra, niser93, alexxander, denzi_, Silvermist, evmboi32, Tumelo_Crypto, givn, Blank_Space, yotov721, juancito, Matue, stakog, 0xWallSecurity, FloatingPragma, ahmedaghadi, aslanbek, agadzhalov, Topmark, immeas, _eperezok, AlexCzm, klau5, petro_1912, Tendency, BARW, peter, ke1caM, Draiakoo, McToady, 0xCiphky, 0xvj, pkqs90, cats, fnanni, rspadi, krikolkk, handsomegiraffe, Giorgio, 0xlemon, kiqo, SpicyMeatball, 0xRiO, haxatron, MrPotatoMagic, 0xDetermination, and sl1
https://github.com/code-423n4/2024-02-ai-arena/blob/main/src/FighterFarm.sol#L313-L331 https://github.com/code-423n4/2024-02-ai-arena/blob/main/src/MergingPool.sol#L139-L167
- Fighter created by mintFromMergingPool can have arbitrary weight and element like 0 or 2**256 - 1
- Invalid weight and element could greatly affect AI Arena battles.
Proof of Concept
When someone claim their nft rewards from MergingPool, they can input customeAttributes and create fighters with arbitrary values since currently there is no check on customeAttributes and it could varies from 0 to 2**256 - 1 (type(uint256).max):
function claimRewards(
string[] calldata modelURIs,
string[] calldata modelTypes,
uint256[2][] calldata customAttributes
)
external
{
....
_fighterFarmInstance.mintFromMergingPool(
msg.sender,
modelURIs[claimIndex],
modelTypes[claimIndex],
customAttributes[claimIndex]
);
....
}
function mintFromMergingPool(
address to,
string calldata modelHash,
string calldata modelType,
uint256[2] calldata customAttributes
)
public
{
require(msg.sender == _mergingPoolAddress);
_createNewFighter(
to,
uint256(keccak256(abi.encode(msg.sender, fighters.length))),
modelHash,
modelType,
0,
0,
customAttributes
);
}
This allow creating fighters with element and weight range from 0 to 2**256 - 1 and can have negative impact on AI Arena matches according to the doc here https://docs.aiarena.io/gaming-competition/nft-makeup, for example:
- Weight is described in the doc as
used to calculate how far the fighter moves when being knocked back.. If an nft has extremely large weight like 2**256- 1, then it could never be knocked back - Element can only be one of Fire, Electricity or Water, an nft with element outside of this list could be created.
Below is a POC, save the test case to contract MergingPoolTest under file test/MergingPool.t.sol and run it using command: forge test --match-path test/MergingPool.t.sol --match-test testClaimRewardsArbitraryElementAndWeight -vvvv
function testClaimRewardsArbitraryElementAndWeight() public {
_mintFromMergingPool(_ownerAddress);
_mintFromMergingPool(_DELEGATED_ADDRESS);
assertEq(_fighterFarmContract.ownerOf(0), _ownerAddress);
assertEq(_fighterFarmContract.ownerOf(1), _DELEGATED_ADDRESS);
uint256[] memory _winners = new uint256[](2);
_winners[0] = 0;
_winners[1] = 1;
// winners of roundId 0 are picked
_mergingPoolContract.pickWinner(_winners);
assertEq(_mergingPoolContract.isSelectionComplete(0), true);
assertEq(_mergingPoolContract.winnerAddresses(0, 0) == _ownerAddress, true);
// winner matches ownerOf tokenId
assertEq(_mergingPoolContract.winnerAddresses(0, 1) == _DELEGATED_ADDRESS, true);
string[] memory _modelURIs = new string[](2);
_modelURIs[0] = "ipfs://bafybeiaatcgqvzvz3wrjiqmz2ivcu2c5sqxgipv5w2hzy4pdlw7hfox42m";
_modelURIs[1] = "ipfs://bafybeiaatcgqvzvz3wrjiqmz2ivcu2c5sqxgipv5w2hzy4pdlw7hfox42m";
string[] memory _modelTypes = new string[](2);
_modelTypes[0] = "original";
_modelTypes[1] = "original";
uint256[2][] memory _customAttributes = new uint256[2][](2);
_customAttributes[0][0] = uint256(0);
_customAttributes[0][1] = uint256(type(uint256).max);
_customAttributes[1][0] = uint256(type(uint256).max);
_customAttributes[1][1] = uint256(0);
// winners of roundId 1 are picked
_mergingPoolContract.pickWinner(_winners);
// winner claims rewards for previous roundIds
_mergingPoolContract.claimRewards(_modelURIs, _modelTypes, _customAttributes);
// Fighter 2 has 2**256 weight and element = 0
(, ,uint256 weight , uint256 element , , , ) = _fighterFarmContract.getAllFighterInfo(2);
assertEq(weight, 2**256 - 1);
assertEq(element, 0);
// Fighter 3 has 0 weight and 2**256 element
(, , weight , element , , , ) = _fighterFarmContract.getAllFighterInfo(3);
assertEq(weight, 0);
assertEq(element, 2**256 - 1);
}
Recommended Mitigation Steps
I recommend checking customAttributes in function mintFromMergingPool and only restrict weight and element in predefined ranges. For example, weight must be in range [65, 95], element must be in range [0,2].
brandinho (AI Arena) confirmed via duplicate issue #1670
https://github.com/ArenaX-Labs/2024-02-ai-arena-mitigation/pull/16
Status: Not fully mitigated. Full details in reports from fnanni and niser93 (1, 2), and also included in the Mitigation Review section below.
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 mintFromMergingPool function does not validate the customAttributes array before passing it to _createNewFighter, so weight and element can be any uint256 value.
Impact
A fighter could have an extremely large weight so it can never be knocked back, or an element outside the allowed set, which could distort AI Arena battles.
What the attacker needed
Be a winner of a merging pool round
The fix
The customAttributes should be validated in mintFromMergingPool, restricting weight to a predefined range such as 65 to 95 and element to 0 to 2.
How it was found
- Become a winner of a merging pool round so you can claim rewards.
- Call claimRewards with customAttributes containing arbitrary weight and element values.
- The contract mints a fighter with those unvalidated attributes.
- Observe that the fighter's weight and element match the arbitrary values.
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 game, players can earn new fighter NFTs by winning rounds. When they claim a reward, they provide two numbers that set the fighter's weight and element. The code never checks those numbers, so a player can set them to any value, including absurdly large ones, which can make the fighter behave unfairly in battles.
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.