Skip to content

Commit 07e3aff

Browse files
bonzinigregkh
authored andcommitted
KVM: x86: fix singlestepping over syscall
commit c8401dda2f0a00cd25c0af6a95ed50e478d25de4 upstream. TF is handled a bit differently for syscall and sysret, compared to the other instructions: TF is checked after the instruction completes, so that the OS can disable #DB at a syscall by adding TF to FMASK. When the sysret is executed the #DB is taken "as if" the syscall insn just completed. KVM emulates syscall so that it can trap 32-bit syscall on Intel processors. Fix the behavior, otherwise you could get #DB on a user stack which is not nice. This does not affect Linux guests, as they use an IST or task gate for #DB. This fixes CVE-2017-7518. Reported-by: Andy Lutomirski <luto@kernel.org> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Radim Krčmář <rkrcmar@redhat.com> [bwh: Backported to 4.4: - kvm_vcpu_check_singlestep() sets some flags differently - Drop changes to kvm_skip_emulated_instruction()] Signed-off-by: Ben Hutchings <ben.hutchings@codethink.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent ceb5c56 commit 07e3aff

3 files changed

Lines changed: 24 additions & 30 deletions

File tree

arch/x86/include/asm/kvm_emulate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ struct x86_emulate_ctxt {
296296

297297
bool perm_ok; /* do not check permissions if true */
298298
bool ud; /* inject an #UD if host doesn't support insn */
299+
bool tf; /* TF value before instruction (after for syscall/sysret) */
299300

300301
bool have_exception;
301302
struct x86_exception exception;

arch/x86/kvm/emulate.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2726,6 +2726,7 @@ static int em_syscall(struct x86_emulate_ctxt *ctxt)
27262726
ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
27272727
}
27282728

2729+
ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
27292730
return X86EMUL_CONTINUE;
27302731
}
27312732

arch/x86/kvm/x86.c

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5095,6 +5095,8 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
50955095
kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
50965096

50975097
ctxt->eflags = kvm_get_rflags(vcpu);
5098+
ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
5099+
50985100
ctxt->eip = kvm_rip_read(vcpu);
50995101
ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
51005102
(ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
@@ -5315,37 +5317,26 @@ static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
53155317
return dr6;
53165318
}
53175319

5318-
static void kvm_vcpu_check_singlestep(struct kvm_vcpu *vcpu, unsigned long rflags, int *r)
5320+
static void kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu, int *r)
53195321
{
53205322
struct kvm_run *kvm_run = vcpu->run;
53215323

5322-
/*
5323-
* rflags is the old, "raw" value of the flags. The new value has
5324-
* not been saved yet.
5325-
*
5326-
* This is correct even for TF set by the guest, because "the
5327-
* processor will not generate this exception after the instruction
5328-
* that sets the TF flag".
5329-
*/
5330-
if (unlikely(rflags & X86_EFLAGS_TF)) {
5331-
if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
5332-
kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 |
5333-
DR6_RTM;
5334-
kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip;
5335-
kvm_run->debug.arch.exception = DB_VECTOR;
5336-
kvm_run->exit_reason = KVM_EXIT_DEBUG;
5337-
*r = EMULATE_USER_EXIT;
5338-
} else {
5339-
vcpu->arch.emulate_ctxt.eflags &= ~X86_EFLAGS_TF;
5340-
/*
5341-
* "Certain debug exceptions may clear bit 0-3. The
5342-
* remaining contents of the DR6 register are never
5343-
* cleared by the processor".
5344-
*/
5345-
vcpu->arch.dr6 &= ~15;
5346-
vcpu->arch.dr6 |= DR6_BS | DR6_RTM;
5347-
kvm_queue_exception(vcpu, DB_VECTOR);
5348-
}
5324+
if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
5325+
kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM;
5326+
kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip;
5327+
kvm_run->debug.arch.exception = DB_VECTOR;
5328+
kvm_run->exit_reason = KVM_EXIT_DEBUG;
5329+
*r = EMULATE_USER_EXIT;
5330+
} else {
5331+
vcpu->arch.emulate_ctxt.eflags &= ~X86_EFLAGS_TF;
5332+
/*
5333+
* "Certain debug exceptions may clear bit 0-3. The
5334+
* remaining contents of the DR6 register are never
5335+
* cleared by the processor".
5336+
*/
5337+
vcpu->arch.dr6 &= ~15;
5338+
vcpu->arch.dr6 |= DR6_BS | DR6_RTM;
5339+
kvm_queue_exception(vcpu, DB_VECTOR);
53495340
}
53505341
}
53515342

@@ -5500,8 +5491,9 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,
55005491
toggle_interruptibility(vcpu, ctxt->interruptibility);
55015492
vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
55025493
kvm_rip_write(vcpu, ctxt->eip);
5503-
if (r == EMULATE_DONE)
5504-
kvm_vcpu_check_singlestep(vcpu, rflags, &r);
5494+
if (r == EMULATE_DONE &&
5495+
(ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)))
5496+
kvm_vcpu_do_singlestep(vcpu, &r);
55055497
if (!ctxt->have_exception ||
55065498
exception_type(ctxt->exception.vector) == EXCPT_TRAP)
55075499
__kvm_set_rflags(vcpu, ctxt->eflags);

0 commit comments

Comments
 (0)