Skip to content
Business logic Audit finding Intermediate

Single plot can be occupied by multiple renters

The LandManager contract's transferToUnoccupiedPlot function fails to update the plotId in the ToilerState struct when moving a staked token to a new plot. This allows a user to move their token while the contract still believes it is in the original plot, leading to incorrect plot occupancy and potential reward misattribution.

Bozho Follow with Bozho, ihtishamsudo and 48 others

Originally disclosed on Code4rena

Submitted by Bozho, also found by Bozho, Rhaydden (1, 2), ihtishamsudo, tedox, stuart_the_minion, dontonka, TECHFUND-inc, EPSec, araj, 0x3b, pipidu83, bearonbike, ironside, waydou, Joshuajee, Chad0, peanuts, iam_emptyset, gd, eta, Tomas0707, 0x04bytes, Shubham, stanchev, Flare, sandy, Fon, franfran20, dhank, vinica_boy, AvantGard, MrPotatoMagic, enckrish, prapandey031, Gosho, gkrastenov, Heaven, radev_sw, m4ttm, Fitro, corporalking, 0xjarix, phoenixV110, mojito_auditor, 0xCiphky, 0xrex, merlinboii, santipu_, dimulski, ke1caM (1, 2), biakia, Drynooo, Utsav, and d4r3d3v1l

The LandManager contract allows users to stake their munchables to a other user’s plots to earn schnibbles. There’s a functionality to transfer a staked token from one plot to another.

This functionality is flawed as it allows a user to transfer his token to another plot but doesn’t update the plotId field in the ToilerState struct. This means that the user can move his token but the contract still thinks it’s in the original plot.

    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
        });
        ...

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

Impact

If the amount of landlord’s plots is decreased and user’s token is inside a plot outside of the new range, the token won’t be set as dirty.

This can lead to a situation where a user can stake initially on the first plots but after that move his token to a plot that is in the end of the range. When the plots are decreased, the contract will still think the token is in the first plot and won’t set it as dirty and another user can stake his token in the same plot which breaks the intended functionality for LandManager.

Proof of Concept

Starting state:

    \=> `_getNumPlots(landlord)` = 4
    \=> `toilerState[tokenId].plotId` = 1
    \=> `plotOccupied[landlord][1].occupied` = true
    \=> `plotOccupied[landlord][3].occupied` = false

Bob transfers his token from plot 1 to plot 3:

    \=> `toilerState[tokenId].plotId` remains = 1
    \=> `plotOccupied[landlord][1].occupied` = false
    \=> `plotOccupied[landlord][3].occupied` = true

Landlord’s plots are decreased to 2:

    \=> `_getNumPlots(landlord)` = 2
    \=> `toilerState[tokenId].plotId` = 1
    \=> `plotOccupied[landlord][1].occupied` is still = false
    \=> `plotOccupied[landlord][3].occupied` is still = true

farmPlots() is called by Bob:

    \=> `toilerState[tokenId].plotId` = 1
    \=> The contract still thinks Bob's token is in plot 1, but it's actually in plot 3
    \=> Bob's token won't be set as dirty as `toilerState[tokenId].plotId` < `_getNumPlots(landlord)`
    \=> `plotOccupied[landlord][1].occupied` = false
    \=> Another user can stake his token in plot 1 as well

Coded proof of concept

Add the following test inside transferToUnoccupiedPlot.test.ts and run with pnpm test:typescript:

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");
});

Recommended Mitigation Steps

Update plotId inside toilerState when moving plots.

        toilerState[tokenId].latestTaxRate = plotMetadata[_toiler.landlord]
            .currentTaxRate;
+        toilerState[tokenId].plotId = plotId;
        plotOccupied[_toiler.landlord][oldPlotId] = Plot({
            occupied: false,
            tokenId: 0
        });

Assessed type

Context

0xinsanity (Munchables) confirmed via duplicate Issue #88

0xsomeone (judge) commented:

The Warden has correctly identified a flaw in the way data is maintained after moving into an unoccupied plot that would result in a plot remaining occupied after having been vacated, rewards to be attributed incorrectly, and unstaking operations to be processed improperly.

Given that rewards can be directly affected by the vulnerability and in line with the evaluation of #89 I believe this merits a high-risk severity rating.


The original report as published on Code4rena, by Bozho. 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

The transferToUnoccupiedPlot function does not update the plotId field in the toilerState mapping after moving a token to a new plot.

Impact

This could lead to incorrect reward distribution and allow multiple users to occupy the same plot, breaking the intended functionality of the LandManager contract.

What the attacker needed

User must have a staked token; Landlord's plots must be decreased

The fix

The fix is to update the plotId in toilerState when moving plots, as shown in the recommended mitigation steps.

How it was found

  1. Bob transfers his token from plot 1 to plot 3 using transferToUnoccupiedPlot.
  2. The contract updates plotOccupied to mark plot 1 as unoccupied and plot 3 as occupied, but toilerState[tokenId].plotId remains 1.
  3. The landlord's number of plots is decreased to 2, so plot 3 is now out of range.
  4. Bob calls farmPlots(), but the contract still thinks his token is in plot 1, so it does not mark the token as dirty.
  5. Another user can now stake their token in plot 1 because it is marked as unoccupied.

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 a blockchain game, players can stake their characters (munchables) on land plots to earn rewards. The contract tracks which plot each character is on, but when a player moves their character to a new plot, the contract forgets to update its record. This means the contract thinks the character is still on the old plot, so the old plot appears empty and can be taken by someone else, while the character is actually on the new plot.

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.

More from Munchables

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.