KVM: nSVM: Add checks for CR3 and CR4 reserved bits to svm_set_nested_state() and test CR3 non-MBZ reserved bits
v3 cover KVM: nSVM: Add checks for CR3 and CR4 reserved bits to svm_set_nested_state() and test CR3 non-MBZ reserved bits
v2 -> v3:
Patch# 2: The local variable "nested_vmcb_lma" in
nested_vmcb_check_cr3_cr4() has been removed.
Patch# 3: Commit message has been enhanced to explain what the test
is doing and why, when testing the 1-setting of the non-MBZ-reserved bits.
Also, the test for legacy-PAE mode has been added. Commit header reflects this addition.
[PATCH 1/4 v3] KVM: nSVM: CR3 MBZ bits are only 63:52
[PATCH 2/4 v3] KVM: nSVM: Add check for reserved bits for CR3, CR4, DR6, [PATCH 3/4 v3] nSVM: Test non-MBZ reserved bits in CR3 in long mode and [PATCH 4/4 v3] KVM: nSVM: nested_vmcb_checks() needs to check all bits
arch/x86/kvm/svm/nested.c | 52 ++++++++++++++++++++++++++--------------------- arch/x86/kvm/svm/svm.h | 2 +- 2 files changed, 30 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 | 4 +++- x86/svm_tests.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 63 insertions(+), 7 deletions(-)
Krish Sadhukhan (1):
nSVM: Test non-MBZ reserved bits in CR3 in long mode and legacy PAE mode
PATCH v3 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
@@ -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 0xffffffffUu32 svm_msrpm_offset (u32 msr);
Signature · 1 line
2.18.4
PATCH v3 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 SadhukhanThe 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 | 49 +++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 22 deletions(-)
arch/x86/kvm/svm/nested.c +27 -22
@@ -215,9 +215,29 @@ 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) +{ +if ((save->efer &EFER_LME ) && (save->cr0 &X86_CR0_PG )) { +if (!(save->cr4 &X86_CR4_PAE ) || !(save->cr0 &X86_CR0_PE ) || + (save->cr3 &MSR_CR3_LONG_MBZ_MASK )) +return false; + }else 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; + } + +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 +248,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 +1118,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
PATCH v3 3/4 nSVM: Test non-MBZ reserved bits in CR3 in long mode and legacy PAE mode
In reply to Krish SadhukhanAccording 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."
But experiments show that consistency checking in SVM ignores these non-MBZ-reserved bits in CR3, meaning they can be set to 1 also. However, setting them to 1 may cause guest crashes in some modes and in bare metal environments. Hence, this test induces an #NPF by clearing the "P" bit in the highest level page table, when testing the 1-setting of these bits. Inducing an #NPF causes the guest to exit to userspace before any guest instruction is executed thus avoiding any crash.
--- x86/svm.h | 4 ++- x86/svm_tests.c | 66 ++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 63 insertions(+), 7 deletions(-)
x86/svm.h +3 -1
@@ -325,7 +325,9 @@ 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_CR3_PAE_LEGACY_RESERVED_MASK 0x0000000000000007U#define SVM_CR4_LEGACY_RESERVED_MASK 0xff88f000U#define SVM_CR4_RESERVED_MASK 0xffffffffff88f000U#define SVM_DR6_RESERVED_MASK 0xffffffffffff1ff0U
x86/svm_tests.c +60 -6
97 lines not on this page · raw message
Signature · 1 line
2.18.4
PATCH v3 4/4 KVM: nSVM: nested_vmcb_checks() needs to check all bits of EFER
In reply to Krish SadhukhanCurrent 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
@@ -238,7 +238,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
Paolo Bonzini
maintainerQuoted 24 lines · Krish Sadhukhan
v2 -> v3:
Patch# 2: The local variable "nested_vmcb_lma" in
nested_vmcb_check_cr3_cr4()has been removed.
Patch# 3: Commit message has been enhanced to explain what the test
is doing and why, when testing the 1-setting of the non-MBZ-reserved bits.
Also, the test for legacy-PAE mode has been added. Commit header reflects this addition.[PATCH 1/4 v3] KVM: nSVM: CR3 MBZ bits are only 63:52
[PATCH 2/4 v3] KVM: nSVM: Add check for reserved bits for CR3, CR4, DR6, [PATCH 3/4 v3] nSVM: Test non-MBZ reserved bits in CR3 in long mode and [PATCH 4/4 v3] KVM: nSVM:nested_vmcb_checks()needs to check all bitsarch/x86/kvm/svm/nested.c | 52 ++++++++++++++++++++++++++--------------------- arch/x86/kvm/svm/svm.h | 2 +- 2 files changed, 30 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 tosvm_set_nested_state()
KVM: nSVM:nested_vmcb_checks()needs to check all bits of EFER
Quoted 7 lines
x86/svm.h | 4 +++- x86/svm_tests.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 63 insertions(+), 7 deletions(-)Krish Sadhukhan (1):
nSVM: Test non-MBZ reserved bits in CR3 in long mode and legacy PAE mode
Queued, but I don't really like the duplication in patch 2 so I'll probably punt it to 5.11 and fix it up.
Paolo