Tokens can be stolen when depositToken == rewardToken
The Streaming contract allows the deposit and reward tokens to be the same, and the recoverTokens function handles each role separately, so when both tokens are identical the creator can recover the same excess twice. This lets the stream creator steal tokens that users should be able to withdraw or claim as rewards.
Submitted by cmichel, also found by 0x0x0x, gzeon, Ruhum, gpersoon, hack3r-0m, and pauliax
The Streaming contract allows the deposit and reward tokens to be the same token.
I believe this is intended, think Sushi reward on Sushi as is the case with
xSushi.
The reward and deposit balances are also correctly tracked independently in depositTokenAmount and rewardTokenAmount. However, when recovering tokens this leads to issues as the token is recovered twice, once for deposits and another time for rewards:
function recoverTokens(address token, address recipient) public lock {
// NOTE: it is the stream creators responsibility to save
// tokens on behalf of their users.
require(msg.sender == streamCreator, "!creator");
if (token == depositToken) {
require(block.timestamp > endDepositLock, "time");
// get the balance of this contract
// check what isnt claimable by either party
// @audit-info depositTokenAmount updated on stake/withdraw/exit, redeemedDepositTokens increased on claimDepositTokens
uint256 excess = ERC20(token).balanceOf(address(this)) - (depositTokenAmount - redeemedDepositTokens);
// allow saving of the token
ERC20(token).safeTransfer(recipient, excess);
emit RecoveredTokens(token, recipient, excess);
return;
}
if (token == rewardToken) {
require(block.timestamp > endRewardLock, "time");
// check current balance vs internal balance
//
// NOTE: if a token rebases, i.e. changes balance out from under us,
// most of this contract breaks and rugs depositors. this isn't exclusive
// to this function but this function would in theory allow someone to rug
// and recover the excess (if it is worth anything)
// check what isnt claimable by depositors and governance
// @audit-info rewardTokenAmount increased on fundStream
uint256 excess = ERC20(token).balanceOf(address(this)) - (rewardTokenAmount + rewardTokenFeeAmount);
ERC20(token).safeTransfer(recipient, excess);
emit RecoveredTokens(token, recipient, excess);
return;
}
// ...
Proof Of Concept
Given recoverTokens == depositToken, Stream creator calls recoverTokens(token = depositToken, creator).
- The
tokenbalance is the sum of deposited tokens (minus reclaimed) plus the reward token amount.ERC20(token).balanceOf(address(this)) >= (depositTokenAmount - redeemedDepositTokens) + (rewardTokenAmount + rewardTokenFeeAmount) if (token == depositToken)executes, theexcessfrom the deposit amount will be the reward amount (excess >= rewardTokenAmount + rewardTokenFeeAmount). This will be transferred.if (token == rewardToken)executes, the new token balance is just the deposit token amount now (because the reward token amount has been transferred out in the step before). Therefore,ERC20(token).balanceOf(address(this)) >= depositTokenAmount - redeemedDepositTokens. If this is non-negative, the transaction does not revert and the creator makes a profit.
Example:
- outstanding redeemable deposit token amount:
depositTokenAmount - redeemedDepositTokens = 1000 - funded
rewardTokenAmount(plusrewardTokenFeeAmountfees):rewardTokenAmount + rewardTokenFeeAmount = 500
Creator receives 1500 - 1000 = 500 excess deposit and 1000 - 500 = 500 excess reward.
Impact
When using the same deposit and reward token, the stream creator can steal tokens from the users who will be unable to withdraw their profit or claim their rewards.
Recommended Mitigation Steps
One needs to be careful with using .balanceOf in this special case as it includes both deposit and reward balances.
Add a special case for recoverTokens when token == depositToken == rewardToken and then the excess should be ERC20(token).balanceOf(address(this)) - (depositTokenAmount - redeemedDepositTokens) - (rewardTokenAmount + rewardTokenFeeAmount);
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 recoverTokens function uses separate balance checks for deposit and reward tokens without accounting for the case where both are the same token, causing the same balance to be counted twice.
Impact
The stream creator can steal tokens from users, who will be unable to withdraw their deposits or claim their rewards.
What the attacker needed
DepositToken and rewardToken are the same; caller is the stream creator
The fix
Add a special case in recoverTokens for when depositToken equals rewardToken, subtracting both the outstanding deposit amount and the total reward amount from the contract balance to calculate the true excess.
How it was found
- The stream creator calls recoverTokens with the token address equal to both depositToken and rewardToken.
- The function enters the depositToken branch, calculates excess as the contract balance minus outstanding deposit tokens, and transfers that excess (which includes reward tokens) to the creator.
- The function then enters the rewardToken branch, recalculates excess using the now-reduced balance, and transfers another amount to the creator.
- The creator ends up receiving more tokens than they should, stealing from users.
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 smart contract lets someone deposit tokens and also earn rewards in tokens. If the deposit token and reward token are the same, the function that recovers leftover tokens checks the deposit case first and sends out the reward portion, then checks the reward case and sends out the deposit portion. This means the person who created the stream can take tokens that belong to users.
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.