Rewards can be claimed multiple times
The smart contract packs claimed epochs into a 256-bit integer, but the check for whether an epoch was claimed fails for epoch IDs of 256 or higher, allowing an attacker to claim the same reward multiple times and drain all tokens from a promotion.
Originally disclosed on Code4rena
Submitted by johnnycash, also found by certora, cmichel, gpersoon, gzeon, harleythedog, kemmio, kenzo, sirhashalot, and 0x421f
Impact
An attacker can claim its reward 256 * epochDuration seconds after the timestamp at which the promotion started. The vulnerability allows him to claim a reward several times to retrieve all the tokens associated to the promotion.
Analysis
claimRewards() claim rewards for a given promotion and epoch. In order to prevent a user from claiming a reward multiple times, the mapping _claimedEpochs keeps track of claimed rewards per user:
/// @notice Keeps track of claimed rewards per user.
/// @dev _claimedEpochs[promotionId][user] => claimedEpochs
/// @dev We pack epochs claimed by a user into a uint256. So we can't store more than 255 epochs.
mapping(uint256 => mapping(address => uint256)) internal _claimedEpochs;
(The comment is wrong, epochs are packed into a uint256 which allows 256 epochs to be stored).
_epochIds is an array of uint256. For each _epochId in this array, claimRewards() checks that the reward associated to this _epochId isn’t already claimed thanks to _isClaimedEpoch(). _isClaimedEpoch() checks that the bit _epochId of _claimedEpochs is unset:
(_userClaimedEpochs >> _epochId) & uint256(1) == 1;
However, if _epochId is greater than 255, _isClaimedEpoch() always returns false. It allows an attacker to claim a reward several times.
_calculateRewardAmount() just makes use of _epochId to tell whether the promotion is over.
Proof of Concept
The following test should result in a reverted transaction, however the transaction succeeds.
it('should fail to claim rewards if one or more epochs have already been claimed', async () => {
const promotionId = 1;
const wallet2Amount = toWei('750');
const wallet3Amount = toWei('250');
await ticket.mint(wallet2.address, wallet2Amount);
await ticket.mint(wallet3.address, wallet3Amount);
await createPromotion(ticket.address);
await increaseTime(epochDuration * 257);
await expect(
twabRewards.claimRewards(wallet2.address, promotionId, ['256', '256']),
).to.be.revertedWith('TwabRewards/rewards-already-claimed');
});
Recommended Mitigation Steps
A possible fix could be to change the type of _epochId to uint8 in:
_calculateRewardAmount()_updateClaimedEpoch()_isClaimedEpoch()
and change the type of _epochIds to uint8[] in claimRewards().
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
The bit-checking logic uses a shift by an epoch ID that can exceed the 256-bit width of the storage variable, causing the check to always return false for large epoch IDs.
Impact
An attacker could claim a reward multiple times and retrieve all tokens associated with the promotion.
What the attacker needed
A promotion with rewards; wait 256 epochs after promotion start
The fix
The fix should change the epoch ID type to uint8 in the relevant functions and the array type to uint8[] in claimRewards.
How it was found
- Mint tickets to two wallets to create a promotion.
- Create a promotion and wait for 257 epochs to pass.
- Call claimRewards with the same epoch ID twice (256) and observe that the transaction succeeds instead of reverting.
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 rewards a user has already claimed by storing a series of bits (0s and 1s) in a single large number. Each bit corresponds to a reward period (epoch). The code checks if a bit is set to know if a reward was claimed, but if the period number is 256 or higher, the check looks at a bit that doesn't exist and always says 'not claimed'. So a user can claim the same reward over and over.
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.