https://github.com/Cyfrin/2024-07-templegold/blob/main/protocol/contracts/templegold/TempleGoldStaking.sol#L495
https://github.com/Cyfrin/2024-07-templegold/blob/main/protocol/contracts/templegold/TempleGoldStaking.sol#L463
The Staking keeps a track of global rewardPerTokenStored
and for a particular user the userRewardPerTokenPaid
, which ensures that the user will get their reward subtracting their previously claimed reward.
When a user opens a stake in future within the current reward distribution period they will have their userRewardPerTokenPaid
for that stake as 0 which was expected to be updated to rewardPerTokenStored
so that they will be excluded from all the previous reward accumulated and have a start with current reward distribution, but due to this missing implementation the user will be included in the rewards distribution from the starting and have their claimable rewards more than expected and will make (Total Claimable Rewards of all Users > Total TGLD avaiable for claims for current distribution period) and thus creates a DoS for withdrawal and other stuffs.
The vulnerability is present in the TempleGoldStaking::_applyStake
function where it lags the implementation to update the userRewardPerTokenPaid
which makes the users participating to have claimable rewards from the starting value of rewardPerTokenStored
which will thus increase their claimable rewards by a large amount and creates a Denial of Service for claiming of rewards, as the total claimable rewards are more than TGLD claim avaiable for a particular distribution period, thus some users will not be able to claim.
When a user stakes, the rewardPerTokenStored
is updated to calculate it for the current timestamp by including the previous supply of stakes before the user has staked so the user will be expected to receive their rewards from when they have deposited for an ongoing reward distribution period, but due to the missing updation of userRewardPerTokenPaid
when a user stakes, it makes it calcuate their rewards by considering the previous rewards.
As the claimable rewards for users will be much larger than they actually should get, will make the total claimable rewards of user much more than the actual TGLD amount available for claim, thus some or all users will not be able to perform their claim.
If minted TGLD is supplied to TempleGoldStaking
, so after the reward for a period is consumed then this TGLD which was for next distribution will be consumed and thus creating DoS for the whole Staking system.
Add the below coded PoC in the TempleGoldStakingTest
contract present in file: test/forge/templegold/TempleGoldStaking.t.sol
Run the test:
Manual Review, Unit Test in Foundry
When a user peforms a stake, then it should be ensured that they are not included in the previous rewards, therefore in the _applyStake
function update the userRewardPerTokenPaid
for that user's stake to current rewardPerTokenStored
so that while calculating their reward they will only be included in their current period and not in previous reward period from the starting.
Now after applying the recommendation, the test fails due to a failing assert which shows that now the rewards are allocated correctly.
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.