More emulator correctness fixes
cover More emulator correctness fixes
This patch series adds proper permission checking during segment selector loading. Some missing fault injections are added.
Gleb Natapov (2):
KVM: Forbid modifying CS segment register by mov instruction.
KVM: Fix segment descriptor loading.
Takuya Yoshikawa (2):
KVM: Fix load_guest_segment_descriptor() to inject page fault
KVM: Fix emulate_sys[call, enter, exit]()'s fault handling
arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/kvm/emulate.c | 71 +++++++-------- arch/x86/kvm/x86.c | 190 +++++++++++++++++++++++++++++++-------- 3 files changed, 186 insertions(+), 78 deletions(-)
PATCH 1/4 KVM: Forbid modifying CS segment register by mov instruction.
Inject #UD if guest attempts to do so. This is in accordance to Intel SDM.
--- arch/x86/kvm/emulate.c | 6 ++++++ 1 files changed, 6 insertions(+), 0 deletions(-)
arch/x86/kvm/emulate.c +6 -0
@@ -2122,6 +2122,12 @@ special_insn:int err; sel = c->src.val; + +if (c->modrm_reg ==VCPU_SREG_CS ) { +kvm_queue_exception (ctxt->vcpu,UD_VECTOR ); + goto done; + } +if (c->modrm_reg ==VCPU_SREG_SS )toggle_interruptibility (ctxt,X86_SHADOW_INT_MOV_SS );
Signature · 1 line
1.6.5
PATCH 2/4 KVM: Fix load_guest_segment_descriptor() to inject page fault
In reply to Gleb NatapovFrom: Takuya Yoshikawa <[email protected]>
This patch injects page fault when reading descriptor in load_guest_segment_descriptor() fails with FAULT.
Effects of this injection: This function is used by kvm_load_segment_descriptor() which is necessary for the following instructions.
- mov seg,r/m16
- jmp far
- pop ?s
This patch makes it possible to emulate the page faults generated by these instructions. But be sure that unless we change the kvm_load_segment_descriptor()'s ret value propagation this patch has no effect.
--- arch/x86/kvm/x86.c | 13 ++++++++++--- 1 files changed, 10 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +10 -3
@@ -4713,6 +4713,9 @@ static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, {struct desc_ptr dtable;u16 index = selector >>3 ; +int ret; +u32 err; +gva_t addr;get_segment_descriptor_dtable (vcpu, selector, &dtable);
@@ -4720,9 +4723,13 @@ static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,kvm_queue_exception_e (vcpu,GP_VECTOR , selector &0xfffc );return X86EMUL_PROPAGATE_FAULT ; } -return kvm_read_guest_virt_system (dtable.address + index*8 , - seg_desc,sizeof (*seg_desc), - vcpu,NULL ); + addr = dtable.address + index *8 ; + ret =kvm_read_guest_virt_system (addr, seg_desc,sizeof (*seg_desc), + vcpu, &err); +if (ret ==X86EMUL_PROPAGATE_FAULT ) +kvm_inject_page_fault (vcpu, addr, err); + +return ret; }/* allowed just for 8 bytes segments */
Signature · 1 line
1.6.5
PATCH 3/4 KVM: Fix segment descriptor loading.
In reply to Gleb NatapovAdd proper error and permission checking. This patch also change task switching code to load segment selectors before segment descriptors, like SDM requires, otherwise permission checking during segment descriptor loading will be incorrect.
--- arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/kvm/emulate.c | 30 ++----- arch/x86/kvm/x86.c | 177 +++++++++++++++++++++++++++++++-------- 3 files changed, 151 insertions(+), 59 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -2
@@ -609,8 +609,7 @@ int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value);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 type_bits,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);
arch/x86/kvm/emulate.c +8 -22
@@ -1304,7 +1304,7 @@ static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,if (rc !=X86EMUL_CONTINUE )return rc; - rc =kvm_load_segment_descriptor (ctxt->vcpu, (u16 )selector,1 , seg); + rc =kvm_load_segment_descriptor (ctxt->vcpu, (u16 )selector, seg);return rc; }
@@ -1482,7 +1482,7 @@ static int emulate_ret_far(struct x86_emulate_ctxt *ctxt, rc =emulate_pop (ctxt, ops, &cs, c->op_bytes);if (rc !=X86EMUL_CONTINUE )return rc; - rc =kvm_load_segment_descriptor (ctxt->vcpu, (u16 )cs,1 ,VCPU_SREG_CS ); + rc =kvm_load_segment_descriptor (ctxt->vcpu, (u16 )cs,VCPU_SREG_CS );return rc; }
@@ -2118,12 +2118,11 @@ special_insn:break ;case 0x8e : {/* mov seg, r/m16 */ uint16_t sel; -int type_bits; -int err; sel = c->src.val; -if (c->modrm_reg ==VCPU_SREG_CS ) { +if (c->modrm_reg ==VCPU_SREG_CS || + c->modrm_reg >VCPU_SREG_GS ) {kvm_queue_exception (ctxt->vcpu,UD_VECTOR ); goto done; }
@@ -2131,18 +2130,7 @@ special_insn:if (c->modrm_reg ==VCPU_SREG_SS )toggle_interruptibility (ctxt,X86_SHADOW_INT_MOV_SS ); -if (c->modrm_reg <=5 ) { - type_bits = (c->modrm_reg ==1 ) ?9 :1 ; - err =kvm_load_segment_descriptor (ctxt->vcpu, sel, - type_bits, c->modrm_reg); - }else { -printk (KERN_INFO "Invalid segreg in modrm byte 0x%02x\n" , - c->modrm); - goto cannot_emulate; - } - -if (err <0 ) - goto cannot_emulate; + rc =kvm_load_segment_descriptor (ctxt->vcpu, sel, c->modrm_reg); c->dst.type =OP_NONE ;/* Disable writeback. */ break ;
@@ -2316,11 +2304,9 @@ special_insn:case 0xe9 :/* jmp rel */ goto jmp;case 0xea :/* jmp far */ -if (kvm_load_segment_descriptor (ctxt->vcpu, c->src2.val,9 , -VCPU_SREG_CS ) <0 ) { -DPRINTF ("jmp far: Failed to load CS descriptor\n" ); - goto cannot_emulate; - } +if (kvm_load_segment_descriptor (ctxt->vcpu, c->src2.val, +VCPU_SREG_CS )) + goto done; c->eip = c->src.val;break ;
arch/x86/kvm/x86.c +142 -35
242 lines not on this page · raw message
Signature · 1 line
1.6.5
PATCH 4/4 KVM: Fix emulate_sys[call, enter, exit]()'s fault handling
In reply to Gleb NatapovFrom: Takuya Yoshikawa <[email protected]>
This patch fixes emulate_syscall(), emulate_sysenter() and emulate_sysexit() to handle injected faults properly.
Even though original code injects faults in these functions, we cannot handle these unless we use the different return value from the UNHANDLEABLE case. So this patch use X86EMUL_* codes instead of -1 and 0 and makes x86_emulate_insn() to handle these propagated faults.
Be sure that, in x86_emulate_insn(), goto cannot_emulate and goto done with rc equals X86EMUL_UNHANDLEABLE have same effect.
--- arch/x86/kvm/emulate.c | 37 ++++++++++++++++++++----------------- 1 files changed, 20 insertions(+), 17 deletions(-)
arch/x86/kvm/emulate.c +20 -17
@@ -1590,7 +1590,7 @@ emulate_syscall(struct x86_emulate_ctxt *ctxt)/* syscall is not available in real mode */ if (ctxt->mode ==X86EMUL_MODE_REAL || ctxt->mode ==X86EMUL_MODE_VM86 ) -return -1 ; +return X86EMUL_UNHANDLEABLE ;setup_syscalls_segments (ctxt, &cs, &ss);
@@ -1627,7 +1627,7 @@ emulate_syscall(struct x86_emulate_ctxt *ctxt) ctxt->eflags &= ~(EFLG_VM |EFLG_IF |EFLG_RF ); } -return 0 ; +return X86EMUL_CONTINUE ; } static int
@@ -1640,14 +1640,14 @@ emulate_sysenter(struct x86_emulate_ctxt *ctxt)/* inject #GP if in real mode */ if (ctxt->mode ==X86EMUL_MODE_REAL ) {kvm_inject_gp (ctxt->vcpu,0 ); -return -1 ; +return X86EMUL_UNHANDLEABLE ; }/* XXX sysenter/sysexit have not been tested in 64bit mode. * Therefore, we inject an #UD. */ if (ctxt->mode ==X86EMUL_MODE_PROT64 ) -return -1 ; +return X86EMUL_UNHANDLEABLE ;setup_syscalls_segments (ctxt, &cs, &ss);
@@ -1656,13 +1656,13 @@ emulate_sysenter(struct x86_emulate_ctxt *ctxt)case X86EMUL_MODE_PROT32 :if ((msr_data &0xfffc ) ==0x0 ) {kvm_inject_gp (ctxt->vcpu,0 ); -return -1 ; +return X86EMUL_PROPAGATE_FAULT ; }break ;case X86EMUL_MODE_PROT64 :if (msr_data ==0x0 ) {kvm_inject_gp (ctxt->vcpu,0 ); -return -1 ; +return X86EMUL_PROPAGATE_FAULT ; }break ; }
@@ -1687,7 +1687,7 @@ emulate_sysenter(struct x86_emulate_ctxt *ctxt) kvm_x86_ops->get_msr (ctxt->vcpu,MSR_IA32_SYSENTER_ESP , &msr_data); c->regs[VCPU_REGS_RSP ] = msr_data; -return 0 ; +return X86EMUL_CONTINUE ; } static int
@@ -1702,7 +1702,7 @@ emulate_sysexit(struct x86_emulate_ctxt *ctxt)if (ctxt->mode ==X86EMUL_MODE_REAL || ctxt->mode ==X86EMUL_MODE_VM86 ) {kvm_inject_gp (ctxt->vcpu,0 ); -return -1 ; +return X86EMUL_UNHANDLEABLE ; }setup_syscalls_segments (ctxt, &cs, &ss);
@@ -1720,7 +1720,7 @@ emulate_sysexit(struct x86_emulate_ctxt *ctxt) cs.selector = (u16)(msr_data +16 );if ((msr_data &0xfffc ) ==0x0 ) {kvm_inject_gp (ctxt->vcpu,0 ); -return -1 ; +return X86EMUL_PROPAGATE_FAULT ; } ss.selector = (u16)(msr_data +24 );break ;
@@ -1728,7 +1728,7 @@ emulate_sysexit(struct x86_emulate_ctxt *ctxt) cs.selector = (u16)(msr_data +32 );if (msr_data ==0x0 ) {kvm_inject_gp (ctxt->vcpu,0 ); -return -1 ; +return X86EMUL_PROPAGATE_FAULT ; } ss.selector = cs.selector +8 ; cs.db =0 ;
@@ -1744,7 +1744,7 @@ emulate_sysexit(struct x86_emulate_ctxt *ctxt) c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX ]; c->regs[VCPU_REGS_RSP ] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX ]; -return 0 ; +return X86EMUL_CONTINUE ; }static bool emulator_bad_iopl (struct x86_emulate_ctxt *ctxt)
@@ -2472,8 +2472,9 @@ twobyte_insn: }break ;case 0x05 :/* syscall */ -if (emulate_syscall (ctxt) ==-1 ) - goto cannot_emulate; + rc =emulate_syscall (ctxt); +if (rc !=X86EMUL_CONTINUE ) + goto done;else goto writeback;break ;
@@ -2541,14 +2542,16 @@ twobyte_insn: c->dst.type =OP_NONE ;break ;case 0x34 :/* sysenter */ -if (emulate_sysenter (ctxt) ==-1 ) - goto cannot_emulate; + rc =emulate_sysenter (ctxt); +if (rc !=X86EMUL_CONTINUE ) + goto done;else goto writeback;break ;case 0x35 :/* sysexit */ -if (emulate_sysexit (ctxt) ==-1 ) - goto cannot_emulate; + rc =emulate_sysexit (ctxt); +if (rc !=X86EMUL_CONTINUE ) + goto done;else goto writeback;break ;
Signature · 1 line
1.6.5
Avi Kivity
AppliedQuoted 3 lines · Gleb Natapov
This patch series adds proper permission checking during segment selector loading. Some missing fault injections are added.
Thanks, applied.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 10 lines · Gleb Natapov
This patch series adds proper permission checking during segment selector loading. Some missing fault injections are added.
Gleb Natapov (2):
KVM: Forbid modifying CS segment register by mov instruction.
KVM: Fix segment descriptor loading.Takuya Yoshikawa (2):
KVM: Fixload_guest_segment_descriptor()to inject page fault
KVM: Fix emulate_sys[call, enter, exit]()'s fault handling
Thanks you for rebasing my work and making the fix more comprehensive.
Quoted 10 lines
arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/kvm/emulate.c | 71 +++++++-------- arch/x86/kvm/x86.c | 190 +++++++++++++++++++++++++++++++-------- 3 files changed, 186 insertions(+), 78 deletions(-)--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html