Users can dodge losses due to StRSR era changes with instant operations
The StRSR contract resets stakeRate and draftRate independently when they exceed maximum values, causing losses for some holders. An attacker can sandwich the rate reset with unstake and cancelUnstake calls to avoid losses and gain a share of RSR left in the contract.
The StRSR contract implements an era-based wrapping of the stakeRate and draftRate exchange rates whenever these pass the maximum accepted value of MAX_STAKE_RATE and MAX_DRAFT_RATE; the effect of this action is that StRSR token holders see their balance reset (stakeRate reset) and/or all StRSR vesting withdraws are forgotten and the relative funds lost (draftRate reset).
From the code below, we can see that these two rates can be reset independently;
File: StRSR.sol
424: function seizeRSR(uint256 rsrAmount) external {
---
440: // Remove RSR from stakeRSR
441: uint256 stakeRSRToTake = (stakeRSR * rsrAmount + (rsrBalance - 1)) / rsrBalance;
442: stakeRSR -= stakeRSRToTake;
443: seizedRSR = stakeRSRToTake;
444:
445: // update stakeRate, possibly beginning a new stake era
446: if (stakeRSR != 0) {
447: // Downcast is safe: totalStakes is 1e38 at most so expression maximum value is 1e56
448: stakeRate = uint192((FIX_ONE_256 * totalStakes + (stakeRSR - 1)) / stakeRSR);
449: }
450: if (stakeRSR == 0 || stakeRate > MAX_STAKE_RATE) {
451: seizedRSR += stakeRSR;
452: beginEra();
453: }
454:
455: // Remove RSR from draftRSR
456: uint256 draftRSRToTake = (draftRSR * rsrAmount + (rsrBalance - 1)) / rsrBalance;
457: draftRSR -= draftRSRToTake;
458: seizedRSR += draftRSRToTake;
459:
460: // update draftRate, possibly beginning a new draft era
461: if (draftRSR != 0) {
462: // Downcast is safe: totalDrafts is 1e38 at most so expression maximum value is 1e56
463: draftRate = uint192((FIX_ONE_256 * totalDrafts + (draftRSR - 1)) / draftRSR);
464: }
465:
466: if (draftRSR == 0 || draftRate > MAX_DRAFT_RATE) {
467: seizedRSR += draftRSR;
468: beginDraftEra();
469: }
… and we’d then argue that they will most likely happen at different times, at the very least because rewards contribute to the evolution of stakeRate only.
In the event only one of the two rates reaches the cap and undergoes a reset, stakers can dodge the bullet of collectivized losses, by:
sandwiching the stakeRate wrapping with unstake and cancelUnstake calls
sandwiching the draftRate wrapping with cancelUnstake and unstake calls
Impact
Users can avoid the collectivization of rate wrapping losses, and artificially get a share of any RSR left in the StRSR contract.
This latter quantity may be considerable, because while unlikely, it’s not impossible that the rate wrapping happens when little RSR is seized from an otherwise large RSR balance in the StRSR contract.
Proof of Concept
Alice is an StRSR token holder:
she notices that a transaction will soon cause RSR to be seized and the stakeRate will wrap to 1.0
she immediately initiates an unstake, so her funds are moved to the drafts bucket
immediately after the transaction that wraps the stakeRate, she calls cancelUnstake
Alice lost only the pro-rata RSR that was seized (which may be minimal) while others lost everything
A similar sequence can be performed to avoid losses on funds that are being unstaked by instead calling cancelUnstake first and unstake later.
Tools Used
Code review, Foundry
Recommended Mitigation Steps
Consider wrapping both rates when at least one reaches the maximum.
/// The stake rate is unsafe when it is either too high or too low.
/// There is the possibility of the rate reaching the borderline of being unsafe,
/// where users won't stake in fear that a reset might be executed.
/// A user may also grief this situation by staking enough RSR to vote against any reset.
/// This standoff will continue until enough RSR is staked and a reset is executed.
/// There is currently no good and easy way to mitigate the possibility of this situation,
/// and the risk of it occurring is low enough that it is not worth the effort to mitigate.
The reported issue requires the same set of assumptions to get into this state, and then additional assumptions about the size of the next seizeRSR(). However, the impact trades off directly against the likelihood: the further the rate is away from the 1e27 bounds, the less likely this case is to occur. The closer to 1e27, the more clear it is to governance that they need to resetStakes(). And there are 3 orders of magnitude between 1e27 and 1e24 for this dynamic to play out.
We would like to acknowledge the issue but dispute severity to Low, given it is a subset of a case already assumed low enough probability to not be worth mitigating.
Hi @cccz and @tbrent - the require statement in resetStakes makes this function a viable option to mitigate only the issue in case of stakeRate being close to wrapping, but does not help with the other scenario presented by this issue - that is when draftRate wraps.
function resetStakes() external {
_requireGovernanceOnly();
require(
stakeRate <= MIN_SAFE_STAKE_RATE || stakeRate >= MAX_SAFE_STAKE_RATE,
"rate still safe"
);
beginEra();
beginDraftEra();
}
Because draftRate normally goes only up with seizeRSR, and stakeRate instead goes up with seizeRSR but also goes down with _payoutRewards, it is still possible that draftRate grows close to MAX_STAKE_RATE while stakeRate is still below MAX_SAFE_STAKE_RATE. This scenario would prevent governance intervention, entering a situation where a draftRate wrapping alone is inevitable.
I would therefore ask you to reconsider the Medium severity, and potentially consider another mitigation option in allowing calls to resetStakes also when draftRate grows above MAX_SAFE_STAKE_RATE.
That is a good point: resetStakes() cannot be used when it is the draft rate that is out of bounds. I think there is a change to the code to make there of some variety.
Still, in order for the below to be true:
draftRate grows close to MAX_STAKE_RATE while stakeRate is still below MAX_SAFE_STAKE_RATE.
there must be at least 1000x historical appreciation of StRSR overall. This is an additional assumption beyond the initial assumptions of: (i) draftRate grows close to MAX_STAKE_RATE (requires consecutive nearly full-but-not-full seizures); (ii) can frontrun seizeRSR().
Based on the above conversation, users can frontrun resetStakes to avoid losses or get benefits. If this is correct, I would consider this issue to be low likelihood + high impact = Medium severity.
Status: Mitigation confirmed. Full details in reports from ether_sky and Bauchibred.
The original report as published on Code4rena, by RadiantLabs. 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
Likely the independent reset of stakeRate and draftRate allows users to move funds between the stake and draft buckets to avoid the reset's effects.
What the attacker needed
Be an StRSR token holder; rate close to maximum; frontrun a seizeRSR transaction
The fix
The project mitigated the issue, but the text does not specify the exact fix; the recommended mitigation was to wrap both rates when at least one reaches the maximum.
How it was found
Notice that a transaction will soon cause RSR to be seized and the stakeRate will wrap to 1.0
Immediately initiate an unstake, moving funds to the drafts bucket
After the transaction that wraps the stakeRate, call cancelUnstake
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
StRSR is a staking contract where users deposit RSR tokens and receive StRSR tokens. The exchange rate between them can reset when it gets too high, which can cause some users to lose funds. The report shows that by timing unstake and cancelUnstake calls around the reset, a user can avoid those losses and even profit at others' expense.
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.