sherlock-audit / 2023-12-flatmoney-judging

9 stars 8 forks source link

bareli - price manipulation #213

Closed sherlock-admin closed 5 months ago

sherlock-admin commented 5 months ago

bareli

medium

price manipulation

Summary

Chainlink aggregators have a built in circuit breaker if the price of an asset goes outside of a predetermined price band. The result is that if an asset experiences a huge drop in value (i.e. LUNA crash) the price of the oracle will continue to return the minPrice instead of the actual price of the asset. This would allow user to continue borrowing with the asset but at the wrong price. This is exactly what happened to Venus on BSC when LUNA imploded.

Vulnerability Detail ChainlinkAdapterOracle uses the ChainlinkFeedRegistry to obtain the price of the requested tokens.

function _getOnchainPrice() internal view returns (uint256 price, uint256 timestamp) { IChainlinkAggregatorV3 oracle = onchainOracle.oracleContract; if (address(oracle) == address(0)) revert FlatcoinErrors.ZeroAddress("oracle");

@>    (, int256 _price, , uint256 updatedAt, ) = oracle.latestRoundData();
    timestamp = updatedAt;
    // check Chainlink oracle price updated within `maxAge` time.
    if (block.timestamp > timestamp + onchainOracle.maxAge)
        revert FlatcoinErrors.PriceStale(FlatcoinErrors.PriceSource.OnChain);

    if (_price > 0) {
        price = uint256(_price) * (10 ** 10); // convert Chainlink oracle decimals 8 -> 18
    } else {
        // Issue with onchain oracle indicates a serious problem
        revert FlatcoinErrors.PriceInvalid(FlatcoinErrors.PriceSource.OnChain);
    }
}

ChainlinkFeedRegistry#latestRoundData pulls the associated aggregator and requests round data from it. ChainlinkAggregators have minPrice and maxPrice circuit breakers built into them. This means that if the price of the asset drops below the minPrice, the protocol will continue to value the token at minPrice instead of it's actual value. This will allow users to take out huge amounts of bad debt and bankrupt the protocol.

Example: TokenA has a minPrice of $1. The price of TokenA drops to $0.10. The aggregator still returns $1 allowing the user to borrow against TokenA as if it is $1 which is 10x it's actual value.

Note: Chainlink oracles are used a just one piece of the OracleAggregator system and it is assumed that using a combination of other oracles, a scenario like this can be avoided. However this is not the case because the other oracles also have their flaws that can still allow this to be exploited. As an example if the chainlink oracle is being used with a UniswapV3Oracle which uses a long TWAP then this will be exploitable when the TWAP is near the minPrice on the way down. In a scenario like that it wouldn't matter what the third oracle was because it would be bypassed with the two matching oracles prices. If secondary oracles like Band are used a malicious user could DDOS relayers to prevent update pricing. Once the price becomes stale the chainlink oracle would be the only oracle left and it's price would be used.

Impact In the event that an asset crashes (i.e. LUNA) the protocol can be manipulated to give out loans at an inflated price

Code Snippet https://github.com/sherlock-audit/2023-12-flatmoney/blob/main/flatcoin-v1/src/OracleModule.sol#L145 Tool used Manual Review

Recommendation ChainlinkAdapterOracle should check the returned answer against the minPrice/maxPrice and revert if the answer is outside of the bounds:

(, int256 _price, , uint256 updatedAt, ) = oracle.latestRoundData();

Duplicate of #134

sherlock-admin commented 5 months ago

1 comment(s) were left on this issue during the judging contest.

takarez commented:

invalid