Skip to content
Business logic Audit finding Advanced

When fallback oracle is frozen fetchPrice can r

When the fallback oracle is frozen and the status is usingFallbackChainlinkFrozen, fetchPrice returns INVALID_PRICE even if Chainlink is working, so the caller uses the last good price. An attacker could exploit the price difference between the last good price and the actual Chainlink price to gain a collateral surplus, potentially leading to protocol insolvency.

savi0ur Follow

Originally disclosed on Immunefi

Informational View original
  • Disclosed after under an hour

28967 - [SC - Insight] When fallback oracle is frozen fetchPrice can r...

Submitted on Mar 3rd 2024 at 16:40:29 UTC by @savi0ur for Boost | eBTC

Report ID: #28967

Report type: Smart Contract

Report severity: Insight

Target: https://github.com/ebtc-protocol/ebtc/blob/release-0.7/packages/contracts/contracts/PriceFeed.sol

Impacts:

  • Protocol insolvency

Description

Bug Description

When fallback oracle is operational and chainlink oracle is frozen, status is Status.usingFallbackChainlinkFrozen. Now upon execution of fetchPrice() again, it will execute below block of if statement. https://github.com/ebtc-protocol/ebtc/blob/a96bd000c23425f04c3223a441a625bfb21f6686/packages/contracts/contracts/PriceFeed.sol#L299-L363

