Skip to contentSkip to conversation

KVM: nSVM: Add checks for CR3 and CR4 reserved bits to svm_set_nested_state() and test CR3 non-MBZ reserved bits

PATCH Superseded Krish Sadhukhan 2 participants 9 messages Sep 28, 2020 – Oct 5, 2020

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover KVM: nSVM: Add checks for CR3 and CR4 reserved bits to svm_set_nested_state() and test CR3 non-MBZ reserved bits

v1 -> v2:
1. Patch# 2 has added checks for DR6, DR7 and EFER to SVM_SET_NESTED_STATE path.
2. Patch# 4 is a new addition. It has added missing checks for EFER to nested_vmcb_checks().

[PATCH 1/4 v2] KVM: nSVM: CR3 MBZ bits are only 63:52
[PATCH 2/4 v2] KVM: nSVM: Add check for reserved bits for CR3, CR4, DR6, [PATCH 3/4 v2] KVM: nSVM: Test non-MBZ reserved bits in CR3 in long mode [PATCH 4/4 v2] KVM: nSVM: nested_vmcb_checks() needs to check all bits of

 arch/x86/kvm/svm/nested.c | 58 ++++++++++++++++++++++++++++-------------------
 arch/x86/kvm/svm/svm.h    |  2 +-
 2 files changed, 36 insertions(+), 24 deletions(-)

Krish Sadhukhan (3):
KVM: nSVM: CR3 MBZ bits are only 63:52
KVM: nSVM: Add check for reserved bits for CR3, CR4, DR6, DR7 and EFER to
svm_set_nested_state()
KVM: nSVM: nested_vmcb_checks() needs to check all bits of EFER

 x86/svm.h       |  3 ++-
 x86/svm_tests.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 48 insertions(+), 7 deletions(-)

Krish Sadhukhan (1):
KVM: nSVM: Test non-MBZ reserved bits in CR3 in long mode

#1 lore raw

PATCH v2 1/4 KVM: nSVM: CR3 MBZ bits are only 63:52

Commit 761e4169346553c180bbd4a383aedd72f905bc9a created a wrong mask for the CR3 MBZ bits. According to APM vol 2, only the upper 12 bits are MBZ.

(Fixes 761e4169346553c180bbd4a383aedd72f905bc9a)

---
 arch/x86/kvm/svm/svm.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/svm/svm.h +1 -1
index a798e1731709..c0d75b1e0664 100644
@@ -345,7 +345,7 @@ static inline bool gif_set(struct vcpu_svm *svm)
 /* svm.c */
 #define MSR_CR3_LEGACY_RESERVED_MASK		0xfe7U
 #define MSR_CR3_LEGACY_PAE_RESERVED_MASK	0x7U
-#define MSR_CR3_LONG_RESERVED_MASK		0xfff0000000000fe7U
+#define MSR_CR3_LONG_MBZ_MASK			0xfff0000000000000U
 #define MSR_INVALID				0xffffffffU
 
 u32 svm_msrpm_offset(u32 msr);
Signature · 1 line
2.18.4
#2 lore raw

PATCH v2 2/4 KVM: nSVM: Add check for reserved bits for CR3, CR4, DR6, DR7 and EFER to svm_set_nested_state()

In reply to Krish Sadhukhan

The path for SVM_SET_NESTED_STATE needs to have the same checks for the CPU registers, as we have in the VMRUN path for a nested guest. This patch adds those missing checks to svm_set_nested_state().

---
 arch/x86/kvm/svm/nested.c | 55 +++++++++++++++++++++++----------------
 1 file changed, 33 insertions(+), 22 deletions(-)
arch/x86/kvm/svm/nested.c +33 -22
index e90bc436f584..55c785587bd4 100644
@@ -215,9 +215,35 @@ static bool nested_vmcb_check_controls(struct vmcb_control_area *control)
 	return true;
 }
 
