_claimRewardsOnBehalf() User’s rewards may be lost
The reward-claiming function caps the payout at the contract's current token balance and then zeroes the user's unclaimed rewards, even when the shortfall is only because rewards still sit in the external incentives controller. A user calling the claim with the update flag off can therefore receive less than owed and have the remainder erased.
From the code above, we can see that if the contract balance is not enough, it will only use the contract balance and set the unclaimed rewards to 0: _unclaimedRewards[user]=0.
But using the current contract’s balance is inaccurate, REWARD_TOKEN may still be stored in INCENTIVES_CONTROLLER.
_updateRewards() and _updateUser(), are just calculations, they don’t transfer REWARD_TOKEN to the current contract, but _unclaimedRewards[user] is always accumulating.
This way if _unclaimedRewards(forceUpdate=false) is executed, it does not trigger the transfer of REWARD_TOKEN to the current contract. This makes it possible that _unclaimedRewards[user] > REWARD_TOKEN.balanceOf(address(this)). According to the _claimedRewardsOnBehalf() current code, the extra value is lost.
It is recommended that if (reward > totBal) be executed only if forceUpdate=true, to avoid losing user rewards.
After a thorough review we can confirm this is not an issue. This is the way it should work and that’s the reason why there is a forceUpdate param. When forceUpdate == true, then you will always have the latest rewards to claim and the updated balance.
When is set to false, it will only distribute the rewards that were previously collected (the ones available in the contract). It is correct there might be additional rewards to be collected, but that can easily be done with another call to the same function using the forceUpdate == true.
There are no rewards “lost” in the process, no fix needs to be implemented. Even though unclaimedRewards is set to zero, then it will be populated with all the pending rewards again so the amount will be ok.
Moreover, the suggested fix would brick the function most of the time (as usually rewards are bigger than balance because it includes uncollected but pending rewards), and in that case it would attempt to transfer rewards not available in the contract. The check of just sending the balance in those cases is required.
@bin2chen - please take a look.
It seems that since _getPendingRewards has a false parameter, _unclaimedRewards does not accumulate unclaimed rewards in the controller, so the rewards are not lost.
@cccz - getPendingRewards(fresh = true). It doesn’t matter if fresh istrue or false, because this can only be used to calculate the latest global accRewardsPerToken.
Since _updateRewards() must be executed before _updateUser (user) is executed to ensure that accRewardsPerToken is up-to-date, it does not matter whether fresh is true.
But the message above
Even though unclaimedRewards is set to zero, then it will be populated with all the pending rewards again so the amount will be ok.
It confuses me a bit, I might need to take another look. I need to familiarize myself with this project again to see if I missed something.
_getClaimableRewards returns _unclaimedRewards + pendingRewards, that is, reward = _unclaimedRewards + pendingRewards, so just setting _unclaimedRewards to 0 will not decrease pendingRewards, which may be somewhat helpful.
@cccz - pendingRewards is assumed to be 0.
But _unclaimedRewards[user] has a value, the point is that the value in there is not in the current contract, it’s in INCENTIVES_CONTROLLER. If it’s cleared, it’s gone. I think I need to take another look.
@cccz - I’ll keep my original point. Please help me see if I’m missing something. Thanks.
The current implementation only moves rewards to the current contract if _collectAndUpdateRewards() is executed.
_updateRewards() and _updateUser() are not triggered.
But _unclaimedRewards[user] is accumulated.
_accRewardsPerToken and _userSnapshotRewardsPerToken[user] keeps getting bigger.
So that if no one has called _collectAndUpdateRewards() (i.e. forceUpdate=false is not called).
This way the rewards balance in the contract will always be zero.
After _claimRewardsOnBehalf(forceUpdate=false).
The user doesn’t get any rewards, but _unclaimedRewards[user] is cleared to 0 and can’t be refilled (note that it’s not pendingRewards, assuming that pendingRewards is 0).
@cccz & @bin2chen - Ok, the issue exists I can confirm now. This is a tricky one, nice catch. Found out that this was built this way on purpose. The idea is to allow the user to “sacrifice” some rewards for gas savings. You can see it in some of the tests, with comments like this one:
expect(pendingRewards5).to.be.eq(0) // User "sacrifice" excess rewards to save on gas-costs
We will discuss today what action we are taking with this issue.
In any case, the suggested mitigation does not address fully the issue, and causes the contract to fail under normal operations (simply try to run our test suite with that change). I believe we should probably address the main issue that the _unclaimed variable is set to 0, instead of to the rewards still pending to be collected.
What do you think about the function working this way? I ran a simple check and seems to address it at least for the example you provided. This mitigation was suggested on the other ticket linked here, it had some rounding issues but overall is the same.
If there is a pendingReward may underflow
For example:
balance = 10
_unclaimedRewards[user]=9
pendingRewards = 2
_unclaimedRewards[onBehalfOf] -= reward.wadToRay(); will underflow
Finally executed _updateUserSnapshotRewardsPerToken(onBehalfOf);
Then we need to accumulate pendingRewards to _unclaimedRewards[user].
Personally, I feel that if we don’t want to revert, try this:
function _claimRewardsOnBehalf(
address onBehalfOf,
address receiver,
bool forceUpdate
) internal {
if (forceUpdate) {
_collectAndUpdateRewards();
}
uint256 balance = balanceOf(onBehalfOf);
uint256 reward = _getClaimableRewards(onBehalfOf, balance, false);
uint256 totBal = REWARD_TOKEN.balanceOf(address(this));
if (reward == 0) {
return;
}
if (reward > totBal) {
+ // Insufficient balance resulting in no transfers out put into _unclaimedRewards[]
+ _unclaimedRewards[onBehalfOf] = (reward -totBal).wadToRay();
reward = totBal;
- _unclaimedRewards[onBehalfOf] -= reward.wadToRay();
} else {
_unclaimedRewards[onBehalfOf] = 0;
}
_updateUserSnapshotRewardsPerToken(onBehalfOf);
REWARD_TOKEN.safeTransfer(receiver, reward);
}
This may still have this prompt.
expect(pendingRewards5).to.be.eq(0) // User "sacrifice" excess rewards to save on gas-costs
But I feel that this use case should be changed. It is okay to sacrifice a little. If it is a lot, it is still necessary to prevent the user from executing it.
@cccz @bin2chen - We had a group call and we decided to ACKNOWLEDGE the issue but we will not make code changes, just add a comment in the contract explaining this risk of losing rewards if you call it with the false parameter.
The reasons are:
We do not expect Aave V2 Rewards to come back in practice.
Our protocol is not exposed to this issue. Because in our code we always claim with the parameter set to true, and nobody can claim on behalf of the protocol with the false parameter, the Protocol will always get the latest rewards when claiming.
It is up to the user to call this with true or false. They might see value in calling it with false and sacrificing some rewards, which was the original reason it was built this way. But they always have the option to call it with the true parameter which will behave normally. So we consider it more like an option they have and not that much of a bug. We also do not expect people holding this wrapper token besides using it in our protocol.
However, we acknowledge and value the finding which was spot on and allowed us to understand the wrapper in more detail. Thanks for that!
The original report as published on Code4rena, by bin2chen. 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 40 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
The claim function treats the contract's current token balance as the maximum payable and clears the user's unclaimed rewards, without accounting for rewards still held in the incentives controller.
Impact
A user could lose part of their accrued rewards when claiming with the update flag off; the protocol itself is not exposed because it always claims with the flag on.
What the attacker needed
User holds the wrapper token; rewards accrued but not collected; user calls claim with update flag false
The fix
The sponsor acknowledged the issue but decided not to change code, only to add a comment explaining the risk; a fuller fix would reduce the unclaimed rewards by the amount actually paid rather than zeroing them.
How it was found
Deposit into the wrapper and let rewards accrue.
Collect rewards once so the contract holds only a small reward balance.
Let more time pass so further rewards accrue but are not collected.
Call the claim function with the update flag set to false.
Observe the user receives only the small contract balance while their unclaimed rewards are reset.
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 40 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
A user earns rewards that are held partly in a separate rewards contract. When they ask to collect, the code pays out only what is already sitting in this contract and then marks their whole reward balance as used up. So the part still held elsewhere is forgotten and the user gets less than 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.