Skip to contentSkip to conversation

KVM: nSVM: Check MBZ bits in CR3 and CR4 on vmrun of nested guests

PATCH Superseded Krish Sadhukhan 2 participants 4 messages May 4, 2020 – May 7, 2020

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover KVM: nSVM: Check MBZ bits in CR3 and CR4 on vmrun of nested guests

Patch# 1: Adds the required KVM check.
Patch# 2: Adds the kvm-unit-test.

[PATCH 1/2] KVM: nSVM: Check that MBZ bits in CR3 and CR4 are not set on vmrun [PATCH 2/2] kvm-unit-tests: nSVM: Test that MBZ bits in CR3 and CR4 are not set on vmrun

 arch/x86/kvm/svm/nested.c | 18 ++++++++++++++++++
 arch/x86/kvm/svm/svm.h    |  7 ++++++-
 2 files changed, 24 insertions(+), 1 deletion(-)

Krish Sadhukhan (1):
nSVM: Check that MBZ bits in CR3 and CR4 are not set on vmrun of nested gu

 x86/svm.h       |   6 ++++
 x86/svm_tests.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 99 insertions(+), 12 deletions(-)

Krish Sadhukhan (1):
nSVM: Test that MBZ bits in CR3 and CR4 are not set on vmrun of nested gue

#1 lore raw

PATCH 1/2 KVM: nSVM: Check that MBZ bits in CR3 and CR4 are not set on vmrun of nested guests

According to section "Canonicalization and Consistency Checks" in APM vol. 2 the following guest state is illegal:

"Any MBZ bit of CR3 is set."
"Any MBZ bit of CR4 is set."

---
 arch/x86/kvm/svm/nested.c | 18 ++++++++++++++++++
 arch/x86/kvm/svm/svm.h    |  7 ++++++-
 2 files changed, 24 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/nested.c +18 -0
index 90a1ca9..1804a97 100644
@@ -207,6 +207,24 @@ static bool nested_vmcb_checks(struct vmcb *vmcb)
 	if ((vmcb->save.efer & EFER_SVME) == 0)
 		return false;
 
+	if (!(vmcb->save.efer & EFER_LMA)) {
+		if (vmcb->save.cr4 & X86_CR4_PAE) {
+			if (vmcb->save.cr3 & MSR_CR3_LEGACY_PAE_RESERVED_MASK)
+				return false;
+		} else {
+			if (vmcb->save.cr3 & MSR_CR3_LEGACY_RESERVED_MASK)
+				return false;
+		}
+		if (vmcb->save.cr4 & MSR_CR4_LEGACY_RESERVED_MASK)
+			return false;
+	} else {
+		if ((vmcb->save.cr4 & X86_CR4_PAE) &&
+		    (vmcb->save.cr3 & MSR_CR3_LONG_RESERVED_MASK))
+			return false;
+		if (vmcb->save.cr4 & MSR_CR4_RESERVED_MASK)
+			return false;
+	}
+
 	if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
 		return false;
 
arch/x86/kvm/svm/svm.h +6 -1
index df3474f..796c083 100644
@@ -354,7 +354,12 @@ static inline bool gif_set(struct vcpu_svm *svm)
 }
 
 /* svm.c */
-#define MSR_INVALID			0xffffffffU
+#define MSR_CR3_LEGACY_RESERVED_MASK		0xfe7U
+#define MSR_CR3_LEGACY_PAE_RESERVED_MASK	0x7U
+#define MSR_CR3_LONG_RESERVED_MASK		0xfff0000000000fe7U
+#define MSR_CR4_LEGACY_RESERVED_MASK		0xffbaf000U
+#define MSR_CR4_RESERVED_MASK			0xffffffffffbaf000U
+#define MSR_INVALID				0xffffffffU
 
 u32 svm_msrpm_offset(u32 msr);
 void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer);
Signature · 1 line
1.8.3.1
maintainer
#2 lore raw

Re: [PATCH 1/2] KVM: nSVM: Check that MBZ bits in CR3 and CR4 are not set on vmrun of nested guests

