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

When PRICE_PER_PLOT rises, a staked plot becomes invalid; farmPlots sets timestamp to stale landlord.lastUpdated, underflowing against lastToilDate and reverting, freezing the NFT.

- Payout: Audit contest
- Severity: high
- Program: Munchables
- Found by: BugPull
- Disclosed: 2024-08-16
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-07-munchables-findings/issues/76
- Library page: https://bugbountylibrary.com/r/c4%3A2024-07-munchables%3AH-04
- Tags: area:web3 domain:web3 impact:dos impact:funds-loss kind:contest platform:code4rena sev:high tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:dos

**Target:** LandManager.farmPlots / _farmPlots staking contract
**Root cause:** _getNumPlots divides locked value by PRICE_PER_PLOT, so a config increase shrinks a landlord's plot count and marks renters' plots dirty; the dirty branch sets timestamp = plotMetadata[landlord].lastUpdated, which can predate the renter's lastToilDate, causing (timestamp - lastToilDate) to underflow.
**Steps:**
1. Landlord has 200 plots; user stakes NFT into plotId 100, setting toilerState.lastToilDate = block.timestamp.
2. Admin calls configUpdated raising PRICE_PER_PLOT so _getNumPlots(landlord) drops to 90.
3. User calls farmPlots; _getNumPlots(landlord)=90 < toiler.plotId=100, so timestamp = plotMetadata[landlord].lastUpdated (stale) and dirty=true.
4. Compute schnibblesTotal = (timestamp - _toiler.lastToilDate) * BASE_SCHNIBBLE_RATE; lastToilDate > timestamp, so the subtraction panics and reverts.
5. User can no longer farm or unstake; NFT is frozen.
**How found:** Reading _getNumPlots and noticing plot count is derived from PRICE_PER_PLOT, then tracing the dirty-plot branch in _farmPlots and comparing the two timestamps (landlord.lastUpdated vs toiler.lastToilDate) to spot the underflow.
**Exploitation:** No attacker profit path; the flaw is triggered by legitimate admin config change plus normal user farming, permanently reverting farmPlots and unstake for affected renters, locking their staked NFT.
**Insight:** When a validity check compares a derived count against a stored index, audit every timestamp the invalid-branch substitutes in: config-driven invalidation can make 'last updated' older than per-user state, breaking subtraction assumptions that hold for the normal unlock path.

## Proof of concept (copied exactly from the original report)

**_getNumPlots divides by PRICE_PER_PLOT**

```
function _getNumPlots(address _account) internal view returns (uint256) {
345:         return lockManager.getLockedWeightedValue(_account) / PRICE_PER_PLOT;
```

