Skip to content
Smart contracts Audit finding Advanced

When rewardToken is erc1155/erc777, an attacker can reenter and cause funds to be stuck in the contract forever

When the reward token is an ERC1155 or ERC777, an attacker can reenter the claim function and redeem an additional token, causing the redeemedTokens counter to be lower than the actual number of redeemed tokens. This mismatch can make the withdrawRemainingTokens calculation underflow and revert, leaving funds stuck in the contract.

simon135 Follow with 0x4non, ForkEth and 2 others

Originally disclosed on Code4rena

Submitted by simon135, also found by 0x4non, ForkEth, zaskoh, and ArmedGoose

https://github.com/rabbitholegg/quest-protocol/blob/068d628f019e9469aecbf676370075c1f6c980fd/contracts/Quest.sol#L113-L116

If the reward token is erc1155/erc777, an attacker can reenter and then buy/transfer another unclaimed token to the attacker address and then the var redeemTokens won’t be equal to how many tokens were actually redeemed.

Proof of Concept

Example: Reward token is an erc1155 that has _afterTokenTransfer Alice(attacker) has 2 receipt tokens, the first one is on a smart contract that will do the reentrancy, and the second one is on Alice’s address but is approved to transfer to the smart contract(the own that holds the first receipt)

  1. Alice calls the sc to claim rewards
 IERC1155(rewardToken).safeTransferFrom(address(this), msg.sender, rewardAmountInWeiOrTokenId, amount_, '0x00');
  1. _afterTokenTransfer which causes the sc to call a function in its fallback function that transfers the approved token to the sc
   try IERC1155Receiver(to).onERC1155Received(operator, from, id, amount, data) returns (bytes4 response) {
  1. We then reenter with recipient, not yet claimed token and we claim it

Result: The invariant that redeemedTokens = tokens that are redeemed is false because it doesn’t account for the first token that we reentered. The issue is worse with erc777 tokens because of the fact that accounting will be in the withdrawRemainingTokens function

        uint256 unclaimedTokens = (receiptRedeemers() - redeemedTokens) * rewardAmountInWeiOrTokenId;
        uint256 nonClaimableTokens = IERC20(rewardToken).balanceOf(address(this)) - protocolFee() - unclaimedTokens;
        IERC20(rewardToken).safeTransfer(to_, nonClaimableTokens);

after the reentrancy ex: redeemedTokens=9 but should be 10 receiptRedeemers()=12 rewardAmountInWeiOrTokenId=1e5 unclaimedTokens=300000 assuming they are some tokens left balance(address(this)=201000 and protocolFee=500 nonClaimableTokens=201000 - 500 - 300000 it would revert ( negative numbers with uint) and funds would be stuck in the contract forever The real estimate for nonClaimableTokens=201000-500-200000=500 and the owner can get funds out but 500 wei will be lost in the contract and it can get worse with large amounts of quests and the attacker reentering multiple times to cause a bigger gap between the real redeemedTokens

Recommended Mitigation Steps

Add nonReentrancy modifier

kirk-baird (judge) decreased severity to Medium

waynehoover (RabbitHole) confirmed


The original report as published on Code4rena, by simon135. 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.

How it worked

Root cause

Likely the claim function updates the redeemedTokens counter after transferring the reward token, allowing a reentrant call to claim again before the counter is updated.

Impact

The redeemedTokens counter can be lower than the actual number of redeemed tokens, which may cause withdrawRemainingTokens to revert due to underflow and leave funds stuck in the contract; the text notes the issue is worse with ERC777 tokens.

What the attacker needed

Reward token is ERC1155 or ERC777; attacker holds receipt tokens; attacker can deploy a contract to receive callback

The fix

Add a nonReentrancy modifier to the claim function.

How it was found

  1. Alice calls the smart contract to claim rewards.
  2. The reward token transfer triggers a callback in the attacker's contract.
  3. The attacker's contract transfers an approved receipt token to itself.
  4. The attacker reenters the claim function with the newly acquired token and claims again.

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.

Some tokens, like ERC1155 and ERC777, can call back into the contract that is sending them. This is called a reentrancy callback. The contract updates its count of redeemed tokens after sending the reward, so the attacker can claim again during the callback and the count becomes wrong. Later, when the owner tries to withdraw leftover tokens, the wrong count can cause a calculation to fail and the funds stay locked.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by simon135

View all

1 more disclosed report, best first.

More from RabbitHole Quest Protocol contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.