Skip to content
DoS Audit finding Intermediate

in farmPlots() an underflow in edge case leading to freeze of funds (NFT)

A user's staked plot can become invalid when the price per plot is increased, causing a timestamp underflow in the farming calculation that reverts the transaction. This prevents the user from farming or unstaking, effectively freezing their funds.

BugPull Follow with typicalHuman, artilugio0 and 16 others

Originally disclosed on Code4rena

Submitted by BugPull, also found by typicalHuman, artilugio0, Bozho, turvy_fuzz, vinica_boy, MrPotatoMagic, matejdb, 0xCiphky, merlinboii, shaflow2, 0x3b, gajiknownnothing, 0x_6a70, DPS, franfran20, 0rpse, forgebyola, and Phoenix_x

https://github.com/code-423n4/2024-07-munchables/blob/94cf468aaabf526b7a8319f7eba34014ccebe7b9/src/managers/LandManager.sol#L232-L310 https://github.com/code-423n4/2024-07-munchables/blob/94cf468aaabf526b7a8319f7eba34014ccebe7b9/src/managers/LandManager.sol#L162-L168

Impact

The user won’t be able to farm their plots or unstake due to the txn always panic reverting. Leading to loss of funds and DOS.

Proof of Concept

The problem arises whenever PRICE_PER_PLOT gets increased through configUpdated. Whenever it gets increased, then _getNumPlots will return less numbers for that LandLord, because its denominated by PRICE_PER_PLOT.

File: LandManager.sol
344:     function _getNumPlots(address _account) internal view returns (uint256) {
345:         return lockManager.getLockedWeightedValue(_account) / PRICE_PER_PLOT;
346:     }

For example:

  • A user is staked in plotId 100, and the LandLord has 200 Plots.
  • PRICE_PER_PLOT gets increased and makes the Plots of that Landlord to be 90.
  • Now the user is staked to a Plot that is considered invalid and should be flagged as dirty of his toilerState[tokenId] struct.

Now, the problem here is that NumPlots is not decreased due to LandLord unlocking funds but instead due to PRICE_PER_PLOT getting increased -> meaning that plotMetadata[landlord].lastUpdated is too old.

In _farmPlots we see:

File: LandManager.sol
232:     function _farmPlots(address _sender) internal {
//////////.............OmmitCode
258:             if (_getNumPlots(landlord) < _toiler.plotId) {
259:                 timestamp = plotMetadata[landlord].lastUpdated;
260:                 toilerState[tokenId].dirty = true;
261:             }
//////////.............OmmitCode
280:             schnibblesTotal =
281:                 (timestamp - _toiler.lastToilDate) *
282:                 BASE_SCHNIBBLE_RATE;
//////////.............OmmitCode
309:         accountManager.updatePlayer(mainAccount, renterMetadata);
310:     }

In Line 258, we check for NumPlots if it’s lower than plotId of the user we then assign timeStamp variable to landlord.lastUpdated which is too old timeStamp (the last time the user locked funds).

Then in Line 281, we subtract timestamp (which is now landlord.lastUpdated which is too old timeStamp) from _toiler.lastToilDate which is larger than timestamp that will lead to panic revert.

Let’s see why lastToilDate is larger:

We set this variable in stakeMunchable to block.timestamp here:

File: LandManager.sol
162:         toilerState[tokenId] = ToilerState({
163:             lastToilDate: block.timestamp,
164:             plotId: plotId,
165:             landlord: landlord,
166:             latestTaxRate: plotMetadata[landlord].currentTaxRate,
167:             dirty: false
168:         });

The landlord.lastUpdated is only updated when he lock or unlock funds (which in our case, didn’t lock or unlock any funds before the user stake to him).

Recommended Mitigation Steps

To avoid this case from happening, when PRICE_PER_PLOT gets increased we should check if landlord.lastUpdated is > than _toiler.lastToilDate inside the block (of this case if (_getNumPlots(landlord) < _toiler.plotId)), so that we can differentiate from cases that LandLord unlocked funds from cases where PRICE_PER_PLOT got increased and the LandLord didn’t do anything with his funds for a while.

Assessed type

Under/Overflow

0xinsanity (Munchables) confirmed

0xsomeone (judge) commented:

The Warden and its duplicates have correctly identified that a plot becoming inactive due to natural price movements and/or configuration adjustments of the PRICE_PER_PLOT value may result in a timestamp-based underflow occurring as the renter of a plot might be more active than its landlord.

I believe that normally a medium severity rating would be appropriate due to the economic incentives that surround the situation. Specifically, it is in the best interest of a landlord to “unlock” the NFTs they hold to permit them to harvest rewards and thus pay rent. As such, it is highly likely that in a scenario such as the one described the landlord would promptly address the issue.

Given that a Munchable becoming deadlocked is a documented invariant of the audit, I am inclined to award this with a high-severity rating. A penalty of 75% has been applied to any submission that detailed an inadequate or incorrect alleviation.


The original report as published on Code4rena, by BugPull. 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 code uses the landlord's last update time instead of the user's last toil time when a plot becomes invalid due to a price change, leading to an underflow in the reward calculation.

What the attacker needed

User staked in a plot; PRICE_PER_PLOT increased; landlord's lastUpdated older than user's lastToilDate

The fix

The code should check if the landlord's lastUpdated is greater than the user's lastToilDate before using it as the timestamp, to differentiate between price changes and landlord unlocking.

How it was found

  1. A user stakes in a plot with a landlord who has many plots.
  2. The price per plot is increased, reducing the number of plots the landlord has.
  3. The user's plot becomes invalid and is marked dirty.
  4. When the user tries to farm, the code sets timestamp to the landlord's lastUpdated, which is older than the user's lastToilDate.
  5. The subtraction in the reward calculation underflows, causing a revert.

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 a user stakes an NFT into a plot, the system records the time. Later, if the cost per plot goes up, the number of plots a landlord owns can drop, making the user's plot invalid. The code then uses an old timestamp from the landlord instead of the user's last activity time, causing a subtraction that goes negative and crashes the transaction, so the user cannot withdraw their NFT.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by BugPull

View all

4 more disclosed reports, best first.

More from Munchables

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.