From b795d9f09b96097f6162da285c2746d93892ee86 Mon Sep 17 00:00:00 2001 From: japabu Date: Thu, 12 Nov 2026 05:55:46 +0200 Subject: [PATCH 1/2] Each CPU's bring-up writes SPEC_CTRL and clears the MCU_OPT_CTRL opt-outs its decision names kernel::mitigation::decide named enhanced IBRS, AMD's always-on IBRS, BHI_DIS_S and the microcode's RDRAND and gather mitigations, and nothing set them: every CPU ran under the IA32_SPEC_CTRL and IA32_MCU_OPT_CTRL firmware left. - Decision::spec_ctrl is the CPU's IA32_SPEC_CTRL while no program that asked runs: IBRS where enhanced IBRS or AMD's IBRS-always-on is named, BHI_DIS_S where it is, every other bit clear. arch::mitigation::apply writes it whole wherever Facts::has_spec_ctrl says the MSR exists (Intel "CPUID Enumeration and Architectural MSRs" Table 3; APM Vol. 3 Fn8000_0008_EBX). - Decision::mcu_opt_ctrl_clears names RNGDS_MITG_DIS and GDS_MITG_DIS where their mitigation is named; apply clears them and keeps the rest. - Enhanced IBRS is enumerated only with CPUID.(07H,0):EDX[26], whose bit it sets: a CPU claiming IBRS_ALL without it says so rather than taking #GP. - The words line gives each register as found and as left, the lines take what is in force from what was read back, and Decision::unapplied names whatever the kernel sets that a register shows off. The metal judge refuses a CPU with any, as it refused only LFENCE's before. - The gds-found-disabled arm sets GDS_MITG_DIS before the kernel's write; gds_found_disabled, a T14 row on the shared-debug boot, holds that every CPU read it set and left it clear. - mitigations_in_a_guest holds a KVM guest's lines and read-backs as the metal row holds a bench's, and prints its words: what the guest runner's CPU shows a guest. IBRS on entry, which RSBA parts without enhanced IBRS are named, and AMD's automatic IBRS (EFER) are recorded, not built. Co-Authored-By: Claude Opus 5.5 --- ...n-mitigations-from-its-vendors-guidance.md | 4 +- ...brs-stays-off-on-an-amd-cpu-that-has-it.md | 22 +++ ...on-a-cpu-with-rsba-and-no-enhanced-ibrs.md | 26 +++ ...ctrl-and-gds-stay-as-firmware-left-them.md | 40 +++- ...s-linux-on-every-machine-toyos-supports.md | 2 + kernel/pure/mitigation.rs | 152 +++++++++++++++- kernel/src/actuator.rs | 5 + kernel/src/arch/x86_64/mitigation.rs | 75 +++++--- tests/checks.rs | 112 ++++++++---- tests/metal/src/lib.rs | 4 + tests/toyos.rs | 171 ++++++++++++++---- 11 files changed, 507 insertions(+), 106 deletions(-) create mode 100644 issues/automatic-ibrs-stays-off-on-an-amd-cpu-that-has-it.md create mode 100644 issues/no-entry-sets-ibrs-on-a-cpu-with-rsba-and-no-enhanced-ibrs.md diff --git a/issues/a-pure-function-decides-a-cpus-speculation-mitigations-from-its-vendors-guidance.md b/issues/a-pure-function-decides-a-cpus-speculation-mitigations-from-its-vendors-guidance.md index e942513e7a4..42e11a22837 100644 --- a/issues/a-pure-function-decides-a-cpus-speculation-mitigations-from-its-vendors-guidance.md +++ b/issues/a-pure-function-decides-a-cpus-speculation-mitigations-from-its-vendors-guidance.md @@ -26,8 +26,8 @@ names it, after its microcode loads and before the clocksource's `RDTSCP; LFENCE` relies on it, and stops the machine if the bit does not hold. Both benches' words, as each one's boot logged them, are pinned in the harness, and `checks::the_benches_words_decide_as_their_vendors_rows` holds -what they decide to each vendor's row for that CPU. No other mitigation the decision -names is applied: each is its own record's. +what they decide to each vendor's row for that CPU. Applying every other +mitigation the decision names is its own record's. **Left**: - Under the policy, an AMD CPU affected by Branch Type Confusion leaves the diff --git a/issues/automatic-ibrs-stays-off-on-an-amd-cpu-that-has-it.md b/issues/automatic-ibrs-stays-off-on-an-amd-cpu-that-has-it.md new file mode 100644 index 00000000000..91c0093447b --- /dev/null +++ b/issues/automatic-ibrs-stays-off-on-an-amd-cpu-that-has-it.md @@ -0,0 +1,22 @@ +--- +status: open +kind: defect +severity: [security] +opened: 2026-11-12 +--- + +# Automatic IBRS stays off on an AMD CPU that has it + +On an AMD CPU that enumerates automatic IBRS (APM Vol. 3 +`CPUID Fn8000_0021_EAX[AutomaticIBRS]`, bit 8), `kernel::mitigation::decide` +names it for spectre_v2 and nothing sets `EFER.AIBRSE`: +`arch::x86_64::control_regs` declares `IA32_EFER` whole and constant. Such a +CPU runs the kernel's indirect branches unrestricted, and its spectre_v2 +line says so. No proving machine enumerates it. + +**Exit**: `control_regs`' declaration sets `AIBRSE` on each CPU whose +decision names automatic IBRS and asserts it there as it does the rest of +`IA32_EFER`; the words line gives `IA32_EFER`, and a boot on a machine that +enumerates it shows the bit on every CPU and its spectre_v2 line with +automatic IBRS in force. **Mutation**: the bit dropped from the declaration. +**Oracle**: the CPU's own register. diff --git a/issues/no-entry-sets-ibrs-on-a-cpu-with-rsba-and-no-enhanced-ibrs.md b/issues/no-entry-sets-ibrs-on-a-cpu-with-rsba-and-no-enhanced-ibrs.md new file mode 100644 index 00000000000..df6eaadad3f --- /dev/null +++ b/issues/no-entry-sets-ibrs-on-a-cpu-with-rsba-and-no-enhanced-ibrs.md @@ -0,0 +1,26 @@ +--- +status: open +kind: defect +severity: [security] +opened: 2026-11-12 +--- + +# No entry sets IBRS on a CPU with RSBA and no enhanced IBRS + +On an Intel CPU with RSBA behavior and no enhanced IBRS, +`kernel::mitigation::decide` names "IBRS on entry" for spectre_v2 and +retbleed: Intel's "Return Stack Buffer Underflow" has IBRS, not retpoline, +hold a return that underflows the RSB there. Nothing sets it, so every entry +from Ring 3 runs with `IA32_SPEC_CTRL.IBRS` as the CPU's bring-up left it, +clear. The guest runner's CPU is one such: it enumerates IBRS +(CPUID.(07H,0):EDX[26]) and no `IBRS_ALL`, and its signature is in +"Retpoline"'s Table 5. No proving machine is. + +**Exit**: each entry from Ring 3 sets `IBRS` in `IA32_SPEC_CTRL` before its +first indirect branch, and each return to Ring 3 writes back the value the +thread runs with; a CPU whose decision does not name it runs no added +instruction, chosen per CPU as the branch-history clear is; and idle holds +what Intel's "Speculative Execution Side Channel Mitigations" asks of `IBRS` +before a sleep state on such a CPU. On the guest runner a probe reads `IBRS` +set inside a syscall and the thread's own value after its return. +**Mutation**: the return's write deleted. **Oracle**: the CPU's own register. diff --git a/issues/spec-ctrl-and-gds-stay-as-firmware-left-them.md b/issues/spec-ctrl-and-gds-stay-as-firmware-left-them.md index f508b4deae9..ae9a984dd29 100644 --- a/issues/spec-ctrl-and-gds-stay-as-firmware-left-them.md +++ b/issues/spec-ctrl-and-gds-stay-as-firmware-left-them.md @@ -7,15 +7,39 @@ opened: 2026-09-29 # SPEC_CTRL and GDS stay as firmware left them -Nothing in `kernel/src` reads or writes `IA32_SPEC_CTRL` (0x48) or -`IA32_MCU_OPT_CTRL` (0x123), so every CPU runs under the speculation controls -firmware left, and an Intel part affected by Gather Data Sampling runs -unmitigated if firmware left `GDS_MITG_DIS` set. Linux at -`Ubuntu-6.8.0-142.142` writes both as its decision selects -(`arch/x86/kernel/cpu/bugs.c:89,777`). - -**Exit**: on every proving machine each CPU's 0x48 holds what +**Built**: each CPU's bring-up writes `IA32_SPEC_CTRL` (0x48) whole with +what `issues/a-pure-function-decides-a-cpus-speculation-mitigations-from-its-vendors-guidance.md` +names for it while no program that asked runs: `IBRS` for enhanced IBRS or +AMD's always-on IBRS, `BHI_DIS_S` where it is named, every other bit clear. +It clears each `IA32_MCU_OPT_CTRL` (0x123) opt-out, `RNGDS_MITG_DIS` and +`GDS_MITG_DIS`, whose microcode mitigation the decision names +(`kernel::mitigation::Decision::spec_ctrl`, `mcu_opt_ctrl_clears`). Each +boot's words line gives both registers as each CPU found them and as it left +them, and its lines take what is in force from what it read back. +`checks::the_benches_words_decide_as_their_vendors_rows` holds the T14's +lines with `IBRS` set, and the `mitigations` row refuses a boot whose +registers leave off anything the kernel sets +(`Decision::unapplied`). The `gds-found-disabled` arm sets `GDS_MITG_DIS` +before the kernel's write, as firmware that opted out would leave it, and the +`gds_found_disabled` row holds that every CPU read it set and left it clear. + +**Left**: +- **Owed on metal:** the `mitigations` row, the `metalcase` boot of main. + - Benches: the T14 and the AMD bench. + - Main must show the row green: on the T14 every CPU's words line with + `spec_ctrl=0x1` and `mcu_opt_ctrl` with bits 0 and 4 clear, and on the + AMD bench `spec_ctrl=0x0`. +- **Owed on metal:** `gds_found_disabled`, the `shared-debug` boot of main. + - Bench: the T14. + - Main must show the row green: every CPU's words line with + `mcu_opt_ctrl_found` holding bit 4 and `mcu_opt_ctrl` without it. The + same boot of main with the clear deleted from `arch::mitigation::apply` + must red it, the mutation below. +- **Owed on metal:** Linux's reads of 0x48 and 0x123 on each bench, beside + ToyOS's, compared in evidence. + +**Exit**: on every proving machine each CPU's 0x48 holds what the decision selects for it; on the T14, the one proving machine with GDS, a probe sets `GDS_MITG_DIS` and reads it clear after the kernel's write. **Mutation**: the clear deleted. **Oracle**: the vendor's guidance through that function, and diff --git a/issues/the-kernel-is-at-least-as-secure-as-linux-on-every-machine-toyos-supports.md b/issues/the-kernel-is-at-least-as-secure-as-linux-on-every-machine-toyos-supports.md index bfe92cfe0a3..f2a685b2e84 100644 --- a/issues/the-kernel-is-at-least-as-secure-as-linux-on-every-machine-toyos-supports.md +++ b/issues/the-kernel-is-at-least-as-secure-as-linux-on-every-machine-toyos-supports.md @@ -21,6 +21,8 @@ work needs. - `issues/a-pure-function-decides-a-cpus-speculation-mitigations-from-its-vendors-guidance.md` - `issues/spec-ctrl-and-gds-stay-as-firmware-left-them.md` +- `issues/automatic-ibrs-stays-off-on-an-amd-cpu-that-has-it.md` +- `issues/no-entry-sets-ibrs-on-a-cpu-with-rsba-and-no-enhanced-ibrs.md` - `issues/no-program-runs-with-speculative-store-bypass-disabled.md` - `issues/no-gate-decodes-kernel-elfs-instructions.md` - `issues/no-entry-or-switch-clears-the-bhb-fills-the-rsb-or-issues-an-ibpb.md` diff --git a/kernel/pure/mitigation.rs b/kernel/pure/mitigation.rs index 26ffc4d7ba5..f9ef80d09ff 100644 --- a/kernel/pure/mitigation.rs +++ b/kernel/pure/mitigation.rs @@ -65,7 +65,10 @@ const IBRS_IBPB: u32 = 1 << 26; const STIBP: u32 = 1 << 27; const L1D_FLUSH: u32 = 1 << 28; const SSBD: u32 = 1 << 31; +const PSFD: u32 = 1 << 0; +const IPRED_CTRL: u32 = 1 << 1; const RRSBA_CTRL: u32 = 1 << 2; +const DDPD_U: u32 = 1 << 3; const BHI_CTRL: u32 = 1 << 4; /// `IA32_ARCH_CAPABILITIES`, the same article's Table 2. @@ -102,6 +105,7 @@ mod amd { pub const SSBD: u32 = 1 << 24; pub const SSBD_VIRT_SPEC_CTRL: u32 = 1 << 25; pub const SSBD_NOT_REQUIRED: u32 = 1 << 26; + pub const PSFD: u32 = 1 << 28; pub const BTC_NO: u32 = 1 << 29; pub const LFENCE_ALWAYS_SERIALIZING: u32 = 1 << 2; pub const VERW_CLEAR: u32 = 1 << 5; @@ -292,6 +296,13 @@ impl Mitigations { } } +/// Each one's name, as a line gives it. +impl fmt::Display for Mitigations { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + Named { set: *self, by_policy: Self::NONE }.fmt(f) + } +} + /// The vulnerabilities decided, each named as its line names it. #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[repr(u8)] @@ -412,11 +423,66 @@ impl Decision { Line { verdict: self.verdict(of), applied } } + /// `IA32_SPEC_CTRL` (48H) on this CPU while no program that asked runs: + /// `IBRS` set once where enhanced IBRS or AMD's IBRS-always-on is named, + /// and `BHI_DIS_S` where it is. Every other bit is clear. + pub fn spec_ctrl(&self) -> u64 { + let mut value = 0; + if self.enumerates(Mitigation::EnhancedIbrs) || self.enumerates(Mitigation::IbrsAlwaysOn) { + value |= msr::IBRS; + } + if self.enumerates(Mitigation::BhiDisS) { + value |= msr::BHI_DIS_S; + } + value + } + + /// The `IA32_MCU_OPT_CTRL` (123H) opt-outs the kernel clears: each where + /// the microcode mitigation it disables is named. + pub fn mcu_opt_ctrl_clears(&self) -> u64 { + let mut clears = 0; + if self.enumerates(Mitigation::SrbdsMicrocode) { + clears |= msr::RNGDS_MITG_DIS; + } + if self.enumerates(Mitigation::GdsMicrocode) { + clears |= msr::GDS_MITG_DIS; + } + clears + } + + /// What the kernel sets at a CPU's bring-up, this decision names and the + /// CPU enumerates, and `registers` do not show in force. + pub fn unapplied(&self, registers: &Registers) -> Mitigations { + let set = Mitigations::of(&[ + Mitigation::LfenceDispatchSerializing, + Mitigation::EnhancedIbrs, + Mitigation::IbrsAlwaysOn, + Mitigation::BhiDisS, + Mitigation::SrbdsMicrocode, + Mitigation::GdsMicrocode, + ]); + let named = Mitigation::ALL.into_iter().filter(|&one| set.has(one) && self.enumerates(one)); + let in_force = self.in_force(registers); + named.filter(|&one| !in_force.has(one)).fold(Mitigations::NONE, Mitigations::with) + } + /// What is in force on a CPU whose registers read `registers`: [`ALWAYS`] - /// and each control a register shows on. Intel's two bits opt out of a - /// mitigation its microcode turns on; AMD's two turn one on. + /// and each control a register shows on. Intel's two `IA32_MCU_OPT_CTRL` + /// bits opt out of a mitigation its microcode turns on; the others turn + /// one on. pub fn in_force(&self, registers: &Registers) -> Mitigations { let mut on = ALWAYS; + if let Some(spec_ctrl) = registers.spec_ctrl { + for (one, bit) in [ + (Mitigation::EnhancedIbrs, msr::IBRS), + (Mitigation::IbrsAlwaysOn, msr::IBRS), + (Mitigation::BhiDisS, msr::BHI_DIS_S), + ] { + if self.enumerates(one) && spec_ctrl & bit != 0 { + on = on.with(one); + } + } + } if let Some(opt) = registers.mcu_opt_ctrl { if self.enumerates(Mitigation::SrbdsMicrocode) && opt & msr::RNGDS_MITG_DIS == 0 { on = on.with(Mitigation::SrbdsMicrocode); @@ -444,6 +510,8 @@ pub const ALWAYS: Mitigations = Mitigations::of(&[Mitigation::UserPointerClamp, /// and `None` elsewhere. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Registers { + /// `IA32_SPEC_CTRL` (48H), wherever the CPU has it ([`Facts::has_spec_ctrl`]). + pub spec_ctrl: Option, /// `IA32_MCU_OPT_CTRL` (123H). pub mcu_opt_ctrl: Option, /// AMD's `DE_CFG2` (C001_10E3H). @@ -455,11 +523,14 @@ pub struct Registers { /// MSR C001_1029[1]: LFENCE is dispatch serializing (AMD 70586 G-2). pub const C001_1029_LFENCE_SERIALIZES_DISPATCH: u64 = 1 << 1; -/// The bits [`Decision::in_force`] reads: `IA32_MCU_OPT_CTRL`'s two -/// opt-outs ("CPUID Enumeration and Architectural MSRs", Table 6) and -/// `DE_CFG2[SuppressBPOnNonBr]` ("Technical Guidance for Mitigating Branch -/// Type Confusion" §6.2.1). +/// The register bits the decision sets and reads: `IA32_SPEC_CTRL`'s +/// ("CPUID Enumeration and Architectural MSRs", Table 3, which AMD's +/// `SPEC_CTRL` shares: APM Vol. 3 Fn8000_0008_EBX), `IA32_MCU_OPT_CTRL`'s two +/// opt-outs (the same, Table 6) and `DE_CFG2[SuppressBPOnNonBr]` ("Technical +/// Guidance for Mitigating Branch Type Confusion" §6.2.1). mod msr { + pub const IBRS: u64 = 1 << 0; + pub const BHI_DIS_S: u64 = 1 << 10; pub const RNGDS_MITG_DIS: u64 = 1 << 0; pub const GDS_MITG_DIS: u64 = 1 << 4; pub const SUPPRESS_BP_ON_NON_BR: u64 = 1 << 1; @@ -609,6 +680,22 @@ pub fn decide(facts: &Facts) -> Decision { } impl Facts { + /// Whether the CPU has `IA32_SPEC_CTRL` (48H), which an absent MSR's + /// `#GP` makes a question to ask first: on Intel wherever it enumerates + /// any of its bits ("CPUID Enumeration and Architectural MSRs", Table 3), + /// on AMD wherever it enumerates IBRS, STIBP, SSBD or PSFD (APM Vol. 3 + /// Fn8000_0008_EBX). + pub fn has_spec_ctrl(&self) -> bool { + match self.vendor { + Vendor::Intel => { + self.cpuid_7_0_edx & (IBRS_IBPB | STIBP | SSBD) != 0 + || self.cpuid_7_2_edx & (PSFD | IPRED_CTRL | RRSBA_CTRL | DDPD_U | BHI_CTRL) != 0 + } + Vendor::Amd => self.cpuid_8000_0008_ebx & (amd::IBRS | amd::STIBP | amd::SSBD | amd::PSFD) != 0, + Vendor::Other(_) => false, + } + } + fn cap(&self, bit: u64) -> bool { self.arch_capabilities & bit != 0 } @@ -663,8 +750,10 @@ fn intel(f: &Facts, of: Vulnerability) -> Verdict { // no need to use STIBP when IBRS is set" (§2.4.1.3). Vulnerability::SpectreV2 => { let enhanced = f.cap(cap::IBRS_ALL); + // The bit it sets is `IBRS`'s, which CPUID.(07H,0):EDX[26] + // enumerates ("CPUID Enumeration and Architectural MSRs", Table 3). let branches = if enhanced { - Asks::of(Mitigation::EnhancedIbrs, true) + Asks::of(Mitigation::EnhancedIbrs, ibrs) } else if f.intel_rsba() { Asks::of(Mitigation::IbrsOnEntry, ibrs) } else { @@ -701,7 +790,7 @@ fn intel(f: &Facts, of: Vulnerability) -> Verdict { Verdict::NotAffected } else if f.cap(cap::IBRS_ALL) { if f.cap(cap::RRSBA) || f.intel_lists(RRSBA) == Some(true) { - Asks::of(Mitigation::EnhancedIbrs, true).affected() + Asks::of(Mitigation::EnhancedIbrs, ibrs).affected() } else { Verdict::NotAffected } @@ -1547,9 +1636,12 @@ mod tests { /// register the CPU was not read for shows nothing. #[test] fn what_is_in_force_is_what_the_registers_show() { - let unread = Registers { mcu_opt_ctrl: None, de_cfg2: None, c001_1029: None }; + let unread = Registers { spec_ctrl: None, mcu_opt_ctrl: None, de_cfg2: None, c001_1029: None }; let intel = decide(&INTEL_806C1); assert_eq!(intel.in_force(&unread), ALWAYS); + assert_eq!(intel.in_force(&Registers { spec_ctrl: Some(msr::IBRS), ..unread }), ALWAYS.with(EnhancedIbrs)); + // A bit the decision does not name shows nothing. + assert_eq!(intel.in_force(&Registers { spec_ctrl: Some(msr::BHI_DIS_S), ..unread }), ALWAYS); let both = ALWAYS.with(SrbdsMicrocode).with(GdsMicrocode); assert_eq!(intel.in_force(&Registers { mcu_opt_ctrl: Some(0), ..unread }), both); assert_eq!(intel.in_force(&Registers { mcu_opt_ctrl: Some(msr::GDS_MITG_DIS), ..unread }), ALWAYS.with(SrbdsMicrocode)); @@ -1561,6 +1653,48 @@ mod tests { assert_eq!(amd.in_force(&Registers { de_cfg2: Some(0), c001_1029: Some(0), ..unread }), ALWAYS); } + /// The registers each verdict sets at bring-up: `IBRS` for enhanced IBRS + /// and AMD's always-on IBRS, `BHI_DIS_S` where it is named, and the + /// microcode opt-outs cleared where their mitigation is; and what a + /// register that does not hold them leaves unapplied. + #[test] + fn each_verdict_sets_the_registers_its_mitigations_name() { + let t14 = decide(&INTEL_806C1); + assert_eq!(t14.spec_ctrl(), msr::IBRS); + assert_eq!(t14.mcu_opt_ctrl_clears(), msr::RNGDS_MITG_DIS | msr::GDS_MITG_DIS); + let set = Registers { spec_ctrl: Some(msr::IBRS), mcu_opt_ctrl: Some(0), de_cfg2: None, c001_1029: None }; + assert_eq!(t14.unapplied(&set), Mitigations::NONE); + let firmware = Registers { spec_ctrl: Some(0), mcu_opt_ctrl: Some(msr::GDS_MITG_DIS), ..set }; + assert_eq!(t14.unapplied(&firmware), Mitigations::of(&[EnhancedIbrs, GdsMicrocode])); + // Enhanced IBRS sets a bit only CPUID.(07H,0):EDX[26] enumerates. + let no_ibrs = decide(&Facts { cpuid_7_0_edx: INTEL_806C1.cpuid_7_0_edx & !IBRS_IBPB, ..INTEL_806C1 }); + assert_eq!(no_ibrs.spec_ctrl(), 0); + assert_eq!(no_ibrs.verdict(SpectreV2), lacking(&[EnhancedIbrs, Smep, Ibpb], &[EnhancedIbrs, Ibpb])); + + let bhi_ctrl = decide(&Facts { cpuid_7_2_edx: BHI_CTRL, ..INTEL_806C1 }); + assert_eq!(bhi_ctrl.spec_ctrl(), msr::IBRS | msr::BHI_DIS_S); + + let zen2 = decide(&AMD_17H_60H); + assert_eq!((zen2.spec_ctrl(), zen2.mcu_opt_ctrl_clears()), (0, 0)); + let preferred = amd::IBRS | amd::IBRS_ALWAYS_ON | amd::IBRS_PREFERRED; + let always_on = decide(&Facts { cpuid_8000_0008_ebx: AMD_17H_60H.cpuid_8000_0008_ebx | preferred, ..AMD_17H_60H }); + assert_eq!(always_on.spec_ctrl(), msr::IBRS); + let lfence = Registers { spec_ctrl: Some(0), mcu_opt_ctrl: None, de_cfg2: None, c001_1029: Some(0) }; + assert_eq!(always_on.unapplied(&lfence), Mitigations::of(&[LfenceDispatchSerializing, IbrsAlwaysOn])); + } + + /// Which CPUs have `IA32_SPEC_CTRL` at all. + #[test] + fn spec_ctrl_exists_where_a_bit_of_it_is_enumerated() { + assert!(INTEL_806C1.has_spec_ctrl()); + assert!(AMD_17H_60H.has_spec_ctrl()); + assert!(!NOTHING.has_spec_ctrl()); + assert!(Facts { cpuid_7_2_edx: PSFD, ..NOTHING }.has_spec_ctrl()); + assert!(!Facts { cpuid_7_0_edx: MD_CLEAR | L1D_FLUSH, ..NOTHING }.has_spec_ctrl()); + assert!(!Facts { vendor: Vendor::Amd, cpuid_8000_0008_ebx: amd::IBPB, ..NOTHING }.has_spec_ctrl()); + assert!(!Facts { vendor: Vendor::Other(*b"HygonGenuine"), ..AMD_17H_60H }.has_spec_ctrl()); + } + /// The set's bits are the enum's discriminants, so a mitigation left /// out of `ALL` would be needed and never named. #[test] diff --git a/kernel/src/actuator.rs b/kernel/src/actuator.rs index 406855f2c58..77c7b17c6d8 100644 --- a/kernel/src/actuator.rs +++ b/kernel/src/actuator.rs @@ -48,6 +48,11 @@ actuators! { /// `a_user_copy_highest_first_faults_or_ends_as_bad_address`. copies_go_down = "copies-go-down"; + /// Set `IA32_MCU_OPT_CTRL.GDS_MITG_DIS` on each CPU whose decision names + /// the microcode's gather mitigation, as firmware that opted out would + /// leave it, before the kernel's own write. Judged by `gds_found_disabled`. + gds_found_disabled = "gds-found-disabled"; + /// Have the first syscall null SS, force a switch, and report whether it reloaded — the /// AMD `SYSRET` SS-attributes workaround's only guest-observable proof. sysret_ss_probe = "sysret-ss-probe"; diff --git a/kernel/src/arch/x86_64/mitigation.rs b/kernel/src/arch/x86_64/mitigation.rs index dc60818f03b..07b17c9a1b3 100644 --- a/kernel/src/arch/x86_64/mitigation.rs +++ b/kernel/src/arch/x86_64/mitigation.rs @@ -5,8 +5,9 @@ //! differ. //! //! [`apply`] sets the per-CPU controls the decision names that the kernel -//! owns here. The boot CPU runs it before the allocator is up, so what it -//! returns and where the boot CPU keeps it hold no heap. +//! owns here: `IA32_SPEC_CTRL` whole, the named `IA32_MCU_OPT_CTRL` +//! opt-outs and AMD's LFENCE bit. The boot CPU runs it before the allocator +//! is up, so what it returns and where the boot CPU keeps it hold no heap. use core::fmt; @@ -23,8 +24,13 @@ use crate::log; use crate::sync::Lock; const IA32_ARCH_CAPABILITIES: u32 = 0x10A; +/// Intel, "CPUID Enumeration and Architectural MSRs", Table 3; AMD's +/// `SPEC_CTRL` at the same address. +const IA32_SPEC_CTRL: u32 = 0x48; /// Intel, "CPUID Enumeration and Architectural MSRs", Table 6. const IA32_MCU_OPT_CTRL: u32 = 0x123; +/// `IA32_MCU_OPT_CTRL.GDS_MITG_DIS`: Intel, "Gather Data Sampling", Table 3. +const GDS_MITG_DIS: u64 = 1 << 4; /// AMD, "Technical Guidance for Mitigating Branch Type Confusion" §6.2.1. const DE_CFG2: u32 = 0xC001_10E3; /// AMD 70586 G-2. @@ -37,8 +43,8 @@ pub struct Read { /// every CPU is up. facts: Facts, registers: Registers, - /// C001_1029 as [`apply`] found it, where it wrote it. - c001_1029_found: Option, + /// Each register [`apply`] writes, as it found it. + found: Registers, } /// The boot CPU's [`Read`]. @@ -81,27 +87,46 @@ fn words() -> Facts { /// Sets this CPU's controls the decision on its words names, once its /// microcode has loaded and before anything relies on them, and returns what -/// it read of itself. The control is LFENCE dispatch serializing, on which -/// the clocksource's ordered TSC read (`RDTSCP; LFENCE`) relies as a -/// bounds-check barrier does: a CPU where the bit does not hold stops the -/// machine, as one without SMEP does. Which registers it reads does not -/// depend on its core's other threads. +/// it read of itself. The clocksource's ordered TSC read (`RDTSCP; LFENCE`) +/// relies on LFENCE dispatch serializing as a bounds-check barrier does: a +/// CPU where that bit does not hold stops the machine, as one without SMEP +/// does. A register that does not hold another write is its line's to say. +/// Which registers it reads does not depend on its core's other threads. pub fn apply() -> Read { let facts = words(); let decision = decide(&facts); let lfence = decision.enumerates(Mitigation::LfenceDispatchSerializing); - let c001_1029_found = lfence.then(|| cpu::rdmsr(C001_1029)); - if let Some(found) = c001_1029_found { - // SAFETY: the decision found the MSR on this CPU's family, outside a - // guest; the bit changes only how LFENCE dispatches. - unsafe { cpu::wrmsr(C001_1029, found | C001_1029_LFENCE_SERIALIZES_DISPATCH) }; + let mcu_opt_ctrl = + decision.enumerates(Mitigation::SrbdsMicrocode) || decision.enumerates(Mitigation::GdsMicrocode); + if mcu_opt_ctrl && decision.enumerates(Mitigation::GdsMicrocode) && crate::actuator::gds_found_disabled() { + // SAFETY: GDS_CTRL enumerates the bit; the write below clears it. + unsafe { cpu::wrmsr(IA32_MCU_OPT_CTRL, cpu::rdmsr(IA32_MCU_OPT_CTRL) | GDS_MITG_DIS) }; } let read = |enumerated: bool, msr: u32| enumerated.then(|| cpu::rdmsr(msr)); + let found = Registers { + spec_ctrl: read(facts.has_spec_ctrl(), IA32_SPEC_CTRL), + mcu_opt_ctrl: read(mcu_opt_ctrl, IA32_MCU_OPT_CTRL), + de_cfg2: None, + c001_1029: read(lfence, C001_1029), + }; + // SAFETY: each write names only bits the CPU enumerates, and each turns a + // mitigation on: `IA32_SPEC_CTRL`'s while no program that asked runs, the + // microcode's by clearing its opt-out, and LFENCE's dispatch ordering, + // whose MSR the decision found on this CPU's family outside a guest. + unsafe { + if found.spec_ctrl.is_some() { + cpu::wrmsr(IA32_SPEC_CTRL, decision.spec_ctrl()); + } + if let Some(opt) = found.mcu_opt_ctrl.filter(|opt| opt & decision.mcu_opt_ctrl_clears() != 0) { + cpu::wrmsr(IA32_MCU_OPT_CTRL, opt & !decision.mcu_opt_ctrl_clears()); + } + if let Some(c001_1029) = found.c001_1029 { + cpu::wrmsr(C001_1029, c001_1029 | C001_1029_LFENCE_SERIALIZES_DISPATCH); + } + } let registers = Registers { - mcu_opt_ctrl: read( - decision.enumerates(Mitigation::SrbdsMicrocode) || decision.enumerates(Mitigation::GdsMicrocode), - IA32_MCU_OPT_CTRL, - ), + spec_ctrl: read(found.spec_ctrl.is_some(), IA32_SPEC_CTRL), + mcu_opt_ctrl: read(mcu_opt_ctrl, IA32_MCU_OPT_CTRL), de_cfg2: read(decision.enumerates(Mitigation::SuppressBpOnNonBr), DE_CFG2), c001_1029: read(lfence, C001_1029), }; @@ -111,7 +136,7 @@ pub fn apply() -> Read { "mitigation: C001_1029 reads {now:#x} once LFENCE dispatch serializing was set, and the ordered TSC read needs it" ); } - Read { facts, registers, c001_1029_found } + Read { facts, registers, found } } /// Keeps the boot CPU's [`apply`], which it takes before anything is sized. @@ -149,7 +174,7 @@ pub fn report() { } fn say(cpu_id: u32, read: &Read) { - let Read { facts, registers, c001_1029_found } = *read; + let Read { facts, registers, found } = *read; let Facts { vendor, signature, @@ -164,17 +189,21 @@ fn say(cpu_id: u32, read: &Read) { cpuid_8000_0021_eax, cpuid_8000_0021_ecx, } = facts; - let Registers { mcu_opt_ctrl, de_cfg2, c001_1029 } = registers; + let Registers { spec_ctrl, mcu_opt_ctrl, de_cfg2, c001_1029 } = registers; log!( "mitigations: cpu{cpu_id} words {vendor:?} signature={signature:#x} microcode={microcode:#x} hypervisor={hypervisor} \ smt={smt} cpuid.7.0.ebx={cpuid_7_0_ebx:#x} cpuid.7.0.edx={cpuid_7_0_edx:#x} cpuid.7.2.edx={cpuid_7_2_edx:#x} \ arch_capabilities={arch_capabilities:#x} cpuid.80000008.ebx={cpuid_8000_0008_ebx:#x} \ cpuid.80000021.eax={cpuid_8000_0021_eax:#x} cpuid.80000021.ecx={cpuid_8000_0021_ecx:#x} \ - mcu_opt_ctrl={} de_cfg2={} c001_1029={} c001_1029_found={}", + spec_ctrl={} mcu_opt_ctrl={} de_cfg2={} c001_1029={} \ + spec_ctrl_found={} mcu_opt_ctrl_found={} c001_1029_found={}", + Msr(spec_ctrl), Msr(mcu_opt_ctrl), Msr(de_cfg2), Msr(c001_1029), - Msr(c001_1029_found), + Msr(found.spec_ctrl), + Msr(found.mcu_opt_ctrl), + Msr(found.c001_1029), ); let decision = decide(&facts); let in_force = decision.in_force(®isters); diff --git a/tests/checks.rs b/tests/checks.rs index 0f63f13c5be..000dbd23e3f 100644 --- a/tests/checks.rs +++ b/tests/checks.rs @@ -557,11 +557,11 @@ mod checks { Ok(()) } - /// The T14's lines with `IA32_MCU_OPT_CTRL` as its microcode leaves it, - /// neither mitigation opted out. + /// The T14's lines with `IA32_SPEC_CTRL.IBRS` and neither + /// `IA32_MCU_OPT_CTRL` opt-out, as the kernel leaves them. const T14_BENCH_LINES: [&str; 17] = [ "spectre_v1: mitigated: user pointers clamped without a branch", - "spectre_v2: vulnerable: needs enhanced IBRS, IBPB between programs that ask; has SMEP", + "spectre_v2: vulnerable: needs IBPB between programs that ask; has SMEP, enhanced IBRS", "branch_history_injection: vulnerable: needs BHB cleared on entry", "retbleed: not affected", "spec_rstack_overflow: not affected", @@ -627,8 +627,9 @@ mod checks { } Ok(()) }; - held("the T14", 0xbe, Registers { mcu_opt_ctrl: Some(0), de_cfg2: None, c001_1029: None }, &T14_BENCH_LINES)?; - let amd = Registers { mcu_opt_ctrl: None, de_cfg2: Some(1 << 1), c001_1029: Some(1 << 1) }; + let t14 = Registers { spec_ctrl: Some(1 << 0), mcu_opt_ctrl: Some(0), de_cfg2: None, c001_1029: None }; + held("the T14", 0xbe, t14, &T14_BENCH_LINES)?; + let amd = Registers { spec_ctrl: Some(0), mcu_opt_ctrl: None, de_cfg2: Some(1 << 1), c001_1029: Some(1 << 1) }; held("the AMD bench", 0x0860_010d, amd, &AMD_BENCH_LINES) } @@ -638,23 +639,30 @@ mod checks { fn mitigations_verdict() -> Result<(), String> { let amd = bench_words("the AMD bench")?.split_whitespace().collect::>().join(" "); let words = |extra: &str| format!("words {amd} microcode=0x860010d {extra}"); - const READ: &str = "mcu_opt_ctrl=- de_cfg2=0x2 c001_1029=0x2 c001_1029_found=0x0"; + let read = |de_cfg2: &str, c001_1029: &str, c001_1029_found: &str| { + format!( + "spec_ctrl=0x0 mcu_opt_ctrl=- de_cfg2={de_cfg2} c001_1029={c001_1029} \ + spec_ctrl_found=0x0 mcu_opt_ctrl_found=- c001_1029_found={c001_1029_found}" + ) + }; + let read_whole = read("0x2", "0x2", "0x0"); + let read_whole = read_whole.as_str(); let lines = |cpu: u32, words: &str, lines: &[&str]| -> String { std::iter::once(words) .chain(lines.iter().copied()) .map(|said| format!("[ 0.050 cpu{cpu} kernel] mitigations: cpu{cpu} {said}\n")) .collect() }; - let boot = lines(0, &words(READ), &AMD_BENCH_LINES); + let boot = lines(0, &words(read_whole), &AMD_BENCH_LINES); mitigations_on_metal(&boot).map_err(|e| format!("a boot's own lines were refused: {e}"))?; // A later CPU says its lines where it differs: here, firmware had set // the bit before the kernel did. - let firmware_set = lines(5, &words("mcu_opt_ctrl=- de_cfg2=0x2 c001_1029=0x2 c001_1029_found=0x2"), &AMD_BENCH_LINES); + let firmware_set = lines(5, &words(&read("0x2", "0x2", "0x2")), &AMD_BENCH_LINES); mitigations_on_metal(&(boot.clone() + &firmware_set)).map_err(|e| format!("a later CPU's own lines were refused: {e}"))?; // A register the kernel only reads, read clear, is another boot's lines and as good. let mut unset = AMD_BENCH_LINES; unset[3] = "retbleed: vulnerable: needs Jmp2Ret, DE_CFG2[SuppressBPOnNonBr]; SMT vulnerable: STIBP only for programs that ask (policy)"; - mitigations_on_metal(&lines(0, &words("mcu_opt_ctrl=- de_cfg2=0x0 c001_1029=0x2 c001_1029_found=0x0"), &unset)) + mitigations_on_metal(&lines(0, &words(&read("0x0", "0x2", "0x0")), &unset)) .map_err(|e| format!("DE_CFG2 read clear was refused: {e}"))?; let refused = |what: &str, log: String, says: &str| match mitigations_on_metal(&log) { @@ -663,45 +671,85 @@ mod checks { Err(e) => Err(format!("{what} was refused for the wrong reason: {e}")), }; refused("a boot with no lines", String::new(), "cpu0 wrote no `mitigations:` lines")?; - refused("sixteen lines", lines(0, &words(READ), &AMD_BENCH_LINES[..16]), "expected one `itlb_multihit: ` line")?; + refused("sixteen lines", lines(0, &words(read_whole), &AMD_BENCH_LINES[..16]), "expected one `itlb_multihit: ` line")?; let mut wrong = AMD_BENCH_LINES; wrong[7] = "meltdown: vulnerable: needs kernel unmapped in user mode"; - refused("a line the words do not decide", lines(0, &words(READ), &wrong), "line is not the decision on its words")?; + refused("a line the words do not decide", lines(0, &words(read_whole), &wrong), "line is not the decision on its words")?; // The bit the kernel sets, read clear, with lines that say so. let mut lfence = AMD_BENCH_LINES; lfence[0] = "spectre_v1: vulnerable: needs LFENCE dispatch-serializing; has user pointers clamped without a branch"; - let clear = words("mcu_opt_ctrl=- de_cfg2=0x2 c001_1029=0x0 c001_1029_found=0x0"); - refused("cpu0's LFENCE bit read clear", lines(0, &clear, &lfence), "the LFENCE bit the kernel sets is clear")?; - refused("a later CPU's LFENCE bit read clear", boot.clone() + &lines(7, &clear, &lfence), "cpu7's C001_1029")?; - refused("a later CPU's wrong line", boot.clone() + &lines(7, &words(READ), &wrong), "cpu7's meltdown: line")?; - refused("a later CPU's lines without words", boot + &lines(7, &words(READ), &AMD_BENCH_LINES).lines().skip(1).map(|l| format!("{l}\n")).collect::(), "cpu7 wrote no one")?; + let clear = words(&read("0x2", "0x0", "0x0")); + refused("cpu0's LFENCE bit read clear", lines(0, &clear, &lfence), "leave off what the kernel sets: LFENCE dispatch-serializing")?; + refused("a later CPU's LFENCE bit read clear", boot.clone() + &lines(7, &clear, &lfence), "cpu7's registers")?; + refused("a later CPU's wrong line", boot.clone() + &lines(7, &words(read_whole), &wrong), "cpu7's meltdown: line")?; + refused("a later CPU's lines without words", boot + &lines(7, &words(read_whole), &AMD_BENCH_LINES).lines().skip(1).map(|l| format!("{l}\n")).collect::(), "cpu7 wrote no one")?; // A misread whose lines agree with it, which only the pin tells from // the bench's: SSBD read clear, and lines decided from that. let agreeing = |said: &str| -> Result { - use kernel::mitigation::{decide, Facts, Registers, Vulnerability}; + use kernel::mitigation::{decide, Facts, Vulnerability}; let (facts, fields) = mitigation_words(said.trim_start_matches("words "))?; - let hex = |name: &str| match fields.get(name).copied() { - Some("-") | None => None, - Some(digits) => u64::from_str_radix(digits.trim_start_matches("0x"), 16).ok(), - }; - let microcode = kernel::microcode::Revision(hex("microcode").and_then(|value| u32::try_from(value).ok()).unwrap_or(0)); - let decision = decide(&Facts { microcode, ..facts }); - let in_force = decision.in_force(&Registers { mcu_opt_ctrl: hex("mcu_opt_ctrl"), de_cfg2: hex("de_cfg2"), c001_1029: hex("c001_1029") }); + let microcode = fields.get("microcode").and_then(|digits| u32::from_str_radix(digits.trim_start_matches("0x"), 16).ok()); + let decision = decide(&Facts { microcode: kernel::microcode::Revision(microcode.unwrap_or(0)), ..facts }); + let in_force = decision.in_force(&mitigation_registers(&fields)?); let decided: Vec = Vulnerability::ALL.iter().map(|&of| format!("{}: {}", of.name(), decision.line(of, in_force))).collect(); Ok(lines(0, said, &decided.iter().map(String::as_str).collect::>())) }; - refused("a misread the lines agree with", agreeing(&words(READ).replace("0x90cf757", "0x80cf757"))?, "not the AMD bench's pinned words")?; - refused("a machine no bench is", agreeing(&words(READ).replace("0x860f01", "0x870f10"))?, "no bench's whose words are pinned")?; + refused("a misread the lines agree with", agreeing(&words(read_whole).replace("0x90cf757", "0x80cf757"))?, "not the AMD bench's pinned words")?; + refused("a machine no bench is", agreeing(&words(read_whole).replace("0x860f01", "0x870f10"))?, "no bench's whose words are pinned")?; // The T14 with `ARCH_CAPABILITIES` read as 0, and lines that agree: // affected by all it was not, which only its pinned words refuse. let t14 = bench_words("the T14")?.split_whitespace().collect::>().join(" "); - let t14 = format!("words {t14} microcode=0xbe mcu_opt_ctrl=0x0 de_cfg2=- c001_1029=- c001_1029_found=-"); - mitigations_on_metal(&agreeing(&t14)?).map_err(|e| format!("the T14's own words and lines were refused: {e}"))?; + let t14 = |spec_ctrl: &str, mcu_opt_ctrl: &str| { + format!( + "words {t14} microcode=0xbe spec_ctrl={spec_ctrl} mcu_opt_ctrl={mcu_opt_ctrl} de_cfg2=- c001_1029=- \ + spec_ctrl_found=0x0 mcu_opt_ctrl_found=0x10 c001_1029_found=-" + ) + }; + mitigations_on_metal(&agreeing(&t14("0x1", "0x0"))?).map_err(|e| format!("the T14's own words and lines were refused: {e}"))?; + // What the kernel writes, read back unwritten, with lines that agree. + refused("the T14's IBRS read clear", agreeing(&t14("0x0", "0x0"))?, "leave off what the kernel sets: enhanced IBRS")?; + refused("the T14's gather opt-out read set", agreeing(&t14("0x1", "0x10"))?, "the kernel sets: microcode's gather mitigation")?; + let t14 = t14("0x1", "0x0"); refused("the T14's ARCH_CAPABILITIES read as 0", agreeing(&t14.replace("arch_capabilities=0xa005c6b", "arch_capabilities=0x0"))?, "not the T14's pinned words")?; - refused("a guest's words", lines(0, &words(READ).replace("hypervisor=false", "hypervisor=true"), &AMD_BENCH_LINES), "a hypervisor's guest's")?; - refused("another vendor's words", lines(0, &words(READ).replace("words Amd", "words Other(\"HygonGenuine\")"), &AMD_BENCH_LINES), "whose guidance nothing reads")?; - refused("words without a field", lines(0, &words(READ).replace(" smt=true", ""), &AMD_BENCH_LINES), "carry no smt")?; - refused("words without a register", lines(0, &words(READ).replace(" de_cfg2=0x2", ""), &AMD_BENCH_LINES), "carry no de_cfg2")?; + refused("a guest's words", lines(0, &words(read_whole).replace("hypervisor=false", "hypervisor=true"), &AMD_BENCH_LINES), "a hypervisor's guest's")?; + // A guest's own lines, judged as a guest's: no pin, and its words + // must be a guest's. + let guest = agreeing(&words(read_whole).replace("hypervisor=false", "hypervisor=true").replace("0x860f01", "0x870f10"))?; + let as_guest = |log: &str| -> Result<(), String> { + mitigation_lines(log)?.iter().try_for_each(|(cpu, said)| mitigations_of_a_cpu(*cpu, said, Machine::Guest)) + }; + as_guest(&guest).map_err(|e| format!("a guest's own lines were refused: {e}"))?; + match as_guest(&lines(0, &words(read_whole), &AMD_BENCH_LINES)) { + Err(e) if e.contains("no guest's") => {} + other => return Err(format!("a bench's words judged as a guest's: {other:?}")), + } + refused("another vendor's words", lines(0, &words(read_whole).replace("words Amd", "words Other(\"HygonGenuine\")"), &AMD_BENCH_LINES), "whose guidance nothing reads")?; + refused("words without a field", lines(0, &words(read_whole).replace(" smt=true", ""), &AMD_BENCH_LINES), "carry no smt")?; + refused("words without a register", lines(0, &words(read_whole).replace(" de_cfg2=0x2", ""), &AMD_BENCH_LINES), "carry no de_cfg2")?; + Ok(()) + } + + /// [`gds_found_disabled`] against the T14's words as the armed boot + /// writes them, and an arm that did not take or a clear that did not. + #[test] + fn gds_found_disabled_verdict() -> Result<(), String> { + let t14 = bench_words("the T14")?.split_whitespace().collect::>().join(" "); + let words = |cpu: u32, found: &str, now: &str| { + format!( + "[ 0.050 cpu{cpu} kernel] mitigations: cpu{cpu} words {t14} microcode=0xbe spec_ctrl=0x1 \ + mcu_opt_ctrl={now} de_cfg2=- c001_1029=- spec_ctrl_found=0x0 mcu_opt_ctrl_found={found} c001_1029_found=-\n" + ) + }; + gds_found_disabled(&words(0, "0x10", "0x0")).map_err(|e| format!("the armed boot's words were refused: {e}"))?; + let refused = |what: &str, log: String, says: &str| match gds_found_disabled(&log) { + Ok(()) => Err(format!("{what} was accepted")), + Err(e) if e.contains(says) => Ok(()), + Err(e) => Err(format!("{what} was refused for the wrong reason: {e}")), + }; + refused("a boot with no words", String::new(), "cpu0 wrote no")?; + refused("an arm that did not take", words(0, "0x0", "0x0"), "did not take")?; + refused("a clear that did not", words(0, "0x10", "0x10"), "GDS_MITG_DIS stands")?; + refused("a later CPU's clear that did not", words(0, "0x10", "0x0") + &words(3, "0x10", "0x10"), "cpu3")?; Ok(()) } diff --git a/tests/metal/src/lib.rs b/tests/metal/src/lib.rs index 4d5102453d4..291e49ed18b 100644 --- a/tests/metal/src/lib.rs +++ b/tests/metal/src/lib.rs @@ -782,6 +782,10 @@ pub const FLASHABLE: &[&str] = &[ // a reset does not; the boot goes on to userland and ends the way an // unarmed one does. "nmi-user-gs", + // It sets `IA32_MCU_OPT_CTRL.GDS_MITG_DIS` on each CPU just before the + // kernel's own write clears it, so the machine runs as an unarmed one + // does; a reset clears it too (Intel, "Gather Data Sampling", Table 3). + "gds-found-disabled", // It enters one port in the kernel's table of declared ports at boot. No // kernel path reads or writes a port for that entry, so it reaches no // device; what it changes is that a claim on the i8042's row is refused. diff --git a/tests/toyos.rs b/tests/toyos.rs index 7d266e9debc..a11d8074279 100644 --- a/tests/toyos.rs +++ b/tests/toyos.rs @@ -701,6 +701,12 @@ const MACHINE_TESTS: &[MachineTest] = &[ // `images/console.toml`'s image, which runs no job and hands no machine // back: a metal boot of it ends with a hand on the power button. ("console_image_boots", |_| console_image_boots()), + // What a hardware-virtualized CPU shows a guest, through the kernel's + // own lines: each CPU's writes at bring-up read back, and its lines are + // the decision on its words. A KVM guest on x86-64 only: the words are the + // hypervisor's, and TCG's CPU enumerates none of the controls. The + // benches' are the `mitigations` row's. + ("mitigations_in_a_guest", |_| mitigations_in_a_guest()), // The UEFI loader's check of a boot program and what follows its refusal, // which no host test reaches: a marked slot whose supervisor is bent is // refused before boot services end, and the machine stops with the whole @@ -1604,6 +1610,13 @@ const METAL: &[(&str, metal::Metal)] = &[ }, }, ), + ( + // The kernel's own write clears the gather mitigation's opt-out that + // the arm set before it, as firmware that opted out would leave it. + // The T14 alone: the AMD bench has no `IA32_MCU_OPT_CTRL`. + "gds_found_disabled", + metal::Metal { on: On::Only(Bench::T14), arms: SELFTESTS, judge: |b| gds_found_disabled(b[0].kernel().text()) }, + ), ( // No drain on this side: the whole boot's records are on the log, so // the probe's line is either in them or it never ran. @@ -2337,6 +2350,7 @@ const SELFTEST_PARAMS: &[&str] = &[ "sysret-ss-probe", "hpet-stall", "nmi-user-gs", + "gds-found-disabled", ]; const SELFTESTS: &[metal::Arm] = &[metal::Arm { @@ -7172,8 +7186,47 @@ fn mitigation_words(words: &str) -> Result<(kernel::mitigation::Facts, BTreeMap< } /// Every CPU's `mitigations:` lines in `log`, cpu0's and those of each later -/// CPU that differed from it: [`mitigations_of_a_cpu`] on each. +/// CPU that differed from it: [`mitigations_of_a_cpu`] on each, as a bench's. fn mitigations_on_metal(log: &str) -> Result<(), String> { + for (cpu, lines) in &mitigation_lines(log)? { + mitigations_of_a_cpu(*cpu, lines, Machine::Bench)?; + } + Ok(()) +} + +/// [`mitigations_of_a_cpu`] on a guest's every CPU, and its words put on the +/// run's output: they are what the host's CPU shows a guest. +fn mitigations_in_a_guest() -> Result<(), String> { + let qemu = QemuInstance::boot_with_options( + &compile::repo_root().join("tests/images/testcases.toml"), + &[], + &[], + BootOptions { smp: 4, ..Default::default() }, + ); + let said = serial::Serial::named("the guest's boot", qemu.boot_log()); + said.must_be_clean()?; + let lines = mitigation_lines(said.text())?; + for (cpu, lines) in &lines { + mitigations_of_a_cpu(*cpu, lines, Machine::Guest)?; + for words in lines.iter().filter_map(|line| line.strip_prefix("words ")) { + eprintln!(" [mitigations] cpu{cpu} words {words}"); + } + } + Ok(()) +} + +/// Whose `mitigations:` lines [`mitigations_of_a_cpu`] judges. +#[derive(Clone, Copy, PartialEq)] +enum Machine { + /// A bench's, whose words are pinned. + Bench, + /// A hypervisor's guest's, whose words are what its hypervisor shows. + Guest, +} + +/// Each CPU's `mitigations:` lines in `log`, by CPU, the words line among +/// them; cpu0's at least. +fn mitigation_lines(log: &str) -> Result>, String> { let mut said: BTreeMap> = BTreeMap::new(); for line in log.lines() { let Some((_, rest)) = line.split_once("mitigations: cpu") else { continue }; @@ -7184,55 +7237,58 @@ fn mitigations_on_metal(log: &str) -> Result<(), String> { if !said.contains_key(&0) { return Err("cpu0 wrote no `mitigations:` lines".to_string()); } - for (cpu, lines) in &said { - mitigations_of_a_cpu(*cpu, lines)?; - } - Ok(()) + Ok(said) } /// One CPU's words and its line for each vulnerability: the words name a -/// vendor whose guidance is read, are no guest's, and are its bench's pinned -/// words; each line is what [`kernel::mitigation::decide`] says of the words -/// with what the registers they give show in force; and what the kernel sets -/// itself is in force wherever the decision names it. -fn mitigations_of_a_cpu(cpu: u32, said: &[&str]) -> Result<(), String> { - use kernel::mitigation::{decide, Facts, Mitigation, Registers, Verdict, Vulnerability}; +/// vendor whose guidance is read, and are a bench's pinned words, or a +/// guest's; each line is what [`kernel::mitigation::decide`] says of the +/// words with what the registers they give show in force; and what the +/// kernel sets itself is in force wherever the decision names it. +fn mitigations_of_a_cpu(cpu: u32, said: &[&str], machine: Machine) -> Result<(), String> { + use kernel::mitigation::{decide, Facts, Verdict, Vulnerability}; let mut words = said.iter().filter_map(|said| said.strip_prefix("words ")); let words = match (words.next(), words.next()) { (Some(words), None) => words, _ => return Err(format!("cpu{cpu} wrote no one `mitigations:` words line: {said:?}")), }; let (facts, fields) = mitigation_words(words).map_err(|e| format!("cpu{cpu}: {e}"))?; - if facts.hypervisor { - return Err(format!("cpu{cpu}'s words are a hypervisor's guest's, and this row judges a machine's: {words:?}")); + match (machine, facts.hypervisor) { + (Machine::Bench, true) => { + return Err(format!("cpu{cpu}'s words are a hypervisor's guest's, and this row judges a machine's: {words:?}")) + } + (Machine::Guest, false) => return Err(format!("cpu{cpu}'s words are no guest's, and this row judges one: {words:?}")), + _ => {} } let field = |name: &str| fields.get(name).copied().ok_or_else(|| format!("cpu{cpu}'s words carry no {name}: {words:?}")); let hex = |name: &str, digits: &str| { u64::from_str_radix(digits.trim_start_matches("0x"), 16).map_err(|e| format!("cpu{cpu}'s {name}={digits}: {e}")) }; - let msr = |name: &str| match field(name)? { - "-" => Ok(None), - digits => hex(name, digits).map(Some), - }; let microcode = field("microcode").and_then(|digits| hex("microcode", digits))?; let microcode = kernel::microcode::Revision(u32::try_from(microcode).map_err(|e| format!("cpu{cpu}'s microcode: {e}"))?); - let pinned = MITIGATION_BENCHES - .iter() - .map(|&(bench, pinned)| mitigation_words(pinned).map(|(pinned, _)| (bench, pinned))) - .collect::, _>>()?; - let Some(&(bench, pinned)) = pinned.iter().find(|(_, pinned)| pinned.signature == facts.signature) else { - return Err(format!("cpu{cpu}'s signature {:#x} is no bench's whose words are pinned: {words:?}", facts.signature)); + let whose = match machine { + Machine::Guest => "a guest", + Machine::Bench => { + let pinned = MITIGATION_BENCHES + .iter() + .map(|&(bench, pinned)| mitigation_words(pinned).map(|(pinned, _)| (bench, pinned))) + .collect::, _>>()?; + let Some(&(bench, pinned)) = pinned.iter().find(|(_, pinned)| pinned.signature == facts.signature) else { + return Err(format!("cpu{cpu}'s signature {:#x} is no bench's whose words are pinned: {words:?}", facts.signature)); + }; + if facts != pinned { + return Err(format!("cpu{cpu}'s words are not {bench}'s pinned words: {words:?} against {pinned:?}")); + } + bench + } }; - if facts != pinned { - return Err(format!("cpu{cpu}'s words are not {bench}'s pinned words: {words:?} against {pinned:?}")); - } let facts = Facts { microcode, ..facts }; - let registers = Registers { mcu_opt_ctrl: msr("mcu_opt_ctrl")?, de_cfg2: msr("de_cfg2")?, c001_1029: msr("c001_1029")? }; + let registers = mitigation_registers(&fields).map_err(|e| format!("cpu{cpu}: {e}"))?; let decision = decide(&facts); let in_force = decision.in_force(®isters); - let lfence = Mitigation::LfenceDispatchSerializing; - if decision.enumerates(lfence) && !in_force.has(lfence) { - return Err(format!("cpu{cpu}'s C001_1029 reads {:?}: the LFENCE bit the kernel sets is clear", registers.c001_1029)); + let unapplied = decision.unapplied(®isters); + if !unapplied.is_empty() { + return Err(format!("cpu{cpu}'s registers {registers:?} leave off what the kernel sets: {unapplied}")); } for of in Vulnerability::ALL { let prefix = format!("{}: ", of.name()); @@ -7248,13 +7304,64 @@ fn mitigations_of_a_cpu(cpu: u32, said: &[&str]) -> Result<(), String> { } let affected = Vulnerability::ALL.iter().filter(|&&of| decision.verdict(of) != Verdict::NotAffected).count(); eprintln!( - " [mitigations] cpu{cpu}, {bench}: its lines are the decision on its pinned words, {affected} naming a vulnerability; \ - C001_1029 found {}", + " [mitigations] cpu{cpu}, {whose}: its lines are the decision on its words, {affected} naming a vulnerability; \ + as found, SPEC_CTRL {} MCU_OPT_CTRL {} C001_1029 {}", + field("spec_ctrl_found")?, + field("mcu_opt_ctrl_found")?, field("c001_1029_found")?, ); Ok(()) } +/// The registers a `mitigations:` words line gives, `-` for one the CPU was +/// not read for. +fn mitigation_registers(fields: &BTreeMap<&str, &str>) -> Result { + let msr = |name: &str| match fields.get(name).copied() { + None => Err(format!("the words carry no {name}")), + Some("-") => Ok(None), + Some(digits) => u64::from_str_radix(digits.trim_start_matches("0x"), 16).map(Some).map_err(|e| format!("{name}={digits}: {e}")), + }; + Ok(kernel::mitigation::Registers { + spec_ctrl: msr("spec_ctrl")?, + mcu_opt_ctrl: msr("mcu_opt_ctrl")?, + de_cfg2: msr("de_cfg2")?, + c001_1029: msr("c001_1029")?, + }) +} + +/// What `gds-found-disabled` asks: every CPU's `mitigations:` words, cpu0's +/// and each later CPU's that differed, found `IA32_MCU_OPT_CTRL.GDS_MITG_DIS` +/// set, as the arm left it, and read it clear after the kernel's write. +fn gds_found_disabled(log: &str) -> Result<(), String> { + const GDS_MITG_DIS: u64 = 1 << 4; + let words: Vec<(&str, &str)> = log + .lines() + .filter_map(|line| line.split_once("mitigations: cpu")?.1.split_once(" words ")) + .collect(); + if !words.iter().any(|(cpu, _)| *cpu == "0") { + return Err("cpu0 wrote no `mitigations:` words line".to_string()); + } + for (cpu, said) in &words { + let (_, fields) = mitigation_words(said).map_err(|e| format!("cpu{cpu}: {e}"))?; + let now = mitigation_registers(&fields).map_err(|e| format!("cpu{cpu}: {e}"))?.mcu_opt_ctrl; + let found = match fields.get("mcu_opt_ctrl_found").copied() { + Some("-") | None => None, + Some(digits) => u64::from_str_radix(digits.trim_start_matches("0x"), 16).ok(), + }; + match (found, now) { + (Some(found), Some(now)) if found & GDS_MITG_DIS != 0 && now & GDS_MITG_DIS == 0 => {} + (Some(found), _) if found & GDS_MITG_DIS == 0 => { + return Err(format!("cpu{cpu} found IA32_MCU_OPT_CTRL {found:#x}: the arm's GDS_MITG_DIS did not take")) + } + (found, now) => { + return Err(format!("cpu{cpu} found IA32_MCU_OPT_CTRL {found:?} and left it {now:?}: GDS_MITG_DIS stands")) + } + } + } + eprintln!(" [gds] {} words line(s): each found GDS_MITG_DIS set and left it clear", words.len()); + Ok(()) +} + /// One `microcode:` line, the fields [`microcode_on_metal`] holds. #[derive(Debug)] struct MicrocodeLine { From 98c605c463224ce199efa3590d0a9db57f374b5a Mon Sep 17 00:00:00 2001 From: japabu Date: Thu, 12 Nov 2026 07:38:13 +0200 Subject: [PATCH 2/2] Review r1 of #1277: the guest words leave out the signature and microcode - mitigations_in_a_guest prints each guest CPU's lines, its words without signature= and microcode=, which name the host's model, into the public run log (B1). The verdict lines show what the guest's decision makes of the words once the signature is gone. - gds_found_disabled says when a CPU read no IA32_MCU_OPT_CTRL, and names GDS_MITG_LOCK where the arm's write did not take; the record says how to read that red (S3, S4). - apply's arm drops an operand the other implies, and its doc says plainly that any other write that does not take is reported (S1, S2). Co-Authored-By: Claude Opus 5.5 --- ...ctrl-and-gds-stay-as-firmware-left-them.md | 4 +++- kernel/src/arch/x86_64/mitigation.rs | 7 ++++--- tests/checks.rs | 1 + tests/toyos.rs | 20 ++++++++++++++----- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/issues/spec-ctrl-and-gds-stay-as-firmware-left-them.md b/issues/spec-ctrl-and-gds-stay-as-firmware-left-them.md index ae9a984dd29..24cf54f98b9 100644 --- a/issues/spec-ctrl-and-gds-stay-as-firmware-left-them.md +++ b/issues/spec-ctrl-and-gds-stay-as-firmware-left-them.md @@ -35,7 +35,9 @@ before the kernel's write, as firmware that opted out would leave it, and the - Main must show the row green: every CPU's words line with `mcu_opt_ctrl_found` holding bit 4 and `mcu_opt_ctrl` without it. The same boot of main with the clear deleted from `arch::mitigation::apply` - must red it, the mutation below. + must red it, the mutation below. A red saying the arm's write did not + take means firmware set `GDS_MITG_LOCK` (bit 5), and the row has no + premise there rather than a defect. - **Owed on metal:** Linux's reads of 0x48 and 0x123 on each bench, beside ToyOS's, compared in evidence. diff --git a/kernel/src/arch/x86_64/mitigation.rs b/kernel/src/arch/x86_64/mitigation.rs index 07b17c9a1b3..d1a47ec01f0 100644 --- a/kernel/src/arch/x86_64/mitigation.rs +++ b/kernel/src/arch/x86_64/mitigation.rs @@ -90,15 +90,16 @@ fn words() -> Facts { /// it read of itself. The clocksource's ordered TSC read (`RDTSCP; LFENCE`) /// relies on LFENCE dispatch serializing as a bounds-check barrier does: a /// CPU where that bit does not hold stops the machine, as one without SMEP -/// does. A register that does not hold another write is its line's to say. -/// Which registers it reads does not depend on its core's other threads. +/// does. Any other write that does not take is reported by the lines, not +/// stopped. Which registers it reads does not depend on its core's other +/// threads. pub fn apply() -> Read { let facts = words(); let decision = decide(&facts); let lfence = decision.enumerates(Mitigation::LfenceDispatchSerializing); let mcu_opt_ctrl = decision.enumerates(Mitigation::SrbdsMicrocode) || decision.enumerates(Mitigation::GdsMicrocode); - if mcu_opt_ctrl && decision.enumerates(Mitigation::GdsMicrocode) && crate::actuator::gds_found_disabled() { + if decision.enumerates(Mitigation::GdsMicrocode) && crate::actuator::gds_found_disabled() { // SAFETY: GDS_CTRL enumerates the bit; the write below clears it. unsafe { cpu::wrmsr(IA32_MCU_OPT_CTRL, cpu::rdmsr(IA32_MCU_OPT_CTRL) | GDS_MITG_DIS) }; } diff --git a/tests/checks.rs b/tests/checks.rs index 000dbd23e3f..068b2af0918 100644 --- a/tests/checks.rs +++ b/tests/checks.rs @@ -749,6 +749,7 @@ mod checks { refused("a boot with no words", String::new(), "cpu0 wrote no")?; refused("an arm that did not take", words(0, "0x0", "0x0"), "did not take")?; refused("a clear that did not", words(0, "0x10", "0x10"), "GDS_MITG_DIS stands")?; + refused("a CPU that read no IA32_MCU_OPT_CTRL", words(0, "-", "-"), "read no IA32_MCU_OPT_CTRL")?; refused("a later CPU's clear that did not", words(0, "0x10", "0x0") + &words(3, "0x10", "0x10"), "cpu3")?; Ok(()) } diff --git a/tests/toyos.rs b/tests/toyos.rs index a11d8074279..efe5fd6e61f 100644 --- a/tests/toyos.rs +++ b/tests/toyos.rs @@ -7194,8 +7194,10 @@ fn mitigations_on_metal(log: &str) -> Result<(), String> { Ok(()) } -/// [`mitigations_of_a_cpu`] on a guest's every CPU, and its words put on the -/// run's output: they are what the host's CPU shows a guest. +/// [`mitigations_of_a_cpu`] on a guest's every CPU, and its lines put on the +/// run's output, which is public: what the host's CPU shows a guest and what +/// its decision makes of it, but the signature and microcode revision, which +/// name the model. fn mitigations_in_a_guest() -> Result<(), String> { let qemu = QemuInstance::boot_with_options( &compile::repo_root().join("tests/images/testcases.toml"), @@ -7208,8 +7210,12 @@ fn mitigations_in_a_guest() -> Result<(), String> { let lines = mitigation_lines(said.text())?; for (cpu, lines) in &lines { mitigations_of_a_cpu(*cpu, lines, Machine::Guest)?; - for words in lines.iter().filter_map(|line| line.strip_prefix("words ")) { - eprintln!(" [mitigations] cpu{cpu} words {words}"); + for line in lines { + let shown: Vec<&str> = line + .split_whitespace() + .filter(|field| !field.starts_with("signature=") && !field.starts_with("microcode=")) + .collect(); + eprintln!(" [mitigations] cpu{cpu} {}", shown.join(" ")); } } Ok(()) @@ -7351,8 +7357,12 @@ fn gds_found_disabled(log: &str) -> Result<(), String> { match (found, now) { (Some(found), Some(now)) if found & GDS_MITG_DIS != 0 && now & GDS_MITG_DIS == 0 => {} (Some(found), _) if found & GDS_MITG_DIS == 0 => { - return Err(format!("cpu{cpu} found IA32_MCU_OPT_CTRL {found:#x}: the arm's GDS_MITG_DIS did not take")) + return Err(format!( + "cpu{cpu} found IA32_MCU_OPT_CTRL {found:#x}: the arm's GDS_MITG_DIS did not take, as it does not \ + where firmware set GDS_MITG_LOCK (bit 5), which leaves this row no premise" + )) } + (None, _) => return Err(format!("cpu{cpu} read no IA32_MCU_OPT_CTRL: its decision names no gather mitigation")), (found, now) => { return Err(format!("cpu{cpu} found IA32_MCU_OPT_CTRL {found:?} and left it {now:?}: GDS_MITG_DIS stands")) }