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

buy and sell will revert when a low decimal ERC20 token is used as baseToken #208

Closed
code423n4 opened this issue Dec 18, 2022 · 2 comments
Closed
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 duplicate-243 edited-by-warden satisfactory satisfies C4 submission criteria; eligible for awards

Comments

@code423n4
Copy link
Contributor

code423n4 commented Dec 18, 2022

Lines of code

https://github.com/code-423n4/2022-12-caviar/blob/main/src/Pair.sol#L154
https://github.com/code-423n4/2022-12-caviar/blob/main/src/Pair.sol#L186
https://github.com/code-423n4/2022-12-caviar/blob/main/src/Pair.sol#L398-L400
https://github.com/code-423n4/2022-12-caviar/blob/main/src/Pair.sol#L406-L409

Vulnerability details

Impact

When buying or selling fractional Tokens from pair, buyQuote and sellQuote is used to calculate how much base Token is needed in exchange of the amount of fractional Tokens user is buying or selling. In the event that a low decimal ERC20 token is used (USDC with 6 decimals), buyQuote and sellQuote will underflow and revert unless fractional tokens amount is big enough.

Proof of Concept

In buy, buyQuote is used to calculate the amount of input base Tokens for the amount of fractional tokens user wants to buy.

        inputAmount = buyQuote(outputAmount);

However, we can see that in buyQuote, fractionalTokenReserves (18 decimals) will be significantly bigger than baseTokenReserves if base Token has a small decimal count. i.e (USDC 6 decimals). buyQuote and sellQuote will underflow and revert due to baseTokenReserves divide by fractionalTokenReserves.

    function buyQuote(uint256 outputAmount) public view returns (uint256) {
        return (outputAmount * 1000 * baseTokenReserves()) / ((fractionalTokenReserves() - outputAmount) * 997);
    }

Tools Used

Manual Review

Recommended Mitigation Steps

There should be a minimum for outputAmount depending on the number of decimal the ERC20 used for base Token has.

require(outputAmount > 10 ** 18- ERC20(baseToken).decimals, "outputAmount needs to be more )

Or if possible, ONE can be based on base token decimals.

@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 Dec 18, 2022
code423n4 added a commit that referenced this issue Dec 18, 2022
@code423n4 code423n4 changed the title buy and sell will always revert when a small decimal ERC20 token is used as baseToken buy and sell will revert when a low decimal ERC20 token is used as baseToken Dec 18, 2022
@c4-judge
Copy link
Contributor

berndartmueller marked the issue as duplicate of #243

@c4-judge
Copy link
Contributor

berndartmueller marked the issue as satisfactory

@c4-judge c4-judge added the satisfactory satisfies C4 submission criteria; eligible for awards label Jan 10, 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 duplicate-243 edited-by-warden satisfactory satisfies C4 submission criteria; eligible for awards
Projects
None yet
Development

No branches or pull requests

2 participants