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.
Submitted by simon135, also found by 0x4non, ForkEth, zaskoh, and ArmedGoose
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)
- Alice calls the sc to
claimrewards
IERC1155(rewardToken).safeTransferFrom(address(this), msg.sender, rewardAmountInWeiOrTokenId, amount_, '0x00');
_afterTokenTransferwhich 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) {
- 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
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
- Alice calls the smart contract to claim rewards.
- The reward token transfer triggers a callback in the attacker's contract.
- The attacker's contract transfers an approved receipt token to itself.
- 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.