From patchwork Tue May 25 03:02:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12277703 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BE50AC2B9F7 for ; Tue, 25 May 2021 03:03:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9C20D61423 for ; Tue, 25 May 2021 03:03:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230327AbhEYDFP (ORCPT ); Mon, 24 May 2021 23:05:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33374 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230289AbhEYDFM (ORCPT ); Mon, 24 May 2021 23:05:12 -0400 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D430DC06138B; Mon, 24 May 2021 20:03:42 -0700 (PDT) Received: by mail-pg1-x52b.google.com with SMTP id q15so21576546pgg.12; Mon, 24 May 2021 20:03:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=mQqRebMZTVl6ktNMXpc5Vi8p6ykYUYFibsRy0qZxtQM=; b=IbPL5TRgFAcAOOSWrZrLhbx6isrhB8d9Fphihhe3t7Pl05RrQlrdEA+qIKATTfcKGP AUT3F68tvxA2ZyAdIOgBxctbkem/Tm4iBlZuLkuzJWjQIFUxcGKjpMLdyUjDWXcpdAFw 2ntzNskMUskjsOwD91JHdwP5elLNVCX2kVRv+h6Cm0KVrL9DTmAhsqjCUhFnheWl5Uj4 tO5Sz4+2f6dNvPMHmIUzFU+J91KJYGA/FPNp7hStKSy/8Dk2hUH3xYTQQopHMiac2i0G XYM/wFC1pxnFWqc/GlqIeOOMUx1OGc8fHKJkIcxLsISXTSVwmwIKui/2/ACDk5xuSsSa YCcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=mQqRebMZTVl6ktNMXpc5Vi8p6ykYUYFibsRy0qZxtQM=; b=aKDCn2lgSyO9F6QFakIwe1e0x3fhX+N/8hKR7K6Uk6ntzIlD0ofayjxE7WgF1FB9GI nvLLANRTMi+UMeubOe4MH9dpMtzyEsOYMi3mXTlXnD1FlNIuV/WPoLI3mLI5aePkJm1u 7I8s4BRnscGFUZwtpc3tOL6cps4X1NEAI5Sz11jxldZZTWxTr8FFvZXvSavffM/JRKXx rrDhPx1zF2yBTYNtFIJDWlzGLwEAyOd37C2uNGESSuXf4HEMwd7ejjbCIt5g8h34KMq0 c4mOCdLrCzx+Vh7Qv0SuLRp2fAjkrsUbrHWAXrnbcb+Ca7ntbE0k7jWHoWJIWQGUHlIH cynw== X-Gm-Message-State: AOAM5312IRGtmbv9/32b1+ehvumkK5HDF5y5uugYHzuL7zaeFROlFd42 tIWirp48vJoaOOgvEKkhA/LTL6UmcUQ= X-Google-Smtp-Source: ABdhPJztdsjuKHiXAOdhyFM2qyUcDH2Hm1ONxO49kQXnnHMd264+I6ZZV346JcBdExjTDwcklbBIIA== X-Received: by 2002:a62:86ce:0:b029:2e0:2bb5:575b with SMTP id x197-20020a6286ce0000b02902e02bb5575bmr28074776pfd.60.1621911822121; Mon, 24 May 2021 20:03:42 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.56]) by smtp.googlemail.com with ESMTPSA id a10sm12506217pfg.173.2021.05.24.20.03.39 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 24 May 2021 20:03:41 -0700 (PDT) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel Subject: [PATCH v2 1/2] KVM: X86: Fix warning caused by stale emulation context Date: Mon, 24 May 2021 20:02:46 -0700 Message-Id: <1621911767-11703-1-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li Reported by syzkaller: WARNING: CPU: 7 PID: 10526 at /home/kernel/ssd/linux/arch/x86/kvm//x86.c:7621 x86_emulate_instruction+0x41b/0x510 [kvm] RIP: 0010:x86_emulate_instruction+0x41b/0x510 [kvm] Call Trace: kvm_mmu_page_fault+0x126/0x8f0 [kvm] vmx_handle_exit+0x11e/0x680 [kvm_intel] vcpu_enter_guest+0xd95/0x1b40 [kvm] kvm_arch_vcpu_ioctl_run+0x377/0x6a0 [kvm] kvm_vcpu_ioctl+0x389/0x630 [kvm] __x64_sys_ioctl+0x8e/0xd0 do_syscall_64+0x3c/0xb0 entry_SYSCALL_64_after_hwframe+0x44/0xae Commit 4a1e10d5b5d8c (KVM: x86: handle hardware breakpoints during emulation()) adds hardware breakpoints check before emulation the instruction and parts of emulation context initialization, actually we don't have the EMULTYPE_NO_DECODE flag here and the emulation context will not be reused. Commit c8848cee74ff (KVM: x86: set ctxt->have_exception in x86_decode_insn()) triggers the warning because it catches the stale emulation context has #UD, however, it is not during instruction decoding which should result in EMULATION_FAILED. This patch fixes it by moving the second part emulation context initialization into init_emulate_ctxt() and before hardware breakpoints check. The ctxt->ud will be dropped by a follow-up patch. syzkaller source: https://syzkaller.appspot.com/x/repro.c?x=134683fdd00000 Reported-by: syzbot+71271244f206d17f6441@syzkaller.appspotmail.com Fixes: 4a1e10d5b5d8 (KVM: x86: handle hardware breakpoints during emulation) Signed-off-by: Wanpeng Li --- v1 -> v2: * move the second part emulation context initialization into init_emulate_ctxt() arch/x86/kvm/x86.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index bed7b53..3c109d3 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7228,6 +7228,11 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu) BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK); BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK); + ctxt->interruptibility = 0; + ctxt->have_exception = false; + ctxt->exception.vector = -1; + ctxt->perm_ok = false; + init_decode_cache(ctxt); vcpu->arch.emulate_regs_need_sync_from_vcpu = false; } @@ -7554,6 +7559,8 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, init_emulate_ctxt(vcpu); + ctxt->ud = emulation_type & EMULTYPE_TRAP_UD; + /* * We will reenter on the same instruction since we do not set * complete_userspace_io. This does not handle watchpoints yet, @@ -7563,13 +7570,6 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, kvm_vcpu_check_breakpoint(vcpu, &r)) return r; - ctxt->interruptibility = 0; - ctxt->have_exception = false; - ctxt->exception.vector = -1; - ctxt->perm_ok = false; - - ctxt->ud = emulation_type & EMULTYPE_TRAP_UD; - r = x86_decode_insn(ctxt, insn, insn_len); trace_kvm_emulate_insn_start(vcpu); From patchwork Tue May 25 03:02:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12277705 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A091BC2B9F7 for ; Tue, 25 May 2021 03:03:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 835D961423 for ; Tue, 25 May 2021 03:03:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230340AbhEYDFV (ORCPT ); Mon, 24 May 2021 23:05:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230321AbhEYDFS (ORCPT ); Mon, 24 May 2021 23:05:18 -0400 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93DDCC061574; Mon, 24 May 2021 20:03:49 -0700 (PDT) Received: by mail-pf1-x436.google.com with SMTP id 22so22076812pfv.11; Mon, 24 May 2021 20:03:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=cYfiwJ/w2ntWHiGn8Ut2Mv2U+24DNRGZ2IaLNOo55oI=; b=B8LrkPwurS60GEzPr6Ae8uMl9/MeFn/h5WNNW40j2dZzw/L7jGMvGWO0297+Q+LrfL mypyYwsswSG8gSgRIyHjVg9w6TBG2Gded/wkvypkjCGKKRR72VfzWrgdcVEMDBelIMb9 kuyLld2iXdt5+nd1PV4rvaTVM6gioIZCNtKeMRnc/xU9GojCAtLILsdzhLxuD8fF4uGX 1KCZtw5twqM2sInI5AAsqM3U+Wv5N1JC4jbWlHpvHM4pHKDxUU0ArRQg/GSqdFIdTmz3 6aan6VuEAdJHNfaeIYQtCBQ35p2TT3JJgs0fbq2djWDNd5rm71ojWSIPYMDn6Oa2FiA/ KKaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=cYfiwJ/w2ntWHiGn8Ut2Mv2U+24DNRGZ2IaLNOo55oI=; b=EAfjIuYoO5svRx4pzAsFrl8I/5+6Olk1KYNX0oIKf0Vxqz6QhJwmghi7CzsHgCmOmC mY0aaeid7yORhLItOl7WVBjfvXOklPZdp0Fugf3CxbdVXG2rrzf99FbmNru7YRrgxQBs E6ElzoK7GD+sw5nIduEhsWBkSkcTwM04kdw7DOEgZZHCrQJ3owWgcnVygf8LSRO89V+N GvSuWYdXxltfXLEVSSoTq0pE+hcwHSn1+jhwzhj4jclLKVD9nbJdTMnmf0n9EGPL1ba8 ECk+Vt7nu8PxAKdfckeVvIF7zY+Sx47nAyLFOeEdP7T4xUAP5hpN+j8DxK2eyi1GzbGF Kjyg== X-Gm-Message-State: AOAM532pcmmTYPDJSEYHWm/cytK06+1Yel4M53Ydnt260h7LV52RxqUi 0oAjz/kI+wOqMEQQsjOxWuzUnO2Nviw= X-Google-Smtp-Source: ABdhPJwZWIf3jK2XH+H7DONmlrr//4efVH4Uf4jivVPJwvAzhkqM3B8hx5McNUNjTVrCXIixvTKVLA== X-Received: by 2002:a05:6a00:7c5:b029:2e8:d5f5:9677 with SMTP id n5-20020a056a0007c5b02902e8d5f59677mr9769497pfu.68.1621911828796; Mon, 24 May 2021 20:03:48 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.56]) by smtp.googlemail.com with ESMTPSA id a10sm12506217pfg.173.2021.05.24.20.03.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 24 May 2021 20:03:48 -0700 (PDT) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel Subject: [PATCH v2 2/2] KVM: X86: Kill off ctxt->ud Date: Mon, 24 May 2021 20:02:47 -0700 Message-Id: <1621911767-11703-2-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1621911767-11703-1-git-send-email-wanpengli@tencent.com> References: <1621911767-11703-1-git-send-email-wanpengli@tencent.com> Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li ctxt->ud is consumed only by x86_decode_insn(), we can kill it off by passing emulation_type to x86_decode_insn() and dropping ctxt->ud altogether. Tracking that info in ctxt for literally one call is silly. Suggested-by: Sean Christopherson Signed-off-by: Wanpeng Li --- arch/x86/kvm/emulate.c | 5 +++-- arch/x86/kvm/kvm_emulate.h | 3 +-- arch/x86/kvm/x86.c | 4 +--- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 8a0ccdb..5e5de05 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -5111,7 +5111,7 @@ static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op, return rc; } -int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) +int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int emulation_type) { int rc = X86EMUL_CONTINUE; int mode = ctxt->mode; @@ -5322,7 +5322,8 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) ctxt->execute = opcode.u.execute; - if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD))) + if (unlikely(emulation_type & EMULTYPE_TRAP_UD) && + likely(!(ctxt->d & EmulateOnUD))) return EMULATION_FAILED; if (unlikely(ctxt->d & diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h index f016838..3e870bf 100644 --- a/arch/x86/kvm/kvm_emulate.h +++ b/arch/x86/kvm/kvm_emulate.h @@ -314,7 +314,6 @@ struct x86_emulate_ctxt { int interruptibility; bool perm_ok; /* do not check permissions if true */ - bool ud; /* inject an #UD if host doesn't support insn */ bool tf; /* TF value before instruction (after for syscall/sysret) */ bool have_exception; @@ -491,7 +490,7 @@ enum x86_intercept { #define X86EMUL_MODE_HOST X86EMUL_MODE_PROT64 #endif -int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len); +int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int emulation_type); bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt); #define EMULATION_FAILED -1 #define EMULATION_OK 0 diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 3c109d3..7ea9424 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7559,8 +7559,6 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, init_emulate_ctxt(vcpu); - ctxt->ud = emulation_type & EMULTYPE_TRAP_UD; - /* * We will reenter on the same instruction since we do not set * complete_userspace_io. This does not handle watchpoints yet, @@ -7570,7 +7568,7 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, kvm_vcpu_check_breakpoint(vcpu, &r)) return r; - r = x86_decode_insn(ctxt, insn, insn_len); + r = x86_decode_insn(ctxt, insn, insn_len, emulation_type); trace_kvm_emulate_insn_start(vcpu); ++vcpu->stat.insn_emulation;