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.
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
function stake(uint112 amount) public lock updateStream(msg.sender) {
...
uint112 trueDepositAmt = uint112(newBal - prevBal);
...
ts.tokens += trueDepositAmt;
function withdraw(uint112 amount) public lock updateStream(msg.sender) {
...
ts.tokens -= amount;
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 :)
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
- Stake some tokens.
- Withdraw all tokens, resulting in ts.tokens being 0.
- Stake tokens again; updateStream() is called but the condition is false because ts.tokens==0, so ts.lastUpdate is not updated.
- 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.