+static bool nested_vmcb_check_cr3_cr4(struct vcpu_svm *svm,
+				      struct vmcb_save_area *save)
+{
+	bool nested_vmcb_lma =
+	        (save->efer & EFER_LME) &&
+		(save->cr0 & X86_CR0_PG);
+
+	if (!nested_vmcb_lma) {
+		if (save->cr4 & X86_CR4_PAE) {
+			if (save->cr3 & MSR_CR3_LEGACY_PAE_RESERVED_MASK)
+				return false;
+		} else {
+			if (save->cr3 & MSR_CR3_LEGACY_RESERVED_MASK)
+				return false;
+		}
+	} else {
+		if (!(save->cr4 & X86_CR4_PAE) ||
+		    !(save->cr0 & X86_CR0_PE) ||
+		    (save->cr3 & MSR_CR3_LONG_MBZ_MASK))
+			return false;
+	}
+	if (kvm_valid_cr4(&svm->vcpu, save->cr4))
+		return false;
+
+	return true;
+}
+
 static bool nested_vmcb_checks(struct vcpu_svm *svm, struct vmcb *vmcb)
 {
-	bool nested_vmcb_lma;
 	if ((vmcb->save.efer & EFER_SVME) == 0)
 		return false;
 
@@ -228,25 +254,7 @@ static bool nested_vmcb_checks(struct vcpu_svm *svm, struct vmcb *vmcb)
 	if (!kvm_dr6_valid(vmcb->save.dr6) || !kvm_dr7_valid(vmcb->save.dr7))
 		return false;
 
-	nested_vmcb_lma =
-	        (vmcb->save.efer & EFER_LME) &&
-		(vmcb->save.cr0 & X86_CR0_PG);
-
-	if (!nested_vmcb_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;
-		}
-	} else {
-		if (!(vmcb->save.cr4 & X86_CR4_PAE) ||
-		    !(vmcb->save.cr0 & X86_CR0_PE) ||
-		    (vmcb->save.cr3 & MSR_CR3_LONG_RESERVED_MASK))
-			return false;
-	}
-	if (kvm_valid_cr4(&svm->vcpu, vmcb->save.cr4))
+	if (!nested_vmcb_check_cr3_cr4(svm, &(vmcb->save)))
 		return false;
 
 	return nested_vmcb_check_controls(&vmcb->control);
@@ -1116,9 +1124,12 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
 	/*
 	 * Validate host state saved from before VMRUN (see
 	 * nested_svm_check_permissions).
-	 * TODO: validate reserved bits for all saved state.
 	 */
