# Single plot can be occupied by multiple renters

transferToUnoccupiedPlot fails to update toilerState.plotId, so a moved token's stale plotId lets another renter occupy the vacated plot, corrupting rewards and unstaking.

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

**Target:** LandManager.sol transferToUnoccupiedPlot (Solidity/EVM staking contract)
**Root cause:** transferToUnoccupiedPlot clears plotOccupied for the old plot and marks the new one occupied but never writes toilerState[tokenId].plotId = plotId, leaving the token's recorded plot stale.
**Steps:**
1. Set landlord with 4 plots; stake tokenId in plot 1 (toilerState.plotId=1, plotOccupied[1]=true).
2. Call `transferToUnoccupiedPlot(tokenId, 3)`; plotOccupied[1]=false, plotOccupied[3]=true, but toilerState.plotId stays 1.
3. Landlord reduces plots to 2 via plot reduction.
4. Call `farmPlots()`; token's stale plotId=1 < numPlots so it is not marked dirty.
5. Another user stakes into plot 1 since plotOccupied[1]=false, double-occupying.
**How found:** Code reading of transferToUnoccupiedPlot revealed the plotOccupied bookkeeping updated but the toilerState.plotId field left untouched, then traced interaction with plot reduction and farmPlots dirty logic.
**Exploitation:** Attacker moves token to a high-index plot, waits for landlord plot reduction, then the stale low plotId prevents dirty-marking and frees plot 1 for a second renter, misattributing schnibble rewards and breaking unstaking accounting.
**Insight:** When a function moves an entity between slots, check every parallel mapping/struct field (occupancy map vs. per-token state) is updated consistently; stale index fields surface only when a later range-shrink or dirty-check compares against them.

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

**TypeScript test showing plotId unchanged after transfer**

```
it("successful path with logging plotId", async () => {
  //@audit-note Log plotId before transfer
  const beforeState =
    await testContracts.landManagerProxy.contract.read.getToilerState([1]);
  console.log("PlotId before transfer: " + beforeState.plotId);

  const { request } =
    await testContracts.landManagerProxy.contract.simulate.transferToUnoccupiedPlot(
      [1, 10],
      {
        account: bob,
      },
    );
  const txHash = await testClient.writeContract(request);
  await assertTxSuccess({ txHash: txHash });

  //@audit-note Log plotId after transfer
  const afterState =
    await testContracts.landManagerProxy.contract.read.getToilerState([1]);
  console.log("PlotId after transfer: " + afterState.plotId);

  //@audit-note Assert plotId hasn't changed
  assert.equal(beforeState.plotId, afterState.plotId, "PlotId did not change");
});
```

**Vulnerable transferToUnoccupiedPlot missing plotId update**

```
function transferToUnoccupiedPlot(
        uint256 tokenId,
        uint256 plotId
    ) external override forceFarmPlots(msg.sender) notPaused {
        (address mainAccount, ) = _getMainAccountRequireRegistered(msg.sender);
        ToilerState memory _toiler = toilerState[tokenId];
        uint256 oldPlotId = _toiler.plotId; // @audit plotId isn't update inside toilerState
        uint256 totalPlotsAvail = _getNumPlots(_toiler.landlord);
        if (_toiler.landlord == address(0)) revert NotStakedError();
        if (munchableOwner[tokenId] != mainAccount) revert InvalidOwnerError();
        if (plotOccupied[_toiler.landlord][plotId].occupied)
            revert OccupiedPlotError(_toiler.landlord, plotId);
        if (plotId >= totalPlotsAvail) revert PlotTooHighError();

        toilerState[tokenId].latestTaxRate = plotMetadata[_toiler.landlord]
            .currentTaxRate;
        ❌@>    // @audit missing toilerState[tokenId].plotId = plotId;
        plotOccupied[_toiler.landlord][oldPlotId] = Plot({
            occupied: false,
            tokenId: 0
        });
```

