Skip to content
Smart contracts Audit finding Intermediate

MultiRewardStaking.changeRewardSpeed() breaks the distribution

The changeRewardSpeed function miscalculates the new end timestamp when the reward rate is increased, causing the contract to schedule more rewards than it holds. This breaks the reward distribution and could leave users unable to claim their full rewards.

Ruhum Follow with minhtrng, ulqiorra and 6 others

Originally disclosed on Code4rena

Submitted by Ruhum, also found by minhtrng, ulqiorra, 0xMirce, rvierdiiev, gjaldon, cccz, 0xRobocop, and chaduke

https://github.com/code-423n4/2023-01-popcorn/blob/main/src/utils/MultiRewardStaking.sol#L296-L315

https://github.com/code-423n4/2023-01-popcorn/blob/main/src/utils/MultiRewardStaking.sol#L351-L360

Impact

The changeRewardSpeed() doesn’t calculate the new endTimestamp correctly. That causes the reward distribution to be broken.

Proof of Concept

Given that we have an existing reward with the following configuration:

  • startTimestamp = 0
  • endTimestamp = 100
  • rewardPerSecond = 2
  • initialBalance = 200

The reward speed is changed at timestamp = 50, meaning 100 tokens were already distributed. The new endTimestamp is calculated by calling _calcRewardsEnd():

    // @audit using balanceOf() here has its own issues but let's ignore those for this submission
    uint256 remainder = rewardToken.balanceOf(address(this));

    uint32 prevEndTime = rewards.rewardsEndTimestamp;

    uint32 rewardsEndTimestamp = _calcRewardsEnd(
      prevEndTime > block.timestamp ? prevEndTime : block.timestamp.safeCastTo32(),
      rewardsPerSecond,
      remainder
    );

And the calculation is:

  function _calcRewardsEnd(
    uint32 rewardsEndTimestamp,
    uint160 rewardsPerSecond,
    uint256 amount
  ) internal returns (uint32) {
    if (rewardsEndTimestamp > block.timestamp)
      amount += uint256(rewardsPerSecond) * (rewardsEndTimestamp - block.timestamp);

    return (block.timestamp + (amount / uint256(rewardsPerSecond))).safeCastTo32();
  }
  • rewardsEndTimestamp = 100 (initial endTimestamp)
  • block.timestamp = 50 (as described earlier)
  • amount = 100
  • rewardsPerSecond = 4 (we update it by calling this function)

Because rewardEndTimestamp > block.timestamp, the if clause is executed and amount is increased:

$amountNew = 100 + 4 \ast (100 - 50) = 300$

Then it calculates the new endTimestamp:

$50 + (300 / 4) = 125$

Thus, by increasing the rewardsPerSecond from 2 to 4, we’ve increased the endTimestamp from 100 to 125 instead of decreasing it. The total amount of rewards that are distributed are calculated using the rewardsPerSecond and endTimestamp. Meaning, the contract will also try to distribute tokens it doesn’t hold. It only has the remaining 100 tokens.

By increasing the rewardsPerSecond the whole distribution is broken.

Recommended Mitigation Steps

It’s not easy to fix this issue with the current implementation of the contract. There are a number of other issues. But, in essence:

  • determine the remaining amount of tokens that need to be distributed
  • calculate the new endTimestamp: endTimestamp = remainingAmount / newRewardsPerSecond

RedVeil (Popcorn) confirmed, but disagreed with severity

LSDan (judge) decreased severity to Medium


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

Likely the function adds the remaining reward amount to the new rate multiplied by the remaining time, instead of using only the remaining tokens to compute the new end time.

Impact

The contract will try to distribute more reward tokens than it holds, breaking the reward distribution and potentially preventing users from claiming their rewards.

What the attacker needed

Existing reward with a set end time; call to changeRewardSpeed

The fix

The function should determine the remaining amount of tokens to distribute and calculate the new end timestamp as remainingAmount divided by the new rewardsPerSecond.

How it was found

  1. Set up a reward with startTimestamp 0, endTimestamp 100, rewardPerSecond 2, and initialBalance 200.
  2. At timestamp 50, call changeRewardSpeed to set rewardPerSecond to 4.
  3. Observe that the new endTimestamp becomes 125 instead of a smaller value, so the contract schedules more rewards than it holds.

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 promises to pay out a fixed number of reward tokens over a set period. When the reward rate is changed, the contract should recalculate when the rewards will run out based on how many tokens are left. Instead, it adds the old remaining rewards to the new rate, making the end time later and promising more tokens than the contract actually has.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by Ruhum

View all

55 more disclosed reports, best first.

More from Popcorn contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.