Chainlink price feed is not sufficiently validated and can return stale price
The Chainlink price feed verification in the trading contract does not check whether the price returned is stale, so an off-chain price that differs by more than 2% from the current Chainlink price can still pass if it is within 2% of a stale price. This allows a trading transaction that should revert to go through, weakening the price verification.
Submitted by rbserver, also found by eierina, 0x52, kwhuo68, joestakey, ladboy233, Jeiwan, 141345, bin2chen, yixxas, koxuan, 8olidity, 0xdeadbeef0x, fs0c, 0xDecorativePineapple, Rolezn, rvierdiiev, and gzeon
As mentioned by https://docs.tigris.trade/protocol/oracle, “Prices provided by the oracle network are also compared to Chainlink’s public price feeds for additional security. If prices have more than a 2% difference the transaction is reverted.” The Chainlink price verification logic in the following TradingLibrary.verifyPrice function serves this purpose. However, besides that IPrice(_chainlinkFeed).latestAnswer() uses Chainlink’s deprecated latestAnswer function, this function also does not guarantee that the price returned by the Chainlink price feed is not stale. When assetChainlinkPriceInt != 0 is true, it is still possible that assetChainlinkPriceInt is stale in which the Chainlink price verification would compare the off-chain price against a stale price returned by the Chainlink price feed. For a off-chain price that has more than a 2% difference when comparing to a more current price returned by the Chainlink price feed, this off-chain price can be incorrectly considered to have less than a 2% difference when comparing to a stale price returned by the Chainlink price feed. As a result, a trading transaction that should revert can go through, which makes the price verification much less secure.
https://github.com/code-423n4/2022-12-tigris/blob/main/contracts/utils/TradingLibrary.sol#L91-L122
function verifyPrice(
uint256 _validSignatureTimer,
uint256 _asset,
bool _chainlinkEnabled,
address _chainlinkFeed,
PriceData calldata _priceData,
bytes calldata _signature,
mapping(address => bool) storage _isNode
)
external view
{
...
if (_chainlinkEnabled && _chainlinkFeed != address(0)) {
int256 assetChainlinkPriceInt = IPrice(_chainlinkFeed).latestAnswer();
if (assetChainlinkPriceInt != 0) {
uint256 assetChainlinkPrice = uint256(assetChainlinkPriceInt) * 10**(18 - IPrice(_chainlinkFeed).decimals());
require(
_priceData.price < assetChainlinkPrice+assetChainlinkPrice*2/100 &&
_priceData.price > assetChainlinkPrice-assetChainlinkPrice*2/100, "!chainlinkPrice"
);
}
}
}
Based on https://docs.chain.link/docs/historical-price-data, the following can be done to avoid using a stale price returned by the Chainlink price feed.
- The
latestRoundDatafunction can be used instead of the deprecatedlatestAnswerfunction. roundIdandansweredInRoundare also returned. “You can checkansweredInRoundagainst the currentroundId. IfansweredInRoundis less thanroundId, the answer is being carried over. IfansweredInRoundis equal toroundId, then the answer is fresh.”- “A read can revert if the caller is requesting the details of a round that was invalid or has not yet been answered. If you are deriving a round ID without having observed it before, the round might not be complete. To check the round, validate that the timestamp on that round is not 0.”
Proof of Concept
The following steps can occur for the described scenario.
- Alice calls the
Trading.initiateMarketOrderfunction, which eventually calls theTradingLibrary.verifyPricefunction, to initiate a market order. - When the
TradingLibrary.verifyPricefunction is called, the off-chain price is compared to the price returned by the Chainlink price feed for the position asset. - The price returned by the Chainlink price feed is stale, and the off-chain price has less than a 2% difference when comparing to this stale price.
- Alice’s
Trading.initiateMarketOrdertransaction goes through. However, this transaction should revert because the off-chain price has more than a 2% difference if comparing to a more current price returned by the Chainlink price feed.
Tools Used
VS Code
Recommended Mitigation Steps
https://github.com/code-423n4/2022-12-tigris/blob/main/contracts/utils/TradingLibrary.sol#L113 can be updated to the following code.
(uint80 roundId, int256 assetChainlinkPriceInt, , uint256 updatedAt, uint80 answeredInRound) = IPrice(_chainlinkFeed).latestRoundData();
require(answeredInRound >= roundId, "price is stale");
require(updatedAt > 0, "round is incomplete");
GainsGoblin (Tigris Trade) acknowledged and commented:
We don’t want a trader’s trade to revert just because the chainlink feed is a round behind.
Alex the Entreprenerd (judge) commented:
The Warden has pointed out to a possible risk related to the price oracle returning stale data.
Alternatively to checking for latest round, a check for
updatedAtto not be too far in the past should also help mitigate the risk of offering an incorrect price which can lead to value extraction or unintended behaviour.Because of the risk, I do agree with Medium Severity.
GainsGoblin (Tigris Trade) confirmed and commented:
Mitigation: https://github.com/code-423n4/2022-12-tigris/pull/2#issuecomment-1419177187
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The contract uses Chainlink's deprecated latestAnswer function without checking the round ID or timestamp to ensure the price is fresh.
Impact
A trading transaction that should revert can go through, making the price verification much less secure and potentially allowing value extraction or unintended behaviour.
What the attacker needed
Chainlink price feed is stale; Off-chain price within 2% of stale price
The fix
The contract should use latestRoundData and check that answeredInRound is at least roundId and that updatedAt is greater than zero, or check that updatedAt is not too far in the past.
How it was found
- Alice calls Trading.initiateMarketOrder, which calls TradingLibrary.verifyPrice.
- The off-chain price is compared to the Chainlink price feed for the asset.
- The Chainlink price is stale, and the off-chain price is within 2% of that stale price.
- Alice's transaction goes through, even though it should revert because the off-chain price differs by more than 2% from a current Chainlink price.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A smart contract uses an external price feed to check that a trade price is close to the market price. If the price feed returns an old price, the check can be fooled into accepting a trade that should have been rejected. This is like using yesterday's newspaper to check today's stock price.