The reward calculation scales the cumulative reward per token by 1e36, which can overflow when the total staked amount is very small. An attacker could stake a tiny amount and deposit a large reward to cause an overflow, bricking reward calculations and preventing users from withdrawing stakes or rewards.
The protocol uses a Sushi Masterchef like method for maintaining rewards for each user. The cumulatedReward variable is used for keeping track of the accumulated amount of assets per staking token. However, since cumulatedReward is scaled by 1e36, it may lead to overflow during reward calculation.
Vulnerability Detail
Let's see how the reward is calculated:
cumulatedReward is maintained as the cumulative asset/share ratio scaled by 1e36.
Earned rewards and rewardDebt are calculated by cumulatedReward times the amount of shared token (with multiplier)
The issue here is cumulatedReward is scaled by 1e36, which is too large, and is susceptible to grief attacks. An example is:
At the beginning of the Lock contract, attacker initially stakes 1 wei of staking token, and deposits 1e18 reward token.
Attacker calls notifyUnseenReward() to accumulate the reward. The cumulatedReward is now 1e18 * 1e36 / 1 = 1e54.
Then, when calculating the earned rewards, if anyone has 1e18 staking tokens, the reward calculation (and the reward debt) would be up to 1e54 * 1e18 = 1e72.
Note that uint256.max is around 1e78, and the limit would be easily hit if the tokens in step 1 and step 3 is 1000e18 instead of 1e18.
Solidity
function _notifyReward(address _rewardToken, uint256 reward) internal {
if (lockedSupplyWithMultiplier == 0)
return; // If there is no locked supply with multiplier, exit without adding rewards (prevents division by zero).
Reward storage r = rewardData[_rewardToken]; // Accesses the reward structure for the specified token.
> uint256 newReward = reward * 1e36 / lockedSupplyWithMultiplier; // Calculates the reward per token, scaled up for precision.
> r.cumulatedReward += newReward; // Updates the cumulative reward for the token.
r.lastUpdateTime = block.timestamp; // Sets the last update time to now.
r.balance += reward; // Increments the balance of the token by the new reward amount.
}
...
function _earned(
address _user,
address _rewardToken
) internal view returns (uint256 earnings) {
Reward memory rewardInfo = rewardData[_rewardToken]; // Retrieves reward data for the specified token.
Balances memory balance = balances[_user]; // Retrieves balance information for the user.
> earnings = rewardInfo.cumulatedReward * balance.lockedWithMultiplier - rewardDebt[_user][_rewardToken]; // Calculates earnings by considering the accumulated reward and the reward debt.
}
Use 1e12 as scale factor (like in Masterchef) instead of 1e36.
Since the staking token is always GAMMA (which has 18 decimals) and is currently priced at $0.117, the lockedSupplyWithMultiplier can be initially set to 1e18 to avoid cumulatedReward to be too large. The loss is be negligible for initial stakers.
Add a minimum staking amount limit (e.g. 1e18).
Discussion
santipu03
This issue has been marked as invalid for the following reasons:
Before users start staking, the admins have to set the staking token.
Before anyone can send rewards to the contract, the admins have to set the reward tokens.
For the reasons above, the admins can set the staking token first, wait for some stakers, and later set the reward tokens and send some to the contract.
Even in the improbable case that this attack is successfully executed, because the only staker is the attacker, the admins can simply redeploy a new contract and stake a minimum amount first to avoid this issue again. If some users have started staking between the attack and the admin's action, they can simply withdraw their locks.
In conclusion, there won't be any loss of funds and the contract can simply be redeployed.
santipu03
@bjp333 What do you think about this issue?
bjp333
I think this issue is a bit of a stretch. We will definitely have more than 1 wei staked prior to any distributions taking place.
pkqs90
Escalate
The overflow scenario does not only occur during the beginning of the lock contract. As stated in the original issue, the beginning of the contract is only given as an example, since it is where this is likely to happen.
This issue may also occur when the contract has been active for a while. The reward tokens are already set, and due to users staking/unstaking, if there exists a moment where the lockedSupplyWithMultiplier is too little, this overflow issue will still occur.
Also, the original code itself handles the case where lockedSupplyWithMultiplier is zero, so it only makes sense that it should also handle where lockedSupplyWithMultiplier is 1 wei or 1e3 wei or something.
Solidity
function _notifyReward(address _rewardToken, uint256 reward) internal {
> if (lockedSupplyWithMultiplier == 0)
> return; // If there is no locked supply with multiplier, exit without adding rewards (prevents division by zero).
Reward storage r = rewardData[_rewardToken]; // Accesses the reward structure for the specified token.
uint256 newReward = reward * 1e36 / lockedSupplyWithMultiplier; // Calculates the reward per token, scaled up for precision.
r.cumulatedReward += newReward; // Updates the cumulative reward for the token.
r.lastUpdateTime = block.timestamp; // Sets the last update time to now.
r.balance += reward; // Increments the balance of the token by the new reward amount.
}
Note that if this overflow issue happens, the entire protocol would brick. This is because reward calculation is used for all operations, including staking, early exiting, late exiting, and getting rewards. This means users would not be able to withdraw their staked tokens nor rewards once cumulatedReward overflows, which is a disaster.
Quoting the definition of a medium severity issue, this issue would fall under "cause loss of fund" but "requires certain external conditions or specific states", and the loss itself definitely exceeds "small, finite amount of funds".
Causes a loss of funds but requires certain external conditions or specific states, or a loss is highly constrained. The losses must exceed small, finite amount of funds, and any amount relevant based on the precision or significance of the loss.
sherlock-admin3
Escalate
The overflow scenario does not only occur during the beginning of the lock contract. As stated in the original issue, the beginning of the contract is only given as an example, since it is where this is likely to happen.
This issue may also occur when the contract has been active for a while. The reward tokens are already set, and due to users staking/unstaking, if there exists a moment where the lockedSupplyWithMultiplier is too little, this overflow issue will still occur.
Also, the original code itself handles the case where lockedSupplyWithMultiplier is zero, so it only makes sense that it should also handle where lockedSupplyWithMultiplier is 1 wei or 1e3 wei or something.
Solidity
function _notifyReward(address _rewardToken, uint256 reward) internal {
> if (lockedSupplyWithMultiplier == 0)
> return; // If there is no locked supply with multiplier, exit without adding rewards (prevents division by zero).
Reward storage r = rewardData[_rewardToken]; // Accesses the reward structure for the specified token.
uint256 newReward = reward * 1e36 / lockedSupplyWithMultiplier; // Calculates the reward per token, scaled up for precision.
r.cumulatedReward += newReward; // Updates the cumulative reward for the token.
r.lastUpdateTime = block.timestamp; // Sets the last update time to now.
r.balance += reward; // Increments the balance of the token by the new reward amount.
}
Note that if this overflow issue happens, the entire protocol would brick. This is because reward calculation is used for all operations, including staking, early exiting, late exiting, and getting rewards. This means users would not be able to withdraw their staked tokens nor rewards once cumulatedReward overflows, which is a disaster.
Quoting the definition of a medium severity issue, this issue would fall under "cause loss of fund" but "requires certain external conditions or specific states", and the loss itself definitely exceeds "small, finite amount of funds".
Causes a loss of funds but requires certain external conditions or specific states, or a loss is highly constrained. The losses must exceed small, finite amount of funds, and any amount relevant based on the precision or significance of the loss.
You've created a valid escalation!
To remove the escalation from consideration: Delete your comment.
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
santipu03
Even though it is true that this issue can be triggered when the contract has been active for a while, it would require that the staked value is almost null, which is highly unlikely. Because the staking token is GAMMA, which is a token currently valued at ~0.14 USD and with 18 decimals, it's almost impossible that during the life of the contract, there are less than 1e18 tokens staked in total.
In a similar way, the classic vault inflation attack can always be triggered when totalSupply is 0, but the only real risk is when a Vault is deployed because it's the only realistic scenario where the total value deposited is strictly 0.
pkqs90
I agree that this is unlikely to happen. However,
When talking about the severity of an issue, we should not talk about how likely it is to happen (some almost impossible examples that actually happened: the Luna crash, or USDC depeg).
The Sherlock rules also does not talk about the possibility, since it is impossible to quantify. The scenario described above is possible (however unlikely) and falls under "certain external conditions or specific states".
Causes a loss of funds but requires certain external conditions or specific states, or a loss is highly constrained. The losses must exceed small, finite amount of funds, and any amount relevant based on the precision or significance of the loss.
santipu03
Also, take into account that there will be stakes locked for 720 days, which belong to the team and investors, so the requirements necessary for this bug to appear are not realistic at all.
I don't believe it's fair to shield behind the expression "requires certain external conditions or specific states" to defend issues that will never be triggered in reality.
pkqs90
This issue has been marked as invalid for the following reasons:
Before users start staking, the admins have to set the staking token.
Before anyone can send rewards to the contract, the admins have to set the reward tokens.
For the reasons above, the admins can set the staking token first, wait for some stakers, and later set the reward tokens and send some to the contract.
Even in the improbable case that this attack is successfully executed, because the only staker is the attacker, the admins can simply redeploy a new contract and stake a minimum amount first to avoid this issue again. If some users have started staking between the attack and the admin's action, they can simply withdraw their locks.
In conclusion, there won't be any loss of funds and the contract can simply be redeployed.
I'd also like to dispute the initial reasons for invalidating this issue.
For point 3, there would be no incentive for stakers if the reward token is not set. It is very likely that only after the admins set the reward token, would the users begin to stake.
For point 4, in order for the attacker to overflow cumulatedReward, he would have to be the first staker. He may even try to frontrun the original first staker. The point here is, after the attacker performs this attack, the staking of the following users would be affected. The admins can definitely just redeploy a new contract to fix this, but this would be unfair to users that has already staked in the original contract, and their tokens would be locked up for nothing.
santipu03
For point 3, there would be no incentive for stakers if the reward token is not set. It is very likely that only after the admins set the reward token, would the users begin to stake.
However, before users would begin to stake, the developers would have already configured the locks for the team and investors, making it impossible to trigger this issue because many tokens are already staked. Realistically, the only moment an attacker can trigger this bug is at market deployment, but it won't be possible to execute the attack if the developers configure the contract correctly and set the locks for the team and investors before setting any reward tokens.
Even under extreme circumstances where developers fail to configure the "team and investor" locks and deploy the contract, the worst outcome would necessitate redeploying the contract. If users have already staked in a compromised contract, admins could remove penalties, allowing users to withdraw their stakes early without consequences and reinvest in the new contract. Thus, even if this vulnerability were exploited, its actual impact would effectively be negligible.
nevillehuang
This issue seems to be contigent on admin actions before the first notification of rewards. The difference between this and a first depositor inflation attack is the attack cannot immediately happen as long as the admin has taken the precautions (stake a minimum amount e.g. 1e18 before setting reward tokens), which is the case as well for ERC4626 vaults wherein a admin can deposit/mint a minimum amount of shares in deployment scripts
I also note #72 and #313 as possible duplicates. Will discuss internally with lead judge. Historically based on my judging experience, first inflation attack and its mitigations must be explicitly stated as a known risk, so I am inclined to believe this issue is valid for now, although still considering the extensive admin actions required highlighted by lead judge before notification of rewards is allowed
cc: @WangSecurity
0xRajkumar
Considering all the points @santipu03 said, I would say the likelihood is very low. Therefore, I believe this is informational at best.
nevillehuang
I believe this issue to be valid with low likelihood and high impact because
The admin actions to prevent this "inflation attack" is not highlighted in contest details and known risks + public discord channels
The scenario of low amount of stakers resulting in extremely low lockedBalance while unlikely, is possible (if many users unstake), and will brick all subsequent stakers from getting rewards
The original report as published on Sherlock, by joicygiore. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.
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 a minute. 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 use of a very large scaling factor (1e36) for the cumulative reward per token, which can overflow when the total staked amount is very small.
Impact
If the overflow occurs, the entire protocol would brick, preventing users from withdrawing staked tokens or rewards; however, the judges noted that the attack requires very specific conditions and is unlikely to happen in practice.
What the attacker needed
Attacker is first staker; very low total staked amount; attacker can deposit reward tokens
The fix
The protocol team fixed this issue in a pull request.
How it was found
Attacker stakes 1 wei of staking token.
Attacker deposits 1e18 reward token.
Attacker calls notifyUnseenReward() to accumulate the reward, making cumulatedReward 1e54.
When calculating earned rewards for a user with 1e18 staking tokens, the calculation reaches 1e72, close to uint256.max.
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 a minute. 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
This is a smart contract bug where a number used to track rewards becomes too big for the computer to store. If it overflows, the contract stops working correctly, and users cannot get their staked tokens or rewards back.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.