Loss of staking yield for stakers when another user stakes in pause/frozen state
Staking is permitted while the system is paused or frozen, but the reward payout function is skipped in that state. A user who stakes during the pause immediately receives a share of rewards that had accrued to existing stakers, diluting their yield.
It is possible for a user to steal the yield from other stakers by staking when the system is paused or frozen.
This is because staking is allowed while paused/frozen, but _payoutRewards() is not called during so. Staking rewards are not paid out to current stakers when a new staker stakes, so the new staker immediately gets a portion of the rewards, without having to wait for a reward period.
function stake(uint256 rsrAmount) external {
require(rsrAmount > 0, "Cannot stake zero");
if (!main.pausedOrFrozen()) _payoutRewards();
...
}
Proof of concept
A test case can be included in ZZStRSR.test.ts under ‘Add RSR / Rewards’:
Note that await advanceTime(Number(config.rewardPeriod) * 5) can be before or after the pause, same result will occur.
Run with:
yarn test:p1 --grep "Audit"
Output:
addr1 RSR = 10000545505689818061216
addr2 RSR = 10000545505689818061214
StRSRP1 contract
Add RSR / Rewards
✔ Audit: Loss of staking yield for stakers when another user stakes in pause/frozen state (1504ms) (1504ms)
1 passing (2m)
The PoC demonstrates that the staker2 stole half of the rewards from staker1. staker1 staked for 5 rewardPeriod, staker2 did not have to wait at all, but still received half of the reward share.
Impact
This should fall into “Theft of unclaimed yield”, suggesting High risk. But the amount of RSR that can be stolen depends on the liveliness of the staking pool (how often _payoutRewards() is called). If the time window between the last stake(...)/unstake(...)/payoutRewards(...) and pause()/freezeUntil(...) is small, then no/less RSR yield can be stolen.
system-design.md rewardPeriod:
Default value: `86400` = 1 day
Mainnet reasonable range: 10 to 31536000 (1 year)
For RTokens which choose a smaller value for rewardPeriod, the risk is higher. If rewardPeriod = 86400 like recommended, then for this attack to occur, no one must have called stake(...)/unstake(...)/payoutRewards(...) for 1 day before the pause/freeze occured.
Likelihood is Low for a reasonably set rewardPeriod and lively project. Therefore submitting as Medium risk.
Recommendations
I’m unsure of why staking is allowed when paused/frozen and the reason for the line:
if (!main.pausedOrFrozen()) _payoutRewards();
The team should consider the reason for the above logic.
If the above logic is required, then I would suggest that poke() in Main.sol be called inside of pause() and freezeUntil(...) to update the state before pausing/freezing. Since distribute(...) has modifier notPausedOrFrozen, I would assume in pause/frozen state, no RSR is sent to stRSR contract (i.e. no rewards when paused/frozen) so this recommendation should be sufficient in preventing the issue.
The original report as published on Code4rena, by Soosh. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
The stake function only calls _payoutRewards when the system is not paused or frozen, so rewards accrued before the pause are not settled before a new staker joins.
Impact
The second staker could take roughly half of the rewards that the first staker had accrued, though the amount stolen depends on how recently rewards were last paid out and how often the pool is active.
What the attacker needed
System is paused or frozen; attacker can stake during the pause; rewards have accrued since last payout
The fix
The team mitigated the issue by disabling staking when the system is frozen.
How it was found
Stake as the first user.
Advance time by several reward periods.
Pause the system.
Stake as a second user while paused.
Unpause, then both users unstake and withdraw.
Observe that both users receive approximately the same RSR balance.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
Staking means locking tokens to earn a share of rewards over time. Normally, before anyone new joins, the contract pays out the rewards earned so far so the newcomer cannot claim a share of past rewards. Here, while the system was paused, that payout step was skipped, so a new staker got a share of rewards that belonged to earlier stakers.
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.