diff --git a/Guide/src/user_guide/openvmm/snapshots.md b/Guide/src/user_guide/openvmm/snapshots.md index 9c1e6359378..01a023b8336 100644 --- a/Guide/src/user_guide/openvmm/snapshots.md +++ b/Guide/src/user_guide/openvmm/snapshots.md @@ -340,6 +340,14 @@ fails (`E_PROFILE_HOST_UNKNOWN`). A host profile, `intel.host.v1` or `amd.host.v1`, serves only the host's CPU model and stepping, is not pinned, can change with the host's microcode, firmware, or hypervisor, and costs a fingerprint of the backend at every cold boot. +Every profile, pinned or host, fixes the CPU time bits that the time ABI +requires: the hypervisor, TSC, RDTSCP, ARAT, and invariant-TSC bits set, and +no TSC-deadline timer, `IA32_TSC_ADJUST`, PDCM, performance monitoring leaf +`0xa`, or rate leaves `0x15` and `0x16`; OpenVMM refuses a profile that +violates them (`E_PROFILE_DIGEST`). Before a partition uses a pinned profile, +OpenVMM verifies the profile's built-in data, once per process: a valid +profile, CPU time bits included, whose built-in encoding, which snapshots +embed, is its canonical encoding (`E_PROFILE_DIGEST`). Every boot checks that the backend supports the profile (`E_PROFILE_UNSUPPORTED`) and that VP 0 observes its effective CPUID (`E_CPU_SURFACE`); where `auto` fell back, such a failure @@ -349,8 +357,8 @@ generation, because a host profile derives from what the backend supports. A snapshot records the profile and the effective CPUID. Restore requires the same profile, pinned in this OpenVMM with the same digest, or, for a host profile, the snapshot's own copy of it, which -must match the recorded digest; a host of the profile's generation; and an -identical effective CPUID. +must be a valid profile, its CPU time bits included, with the recorded digest; +a host of the profile's generation; and an identical effective CPUID. OpenVMM computes the effective CPUID, including its own leaves and the bits it owns, such as the cache topology, so an OpenVMM build that computes it differently rejects earlier snapshots with `E_CPU_SURFACE`. diff --git a/vmm_core/cpu_profile/src/catalog.rs b/vmm_core/cpu_profile/src/catalog.rs index 1284600ca5a..0acbd055d20 100644 --- a/vmm_core/cpu_profile/src/catalog.rs +++ b/vmm_core/cpu_profile/src/catalog.rs @@ -37,40 +37,75 @@ pub const AUTO: &str = "auto"; pub const HOST: &str = "host"; /// Each pinned profile, built from its static data on first use, so that a -/// cold boot or a restore builds only its own. +/// cold boot or a restore builds only the profiles that it considers: the one +/// that it names, or those that cover its host. static PROFILES: [OnceLock; PINNED.len()] = [const { OnceLock::new() }; PINNED.len()]; -/// Returns the pinned profile at `index` in [`PINNED`], building it from its -/// constants on first use: no parsing, no validation, and no hashing, which -/// the tests do for every pinned profile instead. -fn load(index: usize) -> &'static CpuProfile { +/// Whether each pinned profile is valid, verified on its first use through +/// [`load`], so that a process verifies each profile that it uses once. +static VERIFIED: [OnceLock>; PINNED.len()] = + [const { OnceLock::new() }; PINNED.len()]; + +/// Returns the pinned profile at `index` in [`PINNED`], copied from its +/// constants on first use, without verifying it. Only [`select_auto`] reads +/// it, for the ID and generation by which it selects among the revisions +/// that cover the host, and it then [`load`]s the one that it selects; like +/// [`covers`], it takes them from the constants either way. +fn build(index: usize) -> &'static CpuProfile { PROFILES[index].get_or_init(|| PINNED[index].to_profile()) } +/// Returns the pinned profile at `index` in [`PINNED`], built from its +/// constants and verified on first use ([`PinnedProfile::verify`]): no JSON +/// parsing and no hashing, but an encoding, once per profile and process, so +/// that a stale or malformed `pinned_data.rs` fails with `E_PROFILE_DIGEST` +/// before any partition uses the profile. +/// +/// [`PinnedProfile::verify`]: crate::pinned::PinnedProfile::verify +fn load(index: usize) -> Result<&'static CpuProfile, ProfileError> { + let profile = build(index); + VERIFIED[index] + .get_or_init(|| PINNED[index].verify(profile)) + .clone()?; + Ok(profile) +} + /// Returns the index in [`PINNED`] of the profile `id`. fn position(id: &str) -> Option { PINNED.iter().position(|pinned| pinned.id == id) } /// Returns every pinned profile. Selecting by ID with [`pinned`] or -/// [`pinned_for_restore`] builds only that profile. +/// [`pinned_for_restore`] builds and verifies only that profile. /// -/// Offline only: this builds every profile, for tools and tests. A cold boot -/// or restore selects its one profile with [`select`] or -/// [`pinned_for_restore`]. +/// Offline only: this builds and verifies every profile, for tools and tests, +/// and panics if one is invalid. A cold boot or restore selects its one +/// profile with [`select`] or [`pinned_for_restore`]. pub fn pinned_profiles() -> &'static [CpuProfile] { static ALL: OnceLock> = OnceLock::new(); - ALL.get_or_init(|| (0..PINNED.len()).map(|index| load(index).clone()).collect()) + ALL.get_or_init(|| { + (0..PINNED.len()) + .map(|index| match load(index) { + Ok(profile) => profile.clone(), + Err(error) => panic!("{error}"), + }) + .collect() + }) } -/// Returns the pinned profile `id`. -pub fn pinned(id: &str) -> Option<&'static CpuProfile> { - position(id).map(load) +/// Returns the pinned profile `id`, verified. +/// +/// Fails with `E_PROFILE_UNKNOWN` when this OpenVMM does not pin `id`, and +/// with `E_PROFILE_DIGEST` when its pinned profile is invalid. +pub fn pinned(id: &str) -> Result<&'static CpuProfile, ProfileError> { + load(position(id).ok_or_else(|| unknown(id))?) } /// Returns the precomputed record of the pinned profile `id`: its canonical /// encoding and digest, constants that a snapshot's CPU profile record /// copies at capture and that the restore preflight compares byte for byte. +/// It does not verify the profile, which [`select`] and +/// [`pinned_for_restore`] do first. pub fn pinned_record(id: &str) -> Option> { position(id).map(|index| PINNED[index].record()) } @@ -98,17 +133,21 @@ pub struct PartitionProfile(Source); #[derive(Clone)] enum Source { - /// The pinned profile at this index in [`PINNED`]. - Pinned(usize), + /// The pinned profile at this index in [`PINNED`], which [`load`] + /// verified. + Pinned(usize, &'static CpuProfile), /// A host profile. Host(Arc), } impl PartitionProfile { - /// Returns the partition profile of the pinned profile `id`, if this - /// OpenVMM pins it. - pub fn pinned(id: &str) -> Option { - position(id).map(|index| Self(Source::Pinned(index))) + /// Returns the partition profile of the pinned profile `id`, verified. + /// + /// Fails with `E_PROFILE_UNKNOWN` when this OpenVMM does not pin `id`, and + /// with `E_PROFILE_DIGEST` when its pinned profile is invalid. + pub fn pinned(id: &str) -> Result { + let index = position(id).ok_or_else(|| unknown(id))?; + Ok(Self(Source::Pinned(index, load(index)?))) } /// Returns the partition profile of `profile`, a host profile, and @@ -142,7 +181,7 @@ impl PartitionProfile { /// encoding and digest, computed when it was selected. pub fn record(&self) -> PinnedRecord<'_> { match &self.0 { - Source::Pinned(index) => PINNED[*index].record(), + Source::Pinned(index, _) => PINNED[*index].record(), Source::Host(host) => PinnedRecord { id: host.profile.id(), encoding: &host.encoding, @@ -157,7 +196,7 @@ impl Deref for PartitionProfile { fn deref(&self) -> &CpuProfile { match &self.0 { - Source::Pinned(index) => load(*index), + Source::Pinned(_, profile) => profile, Source::Host(host) => &host.profile, } } @@ -245,17 +284,19 @@ pub fn pinned_profile_serves(host: &HostCpuSignature) -> bool { } /// Selects the profile of the host CPU's generation: its latest pinned -/// revision. Only the profiles whose generation covers the host are built. +/// revision. Only the profiles whose generation covers the host are built, +/// and only the selected one is verified ([`pinned`]). /// /// Fails with `E_PROFILE_HOST_UNKNOWN` when no pinned profile serves the /// host, or when profiles of more than one generation do; there is no host -/// CPUID passthrough. +/// CPUID passthrough. Fails with `E_PROFILE_DIGEST` when the selected +/// profile is invalid. pub fn select_auto(host: &HostCpuSignature) -> Result<&'static CpuProfile, ProfileError> { let candidates = (0..PINNED.len()) .filter(|&index| covers(PINNED[index].generation, host)) - .map(load) + .map(build) .collect::>(); - select_auto_in(&candidates, host, pinned_ids) + pinned(select_auto_in(&candidates, host, pinned_ids)?.id()) } /// Returns whether `generation` covers the host CPU, as @@ -316,20 +357,21 @@ fn select_auto_in<'a>( } /// Selects the pinned profile for `--cpu-profile spec`, [`AUTO`] or a pinned -/// ID, and checks that the host CPU is in its generation. A host profile has -/// no ID to select it by: the VM worker derives it for [`HOST`], and for -/// [`AUTO`] on a CPU that no pinned profile serves, or takes a restored -/// snapshot's ([`PartitionProfile::host`]). +/// ID, verifies it, and checks that the host CPU is in its generation. A host +/// profile has no ID to select it by: the VM worker derives it for [`HOST`], +/// and for [`AUTO`] on a CPU that no pinned profile serves, or takes a +/// restored snapshot's ([`PartitionProfile::host`]). /// /// Fails with `E_PROFILE_HOST_UNKNOWN`, `E_PROFILE_UNKNOWN` for an ID that is -/// not pinned, or `E_CPU_GENERATION`. +/// not pinned, `E_PROFILE_DIGEST` for a pinned profile that is invalid, or +/// `E_CPU_GENERATION`. pub fn select(spec: &str, host: &HostCpuSignature) -> Result { let id = if spec == AUTO { select_auto(host)?.id() } else { spec }; - let profile = PartitionProfile::pinned(id).ok_or_else(|| unknown(spec))?; + let profile = PartitionProfile::pinned(id)?; check_generation(&profile, host)?; Ok(profile) } @@ -351,16 +393,19 @@ pub fn check_generation(profile: &CpuProfile, host: &HostCpuSignature) -> Result } } -/// Returns the pinned profile that a snapshot recorded by ID and SHA-256. +/// Returns the pinned profile that a snapshot recorded by ID and SHA-256, +/// verified. /// /// Fails with `E_PROFILE_UNKNOWN` when this OpenVMM does not pin the ID, and -/// with `E_PROFILE_DIGEST` when its pinned profile has another digest. The -/// pinned profile's digest is a constant, which the catalog's tests check, so -/// a restore encodes and hashes nothing here. The restore preflight also -/// compares the recorded document with [`pinned_record`]'s encoding, and -/// checks the destination host with [`check_generation`]. +/// with `E_PROFILE_DIGEST` when its pinned profile is invalid or has another +/// digest. The pinned profile's digest is a constant, which the catalog's +/// tests check, so a restore hashes nothing here. The restore preflight also +/// compares the recorded document with [`pinned_record`]'s encoding, which +/// the profile's verification checks, and checks the destination host with +/// [`check_generation`]. pub fn pinned_for_restore(id: &str, sha256: &[u8]) -> Result<&'static CpuProfile, ProfileError> { let index = position(id).ok_or_else(|| unknown(id))?; + let profile = load(index)?; let golden = &PINNED[index].digest; if sha256 != golden { return Err(ProfileError::new( @@ -371,7 +416,7 @@ pub fn pinned_for_restore(id: &str, sha256: &[u8]) -> Result<&'static CpuProfile ), )); } - Ok(load(index)) + Ok(profile) } fn unknown(id: &str) -> ProfileError { @@ -402,7 +447,9 @@ fn revision(id: &str) -> u32 { #[cfg(test)] mod tests { use super::*; + use crate::CpuidLeafValue; use crate::derive; + use crate::pinned::PinnedProfile; use crate::test_support::fingerprint; use crate::test_support::profile; use test_with_tracing::test; @@ -480,10 +527,11 @@ mod tests { ), ]; - /// The checks that an OpenVMM start skips: every pinned file is a valid - /// profile in pretty canonical form with its golden digest, and the static - /// data generated from it holds the same profile, canonical encoding, - /// digest, and generation. + /// The checks that an OpenVMM start leaves to the tests: every pinned + /// file is a valid profile in pretty canonical form with its golden + /// digest, and the static data generated from it holds the same profile, + /// canonical encoding, digest, and generation. A start verifies only that + /// the static data is a valid profile in its own canonical encoding. #[test] fn the_static_data_is_the_pinned_files() { assert_eq!(PINNED.len(), FILES.len()); @@ -493,7 +541,9 @@ mod tests { assert_eq!(profile.digest_string(), golden, "{id}"); assert_eq!(pinned.id, id); assert_eq!(&pinned.to_profile(), &profile, "{id}"); - assert_eq!(load(index), &profile, "{id}"); + pinned.verify(&profile).unwrap(); + assert_eq!(load(index).unwrap(), &profile, "{id}"); + assert_eq!(VERIFIED[index].get(), Some(&Ok(())), "{id}"); assert_eq!(pinned.encoding.as_bytes(), profile.encode(), "{id}"); assert_eq!(canonical::format_digest(&pinned.digest), golden, "{id}"); assert_eq!( @@ -513,9 +563,13 @@ mod tests { ); assert!(std::ptr::eq( pinned_for_restore(id, &profile.digest()).unwrap(), - load(index) + load(index).unwrap() )); } + assert_eq!( + code(pinned("intel.icelake-sp.v2")), + ProfileErrorCode::ProfileUnknown + ); assert_eq!(pinned_record("intel.icelake-sp.v2"), None); assert_eq!( pinned_profiles() @@ -526,6 +580,89 @@ mod tests { ); } + /// A start refuses static data that is not a valid profile in its own + /// canonical encoding, as a stale or malformed `pinned_data.rs` would + /// hold, and names what is wrong. + #[test] + fn verification_rejects_stale_or_malformed_static_data() { + fn edited( + pinned: &PinnedProfile, + edit: impl FnOnce(&mut Vec), + ) -> &'static [CpuidLeafValue] { + let mut cpuid = pinned.cpuid.to_vec(); + edit(&mut cpuid); + cpuid.leak() + } + fn leaf(cpuid: &mut [CpuidLeafValue], key: (u32, Option)) -> &mut CpuidLeafValue { + cpuid.iter_mut().find(|entry| entry.key() == key).unwrap() + } + fn refusal(edited: PinnedProfile) -> String { + let error = edited.verify(&edited.to_profile()).unwrap_err(); + assert_eq!(error.code, ProfileErrorCode::ProfileDigest, "{error}"); + error.message + } + + let pinned = &PINNED[position("intel.icelake-sp.v1").unwrap()]; + pinned.verify(&pinned.to_profile()).unwrap(); + let invalid = "pinned CPU profile intel.icelake-sp.v1 is invalid: "; + + // Values that violate the CPU time bits: a TSC-deadline timer. + let cpuid = edited(pinned, |cpuid| leaf(cpuid, (1, None)).value[2].0 |= 1 << 24); + assert_eq!( + refusal(PinnedProfile { cpuid, ..*pinned }), + format!("{invalid}it violates the CPU time bits: CPUID 0x1 ECX bit 24 is set") + ); + // A missing leaf. + let cpuid = edited(pinned, |cpuid| cpuid.retain(|entry| entry.leaf.0 != 3)); + assert_eq!( + refusal(PinnedProfile { cpuid, ..*pinned }), + format!("{invalid}CPUID 0x3 lists subleaves [], expected [None]") + ); + // A VM-owned field that the profile pins. + let cpuid = edited(pinned, |cpuid| leaf(cpuid, (1, None)).mask[1].0 = !0); + let message = refusal(PinnedProfile { cpuid, ..*pinned }); + assert!( + message.starts_with(&format!("{invalid}CPUID 0x1 has mask")), + "{message}" + ); + // Another generation. + let generation = derive::known_generation("skylake-sp").unwrap(); + assert_eq!( + refusal(PinnedProfile { + generation, + ..*pinned + }), + format!("{invalid}ID \"intel.icelake-sp.v1\" is not intel.skylake-sp.v") + ); + + // Valid values that the encoding does not hold, and another profile's + // encoding. + let stale = "pinned CPU profile intel.icelake-sp.v1 differs from its constant \ + canonical encoding"; + assert_eq!( + refusal(PinnedProfile { + description: "Another description", + ..*pinned + }), + stale + ); + assert_eq!( + refusal(PinnedProfile { + encoding: PINNED[position("intel.skylake-sp.v1").unwrap()].encoding, + ..*pinned + }), + stale + ); + + // A digest that is not the encoding's: a start hashes nothing, and + // leaves the digest to the tests, which check it against the golden + // digest. + let mut digest = pinned.digest; + digest[0] ^= 1; + let stale_digest = PinnedProfile { digest, ..*pinned }; + stale_digest.verify(&stale_digest.to_profile()).unwrap(); + } + #[test] fn generations_are_known_and_disjoint() { for (i, profile) in pinned_profiles().iter().enumerate() { @@ -649,7 +786,7 @@ mod tests { #[test] fn the_static_generations_cover_what_the_profiles_do() { for (index, pinned) in PINNED.iter().enumerate() { - let profile = load(index); + let profile = load(index).unwrap(); for vendor in [*b"GenuineIntel", *b"AuthenticAMD"] { for (family, model) in [ (6u32, 85u32), @@ -839,7 +976,10 @@ mod tests { pinned.record(), pinned_record("intel.icelake-sp.v1").unwrap() ); - assert!(PartitionProfile::pinned("intel.host.v1").is_none()); + assert_eq!( + code(PartitionProfile::pinned("intel.host.v1")), + ProfileErrorCode::ProfileUnknown + ); let host = host_profile(); let selected = PartitionProfile::host(host.clone()).unwrap(); @@ -933,6 +1073,29 @@ mod tests { )), ProfileErrorCode::ProfileDigest ); + // A canonical document, with its own digest, of a profile that + // violates the CPU time bits: a TSC-deadline timer. + let mut deadline = host.clone(); + let leaf1 = deadline + .cpuid + .iter_mut() + .find(|entry| entry.key() == (1, None)) + .unwrap(); + leaf1.value[2].0 |= 1 << 24; + let encoding_with_deadline = deadline.encode(); + let error = host_profile_for_restore( + host.id(), + &canonical::sha256(&encoding_with_deadline), + &encoding_with_deadline, + ) + .unwrap_err(); + assert_eq!(error.code, ProfileErrorCode::ProfileDigest); + assert!( + error + .message + .contains("violates the CPU time bits: CPUID 0x1 ECX bit 24 is set"), + "{error}" + ); // A pinned profile's document under a host profile's ID. let pinned = pinned_record("intel.icelake-sp.v1").unwrap(); assert_eq!( diff --git a/vmm_core/cpu_profile/src/lib.rs b/vmm_core/cpu_profile/src/lib.rs index 99f2aad28aa..bf4f02d9158 100644 --- a/vmm_core/cpu_profile/src/lib.rs +++ b/vmm_core/cpu_profile/src/lib.rs @@ -29,23 +29,35 @@ //! a pass-through backend (MSHV, WHP) must present zero there //! ([`check_unlisted_cpuid`]), and KVM answers those entries from the //! effective CPUID itself ([`CpuidPresentation`]). -//! - **Restore:** [`pinned_for_restore`] checks a snapshot's recorded profile -//! digest against the pinned constant, core compares the recorded document -//! with [`pinned_record`]'s encoding, and [`check_generation`] checks the -//! destination host. Core compares the effective CPUID record with the -//! recomputed [`EffectiveCpuid`]. A snapshot of a host profile carries the -//! only copy of its document, which [`host_profile_for_restore`] decodes -//! and checks against the recorded digest, and from which the restoring -//! VM worker's [`PartitionProfile`] takes it. +//! - **Restore:** [`pinned_for_restore`] verifies the snapshot's pinned +//! profile and checks its recorded digest against the pinned constant, +//! core compares the recorded document with [`pinned_record`]'s encoding, +//! and [`check_generation`] checks the destination host. Core compares the +//! effective CPUID record with the recomputed [`EffectiveCpuid`]. A +//! snapshot of a host profile carries the only copy of its document, which +//! [`host_profile_for_restore`] decodes and checks against the recorded +//! digest, and from which the restoring VM worker's [`PartitionProfile`] +//! takes it. +//! - **Pinned profile verification:** the first use of a pinned profile in a +//! process, by [`select`], [`pinned_for_restore`], [`pinned`], or +//! [`PartitionProfile::pinned`], verifies its static data before handing it +//! out: the profile is valid, CPU time bits included, and its constant +//! encoding, which snapshots embed, is its canonical encoding +//! (`E_PROFILE_DIGEST`). A cold boot or restore therefore verifies the one +//! profile that it selects, once per process, for which it encodes the +//! profile; [`select_auto`] verifies only the revision that it selects. The +//! constant digest is the profile's identity, which the tests check against +//! the golden digests, so a pinned profile's start hashes nothing. //! - **Offline only:** the codecs and digests -//! ([`CpuProfile::encode`], [`CpuProfile::digest`], -//! [`CpuProfile::digest_string`], [`CpuProfile::decode`], -//! [`CpuProfile::from_pretty_json`], [`CpuProfile::to_pretty_json`], -//! [`EffectiveCpuid::encode`], [`EffectiveCpuid::digest`], -//! [`EffectiveCpuid::decode`], and [`EffectiveCpuid::decode_verified`]) -//! and [`pinned_profiles`] parse, encode, or hash. They serve tools, tests, -//! and `--cpu-fingerprint`, never a cold boot or restore of a pinned -//! profile, whose profile work uses only constants. Only a host profile, +//! ([`CpuProfile::digest`], [`CpuProfile::digest_string`], +//! [`CpuProfile::decode`], [`CpuProfile::from_pretty_json`], +//! [`CpuProfile::to_pretty_json`], [`EffectiveCpuid::encode`], +//! [`EffectiveCpuid::digest`], [`EffectiveCpuid::decode`], and +//! [`EffectiveCpuid::decode_verified`]) and [`pinned_profiles`] parse, +//! encode, or hash. They serve tools, tests, and `--cpu-fingerprint`, never +//! a cold boot or restore of a pinned profile, which parses no JSON, hashes +//! nothing, and compares its records with constants; only its verification +//! encodes it ([`CpuProfile::encode`]), once per process. A host profile, //! which has no constants, encodes and hashes at cold boot and decodes at //! restore. //! diff --git a/vmm_core/cpu_profile/src/pinned.rs b/vmm_core/cpu_profile/src/pinned.rs index 94965c55498..d65e4498674 100644 --- a/vmm_core/cpu_profile/src/pinned.rs +++ b/vmm_core/cpu_profile/src/pinned.rs @@ -6,15 +6,19 @@ //! `pinned_data.rs` holds every pinned profile as Rust constants: its values, //! its canonical encoding, and its golden digest. The `generate_pinned` //! example generates the file from `profiles/.json`. An OpenVMM start -//! therefore neither parses JSON nor hashes: the first use of a profile -//! copies its constants into a [`CpuProfile`], and snapshot records use the -//! precomputed encoding and digest. The tests check every constant against -//! the JSON files and the golden digests. +//! therefore neither parses JSON nor hashes: the first use of a profile in a +//! process copies its constants into a [`CpuProfile`] and verifies the profile +//! and its encoding ([`PinnedProfile::verify`]), as a partition's CPU profile +//! must be verified before the partition uses it, and snapshot records use the +//! precomputed encoding and digest. The tests check every constant against the +//! JSON files and the golden digests. use crate::Hex32; use crate::Hex64; use crate::cpuid::XsaveComponent; use crate::derive::KnownGeneration; +use crate::error::ProfileError; +use crate::error::ProfileErrorCode; use crate::profile::CpuProfile; use crate::profile::CpuidLeafValue; use crate::profile::PinnedMsr; @@ -101,6 +105,34 @@ impl PinnedProfile { digest: self.digest, } } + + /// Verifies `profile`, the profile that [`Self::to_profile`] copies from + /// these constants, as a cold boot or restore must verify its CPU profile + /// before a partition uses it: the profile is valid, with its density, + /// ownership masks, and CPU time bits, and `encoding`, which snapshots + /// embed, is its canonical encoding. + /// + /// A stale or malformed `pinned_data.rs` fails with `E_PROFILE_DIGEST`. + /// This encodes the profile but hashes nothing: `digest` is the profile's + /// identity, which the tests check against the golden digests, as they + /// check the static data against the reviewed JSON files. + pub(crate) fn verify(&self, profile: &CpuProfile) -> Result<(), ProfileError> { + let invalid = |message: String| { + ProfileError::new( + ProfileErrorCode::ProfileDigest, + format!("pinned CPU profile {} {message}", self.id), + ) + }; + profile + .validate() + .map_err(|message| invalid(format!("is invalid: {message}")))?; + if profile.encode() != self.encoding.as_bytes() { + return Err(invalid( + "differs from its constant canonical encoding".to_owned(), + )); + } + Ok(()) + } } /// The CPU profile record that a snapshot copies: the profile ID, the diff --git a/vmm_core/cpu_profile/src/profile.rs b/vmm_core/cpu_profile/src/profile.rs index b79b0dfb5fb..58d7dd16430 100644 --- a/vmm_core/cpu_profile/src/profile.rs +++ b/vmm_core/cpu_profile/src/profile.rs @@ -17,6 +17,10 @@ //! Pinned profiles are stored as pretty canonical JSON. Decoding accepts only //! the canonical bytes, so a profile has exactly one encoding and one digest. //! +//! A valid profile is dense (it lists every leaf and subleaf in range), masks +//! exactly the bits that it does not own, and fixes the time ABI's CPU time +//! bits ([`TIME_BITS`]); every way to build or decode a profile checks this. +//! //! Every serialized struct declares its fields in the byte order of their //! keys, so serializing it directly gives the canonical form (tests check //! this against the sorting encoder). Keep new fields in that order. @@ -33,6 +37,8 @@ use crate::error::ProfileErrorCode; use crate::fingerprint::IA32_ARCH_CAPABILITIES; use crate::signature::HostCpuSignature; use crate::signature::decode_signature; +use crate::surface::REGISTERS; +use crate::surface::bits_phrase; use crate::vendor::CpuVendor; use serde::Deserialize; use serde::Serialize; @@ -50,6 +56,39 @@ const MAX_SUBLEAF_IN_EAX_LEAVES: [u32; 6] = [0x7, 0x14, 0x17, 0x18, 0x1d, 0x24]; /// `CPUID.(7,0):EDX[29]`, the presence of `IA32_ARCH_CAPABILITIES`. const LEAF7_EDX_ARCH_CAPABILITIES: u32 = 1 << 29; +/// The CPU time bits of the NVX time ABI, which every profile fixes: for each +/// leaf and subleaf, the bits of `EAX`, `EBX`, `ECX`, and `EDX` that the time +/// ABI governs, and their values. A leaf that a profile does not list reads +/// as zero, so leaves `0x15` and `0x16` above the maximum basic leaf comply. +pub(crate) const TIME_BITS: [(u32, Option, [u32; 4], [u32; 4]); 8] = [ + // The hypervisor and TSC bits, and neither the TSC-deadline timer nor + // PDCM. + ( + 0x1, + None, + [0, 0, 1 << 31 | 1 << 24 | 1 << 15, 1 << 4], + [0, 0, 1 << 31, 1 << 4], + ), + // ARAT only. + (0x6, None, [!0; 4], [1 << 2, 0, 0, 0]), + // No `IA32_TSC_ADJUST`. + (0x7, Some(0), [0, 1 << 1, 0, 0], [0; 4]), + // No PMU. + (0xa, None, [!0; 4], [0; 4]), + // No TSC or processor frequency leaves. + (0x15, None, [!0; 4], [0; 4]), + (0x16, None, [!0; 4], [0; 4]), + // RDTSCP. + ( + EXTENDED_LEAF_BASE + 1, + None, + [0, 0, 0, 1 << 27], + [0, 0, 0, 1 << 27], + ), + // The invariant TSC only. + (EXTENDED_LEAF_BASE + 7, None, [!0; 4], [0, 0, 0, 1 << 8]), +]; + /// The CPU generation that a profile serves. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] @@ -275,8 +314,9 @@ impl CpuProfile { /// Returns the canonical encoding: compact canonical JSON. /// - /// Offline only, for tools and tests: a pinned profile's encoding is the - /// constant [`pinned_record`](crate::pinned_record) returns. + /// A pinned profile's encoding is the constant + /// [`pinned_record`](crate::pinned_record) returns, which the profile's + /// first use in a process compares with this, to verify the profile. pub fn encode(&self) -> Vec { canonical::to_compact_ordered(self) } @@ -401,7 +441,7 @@ impl CpuProfile { /// Checks every structural invariant of a profile; see the module /// documentation and [`SCHEMA`]. - fn validate(&self) -> Result<(), String> { + pub(crate) fn validate(&self) -> Result<(), String> { if self.schema != SCHEMA { return Err(format!("schema {:?} is not {SCHEMA:?}", self.schema)); } @@ -416,7 +456,7 @@ impl CpuProfile { self.physical_address_width )); } - Ok(()) + self.validate_time_bits() } /// Checks the profile's identity and returns its vendor. @@ -625,6 +665,37 @@ impl CpuProfile { } Ok(()) } + + /// Checks the CPU time bits ([`TIME_BITS`]), naming every bit that + /// violates them. + fn validate_time_bits(&self) -> Result<(), String> { + let mut violations = Vec::new(); + for (leaf, subleaf, governed, expected) in TIME_BITS { + let value = self.lookup(leaf, subleaf.unwrap_or(0)); + for register in 0..4 { + let set = value[register] & governed[register] & !expected[register]; + let clear = !value[register] & governed[register] & expected[register]; + for (bits, state) in [(set, "set"), (clear, "clear")] { + if bits != 0 { + violations.push(format!( + "{} {} {} {state}", + describe_leaf(leaf, subleaf), + REGISTERS[register], + bits_phrase(bits.into()) + )); + } + } + } + } + if violations.is_empty() { + Ok(()) + } else { + Err(format!( + "it violates the CPU time bits: {}", + violations.join("; ") + )) + } + } } /// Returns whether `component` is a valid vendor or generation component of a @@ -765,6 +836,21 @@ mod tests { .unwrap() } + /// Replaces `register` of the CPUID entry of `leaf` and `subleaf` in a + /// profile's JSON with what `edit` makes of it. + fn edit_register( + value: &mut Value, + leaf: u32, + subleaf: Option, + register: usize, + edit: impl FnOnce(u32) -> u32, + ) { + let field = &mut entry(value, leaf, subleaf)["value"][register]; + let old = + u32::from_str_radix(field.as_str().unwrap().trim_start_matches("0x"), 16).unwrap(); + *field = json!(format!("{:#010x}", edit(old))); + } + fn assert_invalid(result: Result, expected: &str) { let error = result.unwrap_err(); assert_eq!(error.code, ProfileErrorCode::ProfileDigest, "{error}"); @@ -956,6 +1042,69 @@ mod tests { ); } + #[test] + fn validation_rejects_profiles_that_violate_the_cpu_time_bits() { + // Every pinned profile fixes them, those whose maximum basic leaf is + // below 0x15 included. + for profile in crate::pinned_profiles() { + profile.validate_time_bits().unwrap(); + } + for (leaf, subleaf, register, set, clear, expected) in [ + (0x1, None, 2, 0, 1 << 31, "CPUID 0x1 ECX bit 31 is clear"), + (0x1, None, 2, 1 << 24, 0, "CPUID 0x1 ECX bit 24 is set"), + (0x1, None, 2, 1 << 15, 0, "CPUID 0x1 ECX bit 15 is set"), + (0x1, None, 3, 0, 1 << 4, "CPUID 0x1 EDX bit 4 is clear"), + ( + 0x6, + None, + 0, + 0x73, + 1 << 2, + "CPUID 0x6 EAX bits 0, 1, 4, 5, 6 are set; CPUID 0x6 EAX bit 2 is clear", + ), + (0x6, None, 2, 1 << 3, 0, "CPUID 0x6 ECX bit 3 is set"), + (0x7, Some(0), 1, 1 << 1, 0, "CPUID 0x7.0 EBX bit 1 is set"), + (0xa, None, 0, 1, 0, "CPUID 0xa EAX bit 0 is set"), + (0x15, None, 2, 1 << 5, 0, "CPUID 0x15 ECX bit 5 is set"), + (0x16, None, 0, 1 << 11, 0, "CPUID 0x16 EAX bit 11 is set"), + ( + 0x8000_0001, + None, + 3, + 0, + 1 << 27, + "CPUID 0x80000001 EDX bit 27 is clear", + ), + ( + 0x8000_0007, + None, + 3, + 1 << 9, + 1 << 8, + "CPUID 0x80000007 EDX bit 9 is set; CPUID 0x80000007 EDX bit 8 is clear", + ), + ] { + assert_invalid( + decode_edited(|value| { + edit_register(value, leaf, subleaf, register, |old| (old | set) & !clear) + }), + &format!( + "profile \"{ICELAKE}\" is invalid: it violates the CPU time bits: {expected}" + ), + ); + } + + // Every violation is named. + assert_invalid( + decode_edited(|value| { + edit_register(value, 1, None, 2, |ecx| ecx | 1 << 24); + edit_register(value, 0x8000_0001, None, 3, |edx| edx & !(1 << 27)); + }), + "it violates the CPU time bits: CPUID 0x1 ECX bit 24 is set; \ + CPUID 0x80000001 EDX bit 27 is clear", + ); + } + #[test] fn masks_leave_the_vm_and_runtime_fields_to_openvmm() { for vendor in CpuVendor::ALL { diff --git a/vmm_core/cpu_profile/src/surface.rs b/vmm_core/cpu_profile/src/surface.rs index 246a6b27ba8..2f08358d20a 100644 --- a/vmm_core/cpu_profile/src/surface.rs +++ b/vmm_core/cpu_profile/src/surface.rs @@ -17,7 +17,7 @@ use crate::unlisted::unlisted_cpuid_error; use crate::unlisted::unlisted_cpuid_violations; /// The register names, for messages. -const REGISTERS: [&str; 4] = ["EAX", "EBX", "ECX", "EDX"]; +pub(crate) const REGISTERS: [&str; 4] = ["EAX", "EBX", "ECX", "EDX"]; /// The CPU surface a backend supports on this host, which it reports through /// `supported_cpu_surface()` and which the CPU fingerprint records. @@ -375,7 +375,7 @@ pub(crate) fn field_value(value: u32, shift: u32, width: u32) -> u32 { } /// Returns `bit 16 is` or `bits 3, 7 are`. -fn bits_phrase(bits: u64) -> String { +pub(crate) fn bits_phrase(bits: u64) -> String { let list = (0..64) .filter(|bit| bits & (1 << bit) != 0) .map(|bit| bit.to_string()) diff --git a/vmm_core/cpu_profile/src/test_support.rs b/vmm_core/cpu_profile/src/test_support.rs index b6d086a9c13..be9b3ce332e 100644 --- a/vmm_core/cpu_profile/src/test_support.rs +++ b/vmm_core/cpu_profile/src/test_support.rs @@ -19,7 +19,7 @@ use crate::signature::decode_signature; /// Returns the pinned profile `id`. pub(crate) fn profile(id: &str) -> &'static CpuProfile { - crate::pinned(id).unwrap_or_else(|| panic!("profile {id} is not pinned")) + crate::pinned(id).unwrap_or_else(|error| panic!("{error}")) } /// Returns a profile's CPUID values as fingerprint entries. diff --git a/vmm_core/virt_mshv/src/x86_64/profile_features.rs b/vmm_core/virt_mshv/src/x86_64/profile_features.rs index d934c2db65a..63522b61da0 100644 --- a/vmm_core/virt_mshv/src/x86_64/profile_features.rs +++ b/vmm_core/virt_mshv/src/x86_64/profile_features.rs @@ -201,7 +201,7 @@ mod tests { ]; fn profile(id: &str) -> &'static CpuProfile { - cpu_profile::pinned(id).unwrap_or_else(|| panic!("profile {id} is not pinned")) + cpu_profile::pinned(id).unwrap_or_else(|error| panic!("{error}")) } fn has(features: &HvFeatures, word: HvFeatureWord, mask: u64) -> bool {