Broken assumptions can lead to the inability to seize RSR
A broken invariant in the staking contract allows a staker to make totalStakes zero while stakeRSR remains non-zero, causing a division by zero when seizeRSR is called. An attacker can front-run a seize transaction to prevent the protocol from seizing RSR, potentially disrupting a necessary rebalance.
The seizeRSRfunction takes RSR from the staking contract when BackingManager wants to sell RSR, but it does not have enough. In this case, the stakers can lose a portion of their stake in order to keep the system healthy. However, an issue arises from broken assumptions about stakeRSR and totalStakes, which will make the contract unable to seize due to revert.
Proof of Concept
Let’s assume the system becomes unhealthy, and the stakers will start unstaking their tokens in fear of an upcoming reset/seize. Then, the expected event comes, and the BackingManager tries to seize RSR from the staking contract.
This action is frontran however, with the last staker who:
_payoutRewards is called. Since totalStakes is still above 1e18, stakeRSR can be increased and become greater than totalStakes. Let’s assume that totalStakes would be 1000e18 and stakeRSR would end at 1001e18 after this step.
Now, the stakeRate would be updated. Since stakeRSR is not zero yet, and so is not totalStakes, it would be set to
payoutRewards is skipped because it was already called in this block
newTotalStakes in mintStakes is calculated as (stakeRate * newStakeRSR) / FIX_ONE, which translates to (stakeRate * (stakeRSR + rsrAmount)) / FIX_ONE which translates to (999000999000999001 * (0 + 1)) / 1e18 resulting in 0, meaning 0 tokens are minted and totalStakes remains 0
stakeRSR is increased by 1
The second stake call will have the same effects
after this call, the stakeRate is 999000999000999001, totalStakes is 0, and stakeRSR is 2 (note that this breaks this assumption)
Now we examine the important parts of the seizeRSR call
stakeRSRToTake is set to (2 * rsrAmount + (rsrBalance - 1)) / rsrBalance, which will result in 1 every time the we are not seizing more than half of the total balance of the contract
stakeRSR is therefore set to 1, and since it is non-zero, stakeRate is updated to uint192((FIX_ONE_256 * totalStakes + (stakeRSR - 1)) / stakeRSR), which translates to uint192((1e18 * 0 + (1 - 1)) / 1) which comes to 0
the rest of the call is not important until it comes to
since stakeRate was updated to 0 before, the call will revert
Impact and Likelihood
The likelihood of this issue is LOW. The impact seems somewhere between HIGH and MEDIUM since a necessary rebalance action can be DoSed until it is noticed and an action is taken. Considering that the issue is possible due to a broken invariant, the severity should be judged as MEDIUM.
Recommendation
One way to fix the issue would be to enforce the invariant if totalStakes == 0, then stakeRSR == 0. This could be done by assuring that amount is not 0 in _mint.
Another mitigation could be to update the seizeRSR function to update the stakeRate only if both stakeRSR and totalStakes are non-zero or update the stakeRate to FIX_ONE if either of these two is zero.
Since the initial judging time is not enough to verify it, consider it valid first and keep open, and wait for the PoC from warden.
And may invalidate it without further proof.
Hi @akshatmittal @tbrent - The PoC in the issue is wrong; however, the issue exists. Below I attach the updated PoC with a test proving the issue. Under those conditions seizeRSR will be dosed due to a broken protocol invariant.
Let's assume the system becomes unhealthy, and the stakers will start unstaking their tokens in fear of an upcoming reset/seize. Then the following chain of action happens:
The last staker unstakes everything but one token so _payoutRewards updates stakeRSR
Stake rate is below 5e17 - this can happen natuarally or forcefully (in our scenario everybody unstaked so we can influence it in some way)
BackingManager tries to seize RSR from the staking contract, but this transaction is frontran with another transaction in which
payoutRewards is skipped because it was already called in this block
newTotalStakes in mintStakes is calculated as (stakeRate * newStakeRSR) / FIX_ONE, which translates to (stakeRate * (stakeRSR + rsrAmount)) / FIX_ONE which translates to (499999999999999999 * (0 + 2)) / 1e18 resulting in 0, meaning 0 tokens are minted and totalStakes remains 0
stakeRSR is increased by 2
Note that 499999999999999999 is the greatest possible value of stakeRate when the issue arises
in the end we can see that totalStakes is 0, and stakeRSR is 2 (note that this breaks this assumption)
Now we examine the important parts of the seizeRSR call
stakeRSRToTake is set to (2 * rsrAmount + (rsrBalance - 1)) / rsrBalance, which will result in 1 every time the we are not seizing more than half of the total balance of the contract
stakeRSR is therefore set to 1, and since it is non-zero, stakeRate is updated to uint192((FIX_ONE_256 * totalStakes + (stakeRSR - 1)) / stakeRSR), which translates to uint192((1e18 * 0 + (1 - 1)) / 1) which comes to 0
the rest of the call is not important until it comes to
Hey @krikolkk, thanks for that. Can you please share the code for SeizeAttacker contract and replace the last call with impersonating BackingManager instead? (So I’m able to reproduce this)
@akshatmittal - the volume can be cumulated with other stakers who already unstaked. The main problem here is that we are breaking a protocol variant, as shown in the PoC, hence the code behaves unexpectedly.
The original report as published on Code4rena, by krikolkk. 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 contract allows minting zero shares when stakeRate is low, breaking the invariant that totalStakes and stakeRSR are both zero or both non-zero.
Impact
A necessary rebalance action can be blocked (DoS) until noticed, but the likelihood is low and requires specific conditions.
What the attacker needed
Attacker is the last staker; stakeRate < 5e17 - 1; Attacker can front-run seize call; Sufficient RSR volume
The fix
The issue was mitigated by enforcing that amount is not zero in _mint or by updating stakeRate only when both stakeRSR and totalStakes are non-zero.
How it was found
The last staker unstakes everything but one token, which updates stakeRSR and sets stakeRate below 5e17.
The staker unstakes the last token and stakes 2 wei in the same transaction, leaving totalStakes at 0 and stakeRSR at 2.
The BackingManager attempts to seize RSR, but the transaction reverts due to division by zero when updating stakeRate.
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
In a staking system, users deposit tokens and receive shares. The contract tracks total deposits (stakeRSR) and total shares (totalStakes). Normally, if shares are zero, deposits should also be zero. This report shows a way to make shares zero while deposits are not, which later causes a division by zero when the system tries to take some tokens, blocking an important action.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.