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.
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
1000reward tokens throughfundStream(1000). ThenrewardTokenAmount = 1000 - The stream and reward lock period is over, i.e.
block.timestamp > endRewardLock - The user claims their full reward and receives
1000reward tokens by callingclaimReward(). The reward contract balance is now0butrewardTokenAmount = 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.
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
- Fund 1000 reward tokens via fundStream, setting rewardTokenAmount to 1000.
- Wait until the stream and reward lock period is over.
- User claims full reward via claimReward, receiving 1000 tokens; contract balance becomes 0 but rewardTokenAmount stays 1000.
- Someone accidentally sends 1000 reward tokens to the contract.
- 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.