ConvexStakingWrapper.sol#_calcRewardIntegral Wrong implementation can disrupt rewards calculation and distribution
The reward calculation in ConvexStakingWrapper.sol incorrectly tracks the contract's reward token balance, causing underflows and misdistribution when multiple pools share the same reward token. An attacker could cause deposits and withdrawals to revert or leave rewards frozen in the claim contract.
Submitted by WatchPug, also found by cmichel, harleythedog, hickuphh3, kirk-baird, and leastwood
ConvexStakingWrapper.sol#L175-L204
uint256 bal = IERC20(reward.token).balanceOf(address(this));
uint256 d_reward = bal - reward.remaining;
// send 20 % of cvx / crv reward to treasury
if (reward.token == cvx || reward.token == crv) {
IERC20(reward.token).transfer(treasury, d_reward / 5);
d_reward = (d_reward * 4) / 5;
}
IERC20(reward.token).transfer(address(claimContract), d_reward);
if (_supply > 0 && d_reward > 0) {
reward.integral =
reward.integral +
uint128((d_reward * 1e20) / _supply);
}
//update user integrals
uint256 userI = userReward[_pid][_index][_account].integral;
if (userI < reward.integral) {
userReward[_pid][_index][_account].integral = reward.integral;
claimContract.pushReward(
_account,
reward.token,
(_balance * (reward.integral - userI)) / 1e20
);
}
//update remaining reward here since balance could have changed if claiming
if (bal != reward.remaining) {
reward.remaining = uint128(bal);
}
The problems in the current implementation:
reward.remainingis not a global state; thereward.remainingof otherrewards with the same rewardToken are not updated;balshould be refreshed beforereward.remaining = uint128(bal);;- L175 should not use
balanceOfbut take the diff before and aftergetReward().
Proof of Concept
- convexPool[1] is incentivized with CRV as the reward token,
1000 lpTokencan get10 CRVper day; - convexPool[2] is incentivized with CRV as the reward token,
1000 lpTokencan get20 CRVper day. - Alice deposits
1,000lpToken to_pid=1 - 1 day later, Alice deposits
500lpToken to_pid=1 - convexPool
getReward()sends10 CRVas reward to contract d_reward= 10,2 CRVsends totreasury,8 CRVsend toclaimContractrewards[1][0].remaining= 10- 0.5 day later, Alice deposits
500lpToken to_pid=1, and the tx will fail: - convexPool
getReward()sends7.5 CRVas reward to contract reward.remaining= 10bal= 7.5bal - reward.remainingwill fail due to underflow- 0.5 day later, Alice deposits
500lpToken to_pid=1, most of the reward tokens will be left in the contract: - convexPool
getReward()sends15 CRVas reward to the contract; d_reward = bal - reward.remaining= 51 CRVgot sent totreasury,4 CRVsent toclaimContract,10 CRVleft in the contract;rewards[1][0].remaining= 15
Expected Results:
All the 15 CRV get distributed: 3 CRV to the treasury, and 12 CRV to claimContract.
Actual Results:
Only 5 CRV got distributed. The other 10 CRV got left in the contract which can be frozen in the contract, see below for the details:
- Bob deposits
1,000lpToken to_pid=2 - convexPool
getReward()sends0 CRVas reward to the contract d_reward = bal - reward.remaining= 102 CRVsent totreasury,8 CRVsent toclaimContractwithout callingpushReward(), so the8 CRVare now frozen inclaimContract;rewards[2][0].remaining= 10
Impact
- The two most important methods:
deposit()andwithdraw()will frequently fail as the tx will revert at_calcRewardIntegral(); - Rewards distributed to users can often be fewer than expected;
- If there are different pools that use the same token as rewards, part of the rewards can be frozen at
claimContractand no one can claim them.
Recommended Mitigation Steps
Consider comparing the balanceOf reward token before and after getReward() to get the actual rewarded amount, and reward.remaining should be removed.
Alex the Entreprenerd (judge) commented:
The warden has shown how
_calcRewardIntegralcan be broken in multiple ways.While I believe a set of similar findings have been reported, this one is extremely well written so I think this can stand on it’s own.
Because
_calRewardIntegralis a core functionality of the contract (giving out reward) and the warden has shown how it can be broken, I agree with High 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
Likely the code uses the contract's total balance of the reward token instead of the difference before and after claiming, and it does not update the remaining reward for all pools sharing the same token.
Impact
Deposits and withdrawals may frequently revert, rewards distributed to users can be less than expected, and rewards can become frozen in the claim contract when multiple pools share the same reward token.
What the attacker needed
Multiple pools share the same reward token; Users deposit and withdraw over time
The fix
The code should compare the balance of the reward token before and after calling getReward() to determine the actual reward amount, and remove the reward.remaining variable.
How it was found
- Alice deposits 1,000 lpToken to pool 1.
- One day later, Alice deposits another 500 lpToken to pool 1.
- The pool sends 10 CRV as reward to the contract.
- The contract sends 2 CRV to treasury and 8 CRV to claimContract, and sets remaining to 10.
- Half a day later, Alice deposits another 500 lpToken to pool 1, but the transaction fails due to underflow when calculating the reward difference.
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.
This smart contract handles staking and rewards. A mistake in how it calculates rewards can cause transactions to fail or rewards to get stuck, meaning users might not get what they earned.
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.