Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2898316pxb; Tue, 24 Aug 2021 10:08:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwM0dtxOh5F4uggmC5TAIe66GM/bHpu4CivXCGqy1e4uBMLTtQfOZBbWGpmal/graVIXays X-Received: by 2002:a02:90d0:: with SMTP id c16mr35270432jag.106.1629824888020; Tue, 24 Aug 2021 10:08:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629824888; cv=none; d=google.com; s=arc-20160816; b=XfEDJNagA/ErKrAqrRL5Q+0Dtidjzr6ILIYGVZKOaHp+tlZN1I8Dst0Vmc3IzoKZVI UmtRdtZmhk9y3jYSiNY9HvIxO/o4mHQqXd2k+sBn70cqZkZXO1wVwsfLLORTakhJRagh 3Urld7IbwLpkm7yjVl91pWfFw5D1q0JYVBVLcCQfbudHsBkF+O8QCe11N/5UNSUTanlt entnu4SqWkThJHkcmPJCDruwkZgdQ89gFpKTFYdsRn9qJ80SdmbPpCYeJA/QsSxf7THd AarPj/RV5thRRZnOEQdjRTSKdpej8PT7mceqX6AvWn0BCMj108vQZBBtq6fyE5OTz4Df s+Nw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=BDUwiMVn8zqjiSiDzqyEX6VzQZlxO+3wzb1w1efBom4=; b=TgE+zIDJD58p8Ueg9avihYqK3f7qEuNoVwCbVrAX/GoAhlURp+gFvfl+0Qpnypd5ZI xoXigZjnmcfacVBhcKDmfnwRjw9KK9v4DNt8NkNn1ITTi/5CCR5Z1geggDelCfNfpzVz B7CdUlznXPeDkxjl4klL8Filu0M4TIBq6VQsBZZG5QlupoPYC3t4QegxeEndz6Yzq/7V 0z1fwMbubUxFaYE6KN9I4M8cpeD4lHKGoruh/0bQ9pssFnwY1bRM+2hNOe4YcuRLC0B+ EuVi1/FyD7BxCqboSTChDpNY8xf25CyfxtgRCLe9ZINoScWC3oBc5GSdfpW/gbQ3Vt8a 0Vig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DwiFvGz0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b24si17755403jal.12.2021.08.24.10.07.55; Tue, 24 Aug 2021 10:08:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DwiFvGz0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238851AbhHXRGm (ORCPT + 99 others); Tue, 24 Aug 2021 13:06:42 -0400 Received: from mail.kernel.org ([198.145.29.99]:47426 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239448AbhHXREN (ORCPT ); Tue, 24 Aug 2021 13:04:13 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 3212F61406; Tue, 24 Aug 2021 16:59:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1629824363; bh=NMYGCXl5ejWBmVVTJId6Pqozev18JKzX4E3XUjA8LpE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DwiFvGz0fInVf6m5FWjQtyEvM0lyIfFE6/pt0tzSUzvY+sgxeyTBfL/xxCV1LuH8o Bq09JNQF2z2FUQggh+EVnKJqx7Yd+6JNMH9ORLMZ7A5OP0wGMRVIpLsZCkscU4RCwy 5iiSNcalGBitY8VhJSv2t+K6Q+6k4s75PX8M0cTMcuzob+0dNxiXMS7KpW20EK2sqK MYN2wM8brcwwsU8KCKEPyOjnPOF2mBT0DTzCsT5+pFkZXN8XOX/JeD6+4YkGKYkDIk yLS03Deu537vKnWGnPziTZqvhCTq2KxWk/SVCECwsBwipBojUcDOdPExxUao5LrzxE NKS8JxSn2TypQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Wanpeng Li , syzbot+71271244f206d17f6441@syzkaller.appspotmail.com, Sean Christopherson , Sasha Levin Subject: [PATCH 5.10 13/98] KVM: X86: Fix warning caused by stale emulation context Date: Tue, 24 Aug 2021 12:57:43 -0400 Message-Id: <20210824165908.709932-14-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210824165908.709932-1-sashal@kernel.org> References: <20210824165908.709932-1-sashal@kernel.org> MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.10.61-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-5.10.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 5.10.61-rc1 X-KernelTest-Deadline: 2021-08-26T16:58+00:00 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wanpeng Li [ Upstream commit da6393cdd8aaa354b3a2437cd73ebb34cac958e3 ] Reported by syzkaller: WARNING: CPU: 7 PID: 10526 at 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 4a1e10d5b5d8 ("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 Reviewed-by: Sean Christopherson Message-Id: <1622160097-37633-1-git-send-email-wanpengli@tencent.com> Signed-off-by: Sasha Levin --- arch/x86/kvm/x86.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index d5e25bf51f47..812585986bb8 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7023,6 +7023,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; } @@ -7358,11 +7363,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); -- 2.30.2