Skip to content
Audit finding Intermediate

Constanct value is used to check price.confidence

The contract's price validation uses a single hard-coded confidence threshold for every Pyth price feed, but different feeds have very different confidence widths. A feed with a wide confidence interval can fail the check and cause transactions to revert, while a feed with a narrow interval may pass even when the price is unreliable.

jasonxiale Follow

Originally disclosed on Immunefi

Critical View original
  • Disclosed after under an hour

#35767 [SC-Critical] constanct value is used to check `price.confidence`

Submitted on Oct 7th 2024 at 09:07:06 UTC by @jasonxiale for IOP | Swaylend

Description

Brief/Intro

In [Market.get_price_internal] (https://github.com/Swaylend/swaylend-monorepo/blob/34ada63c18efd163ef80694c404d0573d49d46b4/contracts/market/src/main.sw#L1017-L1050), while validating the Pyth's price, the function checks the `price.confidence` in main.sw#L1045-L1048.

The issue is that `get_price_internal` will be used to get price for different `price_fee_id`, but the returned price will be checked against the same constant value

Vulnerability Details

As shown in main.sw#L1048-L1051, Market.get_price_internal uses constant values to check `price.confidence`

```Rust 1017 #[storage(read)] 1018 fn get_price_internal(price_feed_id: PriceFeedId) -> Price { 1019 let contract_id = storage.pyth_contract_id.read(); 1020 require( 1021 contract_id != ContractId::zero(), 1022 Error::OracleContractIdNotSet, 1023 ); 1024 ... 1045 require( 1046 u256::from(price.confidence) <= (u256::from(price.price) * ORACLE_MAX_CONF_WIDTH / ORACLE_CONF_BASIS_POINTS), 1047 Error::OraclePriceValidationError, 1048 ); <<<--- constant value is checked here 1049 1050 price 1051 } ```

Impact Details

`Market.get_price_internal` is important, because it is used while borrow/liquidate assets:

  1. if the constant value is too wide for some price-feeds, the user/protocol might execute the tx at a bad price
  2. if the constant value is too restrict for some price-feeds, the tx might always revert

References

Add any relevant links to documentation or code

Proof of Concept

Proof of Concept

According to the technical walkthrough at 185s, the derivate BTC might be used,

And from Pyth's network, we will take `BTC/USD` and `TBTC/USD` pair as an example

At the moment: >BTC/USD: price $63620, confidence: +- $16

>CBBTC/USD: price $63610, confidence: +- $509

To prove the point, we'll use the following code to simulate the check in main.sw#L1045-L1048

Please put the following code into a new file named `check.rs` and run: ```bash root@4d98affa1474:/in/temp# rustc check.rs ; ./check BTC check: true CBBTC check: false ```

As we can see, BTC/USD can pass the check, and CBBTC/USD can't pass the check

```Rust pub const ORACLE_MAX_CONF_WIDTH: u64 = 20; pub const ORACLE_CONF_BASIS_POINTS: u64 = 10_000; fn main() { let btc_price : u64 = 63620; let btc_confidence: u64 = 16;

let cbbtc_price     : u64 &#x3D; 63610;
let cbbtc_confidence: u64 &#x3D; 509;

println!(&quot;BTC check: {}&quot;, btc_confidence &lt;&#x3D; btc_price * ORACLE_MAX_CONF_WIDTH / ORACLE_CONF_BASIS_POINTS);
println!(&quot;CBBTC check: {}&quot;, cbbtc_confidence &lt;&#x3D; cbbtc_price * ORACLE_MAX_CONF_WIDTH / ORACLE_CONF_BASIS_POINTS);

} ```

The original report as published on Immunefi, by jasonxiale. 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 contract applies one constant maximum confidence width to all price feeds instead of a per-feed threshold.

Impact

Borrowing or liquidating with a feed whose confidence is wider than the constant could always revert, while a feed with a narrower confidence could let a transaction execute at a bad price, potentially leading to protocol insolvency.

What the attacker needed

A price feed with a different confidence width; borrow or liquidate using that feed

The fix

The check should use a per-feed confidence threshold rather than one shared constant.

How it was found

  1. Pick two Pyth feeds, BTC/USD and CBBTC/USD, with different confidence widths.
  2. Run the same constant-based check against both feeds' price and confidence values.
  3. Observe that BTC/USD passes the check while CBBTC/USD fails.

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 reports both a price and a confidence range showing how sure it is. The contract compares that range to a fixed percentage for every asset, but some assets naturally have much wider ranges than others. So the check can wrongly reject a valid price or accept a shaky one.

More by jasonxiale

View all

25 more disclosed reports, best first.

More from Swaylend

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.