Skip to content
Business logic Audit finding Intermediate

ts.tokens sometimes calculated incorrectly

The staking contract fails to update the last interaction timestamp when a user stakes after having withdrawn all tokens, because the update condition requires a non-zero token balance. This causes the next withdrawal to calculate the streamed token amount using an outdated timestamp, allowing the user to withdraw more tokens than entitled.

gpersoon Follow

Originally disclosed on Code4rena

Submitted by gpersoon, also found by WatchPug

Impact

Suppose someone stakes some tokens and then withdraws all of his tokens (he can still withdraw). This will result in ts.tokens being 0.

Now after some time he stakes some tokens again. At the second stake updateStream() is called and the following if condition is false because ts.tokens==0

  if (acctTimeDelta > 0 && ts.tokens > 0) {

Thus ts.lastUpdate is not updated and stays at the value from the first withdraw. Now he does a second withdraw. updateStream() is called an calculates the updated value of ts.tokens. However it uses ts.lastUpdate, which is the time from the first withdraw and not from the second stake. So the value of ts.token is calculated incorrectly. Thus more tokens can be withdrawn than you are supposed to be able to withdraw.

Proof of Concept

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

function stake(uint112 amount) public lock updateStream(msg.sender) {
    ...
    uint112 trueDepositAmt = uint112(newBal - prevBal);
    ...
    ts.tokens += trueDepositAmt;

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

function withdraw(uint112 amount) public lock updateStream(msg.sender) {
    ...
    ts.tokens -= amount;

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

function updateStreamInternal(address who) internal {
...
uint32 acctTimeDelta = uint32(block.timestamp) - ts.lastUpdate;
    if (acctTimeDelta > 0 && ts.tokens > 0) {
        // some time has passed since this user last interacted
        // update ts not yet streamed
        ts.tokens -= uint112(acctTimeDelta * ts.tokens / (endStream - ts.lastUpdate));
        ts.lastUpdate = uint32(block.timestamp);
    }

Recommended Mitigation Steps

Change the code in updateStream() to:

if (acctTimeDelta > 0 ) {
    // some time has passed since this user last interacted
    // update ts not yet streamed
    if (ts.tokens > 0)
            ts.tokens -= uint112(acctTimeDelta * ts.tokens / (endStream - ts.lastUpdate));
    ts.lastUpdate = uint32(block.timestamp);  // always update ts.lastUpdate (if time has elapsed)
}

Note: the next if statement with unstreamed and lastUpdate can be changed in a similar way to save some gas

brockelmore (Streaming Protocol) confirmed:

Nice catch :)

The original report as published on Code4rena, by gpersoon. 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 condition for updating the last interaction timestamp requires a non-zero token balance, so staking after a full withdrawal leaves the timestamp stale.

What the attacker needed

A staking account with tokens

The fix

The code should be changed so that ts.lastUpdate is always updated when time has elapsed, regardless of the token balance.

How it was found

  1. Stake some tokens.
  2. Withdraw all tokens, resulting in ts.tokens being 0.
  3. Stake tokens again; updateStream() is called but the condition is false because ts.tokens==0, so ts.lastUpdate is not updated.
  4. Withdraw again; updateStream() calculates ts.tokens using the old ts.lastUpdate from the first withdrawal, leading to an incorrect value.

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 staking contract tracks how many tokens a user has and when they last interacted. When a user stakes after emptying their balance, the contract skips updating the 'last interacted' time because the balance is zero. Later, when the user withdraws, the contract uses the old time to calculate how many tokens should have been released, giving the user too many tokens.

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 gpersoon

View all

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