From patchwork Thu May 27 05:01:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12283339 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.8 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 9F0AAC4707F for ; Thu, 27 May 2021 05:02:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 77C8A613AB for ; Thu, 27 May 2021 05:02:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234954AbhE0FDo (ORCPT ); Thu, 27 May 2021 01:03:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232324AbhE0FDn (ORCPT ); Thu, 27 May 2021 01:03:43 -0400 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A43BC061574; Wed, 26 May 2021 22:02:11 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id q67so2732282pfb.4; Wed, 26 May 2021 22:02:11 -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=JyLaYThv3NNpHzg8SQfk89DGWSaPbCfxZj9NwLUkGkc=; b=JniPRzLBCo1/vCFwMMamhh/yUPPMCJEiF+NF9ut+dLAonrFX/PPFbMeJFE6IvbLRai uiAHhTOAgIie7aKlt1G/vobcCBeEXisOn619t0xajxw+5qddHvj8+znCaSgZnqZ7VjFr dJ8o7DZe0XR2av6Ska+8BgQr89es+5V6Sg2rELxZY4y5r0RLX75LYs13TE5QCGiZ7vyw pmrLEkwVzfPxCoY/KpCOi0TWAO33VdvbvHXSj2CH0y8skLN727htkEQt8pZMkK62viO8 p0iu7MCe7h0RlQMdzLOVQmrv4mx++G0aEz5a3UDoXpOBEkzROyJWauqv1R5/2LBh0AIw HPww== 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=JyLaYThv3NNpHzg8SQfk89DGWSaPbCfxZj9NwLUkGkc=; b=uAbZkMl4D8P7I9tYP5zqVgcj2sQoUwaaWtEJ9iA0I0B+czcWWbfNPvOXDCSB+EB22U 90FCUYfXJeGGk7hE92TvpsDONhgneXfrWC78QWiYUoqKiEu4sjYynHBG7WumqnFtJLW0 kMM0UmQObXxe+wkvqHDwSn5Fk9bJqmmlXDffhEoyOv6AW/eUtEawj93LbmNMeTqAmaAG qqT7cjWOe+EQYdMg+3lR62jjywmrCz/vjvVm+PqAO6GDxILkfe0uXD7fGymIfZweRqnV lRSOtFnDrQWO/8SDUJc8ddlTbzXiZLPbpqHhPtwEpPI9IhOPLGzTWeB3EN22JSmOAbq3 CpWA== X-Gm-Message-State: AOAM532+eK+4LBiNJw5EONNJpUW5lPpLe69vujM5Nb61XhQmb8tJ2DHG 9S5FUbl1eoLW1EDh5yQAp4v0b+ExlxM= X-Google-Smtp-Source: ABdhPJykSfoIMilDriJP/wgdluGR2twpNZG+C1IxxNAuGD3mkXExMbtBxK7kA2l5j9dPVbtPVyo3OA== X-Received: by 2002:a05:6a00:2126:b029:2e2:89d8:5c89 with SMTP id n6-20020a056a002126b02902e289d85c89mr1951538pfj.37.1622091730652; Wed, 26 May 2021 22:02:10 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.40]) by smtp.googlemail.com with ESMTPSA id z7sm668384pgr.28.2021.05.26.22.02.07 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 26 May 2021 22:02:10 -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 v3 1/2] KVM: X86: Fix warning caused by stale emulation context Date: Wed, 26 May 2021 22:01:18 -0700 Message-Id: <1622091679-31683-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. 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 --- v2 -> v3: * squash ctxt->ud 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 bbc4e04..ae47b19 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7226,6 +7226,13 @@ 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; + + ctxt->ud = emulation_type & EMULTYPE_TRAP_UD; + init_decode_cache(ctxt); vcpu->arch.emulate_regs_need_sync_from_vcpu = false; } @@ -7561,13 +7568,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 Thu May 27 05:01:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12283341 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.8 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 C9589C47089 for ; Thu, 27 May 2021 05:02:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A85D4613BE for ; Thu, 27 May 2021 05:02:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234987AbhE0FDw (ORCPT ); Thu, 27 May 2021 01:03:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234964AbhE0FDr (ORCPT ); Thu, 27 May 2021 01:03:47 -0400 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 71935C061574; Wed, 26 May 2021 22:02:14 -0700 (PDT) Received: by mail-pl1-x62c.google.com with SMTP id v13so1733552ple.9; Wed, 26 May 2021 22:02:14 -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=l7VuWFdCQyNw6QUbl6mT63Xq+VHRKrFG3jQVWST27ZU=; b=R4NE0vQyugGc63JoThiZhysQsbp1MdngY/pUndjsX1O8znXI9+cgoU0wDmqRFrMslf w0SRPdxuNlfVFMJ4vbtauHOTVG9hTTS7woYwH42qnGRHaBvlexevEUw++3g7i43DDcpq gN3uC1Ss3yT+Q3dLLaa0a7RNSZfNtqeMg+8BT/MNAlKRzCxXT/3qVjULmXed+QvNz5tj D35W2qJC5OrVN63EbXZwQtuCVc7kf3IWI9MmWAP951gDZUMEAGpavG1oLAc7NC9abPN0 6P+TDB+mKbLwWVJNqy7KFNZUwE3HArmRF10yCmjOp1GmyB4WAo2Law7tfkoy8JVC+wFb QOSg== 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=l7VuWFdCQyNw6QUbl6mT63Xq+VHRKrFG3jQVWST27ZU=; b=oWwkTdxJ6bh6aOTHen3ts7LkZABIZJSybp9cFW72qI98hRGU9bKo0+h7y3YsYaoDrA b4oDRBpO9raUmV7zqtx39lnijv+ToHJ+T0ga5hOe7UuFsJu27wqo1TcqWX8z3sVrTn9L HOJOZVOAkRrp5XsAVn/jKgg4fFS1+EdfK+TARbMkHzNzUn/jCTw7tlrVm0QfLuOa2Pqb hbeu5xKCPYvC9nXi/1cl8CRAZjSEfoiDtcPvKbN6K1M92o3k+AgTAUfvpR/zKC0OPFDz UOIkAcJIbP2kl5YDQgjcQaj8ixb9Jn7wmbR8Zvc128tMPV30A6grvU2xvZZQeY6Kxlck zMeA== X-Gm-Message-State: AOAM5336+VXn1AcsXOh+nPG0lk+9XiNO9l9mdI1F4cvGeRLuHr2xuzLK ek2+LTBeLiBRoD2jonZh3KBBhclwEyE= X-Google-Smtp-Source: ABdhPJwZLUsdt3ZlRkWWrKCJIljnp9XhZ5QFo8W/7wIZUECDt4xSMGx9pH6u9xehQ3SnMQuIRKw5Jw== X-Received: by 2002:a17:902:b594:b029:f8:fb4f:f8d3 with SMTP id a20-20020a170902b594b02900f8fb4ff8d3mr1609343pls.25.1622091733714; Wed, 26 May 2021 22:02:13 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.40]) by smtp.googlemail.com with ESMTPSA id z7sm668384pgr.28.2021.05.26.22.02.10 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 26 May 2021 22:02:13 -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 v3 2/2] KVM: X86: Kill off ctxt->ud Date: Wed, 26 May 2021 22:01:19 -0700 Message-Id: <1622091679-31683-2-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1622091679-31683-1-git-send-email-wanpengli@tencent.com> References: <1622091679-31683-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 ae47b19..d752345 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7231,8 +7231,6 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu) ctxt->exception.vector = -1; ctxt->perm_ok = false; - ctxt->ud = emulation_type & EMULTYPE_TRAP_UD; - init_decode_cache(ctxt); vcpu->arch.emulate_regs_need_sync_from_vcpu = false; } @@ -7568,7 +7566,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;