Award is still distributed when there aren’t any stakers, allowing users to get reward without staking
The reward calculation in a staking contract fails to update the last calculation time when there are no stakers, allowing a user to stake and immediately withdraw to capture all rewards from the period with no stakers. This lets an attacker earn rewards without actually staking for any meaningful duration.
evan Follow with HollaDieWaldfee
Originally disclosed on Code4rena
Submitted by evan, also found by HollaDieWaldfee
Consider the update modifier for the amplifier.
modifier update() {
if (_mostRecentValueCalcTime == 0) {
_mostRecentValueCalcTime = firstStakeTime;
}
uint256 totalCurrentStake = totalStake();
if (totalCurrentStake > 0 && _mostRecentValueCalcTime < endTime) {
uint256 value = 0;
uint256 sinceLastCalc = block.timestamp.sub(_mostRecentValueCalcTime);
uint256 perSecondReward = totalRewards.div(endTime.sub(firstStakeTime));
if (block.timestamp < endTime) {
value = sinceLastCalc.mul(perSecondReward);
} else {
uint256 sinceEndTime = block.timestamp.sub(endTime);
value = (sinceLastCalc.sub(sinceEndTime)).mul(perSecondReward);
}
_totalWeight = _totalWeight.add(value.mul(10**18).div(totalCurrentStake));
_mostRecentValueCalcTime = block.timestamp;
(uint fee0, uint fee1) = IMuteSwitchPairDynamic(lpToken).claimFees();
_totalWeightFee0 = _totalWeightFee0.add(fee0.mul(10**18).div(totalCurrentStake));
_totalWeightFee1 = _totalWeightFee1.add(fee1.mul(10**18).div(totalCurrentStake));
totalFees0 = totalFees0.add(fee0);
totalFees1 = totalFees1.add(fee1);
}
_;
}
Suppose there’s been a period with totalCurrentStake = 0, and a user stakes and immediately withdraws in the same transaction.
When the user stakes, update doesn’t do anything (including updating _mostRecentValueCalcTime) since totalCurrentStake = 0, and _userWeighted[account] gets set to _totalWeight (which hasn’t been updated) in the stake function.
https://github.com/code-423n4/2023-03-mute/blob/main/contracts/amplifier/MuteAmplifier.sol#L349
function _stake(uint256 lpTokenIn, address account) private {
...
_userWeighted[account] = _totalWeight;
...
}
When the user withdraws, totalCurrentStake is no longer zero. Since _mostRecentValueCalcTime wasn’t updated when the user staked (since totalCurrentStake was 0), the reward from the period with no stakers gets added to _totalWeight.
uint256 sinceLastCalc = block.timestamp.sub(_mostRecentValueCalcTime);
value = sinceLastCalc.mul(perSecondReward);
_totalWeight = _totalWeight.add(value.mul(10**18).div(totalCurrentStake));
(These are lines in the update modifier)
As a result, this user who staked and immediately withdrew, got all the reward from the period with no stakers. See the reward calculation: https://github.com/code-423n4/2023-03-mute/blob/main/contracts/amplifier/MuteAmplifier.sol#L371
function _applyReward(address account) private returns (uint256 lpTokenOut, uint256 reward, uint256 remainder, uint256 fee0, uint256 fee1) {
...
reward = lpTokenOut.mul(_totalWeight.sub(_userWeighted[account])).div(calculateMultiplier(account, true));
...
}
Observe that the exploit condition is met as soon as the staking period starts (as long as nobody stakes immediately). The code attempts to prevent this situation setting _mostRecentValueCalcTime to firstStakeTime the first time that update is invoked (which must be a stake call). https://github.com/code-423n4/2023-03-mute/blob/main/contracts/amplifier/MuteAmplifier.sol#L89-L91
if (_mostRecentValueCalcTime == 0) {
_mostRecentValueCalcTime = firstStakeTime;
}
However, this doesn’t do anything since the user can first set the firstStakeTime by staking as soon as the staking period starts, and then make totalCurrentStake 0 by immediately withdrawing. In fact, observe that this takes away from other staker’s rewards since perSecondReward is now lower. https://github.com/code-423n4/2023-03-mute/blob/main/contracts/amplifier/MuteAmplifier.sol#L98
uint256 perSecondReward = totalRewards.div(endTime.sub(firstStakeTime));
Please add the following test to the “advance to start time” context in amplifier.ts (add it here), and run with npm run test-amplifier.
it("get reward without staking", async function () {
await this.lpToken.transfer(this.staker1.address, staker1Initial.toFixed(), {from: this.owner.address});
await this.lpToken.connect(this.staker1).approve(
this.amplifier.address,
staker1Initial.toFixed()
);
console.log("dmute balance before: " + await this.dMuteToken.balanceOf(this.staker1.address))
await this.amplifier.connect(this.staker1).stake(1);
await this.amplifier.connect(this.staker1).withdraw();
await time.increaseTo(staking_end-10);
await this.amplifier.connect(this.staker1).stake(1);
await this.amplifier.connect(this.staker1).withdraw();
console.log("dmute balance after: " + await this.dMuteToken.balanceOf(this.staker1.address))
});
it("get reward with staking", async function () {
await this.lpToken.transfer(this.staker1.address, staker1Initial.toFixed(), {from: this.owner.address});
await this.lpToken.connect(this.staker1).approve(
this.amplifier.address,
staker1Initial.toFixed()
);
console.log("dmute balance before: " + await this.dMuteToken.balanceOf(this.staker1.address))
await this.amplifier.connect(this.staker1).stake(1);
//await this.amplifier.connect(this.staker1).withdraw();
await time.increaseTo(staking_end-10);
//await this.amplifier.connect(this.staker1).stake(1);
await this.amplifier.connect(this.staker1).withdraw();
console.log("dmute balance after: " + await this.dMuteToken.balanceOf(this.staker1.address))
});
Result on my side:
advance to start time
✓ reverts without tokens approved for staking
dmute balance before: 0
dmute balance after: 576922930658129099273
✓ get reward without staking
dmute balance before: 0
dmute balance after: 576922912276064160247
✓ get reward with staking
This POC is a bit on the extreme side to get the point across. In the first test, the user stakes and then immediately unstakes, while in the second test, the user stakes for the entire period. In the end, the user gets roughly the same amount of reward.
Impact
After periods with no stakers, users can get reward without staking. This is also possible at the beginning of the staking period, and doing so then will reduce the reward for other users in the process.
Tools Used
Manual review, Hardhat
Recommended Mitigation Steps
Possible solution 1: set a minimum duration that a user must stake for (prevent them from staking and immediately withdrawing)
Possible solution 2: always update _mostRecentValueCalcTime (regardless totalCurrentStake). i.e. move the following line out of the if statement. https://github.com/code-423n4/2023-03-mute/blob/main/contracts/amplifier/MuteAmplifier.sol#L109
Keep in mind that with solution 2, no one gets the rewards in periods with no stakers - this means that the rescueTokens function needs to be updated to get retrieve these rewards.
mattt21 (Mute Switch) confirmed, but disagreed with severity
Can the sponsor explain why they disagree with the severity?
The issue is valid in my opinion. The desired behavior from this line seems to be that rewards not distributed because there is no stakers are added to the rewards per seconds distributed when there are some. The fact that the first next staker could take all pending rewards is a bug.
Another mitigation could be to reset
firstStakeTimeand replicate the initial behavior if at some pointtotalStake()goes back to 0.
In the absence of comment from the sponsor, I’ll keep Medium severity, considering it isn’t the desired behavior.
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 update modifier only updates _mostRecentValueCalcTime when totalCurrentStake > 0, so periods with zero stakers are not accounted for, and the next staker receives rewards for that period.
Impact
After periods with no stakers, users can get reward without staking, and doing so at the beginning of the staking period reduces the reward for other users.
What the attacker needed
No stakers at the start of the period; attacker can stake and withdraw
The fix
The fix should ensure _mostRecentValueCalcTime is always updated, even when totalCurrentStake is zero, or enforce a minimum staking duration.
How it was found
- Wait until the staking period starts and no one has staked yet.
- Stake a minimal amount (e.g., 1 wei) to set firstStakeTime and _userWeighted.
- Immediately withdraw in the same transaction, making totalCurrentStake zero again.
- Wait until near the end of the staking period.
- Stake a minimal amount again and immediately withdraw.
- Observe that the user receives rewards approximately equal to staking for the entire period.
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.
In a staking system, users lock tokens to earn rewards over time. The contract tracks rewards by updating a total weight whenever someone stakes or withdraws. If nobody is staking, the update is skipped, so the time since the last update is not recorded. When someone finally stakes and quickly withdraws, the contract calculates rewards for the entire period as if that person had been staking the whole time, giving them all the rewards unfairly.
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.