Skip to content
Business logic Audit finding Intermediate

Players can gain more NFTs benefiting from that past remainder in subsequent locks

The lock manager contract fails to reset the remainder of locked tokens when a player unlocks, allowing the remainder to carry over to future locks. An attacker can exploit this to receive more NFTs than deserved by strategically locking and unlocking tokens.

merlinboii Follow with 0xhacksmithh, dhank and 14 others

Originally disclosed on Code4rena

Submitted by merlinboii, also found by 0xhacksmithh, dhank, Utsav, Stormreckson, rouhsamad, sandy, AvantGard, araj, SovaSlava, Holipot, DPS, Audinarey, Haipls, Varun_05, shaflow2, and Limbooo

Lines of code

https://github.com/code-423n4/2024-05-munchables/blob/main/src/managers/LockManager.sol#L311-L398 https://github.com/code-423n4/2024-05-munchables/blob/main/src/managers/LockManager.sol#L401-L427

Description

The remaining locked tokens continue to accumulate after the player unlocks their tokens, whether partially or fully.

As a result, the player benefits by retrieving more NFTs when they lock their tokens again during the lock drop period or in the next lock drop period.

Location: LockManager::_lock()L344 - L380

    uint256 quantity = _quantity + lockedToken.remainder;
    uint256 remainder;
    uint256 numberNFTs;
    uint32 _lockDuration = playerSettings[_lockRecipient].lockDuration;

    // SNIPPED

    if (
        lockdrop.start <= uint32(block.timestamp) &&
        lockdrop.end >= uint32(block.timestamp)
    ) {
        if (
            _lockDuration < lockdrop.minLockDuration ||
            _lockDuration >
            uint32(configStorage.getUint(StorageKey.MaxLockDuration))
        ) revert InvalidLockDurationError();
        if (msg.sender != address(migrationManager)) {
            // calculate number of nfts
-->         remainder = quantity % configuredToken.nftCost;
            numberNFTs = (quantity - remainder) / configuredToken.nftCost;

            if (numberNFTs > type(uint16).max) revert TooManyNFTsError();

            // Tell nftOverlord that the player has new unopened Munchables
            nftOverlord.addReveal(_lockRecipient, uint16(numberNFTs));
        }
    }

  // SNIPPED

->  lockedToken.remainder = remainder;
    lockedToken.quantity += _quantity;

Location: LockManager::unlock()L401 - L427

    function unlock(
        address _tokenContract,
        uint256 _quantity
    ) external notPaused nonReentrant {
        LockedToken storage lockedToken = lockedTokens[msg.sender][
            _tokenContract
        ];
        if (lockedToken.quantity < _quantity)
            revert InsufficientLockAmountError();
        if (lockedToken.unlockTime > uint32(block.timestamp))
            revert TokenStillLockedError();

        // force harvest to make sure that they get the schnibbles that they are entitled to
        accountManager.forceHarvest(msg.sender);

        lockedToken.quantity -= _quantity;

        // send token
        if (_tokenContract == address(0)) {
            payable(msg.sender).transfer(_quantity);
        } else {
            IERC20 token = IERC20(_tokenContract);
            token.transfer(msg.sender, _quantity);
        }

        emit Unlocked(msg.sender, _tokenContract, _quantity);
    }

Impact

During the lock drop period or in the subsequent lock drop period, the player retrieves more NFTs in proportion to the quantity locked. The remaining tokens from previous locks enhance the new locking quantity when relocked until the end of the lock period.

Moreover, it probably assumes that the lockDuration can either be equal or greater than the lock drop duration (lockDuration >= lockdrop.start - lockdrop.end), allowing players to reenter by each lock drop, or significantly less than the lock drop duration (lockDuration < lockdrop.start - lockdrop.end), enabling players to reenter during the same lockdrop.

Therefore, the likelihood of the issue is based on how offset players can reenter and take advantage of the remainders.

The following core functions take an effect of this issue:

Proof of Concept

Prerequisite

  • The LockManager contract has zero balance.

  • The actions taken during the any active lock drop period.

  • Using the address(0) as a token contract represents locking the native token

    • _tokenContract = address(0)
  • Assuming the configuration of nftCostof the address(0) is 3 ethers (3e18)

    • configuredTokens[_tokenContract: address(0)] = 3e18

Partially Unlocking Case

  1. Lock 10e18 (_quantity) tokens

as uint256 quantity = _quantity + lockedToken.remainder :: L344

The state updates as follows:

  • quantity = 10e18(_quantity) + 0 (lockedToken.remainder) = 10e18
  • remainder = 10e18 % 3e18 = 1e18
  • numberNFTs = (10e18 - 1e18) / 3e18 = 3

Updates to lockedToken struct and LockManager contract balance:

  • lockedToken.remainder = remainder = 1e18
  • lockedToken.quantity += _quantity = 0 + 10e18 = 10e18
  • address(LockManager).balance = 10e18

This shows that locking of 10e18 tokens can retrieve 3 NFTs.

Location: LockManager.sol::_lock()

    // add remainder from any previous lock
--> L344:   uint256 quantity = _quantity + lockedToken.remainder;
            uint256 remainder;
            uint256 numberNFTs;
            uint32 _lockDuration = playerSettings[_lockRecipient].lockDuration;

    // SNIPPED

    if (
        lockdrop.start <= uint32(block.timestamp) &&
        lockdrop.end >= uint32(block.timestamp)
    ) {
        if (
            _lockDuration < lockdrop.minLockDuration ||
            _lockDuration >
            uint32(configStorage.getUint(StorageKey.MaxLockDuration))
        ) revert InvalidLockDurationError();
        if (msg.sender != address(migrationManager)) {
            // calculate number of nfts
--> L363:   remainder = quantity % configuredToken.nftCost;
            numberNFTs = (quantity - remainder) / configuredToken.nftCost;

            if (numberNFTs > type(uint16).max) revert TooManyNFTsError();

            // Tell nftOverlord that the player has new unopened Munchables
            nftOverlord.addReveal(_lockRecipient, uint16(numberNFTs));
        }
    }

  // SNIPPED

