Unstaked tokens incorrectly counted as rewards during vesting period
An accounting error in the StakeV2 contract causes tokens that are in the vesting period after unstaking to be counted as available rewards. When the manager distributes rewards, these vesting tokens are paid out, leaving the contract unable to cover other stakers' principal.
Originally disclosed on Immunefi
- Disclosed after under an hour
#41521 [SC-Critical] Unstaked tokens incorrectly counted as rewards during vesting period
Submitted on Mar 16th 2025 at 07:55:16 UTC by @merlinboii for Audit Comp | Yeet
- Report ID: #41521
- Report Type: Smart Contract
- Report severity: Critical
- Target: https://github.com/immunefi-team/audit-comp-yeet/blob/main/src/StakeV2.sol
- Impacts:
- Direct theft of any user funds, whether at-rest or in-motion, other than unclaimed yield
Description
Brief/Intro
An accounting error in StakeV2 causes unstaked tokens in vesting to be incorrectly counted as rewards. This could lead to unintended loss of funds for stakers and potential insolvency for the protocol.
Vulnerability Details
The accumulatedDeptRewardsYeet() function does not account for unstaked tokens during the vesting period:
function accumulatedDeptRewardsYeet() public view returns (uint256) {
@> return stakingToken.balanceOf(address(this)) - totalSupply;
}
Since totalSupply is reduced immediately for the unstakeAmount at startUnstake(), while the unstaked amount remains in the contract during vesting, accumulatedDeptRewardsYeet() mistakenly includes this unstaked amount as part of the rewards. Consequently, when executeRewardDistributionYeet() is called, these mistakenly counted rewards are distributed to stakers, leading to an unintended loss of funds.
function startUnstake(uint256 unStakeAmount) external {
--- SNIPPED ---
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);
}
Impact Details
Consider the following scenario:
0. Users A and B each stake 100e18 YEET
- Contract State:
- YEET.balance(StakeV2): 200e18
- totalSupply: 200e18
- Real rewards: 0
- B starts unstaking 100e18 YEET
- Contract State:
YEET.balance(StakeV2): 200e18 (unchanged)totalSupply: 100e18 (reduced by B's unstake)- Tokens in vesting: 100e18 (B's unstaking amount)
- Contract State:
- Manager Distributes "Rewards":
accumulatedDeptRewardsYeet()returns: 200e18 - 100e18 = 100e18- These rewards (actually B's vesting tokens) are distributed
- Contract State:
YEET.balance(StakeV2): 100e18totalSupply: 100e18- Tokens distributed as "rewards": 100e18
- B finalizes unstake at vesting period ends and withdraws their 100e18 YEET
- Final Contract State:
YEET.balance(StakeV2): 0totalSupply: 100e18 (A's stake)- Result: A's stake becomes unbacked by tokens
- Final Contract State:
This scenario demonstrates the following impacts:
- User A suffers a complete loss of their 100e18 YEET stake as they cannot unstake due to insufficient contract balance
- The protocol becomes technically insolvent as it owes User A 100e18 YEET but has 0 balance
- The lack of vesting token tracking allows the manager to unknowingly distribute vesting tokens as rewards through
accumulatedDeptRewardsYeet(), creating a HIGH likelihood.
The severity assessment
This issue qualifies as Direct theft of any user funds because:
- Stakers lose their entire principal investment
- The loss occurs while funds are at-rest in the staking contract
- The vulnerability allows one user (B) to profit at the expense of another (A)
References
https://github.com/immunefi-team/audit-comp-yeet/blob/main/src/StakeV2.sol#L247-L262\\ https://github.com/immunefi-team/audit-comp-yeet/blob/main/src/StakeV2.sol#L148-L150
Proof of Concept
Proof of Concept
- Users A and B each stake 100e18 YEET
- Contract State:
YEET.balance(StakeV2): 200e18totalSupply: 200e18- Real rewards: 0
- Contract State:
- B starts unstaking 100e18 YEET
- Contract State:
YEET.balance(StakeV2): 200e18 (unchanged)totalSupply: 100e18 (reduced by B's unstake)- Tokens in vesting: 100e18 (B's unstaking amount)
- Contract State:
- Manager Distributes "Rewards":
accumulatedDeptRewardsYeet()returns: 200e18 - 100e18 = 100e18- These rewards (actually B's vesting tokens) are distributed
- Contract State:
YEET.balance(StakeV2): 100e18totalSupply: 100e18- Tokens distributed as "rewards": 100e18
- B finalizes unstake at vesting period ends and withdraws their 100e18 YEET
- Final Contract State:
YEET.balance(StakeV2): 0totalSupply: 100e18 (A's stake)- Result: A's stake becomes unbacked by tokens
- Final Contract State:
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 reward calculation uses the contract's token balance minus totalSupply, but totalSupply is reduced immediately on unstake while the tokens remain in the contract during vesting, so vesting tokens are treated as rewards.
Impact
The remaining staker's principal becomes unbacked and cannot be withdrawn, making the protocol insolvent; the report states this could lead to unintended loss of funds for stakers.
What the attacker needed
At least two stakers; one staker starts unstaking; manager distributes rewards during vesting
The fix
The contract should track tokens that are in vesting separately and exclude them from the reward calculation, for example by subtracting the total vesting amount from the contract balance before computing rewards.
How it was found
- Two users each stake 100e18 YEET, so the contract holds 200e18 and totalSupply is 200e18.
- One user starts unstaking 100e18 YEET; totalSupply drops to 100e18 but the contract balance stays at 200e18.
- The manager calls the reward distribution, and accumulatedDeptRewardsYeet returns 100e18, which is paid out as rewards.
- The unstaking user withdraws their 100e18 after vesting, leaving the contract with 0 balance while totalSupply is still 100e18.
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 locked for a while before they can withdraw them. The contract still holds those tokens but no longer counts them as part of the total staked supply. The reward calculation subtracts the total supply from the contract's token balance, so the locked tokens look like extra rewards. If the manager distributes rewards at that moment, those locked tokens get paid out to stakers, and the contract no longer has enough tokens to let everyone withdraw.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.