Users may not claim Erc1155 rewards when the Quest has ended
The Erc1155Quest contract's withdrawRemainingTokens function transfers the entire remaining token balance to the owner when the quest ends, without reserving tokens for unclaimed rewards. As a result, users who try to claim after the owner withdraws will have their transactions revert because the contract no longer holds enough tokens.
RaymondFam Follow with timongty, CodingNameKiki and 32 others
Originally disclosed on Code4rena
Submitted by RaymondFam, also found by timongty, CodingNameKiki, MiniGlome, holme, Aymen0909, AlexCzm, CodingNameKiki, adriro, StErMi, Josiah, minhquanym, ubermensch, peanuts, BClabs, wait, cccz, cccz, rbserver, bin2chen, KIntern_NA, gzeon, peakbolt, 0xMirce, chaduke, ElKu, libratus, omis, zaskoh, AkshaySrivastav, hihen, usmannk, HollaDieWaldfee, csanuragjain, and rvierdiiev
https://github.com/rabbitholegg/quest-protocol/blob/8c4c1f71221570b14a0479c216583342bd652d8d/contracts/Erc1155Quest.sol#L60 https://github.com/rabbitholegg/quest-protocol/blob/8c4c1f71221570b14a0479c216583342bd652d8d/contracts/Quest.sol#L114 https://github.com/rabbitholegg/quest-protocol/blob/8c4c1f71221570b14a0479c216583342bd652d8d/contracts/Erc1155Quest.sol#L41-L43
Unlike Erc20Quest.sol, owner of Erc1155Quest.sol is going to withdraw the remaining tokens from the contract when block.timestamp == endTime without deducting the unclaimedTokens. As a result, users will be denied of service when attempting to call the inherited claim() from Quest.sol.
Proof of Concept
As can be seen from the code block below, when the Quest time has ended, withdrawRemainingTokens() is going to withdraw the remaining tokens from the contract on line 60:
File: Erc1155Quest.sol#L52-L63
/// @dev Withdraws the remaining tokens from the contract. Only able to be called by owner
/// @param to_ The address to send the remaining tokens to
function withdrawRemainingTokens(address to_) public override onlyOwner {
super.withdrawRemainingTokens(to_);
IERC1155(rewardToken).safeTransferFrom(
address(this),
to_,
rewardAmountInWeiOrTokenId,
60: IERC1155(rewardToken).balanceOf(address(this), rewardAmountInWeiOrTokenId),
'0x00'
);
}
When a user tries to call claim() below, line 114 is going to internally invoke _transferRewards():
/// @notice Allows user to claim the rewards entitled to them
/// @dev User can claim based on the (unclaimed) number of tokens they own of the Quest
function claim() public virtual onlyQuestActive {
if (isPaused) revert QuestPaused();
uint[] memory tokens = rabbitHoleReceiptContract.getOwnedTokenIdsOfQuest(questId, msg.sender);
if (tokens.length == 0) revert NoTokensToClaim();
uint256 redeemableTokenCount = 0;
for (uint i = 0; i < tokens.length; i++) {
if (!isClaimed(tokens[i])) {
redeemableTokenCount++;
}
}
if (redeemableTokenCount == 0) revert AlreadyClaimed();
uint256 totalRedeemableRewards = _calculateRewards(redeemableTokenCount);
_setClaimed(tokens);
114: _transferRewards(totalRedeemableRewards);
redeemedTokens += redeemableTokenCount;
emit Claimed(msg.sender, totalRedeemableRewards);
}
safeTransferFrom() is going to revert on line 42 because the token balance of the contract is now zero. i.e. less than amount_:
File: Erc1155Quest.sol#L39-L43
/// @dev Transfers the reward token `rewardAmountInWeiOrTokenId` to the msg.sender
/// @param amount_ The amount of reward tokens to transfer
function _transferRewards(uint256 amount_) internal override {
42: IERC1155(rewardToken).safeTransferFrom(address(this), msg.sender, rewardAmountInWeiOrTokenId, amount_, '0x00');
}
Recommended Mitigation Steps
Consider refactoring withdrawRemainingTokens() as follows:
(Note: The contract will have to separately import {QuestFactory} from ‘./QuestFactory.sol’ and initialize questFactoryContract.
+ function receiptRedeemers() public view returns (uint256) {
+ return questFactoryContract.getNumberMinted(questId);
+ }
function withdrawRemainingTokens(address to_) public override onlyOwner {
super.withdrawRemainingTokens(to_);
+ uint unclaimedTokens = (receiptRedeemers() - redeemedTokens)
+ uint256 nonClaimableTokens = IERC1155(rewardToken).balanceOf(address(this), rewardAmountInWeiOrTokenId) - unclaimedTokens;
IERC1155(rewardToken).safeTransferFrom(
address(this),
to_,
rewardAmountInWeiOrTokenId,
- IERC1155(rewardToken).balanceOf(address(this), rewardAmountInWeiOrTokenId),
+ nonClaimableTokens,
'0x00'
);
}
kirk-baird (judge) increased severity to High
waynehoover (RabbitHole) disagreed with severity and commented:
I agree that this is an issue, but not a high risk issue. I expect high risk issues to be issues that can be called by anyone, not owners.
As owners there are plenty of ways we can sabotage our contracts (for example via the set* functions) it is an issue for an owner.
The owner understands how this function works, so they can be sure not to call it before all users have called claim.
kirk-baird (judge) decreased severity to Medium and commented:
Similarly to
#122, this is anonlyOwnerfunction and therefore the likelihood is significantly reduce. Therefore I’m going to downgrade this issue to Medium.
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 withdrawRemainingTokens function does not subtract the amount of unclaimed rewards from the balance before transferring tokens to the owner.
Impact
Users may be unable to claim their earned ERC1155 rewards after the quest ends if the owner withdraws the remaining tokens first, resulting in a denial of service for those users.
What the attacker needed
Owner calls withdrawRemainingTokens after quest ends; User has not yet claimed their reward
The fix
The withdrawRemainingTokens function should be modified to only withdraw tokens that are not needed for unclaimed rewards, by calculating the unclaimed amount and subtracting it from the balance before transferring.
How it was found
- The quest ends (block.timestamp == endTime).
- The owner calls withdrawRemainingTokens, which transfers all remaining reward tokens to the owner.
- A user who has not yet claimed calls claim().
- The claim function tries to transfer reward tokens to the user, but the contract balance is zero, so the transfer reverts.
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.
In this quest contract, users earn rewards by claiming them after completing tasks. When the quest ends, the owner can withdraw leftover reward tokens. But the owner's withdrawal takes all tokens, including those still owed to users who haven't claimed yet. So if a user tries to claim after the owner withdraws, the transfer fails because the contract has no tokens left.
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.