Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERC1155Quest contract owner could withdraw all users rewards when the quest has ended, if users don't claim a reward before the end #298

Closed
code423n4 opened this issue Jan 29, 2023 · 5 comments
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working downgraded by judge Judge downgraded the risk level of this issue duplicate-528 edited-by-warden satisfactory satisfies C4 submission criteria; eligible for awards

Comments

@code423n4
Copy link
Contributor

code423n4 commented Jan 29, 2023

Lines of code

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

Vulnerability details

Impact

When the quest ended, the ERC1155Quest contract owner could withdraw all remaining tokens, including users' reward tokens which are not claimed before the end. Because of that, if the user wants to claim their rewards after the quest end and after the ERC1155Quest contract owner calls a function withdrawRemainingTokens it will get an error that there are not enough tokens at the ERC1155Quest contract and not possible to claim a reward.

On the other side, in the ERC20Quest contract by lines Erc20Quest.sol#L84:L86 there is calculation how many tokens are not claimed, so the contract owner will get the difference between current contract balance and not claimed rewards. Because of that calculation, users could claim their reward after the ERC20Quest ended and the contract owner calls a function withdrawRemainingTokens.

Proof of Concept

Add two tests in Erc1155Quest.spec.ts

it('after quest end, should claim and withdraw remaining tokens', async () => {
    await deployedRabbitholeReceiptContract.mint(firstAddress.address, questId)
    await deployedQuestContract.start()

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

    expect(await deployedSampleErc1155Contract.balanceOf(firstAddress.address, rewardAmount)).to.equal(0)
    expect(await deployedSampleErc1155Contract.balanceOf(owner.address, rewardAmount)).to.equal(0)

    await deployedQuestContract.connect(firstAddress).claim()

    await deployedQuestContract.connect(owner).withdrawRemainingTokens(owner.address)

    expect(await deployedSampleErc1155Contract.balanceOf(firstAddress.address, rewardAmount)).to.equal(1)
    expect(await deployedSampleErc1155Contract.balanceOf(owner.address, rewardAmount)).to.equal(99)
    await ethers.provider.send('evm_increaseTime', [-86400])
})
it('after quest end, should withdraw remaining tokens and cannot claim', async () => {
    await deployedRabbitholeReceiptContract.mint(firstAddress.address, questId)
    await deployedQuestContract.start()

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

    expect(await deployedSampleErc1155Contract.balanceOf(firstAddress.address, rewardAmount)).to.equal(0)
    expect(await deployedSampleErc1155Contract.balanceOf(owner.address, rewardAmount)).to.equal(0)

    await deployedQuestContract.connect(owner).withdrawRemainingTokens(owner.address)
    expect(await deployedSampleErc1155Contract.balanceOf(owner.address, rewardAmount)).to.equal(100)
    await expect(
    deployedQuestContract.connect(firstAddress).claim()
    ).to.be.revertedWith('ERC1155: insufficient balance for transfer') 

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

As seen from the first test, if the account firstAddress claims reward before the owner calls withdrawRemainingTokens, all will be good, and firstAddress will have 1 unit of reward token, and the contract owner will have 99 units.

But, if a contract owner calls withdrawRemainingTokens before the account firstAddress claim rewards, he will get all reward tokens (he will have 100 units), and on the other side, firstAddress will get error ERC1155: insufficient balance for transfer.

Tools Used

VSCode, Hardhat, Solidity Visual Developer

Recommended Mitigation Steps

In the ERC1155Quest.sol contract (function Erc1155Quest.sol#L54):

  1. calculate how many unclaimed rewards uint unclaimedRewards = questFactoryContract.getNumberMinted(questId) - redeemedTokens;
  2. calculate how many are non-claimable rewards IERC1155(rewardToken).balanceOf(address(this), rewardAmountInWeiOrTokenId) - unclaimedRewards
  3. transfer non-claimable rewards
function withdrawRemainingTokens(address to_) public override onlyOwner {
        super.withdrawRemainingTokens(to_);
        uint256 unclaimedRewards = questFactoryContract.getNumberMinted(questId) - redeemedTokens;
        uint256 nonClaimableRewards =
            IERC1155(rewardToken).balanceOf(address(this), rewardAmountInWeiOrTokenId) - unclaimedRewards;
        IERC1155(rewardToken).safeTransferFrom(
            address(this), to_, rewardAmountInWeiOrTokenId, nonClaimableRewards, "0x00"
        );
}
@code423n4 code423n4 added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working labels Jan 29, 2023
code423n4 added a commit that referenced this issue Jan 29, 2023
@code423n4 code423n4 changed the title ERC1155Quest contract owner could withdraw all users rewards when the quest has ended if users don't claim a reward before the end ERC1155Quest contract owner could withdraw all users rewards when the quest has ended, if users don't claim a reward before the end Jan 29, 2023
@c4-judge c4-judge closed this as completed Feb 5, 2023
@c4-judge
Copy link
Contributor

c4-judge commented Feb 5, 2023

kirk-baird marked the issue as duplicate of #42

@c4-judge c4-judge added downgraded by judge Judge downgraded the risk level of this issue QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax and removed 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value labels Feb 10, 2023
@c4-judge
Copy link
Contributor

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

@c4-judge c4-judge reopened this Feb 10, 2023
@c4-judge c4-judge added 3 (High Risk) Assets can be stolen/lost/compromised directly and removed downgraded by judge Judge downgraded the risk level of this issue QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax labels Feb 10, 2023
@c4-judge
Copy link
Contributor

This previously downgraded issue has been upgraded by kirk-baird

@c4-judge c4-judge added the satisfactory satisfies C4 submission criteria; eligible for awards label Feb 14, 2023
@c4-judge
Copy link
Contributor

kirk-baird marked the issue as satisfactory

@c4-judge c4-judge added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value downgraded by judge Judge downgraded the risk level of this issue and removed 3 (High Risk) Assets can be stolen/lost/compromised directly labels Feb 23, 2023
@c4-judge
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working downgraded by judge Judge downgraded the risk level of this issue duplicate-528 edited-by-warden satisfactory satisfies C4 submission criteria; eligible for awards
Projects
None yet
Development

No branches or pull requests

2 participants