Skip to content
Audit finding Intermediate

Precision Loss in `notifyRewardAmount` Function Causes Unclaimable RewardToken

The notifyRewardAmount function in StakedEXA calculates the reward rate by integer division, causing precision loss so that the distributed reward amount is less than the notified amount. As a result, a small portion of reward tokens remains locked in the contract and cannot be claimed.

0uts1der Follow with 0x73696d616f, Bigsam and 4 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-07-exactly-stacking-contracts-judging/issues/96

Found by

0uts1der, 0x73696d616f, Bigsam, HaxSecurity, KupiaSec, santiellena, santipu_

Summary

In the StakedEXA contract, the notifyRewardAmount function suffers from precision loss when calculating the reward rate, leading to some rewards being locked and unclaimable.

Vulnerability Detail

In the StakedEXA contract, there is a precision loss in the notifyRewardAmount function when calculating rewardData.rate, which results in some of the reward funds being locked in the contract and not being available for distribution. This leads to economic loss.

Solidity
function notifyRewardAmount(IERC20 reward, uint256 amount, address notifier) internal onlyReward(reward) {
    updateIndex(reward);
    RewardData storage rewardData = rewards[reward];
    if (block.timestamp >= rewardData.finishAt) {
      rewardData.rate = amount / rewardData.duration;
    } else {
      uint256 remainingRewards = (rewardData.finishAt - block.timestamp) * rewardData.rate;
      rewardData.rate = (amount + remainingRewards) / rewardData.duration;
    }

    if (rewardData.rate == 0) revert ZeroRate();
    if (
      rewardData.rate * rewardData.duration >
      reward.balanceOf(address(this)) - (address(reward) == asset() ? totalAssets() : 0)
    ) revert InsufficientBalance();

    rewardData.finishAt = uint40(block.timestamp) + rewardData.duration;
    rewardData.updatedAt = uint40(block.timestamp);

    emit RewardAmountNotified(reward, notifier, amount);
  }

Clearly, the formulas rewardData.rate = amount / rewardData.duration; and rewardData.rate = (amount + remainingRewards) / rewardData.duration; in the calculation of rewardData.rate cause precision loss. This results in the final reward amount rewardData.rate * rewardData.duration being less than the amount actually passed to the notifyRewardAmount function. Since there is no suitable function to extract this remaining portion of funds, it causes economic loss.

A Proof of Concept (POC) can be constructed in StakedEXA.t.sol as follows.

Solidity
function testRemaining() external {
    MockERC20 WBTC = new MockERC20("Wrapped BTC", "WBTC", 8);
    vm.label(address(WBTC), "WBTC");
    WBTC.mint(address(stEXA), 10e8);
    stEXA.enableReward(WBTC);
    stEXA.setRewardsDuration(WBTC, 1 weeks);
    stEXA.notifyRewardAmount(WBTC, 10e8);

    address Alice = address(0x1234);
    vm.startPrank(Alice);
    exa.mint(address(Alice), 1 ether);
    exa.approve(address(stEXA), 1 ether);
    stEXA.deposit(1 ether, Alice);
    
    skip(30 weeks);

    stEXA.claim(WBTC);
    
    emit log_named_decimal_uint("balance", WBTC.balanceOf(address(stEXA)), WBTC.decimals());
    emit log_named_decimal_uint("Alice reward WBTC", WBTC.balanceOf(Alice), WBTC.decimals());
    emit log_named_decimal_uint("saving WBTC", WBTC.balanceOf(SAVINGS), WBTC.decimals());
}

The output is as follows:

Shell
Logs:
  balance: 0.00265600
  Alice reward WBTC: 8.99760960
  saving WBTC: 0.99973440

Assuming WBTC as the reward token with an amount of 10 and RewardsDuration set to one week, and Alice as the only staker with a stake of 1 ether worth of EXA tokens. After 30 weeks, based on the set parameters, the reward distribution should be complete. When Alice calls the claim function, the full 10 ether worth of rewards should be distributed to Alice and savings. However, due to precision loss, 0.0026 WBTC will remain in the contract (worth over 100 USD).

Impact

The precision loss in the StakedEXA contract's notifyRewardAmount function results in a portion of the reward funds being locked in the contract and unavailable for distribution.

Code Snippet

https://github.com/sherlock-audit/2024-07-exactly-stacking-contracts/blob/main/protocol/contracts/StakedEXA.sol#L209-L229

Tool used

Manual Review

Recommendation

Add an admin function to extract the reward tokens that remain undistributed due to precision loss.

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/exactly/protocol/pull/754

sherlock-admin2

The Lead Senior Watson signed off on the fix.

The original report as published on Sherlock, by 0uts1der. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 the reward rate, so the product of rate and duration is less than the notified amount, leaving dust locked.

Impact

A small portion of reward funds becomes permanently locked in the contract and cannot be distributed or claimed, causing economic loss.

What the attacker needed

Reward token with low decimals (e.g., 8); reward amount not evenly divisible by duration; no admin function to recover dust

The fix

The protocol team fixed this issue by adding an admin function to extract the remaining undistributed reward tokens.

How it was found

  1. Deploy a mock reward token with 8 decimals and mint a balance to the staking contract.
  2. Enable the reward token and set its rewards duration to one week.
  3. Call notifyRewardAmount with the full reward amount.
  4. Deposit a stake as a user and advance time by 30 weeks.
  5. Call claim and observe that a small amount of reward token remains in the contract.

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 smart contract splits a reward among many people, it often divides the total amount by a time period to get a rate per second. If the division does not come out even, the leftover fraction is lost because the contract only stores whole numbers. Here, that leftover stays stuck in the contract forever because there is no way to withdraw it.

More by 0uts1der

View all

3 more disclosed reports, best first.

More from Exactly Protocol Update - Staking Contract

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.