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

TradingLibrary.verifyPrice: Negative prices not correctly handled #297

Closed
code423n4 opened this issue Dec 16, 2022 · 8 comments
Closed

TradingLibrary.verifyPrice: Negative prices not correctly handled #297

code423n4 opened this issue Dec 16, 2022 · 8 comments
Labels
bug Something isn't working downgraded by judge Judge downgraded the risk level of this issue grade-c QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue

Comments

@code423n4
Copy link
Contributor

Lines of code

https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/utils/TradingLibrary.sol#L113-L115

Vulnerability details

Impact

Referring to the docs, Chainlink oracles are returning the price as an int256, which means that the answer can be a negative price.

https://docs.chain.link/data-feeds/price-feeds/api-reference

Later, this price is casted as an uint256, which overflows when price < 0.

            int256 assetChainlinkPriceInt = IPrice(_chainlinkFeed).latestAnswer();
            if (assetChainlinkPriceInt != 0) {
                uint256 assetChainlinkPrice = uint256(assetChainlinkPriceInt) * 10**(18 - IPrice(_chainlinkFeed).decimals());

Let's say _chainlinkFeed.latestAnswer() = -1. Then, price will be equal to type(uint256).max (i.e., a huge number) and this wrong price will be used by the system, leading to system stuck.

Proof of Concept

https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/utils/TradingLibrary.sol#L113-L115

Tools Used

None

Recommended Mitigation Steps

Check that the latest answer is > 0 before casting

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

Agree with finding, but not convinced by severity, what are the odds of a negative CL feed? Has this ever happened?

@GalloDaSballo
Copy link

Have asked some info to CL and believe this finding will be downgraded to R at most (not even Low)

A CL feed ultimately has min and max boundaries, and Price Feeds will have positive min / max values

Negative prices seem to be supported for Commodities Future if you were wondering

@TriHaz
Copy link

TriHaz commented Jan 3, 2023

Price feed can't be negative. Even if we assumed it can be, code will revert.

@c4-sponsor
Copy link

TriHaz marked the issue as sponsor disputed

@c4-sponsor c4-sponsor added the sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue label Jan 3, 2023
@GalloDaSballo
Copy link

Downgrading to R as I would recommend a check before casting, however no clear risk was shown

R

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

Duplicate of #334

@GalloDaSballo
Copy link

Note that a cast to uint256 will not revert on a negative number, you'll get the twos complement of the value
https://docs.soliditylang.org/en/v0.8.17/types.html?highlight=int256#explicit-conversions

@Simon-Busch
Copy link

Remove Dup of 334 label and mark as unsatisfactory as requested by @GalloDaSballo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working downgraded by judge Judge downgraded the risk level of this issue grade-c QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue
Projects
None yet
Development

No branches or pull requests

6 participants