Attacker can make stakeRate to be 1 in the StRSR contract and users depositing tokens can lose funds because of the big rounding error
The StRSR contract sets its reward baseline from its own token balance at initialisation, so an attacker can pre-fund the not-yet-deployed address and then stake a tiny amount to drive the stake rate down to 1. Later stakers then lose up to 1e18 units of value to rounding, and the attacker can unstake to extract a disproportionate amount.
Originally disclosed on Code4rena
Submitted by unforgiven
https://github.com/reserve-protocol/protocol/blob/df7ecadc2bae74244ace5e8b39e94bc992903158/contracts/p1/StRSR.sol#L160-L188 https://github.com/reserve-protocol/protocol/blob/df7ecadc2bae74244ace5e8b39e94bc992903158/contracts/p1/StRSR.sol#L496-L530 https://github.com/reserve-protocol/protocol/blob/df7ecadc2bae74244ace5e8b39e94bc992903158/contracts/p1/StRSR.sol#L212-L237
Code calculates amount of stake token and rsr token based on stakeRate and if stakeRate was near 1e18 then division error is small but attacker can cause stakeRate to be 1 and that can cause users to loss up to 1e18 token during stake and unstake.
Proof of Concept
This is init() code:
function init(
IMain main_,
string calldata name_,
string calldata symbol_,
uint48 unstakingDelay_,
uint48 rewardPeriod_,
uint192 rewardRatio_
) external initializer {
require(bytes(name_).length > 0, "name empty");
require(bytes(symbol_).length > 0, "symbol empty");
__Component_init(main_);
__EIP712_init(name_, "1");
name = name_;
symbol = symbol_;
assetRegistry = main_.assetRegistry();
backingManager = main_.backingManager();
basketHandler = main_.basketHandler();
rsr = IERC20(address(main_.rsr()));
payoutLastPaid = uint48(block.timestamp);
rsrRewardsAtLastPayout = main_.rsr().balanceOf(address(this));
setUnstakingDelay(unstakingDelay_);
setRewardPeriod(rewardPeriod_);
setRewardRatio(rewardRatio_);
beginEra();
beginDraftEra();
}
As you can see it sets the value of the rsrRewardsAtLastPayout as contract balance when contract is deployed. This is _payoutReward() code:
function _payoutRewards() internal {
if (block.timestamp < payoutLastPaid + rewardPeriod) return;
uint48 numPeriods = (uint48(block.timestamp) - payoutLastPaid) / rewardPeriod;
uint192 initRate = exchangeRate();
uint256 payout;
// Do an actual payout if and only if stakers exist!
if (totalStakes > 0) {
// Paying out the ratio r, N times, equals paying out the ratio (1 - (1-r)^N) 1 time.
// Apply payout to RSR backing
// payoutRatio: D18 = FIX_ONE: D18 - FixLib.powu(): D18
// Both uses of uint192(-) are fine, as it's equivalent to FixLib.sub().
uint192 payoutRatio = FIX_ONE - FixLib.powu(FIX_ONE - rewardRatio, numPeriods);
// payout: {qRSR} = D18{1} * {qRSR} / D18
payout = (payoutRatio * rsrRewardsAtLastPayout) / FIX_ONE;
stakeRSR += payout;
}
payoutLastPaid += numPeriods * rewardPeriod;
rsrRewardsAtLastPayout = rsrRewards();
// stakeRate else case: D18{qStRSR/qRSR} = {qStRSR} * D18 / {qRSR}
// downcast is safe: it's at most 1e38 * 1e18 = 1e56
// untestable:
// the second half of the OR comparison is untestable because of the invariant:
// if totalStakes == 0, then stakeRSR == 0
stakeRate = (stakeRSR == 0 || totalStakes == 0)
? FIX_ONE
: uint192((totalStakes * FIX_ONE_256 + (stakeRSR - 1)) / stakeRSR);
emit RewardsPaid(payout);
emit ExchangeRateSet(initRate, exchangeRate());
}
As you can see it sets the value of the stakeRate to (totalStakes * FIX_ONE_256 + (stakeRSR - 1)) / stakeRSR.
So to exploit this attacker needs to perform these steps:
- send
200 * 1e18RSR tokens (18 is the precision) to the StRSR address before its deployment by watching mempool and front running. the deployment address is calculable before deployment. - function
init()would get executed and would set200 * 1e18asrsrRewardsAtLastPayout. - then attacker would call
stake()and stake 1 RSR token (1 wei) in the contract and the value ofstakeRSRandtotalStakeswould be 1. - then attacker wait for
rewardPeriodseconds and then callpayoutReward()and code would pay rewards based onrewardRatioandrsrRewardsAtLastPayoutand asrewardRatiois higher than 1% (default and normal mode) code would increasestakeRatemore than2 * 1e18amount. and then code would setstakeRateastotalStakes * FIX_ONE_256 + (stakeRSR - 1)) / stakeRSR = 1. - then calls to
stake()would cause users to lose up to1e18RSR tokens as code calculates stake amount asnewTotalStakes = (stakeRate * newStakeRSR) / FIX_ONEand rounding error happens up toFIX_ONE. because the calculated stake amount is worth less than deposited rsr amount up to1e18. - attacker can still users funds by unstaking 1 token and receiving
1e18RSR tokens. because of the rounding error inunstake()
so attacker can manipulate the stakeRate in contract deployment time with sandwich attack which can cause other users to lose funds because of the big rounding error.
Tools Used
VIM
Recommended Mitigation Steps
Prevent early manipulation of the PPS.
Addressed in https://github.com/reserve-protocol/protocol/pull/617
Status: Mitigation confirmed with comments. Full details in reports from 0xA5DF, HollaDieWaldfee, and AkshaySrivastav.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the reward baseline is taken from the contract's current token balance at initialisation, letting an attacker pre-fund the address and then use a tiny stake plus a reward payout to force the stake rate to its minimum.
Impact
Users who stake after the manipulation could lose up to 1e18 units of value to rounding, and the attacker could gain by unstaking.
What the attacker needed
Ability to front-run the deployment transaction; attacker holds RSR tokens to pre-fund the address; victims stake after the rate is manipulated
The fix
The project addressed the issue in a pull request, and the mitigation was confirmed.
How it was found
- Send RSR tokens to the StRSR address before deployment by front-running the deployment transaction from the mempool.
- The init function runs and records that balance as the reward baseline.
- Stake a single wei of RSR so total stakes and staked RSR are both 1.
- Wait for the reward period and call the payout function so the stake rate is recalculated to 1.
- Later users who stake lose value to rounding, and the attacker can unstake to receive a disproportionate amount.
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.
A staking contract tracks how much each person's deposit is worth using a shared exchange rate. If that rate is pushed to an extreme value, the maths that converts deposits into shares rounds badly, so a new depositor can receive far fewer shares than their tokens are worth. The attacker here sets up that extreme rate before anyone else joins.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.