vestTokens bug in MultiFeeDistribution.sol causes new incentives to erase previous incentives
The vestTokens function in MultiFeeDistribution.sol calls _notifyReward without first updating rewardPerTokenStored, so when a minter adds new incentives, the accumulated rewards from previous incentives are erased. An attacker (or any minter) could cause lockers to lose previously earned rewards.
In MultiFeeDistribution.sol, the function _notifyReward(address rewardToken, uint256 reward) does not change r.rewardPerTokenStored and hence, relies on _updateReward(address account) to be called before any calls to _notifyReward. The rest of the functions who call _notifyReward follows this rule; except for vestTokens when it is called by a minter to give incentives to the lockers.
function vestTokens(address user, uint256 amount, bool withPenalty) external whenNotPaused {
if (!minters[msg.sender]) revert InsufficientPermission();
if (amount == 0) return;
if (user == address(this)) {
-> // minting to this contract adds the new tokens as incentives for lockers
-> _notifyReward(address(rdntToken), amount);
-> return;
}
......
}
Since _updateReward is an internal function, we also cannot expect minters to call it on their side before calling vestTokens.
Hence, this bug results in rewardData[rewardToken].rewardPerTokenStored being inaccurate as it will not contain the previous results accumulated by the previous rewardData[rewardToken].rewardPerSecond when minters call vestTokens. This results in all lockers losing the rewards previously accumulated by the previous rewardPerSecond.
Proof of Concept
01: function test_rewardsGone() public {
02: assert(rewardsDuration == 30 days); // we will use 30 days as the rewardsDuration for convenience
03: address Alice = address(0x123456);
04: uint256 amount = 1 ether;
05: uint256[] memory lockDurations = new uint256[](1);
06: uint256[] memory rewardMultipliers = new uint256[](1);
07: lockDurations[0] = 700 days;
08: rewardMultipliers[0] = 1;
09: multiFeeDistribution.setLockTypeInfo(lockDurations, rewardMultipliers);
10:
11: stakeToken.mint(address(this), amount);
12: multiFeeDistribution.setLPToken(address(stakeToken));
13:
14: multiFeeDistribution.setAddresses(IChefIncentivesController(vm.addr(uint256(keccak256("incentivesController")))), vm.addr(uint256(keccak256("treasury"))));
15: vm.mockCall(
16: vm.addr(uint256(keccak256("incentivesController"))),
17: abi.encodeWithSelector(IChefIncentivesController.afterLockUpdate.selector, Alice),
18: abi.encode(true)
19: );
20: stakeToken.approve(address(multiFeeDistribution), amount);
21: multiFeeDistribution.stake(amount, Alice, 0); // Alice now has 1 ether staked (with lockTypeIndex=0)
22: require(multiFeeDistribution.lockedBalance(Alice) == amount);
23:
24: address[] memory minters = new address[](1);
25: minters[0] = address(this);
26: multiFeeDistribution.setMinters(minters);
27:
28: amount = 10000 ether;
29: loopToken.mint(address(this), amount);
30: loopToken.transfer(address(multiFeeDistribution), amount);
31:
32: vm.mockCall(
33: mockPriceProvider,
34: abi.encodeWithSelector(IPriceProvider.getRewardTokenPrice.selector, address(loopToken), amount),
35: abi.encode(8)
36: );
37: multiFeeDistribution.vestTokens(address(multiFeeDistribution), amount, false); //Minter gives first incentives for lockers
38:
39: skip(31 days);
40: IFeeDistribution.RewardData[] memory rewardsData = multiFeeDistribution.claimableRewards(Alice);
41: require(rewardsData[0].token == address(loopToken));
42: console.log("After the first incentive given |", rewardsData[0].amount);
43:
44:
45: amount = 1 ether;
46: loopToken.mint(address(this), amount);
47: loopToken.transfer(address(multiFeeDistribution), amount);
48: vm.mockCall(
49: mockPriceProvider,
50: abi.encodeWithSelector(IPriceProvider.getRewardTokenPrice.selector, address(loopToken), amount),
51: abi.encode(8)
52: );
53: multiFeeDistribution.vestTokens(address(multiFeeDistribution), amount, false); //Minter gives second incentives for lockers
54: rewardsData = multiFeeDistribution.claimableRewards(Alice);
55: require(rewardsData[0].token == address(loopToken));
56: console.log("Right after second incentive given|", rewardsData[0].amount);
57:
58: skip(31 days);
59: rewardsData = multiFeeDistribution.claimableRewards(Alice);
60: require(rewardsData[0].token == address(loopToken));
61: console.log("After everything ends |", rewardsData[0].amount);
62: }
Console Output:
Ran 1 test for src/test/unit/MultiFeeDistribution.t.sol:MultiFeeDistributionTest
[PASS] test_rewardsGone() (gas: 738287)
Logs:
After the first incentive given | 9999999999999999999999
Right after second incentive given| 0
After everything ends | 999999999999999999
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 6.35ms (1.38ms CPU time)
Ran 1 test suite in 276.54ms (6.35ms CPU time): 1 tests passed, 0 failed, 0 skipped (1 total tests)
Code walkthrough:
(Line 21) Alice stakes 1 ether to become a locker.
(Line 37) Trusted minter gives out 10000 ether as incentive to lockers by calling vestTokens with parameter user == address(MultiFeeDistribution).
(Line 42) 31 days passed, calling claimableRewards now show that Alice has 9999999999999999999999 tokens(10 000 ether) to claim. (Refer to console output).
(Line 53) To further reward lockers, the trusted minter further sends a 2nd incentive of 1 ether (some time after the first incentive was given out), by again calling vestTokens with parameter user == address(MultiFeeDistribution).
(Line 56) Now, calling claimableRewards will show that Alice has 0 tokens to claim, proving that the previous rewards accumulated from the 1st incentive for the user has been erased.
(Line 61) After rewardsDuration for the 2nd incentive ends, we call claimableRewards again. We can see from the last line of the console output that only 999999999999999999 tokens(1 ether) are left. Proving that Alice’s tokens from the first incentive(10 000 ether) has been erased, and she only retains her share of the rewards coming from the 2nd incentive(1 ether).
Recommended Mitigation Steps
function vestTokens(address user, uint256 amount, bool withPenalty) external whenNotPaused {
if (!minters[msg.sender]) revert InsufficientPermission();
if (amount == 0) return;
if (user == address(this)) {
// minting to this contract adds the new tokens as incentives for lockers
+ _updateReward(address(this));
_notifyReward(address(rdntToken), amount);
return;
}
......
}
Adding _updateReward(address(this)) will ensure rewardData[address(rdntToken)].rewardPerTokenStored is updated accordingly, so that lockers will not lose previously given incentives.
@Koolex - The report shows a valid scenario where rewards are messed up; however, it is being confirmed by calling multiFeeDistribution.claimableRewards which is just a view function. However, when a user claims his rewards, after the mentioned scenario, he’ll be calling getReward which updates the rewards (_updateReward(msg.sender);), so the impact here is just in a view function. Hence, I believe this is a low finding.
I believe there is a mistake in the comment above. The issue is not just in a view function. The root cause is the following storage update here.
This is done without actually updating the rewardPerTokenStored from the _updateReward. As a result the reward for the period between now and the previous update will be erased. For example, check out the vestTokens function:
function vestTokens(address user, uint256 amount, bool withPenalty) external whenNotPaused {
if (!minters[msg.sender]) revert InsufficientPermission();
if (amount == 0) return;
if (user == address(this)) {
// minting to this contract adds the new tokens as incentives for lockers
_notifyReward(address(rdntToken), amount);
return;
}
As you can see the lastUpdateTime is set without actually updating the rewardPerTokenStored. So next time the reward is updated, the rewardPerToken will not account for the missed period:
As the new reward is calculated based on the lastUpdateTime which will wrongly be updated anytime vestTokens is called.
He’ll be calling getReward which updates the rewards (_updateReward(msg.sender);). When the user actually calls getReward the period between the time of calling vestTokens and the last time _updateReward was executed will actually be skipped because of the wrong storage update. As a result, anytime vestTokens is called, the notifyReward will skip reward periods erasing rewards, which is indeed a valid High.
The original report as published on Code4rena, by rscodes. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the _notifyReward function updates lastUpdateTime without first calling _updateReward to store the accumulated rewardPerTokenStored, so the period between the last update and the new notification is not accounted for.
Impact
Lockers lose all rewards accumulated from previous incentives when a minter calls vestTokens, as the new incentive overwrites the old reward rate without preserving past earnings.
What the attacker needed
Caller must be a minter; lockers must have staked tokens
The fix
The fix is to call _updateReward(address(this)) before _notifyReward in vestTokens, so that rewardPerTokenStored is updated before the new reward rate is set.
How it was found
Alice stakes 1 ether to become a locker.
A trusted minter calls vestTokens with user set to the contract address, adding 10000 ether as incentives.
After 31 days, claimableRewards shows Alice has 10000 ether to claim.
The minter calls vestTokens again with 1 ether as a second incentive.
Immediately after, claimableRewards shows Alice has 0 tokens to claim.
After the second incentive's duration ends, claimableRewards shows only 1 ether, proving the first incentive was erased.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
In a staking contract, rewards build up over time based on a rate. When new rewards are added, the contract must first record how much each staker has earned so far. Here, that recording step is skipped, so the new reward rate overwrites the old one and past earnings are lost.
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.