-> L379 lockedToken.remainder = remainder;
        lockedToken.quantity += _quantity;

After the lockDuration has passed, the player can unlock the locked tokens.

  1. Unlock 5e18 tokens partially:
  2. L416::lockedToken.quantity -= _quantity = 10e18 - 5e18 = 5e18
  3. address(LockManager).balance = 5e18
  4. Lock 5e18 tokens back again

as uint256 quantity = _quantity + lockedToken.remainder (prev remainder#1);

  • quantity = 5e18(_quantity) + 1e18 (lockedToken.remainder) = 6e18
  • remainder = 6e18 % 3e18 = 0
  • numberNFTs = (6e18 - 0) / 3e18 = 2

Updates to lockedToken struct and LockManager contract balance:

  • lockedToken.remainder = remainder = 0
  • lockedToken.quantity += _quantity = 5e18 + 5e18 = 10e18
  • address(LockManager).balance = 10e18

In total, the player retrieves 5 NFTs (3 from the first locking + 2 from the second locking) by locking 10e18 tokens, benefiting from the remainder of the previous locking.

Fully Unlocking Case

  1. Lock 13e18 (_quantity) tokens

as uint256 quantity = _quantity + lockedToken.remainder :: L344

The state updates as follows:

  • quantity = 13e18(_quantity) + 0 (lockedToken.remainder) = 10e18
  • remainder = 13e18 % 3e18 = 1e18
  • numberNFTs = (13e18 - 1e18) / 3e18 = 4 πŸ“

Updates to lockedToken struct and LockManager contract balance:

  • lockedToken.remainder = remainder = 1e18 πŸ“
  • lockedToken.quantity += _quantity = 0 + 13e18 = 13e18 πŸ“
  • address(LockManager).balance = 13e18 πŸ“

This shows that locking of 13e18 tokens can retrieve 4 NFTs.

After the lockDuration has passed, the player can unlock the locked tokens.

  1. Unlock 13e18 tokens fully:
  2. L416::lockedToken.quantity -= _quantity = 13e18 - 13e18 = 0 πŸ“
  3. address(LockManager).balance = 0 πŸ“
  4. Lock 11e18 tokens

as uint256 quantity = _quantity + lockedToken.remainder (prev remainder#1);

  • quantity = 11e18(_quantity) + 1e18 (lockedToken.remainder) = 12e18
  • remainder = 12e18 % 3e18 = 0
  • numberNFTs = (12e18 - 0) / 3e18 = 4 πŸ“

Updates to lockedToken and LockManager contract balance:

  • lockedToken.remainder = remainder = 0 πŸ“
  • lockedToken.quantity += _quantity = 0 + 11e18 = 11e18 πŸ“
  • address(LockManager).balance = 11e18 πŸ“

In total, the player retrieves 4 new NFTs from locking 11e18 tokens. Compared to the first locking of 13e18 tokens, the second lock yields similar power to gain 4 NFTs, benefiting from the remainder of the previous locking round.

Recommended Mitigation Steps

Manage the remaining tokens when the player unlocks locked tokens. Alternatively, use other methods to handling the total amount of NFTs and locked quantity during the lock drop.

Assessed type

Math

0xinsanity (Munchables) confirmed and commented:

Fixed

0xsomeone (judge) commented:

The Warden outlines a situation whereby the remainder of an account will not be properly maintained when unlocking a lock, permitting them to effectively carry over remainders that should otherwise not be accounted for.

While the vulnerability is present, its actual risk level is medium as the amounts involved would be insignificant and it would also imply that a user fully vested a minimum lock, and took advantage of the carried-over remainder in a second lock in the same β€œlockdrop”.

I selected this submission as the best given that it outlines the issue using a consumable step-by-step case. I would like to note that the mitigation cited by the Sponsor above is insufficient and the mitigation followed in #41 is the correct one.

Note: For full discussion, see here.


The original report as published on Code4rena, by merlinboii. 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 unlock function does not reset the remainder stored in the lockedToken struct, so it persists and is added to future lock quantities.

Impact

Players could gain more NFTs than they should, but the amounts are likely small and require specific conditions.

What the attacker needed

Active lock drop period; LockManager contract has zero balance; Using native token (address(0)); nftCost configured (e.g., 3e18)

The fix

The unlock function should reset the remainder when tokens are unlocked, or the remainder should be adjusted proportionally.

How it was found

  1. Lock 10e18 tokens, which yields 3 NFTs and leaves a remainder of 1e18.
  2. After the lock duration, unlock 5e18 tokens partially.
  3. Lock 5e18 tokens again; the previous remainder of 1e18 is added, making the quantity 6e18, which yields 2 NFTs.
  4. Total NFTs received: 5 for locking 10e18 tokens, instead of the expected 3.

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 lock tokens in this system, you earn NFTs based on how many tokens you lock. The system keeps track of any leftover tokens that don't add up to a full NFT (called a remainder). The bug is that when you unlock your tokens, this remainder is not cleared, so if you lock again later, the old remainder is added to your new lock, giving you extra NFTs you shouldn't get.

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 by merlinboii

View all

9 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.

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.