Updating a pool’s total points doesn’t affect existing stake positions for rewards calculation
The staking contract calculates rewards using the current total points of the pool, not the total points at the time each stake was made. This means that when new stakes increase total points, existing stakers earn fewer rewards, and when stakes are withdrawn, remaining stakers earn more; an attacker can also deliberately claim rewards for another user at a moment when total points are high to reduce that user's rewards.
Submitted by adriro, also found by kutugu, joestakey, Madalad, rbserver, minhquanym, minhquanym, auditor0517, sinarette, ast3ros, ABA, Haipls, J4de, and Dug
Staking rewards are calculated based on the user’s share of total points in the corresponding asset pool, this is the sum of the points associated to the staker’s positions divided by the total points from all positions in the pool. We can see this calculation in the getPoolReward function:
NeoTokyoStaker.sol#L1386-L1393
// Return final shares.
unchecked {
uint256 share = points * _PRECISION / pool.totalPoints * totalReward;
uint256 daoShare = share * pool.daoTax / (100 * _DIVISOR);
share /= _PRECISION;
daoShare /= _PRECISION;
return ((share - daoShare), daoShare);
}
However, note that pool.totalPoints is the current value of the pool’s total point at the time the function getPoolReward is called. It isn’t related to the time the user staked their position, or isn’t affected in any way by other stake/unstake actions from potentially other users.
This means that any action that modifies the pool’s total points (stake or unstake) won’t affect current staking positions, as previously opened staking positions won’t accrue their rewards correctly. For stake actions, it will cause rewards from existing staking positions to be reduced, as their calculation of the shares now divided by a higher pool.totalPoints value. From unstake actions, it will cause rewards from existing staking positions to be incorrectly increased, as the calculation of the shares is now divided by a lower pool.totalPoints value. See section “Proof of Concept” for a more detailed walkthrough.
In a similar way, this could also be used by a griefer to intentionally harm another user. As the getReward function present in the BYTES2 contract is permissionless (anyone can call this on behalf of an arbitrary account), a bad actor can call this when the pool’s total points is high, which will have the effect of reducing the user rewards.
Proof of Concept
Let’s assume the pool is empty. Alice stakes at t1 an asset worth 100 points and Bob stakes at t2 another asset worth 100 points. In order to simplify the examples, let’s also consider that all periods fall in the same window, thus having a constant reward rate.
Alice claims after Bob stakes
In this scenario, Alice claims her rewards in t3 after Bob stakes. She will get less rewards from the [t1, t2] period, as the calculation will consider the entire period [t1, t3] and calculate the shares using 200 points. Here the correct way would be to calculate the period [t1, t2] using 100 total points, and the period [t2, t3] using 100 total points.
- Alice stakes at
t1and gets 100 points. Total points is 100. - Bob stakes at
t2and gets 100 points. Total points is 200. - Alice claims rewards at
t3. She will get less rewards since the calculation will be done using 200 points.
Alice and Bob stake at same time
Here, t1 == t2 and Bob and Alice stake at the same time. Alice unstakes at t3 and Bob claims rewards at t4. In this case, Bob will get more rewards, as the calculation will consider the entire period [t1, t4] and calculate the shares using 100 points. Here the correct way would be to calculate the period [t1, t3] using 200 total points, and the period [t3, t4] using 100 total points.
- Alice and Bob stake at
t1 == t2and each one gets 100 points. Total points is 200. - Alice unstakes at
t3. Total points is 100. - Bob claims rewards at
t4. He will get more rewards since the calculation will be done using 100 points.
Griefer intentionally claims rewards of Alice
As described in the previous section, a bad actor can intentionally claim the rewards of another user at a time the pool has a high value for total points, since this call as this is a permissionless action.
- Alice stakes at
t1and gets 100 points. Total points is 100. - Bob stakes at
t2and gets 100 points. Total points is 200. - Bad actor claims rewards of Alice at
t3. She will get less rewards since the calculation will be done using 200 points.
Recommendation
Rewards calculation should track reward rate according to modifications in the pool’s total points caused by stake or unstake actions.
My recommendation for a performant solution would be to follow this staking example or the full Staking contract from Synthetix. The principal idea here is that every action that affects rewards triggers the updateReward modifier, which updates the rewardPerTokenStored variable that tracks the reward amount per staked token. A similar idea could be adapted to track the reward per point for the current contract. Stake and unstake actions should update this variable before modifying the pool’s total points.
TimTinkers (Neo Tokyo) confirmed and commented:
Excellent catch, good write-up.
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 reward calculation uses the current pool.totalPoints instead of tracking the total points over time, so changes to total points are not accounted for when computing rewards for past periods.
Impact
Existing stakers may receive fewer rewards when others stake, or more rewards when others unstake; an attacker can also deliberately claim another user's rewards at a time when total points are high to reduce that user's rewards.
What the attacker needed
A staking pool with at least one staker; ability to stake or unstake to change total points; for griefing: permissionless reward claim
The fix
Rewards calculation should track reward rate according to modifications in the pool’s total points caused by stake or unstake actions, for example by updating a reward-per-point variable before changing total points.
How it was found
- Alice stakes at t1 and gets 100 points. Total points is 100.
- Bob stakes at t2 and gets 100 points. Total points is 200.
- Alice claims rewards at t3. She will get less rewards since the calculation will be done using 200 points.
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.
In a staking pool, each person's share of the rewards is based on their points divided by the total points in the pool. The bug is that the contract uses the total points at the moment rewards are claimed, not the total points during the time the person was actually staked. So if someone else joins later, your share is calculated as if they were there the whole time, and you get less than you should.
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.