The TempleGoldStaking contract is susceptible to reward rate manipulation through large stake and unstake operations. This vulnerability allows an attacker to significantly reduce the reward rate for other stakers by manipulating the total supply just before reward distribution.
The vulnerability exists in the reward calculation mechanism, specifically in the _rewardPerToken() function:
This function calculates the reward per token based on the current totalSupply
. The vulnerability arises from the fact that an attacker can manipulate this totalSupply
just before reward distribution to significantly impact the reward rate.
The attack can be executed as follows:
1) The attacker waits until just before the reward distribution period.
2) They stake a large amount of tokens, significantly increasing the totalSupply
:
3) If the distributionStarter
is set to address(0)
they trigger the reward distribution:
4) Immediately after distribution, the attacker withdraws their large stake:
This sequence of actions results in a significantly reduced rewardRate
for other stakers, as the rate is calculated based on the inflated totalSupply
at the time of distribution.
Detailed Exploit Scenario:
Initial State:
The TempleGoldStaking contract is deployed with a 7-day vesting period and a 7-day reward duration.
The attacker and victim each have 500,000 staking tokens.
The staking contract has 10,000 reward tokens available for distribution.
Step 1: The victim stakes 100 tokens, believing they will receive a fair share of rewards.
Step 2: Just before the reward distribution (1 hour before), the attacker stakes 500,000 tokens, dramatically increasing the total supply.
Step 3: The attacker triggers the reward distribution immediately after staking. The reward rate is calculated based on the inflated total supply.
Step 4: The attacker immediately unstakes their tokens after the reward distribution.
Step 5: Over the next reward period, rewards accrue based on the manipulated rate.
Outcome:
The victim receives significantly fewer rewards than expected, despite being staked for the entire period.
The attacker receives a disproportionately high amount of rewards for their brief staking period.
The exploitation of this vulnerability could have severe consequences:
Honest stakers would receive substantially fewer rewards than they should, effectively having their rewards stolen by the attacker.
The repeated exploitation of this vulnerability could lead to significant financial losses for legitimate stakers.
As users become aware of the unfair reward distribution, they may lose faith in the protocol, potentially leading to a mass exodus of stakers.
Manual Review
To address this vulnerability, consider implementing the following mitigations:
Time-Weighted Average Total Supply: Instead of using the current totalSupply
for reward calculations, implement a time-weighted average of the total supply over the reward period. This would significantly reduce the impact of short-term supply manipulations.
Vesting Period for New Stakes: Implement a vesting period for newly staked tokens before they're eligible for rewards. This would prevent rapid stake-distribute-unstake attacks.
Maximum Stake Size: Implement a maximum stake size relative to the current total supply to limit the impact of large stakes.
Delay Between Staking and Reward Distribution: Implement a mandatory delay between large stake changes and reward distribution to prevent rapid manipulation.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.