Skip to content
Business logic Audit finding Intermediate

StakingRewards.sol#notifyRewardAmount() Improper reward balance checks can make some users unable to withdraw their rewards

The notifyRewardAmount function only checks that the contract's current reward token balance covers the new reward rate, ignoring rewards already owed to users but not yet withdrawn. If the rewards distributor calls notifyRewardAmount again without sending new tokens, the contract can become insolvent, causing users' getReward calls to fail.

cccz Follow

Originally disclosed on Code4rena

Submitted by cccz, also found by csanuragjain and pashov

Similar to https://github.com/code-423n4/2022-02-concur-findings/issues/209

        uint256 balance = rewardsToken.balanceOf(address(this));
        require(
            rewardRate <= balance.div(rewardsDuration),
            "Provided reward too high"
        );

In the current implementation, the contract only checks if balanceOf rewardsToken is greater than or equal to the future rewards.

However, under normal circumstances, since users can not withdraw all their rewards in time, the balance in the contract contains rewards that belong to the users but have not been withdrawn yet. This means the current checks can not be sufficient enough to make sure the contract has enough amount of rewardsToken.

As a result, if the rewardsDistribution mistakenly notifyRewardAmount with a larger amount, the contract may end up in a wrong state that makes some users unable to claim their rewards.

Given:

  • rewardsDuration = 7 days;
  • Alice stakes 1,000 stakingToken;
  • rewardsDistribution sends 100 rewardsToken to the contract;
  • rewardsDistribution calls notifyRewardAmount() with amount = 100;
  • 7 days later, Alice calls earned() and it returns 100 rewardsToken, but Alice choose not to getReward() for now;
  • rewardsDistribution calls notifyRewardAmount() with amount = 100 without send any fund to contract, the tx will succeed;
  • 7 days later, Alice calls earned() 200 rewardsToken, when Alice tries to call getReward(), the transaction will fail due to insufficient balance of rewardsToken.

Expected Results:

The tx in step 5 should revert.

Proof of Concept

https://github.com/code-423n4/2022-09-y2k-finance/blob/ac3e86f07bc2f1f51148d2265cc897e8b494adf7/src/rewards/StakingRewards.sol#L201-L205

Recommended Mitigation Steps

Consider changing the function notifyRewardAmount to addRward and use transferFrom to transfer rewardsToken into the contract:

function addRward(uint256 reward)
    external
    updateReward(address(0))
{
    require(
        msg.sender == rewardsDistribution,
        "Caller is not RewardsDistribution contract"
    );

    if (block.timestamp >= periodFinish) {
        rewardRate = reward / rewardsDuration;
    } else {
        uint256 remaining = periodFinish - block.timestamp;
        uint256 leftover = remaining * rewardRate;
        rewardRate = (reward + leftover) / rewardsDuration;
    }

    rewardsToken.safeTransferFrom(msg.sender, address(this), reward);

    lastUpdateTime = block.timestamp;
    periodFinish = block.timestamp + rewardsDuration;
    emit RewardAdded(reward);
}

MiguelBits (Y2K Finance) disputed


The original report as published on Code4rena, by cccz. 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 contract checks only the current token balance against the new reward rate, without accounting for rewards already earned but not yet claimed by users.

Impact

If the rewards distributor mistakenly calls notifyRewardAmount with a larger amount without sending new tokens, the contract may become insolvent, causing some users to be unable to claim their rewards.

What the attacker needed

RewardsDistribution role; mistakenly calls notifyRewardAmount without sending tokens

The fix

The function should be changed to transfer the reward tokens into the contract via transferFrom when notifying the reward amount, ensuring the contract actually holds the new funds.

How it was found

  1. Alice stakes 1,000 stakingToken.
  2. rewardsDistribution sends 100 rewardsToken to the contract.
  3. rewardsDistribution calls notifyRewardAmount() with amount = 100.
  4. 7 days later, Alice calls earned() and it returns 100 rewardsToken, but Alice chooses not to getReward() for now.
  5. rewardsDistribution calls notifyRewardAmount() with amount = 100 without sending any fund to contract; the tx succeeds.
  6. 7 days later, Alice calls earned() and gets 200 rewardsToken; when Alice tries to call getReward(), the transaction fails due to insufficient balance of rewardsToken.

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 rewards over time. It checks it has enough tokens for the new reward rate, but forgets that some tokens are already promised to users who haven't collected them yet. So the contract can promise more than it holds, and later users can't withdraw their rewards.

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 cccz

View all

104 more disclosed reports, best first.

More from Y2k Finance 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.