Claiming rewards from a future not yet existing epoch prevents claiming rewards for those epochs later on
The rewards contract lets a user claim rewards for an epoch that has not happened yet, and doing so marks every epoch up to that point as already claimed. The user then cannot claim the real rewards for those intervening epochs, so those rewards are lost.
berndartmueller Follow with Blockian
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-01-ajna-judging/issues/122
Found by
berndartmueller, Blockian
Summary
If a user claims rewards for a future epoch, all epochs are marked as claimed up until that future epoch. This prevents the user from claiming rewards for those epochs later, leading to a loss of rewards.
Vulnerability Detail
Already claimed rewards are tracked in the isEpochClaimed mapping and checked in the RewardsManager.claimRewards function to prevent claiming rewards multiple times. However, the current implementation does not prevent a user from accidentally claiming rewards for a future epoch. This would iterate through all epochs up until the future epoch and mark them all as claimed. This prevents the user from claiming rewards for those epochs later on, leading to a loss of rewards.
Impact
If a user accidentally claims rewards for a future epoch, the rewards are lost and unclaimable.
Code Snippet
contracts/src/RewardsManager.sol#L112
106: function claimRewards(
107: uint256 tokenId_,
108: uint256 epochToClaim_
109: ) external override {
110: if (msg.sender != stakes[tokenId_].owner) revert NotOwnerOfDeposit();
111:
112: if (isEpochClaimed[tokenId_][epochToClaim_]) revert AlreadyClaimed();
113:
114: _claimRewards(tokenId_, epochToClaim_);
115: }
contracts/src/RewardsManager.sol#L298
272: function _calculateAndClaimRewards(
273: uint256 tokenId_,
274: uint256 epochToClaim_
275: ) internal returns (uint256 rewards_) {
276:
277: address ajnaPool = stakes[tokenId_].ajnaPool;
278: uint256 lastBurnEpoch = stakes[tokenId_].lastInteractionBurnEpoch;
279: uint256 stakingEpoch = stakes[tokenId_].stakingEpoch;
280:
281: uint256[] memory positionIndexes = positionManager.getPositionIndexes(tokenId_);
282:
283: // iterate through all burn periods to calculate and claim rewards
284: for (uint256 epoch = lastBurnEpoch; epoch < epochToClaim_; ) {
285:
286: uint256 nextEpochRewards = _calculateNextEpochRewards(
287: tokenId_,
288: epoch,
289: stakingEpoch,
290: ajnaPool,
291: positionIndexes
292: );
293:
294: uint256 nextEpoch = epoch + 1;
295:
296: // update epoch token claim trackers
297: rewardsClaimed[nextEpoch] += nextEpochRewards;
298: isEpochClaimed[tokenId_][nextEpoch] = true;
299:
300: rewards_ += nextEpochRewards;
301:
302: unchecked { ++epoch; }
303: }
304: }
Tool used
Manual Review
Recommendation
Consider adding a check to the RewardsManager.claimRewards function to prevent claiming rewards for future epochs.
Discussion
grandizzy
Has #151 as dupe
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 claim function does not validate that the requested epoch is not in the future before the loop marks each preceding epoch as claimed.
Impact
A user who accidentally claims a future epoch loses the rewards for all epochs up to that point, and they cannot be claimed afterwards.
What the attacker needed
A staked position (token id); caller owns the staked position
The fix
The claim function should reject requests for epochs that have not yet occurred, as the report recommends.
How it was found
- Call the claim function with an epoch number that lies in the future.
- The contract loops from the last burn epoch up to the requested epoch.
- Each epoch in that range is marked as claimed and its rewards are added.
- Later attempts to claim the real rewards for those epochs revert as already claimed.
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.
A smart contract keeps track of which reward periods (epochs) a staker has already collected. The claim function never checks that the requested period has actually started, so asking for a period far in the future silently marks all the periods before it as collected too. The staker then loses the rewards for those real periods because the contract thinks they were already paid.
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.