KVM: nSVM: Check addresses of MSR bitmap and IO bitmap tables on vmrun of nested guests
v5 cover KVM: nSVM: Check addresses of MSR bitmap and IO bitmap tables on vmrun of nested guests
v4 -> v5:
1. In patch# 1, the actual size of the MSRPM and IOPM tables are now defined. The initialization code for the tables has been adjusted accordingly.
2. In patch# 2, the checks have been adjusted based on the actual size of the tables. The check for IOPM has also been fixed.
3. In patch# 4, a new test case has been added. This new test uses an address whose last byte touched the limit of the maximum physical address.
[PATCH 1/5 v5] KVM: SVM: Define actual size of IOPM and MSRPM tables [PATCH 2/5 v5] nSVM: Check addresses of MSR and IO permission maps [PATCH 3/5 v5] KVM: nSVM: Cleanup in nested_svm_vmrun() [PATCH 4/5 v5] nSVM: Test addresses of MSR and IO permissions maps [PATCH 5/5 v5] SVM: Use ALIGN macro when aligning 'io_bitmap_area'
arch/x86/kvm/svm/nested.c | 59 +++++++++++++++++++++++++++++------------------ arch/x86/kvm/svm/svm.c | 20 ++++++++-------- arch/x86/kvm/svm/svm.h | 3 +++ 3 files changed, 50 insertions(+), 32 deletions(-)
Krish Sadhukhan (3):
KVM: SVM: Define actual size of IOPM and MSRPM tables
nSVM: Check addresses of MSR and IO permission maps
KVM: nSVM: Cleanup in nested_svm_vmrun()
x86/svm.c | 2 +- x86/svm_tests.c | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-)
Krish Sadhukhan (2):
nSVM: Test addresses of MSR and IO permissions maps
SVM: Use ALIGN macro when aligning 'io_bitmap_area'
PATCH v5 1/5 KVM: SVM: Define actual size of IOPM and MSRPM tables
Define the actual size of the IOPM and MSRPM tables so that the actual size can be used when initializing them and when checking the consistency of the physical addresses. These #defines are placed in svm.h so that they can be shared.
--- arch/x86/kvm/svm/svm.c | 20 ++++++++++---------- arch/x86/kvm/svm/svm.h | 3 +++ 2 files changed, 13 insertions(+), 10 deletions(-)
arch/x86/kvm/svm/svm.c +10 -10
@@ -56,9 +56,6 @@ static const struct x86_cpu_id svm_cpu_id[] = {MODULE_DEVICE_TABLE (x86cpu, svm_cpu_id);#endif -#define IOPM_ALLOC_ORDER 2 -#define MSRPM_ALLOC_ORDER 1 -#define SEG_TYPE_LDT 2#define SEG_TYPE_BUSY_TSS16 3
@@ -681,14 +678,15 @@ void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr,u32 *svm_vcpu_alloc_msrpm (void ) { -struct page *pages =alloc_pages (GFP_KERNEL_ACCOUNT ,MSRPM_ALLOC_ORDER ); +unsigned int order =get_order (MSRPM_ALLOC_SIZE ); +struct page *pages =alloc_pages (GFP_KERNEL_ACCOUNT , order);u32 *msrpm;if (!pages)return NULL ; msrpm =page_address (pages); -memset (msrpm,0xff ,PAGE_SIZE * (1 <<MSRPM_ALLOC_ORDER )); +memset (msrpm,0xff ,PAGE_SIZE * (1 << order));return msrpm; }
@@ -707,7 +705,7 @@ void svm_vcpu_init_msrpm(struct kvm_vcpu *vcpu, u32 *msrpm)void svm_vcpu_free_msrpm (u32 *msrpm) { -__free_pages (virt_to_page (msrpm),MSRPM_ALLOC_ORDER ); +__free_pages (virt_to_page (msrpm),get_order (MSRPM_ALLOC_SIZE )); }static void svm_msr_filter_changed (struct kvm_vcpu *vcpu)
@@ -894,7 +892,8 @@ static void svm_hardware_teardown(void)for_each_possible_cpu (cpu)svm_cpu_uninit (cpu); -__free_pages (pfn_to_page (iopm_base >>PAGE_SHIFT ),IOPM_ALLOC_ORDER ); +__free_pages (pfn_to_page (iopm_base >>PAGE_SHIFT ), +get_order (IOPM_ALLOC_SIZE )); iopm_base =0 ; }
@@ -930,14 +929,15 @@ static __init int svm_hardware_setup(void)struct page *iopm_pages;void *iopm_va;int r; +unsigned int order =get_order (IOPM_ALLOC_SIZE ); - iopm_pages =alloc_pages (GFP_KERNEL ,IOPM_ALLOC_ORDER ); + iopm_pages =alloc_pages (GFP_KERNEL , order);if (!iopm_pages)return -ENOMEM ; iopm_va =page_address (iopm_pages); -memset (iopm_va,0xff ,PAGE_SIZE * (1 <<IOPM_ALLOC_ORDER )); +memset (iopm_va,0xff ,PAGE_SIZE * (1 << order)); iopm_base =page_to_pfn (iopm_pages) <<PAGE_SHIFT ;init_msrpm_offsets ();
@@ -1408,7 +1408,7 @@ static void svm_free_vcpu(struct kvm_vcpu *vcpu)sev_free_vcpu (vcpu);__free_page (pfn_to_page (__sme_clr (svm->vmcb_pa) >>PAGE_SHIFT )); -__free_pages (virt_to_page (svm->msrpm),MSRPM_ALLOC_ORDER ); +__free_pages (virt_to_page (svm->msrpm),get_order (MSRPM_ALLOC_SIZE )); }static void svm_prepare_guest_switch (struct kvm_vcpu *vcpu)
arch/x86/kvm/svm/svm.h +3 -0
@@ -28,6 +28,9 @@ static const u32 host_save_user_msrs[] = { };#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs) +#define IOPM_ALLOC_SIZE PAGE_SIZE * 3 +#define MSRPM_ALLOC_SIZE PAGE_SIZE * 2 +#define MAX_DIRECT_ACCESS_MSRS 18#define MSRPM_OFFSETS 16extern u32 msrpm_offsets[MSRPM_OFFSETS ]__read_mostly ;
Signature · 1 line
2.27.0
PATCH v5 2/5 nSVM: Check addresses of MSR and IO permission maps
In reply to Krish SadhukhanAccording to section "Canonicalization and Consistency Checks" in APM vol 2, the following guest state is illegal:
"The MSR or IOIO intercept tables extend to a physical address that
is greater than or equal to the maximum supported physical address."
--- arch/x86/kvm/svm/nested.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-)
arch/x86/kvm/svm/nested.c +21 -7
@@ -231,7 +231,15 @@ static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)return true; } -static bool nested_vmcb_check_controls (struct vmcb_control_area *control) +static bool nested_svm_check_bitmap_pa (struct kvm_vcpu *vcpu,u64 pa, +u32 size) +{ +u64 last_pa =PAGE_ALIGN (pa) + size -1 ; +return (kvm_vcpu_is_legal_gpa (vcpu, last_pa)); +} + +static bool nested_vmcb_check_controls (struct kvm_vcpu *vcpu, +struct vmcb_control_area *control) {if ((vmcb_is_intercept (control,INTERCEPT_VMRUN )) ==0 )return false;
@@ -243,12 +251,18 @@ static bool nested_vmcb_check_controls(struct vmcb_control_area *control) !npt_enabled)return false; +if (!nested_svm_check_bitmap_pa (vcpu, control->msrpm_base_pa, +MSRPM_ALLOC_SIZE )) +return false; +if (!nested_svm_check_bitmap_pa (vcpu, control->iopm_base_pa, +IOPM_ALLOC_SIZE -PAGE_SIZE +1 )) +return false; +return true; } -static bool nested_vmcb_checks (struct vcpu_svm *svm,struct vmcb *vmcb12) +static bool nested_vmcb_checks (struct kvm_vcpu *vcpu,struct vmcb *vmcb12) { -struct kvm_vcpu *vcpu = &svm->vcpu;bool vmcb12_lma;if ((vmcb12->save.efer &EFER_SVME ) ==0 )
@@ -268,10 +282,10 @@ static bool nested_vmcb_checks(struct vcpu_svm *svm, struct vmcb *vmcb12) kvm_vcpu_is_illegal_gpa(vcpu, vmcb12->save.cr3))return false; } -if (!kvm_is_valid_cr4 (&svm->vcpu, vmcb12->save.cr4)) +if (!kvm_is_valid_cr4 (vcpu, vmcb12->save.cr4))return false; -return nested_vmcb_check_controls (&vmcb12->control); +return nested_vmcb_check_controls (vcpu, &vmcb12->control); } static void load_nested_vmcb_control(struct vcpu_svm *svm,
@@ -515,7 +529,7 @@ int nested_svm_vmrun(struct vcpu_svm *svm)if (WARN_ON_ONCE (!svm->nested.initialized))return -EINVAL ; -if (!nested_vmcb_checks (svm, vmcb12)) { +if (!nested_vmcb_checks (&svm->vcpu, vmcb12)) { vmcb12->control.exit_code =SVM_EXIT_ERR ; vmcb12->control.exit_code_hi =0 ; vmcb12->control.exit_info_1 =0 ;
@@ -1191,7 +1205,7 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu, goto out_free; ret = -EINVAL ; -if (!nested_vmcb_check_controls (ctl)) +if (!nested_vmcb_check_controls (vcpu, ctl)) goto out_free;/*
Signature · 1 line
2.27.0
PATCH v5 3/5 KVM: nSVM: Cleanup in nested_svm_vmrun()
In reply to Krish SadhukhanUse local variables to derefence svm->vcpu and svm->vmcb as they make the code tidier.
--- arch/x86/kvm/svm/nested.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-)
arch/x86/kvm/svm/nested.c +17 -16
@@ -503,33 +503,34 @@ int nested_svm_vmrun(struct vcpu_svm *svm) {int ret;struct vmcb *vmcb12; +struct kvm_vcpu *vcpu = &svm->vcpu;struct vmcb *hsave = svm->nested.hsave;struct vmcb *vmcb = svm->vmcb;struct kvm_host_map map;u64 vmcb12_gpa; -if (is_smm (&svm->vcpu)) { -kvm_queue_exception (&svm->vcpu,UD_VECTOR ); +if (is_smm (vcpu)) { +kvm_queue_exception (vcpu,UD_VECTOR );return 1 ; } - vmcb12_gpa = svm->vmcb->save.rax; - ret =kvm_vcpu_map (&svm->vcpu,gpa_to_gfn (vmcb12_gpa), &map); + vmcb12_gpa = vmcb->save.rax; + ret =kvm_vcpu_map (vcpu,gpa_to_gfn (vmcb12_gpa), &map);if (ret == -EINVAL ) { -kvm_inject_gp (&svm->vcpu,0 ); +kvm_inject_gp (vcpu,0 );return 1 ; }else if (ret) { -return kvm_skip_emulated_instruction (&svm->vcpu); +return kvm_skip_emulated_instruction (vcpu); } - ret =kvm_skip_emulated_instruction (&svm->vcpu); + ret =kvm_skip_emulated_instruction (vcpu); vmcb12 = map.hva;if (WARN_ON_ONCE (!svm->nested.initialized))return -EINVAL ; -if (!nested_vmcb_checks (&svm->vcpu, vmcb12)) { +if (!nested_vmcb_checks (vcpu, vmcb12)) { vmcb12->control.exit_code =SVM_EXIT_ERR ; vmcb12->control.exit_code_hi =0 ; vmcb12->control.exit_info_1 =0 ;
@@ -539,8 +540,8 @@ int nested_svm_vmrun(struct vcpu_svm *svm)/* Clear internal status */ -kvm_clear_exception_queue (&svm->vcpu); -kvm_clear_interrupt_queue (&svm->vcpu); +kvm_clear_exception_queue (vcpu); +kvm_clear_interrupt_queue (vcpu);/* * Save the old vmcb, so we don't need to pick what we save, but can
@@ -552,17 +553,17 @@ int nested_svm_vmrun(struct vcpu_svm *svm) hsave->save.ds = vmcb->save.ds; hsave->save.gdtr = vmcb->save.gdtr; hsave->save.idtr = vmcb->save.idtr; - hsave->save.efer = svm->vcpu.arch.efer; - hsave->save.cr0 =kvm_read_cr0 (&svm->vcpu); + hsave->save.efer = vcpu->arch.efer; + hsave->save.cr0 =kvm_read_cr0 (vcpu); hsave->save.cr4 = svm->vcpu.arch.cr4; - hsave->save.rflags =kvm_get_rflags (&svm->vcpu); - hsave->save.rip =kvm_rip_read (&svm->vcpu); + hsave->save.rflags =kvm_get_rflags (vcpu); + hsave->save.rip =kvm_rip_read (vcpu); hsave->save.rsp = vmcb->save.rsp; hsave->save.rax = vmcb->save.rax;if (npt_enabled) hsave->save.cr3 = vmcb->save.cr3;else - hsave->save.cr3 =kvm_read_cr3 (&svm->vcpu); + hsave->save.cr3 =kvm_read_cr3 (vcpu);copy_vmcb_control_area (&hsave->control, &vmcb->control);
@@ -585,7 +586,7 @@ int nested_svm_vmrun(struct vcpu_svm *svm)nested_svm_vmexit (svm); out: -kvm_vcpu_unmap (&svm->vcpu, &map, true); +kvm_vcpu_unmap (vcpu, &map, true);return ret; }
Signature · 1 line
2.27.0
PATCH v5 4/5 nSVM: Test addresses of MSR and IO permissions maps
In reply to Krish SadhukhanAccording to section "Canonicalization and Consistency Checks" in APM vol 2, the following guest state is illegal:
"The MSR or IOIO intercept tables extend to a physical address that
is greater than or equal to the maximum supported physical address."
--- x86/svm_tests.c | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-)
x86/svm_tests.c +41 -1
@@ -2304,15 +2304,55 @@ static void test_dr(void) vmcb->save.dr7 = dr_saved; } +/* +* If the MSR or IOIO intercept table extends to a physical address that +* is greater than or equal to the maximum supported physical address, the +* guest state is illegal. +* +* [APM vol 2] +*/ +static void test_msrpm_iopm_bitmap_addrs (void ) +{ +u64 saved_intercepts = vmcb->control.intercept; +u64 bitmap_addr_1 = + (u64)(((u64 )1 <<cpuid_maxphyaddr ()) -PAGE_SIZE ); +u64 bitmap_addr_2 = + (u64)(((u64 )1 <<cpuid_maxphyaddr ()) -PAGE_SIZE *2 ); + +/* +* MSR bitmap address +*/ + vmcb->control.intercept = saved_intercepts |1ULL <<INTERCEPT_MSR_PROT ; + vmcb->control.msrpm_base_pa = bitmap_addr_1; +report (svm_vmrun () ==SVM_EXIT_ERR ,"Test MSRPM address: %lx" , + bitmap_addr_1); + vmcb->control.msrpm_base_pa = bitmap_addr_2; +report (svm_vmrun () ==SVM_EXIT_ERR ,"Test MSRPM address: %lx" , + bitmap_addr_2); + +/* +* IOIO bitmap address +*/ + vmcb->control.intercept = saved_intercepts |1ULL <<INTERCEPT_IOIO_PROT ; + vmcb->control.iopm_base_pa = bitmap_addr_1; +report (svm_vmrun () ==SVM_EXIT_ERR ,"Test IOPM address: %lx" , + bitmap_addr_1); + vmcb->control.iopm_base_pa = bitmap_addr_2 +=1 ; +report (svm_vmrun () ==SVM_EXIT_ERR ,"Test IOPM address: %lx" , + bitmap_addr_2); + + vmcb->control.intercept = saved_intercepts; +} +static void svm_guest_state_test (void ) {test_set_guest (basic_guest_main); -test_efer ();test_cr0 ();test_cr3 ();test_cr4 ();test_dr (); +test_msrpm_iopm_bitmap_addrs (); }
Signature · 1 line
2.27.0
PATCH v5 5/5 SVM: Use ALIGN macro when aligning 'io_bitmap_area'
In reply to Krish SadhukhanSince the macro is available and we already use it for MSR bitmap table, use it for aligning IO bitmap table also.
--- x86/svm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
x86/svm.c +1 -1
@@ -298,7 +298,7 @@ static void setup_svm(void)wrmsr (MSR_EFER ,rdmsr (MSR_EFER ) |EFER_SVME );wrmsr (MSR_EFER ,rdmsr (MSR_EFER ) |EFER_NX ); - io_bitmap = (void *) (((ulong )io_bitmap_area +4095 ) & ~4095 ); + io_bitmap = (void *)ALIGN ((ulong )io_bitmap_area,PAGE_SIZE ); msr_bitmap = (void *)ALIGN ((ulong )msr_bitmap_area,PAGE_SIZE );
Signature · 1 line
2.27.0
Sean Christopherson
maintainerQuoted 10 lines · Krish Sadhukhan
v4 -> v5:
1. In patch# 1, the actual size of the MSRPM and IOPM tables are now defined. The initialization code for the tables has been adjusted accordingly.
2. In patch# 2, the checks have been adjusted based on the actual size of the tables. The check for IOPM has also been fixed.
3. In patch# 4, a new test case has been added. This new test uses an address whose last byte touched the limit of the maximum physical address.
Quoted 3 lines
[PATCH 1/5 v5] KVM: SVM: Define actual size of IOPM and MSRPM tables [PATCH 2/5 v5] nSVM: Check addresses of MSR and IO permission maps [PATCH 3/5 v5] KVM: nSVM: Cleanup in
nested_svm_vmrun()
The kernel patches need to be rebased, their base is very stale and none of them apply on kvm/queue.