Fix task switches into/out of VM86
v4 cover Fix task switches into/out of VM86
Kevin Wolf (4):
KVM: x86 emulator: Fix task switch privilege checks
KVM: x86 emulator: VM86 segments must have DPL 3
KVM: SVM: Fix CPL updates
KVM: x86 emulator: Allow PM/VM86 switch during task switch
arch/x86/include/asm/kvm_emulate.h | 3 +- arch/x86/include/asm/kvm_host.h | 4 +- arch/x86/kvm/emulate.c | 75 ++++++++++++++++++++++++++++++++--- arch/x86/kvm/svm.c | 28 +++++++++++-- arch/x86/kvm/vmx.c | 8 ++- arch/x86/kvm/x86.c | 12 ++++- 6 files changed, 110 insertions(+), 20 deletions(-)
Signature · 1 line
1.7.6.5
PATCH v4 1/4 KVM: x86 emulator: Fix task switch privilege checks
Currently, all task switches check privileges against the DPL of the TSS. This is only correct for jmp/call to a TSS. If a task gate is used, the DPL of this take gate is used for the check instead. Exceptions, external interrupts and iret shouldn't perform any check.
--- arch/x86/include/asm/kvm_emulate.h | 2 +- arch/x86/include/asm/kvm_host.h | 4 +- arch/x86/kvm/emulate.c | 53 +++++++++++++++++++++++++++++++----- arch/x86/kvm/svm.c | 5 +++- arch/x86/kvm/vmx.c | 8 +++-- arch/x86/kvm/x86.c | 6 ++-- 6 files changed, 61 insertions(+), 17 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -1
@@ -372,7 +372,7 @@ bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt);#define EMULATION_INTERCEPTED 2int x86_emulate_insn (struct x86_emulate_ctxt *ctxt);int emulator_task_switch (struct x86_emulate_ctxt *ctxt, -u16 tss_selector,int reason, +u16 tss_selector,int idt_index,int reason,bool has_error_code,u32 error_code);int emulate_int_real (struct x86_emulate_ctxt *ctxt,int irq);#endif /* _ASM_X86_KVM_X86_EMULATE_H */
arch/x86/include/asm/kvm_host.h +2 -2
@@ -741,8 +741,8 @@ int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu);void kvm_get_segment (struct kvm_vcpu *vcpu,struct kvm_segment *var,int seg);int kvm_load_segment_descriptor (struct kvm_vcpu *vcpu,u16 selector,int seg); -int kvm_task_switch (struct kvm_vcpu *vcpu,u16 tss_selector,int reason, -bool has_error_code,u32 error_code); +int kvm_task_switch (struct kvm_vcpu *vcpu,u16 tss_selector,int idt_index, +int reason,bool has_error_code,u32 error_code);int kvm_set_cr0 (struct kvm_vcpu *vcpu,unsigned long cr0);int kvm_set_cr3 (struct kvm_vcpu *vcpu,unsigned long cr3);
arch/x86/kvm/emulate.c +46 -7
85 lines not on this page · raw message
arch/x86/kvm/svm.c +4 -1
@@ -2730,7 +2730,10 @@ static int task_switch_interception(struct vcpu_svm *svm) (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))skip_emulated_instruction (&svm->vcpu); -if (kvm_task_switch (&svm->vcpu, tss_selector, reason, +if (int_type !=SVM_EXITINTINFO_TYPE_SOFT ) + int_vec =-1 ; + +if (kvm_task_switch (&svm->vcpu, tss_selector, int_vec, reason, has_error_code, error_code) ==EMULATE_FAIL ) { svm->vcpu.run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; svm->vcpu.run->internal.suberror =KVM_INTERNAL_ERROR_EMULATION ;
arch/x86/kvm/vmx.c +5 -3
@@ -4672,9 +4672,10 @@ static int handle_task_switch(struct kvm_vcpu *vcpu)bool has_error_code = false;u32 error_code =0 ;u16 tss_selector; -int reason, type, idt_v; +int reason, type, idt_v, idt_index; idt_v = (vmx->idt_vectoring_info &VECTORING_INFO_VALID_MASK ); + idt_index = (vmx->idt_vectoring_info &VECTORING_INFO_VECTOR_MASK ); type = (vmx->idt_vectoring_info &VECTORING_INFO_TYPE_MASK ); exit_qualification =vmcs_readl (EXIT_QUALIFICATION );
@@ -4712,8 +4713,9 @@ static int handle_task_switch(struct kvm_vcpu *vcpu) type != INTR_TYPE_NMI_INTR))skip_emulated_instruction (vcpu); -if (kvm_task_switch (vcpu, tss_selector, reason, - has_error_code, error_code) ==EMULATE_FAIL ) { +if (kvm_task_switch (vcpu, tss_selector, + type ==INTR_TYPE_SOFT_INTR ? idt_index :-1 , reason, + has_error_code, error_code) ==EMULATE_FAIL ) { vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; vcpu->run->internal.suberror =KVM_INTERNAL_ERROR_EMULATION ; vcpu->run->internal.ndata =0 ;
arch/x86/kvm/x86.c +3 -3
@@ -5541,15 +5541,15 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,return 0 ; } -int kvm_task_switch (struct kvm_vcpu *vcpu,u16 tss_selector,int reason, -bool has_error_code,u32 error_code) +int kvm_task_switch (struct kvm_vcpu *vcpu,u16 tss_selector,int idt_index, +int reason,bool has_error_code,u32 error_code) {struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;int ret;init_emulate_ctxt (vcpu); - ret =emulator_task_switch (ctxt, tss_selector, reason, + ret =emulator_task_switch (ctxt, tss_selector, idt_index, reason, has_error_code, error_code);if (ret)
Signature · 1 line
1.7.6.5
PATCH v4 2/4 KVM: x86 emulator: VM86 segments must have DPL 3
In reply to Kevin WolfSetting the segment DPL to 0 for at least the VM86 code segment makes the VM entry fail on VMX.
--- arch/x86/kvm/emulate.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
arch/x86/kvm/emulate.c +2 -0
@@ -1243,6 +1243,8 @@ static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, seg_desc.type =3 ; seg_desc.p =1 ; seg_desc.s =1 ; +if (ctxt->mode ==X86EMUL_MODE_VM86 ) + seg_desc.dpl =3 ; goto load; }
Signature · 1 line
1.7.6.5
PATCH v4 3/4 KVM: SVM: Fix CPL updates
In reply to Kevin WolfKeep CPL at 0 in real mode and at 3 in VM86. In protected/long mode, use RPL rather than DPL of the code segment.
--- arch/x86/kvm/svm.c | 19 ++++++++++++++++--- 1 files changed, 16 insertions(+), 3 deletions(-)
arch/x86/kvm/svm.c +16 -3
@@ -1263,6 +1263,21 @@ static void svm_vcpu_put(struct kvm_vcpu *vcpu)wrmsrl (host_save_user_msrs[i], svm->host_user_msrs[i]); } +static void svm_update_cpl (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); +int cpl; + +if (!is_protmode (vcpu)) + cpl =0 ; +else if (svm->vmcb->save.rflags &X86_EFLAGS_VM ) + cpl =3 ; +else + cpl = svm->vmcb->save.cs.selector &0x3 ; + + svm->vmcb->save.cpl = cpl; +} +static unsigned long svm_get_rflags (struct kvm_vcpu *vcpu) {return to_svm (vcpu)->vmcb->save.rflags;
@@ -1538,9 +1553,7 @@ static void svm_set_segment(struct kvm_vcpu *vcpu, s->attrib |= (var->g &1 ) <<SVM_SELECTOR_G_SHIFT ; }if (seg ==VCPU_SREG_CS ) - svm->vmcb->save.cpl - = (svm->vmcb->save.cs.attrib - >>SVM_SELECTOR_DPL_SHIFT ) &3 ; +svm_update_cpl (vcpu);mark_dirty (svm->vmcb,VMCB_SEG ); }
Signature · 1 line
1.7.6.5
PATCH v4 4/4 KVM: x86 emulator: Allow PM/VM86 switch during task switch
In reply to Kevin WolfTask switches can switch between Protected Mode and VM86. The current mode must be updated during the task switch emulation so that the new segment selectors are interpreted correctly.
In order to let privilege checks succeed, rflags needs to be updated in the vcpu struct as this causes a CPL update.
--- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/kvm/emulate.c | 20 ++++++++++++++++++++ arch/x86/kvm/svm.c | 4 ++++ arch/x86/kvm/x86.c | 6 ++++++ 4 files changed, 31 insertions(+), 0 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -0
@@ -176,6 +176,7 @@ struct x86_emulate_ops {void (*set_idt)(struct x86_emulate_ctxt *ctxt,struct desc_ptr *dt);ulong (*get_cr)(struct x86_emulate_ctxt *ctxt,int cr);int (*set_cr)(struct x86_emulate_ctxt *ctxt,int cr,ulong val); +void (*set_rflags)(struct x86_emulate_ctxt *ctxt,ulong val);int (*cpl)(struct x86_emulate_ctxt *ctxt);int (*get_dr)(struct x86_emulate_ctxt *ctxt,int dr,ulong *dest);int (*set_dr)(struct x86_emulate_ctxt *ctxt,int dr,ulong value);
arch/x86/kvm/emulate.c +20 -0
@@ -2273,6 +2273,8 @@ static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,return emulate_gp (ctxt,0 ); ctxt->_eip = tss->eip; ctxt->eflags = tss->eflags |2 ; + +/* General purpose registers */ ctxt->regs[VCPU_REGS_RAX ] = tss->eax; ctxt->regs[VCPU_REGS_RCX ] = tss->ecx; ctxt->regs[VCPU_REGS_RDX ] = tss->edx;
@@ -2295,6 +2297,24 @@ static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,set_segment_selector (ctxt, tss->gs,VCPU_SREG_GS );/* +* If we're switching between Protected Mode and VM86, we need to make +* sure to update the mode before loading the segment descriptors so +* that the selectors are interpreted correctly. +* +* Need to get rflags to the vcpu struct immediately because it +* influences the CPL which is checked at least when loading the segment +* descriptors and when pushing an error code to the new kernel stack. +* +* TODO Introduce a separate ctxt->ops->set_cpl callback +*/ +if (ctxt->eflags &X86_EFLAGS_VM ) + ctxt->mode =X86EMUL_MODE_VM86 ; +else + ctxt->mode =X86EMUL_MODE_PROT32 ; + + ctxt->ops->set_rflags (ctxt, ctxt->eflags); + +/* * Now load segment descriptors. If fault happenes at this stage * it is handled in a context of new task */
arch/x86/kvm/svm.c +4 -0
@@ -1285,7 +1285,11 @@ static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)static void svm_set_rflags (struct kvm_vcpu *vcpu,unsigned long rflags) { +unsigned long old_rflags =to_svm (vcpu)->vmcb->save.rflags; +to_svm (vcpu)->vmcb->save.rflags = rflags; +if ((old_rflags ^ rflags) &X86_EFLAGS_VM ) +svm_update_cpl (vcpu); }static void svm_cache_reg (struct kvm_vcpu *vcpu,enum kvm_reg reg)
arch/x86/kvm/x86.c +6 -0
@@ -4040,6 +4040,11 @@ static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)return res; } +static void emulator_set_rflags (struct x86_emulate_ctxt *ctxt,ulong val) +{ +kvm_set_rflags (emul_to_vcpu (ctxt), val); +} +static int emulator_get_cpl (struct x86_emulate_ctxt *ctxt) {return kvm_x86_ops->get_cpl (emul_to_vcpu (ctxt));
@@ -4199,6 +4204,7 @@ static struct x86_emulate_ops emulate_ops = { .set_idt = emulator_set_idt, .get_cr = emulator_get_cr, .set_cr = emulator_set_cr, + .set_rflags = emulator_set_rflags, .cpl = emulator_get_cpl, .get_dr = emulator_get_dr, .set_dr = emulator_set_dr,
Signature · 1 line
1.7.6.5
Avi Kivity
AppliedQuoted 6 lines · Kevin Wolf
Kevin Wolf (4):
KVM: x86 emulator: Fix task switch privilege checks
KVM: x86 emulator: VM86 segments must have DPL 3
KVM: SVM: Fix CPL updates
KVM: x86 emulator: Allow PM/VM86 switch during task switch
Thanks, applied.
Note that patch 1 is racy, since it re-reads the IDT after a decision has been made to use a task gate. I'm assuming that SMP OSes that swap IDT entries are rare, though.
Signature · 1 line
error compiling committee.c: too many arguments to function