Skip to content
Business logic Audit finding Intermediate

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.

cmichel Follow

Originally disclosed on Code4rena

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:

  1. Assume someone staked 1000 amount and received 1000 shares, and v.exchangeRate = 1.0. (This user is the single staker)
  2. Several epochs pass, interest accrues, and 1000 tokens accrue for the validator, tokensGivenToValidator = 1000. User should be entitled to 1000 in principal + 1000 in rewards = 2000 tokens.
  3. But user calls unstake(1000), which sets validatorSharesRemove = 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 with s.shares -= validatorSharesRemove = 0 and s.staked -= amount = 0. And the user receives their 1000 tokens but notice how the user’s shares are now at zero as well.
  4. User tries to claim rewards calling redeemAllRewards which fails as the rewards are 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

The original report as published on Code4rena, by cmichel. 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.

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

  1. Assume a user staked 1000 tokens and received 1000 shares with an exchange rate of 1.0.
  2. After several epochs, 1000 tokens of interest accrue, so the user should be entitled to 2000 tokens total.
  3. The user calls unstake(1000), which computes shares to remove using the old exchange rate (1.0), resulting in 1000 shares removed.
  4. Afterwards, the exchange rate is updated to 2.0, but the user's shares are now zero.
  5. 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.

More by cmichel

View all

196 more disclosed reports, best first.

More from Covalent contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.