Skip to content
Audit finding Intermediate

money-market-contracts/oracle#feed_prices() delayed transaction may disrupt price feeds

The oracle contract's feed_prices function sets the last_updated_time to the current block time, so a delayed transaction with an old price can be accepted as fresh. An attacker could exploit this to cause wrongful liquidations and fund loss.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

https://github.com/code-423n4/2022-02-anchor/blob/7af353e3234837979a19ddc8093dc9ad3c63ab6b/contracts/money-market-contracts/contracts/oracle/src/contract.rs#L106-L113

The implementation only takes two attributes: asset and price. And the last_updated_time of the record will always be set to the current block.time.

This makes it possible for the price feeds to be disrupted when the network is congested, or the endpoint is down for a while, or the feeder bot handled the message queue inappropriately, as a result, the transactions with stale prices get accepted as fresh prices.

Since the price feeds are essential to the protocol, that can result in users’ positions being liquidated wrongfully and case fund loss to users.

Proof of Concept

Given:

  • feeder i connected to an endpoint currently experiencing degraded performance;
  • ETH price is $10,000;
  • The max_ltv ratio of ETH is 60%.
  • Alice borrowed 5,000 USDC with 1 ETH as collateral;
  • ETH price dropped to $9,000, to avoid liquidation, Alice repaid 1,000 USD;
  • The price of ETH dropped to $8,000; feeder tries to updateMainFeedData() with the latest price: $8,000, however, since the network is congested, the transactions were not get packed timely;
  • ETH price rebound to $10,000; Alice borrowed another 1,000 USDC;
  • The txs send by feeder at step 3 finally got packed, the protocol now believes the price of ETH has suddenly dropped to $8,000, as a result, Alice’s position got liquidated.

Recommended Mitigation Steps

Change to:

pub fn feed_prices(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    prices: Vec<(String, Decimal256, u64)>,
) -> Result<Response, ContractError> {
    let mut attributes = vec![attr("action", "feed_prices")];
    let sender_raw = deps.api.addr_canonicalize(info.sender.as_str())?;
    for price in prices {
        let asset: String = price.0;
        let mut updated_time: u64 = price.2;
        let price: Decimal256 = price.1;

        // Check feeder permission
        let feeder = read_feeder(deps.storage, &asset)?;
        if feeder != sender_raw {
            return Err(ContractError::Unauthorized {});
        }

        let config: Config = read_config(deps.storage)?;
        if env.block.time.seconds() > updated_time {
            // reject stale price
            if env.block.time.seconds() - updated_time > config.valid_period {
                return Err(ContractError::InvalidInputs {});
            }
        } else {
            // reject future timestamp, graceFuturePeriod can be set to 3, which means < 3s is allowed
            if updated_time - env.block.time.seconds() > config.grace_future_period {
                return Err(ContractError::InvalidInputs {});
            }
            updated_time = env.block.time.seconds();
        }

        attributes.push(attr("asset", asset.to_string()));
        attributes.push(attr("price", price.to_string()));

        store_price(
            deps.storage,
            &asset,
            &PriceInfo {
                last_updated_time: updated_time,
                price,
            },
        )?;
    }

    Ok(Response::new().add_attributes(attributes))
}

bitn8 (Anchor) disputed and commented:

We currently have a mean shorting function that pulls multiple price feeds so that if one is stale it gets rejected.

Alex the Entreprenerd (triage) commented:

Seems like the warden has shown a specific scenario, contingent on external conditions.

However, from the code, there seems to be no “mean shorting function”, at least in the code in scope.

Albert Chon (judge) commented:

Agreed with @Alex the Entreprenerd, oracle staleness is still an issue in this version of the code.


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

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 sets last_updated_time to the current block time instead of the time the price was actually fetched, so stale prices are treated as fresh.

Impact

This could result in users’ positions being liquidated wrongfully and cause fund loss to users.

What the attacker needed

Network congestion or endpoint downtime; feeder bot sends delayed transaction

The fix

The contract should be changed to accept a timestamp with each price and reject prices that are too old or too far in the future.

How it was found

  1. A feeder bot connected to a degraded endpoint tries to update the ETH price to $8,000, but the transaction is delayed due to network congestion.
  2. The ETH price rebounds to $10,000, and a user borrows more USDC.
  3. The delayed transaction is finally processed, and the protocol now believes the ETH price is $8,000.
  4. The user's position is liquidated because the stale price is treated as current.

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 oracle is a service that tells a smart contract the current price of an asset. This report says the oracle always marks a price as updated at the time the transaction is processed, even if the price was fetched much earlier. So if a transaction is delayed, an old price can be treated as current, which could cause users to be liquidated incorrectly.

More by WatchPug

View all

197 more disclosed reports, best first.

More from Anchor contest

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.