unstake should update exchange rates first
The unstake function computes the number of shares to remove using the old exchange rate before updating it, causing users to burn more shares than necessary and lose accrued rewards. An attacker could exploit this to deprive themselves or others of rewards, though the primary impact is loss of funds for the user.
Submitted by cmichel
The unstake function does not immediately update the exchange rates. It first computes the validatorSharesRemove = tokensToShares(amount, v.exchangeRate) with the old exchange rate.
Only afterwards, it updates the exchange rates (if the validator is not disabled):
// @audit shares are computed here with old rate
uint128 validatorSharesRemove = tokensToShares(amount, v.exchangeRate);
require(validatorSharesRemove > 0, "Unstake amount is too small");
if (v.disabledEpoch == 0) {
// @audit rates are updated here
updateGlobalExchangeRate();
updateValidator(v);
// ...
}
Impact
More shares for the amount are burned than required and users will lose rewards in the end.
POC
Demonstrating that users will lose rewards:
- Assume someone staked
1000 amountand received1000 shares, andv.exchangeRate = 1.0. (This user is the single staker) - Several epochs pass, interest accrues, and
1000 tokensaccrue for the validator,tokensGivenToValidator = 1000. User should be entitled to 1000 in principal + 1000 in rewards = 2000 tokens. - But user calls
unstake(1000), which setsvalidatorSharesRemove = tokensToShares(amount, v.exchangeRate) = 1000 / 1.0 = 1000. Afterwards, the exchange rate is updated:v.exchangeRate += tokensGivenToValidator / totalShares = 1.0 + 1.0 = 2.0. The staker is updated withs.shares -= validatorSharesRemove = 0ands.staked -= amount = 0. And the user receives their 1000 tokens but notice how the user’s shares are now at zero as well. - User tries to claim rewards calling
redeemAllRewardswhich fails as therewardsare 0.
If the user had first called redeemAllRewards and unstake afterwards they’d have received their 2000 tokens.
Recommended Mitigation Steps
The exchange rates always need to be updated first before doing anything. Move the updateGlobalExchangeRate() and updateValidator(v) calls to the beginning of the function.
kitti-katy (Covalent) confirmed
GalloDaSballo (judge) commented:
Agree with the finding, using the old exchange rate ends up burning more shares than what would be correct The sponsor has mitigated the issue
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
The unstake function computes the shares to remove using the old exchange rate before updating the exchange rate, leading to an incorrect calculation.
What the attacker needed
User has staked tokens; interest has accrued
The fix
The exchange rates should be updated before computing the shares to remove, by moving the update calls to the beginning of the function.
How it was found
- Assume a user staked 1000 tokens and received 1000 shares with an exchange rate of 1.0.
- After several epochs, 1000 tokens of interest accrue, so the user should be entitled to 2000 tokens total.
- The user calls unstake(1000), which computes shares to remove using the old exchange rate (1.0), resulting in 1000 shares removed.
- Afterwards, the exchange rate is updated to 2.0, but the user's shares are now zero.
- The user tries to claim rewards with redeemAllRewards, but it fails because rewards are zero.
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.
When you unstake, the system calculates how many 'shares' to take away based on an exchange rate. But it uses an old rate before updating it, so it takes away too many shares. This means you lose rewards you should have earned.
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.