Miscalculation in _farmPlots function could lead to a user unable to unstake all NFTs
A miscalculation in the _farmPlots function causes schnibblesTotal to become negative when finalBonus is negative, leading to an overflow that reverts the transaction. This blocks unstaking of NFTs that have been staked in certain realms, potentially locking them permanently.
KaligoAudits Follow with Topmark, Rhaydden and 28 others
Originally disclosed on Code4rena
Submitted by KaligoAudits, also found by Topmark, Rhaydden, cheatc0d3, stuart_the_minion, K42, 0x3b, Trooper, rudhra, artilugio0, ironside, Nexarion, waydou, gd, robertodf99, franfran20, King_, Maroutis, yaioxy, TPSec, MrPotatoMagic, BugPull, prapandey031, 0xrex, merlinboii, santipu_, shaflow2, dimulski, 0xb0k0, Flare, and Tamer
The function _farmPlots() is used to calculate rewards farming rewards. This function is used in different scenarios, including like a modifier in the function unstakeMunchable() function when a user unstakes their NFT. Let’s have a look at how finalBonus is calculated in the function:
finalBonus =
int16(
REALM_BONUSES[
(uint256(immutableAttributes.realm) * 5) +
uint256(landlordMetadata.snuggeryRealm)
]
) +
int16(
int8(RARITY_BONUSES[uint256(immutableAttributes.rarity)])
);
The finalBonus consists of bonus from the realm (REALM_BONUSES), as well as a rarity bonus (RARITY_BONUSES).
REALM_BONUSES can be either -10, -5, 0, 5 or 10, and RARITY_BONUSES can be either 0, 10, 20, 30 or 50.
finalBonus is meant to incentivize staking Munchables in a plot in a suitable realm. It can either be a positive bonus or reduction in the amount of schnibblesTotal. An example of a negative scenario is when REALM_BONUSES is -10 and RARITY_BONUSES is 0.
Let’s look at the calculation of schnibblesTotal:
schnibblesTotal =
(timestamp - _toiler.lastToilDate) *
BASE_SCHNIBBLE_RATE;
schnibblesTotal = uint256(
(int256(schnibblesTotal) +
(int256(schnibblesTotal) * finalBonus)) / 100
);
schnibblesTotal is typed as uint256; therefore, it is meant as always positive. The current calculation, however, will result in a negative value of schnibblesTotal, when finalBonus < 0, the conversion to uint256 will cause the value to evaluate to something near type(uint256).max. In that case the next calculation of schnibblesLandlord will revert due to overflow:
schnibblesLandlord =
(schnibblesTotal * _toiler.latestTaxRate) /
1e18;
This is due to the fact that _toiler.latestTaxRate > 1e16.
Since unstakeMunchable() has a modifier forceFarmPlots(), the unstaking will be blocked if _farmPlots() reverts.
Scenario
- Alice has a Common or Primordial Munchable NFT from Everfrost.
- Bob has land (plots) in Drench.
- Alice stakes her NFT in Bob’s plot.
- After some time Alice unstakes her NFT.
- Since in the calculation of schnibblesTotal is used a negative
finalBonus, the transaction will revert. - Alice’s Munchable NFT and any other staked beforehand are blocked.
Proof of Concept
Add the following lines of code in tests/managers/LandManager/unstakeMunchable.test.ts:
.
.
.
await registerPlayer({
account: alice,
+ realm: 1,
testContracts,
});
await registerPlayer({
account: bob,
testContracts,
});
await registerPlayer({
account: jirard,
testContracts,
});
.
.
.
await mockNFTOverlord.write.addReveal([alice, 100], { account: alice });
await mockNFTOverlord.write.addReveal([bob, 100], { account: bob });
await mockNFTOverlord.write.addReveal([bob, 100], { account: bob });
await mockNFTOverlord.write.startReveal([bob], { account: bob }); // 1
- await mockNFTOverlord.write.reveal([bob, 4, 12], { account: bob }); // 1
+ await mockNFTOverlord.write.reveal([bob, 1, 0], { account: bob }); // 1
await mockNFTOverlord.write.startReveal([bob], { account: bob }); // 2
await mockNFTOverlord.write.reveal([bob, 0, 13], { account: bob }); // 2
await mockNFTOverlord.write.startReveal([bob], { account: bob }); // 3
When we run the command pnpm test:typescript the logs from the tests show that the successful path test reverts.
Impact
A user’s Munchable NFT with specific attributes can be stuck in the protocol.
Taking into account the rarity distribution across the different NFT-s and assuming that the distribution across realms is equal, the chance of this problem occurring is around 26% or 1 in every 4 staked munchables.
Further, this finding blocks unstaking every munchable that has previously been staked.
Recommended Mitigation
finalBonus is meant as a percentage change in the Schnibbles earned by a Munchable. Change the calculation of schnibblesTotal in _farmPlots() to reflect that by removing the brackets:
schnibblesTotal =
(timestamp - _toiler.lastToilDate) *
BASE_SCHNIBBLE_RATE;
-schnibblesTotal = uint256(
- (int256(schnibblesTotal) +
- (int256(schnibblesTotal) * finalBonus)) / 100
- );
+schnibblesTotal = uint256(
+ int256(schnibblesTotal) +
+ (int256(schnibblesTotal) * finalBonus) / 100
+ );
Assessed type
Math
0xinsanity (Munchables) confirmed
The Warden and its duplicates have outlined a significant issue in the way the
finalBonusis added or subtracted from the total as the order of operations is incorrect.I believe a high-risk severity rating is appropriate given that rewards are significantly impacted and it may ultimately be possible for a Munchable to be permanently locked in the system.
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 order of operations in the schnibblesTotal calculation: the division by 100 is applied after adding the bonus, so a negative finalBonus can make the whole expression negative before conversion to uint256.
Impact
A user's Munchable NFT with specific attributes can be stuck in the protocol, and this finding blocks unstaking every munchable that has previously been staked.
What the attacker needed
Alice has a Common or Primordial Munchable NFT from…; Bob has land (plots) in Drench; Alice stakes her NFT in Bob's plot
The fix
The calculation of schnibblesTotal should be changed to apply the division by 100 only to the bonus part, so that the total remains positive.
How it was found
- Alice registers with a realm that gives a negative realm bonus (e.g., realm 1).
- Bob registers and stakes land in a different realm.
- Alice stakes her NFT in Bob's plot.
- After some time, Alice tries to unstake her NFT.
- The transaction reverts because schnibblesTotal becomes negative and overflows when converted 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.
When you stake an NFT, the system calculates rewards. A bonus or penalty is applied based on the land and NFT rarity. If the penalty is negative, the reward calculation goes wrong and becomes a huge number, causing the unstaking transaction to fail. This means you cannot get your NFT back.
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.