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

QA report #66

Open
code423n4 opened this issue Feb 6, 2022 · 4 comments
Open

QA report #66

code423n4 opened this issue Feb 6, 2022 · 4 comments
Labels
bug Something isn't working QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax

Comments

@code423n4
Copy link
Contributor

Lines of code

https://github.com/code-423n4/2022-02-badger-citadel/blob/84596551d62f243d13fcb2d486346dde08002f7b/contracts/TokenSaleUpgradeable.sol#L216-L224

Vulnerability details

Impact

The contract comments nor the submission text detail what happens when the user submits more tokenIn tokens than are required to recieve N tokenOut tokens, but not enough for N+1 tokens. The contract as written takes any excess contributions for itself.

Impact

Due to loss of precision, the number of tokenOut tokens receieved is rounded down and the user loses any excess contributions, and also has to deal with any tax implications, due to this undocumented and surprising behavior. If amount of funds lost is directly proportional to the ratio of the tokenOutPrice to tokenOut.decimals().

Proof of Concept

If tokenOutPrice is greater than 10**tokenOut.decimals(), loss of precision due to tokenOutAmount_ not being a floating type, will mean that the caller will lose out on fractions of tokenOut tokens. The buy() function does not return any excess tokenIn tokens, and does not keep track of fractions of tokens partially paid for, and instead keeps them for itself.

buy() calls getAmountOut():

        tokenOutAmount_ = getAmountOut(_tokenInAmount);

        boughtAmounts[msg.sender] = boughtAmountTillNow + tokenOutAmount_;
        daoCommitments[_daoId] += tokenOutAmount_;

        totalTokenIn += _tokenInAmount;
        totalTokenOutBought += tokenOutAmount_;

https://github.com/code-423n4/2022-02-badger-citadel/blob/84596551d62f243d13fcb2d486346dde08002f7b/contracts/TokenSaleUpgradeable.sol#L175-L181

And getAmount() doesn't keep track of partial tokenOutAmount_s

    function getAmountOut(uint256 _tokenInAmount)
        public
        view
        returns (uint256 tokenOutAmount_)
    {
        tokenOutAmount_ =
            (_tokenInAmount * 10**tokenOut.decimals()) /
            tokenOutPrice;
    }

https://github.com/code-423n4/2022-02-badger-citadel/blob/84596551d62f243d13fcb2d486346dde08002f7b/contracts/TokenSaleUpgradeable.sol#L216-L224

Based on this conversion comment

/// eg. 1 WBTC (8 decimals) = 40,000 CTDL ==> price = 10^8 / 40,000

https://github.com/code-423n4/2022-02-badger-citadel/blob/84596551d62f243d13fcb2d486346dde08002f7b/contracts/TokenSaleUpgradeable.sol#L32

If tokenOut were something like a BTC with only 2 decimals and tokenIn were USD, the comment would read:

/// eg. 40,000 USD (2 decimals) = 1 BTC ==> price = (40,000 * 10^2) / 1

which is 4,000,000. With this tokenOutPrice, if someone submitted $79k USDC, they'd get int((79000 * 10**2)/(4000000) which is only 1 BTC, when they spent almost 2 BTC's worth of USDC.

Tools Used

Code inspection

Recommended Mitigation Steps

The code should follow the "principle of least surprise" and require() that tokenOutPrice be less than or equal to 10**tokenOut.decimals(), or keep track of partial amounts, or refund excess contributions.

@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 Feb 6, 2022
code423n4 added a commit that referenced this issue Feb 6, 2022
@GalloDaSballo
Copy link
Collaborator

Ultimately the rounding issue is a problem with integer division, I don't think the finding as dramatic as it sounds

@GalloDaSballo GalloDaSballo added the disagree with severity Sponsor confirms validity, but disagrees with warden’s risk assessment (sponsor explain in comments) label Feb 10, 2022
@0xleastwood
Copy link
Collaborator

Duplicate of #50

@0xleastwood 0xleastwood marked this as a duplicate of #50 Mar 14, 2022
@0xleastwood 0xleastwood added duplicate This issue or pull request already exists and removed disagree with severity Sponsor confirms validity, but disagrees with warden’s risk assessment (sponsor explain in comments) labels Mar 14, 2022
@0xleastwood
Copy link
Collaborator

After further thought, I actually think this is more of an issue of state handling. Users can mis-use this function to receive slightly lower amounts than expected due to the imprecise nature of arithmetic in Solidity. I'll mark this as low to reflect this.

@CloudEllie
Copy link
Contributor

Since this issue was downgraded to a QA level, and the warden did not submit a separate QA report, we've renamed this one to "QA report" for consistency.

The original title, for the record, was "Loss of excess funds due to loss of precision."

@CloudEllie CloudEllie changed the title Loss of excess funds due to loss of precision QA report Mar 25, 2022
@CloudEllie CloudEllie added QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax and removed duplicate This issue or pull request already exists 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value labels Mar 25, 2022
@CloudEllie CloudEllie reopened this Mar 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax
Projects
None yet
Development

No branches or pull requests

4 participants