Quoted 11 lines · Krish Sadhukhan
arch/x86/kvm/svm/nested.c +10 -0
+	if (!(vmcb->save.efer & EFER_LMA)) {
+		if (vmcb->save.cr4 & X86_CR4_PAE) {
+			if (vmcb->save.cr3 & MSR_CR3_LEGACY_PAE_RESERVED_MASK)
+				return false;
+		} else {
+			if (vmcb->save.cr3 & MSR_CR3_LEGACY_RESERVED_MASK)
+				return false;
+		}
+		if (vmcb->save.cr4 & MSR_CR4_LEGACY_RESERVED_MASK)
+			return false;

arch/x86/kvm/svm/nested.c static bool nested_vmcb_checks(struct vmcb *vmcb) · line 229

Quoted 10 lines
arch/x86/kvm/svm/nested.c +8 -0
+	} else {
+		if ((vmcb->save.cr4 & X86_CR4_PAE) &&
+		    (vmcb->save.cr3 & MSR_CR3_LONG_RESERVED_MASK))
+			return false;
+		if (vmcb->save.cr4 & MSR_CR4_RESERVED_MASK)
+			return false;
+	}
+
 	if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
 		return false;

I think checking LMA from the guest state is incorrect, the number of bits in CR3 and CR4 remains 64 as long as the host processor is 64-bits.
This of course is unless you have reproduced on bare metal that a
hypervisor running in 32-bit mode ignores the top 32 bits.

Also, the checks for CR4 must use the guest's reserved bits, using kvm_valid_cr4. However this can be a bit slow so it is probably a good idea to cache the bits in kvm_update_cpuid.

Thanks,

Paolo

Quoted 16 lines
arch/x86/kvm/svm/svm.h +6 -1
index df3474f..796c083 100644
@@ -354,7 +354,12 @@ static inline bool gif_set(struct vcpu_svm *svm)
 }
 
 /* svm.c */
-#define MSR_INVALID			0xffffffffU
+#define MSR_CR3_LEGACY_RESERVED_MASK		0xfe7U
+#define MSR_CR3_LEGACY_PAE_RESERVED_MASK	0x7U
+#define MSR_CR3_LONG_RESERVED_MASK		0xfff0000000000fe7U
+#define MSR_CR4_LEGACY_RESERVED_MASK		0xffbaf000U
+#define MSR_CR4_RESERVED_MASK			0xffffffffffbaf000U
+#define MSR_INVALID				0xffffffffU
 
#3 lore raw

PATCH 2/2 kvm-unit-tests: nSVM: Test that MBZ bits in CR3 and CR4 are not set on vmrun of nested guests

In reply to Krish Sadhukhan

According to section "Canonicalization and Consistency Checks" in APM vol. 2, the following guest state is illegal:

"Any MBZ bit of CR3 is set."
"Any MBZ bit of CR4 is set."

---
 x86/svm.h       |   6 ++++
 x86/svm_tests.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 99 insertions(+), 12 deletions(-)
x86/svm.h +6 -0
index 645deb7..3d5b79c 100644
@@ -323,6 +323,12 @@ struct __attribute__ ((__packed__)) vmcb {
 #define SVM_EXIT_ERR		-1
 
 #define SVM_CR0_SELECTIVE_MASK (X86_CR0_TS | X86_CR0_MP)
+#define	SVM_CR0_RESERVED_MASK			0xffffffff00000000U
+#define	SVM_CR3_LEGACY_RESERVED_MASK		0xfe7U
+#define	SVM_CR3_LEGACY_PAE_RESERVED_MASK	0x7U
+#define	SVM_CR3_LONG_RESERVED_MASK		0xfff0000000000fe7U
+#define	SVM_CR4_LEGACY_RESERVED_MASK		0xffbaf000U
+#define	SVM_CR4_RESERVED_MASK			0xffffffffffbaf000U
 
 #define MSR_BITMAP_SIZE 8192
 
x86/svm_tests.c +93 -12

131 lines not on this page · raw message

Signature · 1 line
1.8.3.1