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.
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).
_mintis called, calls_updateRewardsPerTokenwhich short-circuits.rewardsPerToken.lastUpdatedis still set torewardsPeriod.startfrom the constructor. Then_updateUserRewardsis called and does not currently yield any rewards. (because both balance and the index diff are zero). User has now minted the tokens,totalSupplyincreases and user balance is set.- User performs a
claim:_updateRewardsPerTokenis called andtimeSinceLastUpdated = 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.
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_.accumulatedmath 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
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
- Ensure the rewards period is active and totalSupply is zero.
- Mint the first tokens, which calls _updateRewardsPerToken but it short-circuits without updating lastUpdated.
- Call claim, which triggers _updateRewardsPerToken and calculates rewards for the entire period since rewardsPeriod.start.
- 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.