Solidity
// --- CASE 4: Using Fallback, and Chainlink is frozen ---
if (status == Status.usingFallbackChainlinkFrozen) {
    if (_chainlinkIsBroken(chainlinkResponse, prevChainlinkResponse)) {
        // ...
    }

    if (_chainlinkIsFrozen(chainlinkResponse)) {
        // ...
    }

    if (_chainlinkPriceChangeAboveMax(chainlinkResponse, prevChainlinkResponse)) {
        // ...
    }

    // if Chainlink is live and Fallback is broken, remember Fallback broke, and return Chainlink price
    if (_fallbackIsBroken(fallbackResponse)) {
        _changeStatus(Status.usingChainlinkFallbackUntrusted);
        return _storeChainlinkPrice(chainlinkResponse.answer);
    }

    // If Chainlink is live and Fallback is frozen, just use last good price (no status change) since we have no basis for comparison
    if (_fallbackIsFrozen(fallbackResponse)) {
        return INVALID_PRICE;
    }

If chainlink oracle is working this time and fallback oracle is frozen, status remain unchanged i.e, Status.usingFallbackChainlinkFrozen even if chainlink oracle is working as expected - Not Broken, Not Frozen and No Price change above 5% from last reported chainlink price. And then return INVALID_PRICE and caller will consume the lastGoodPrice.

Its blindly assuming that chainlink oracle is still not operational and its frozen and continue to be in the same state Status.usingFallbackChainlinkFrozen even if chainlink is working perfectly fine.

Suppose chainlink oracle is still operational and fallback oracle is still frozen, upon calling fetchPrice() again will execute same block of statements as shown above and report the same INVALID_PRICE and caller will consume the same lastGoodPrice as status is still `Status.usingFallbackChainlinkFrozen.

Now being status in Status.usingFallbackChainlinkFrozen, this will always return INVALID_PRICE and caller consuming lastGoodPrice as long as fallback is frozen, even if chainlink is ready to serve correct price.

Impact

Since its always returning lastGoodPrice when fallback is frozen and status = Status.usingFallbackChainlinkFrozen even if chainlink is operational, there is a possibility of actual price of eBTC feed deviates more/less from lastGoodPrice.

In such case, user can monitor fallback oracle to bring up. Once its up and report the price which is almost similar to chainlink price but deviates more/less from lastGoodPrice, then since status is still Status.usingFallbackChainlinkFrozen, it will then check _bothOraclesSimilarPrice() which return true as prices reported are almost similar and then change status to Status.chainlinkWorking and report chainlink price.

https://github.com/ebtc-protocol/ebtc/blob/a96bd000c23425f04c3223a441a625bfb21f6686/packages/contracts/contracts/PriceFeed.sol#L365-L370

Solidity
// If Chainlink is live and Fallback is working, compare prices. Switch to Chainlink
// if prices are within 5%, and return Chainlink price.
if (_bothOraclesSimilarPrice(chainlinkResponse, fallbackResponse)) {
	_changeStatus(Status.chainlinkWorking);
	return _storeChainlinkPrice(chainlinkResponse.answer);
}

Now in this situation, user can take advantage of this situation as follows:

  • Chainlink eBTC Price < lastGoodPrice: A user can redeem a CDP with the lastGoodPrice and then open a new CDP with the current Chainlink price, obtaining a collateral surplus.
  • Chainlink eBTC Price > lastGoodPrice: A user can open a CDP with the lastGoodPrice and then redeem it with the current Chainlink price, obtaining a collateral surplus.

In both cases, collateral surplus is obtained at the expense of the protocol with no risk for the user. Doing this multiple times, by multiple users, will results in collateral being depleted slowly and eventually leads to protocol insolvency where debt (eBTC) > collateral (stETH).

Recommendation

Make sure to handle the case when status == Status.usingFallbackChainlinkFrozen and fallback is frozen, use chainlink price when chainlink is working as expected and fallback is broken/frozen. For example,

Diff
// --- CASE 4: Using Fallback, and Chainlink is frozen ---
if (status == Status.usingFallbackChainlinkFrozen) {
    if (_chainlinkIsBroken(chainlinkResponse, prevChainlinkResponse)) {
        // ...
    }

    if (_chainlinkIsFrozen(chainlinkResponse)) {
        // ...
    }

    if (_chainlinkPriceChangeAboveMax(chainlinkResponse, prevChainlinkResponse)) {
        // ...
    }

    // if Chainlink is live and Fallback is broken, remember Fallback broke, and return Chainlink price
    if (_fallbackIsBroken(fallbackResponse)) {
        _changeStatus(Status.usingChainlinkFallbackUntrusted);
        return _storeChainlinkPrice(chainlinkResponse.answer);
    }

    // If Chainlink is live and Fallback is frozen, just use last good price (no status change) since we have no basis for comparison
    if (_fallbackIsFrozen(fallbackResponse)) {
+		_changeStatus(Status.usingChainlinkFallbackUntrusted);
+       return _storeChainlinkPrice(chainlinkResponse.answer);
-       return INVALID_PRICE;
    }

References

https://github.com/ebtc-protocol/ebtc/blob/a96bd000c23425f04c3223a441a625bfb21f6686/packages/contracts/contracts/PriceFeed.sol

Proof Of Concept

Steps to Run using Foundry:

  • Paste following foundry code in /ebtc-boost/packages/contracts/foundry_test/PriceFeed.stateTransitions.t.sol
  • Run using forge test --match-contract PriceFeedStateTransitionTest --match-test testStatusUnchangedWhenChainlinkWorking -vvvv
Solidity
function testStatusUnchangedWhenChainlinkWorking() public {
    // froze CL
    _frozeChainlink(_mockChainLinkEthBTC);

    // update state machine
    priceFeedTester.fetchPrice();
    IPriceFeed.Status status = priceFeedTester.status();
    assertEq(uint256(status), 3); // usingFallbackChainlinkFrozen

    // restore CL and froze FB
    _restoreChainlinkPriceAndTimestamp(_mockChainLinkEthBTC, initEthBTCPrice);
    _frozeFallback();

    // update state machine again
    priceFeedTester.fetchPrice();
    status = priceFeedTester.status();
    assertEq(uint256(status), 3); // usingFallbackChainlinkFrozen
}

The original report as published on Immunefi, by savi0ur. Images and videos are our own copies; raw HTML is shown as text. View it on Immunefi.

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

Likely the state machine does not update the status when Chainlink is working and the fallback is frozen, so it keeps returning INVALID_PRICE and the caller uses the last good price.

Impact

Users could repeatedly exploit the price difference to obtain a collateral surplus, which may lead to protocol insolvency where debt exceeds collateral.

What the attacker needed

Fallback oracle is frozen; Chainlink oracle is working; status is usingFallbackChainlinkFrozen

The fix

The code should be changed to switch to using Chainlink price when Chainlink is working and the fallback is frozen, instead of returning INVALID_PRICE.

How it was found

  1. Freeze the Chainlink oracle and call fetchPrice, which sets status to usingFallbackChainlinkFrozen.
  2. Restore Chainlink price and timestamp, and freeze the fallback oracle.
  3. Call fetchPrice again; the status remains usingFallbackChainlinkFrozen and INVALID_PRICE is returned.

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 price feed contract uses two oracles (Chainlink and a fallback) to get the price of eBTC. If one oracle fails, it switches to the other. The bug is that when the fallback is frozen but Chainlink is working again, the contract still returns an old saved price instead of the fresh Chainlink price. This lets users trade at the outdated price and profit at the protocol's expense.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by savi0ur

View all

18 more disclosed reports, best first.

More from eBTC

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.