Emulate VMXON region correctly
v2 cover Emulate VMXON region correctly
Reference: https://bugzilla.kernel.org/show_bug.cgi?id=54521
The vmxon region is unused by nvmx, but adding these checks are probably harmless and may detect buggy L1 hypervisors in the future!
v2:
1/4
- Commit message change to reflect addition of new function
2/4
- Use cpuid_maxphyaddr()
- Fix a leak with kunmap()
- Remove unnecessary braces around comparisions
- Move all checks into a common function, this will be later used by handle_vmptrld and handle_vmclear in 4/4
4/4
- New patch - use common function to perform checks on vmptr
Bandan Das (4):
KVM: nVMX: rearrange get_vmx_mem_address
KVM: nVMX: additional checks on vmxon region
KVM: nVMX: fail on invalid vmclear/vmptrld pointer
KVM: nVMX: move vmclear and vmptrld pre-checks to
nested_vmx_check_vmptr
arch/x86/kvm/cpuid.c | 1 + arch/x86/kvm/vmx.c | 240 +++++++++++++++++++++++++++++++++------------------ 2 files changed, 156 insertions(+), 85 deletions(-)
Signature · 1 line
1.8.3.1
PATCH v2 1/4 KVM: nVMX: rearrange get_vmx_mem_address
Our common function for vmptr checks (in 2/4) needs to fetch the memory address
--- arch/x86/kvm/vmx.c | 106 ++++++++++++++++++++++++++--------------------------- 1 file changed, 53 insertions(+), 53 deletions(-)
arch/x86/kvm/vmx.c +53 -53
@@ -5775,6 +5775,59 @@ static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) }/* +* Decode the memory-address operand of a vmx instruction, as recorded on an +* exit caused by such an instruction (run by a guest hypervisor). +* On success, returns 0. When the operand is invalid, returns 1 and throws +* #UD or #GP. +*/ +static int get_vmx_mem_address (struct kvm_vcpu *vcpu, +unsigned long exit_qualification, +u32 vmx_instruction_info,gva_t *ret) +{ +/* +* According to Vol. 3B, "Information for VM Exits Due to Instruction +* Execution", on an exit, vmx_instruction_info holds most of the +* addressing components of the operand. Only the displacement part +* is put in exit_qualification (see 3B, "Basic VM-Exit Information"). +* For how an actual address is calculated from all these components, +* refer to Vol. 1, "Operand Addressing". +*/ +int scaling = vmx_instruction_info &3 ; +int addr_size = (vmx_instruction_info >>7 ) &7 ; +bool is_reg = vmx_instruction_info & (1u <<10 ); +int seg_reg = (vmx_instruction_info >>15 ) &7 ; +int index_reg = (vmx_instruction_info >>18 ) &0xf ; +bool index_is_valid = !(vmx_instruction_info & (1u <<22 )); +int base_reg = (vmx_instruction_info >>23 ) &0xf ; +bool base_is_valid = !(vmx_instruction_info & (1u <<27 )); + +if (is_reg) { +kvm_queue_exception (vcpu,UD_VECTOR ); +return 1 ; + } + +/* Addr = segment_base + offset */ +/* offset = base + [index * scale] + displacement */ + *ret =vmx_get_segment_base (vcpu, seg_reg); +if (base_is_valid) + *ret +=kvm_register_read (vcpu, base_reg); +if (index_is_valid) + *ret +=kvm_register_read (vcpu, index_reg)<<scaling; + *ret += exit_qualification;/* holds the displacement */ + +if (addr_size ==1 )/* 32 bit */ + *ret &=0xffffffff ; + +/* +* TODO: throw #GP (and return 1) in various cases that the VM* +* instructions require it - e.g., offset beyond segment limit, +* unusable or unreadable/unwritable segment, non-canonical 64-bit +* address, and so on. Currently these are not checked. +*/ +return 0 ; +} + +/* * Emulate the VMXON instruction. * Currently, we just remember that VMX is active, and do not save or even * inspect the argument to VMXON (the so-called "VMXON pointer") because we
@@ -5934,59 +5987,6 @@ static int handle_vmoff(struct kvm_vcpu *vcpu)return 1 ; } -/* -* Decode the memory-address operand of a vmx instruction, as recorded on an -* exit caused by such an instruction (run by a guest hypervisor). -* On success, returns 0. When the operand is invalid, returns 1 and throws -* #UD or #GP. -*/ -static int get_vmx_mem_address (struct kvm_vcpu *vcpu, -unsigned long exit_qualification, -u32 vmx_instruction_info,gva_t *ret) -{ -/* -* According to Vol. 3B, "Information for VM Exits Due to Instruction -* Execution", on an exit, vmx_instruction_info holds most of the -* addressing components of the operand. Only the displacement part -* is put in exit_qualification (see 3B, "Basic VM-Exit Information"). -* For how an actual address is calculated from all these components, -* refer to Vol. 1, "Operand Addressing". -*/ -int scaling = vmx_instruction_info &3 ; -int addr_size = (vmx_instruction_info >>7 ) &7 ; -bool is_reg = vmx_instruction_info & (1u <<10 ); -int seg_reg = (vmx_instruction_info >>15 ) &7 ; -int index_reg = (vmx_instruction_info >>18 ) &0xf ; -bool index_is_valid = !(vmx_instruction_info & (1u <<22 )); -int base_reg = (vmx_instruction_info >>23 ) &0xf ; -bool base_is_valid = !(vmx_instruction_info & (1u <<27 )); - -if (is_reg) { -kvm_queue_exception (vcpu,UD_VECTOR ); -return 1 ; - } - -/* Addr = segment_base + offset */ -/* offset = base + [index * scale] + displacement */ - *ret =vmx_get_segment_base (vcpu, seg_reg); -if (base_is_valid) - *ret +=kvm_register_read (vcpu, base_reg); -if (index_is_valid) - *ret +=kvm_register_read (vcpu, index_reg)<<scaling; - *ret += exit_qualification;/* holds the displacement */ - -if (addr_size ==1 )/* 32 bit */ - *ret &=0xffffffff ; - -/* -* TODO: throw #GP (and return 1) in various cases that the VM* -* instructions require it - e.g., offset beyond segment limit, -* unusable or unreadable/unwritable segment, non-canonical 64-bit -* address, and so on. Currently these are not checked. -*/ -return 0 ; -} -/* Emulate the VMCLEAR instruction */ static int handle_vmclear (struct kvm_vcpu *vcpu) {
Signature · 1 line
1.8.3.1
PATCH v2 2/4 KVM: nVMX: additional checks on vmxon region
In reply to Bandan DasCurrently, the vmxon region isn't used in the nested case. However, according to the spec, the vmxon instruction performs additional sanity checks on this region and the associated pointer. Modify emulated vmxon to better adhere to the spec requirements
--- arch/x86/kvm/cpuid.c | 1 + arch/x86/kvm/vmx.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+)
arch/x86/kvm/cpuid.c +1 -0
@@ -726,6 +726,7 @@ int cpuid_maxphyaddr(struct kvm_vcpu *vcpu) not_found:return 36 ; } +EXPORT_SYMBOL_GPL (cpuid_maxphyaddr);/* * If no match is found, check whether we exceed the vCPU's limit
arch/x86/kvm/vmx.c +67 -0
85 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH v2 3/4 KVM: nVMX: fail on invalid vmclear/vmptrld pointer
In reply to Bandan DasThe spec mandates that if the vmptrld or vmclear address is equal to the vmxon region pointer, the instruction should fail with error "VMPTRLD with VMXON pointer" or "VMCLEAR with VMXON pointer"
--- arch/x86/kvm/vmx.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
arch/x86/kvm/vmx.c +12 -0
@@ -6083,6 +6083,12 @@ static int handle_vmclear(struct kvm_vcpu *vcpu)return 1 ; } +if (vmptr == vmx->nested.vmxon_ptr) { +nested_vmx_failValid (vcpu,VMXERR_VMCLEAR_VMXON_POINTER ); +skip_emulated_instruction (vcpu); +return 1 ; + } +if (vmptr == vmx->nested.current_vmptr) {nested_release_vmcs12 (vmx); vmx->nested.current_vmptr =-1ull ;
@@ -6426,6 +6432,12 @@ static int handle_vmptrld(struct kvm_vcpu *vcpu)return 1 ; } +if (vmptr == vmx->nested.vmxon_ptr) { +nested_vmx_failValid (vcpu,VMXERR_VMCLEAR_VMXON_POINTER ); +skip_emulated_instruction (vcpu); +return 1 ; + } +if (vmx->nested.current_vmptr != vmptr) {struct vmcs12 *new_vmcs12;struct page *page;
Signature · 1 line
1.8.3.1
PATCH v2 4/4 KVM: nVMX: move vmclear and vmptrld pre-checks to nested_vmx_check_vmptr
In reply to Bandan DasSome checks are common to all, and moreover, according to the spec, the check for whether any bits beyond the physical address width are set are also applicable to all of them
--- arch/x86/kvm/vmx.c | 83 ++++++++++++++++++++++++------------------------------ 1 file changed, 37 insertions(+), 46 deletions(-)
arch/x86/kvm/vmx.c +37 -46
@@ -5833,8 +5833,10 @@ static int get_vmx_mem_address(struct kvm_vcpu *vcpu,* - if it's 4KB aligned * - No bits beyond the physical address width are set * - Returns 0 on success or else 1 +* (Intel SDM Section 30.3) */ -static int nested_vmx_check_vmptr (struct kvm_vcpu *vcpu,int exit_reason) +static int nested_vmx_check_vmptr (struct kvm_vcpu *vcpu,int exit_reason, +gpa_t *vmpointer) {gva_t gva;gpa_t vmptr;
@@ -5882,11 +5884,42 @@ static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason)kunmap (page); vmx->nested.vmxon_ptr = vmptr;break ; +case EXIT_REASON_VMCLEAR : +if (!IS_ALIGNED (vmptr,PAGE_SIZE ) || (vmptr >> maxphyaddr)) { +nested_vmx_failValid (vcpu, +VMXERR_VMCLEAR_INVALID_ADDRESS ); +skip_emulated_instruction (vcpu); +return 1 ; + } +if (vmptr == vmx->nested.vmxon_ptr) { +nested_vmx_failValid (vcpu, +VMXERR_VMCLEAR_VMXON_POINTER ); +skip_emulated_instruction (vcpu); +return 1 ; + } +break ; +case EXIT_REASON_VMPTRLD : +if (!IS_ALIGNED (vmptr,PAGE_SIZE ) || (vmptr >> maxphyaddr)) { +nested_vmx_failValid (vcpu, +VMXERR_VMPTRLD_INVALID_ADDRESS ); +skip_emulated_instruction (vcpu); +return 1 ; + } + +if (vmptr == vmx->nested.vmxon_ptr) { +nested_vmx_failValid (vcpu, +VMXERR_VMCLEAR_VMXON_POINTER ); +skip_emulated_instruction (vcpu); +return 1 ; + } +break ;default :return 1 ;/* shouldn't happen */ } +if (vmpointer) + *vmpointer = vmptr;return 0 ; }
@@ -5929,7 +5962,7 @@ static int handle_vmon(struct kvm_vcpu *vcpu)return 1 ; } -if (nested_vmx_check_vmptr (vcpu,EXIT_REASON_VMON )) +if (nested_vmx_check_vmptr (vcpu,EXIT_REASON_VMON ,NULL ))return 1 ;if (vmx->nested.vmxon) {
@@ -6058,37 +6091,16 @@ static int handle_vmoff(struct kvm_vcpu *vcpu)static int handle_vmclear (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu); -gva_t gva;gpa_t vmptr;struct vmcs12 *vmcs12;struct page *page; -struct x86_exception e;if (!nested_vmx_check_permission (vcpu))return 1 ; -if (get_vmx_mem_address (vcpu,vmcs_readl (EXIT_QUALIFICATION ), -vmcs_read32 (VMX_INSTRUCTION_INFO ), &gva)) +if (nested_vmx_check_vmptr (vcpu,EXIT_REASON_VMCLEAR , &vmptr))return 1 ; -if (kvm_read_guest_virt (&vcpu->arch.emulate_ctxt, gva, &vmptr, -sizeof (vmptr), &e)) { -kvm_inject_page_fault (vcpu, &e); -return 1 ; - } - -if (!IS_ALIGNED (vmptr,PAGE_SIZE )) { -nested_vmx_failValid (vcpu,VMXERR_VMCLEAR_INVALID_ADDRESS ); -skip_emulated_instruction (vcpu); -return 1 ; - } - -if (vmptr == vmx->nested.vmxon_ptr) { -nested_vmx_failValid (vcpu,VMXERR_VMCLEAR_VMXON_POINTER ); -skip_emulated_instruction (vcpu); -return 1 ; - } -if (vmptr == vmx->nested.current_vmptr) {nested_release_vmcs12 (vmx); vmx->nested.current_vmptr =-1ull ;
@@ -6408,35 +6420,14 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu)static int handle_vmptrld (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu); -gva_t gva;gpa_t vmptr; -struct x86_exception e;u32 exec_control;if (!nested_vmx_check_permission (vcpu))return 1 ; -if (get_vmx_mem_address (vcpu,vmcs_readl (EXIT_QUALIFICATION ), -vmcs_read32 (VMX_INSTRUCTION_INFO ), &gva)) -return 1 ; - -if (kvm_read_guest_virt (&vcpu->arch.emulate_ctxt, gva, &vmptr, -sizeof (vmptr), &e)) { -kvm_inject_page_fault (vcpu, &e); -return 1 ; - } - -if (!IS_ALIGNED (vmptr,PAGE_SIZE )) { -nested_vmx_failValid (vcpu,VMXERR_VMPTRLD_INVALID_ADDRESS ); -skip_emulated_instruction (vcpu); +if (nested_vmx_check_vmptr (vcpu,EXIT_REASON_VMPTRLD , &vmptr))return 1 ; - } - -if (vmptr == vmx->nested.vmxon_ptr) { -nested_vmx_failValid (vcpu,VMXERR_VMCLEAR_VMXON_POINTER ); -skip_emulated_instruction (vcpu); -return 1 ; - }if (vmx->nested.current_vmptr != vmptr) {struct vmcs12 *new_vmcs12;
Signature · 1 line
1.8.3.1
Paolo Bonzini
maintainerIl 06/05/2014 08:19, Bandan Das ha scritto:
Quoted 5 lines
Reference: https://bugzilla.kernel.org/show_bug.cgi?id=54521
The vmxon region is unused by nvmx, but adding these checks are probably harmless and may detect buggy L1 hypervisors in the future!
Applied to kvm/queue, unit test patches are welcome (hint, hint!).
Paolo
Quoted 8 lines · Paolo Bonzini
Il 06/05/2014 08:19, Bandan Das ha scritto:
Quoted 5 lines
Reference: https://bugzilla.kernel.org/show_bug.cgi?id=54521
The vmxon region is unused by nvmx, but adding these checks are probably harmless and may detect buggy L1 hypervisors in the future!
Applied to kvm/queue, unit test patches are welcome (hint, hint!).
Jan and Paolo,
Ok, I thought a little bit about this and I am wondering what kind of tests we could have here. All the changes are pure checks and no data is returned back to the guest. The vmxon code in kvm-unit-tests already does the right thing i.e sets the revision identifier, allocs a page for the region etc
Quoted 1 line
Paolo
Quoted 11 lines · Bandan Das
Quoted 8 lines · Paolo Bonzini
Il 06/05/2014 08:19, Bandan Das ha scritto:
Quoted 5 lines
Reference: https://bugzilla.kernel.org/show_bug.cgi?id=54521
The vmxon region is unused by nvmx, but adding these checks are probably harmless and may detect buggy L1 hypervisors in the future!
Applied to kvm/queue, unit test patches are welcome (hint, hint!).
Quoted 7 lines
Jan and Paolo,
Ok, I thought a little bit about this and I am wondering what kind of tests we could have here. All the changes are pure checks and no data is returned back to the guest. The vmxon code in kvm-unit-tests already does the right thing i.e sets the revision identifier, allocs a page for the region etc
Then try to do the wrong thing in the unit test and validate that you get the right error report from the emulated VMX hardware.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE Corporate Competence Center Embedded Linux