Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752320AbdLMDKa (ORCPT ); Tue, 12 Dec 2017 22:10:30 -0500 Received: from mail-pf0-f196.google.com ([209.85.192.196]:42908 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751643AbdLMDK0 (ORCPT ); Tue, 12 Dec 2017 22:10:26 -0500 X-Google-Smtp-Source: ACJfBoshwcUnQdnAs6qUPtbO3GA/6v0zmVNJ8rcu6VBVgZMg4zTxdZf0f/dH+EqDt7LnUCGOyJJT4g== From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Wanpeng Li , David Hildenbrand , Dmitry Vyukov Subject: [PATCH v2] KVM: X86: Fix host dr6 miss restore Date: Tue, 12 Dec 2017 19:10:13 -0800 Message-Id: <1513134613-7900-1-git-send-email-wanpeng.li@hotmail.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2123 Lines: 57 From: Wanpeng Li Reported by syzkaller: WARNING: CPU: 0 PID: 12927 at arch/x86/kernel/traps.c:780 do_debug+0x222/0x250 CPU: 0 PID: 12927 Comm: syz-executor Tainted: G OE 4.15.0-rc2+ #16 RIP: 0010:do_debug+0x222/0x250 Call Trace: <#DB> debug+0x3e/0x70 RIP: 0010:copy_user_enhanced_fast_string+0x10/0x20 _copy_from_user+0x5b/0x90 SyS_timer_create+0x33/0x80 entry_SYSCALL_64_fastpath+0x23/0x9a The syzkaller will mmap a buffer which is also the struct sigevent parameter of timer_create(), it will also call perf_event_open() to set a BP for the buffer, so when the implementation of timer_create() in kernel tries to get the struct sigevent parameter by copy_from_user(), rep movsb triggers the BP. The syzkaller testcase also sets the debug registers for the guest, however, the kvm just restores host debug registers when we have active breakpoints. I can observe the dr6 single step bit is set and !hw_breakpoint_active() sporadically by print when running the testcase heavy multithreading. The do_debug() which is triggered by rep movsb will splash when (dr6 & DR_STEP && !user_mode(regs)). This patch fixes it by restoring host dr6 in sched_out if no breakpoint is active. Reported-by: Dmitry Vyukov Cc: Paolo Bonzini Cc: Radim Krčmář Cc: David Hildenbrand Cc: Dmitry Vyukov Reviewed-by: David Hildenbrand Signed-off-by: Wanpeng Li --- v1 -> v2: * move to sched_out path arch/x86/kvm/x86.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 1c5c7a3..76886c4 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2964,6 +2964,8 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) pagefault_enable(); kvm_x86_ops->vcpu_put(vcpu); vcpu->arch.last_host_tsc = rdtsc(); + if (!hw_breakpoint_active()) + set_debugreg(current->thread.debugreg6, 6); } static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, -- 2.7.4