Skip to content
Business logic Audit finding Intermediate

Unstaked Tokens Included in Excess Reward Calculation Can Cause DoS for Unstaking Users

The contract's excess-reward calculation counts tokens that users have started unstaking but which are still held during a vesting period, so those tokens can be sent to the vault as if they were surplus. A user who later tries to withdraw after vesting finds the contract short of tokens, blocking the withdrawal.

x0bserver Follow

Originally disclosed on Immunefi

Critical View original
  • Disclosed after under an hour

#42723 [SC-Critical] Unstaked Tokens Included in Excess Reward Calculation Can Cause DoS for Unstaking Users

Submitted on Mar 25th 2025 at 13:45:07 UTC by @x0bserver for Audit Comp | Yeet

Description

Description

The StakeV2 contract's accumulatedDeptRewardsYeet function is designed to return the undistributed rewards, i.e., the excess rewards. However, the function does not properly account for users who have initiated unstaking. When a user calls the startUnstake function, their tokens are not immediately transferred out of the StakeV2 contract due to the vesting period. Instead, these tokens are only deducted from totalSupply:

Solidity
    function startUnstake(uint256 unStakeAmount) external {  
        require(unStakeAmount > 0, "Amount must be greater than 0");  
        require(stakedTimes[msg.sender] < STAKING_LIMIT, "Amount must be less than the STAKING_LIMIT constant"); // DOS protection  
        _updateRewards(msg.sender);  
        uint256 amount = balanceOf[msg.sender];  
        require(amount >= unStakeAmount, "Insufficient balance");  

        balanceOf[msg.sender] -= unStakeAmount;  
@>      totalSupply -= unStakeAmount;  

This means the user's unstaked tokens remain in the contract until the end of the vesting period. After this period, the user can withdraw their tokens through a separate transaction using the _unstake function:

Solidity
    function _unstake(uint256 index) private {  
        Vesting memory vesting = vestings[msg.sender][index];  

        (uint256 unlockedAmount, uint256 lockedAmount) = calculateVesting(vesting);  
        require(unlockedAmount != 0, "No unlocked amount");  

@>      stakingToken.transfer(msg.sender, unlockedAmount);  

This process creates a time gap between when the tokens are deducted from totalSupply and when they are actually withdrawn from the contract. During this gap, the unstaked tokens are still present in the contract. While this alone isn't problematic, the accumulatedDeptRewardsYeet function fails to account for these tokens.

This function is called during the executeRewardDistributionYeet process, which distributes excess rewards to the vault. The function calculates the undistributed rewards using the following logic:

Solidity
    function accumulatedDeptRewardsYeet() public view returns (uint256) {  
        return stakingToken.balanceOf(address(this)) - totalSupply;  
    }  

Due to the aforementioned gap, this function incorrectly considers tokens in the vesting period as excess rewards. As a result, these tokens can be transferred to the vault via the executeRewardDistributionYeet function. This miscalculation DoSes the unstaking process, preventing users from withdrawing their tokens after the vesting period ends.

Impact

Affected users will be unable to withdraw their staked tokens after the vesting period, causing denial of service (DoS). Additionally, excess tokens meant for users will be misdirected to the vault.

Mitigation

Update the accumulatedDeptRewardsYeet function to exclude tokens in the vesting period from the excess reward calculation. This can be achieved by maintaining a separate record of unstaked tokens currently in vesting and subtracting them from the total contract balance:

Solidity
function accumulatedDeptRewardsYeet() public view returns (uint256) {  
    return stakingToken.balanceOf(address(this)) - totalSupply - totalVestingAmount;  
}  

Here, totalVestingAmount should track the sum of all tokens currently in the vesting process. This ensures that only actual excess rewards are distributed to the vault, preventing DoS during unstaking.

Proof of Concept

Attack Scenario

  1. User A stakes tokens in the StakeV2 contract and decides to unstake.
  2. User A calls the startUnstake function. The tokens are deducted from totalSupply, but they remain locked in the contract during the vesting period.
  3. The executeRewardDistributionYeet function is later called, which uses the accumulatedDeptRewardsYeet function to calculate excess rewards.
  4. The function mistakenly considers User A's unstaked but still-locked tokens as excess and transfers them to the vault.
  5. After the vesting period, User A tries to withdraw their tokens via _unstake but finds that the contract lacks sufficient balance due to the previous incorrect reward distribution.

The original report as published on Immunefi, by x0bserver. Images and videos are our own copies; raw HTML is shown as text. View it on Immunefi.

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-reward calculation subtracts only totalSupply from the contract balance, ignoring tokens that were removed from totalSupply but remain in the contract during vesting.

Impact

Affected users could be unable to withdraw their tokens after vesting, and user tokens may be misdirected to the vault.

What the attacker needed

User has staked tokens; user starts unstaking; reward distribution is triggered

The fix

The excess-reward calculation should also subtract a tracked total of tokens currently in vesting.

How it was found

  1. A user stakes tokens and then calls startUnstake, which deducts the amount from totalSupply while the tokens stay in the contract.
  2. The reward distribution function is called and uses the excess-reward calculation.
  3. The calculation treats the vesting tokens as excess and transfers them to the vault.
  4. After vesting, the user tries to withdraw but the contract lacks the 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.

When someone unstakes, their tokens are not sent straight away; they sit in the contract for a waiting period. The contract works out how many 'extra' tokens it holds by subtracting the staked total from its balance, but it forgets the tokens waiting to be collected. So it can hand those waiting tokens to the vault, and the user cannot get them back.

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 from Yeet

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.