Skip to content
Audit finding Intermediate

Possible rounding during the reward calculation

The reward calculation in a staking contract rounds down when converting reward amounts to per-share values, causing a loss of rewards that become locked in the contract. An attacker could exploit this by triggering frequent reward claims, potentially causing users to lose up to 50% of their rewards.

auditor0517 Follow

Originally disclosed on Code4rena

Submitted by auditor0517

Some rewards might be locked inside the contract due to the rounding loss.

Proof of Concept

_claimAndSyncRewards() claimed the rewards from the staking contract and tracks rewardsPerShare with the current supply.

    function _claimAndSyncRewards() internal virtual {
        uint256 _totalSupply = totalSupply();
        if (_totalSupply == 0) {
            return;
        }
        _claimAssetRewards();
        uint256 balanceAfterClaimingRewards = rewardToken.balanceOf(address(this));

        uint256 _rewardsPerShare = rewardsPerShare;
        uint256 _previousBalance = lastRewardBalance;

        if (balanceAfterClaimingRewards > _previousBalance) {
            uint256 delta = balanceAfterClaimingRewards - _previousBalance;
            // {qRewards/share} += {qRewards} * {qShare/share} / {qShare}
            _rewardsPerShare += (delta * one) / _totalSupply; //@audit possible rounding loss
        }
        lastRewardBalance = balanceAfterClaimingRewards;
        rewardsPerShare = _rewardsPerShare;
    }

It uses one as a multiplier and from this setting we know it has the same decimals as underlying(thus totalSupply).

My concern is _claimAndSyncRewards() is called for each deposit/transfer/withdraw in _beforeTokenTransfer() and it will make the rounding problem more serious.

  1. Let’s consider underlyingDecimals = 18. totalSupply = 10**6 with 18 decimals, rewardToken has 6 decimals. And total rewards for 1 year are 1M rewardToken for 1M totalSupply.
  2. With the above settings, _claimAndSyncRewards() might be called every 1 min due to the frequent user actions.
  3. Then expected rewards for 1 min are 1000000 / 365 / 24 / 60 = 1.9 rewardToken = 1900000 wei.
  4. During the division, it will be 1900000 * 10**18 / (1000000 * 10**18) = 1.

So users would lose almost 50% of rewards due to the rounding loss and these rewards will be locked inside the contract.

Recommended Mitigation Steps

I think there would be 2 mitigations.

  1. Use a bigger multiplier.
  2. Keep the remainders and use them next time in _claimAndSyncRewards() like this.
    if (balanceAfterClaimingRewards > _previousBalance) {
        uint256 delta = balanceAfterClaimingRewards - _previousBalance; //new rewards
        uint256 deltaPerShare = (delta * one) / _totalSupply; //new delta per share

        // decrease balanceAfterClaimingRewards so remainders can be used next time
        balanceAfterClaimingRewards = _previousBalance + deltaPerShare * _totalSupply / one;

        _rewardsPerShare += deltaPerShare;
    }
    lastRewardBalance = balanceAfterClaimingRewards;

Assessed type

Math

tbrent (Reserve) confirmed and commented:

Mitigation option #2 seems quite good.

Reserve Mitigated:

Roll over remainder to next call. PR: https://github.com/reserve-protocol/protocol/pull/896

Status: Mitigation confirmed. Full details in reports from ronnyx2017, RaymondFam and bin2chen.


The original report as published on Code4rena, by auditor0517. 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 integer division truncation when computing rewards per share, with no mechanism to carry over the remainder.

Impact

Users could lose almost 50% of their rewards, which become locked in the contract.

What the attacker needed

Frequent reward claims; specific token decimals

The fix

The issue was fixed by rolling over the remainder to the next call, as confirmed by the mitigation.

How it was found

  1. Set up a scenario with 18-decimal underlying token, total supply of 1e6 tokens, and a 6-decimal reward token.
  2. Assume total yearly rewards of 1M reward tokens for the 1M total supply.
  3. Trigger _claimAndSyncRewards frequently, e.g., every minute, through user actions.
  4. Observe that the per-share reward calculation rounds down, losing a significant portion of rewards.

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 a contract calculates how much reward each person should get, it does a division that can drop small remainders. If this happens often, those lost fractions add up and get stuck in the contract, so users receive less than they should.

More by auditor0517

View all

11 more disclosed reports, best first.

More from Reserve Core

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.