Skip to content
Business logic Audit finding Intermediate

Incorrect handling of total staked funds will lead to protocol insolvency

The staking contract reduces its recorded total supply as soon as a user starts unstaking, even though the tokens stay in the contract during the vesting period. The reward calculation then treats those pending withdrawal tokens as surplus rewards and distributes them to other stakers, leaving nothing for the original user to withdraw.

dobrevaleri Follow

Originally disclosed on Immunefi

Critical View original
  • Disclosed after under an hour

#42518 [SC-Critical] Incorrect handling of total staked funds will lead to protocol insolvency

Submitted on Mar 24th 2025 at 13:23:43 UTC by @dobrevaleri for Audit Comp | Yeet

Description

Brief/Intro

The StakeV2::executeRewardDistributionYeet() function incorrectly distributes pending withdrawals as rewards to remaining stakers, leading to protocol insolvency.

Vulnerability Details

The root cause lies in how totalSupply is decremented when users initiate withdrawals, while those tokens remain in the contract during the vesting period.

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 then the STAKING_LIMIT constant"); // DOS protection https://github.com/Enigma-Dark/Yeet/issues/12
        _updateRewards(msg.sender);
        uint256 amount = balanceOf[msg.sender];
        require(amount >= unStakeAmount, "Insufficient balance");

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

        uint256 start = block.timestamp;
        uint256 end = start + VESTING_PERIOD;
        vestings[msg.sender].push(Vesting(unStakeAmount, start, end));
        stakedTimes[msg.sender]++;
        emit VestingStarted(msg.sender, unStakeAmount, vestings[msg.sender].length - 1);
    }

$YEET tokens as rewards are distributed as:

Solidity
    function executeRewardDistributionYeet(
        IZapper.SingleTokenSwap calldata swap,
        IZapper.KodiakVaultStakingParams calldata stakingParams,
        IZapper.VaultDepositParams calldata vaultParams
    ) external onlyManager nonReentrant {
@>      uint256 accRevToken0 = accumulatedDeptRewardsYeet();
        require(accRevToken0 > 0, "No rewards to distribute");
        require(swap.inputAmount <= accRevToken0, "Insufficient rewards to distribute");

        stakingToken.approve(address(zapper), accRevToken0);
        IERC20 token0 = IKodiakVaultV1(stakingParams.kodiakVault).token0();  //KodiakVault - KodiakIsland - 
        IERC20 token1 = IKodiakVaultV1(stakingParams.kodiakVault).token1();

        uint256 vaultSharesMinted;
        require(
            address(token0) == address(stakingToken) || address(token1) == address(stakingToken),
            "Neither token0 nor token1 match staking token"
        );

        if (address(token0) == address(stakingToken)) {
            (, vaultSharesMinted) = zapper.zapInToken0(swap, stakingParams, vaultParams);
        } else {
            (, vaultSharesMinted) = zapper.zapInToken1(swap, stakingParams, vaultParams);
        }

        _handleVaultShares(vaultSharesMinted);
        emit RewardsDistributedToken0(accRevToken0, rewardIndex);
    }

The accumulatedDeptRewardsYeet() function calculates rewards as:

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

When users call startUnstake(), their tokens are subtracted from totalSupply but remain in the contract until the vesting period ends. This causes accumulatedDeptRewardsYeet() to incorrectly count pending withdrawal tokens as distributable rewards.

Impact

Incorrect handling of the totalSupply will lead to distribution of user's stake as rewards, which will lead to insolvency.

Proof of Concept

Proof of Concept

  1. User A stakes 1000 YEET tokens
  2. User A calls startUnstake(1000)
    • totalSupply decreases by 1000
    • Tokens remain in contract during 10 day vesting
  3. During vesting period, manager calls executeRewardDistributionYeet()
    • accumulatedDeptRewardsYeet() returns 1000 (contract balance - totalSupply)
    • The 1000 tokens are distributed to other stakers as rewards
  4. After vesting period, User A tries to withdraw but contract lacks funds

The original report as published on Immunefi, by dobrevaleri. 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 total supply is decremented at the start of unstaking while the tokens remain in the contract, so the reward calculation mistakes pending withdrawals for surplus rewards.

Impact

User stake can be paid out as rewards to other stakers, which could make the protocol insolvent and leave withdrawing users unable to recover their tokens.

What the attacker needed

A staked position; a manager triggers reward distribution; distribution occurs during vesting

The fix

The reward calculation should exclude tokens that are pending withdrawal, for example by only reducing the recorded total supply once the vesting period ends or by tracking pending withdrawals separately.

How it was found

  1. A user stakes tokens in the contract.
  2. The user starts an unstake, which lowers the recorded total supply while the tokens stay in the contract during vesting.
  3. During the vesting period a manager triggers reward distribution.
  4. The reward calculation counts the pending withdrawal tokens as rewards and distributes them to other stakers.
  5. After vesting the original user cannot withdraw because the contract lacks funds.

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 asks to take their staked tokens back, the contract immediately lowers its internal count of staked tokens, but the actual tokens stay in the contract for a waiting period. The reward calculation compares the real token balance with that lowered count, so it thinks the waiting tokens are spare rewards and hands them out. The original user then cannot get their tokens 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 by dobrevaleri

View all

13 more disclosed reports, best first.

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.