[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20240725150110.327601-2-mlevitsk@redhat.com>
Date: Thu, 25 Jul 2024 11:01:09 -0400
From: Maxim Levitsky <mlevitsk@...hat.com>
To: kvm@...r.kernel.org
Cc: Dave Hansen <dave.hansen@...ux.intel.com>,
Sean Christopherson <seanjc@...gle.com>,
Borislav Petkov <bp@...en8.de>,
Thomas Gleixner <tglx@...utronix.de>,
x86@...nel.org,
linux-kernel@...r.kernel.org,
Ingo Molnar <mingo@...hat.com>,
Paolo Bonzini <pbonzini@...hat.com>,
"H. Peter Anvin" <hpa@...or.com>,
Maxim Levitsky <mlevitsk@...hat.com>
Subject: [PATCH 1/2] KVM: x86: relax canonical checks for some x86 architectural msrs
Several architectural msrs (e.g MSR_KERNEL_GS_BASE) must contain
a canonical address, and according to Intel PRM, this is enforced
by #GP on a MSR write.
However with the introduction of the LA57 the definition of
what is a canonical address became blurred.
Few tests done on Sapphire Rapids CPU and on Zen4 CPU,
reveal:
1. These CPUs do allow full 57-bit wide non canonical values
to be written to MSR_GS_BASE, MSR_FS_BASE, MSR_KERNEL_GS_BASE,
regardless of the state of CR4.LA57.
Zen4 in addition to that even allows such writes to
MSR_CSTAR and MSR_LSTAR.
2. These CPUs don't prevent the user from switching back to 4 level
paging with values that will be non canonical in 4 level paging,
and instead just allow the msrs to contain these values.
Since these MSRS are all passed through to the guest, and microcode
allows the non canonical values to get into these msrs,
KVM has to tolerate such values and avoid crashing the guest.
To do so, always allow the host initiated values regardless of
the state of CR4.LA57, instead only gate this by the actual hardware
support for 5 level paging.
To be on the safe side leave the check for guest writes as is.
Signed-off-by: Maxim Levitsky <mlevitsk@...hat.com>
---
arch/x86/kvm/x86.c | 31 ++++++++++++++++++++++++++++++-
1 file changed, 30 insertions(+), 1 deletion(-)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index a6968eadd418..c599deff916e 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -1844,7 +1844,36 @@ static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,
case MSR_KERNEL_GS_BASE:
case MSR_CSTAR:
case MSR_LSTAR:
- if (is_noncanonical_address(data, vcpu))
+
+ /*
+ * Both AMD and Intel cpus tend to allow values which
+ * are canonical in the 5 level paging mode but are not
+ * canonical in the 4 level paging mode to be written
+ * to the above msrs, regardless of the state of the CR4.LA57.
+ *
+ * Intel CPUs do honour CR4.LA57 for the MSR_CSTAR/MSR_LSTAR,
+ * AMD cpus don't even do that.
+ *
+ * Both CPUs also allow non canonical values to remain in
+ * these MSRs if the CPU was in 5 level paging mode and was
+ * switched back to 4 level paging, and tolerate these values
+ * both in native MSRs and in vmcs/vmcb fields.
+ *
+ * To avoid crashing a guest, which manages using one of the above
+ * tricks to get non canonical value to one of
+ * these MSRs, and later migrates, allow the host initiated
+ * writes regardless of the state of CR4.LA57.
+ *
+ * To be on the safe side, don't allow the guest initiated
+ * writes to bypass the canonical check (e.g be more strict
+ * than what the actual ucode usually does).
+ */
+
+ if (!host_initiated && is_noncanonical_address(data, vcpu))
+ return 1;
+
+ if (!__is_canonical_address(data,
+ boot_cpu_has(X86_FEATURE_LA57) ? 57 : 48))
return 1;
break;
case MSR_IA32_SYSENTER_EIP:
--
2.26.3
Powered by blists - more mailing lists