-	if (!(save.cr0 & X86_CR0_PG))
+	if (!(save.cr0 & X86_CR0_PG) ||
+	    !nested_vmcb_check_cr3_cr4(svm, &save) ||
+	    !kvm_dr6_valid(save.dr6) ||
+	    !kvm_dr7_valid(save.dr7) ||
+	    !kvm_valid_efer(vcpu, save.efer))
 		return -EINVAL;
 
 	/*
Signature · 1 line
2.18.4
#3 lore raw

PATCH v2 3/4 KVM: nSVM: Test non-MBZ reserved bits in CR3 in long mode

In reply to Krish Sadhukhan

According to section "CR3" in APM vol. 2, the non-MBZ reserved bits in CR3 need to be set by software as follows:

"Reserved Bits. Reserved fields should be cleared to 0 by software when writing CR3."

---
 x86/svm.h       |  3 ++-
 x86/svm_tests.c | 52 +++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 48 insertions(+), 7 deletions(-)
x86/svm.h +2 -1
index 15e0f18..465d794 100644
@@ -325,7 +325,8 @@ struct __attribute__ ((__packed__)) vmcb {
 #define SVM_CR0_SELECTIVE_MASK (X86_CR0_TS | X86_CR0_MP)
 
 #define	SVM_CR0_RESERVED_MASK			0xffffffff00000000U
-#define	SVM_CR3_LONG_RESERVED_MASK		0xfff0000000000000U
+#define	SVM_CR3_LONG_MBZ_MASK			0xfff0000000000000U
+#define	SVM_CR3_LONG_RESERVED_MASK		0x0000000000000fe7U
 #define	SVM_CR4_LEGACY_RESERVED_MASK		0xff88f000U
 #define	SVM_CR4_RESERVED_MASK			0xffffffffff88f000U
 #define	SVM_DR6_RESERVED_MASK			0xffffffffffff1ff0U
x86/svm_tests.c +46 -6

83 lines not on this page · raw message

Signature · 1 line
2.18.4
#4 lore raw

Re: [PATCH 3/4 v2] KVM: nSVM: Test non-MBZ reserved bits in CR3 in long mode

Quoted 5 lines · Krish Sadhukhan

According to section "CR3" in APM vol. 2, the non-MBZ reserved bits in CR3 need to be set by software as follows:

"Reserved Bits. Reserved fields should be cleared to 0 by software when writing CR3."

Nothing in the shortlog or changelog actually states what this patch does. "Test non-MBZ reserved bits in CR3 in long mode" is rather ambiguous, and IIUC, the changelog is straight up misleading.

Based on the discussion from v1, I _think_ this test verifies that KVM does _not_ fail nested VMRUN if non-MBZ bits are set, correct?

If so, then something like:

KVM: nSVM: Verify non-MBZ CR3 reserved bits can be set in long mode

with further explanation in the changelog would be very helpful.

#5 lore raw

Re: [PATCH 3/4 v2] KVM: nSVM: Test non-MBZ reserved bits in CR3 in long mode

Quoted 4 lines · Sean Christopherson
Quoted 3 lines · Krish Sadhukhan

According to section "CR3" in APM vol. 2, the non-MBZ reserved bits in CR3 need to be set by software as follows:

Quoted 8 lines
Quoted 2 lines

"Reserved Bits. Reserved fields should be cleared to 0 by software when writing CR3."

Nothing in the shortlog or changelog actually states what this patch does. "Test non-MBZ reserved bits in CR3 in long mode" is rather ambiguous, and IIUC, the changelog is straight up misleading.

Based on the discussion from v1, I _think_ this test verifies that KVM does _not_ fail nested VMRUN if non-MBZ bits are set, correct?

Not KVM, hardware rather.  Hardware doesn't consider it as an invalid guest state if non-MBZ reserved bits are set.

Quoted 6 lines

If so, then something like:

KVM: nSVM: Verify non-MBZ CR3 reserved bits can be set in long mode

with further explanation in the changelog would be very helpful.

Even though the non-MBZ reserved bits are ignored by the consistency checks in hardware, eventually page-table walks fail. So, I am wondering whether it is appropriate to say,

            "Verify non-MBZ CR3 reserved bits can be set in long mode"

because the test is inducing an artificial failure even before any guest instruction is executed. We are not entering the guest with these bits set.

I prefer to keep the commit header as is and rather expand the commit message to explain what I have described here. How about that ?

#6 lore raw

Re: [PATCH 3/4 v2] KVM: nSVM: Test non-MBZ reserved bits in CR3 in long mode

Quoted 18 lines · Krish Sadhukhan
Quoted 4 lines · Sean Christopherson
Quoted 3 lines · Krish Sadhukhan

According to section "CR3" in APM vol. 2, the non-MBZ reserved bits in CR3 need to be set by software as follows:

Quoted 8 lines
Quoted 2 lines

"Reserved Bits. Reserved fields should be cleared to 0 by software when writing CR3."

Nothing in the shortlog or changelog actually states what this patch does. "Test non-MBZ reserved bits in CR3 in long mode" is rather ambiguous, and IIUC, the changelog is straight up misleading.

Based on the discussion from v1, I _think_ this test verifies that KVM does _not_ fail nested VMRUN if non-MBZ bits are set, correct?

Not KVM, hardware rather.  Hardware doesn't consider it as an invalid guest state if non-MBZ reserved bits are set.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines

If so, then something like:

KVM: nSVM: Verify non-MBZ CR3 reserved bits can be set in long mode

with further explanation in the changelog would be very helpful.

Even though the non-MBZ reserved bits are ignored by the consistency checks in hardware, eventually page-table walks fail. So, I am wondering whether it

Page table walks fail how? Are you referring to forcing the #NPF, or does the CPU puke on the non-MBZ reserved bits at some point?

Quoted 6 lines

is appropriate to say,

            "Verify non-MBZ CR3 reserved bits can be set in long mode"

because the test is inducing an artificial failure even before any guest instruction is executed. We are not entering the guest with these bits set.

Yes we are, unless I'm misunderstanding how SVM handles VMRUN. "entering" the guest does not mean successfully executing guest code, it means loading guest state and completing the world switch. I don't think I'm misunderstanding, because the test explicitly clears the NPT PML4[0]'s present bit to induce a #NPF. That means the CPU is fetching instructions, and again unless there's details about NPT that I'm missing, the fact that the test sees a #NPF means that the CPU successfully completed the GVA->GPA translation using the "bad" CR3.

Quoted 2 lines

I prefer to keep the commit header as is and rather expand the commit message to explain what I have described here. How about that ?

That's fine, so long as it documents both what the test is actually verifying and what is/isn't legal.

#7 lore raw

Re: [PATCH 3/4 v2] KVM: nSVM: Test non-MBZ reserved bits in CR3 in long mode

Quoted 18 lines · Sean Christopherson
Quoted 17 lines · Krish Sadhukhan
Quoted 4 lines · Sean Christopherson
Quoted 3 lines · Krish Sadhukhan

According to section "CR3" in APM vol. 2, the non-MBZ reserved bits in CR3 need to be set by software as follows:

Quoted 8 lines
Quoted 2 lines

"Reserved Bits. Reserved fields should be cleared to 0 by software when writing CR3."

Nothing in the shortlog or changelog actually states what this patch does. "Test non-MBZ reserved bits in CR3 in long mode" is rather ambiguous, and IIUC, the changelog is straight up misleading.

Based on the discussion from v1, I _think_ this test verifies that KVM does _not_ fail nested VMRUN if non-MBZ bits are set, correct?

Not KVM, hardware rather.  Hardware doesn't consider it as an invalid guest state if non-MBZ reserved bits are set.

Quoted 2 lines

If so, then something like:

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

KVM: nSVM: Verify non-MBZ CR3 reserved bits can be set in long mode

with further explanation in the changelog would be very helpful.

Even though the non-MBZ reserved bits are ignored by the consistency checks in hardware, eventually page-table walks fail. So, I am wondering whether it

Page table walks fail how? Are you referring to forcing the #NPF, or does the CPU puke on the non-MBZ reserved bits at some point?

I notice the following in my experiments when I don't clear the P bit in NPT PML4[0] to induce an #NPF:

    1. In long mode and in legacy-PAE mode, guest VMMCALL is successfully executed and kvm_x86_ops.handle_exit() receives VMEXIT_VMMCALL.

    2. In legacy-non-PAE mode, kvm_x86_ops.handle_exit(), receives VMEXIT_NPF.

Quoted 7 lines
Quoted 7 lines

is appropriate to say,

            "Verify non-MBZ CR3 reserved bits can be set in long mode"

because the test is inducing an artificial failure even before any guest instruction is executed. We are not entering the guest with these bits set.

Quoted 8 lines

Yes we are, unless I'm misunderstanding how SVM handles VMRUN. "entering" the guest does not mean successfully executing guest code, it means loading guest state and completing the world switch. I don't think I'm misunderstanding, because the test explicitly clears the NPT PML4[0]'s present bit to induce a #NPF. That means the CPU is fetching instructions, and again unless there's details about NPT that I'm missing, the fact that the test sees a #NPF means that the CPU successfully completed the GVA->GPA translation using the "bad" CR3.

You are right. According to APM, the hardware loads the guest state first and then does the consistency checking. So, yes, world switch happens before consistency checking begins.

Quoted 5 lines
Quoted 3 lines

I prefer to keep the commit header as is and rather expand the commit message to explain what I have described here. How about that ?

That's fine, so long as it documents both what the test is actually verifying and what is/isn't legal.

#8 lore raw

PATCH v2 4/4 KVM: nSVM: nested_vmcb_checks() needs to check all bits of EFER

In reply to Krish Sadhukhan

Current implementation of nested_vmcb_checks() checks only the SVME bit in EFER. We need to check all other bits of EFER including the reserved bits. This patch enhances nested_vmcb_checks() by calling kvm_valid_efer() which checks all bits of EFER.

---
 arch/x86/kvm/svm/nested.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/nested.c +2 -1
index 55c785587bd4..cf4048401737 100644
@@ -244,7 +244,8 @@ static bool nested_vmcb_check_cr3_cr4(struct vcpu_svm *svm,
 
 static bool nested_vmcb_checks(struct vcpu_svm *svm, struct vmcb *vmcb)
 {
-	if ((vmcb->save.efer & EFER_SVME) == 0)
+	if (((vmcb->save.efer & EFER_SVME) == 0) ||
+	     !kvm_valid_efer(&(svm->vcpu), vmcb->save.efer))
 		return false;
 
 	if (((vmcb->save.cr0 & X86_CR0_CD) == 0) &&
Signature · 1 line
2.18.4