Skip to content
Smart contracts Audit finding Intermediate

ERC20Rewards returns wrong rewards if no tokens initially exist

The ERC20Rewards contract fails to update the lastUpdated timestamp when totalSupply is zero, causing the first minter to receive all rewards accumulated since the rewards period started. An attacker could exploit this by minting the first token and claiming the entire reward pool.

cmichel Follow

Originally disclosed on Code4rena

Submitted by cmichel

The ERC20Rewards._updateRewardsPerToken function exits without updating rewardsPerToken_.lastUpdated if totalSupply is zero, i.e., if there are no tokens initially.

This leads to an error if there is an active rewards period but no tokens have been minted yet.

Example: rewardsPeriod.start: 1 month ago, rewardsPeriod.end: in 1 month, totalSupply == 0.

The first mint leads to the user (mintee) receiving all rewards for the past period (50% of the total rewards in this case).

  • _mint is called, calls _updateRewardsPerToken which short-circuits. rewardsPerToken.lastUpdated is still set to rewardsPeriod.start from the constructor. Then _updateUserRewards is called and does not currently yield any rewards. (because both balance and the index diff are zero). User has now minted the tokens, totalSupply increases and user balance is set.
  • User performs a claim: _updateRewardsPerToken is called and timeSinceLastUpdated = end - rewardsPerToken_.lastUpdated = block.timestamp - rewardsPeriod.start = 1 month. Contract “issues” rewards for the past month. The first mintee receives all of it.

The first mintee receives all pending rewards when they should not receive any past rewards. This can easily happen if the token is new, the reward period has already been initialized and is running, but the protocol has not officially launched yet. Note that setRewards also allows setting a date in the past which would also be fatal in this case.

Recommend that the rewardsPerToken_.lastUpdated field must always be updated in _updateRewardsPerToken to the current time (or end) even if _totalSupply == 0. Don’t return early.

alcueca (Yield) confirmed:

You are right, that’s a great finding. For the record, I think that this is what this line in Unipool.sol does:

function rewardPerToken() public view returns (uint256) {
  if (totalSupply() == 0) {
    return rewardPerTokenStored;
  }

I’ll apply the mitigation step suggested, with a conditional to not do the rewardsPerToken_.accumulated math that would revert.

Now I know the feeling of the devs that fork a known project and leave a pesky conditional out, thanks again :D

alcueca (Yield) patched:

Fix

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

Likely, the _updateRewardsPerToken function returns early without updating rewardsPerToken_.lastUpdated when totalSupply is zero, so the timestamp remains at the rewards period start.

Impact

The first minter could receive all rewards accumulated since the start of the rewards period, even though they should not receive any past rewards.

What the attacker needed

Rewards period active; No tokens minted yet; Attacker mints first tokens

The fix

The rewardsPerToken_.lastUpdated field should always be updated in _updateRewardsPerToken to the current time (or end) even if _totalSupply == 0, and the function should not return early.

How it was found

  1. Ensure the rewards period is active and totalSupply is zero.
  2. Mint the first tokens, which calls _updateRewardsPerToken but it short-circuits without updating lastUpdated.
  3. Call claim, which triggers _updateRewardsPerToken and calculates rewards for the entire period since rewardsPeriod.start.
  4. The first minter receives all pending rewards.

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.

This is a smart contract bug where a reward system incorrectly gives all past rewards to the first person who gets tokens. Normally, rewards should only be earned after you have tokens, but here the system forgets to reset a timer when there are no tokens, so the first user gets credit for time they didn't participate.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by cmichel

View all

196 more disclosed reports, best first.

More from Yield Witch v2 contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.