The user who withdraws liquidity from a particular pool is able to claim more rewards than they should by carefully selecting a decreaseShareAmount value such that the virtualRewardsToRemove is rounded down to zero
The staking contract rounds down the virtual rewards to remove when a user decreases their share, so a user can choose a very small decrease amount that makes this value zero and claim rewards that should not be claimable. This lets the user withdraw more rewards than they are entitled to.
Submitted by Udsen, also found by stackachu, ether_sky, Jorgect, Banditx0x, J4X, DanielArmstrong, santiellena, Draiakoo, and 0xfave
https://github.com/code-423n4/2024-01-salty/blob/main/src/staking/StakingRewards.sol#L113-L118
https://github.com/code-423n4/2024-01-salty/blob/main/src/staking/StakingRewards.sol#L132-L133
https://github.com/code-423n4/2024-01-salty/blob/main/src/staking/StakingRewards.sol#L99
The StakingRewards._decreaseUserShare function is used to decrease a user’s share for the pool and have any pending rewards sent to them. When the amount of pending rewards are calculated, initially the virtualRewardsToRemove are calculated as follows:
uint256 virtualRewardsToRemove = (user.virtualRewards * decreaseShareAmount) / user.userShare;
Then the virtualRewardsToRemove is substracted from the rewardsForAmount value to calculate the claimableRewards amount as shown below:
if ( virtualRewardsToRemove < rewardsForAmount )
claimableRewards = rewardsForAmount - virtualRewardsToRemove;
But the issue here is that the virtualRewardsToRemove calculation is rounded down in favor of the user and not in the favor of the protocol. Since the virtualRewardsToRemove is rounded down there is an opportunity to the user to call the StakingRewards._decreaseUserShare function with a very small decreaseShareAmount value such that the virtualRewardsToRemove will be rounded down to 0. Providing a very small decreaseShareAmount value is possible since only input validation on decreaseShareAmount is ! = 0 as shown below:
require( decreaseShareAmount != 0, "Cannot decrease zero share" );
When the claimableRewards is calculated it will be equal to the rewardsForAmount value since the virtualRewardsToRemove will be 0. This way the user can keep on removing his liquidity from a particular pool by withdrawing small decreaseShareAmount at a time such that keeping virtualRewardsToRemove at 0 due to rounding down.
Furthermore the decreaseShareAmount value should be selected in such a way rewardsForAmount is calculated to a considerable amount after round down (not zero) and the virtualRewardsToRemove should round down to zero.
Hence as a result the user can withdraw all the rewardsForAmount as the claimableRewards even though some of those rewards are virtual rewards which should not be claimable as clearly stated by the following natspec comment:
// Some of the rewardsForAmount are actually virtualRewards and can't be claimed.
Hence as a result the user is able to get an undue advantage and claim more rewards for his liquidity during liquidity withdrawable. This happens because the user can bypass the virtual reward subtraction by making it round down to 0. As a result the virtualReward amount of the rewardsForAmount, which should not be claimable is also claimed by the user unfairly.
Proof of Concept
// Determine the share of the rewards for the amountToDecrease (will include previously added virtual rewards)
uint256 rewardsForAmount = ( totalRewards[poolID] * decreaseShareAmount ) / totalShares[poolID];
// For the amountToDecrease determine the proportion of virtualRewards (proportional to all virtualRewards for the user)
// Round virtualRewards down in favor of the protocol
uint256 virtualRewardsToRemove = (user.virtualRewards * decreaseShareAmount) / user.userShare;
https://github.com/code-423n4/2024-01-salty/blob/main/src/staking/StakingRewards.sol#L113-L118
if ( virtualRewardsToRemove < rewardsForAmount )
claimableRewards = rewardsForAmount - virtualRewardsToRemove;
https://github.com/code-423n4/2024-01-salty/blob/main/src/staking/StakingRewards.sol#L132-L133
require( decreaseShareAmount != 0, "Cannot decrease zero share" );
https://github.com/code-423n4/2024-01-salty/blob/main/src/staking/StakingRewards.sol#L99
Tools Used
VSCode
Recommended Mitigation Steps
Hence it is recommended to round up the virtualRewardsToRemove value during its calculation such that it will not be rounded down to zero for a very small decreaseShareAmount. This way user is unable to claim the rewards which he is not eligible for and the rewards will be claimed after accounting for the virtual rewards.
othernet-global (Salty.IO) confirmed and commented:
virtualRewards now rounded up on _decreaseUserShare
https://github.com/othernet-global/salty-io/commit/b3b8cb955db2b9f0e47a4964e1e4f833a447a72d
Status: Mitigated with an Error. Full details in report from t0x1c, and also included in the Mitigation Review section below.
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 calculation of virtualRewardsToRemove rounds down instead of up, allowing it to become zero for very small decreaseShareAmount values.
Impact
A user could claim more rewards than they should, including virtual rewards that are not meant to be claimable.
What the attacker needed
User has a share in a pool with virtual rewards; user can call decreaseUserShare
The fix
The virtualRewardsToRemove calculation should round up instead of down, and the text says this was fixed.
How it was found
- Call the decreaseUserShare function with a very small decreaseShareAmount value.
- Ensure the virtualRewardsToRemove rounds down to zero.
- Receive claimableRewards equal to rewardsForAmount, including virtual rewards.
- Repeat to withdraw all rewards unfairly.
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 you take your money out of a staking pool, the contract works out how much of the rewards you can keep. Some of those rewards are 'virtual' and should not be paid out. Because of a rounding mistake, a user can take out a tiny amount at a time so the contract forgets to subtract the virtual rewards, and they get paid extra.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.