Lost rewards when the supply is `0`, which always happens if the rewards are queued before anyone has `StakeTracker` tokens
When the total supply of StakeTracker tokens is zero, the reward-per-token value does not increase but the last update block is still advanced, so rewards queued during that period are never distributed. An attacker or ordinary user could cause reward tokens to become stuck in the rewarder contracts, leading to lost rewards.
0x73696d616f Follow with chaduke, hassan-truscova and 2 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-06-tokemak-judging/issues/387
Found by
0x73696d616f, chaduke, hassan-truscova, lucifero, p0wd3r
If the supply of StakeTracker tokens is 0, the rewardPerTokenStored won't increase, but the lastUpdateBlock will, leading to lost rewards.
Vulnerability Detail
The rewards are destributed in a MasterChef style, which takes snapshots of the total accrued rewards over time and whenever someone wants to get the rewards, it subtracts the snapshot of the user from the most updated, global snapshot.
The rewardsPerToken() calculation factors the blocks passed times the reward rate by the totalSupply(), to get the reward per token in a specific interval (and then accrues to the previous intervals, as stated in the last paragraph). When the totalSupply() is 0, there is 0 rewardPerToken() increment as there is no supply to factor the rewards by.
The current solution is to maintain the same rewardsPerToken() if the totalSupply() is 0, but the lastUpdateBlock is still updated. This means that, during the interval in which the totalSupply() is 0, no rewards are destributed but the block numbers still move forward, leaving the tokens stuck in the MainRewarder and ExtraRewarder smart contracts.
This will always happen if the rewards are quewed before the totalSupply() is bigger than 0 (before an initial deposit to either DestinationVault or LMPVault). It might also happen if users withdraw all their tokens from the vaults, leading to a totalSupply() of 0, but this is very unlikely.
Impact
Lost reward tokens. The amount depends on the time during which the totalSupply() is 0, but could be significant.
Code Snippet
The rewardPerToken() calculation:
function rewardPerToken() public view returns (uint256) {
uint256 total = totalSupply();
if (total == 0) {
return rewardPerTokenStored;
}
return rewardPerTokenStored + ((lastBlockRewardApplicable() - lastUpdateBlock) * rewardRate * 1e18 / total);
}
The rewardPerTokenStored does not increment when the totalSupply() is 0.
Tool used
Vscode Foundry Manual Review
Recommendation
The totalSupply() should not realistically be 0 after the initial setup period (unless for some reason everyone decides to withdraw from the vaults, but this should be handled separately). It should be enough to only allow queueing rewards if the totalSupply() is bigger than 0. For this, only a new check needs to be added:
function queueNewRewards(uint256 newRewards) external onlyWhitelisted {
if (totalSupply() == 0) revert ZeroTotalSupply();
...
}
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
The reward calculation returns the stored reward-per-token unchanged when total supply is zero, but the last update block is still set to the current block, so the elapsed interval is never accounted for.
Impact
Reward tokens could be permanently lost, with the amount depending on how long the total supply remains zero, which could be significant.
What the attacker needed
Rewards queued before any stake; total supply of StakeTracker is zero
The fix
The contract should reject queuing new rewards when the total supply is zero, for example by reverting if totalSupply() equals zero.
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.
A staking contract hands out rewards over time based on how many tokens are staked. If nobody has staked anything yet, the contract still moves its internal clock forward, so any rewards queued during that empty period are skipped and stay stuck in the contract.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.