Skip to content
Business logic Audit finding Intermediate

Wrong calculation of excess depositToken allows stream creator to retrieve depositTokenFlashloanFeeAmount , which may cause fund loss to users

The excess deposit token calculation did not exclude the flashloan fee amount, allowing the stream creator to withdraw that fee. This could cause the protocol governance's claimFees to fail or force other users to lose part of their deposits.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug, also found by 0x0x0x, ScopeLift, gpersoon, harleythedog, hyh, gzeon, jonah1005, and kenzo

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

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

In the current implementation, depositTokenFlashloanFeeAmount is not excluded when calculating excess depositToken. Therefore, the stream creator can call recoverTokens(depositToken, recipient) and retrieve depositTokenFlashloanFeeAmount if there are any.

As a result:

  • When the protocol governance calls claimFees() and claim accumulated depositTokenFlashloanFeeAmount, it may fail due to insufficient balance of depositToken.
  • Or, part of users’ funds (depositToken) will be transferred to the protocol governance as fees, causing some users unable to withdraw or can only withdraw part of their deposits.

Proof of Concept

Given:

  • feeEnabled: true
  • feePercent: 10 (0.1%)
  • Alice deposited 1,000,000 depositToken;
  • Bob called flashloan() and borrowed 1,000,000 depositToken, then repaid 1,001,000;
  • Charlie deposited 1,000 depositToken;
  • After endDepositLock, Alice called claimDepositTokens() and withdrawn 1,000,000 depositToken;
  • streamCreator called recoverTokens(depositToken, recipient) and retrieved 1,000 depositToken (2,000 - (1,001,000 - 1,000,000));
  • governance called claimFees() and retrieved another 1,000 depositToken;
  • Charlie tries to claimDepositTokens() but since the current balanceOf depositToken is 0, the transcation always fails, and Charlie loses all the depositToken.

Recommendation

Change to:

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

brockelmore (Streaming Protocol) confirmed

The original report as published on Code4rena, by WatchPug. 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 excess calculation subtracted only the net deposit token amount, not the accumulated flashloan fee, so the fee was treated as excess and recoverable by the stream creator.

Impact

The protocol governance's claimFees may fail due to insufficient balance, or part of users' depositToken may be transferred to governance as fees, causing some users to be unable to withdraw or only withdraw part of their deposits.

What the attacker needed

FeeEnabled is true; stream creator can call recoverTokens; flashloan fee has accumulated

The fix

The excess calculation should also subtract depositTokenFlashloanFeeAmount.

How it was found

  1. Alice deposits 1,000,000 depositToken.
  2. Bob takes a flashloan of 1,000,000 depositToken and repays 1,001,000, generating a 1,000 fee.
  3. Charlie deposits 1,000 depositToken.
  4. After the deposit lock ends, Alice claims her 1,000,000 depositToken.
  5. The stream creator calls recoverTokens and retrieves 1,000 depositToken (the excess).
  6. Governance calls claimFees and retrieves another 1,000 depositToken, leaving zero balance.

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 held users' deposit tokens and also collected a small fee from flash loans. The code that let the stream creator recover extra tokens mistakenly included the fee as extra, so the creator could take it. This meant the protocol might not have enough tokens to pay its fees or return deposits 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.

More by WatchPug

View all

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