KVM: nSVM: Check MBZ bits in CR3 and CR4 on vmrun of nested guests
v3 cover KVM: nSVM: Check MBZ bits in CR3 and CR4 on vmrun of nested guests
v2 -> v3:
In patch# 1, the mask for guest CR4 reserved bits is now cached in 'struct kvm_vcpu_arch', instead of in a global variable.
[PATCH 1/3 v3] KVM: x86: Create mask for guest CR4 reserved bits in [PATCH 2/3 v3] KVM: nSVM: Check that MBZ bits in CR3 and CR4 are not set on [PATCH 3/3 v3] KVM: nSVM: Test that MBZ bits in CR3 and CR4 are not set on vmrun
arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/cpuid.c | 2 ++ arch/x86/kvm/svm/nested.c | 22 ++++++++++++++++++++-- arch/x86/kvm/svm/svm.h | 5 ++++- arch/x86/kvm/x86.c | 27 ++++----------------------- arch/x86/kvm/x86.h | 21 +++++++++++++++++++++ 6 files changed, 53 insertions(+), 26 deletions(-)
Krish Sadhukhan (2):
KVM: x86: Create mask for guest CR4 reserved bits in kvm_update_cpuid()
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 g
PATCH v3 1/3 KVM: x86: Create mask for guest CR4 reserved bits in kvm_update_cpuid()
Instead of creating the mask for guest CR4 reserved bits in kvm_valid_cr4(), do it in kvm_update_cpuid() so that it can be reused instead of creating it each time kvm_valid_cr4() is called.
--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/cpuid.c | 2 ++ arch/x86/kvm/x86.c | 24 ++---------------------- arch/x86/kvm/x86.h | 21 +++++++++++++++++++++ 4 files changed, 27 insertions(+), 22 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -820,6 +820,8 @@ struct kvm_vcpu_arch {/* AMD MSRC001_0015 Hardware Configuration */ u64 msr_hwcr; + +u64 guest_cr4_reserved_bits; };struct kvm_lpage_info {
arch/x86/kvm/cpuid.c +2 -0
@@ -129,6 +129,8 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu)kvm_mmu_reset_context (vcpu);kvm_pmu_refresh (vcpu); + vcpu->arch.guest_cr4_reserved_bits = +__cr4_reserved_bits (guest_cpuid_has, vcpu);return 0 ; }
arch/x86/kvm/x86.c +2 -22
@@ -96,6 +96,7 @@#endif static u64 __read_mostly cr4_reserved_bits =CR4_RESERVED_BITS ; +u64 __guest_cr4_reserved_bits;#define VM_STAT (x, ...) offsetof(struct kvm, stat.x), KVM_STAT_VM, ## __VA_ARGS__#define VCPU_STAT (x, ...) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU, ## __VA_ARGS__
@@ -905,27 +906,6 @@ int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) }EXPORT_SYMBOL_GPL (kvm_set_xcr); -#define __cr4_reserved_bits (__cpu_has, __c) \ -({ \ - u64 __reserved_bits = CR4_RESERVED_BITS; \ - \ - if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \ - __reserved_bits |= X86_CR4_OSXSAVE; \ - if (!__cpu_has(__c, X86_FEATURE_SMEP)) \ - __reserved_bits |= X86_CR4_SMEP; \ - if (!__cpu_has(__c, X86_FEATURE_SMAP)) \ - __reserved_bits |= X86_CR4_SMAP; \ - if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \ - __reserved_bits |= X86_CR4_FSGSBASE; \ - if (!__cpu_has(__c, X86_FEATURE_PKU)) \ - __reserved_bits |= X86_CR4_PKE; \ - if (!__cpu_has(__c, X86_FEATURE_LA57)) \ - __reserved_bits |= X86_CR4_LA57; \ - if (!__cpu_has(__c, X86_FEATURE_UMIP)) \ - __reserved_bits |= X86_CR4_UMIP; \ - __reserved_bits; \ -}) -static u64 kvm_host_cr4_reserved_bits (struct cpuinfo_x86 *c) {u64 reserved_bits =__cr4_reserved_bits (cpu_has, c);
@@ -944,7 +924,7 @@ static int kvm_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)if (cr4 & cr4_reserved_bits)return -EINVAL ; -if (cr4 &__cr4_reserved_bits (guest_cpuid_has, vcpu)) +if (cr4 & vcpu->arch.guest_cr4_reserved_bits)return -EINVAL ;return 0 ;
arch/x86/kvm/x86.h +21 -0
@@ -359,4 +359,25 @@ static inline bool kvm_dr7_valid(u64 data)void kvm_load_host_xsave_state (struct kvm_vcpu *vcpu);u64 kvm_spec_ctrl_valid_bits (struct kvm_vcpu *vcpu); +#define __cr4_reserved_bits (__cpu_has, __c) \ +({ \ + u64 __reserved_bits = CR4_RESERVED_BITS; \ + \ + if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \ + __reserved_bits |= X86_CR4_OSXSAVE; \ + if (!__cpu_has(__c, X86_FEATURE_SMEP)) \ + __reserved_bits |= X86_CR4_SMEP; \ + if (!__cpu_has(__c, X86_FEATURE_SMAP)) \ + __reserved_bits |= X86_CR4_SMAP; \ + if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \ + __reserved_bits |= X86_CR4_FSGSBASE; \ + if (!__cpu_has(__c, X86_FEATURE_PKU)) \ + __reserved_bits |= X86_CR4_PKE; \ + if (!__cpu_has(__c, X86_FEATURE_LA57)) \ + __reserved_bits |= X86_CR4_LA57; \ + if (!__cpu_has(__c, X86_FEATURE_UMIP)) \ + __reserved_bits |= X86_CR4_UMIP; \ + __reserved_bits; \ +}) +#endif
Signature · 1 line
1.8.3.1
Re: [PATCH 1/3 v3] KVM: x86: Create mask for guest CR4 reserved bits in kvm_update_cpuid()
Quoted 18 lines · Krish Sadhukhan
Instead of creating the mask for guest CR4 reserved bits in
kvm_valid_cr4(), do it inkvm_update_cpuid()so that it can be reused instead of creating it each timekvm_valid_cr4()is called.Suggested-by Paolo Bonzini <[email protected]>Signed-off-by Krish Sadhukhan <[email protected]>--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/cpuid.c | 2 ++ arch/x86/kvm/x86.c | 24 ++---------------------- arch/x86/kvm/x86.h | 21 +++++++++++++++++++++ 4 files changed, 27 insertions(+), 22 deletions(-)arch/x86/include/asm/kvm_host.h +0
-0
arch/x86/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 824
Quoted 5 lines
arch/x86/include/asm/kvm_host.h +2
-0@@ -820,6 +820,8 @@ struct kvm_vcpu_arch {/* AMD MSRC001_0015 Hardware Configuration */ u64 msr_hwcr; + +u64 guest_cr4_reserved_bits;
This can be an 'unsigned long', the u64 type in the existing code is a goof on my part. I'd also vote to co-locate it with the other cr4 variables and abbreviate reserved to keep the name manageable, e.g.
arch/x86/include/asm/kvm_host.h +1 -0
@@ -573,6 +573,7 @@ struct kvm_vcpu_arch {unsigned long cr3;unsigned long cr4;unsigned long cr4_guest_owned_bits; +unsigned long cr4_guest_rsvd_bits;unsigned long cr8;u32 pkru;u32 hflags;
PATCH v3 2/3 KVM: nSVM: Check that MBZ bits in CR3 and CR4 are not set on vmrun of nested guests
In reply to Krish SadhukhanAccording 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 | 22 ++++++++++++++++++++-- arch/x86/kvm/svm/svm.h | 5 ++++- arch/x86/kvm/x86.c | 3 ++- 3 files changed, 26 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/nested.c +20 -2
@@ -202,11 +202,29 @@ static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)return true; } -static bool nested_vmcb_checks (struct vmcb *vmcb) +extern int kvm_valid_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4); + +static bool nested_vmcb_checks (struct vcpu_svm *svm,struct vmcb *vmcb) {if ((vmcb->save.efer &EFER_SVME ) ==0 )return false; +if (!is_long_mode (&(svm->vcpu))) { +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.cr3 &MSR_CR3_LONG_RESERVED_MASK )) +return false; + } +if (kvm_valid_cr4 (&(svm->vcpu), vmcb->save.cr4)) +return false; +if ((vmcb->control.intercept & (1ULL <<INTERCEPT_VMRUN )) ==0 )return false;
@@ -355,7 +373,7 @@ int nested_svm_vmrun(struct vcpu_svm *svm) nested_vmcb = map.hva; -if (!nested_vmcb_checks (nested_vmcb)) { +if (!nested_vmcb_checks (svm, nested_vmcb)) { nested_vmcb->control.exit_code =SVM_EXIT_ERR ; nested_vmcb->control.exit_code_hi =0 ; nested_vmcb->control.exit_info_1 =0 ;
arch/x86/kvm/svm/svm.h +4 -1
@@ -354,7 +354,10 @@ 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_INVALID 0xffffffffUu32 svm_msrpm_offset (u32 msr);void svm_set_efer (struct kvm_vcpu *vcpu,u64 efer);
arch/x86/kvm/x86.c +2 -1
@@ -919,7 +919,7 @@ static u64 kvm_host_cr4_reserved_bits(struct cpuinfo_x86 *c)return reserved_bits; } -static int kvm_valid_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4) +int kvm_valid_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4) {if (cr4 & cr4_reserved_bits)return -EINVAL ;
@@ -929,6 +929,7 @@ static int kvm_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)return 0 ; } +EXPORT_SYMBOL_GPL (kvm_valid_cr4);int kvm_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4) {
Signature · 1 line
1.8.3.1
PATCH v3 3/3 KVM: nSVM: Test that MBZ bits in CR3 and CR4 are not set on vmrun of nested guests
In reply to Krish SadhukhanAccording 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
@@ -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
Ping.
Quoted 28 lines · Krish Sadhukhan
v2 -> v3:
In patch# 1, the mask for guest CR4 reserved bits is now cached in 'structkvm_vcpu_arch', instead of in a global variable.[PATCH 1/3 v3] KVM: x86: Create mask for guest CR4 reserved bits in [PATCH 2/3 v3] KVM: nSVM: Check that MBZ bits in CR3 and CR4 are not set on [PATCH 3/3 v3] KVM: nSVM: Test that MBZ bits in CR3 and CR4 are not set on vmrun
arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/cpuid.c | 2 ++ arch/x86/kvm/svm/nested.c | 22 ++++++++++++++++++++-- arch/x86/kvm/svm/svm.h | 5 ++++- arch/x86/kvm/x86.c | 27 ++++----------------------- arch/x86/kvm/x86.h | 21 +++++++++++++++++++++ 6 files changed, 53 insertions(+), 26 deletions(-)Krish Sadhukhan (2):
KVM: x86: Create mask for guest CR4 reserved bits inkvm_update_cpuid()
nSVM: Check that MBZ bits in CR3 and CR4 are not set on vmrun of nested gux86/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 g
Quoted 38 lines · Krish Sadhukhan
Ping.
Quoted 34 lines · Krish Sadhukhan
v2 -> v3:
In patch# 1, the mask for guest CR4 reserved bits is now cached in 'structkvm_vcpu_arch', instead of in a global variable.[PATCH 1/3 v3] KVM: x86: Create mask for guest CR4 reserved bits in [PATCH 2/3 v3] KVM: nSVM: Check that MBZ bits in CR3 and CR4 are not set on
[PATCH 3/3 v3] KVM: nSVM: Test that MBZ bits in CR3 and CR4 are not set on vmrun
arch/x86/include/asm/kvm_host.h| 2 ++ arch/x86/kvm/cpuid.c | 2 ++ arch/x86/kvm/svm/nested.c | 22 ++++++++++++++++++++-- arch/x86/kvm/svm/svm.h | 5 ++++- arch/x86/kvm/x86.c | 27 ++++----------------------- arch/x86/kvm/x86.h | 21 +++++++++++++++++++++6 files changed, 53 insertions(+), 26 deletions(-)Krish Sadhukhan (2):
KVM: x86: Create mask for guest CR4 reserved bits inkvm_update_cpuid()
nSVM: Check that MBZ bits in CR3 and CR4 are not set on vmrun of nested gux86/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 g
Queued, thanks.
Paolo
Paolo Bonzini
maintainerQuoted 38 lines · Krish Sadhukhan
Ping.
Quoted 34 lines · Krish Sadhukhan
v2 -> v3:
In patch# 1, the mask for guest CR4 reserved bits is now cached in 'structkvm_vcpu_arch', instead of in a global variable.[PATCH 1/3 v3] KVM: x86: Create mask for guest CR4 reserved bits in [PATCH 2/3 v3] KVM: nSVM: Check that MBZ bits in CR3 and CR4 are not set on
[PATCH 3/3 v3] KVM: nSVM: Test that MBZ bits in CR3 and CR4 are not set on vmrun
arch/x86/include/asm/kvm_host.h| 2 ++ arch/x86/kvm/cpuid.c | 2 ++ arch/x86/kvm/svm/nested.c | 22 ++++++++++++++++++++-- arch/x86/kvm/svm/svm.h | 5 ++++- arch/x86/kvm/x86.c | 27 ++++----------------------- arch/x86/kvm/x86.h | 21 +++++++++++++++++++++6 files changed, 53 insertions(+), 26 deletions(-)Krish Sadhukhan (2):
KVM: x86: Create mask for guest CR4 reserved bits inkvm_update_cpuid()
nSVM: Check that MBZ bits in CR3 and CR4 are not set on vmrun of nested gux86/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 g
Sorry, this one was not queued because there were comments (also I think it doesn't apply anymore).
Paolo
Quoted 34 lines · Paolo Bonzini
Quoted 33 lines · Krish Sadhukhan
Ping.
Quoted 30 lines · Krish Sadhukhan
v2 -> v3:
In patch# 1, the mask for guest CR4 reserved bits is now cached in 'structkvm_vcpu_arch', instead of in a global variable.[PATCH 1/3 v3] KVM: x86: Create mask for guest CR4 reserved bits in [PATCH 2/3 v3] KVM: nSVM: Check that MBZ bits in CR3 and CR4 are not set on
[PATCH 3/3 v3] KVM: nSVM: Test that MBZ bits in CR3 and CR4 are not set on vmrunarch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/cpuid.c | 2 ++ arch/x86/kvm/svm/nested.c | 22 ++++++++++++++++++++-- arch/x86/kvm/svm/svm.h | 5 ++++- arch/x86/kvm/x86.c | 27 ++++----------------------- arch/x86/kvm/x86.h | 21 +++++++++++++++++++++ 6 files changed, 53 insertions(+), 26 deletions(-)Krish Sadhukhan (2):
KVM: x86: Create mask for guest CR4 reserved bits inkvm_update_cpuid()
nSVM: Check that MBZ bits in CR3 and CR4 are not set on vmrun
of nested gux86/svm.h | 6 ++++ x86/svm_tests.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 99 insertions(+), 12 deletions(-)
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Krish Sadhukhan (1):
nSVM: Test that MBZ bits in CR3 and CR4 are not set on vmrun of
nested gSorry, this one was not queued because there were comments (also I think it doesn't apply anymore).
Sorry, Paolo, I got bit lost here :-). IIRC, you had two comments on this set:
1. kvm_valid_cr4() should be used for checking the reserved bits in guest CR4
2. LMA shouldn't be checked via guest state
v3 has addressd your first suggestion by caching CR4 reserved bits in kvm_update_cpuid() and then using kvm_valid_cr4() in nested_vmcb_checks().
As for your second suggestion, v3 uses is_long_mode() which uses vcpu->arch.efer for checking long mode.
I will send out a rebased version.
Is there anything I missed ?
Thanks.
Quoted 3 lines
Paolo
Paolo Bonzini
maintainerQuoted 13 lines · Krish Sadhukhan
Quoted 1 line
Sorry, Paolo, I got bit lost here :-). IIRC, you had two comments on this set:
1.
kvm_valid_cr4()should be used for checking the reserved bits in guest CR42. LMA shouldn't be checked via guest state
v3 has addressd your first suggestion by caching CR4 reserved bits in
kvm_update_cpuid()and then usingkvm_valid_cr4()innested_vmcb_checks().
Quoted 6 lines
As for your second suggestion, v3 uses
is_long_mode()which usesvcpu->arch.eferfor checking long mode.I will send out a rebased version.
Is there anything I missed ?
Maybe Sean's comment here: https://patchwork.kernel.org/patch/11550707/
Paolo