Skip to content
Business logic Audit finding Intermediate

recoverTokens doesn’t work when isSale is true

The recoverTokens function incorrectly calculates excess deposit tokens when isSale is true and creatorClaimSoldTokens has been called, because redeemedDepositTokens remains zero and depositTokenAmount is unchanged. This causes recoverTokens to revert or send fewer tokens than intended, potentially locking deposit tokens in the contract.

harleythedog Follow

Originally disclosed on Code4rena

Submitted by harleythedog, also found by kenzo, pedroais, hyh, and pauliax

Impact

In recoverTokens, the logic to calculate the excess number of deposit tokens in the contract is:

uint256 excess = ERC20(token).balanceOf(address(this)) - (depositTokenAmount - redeemedDepositTokens);

This breaks in the case where isSale is true and the deposit tokens have already been claimed through the use of creatorClaimSoldTokens. In this case, redemeedDepositTokens will be zero, and depositTokenAmount will still be at its original value when the streaming ended. As a result, any attempts to recover deposit tokens from the contract would either revert or send less tokens than should be sent, since the logic above would still think that there are the full amount of deposit tokens in the contract. This breaks the functionality of the function completely in this case.

Proof of Concept

See the excess calculation here: https://github.com/code-423n4/2021-11-streaming/blob/56d81204a00fc949d29ddd277169690318b36821/Streaming/src/Locke.sol#L654

See creatorClaimSoldTokens here: https://github.com/code-423n4/2021-11-streaming/blob/56d81204a00fc949d29ddd277169690318b36821/Streaming/src/Locke.sol#L583

Notice that creatorClaimSoldTokens does not change depositTokenAmount or redeemedDepositTokens, so the excess calculation will be incorrect in the case of sales.

Tools Used

Inspection

Recommended Mitigation Steps

I would recommend setting redeemedDepositTokens to be depositTokenAmount in the function creatorClaimSoldTokens, since claiming the sold tokens is like “redeeming” them in a sense. This would fix the logic issue in recoverTokens.

brockelmore (Streaming Protocol) commented

0xean (judge) commented:

upgrading to High as assets would be lost in the case outlined by the warden

3 — High: Assets can be stolen/lost/compromised directly (or indirectly if there is a valid attack path that does not have hand-wavy hypotheticals).

The original report as published on Code4rena, by harleythedog. 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 excess calculation in recoverTokens does not account for deposit tokens already claimed via creatorClaimSoldTokens, because that function does not update redeemedDepositTokens or depositTokenAmount.

Impact

Deposit tokens could be lost or stuck in the contract because recoverTokens fails to return the correct amount.

What the attacker needed

IsSale is true; creatorClaimSoldTokens has been called

The fix

The fix should set redeemedDepositTokens to depositTokenAmount inside creatorClaimSoldTokens, so that the excess calculation in recoverTokens becomes correct.

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.

This is a bug in a smart contract that handles token sales. When the sale is active and the creator has claimed the sold tokens, a function meant to recover leftover deposit tokens miscalculates the balance, so it either fails or sends too few tokens, leaving funds stuck.

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 harleythedog

View all

14 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.