Skip to content
Business logic Audit finding Intermediate

Reward token not correctly recovered

The recoverTokens function incorrectly calculates excess reward tokens by subtracting rewardTokenAmount, which never decreases after claims, from the contract balance. This prevents recovery of accidentally sent reward tokens because the excess underflows or is zero.

cmichel Follow

Originally disclosed on Code4rena

Submitted by cmichel, also found by GeekyLumberjack, kenzo, pedroais, and hyh

The Streaming contract allows recovering the reward token by calling recoverTokens(rewardToken, recipient).

However, the excess amount is computed incorrectly as ERC20(token).balanceOf(address(this)) - (rewardTokenAmount + rewardTokenFeeAmount):

function recoverTokens(address token, address recipient) public lock {
    if (token == rewardToken) {
        require(block.timestamp > endRewardLock, "time");

        // check what isnt claimable by depositors and governance
        // @audit-issue rewardTokenAmount increased on fundStream, but never decreased! this excess underflows
        uint256 excess = ERC20(token).balanceOf(address(this)) - (rewardTokenAmount + rewardTokenFeeAmount);
        ERC20(token).safeTransfer(recipient, excess);

        emit RecoveredTokens(token, recipient, excess);
        return;
    }
    // ...

Note that rewardTokenAmount only ever increases (when calling fundStream) but it never decreases when claiming the rewards through claimReward. However, claimReward transfers out the reward token.

Therefore, the rewardTokenAmount never tracks the contract’s reward balance and the excess cannot be computed that way.

Proof Of Concept

Assume no reward fees for simplicity and only a single user staking.

  • Someone funds 1000 reward tokens through fundStream(1000). Then rewardTokenAmount = 1000
  • The stream and reward lock period is over, i.e. block.timestamp > endRewardLock
  • The user claims their full reward and receives 1000 reward tokens by calling claimReward(). The reward contract balance is now 0 but rewardTokenAmount = 1000
  • Some fool sends 1000 reward tokens to the contract by accident. These cannot be recovered as the excess = balance - rewardTokenAmount = 0

Impact

Reward token recovery does not work.

Recommended Mitigation Steps

The claimed rewards need to be tracked as well, just like the claimed deposits are tracked. I think you can even decrease rewardTokenAmount in claimReward because at this point rewardTokenAmount is not used to update the cumulativeRewardPerToken anymore.

brockelmore (Streaming Protocol) confirmed

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

Likely the rewardTokenAmount variable is only increased on funding and never decreased on claims, so it does not reflect the actual reward token balance.

Impact

Reward token recovery does not work, so accidentally sent reward tokens cannot be recovered.

What the attacker needed

RewardTokenAmount > 0; block.timestamp > endRewardLock; excess reward tokens sent to contract

The fix

The claimed rewards should be tracked and rewardTokenAmount decreased in claimReward, similar to how claimed deposits are tracked.

How it was found

  1. Fund 1000 reward tokens via fundStream, setting rewardTokenAmount to 1000.
  2. Wait until the stream and reward lock period is over.
  3. User claims full reward via claimReward, receiving 1000 tokens; contract balance becomes 0 but rewardTokenAmount stays 1000.
  4. Someone accidentally sends 1000 reward tokens to the contract.
  5. Attempt to recover tokens; excess is computed as balance minus rewardTokenAmount, resulting in 0, so recovery fails.

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.

The contract tracks how many reward tokens were added but not how many were claimed, so its internal counter stays too high. When someone tries to recover extra tokens, the maths subtracts this inflated number and thinks there is nothing to recover, even if tokens were sent by mistake.

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 Streaming Protocol 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.