Calling unstake() can cause locked funds
The unstake function incorrectly zeroes out the staked amount for a shorter vesting period even when no tokens from that period are removed, causing those tokens to become locked in the contract. An attacker could stake tokens for a short period, then unstake vested tokens from a longer period, resulting in the shorter-period tokens being permanently locked.
Submitted by Ruhum, also found by 0xDjango, auditor0517, dipp, GimelSec, GreyArt, p4st13r4, and wagmi
Following scenario:
Alice has staked X token for 6 months that have vested. She stakes Y tokens for another three months. If she now calls unstake(X) to take out the tokens that have vested, the Y tokens she staked for three months will be locked up.
Proof of Concept
First, here’s a test showcasing the issue:
describe('should cause trouble', () => {
it('should lock up funds', async function () {
await approveERC20(signer1.address, token.address, amountStaked, signer1, infinityStaker.address);
await infinityStaker.connect(signer1).stake(amountStaked, 2);
await network.provider.send("evm_increaseTime", [181 * DAY]);
await network.provider.send('evm_mine', []);
// The funds we staked for 6 months have vested
expect(await infinityStaker.getUserTotalVested(signer1.address)).to.eq(amountStaked);
// Now we want to stake funds for three months
await approveERC20(signer1.address, token.address, amountStaked2, signer1, infinityStaker.address);
await infinityStaker.connect(signer1).stake(amountStaked2, 1);
// total staked is now the funds staked for three & six months
// total vested stays the same
expect(await infinityStaker.getUserTotalStaked(signer1.address)).to.eq(amountStaked.add(amountStaked2));
expect(await infinityStaker.getUserTotalVested(signer1.address)).to.eq(amountStaked);
// we unstake the funds that are already vested.
const userBalanceBefore = await token.balanceOf(signer1.address);
await infinityStaker.connect(signer1).unstake(amountStaked);
const userBalanceAfter = await token.balanceOf(signer1.address);
expect(userBalanceAfter).to.eq(userBalanceBefore.add(amountStaked));
expect(await infinityStaker.getUserTotalStaked(signer1.address)).to.eq(ethers.BigNumber.from(0));
expect(await infinityStaker.getUserTotalVested(signer1.address)).to.eq(ethers.BigNumber.from(0));
});
});
The test implements the scenario I’ve described above. In the end, the user got back their amountStaked tokens with the amountStaked2 tokens being locked up in the contract. The user has no tokens staked at the end.
The issue is in the _updateUserStakedAmounts() function:
if (amount > noVesting) {
userstakedAmounts[user][Duration.NONE].amount = 0;
userstakedAmounts[user][Duration.NONE].timestamp = 0;
amount = amount - noVesting;
if (amount > vestedThreeMonths) {
// MAIN ISSUE:
// here `vestedThreeMonths` is 0. The current staked tokens are set to `0` and `amount` is decreased by `0`.
// Since `vestedThreeMonths` is `0` we shouldn't decrease `userstakedAmounts` at all here.
userstakedAmounts[user][Duration.THREE_MONTHS].amount = 0;
userstakedAmounts[user][Duration.THREE_MONTHS].timestamp = 0;
amount = amount - vestedThreeMonths;
// `amount == vestedSixMonths` so we enter the else block
if (amount > vestedSixMonths) {
userstakedAmounts[user][Duration.SIX_MONTHS].amount = 0;
userstakedAmounts[user][Duration.SIX_MONTHS].timestamp = 0;
amount = amount - vestedSixMonths;
if (amount > vestedTwelveMonths) {
userstakedAmounts[user][Duration.TWELVE_MONTHS].amount = 0;
userstakedAmounts[user][Duration.TWELVE_MONTHS].timestamp = 0;
} else {
userstakedAmounts[user][Duration.TWELVE_MONTHS].amount -= amount;
}
} else {
// the staked amount is set to `0`.
userstakedAmounts[user][Duration.SIX_MONTHS].amount -= amount;
}
} else {
userstakedAmounts[user][Duration.THREE_MONTHS].amount -= amount;
}
} else {
userstakedAmounts[user][Duration.NONE].amount -= amount;
}
Recommended Mitigation Steps
Don’t set userstakedAmounts.amount to 0 if none of its tokens are removed (vestedAmount == 0)
nneverlander (Infinity) confirmed
HardlyDifficult (judge) commented:
When unstaking, unvested tokens may become locked in the contract forever.
Accepting this as a High risk issue.
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
Likely the unstake logic sets the staked amount for a vesting duration to zero even when no tokens from that duration are actually being removed, because it does not check if the vested amount for that duration is zero before zeroing.
Impact
Unvested tokens from a shorter vesting period can become permanently locked in the contract, causing a loss of funds for the user.
What the attacker needed
User has staked tokens in multiple vesting periods; At least one period is fully vested
The fix
The contract should not set the staked amount for a vesting duration to zero when no tokens from that duration are being removed (i.e., when the vested amount for that duration is zero).
How it was found
- Stake tokens for a 6-month vesting period.
- Wait until those tokens are fully vested.
- Stake additional tokens for a 3-month vesting period.
- Call unstake with the amount of the vested 6-month tokens.
- Observe that the 3-month tokens are locked and the user's total staked becomes zero.
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.
This is a smart contract bug where a user's tokens can get stuck. When you unstake some tokens, the contract mistakenly resets the record of other tokens you staked for a different time period, so those tokens can no longer be withdrawn. The user loses access to those funds.
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.