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

Erc20Quest.sol: withdrawRemainingTokens function calculates wrong amount after protocol fees are withdrawn #61

Closed
code423n4 opened this issue Jan 26, 2023 · 8 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-122 satisfactory satisfies C4 submission criteria; eligible for awards sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Lines of code

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

Vulnerability details

Impact

The Erc20Quest.withdrawRemainingTokens function (https://github.com/rabbitholegg/quest-protocol/blob/8c4c1f71221570b14a0479c216583342bd652d8d/contracts/Erc20Quest.sol#L81-L87) is used to withdraw all remaining reward tokens once the endTime is reached.

The issue is that this function incorporates the protocolFee into its calculation.

The protocolFee however can already be paid out when the Erc20Quest.withdrawRemainingTokens function is called. There is no mechanism by which Erc20Quest.withdrawFee() must be called after Erc20Quest.withdrawRemainingTokens.

At the very least this results in a withdrawn amount that is too small if protocol fees have been withdrawn before.

However the fact that protocol fees are subtracted that are not in the contract anymore can also make the calculation underflow which causes an amount up to the protocol fees to be stuck in the contract.

Proof of Concept

The calculation to determine the amount of tokens to withdraw is this:

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

uint256 nonClaimableTokens = IERC20(rewardToken).balanceOf(address(this)) - protocolFee() - unclaimedTokens;

Assume the following:

IERC20(rewardToken).balanceOf(address(this)) = 40 USDC
protocolFee() = 50 USDC
unclaimedTokens = 0 USDC

In this situation all 40 USDC should be withdrawn because there are no unclaimedTokens and the protocolFee is already paid out.

However the calculation reverts which causes the 40 USDC to be stuck in the contract.

Tools Used

VSCode

Recommended Mitigation Steps

I discussed this issue with the sponsor and it was decided that probably the best solution is to save the yet to be paid fees in a uint variable that is subtracted from when fees are paid out.

This variable is added to when receipts are minted.

This solution also requires a modification of the data flow, i.e. the quest contract must know when a new receipt is minted.

The sponsor mentioned that this will be investigated as part of a broader refactoring of the data flow.

@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 26, 2023
code423n4 added a commit that referenced this issue Jan 26, 2023
@c4-judge c4-judge closed this as completed Feb 3, 2023
@c4-judge
Copy link
Contributor

c4-judge commented Feb 3, 2023

kirk-baird marked the issue as duplicate of #42

@c4-judge
Copy link
Contributor

c4-judge commented Feb 6, 2023

kirk-baird marked the issue as not a duplicate

@c4-judge c4-judge reopened this Feb 6, 2023
@c4-judge c4-judge added primary issue Highest quality submission among a set of duplicates and removed duplicate-42 labels Feb 6, 2023
@c4-judge
Copy link
Contributor

c4-judge commented Feb 6, 2023

kirk-baird marked the issue as primary issue

@c4-sponsor c4-sponsor added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Feb 7, 2023
@c4-sponsor
Copy link

waynehoover marked the issue as sponsor confirmed

@c4-judge c4-judge added 3 (High Risk) Assets can be stolen/lost/compromised directly upgraded by judge Original issue severity upgraded from QA/Gas by judge and removed 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value labels Feb 14, 2023
@c4-judge
Copy link
Contributor

kirk-baird changed the severity to 3 (High Risk)

@c4-judge c4-judge added duplicate-122 and removed primary issue Highest quality submission among a set of duplicates labels Feb 14, 2023
@c4-judge
Copy link
Contributor

kirk-baird marked issue #122 as primary and marked this issue as a duplicate of 122

@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 removed 3 (High Risk) Assets can be stolen/lost/compromised directly upgraded by judge Original issue severity upgraded from QA/Gas by judge labels Feb 23, 2023
@c4-judge
Copy link
Contributor

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

@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 labels Feb 23, 2023
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-122 satisfactory satisfies C4 submission criteria; eligible for awards sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

3 participants