Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3919926pxj; Mon, 21 Jun 2021 09:22:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJywYN5mA0/UnhPjfBQ4YjVTVBB6XdFR5SN/j4GINAyo81x/MKe7I8ZyRB6gH9fjAgeLzmKY X-Received: by 2002:aa7:da8a:: with SMTP id q10mr16189390eds.81.1624292577485; Mon, 21 Jun 2021 09:22:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624292577; cv=none; d=google.com; s=arc-20160816; b=JLwFB2ce5+3USxM9FWiBrHggM14RjwxZcm6pPur6ukbQgo8uZHs4QEArS5eHqng8a1 NfNBalzkBc/1UiOa7y2nq/mNEcMp3LB0aa9ceOpw1TzEzVb4dt70RvQJWWsoWdhgZGSu prwocg0XatfVwTAYL0SrXMp6+G9vKDU//SqIu+881Rdd4fZrNQPxOywNaIFuHA92F8BO Hd4LyJpsRvuNVaNEvE7bUC/B2w2loiDyylvIKNKd+i03JmN7lFjsbC9Jf+ckU5VQ2IKR jmyJbm7l1rbJ7V0EyEmO8+o5O7fMdtIlvSej3zQo17bVwmLuEBSVAE05Xer5QkgkVmlX rnGg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=NWSbYlF9ualICalaljdI2uzkK8iUyHkjXH7MeGXAZfA=; b=TX6KLOolOn4BGgVhveFZ8rpNqh6uG1qtBLmbpYpr651VMnvA1QEdFRQXpkAeFuq/WV Aya808bRgwJCzedW5It+EE58UwfD8K4KcesOnDDu/TKtI89YEA02FJPTwocHqhRGrGs1 9uV7XNr2SxPE7FDvMrTeFCNbZ1AHy2PtySbI0Hn1OP3KxHnvXsHVZfSJKeCCFGc9ACfE pqKe9UzCMQ4pOx5r+dPE534mpM5HRXMz9CEvtKTR47KUXGncQLY2H+D7+noj5fJR2DkN vfPPUaEGvWnUYhsW3cgLUpWkrXDP3Qib335suAOqkEZMxXloQzdMTFVw8dKtH8ufO+uO vCmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=NspSO2mI; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s2si10859385ejj.704.2021.06.21.09.22.33; Mon, 21 Jun 2021 09:22:57 -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=@linuxfoundation.org header.s=korg header.b=NspSO2mI; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230496AbhFUQXI (ORCPT + 99 others); Mon, 21 Jun 2021 12:23:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:40216 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231239AbhFUQVy (ORCPT ); Mon, 21 Jun 2021 12:21:54 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 15CD760231; Mon, 21 Jun 2021 16:19:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1624292368; bh=y67vgbwC42t9Bs6Md/z+brOwPj1y8+tGt45Lx19xP14=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NspSO2mI+FCVAvbBeB98Bwvq4fZSHf7dhoEkWzraCrVExvsCHZjSpuIFSzDP3+z97 NXWLclPJqirRp8XGvc80tXZvFOCcXFsbpYexX+gGGmYf+LjBQqxo2DypFozAiq934f qGpu86YqAhw0U/B4gs7GfZhWxrSqess1KeRY0iEU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+fb0b6a7e8713aeb0319c@syzkaller.appspotmail.com, Sean Christopherson , Paolo Bonzini Subject: [PATCH 5.4 64/90] KVM: x86: Immediately reset the MMU context when the SMM flag is cleared Date: Mon, 21 Jun 2021 18:15:39 +0200 Message-Id: <20210621154906.323110086@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210621154904.159672728@linuxfoundation.org> References: <20210621154904.159672728@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson commit 78fcb2c91adfec8ce3a2ba6b4d0dda89f2f4a7c6 upstream. Immediately reset the MMU context when the vCPU's SMM flag is cleared so that the SMM flag in the MMU role is always synchronized with the vCPU's flag. If RSM fails (which isn't correctly emulated), KVM will bail without calling post_leave_smm() and leave the MMU in a bad state. The bad MMU role can lead to a NULL pointer dereference when grabbing a shadow page's rmap for a page fault as the initial lookups for the gfn will happen with the vCPU's SMM flag (=0), whereas the rmap lookup will use the shadow page's SMM flag, which comes from the MMU (=1). SMM has an entirely different set of memslots, and so the initial lookup can find a memslot (SMM=0) and then explode on the rmap memslot lookup (SMM=1). general protection fault, probably for non-canonical address 0xdffffc0000000000: 0000 [#1] PREEMPT SMP KASAN KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] CPU: 1 PID: 8410 Comm: syz-executor382 Not tainted 5.13.0-rc5-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 RIP: 0010:__gfn_to_rmap arch/x86/kvm/mmu/mmu.c:935 [inline] RIP: 0010:gfn_to_rmap+0x2b0/0x4d0 arch/x86/kvm/mmu/mmu.c:947 Code: <42> 80 3c 20 00 74 08 4c 89 ff e8 f1 79 a9 00 4c 89 fb 4d 8b 37 44 RSP: 0018:ffffc90000ffef98 EFLAGS: 00010246 RAX: 0000000000000000 RBX: ffff888015b9f414 RCX: ffff888019669c40 RDX: 0000000000000000 RSI: 0000000000000001 RDI: 0000000000000001 RBP: 0000000000000001 R08: ffffffff811d9cdb R09: ffffed10065a6002 R10: ffffed10065a6002 R11: 0000000000000000 R12: dffffc0000000000 R13: 0000000000000003 R14: 0000000000000001 R15: 0000000000000000 FS: 000000000124b300(0000) GS:ffff8880b9b00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000000000000000 CR3: 0000000028e31000 CR4: 00000000001526e0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 Call Trace: rmap_add arch/x86/kvm/mmu/mmu.c:965 [inline] mmu_set_spte+0x862/0xe60 arch/x86/kvm/mmu/mmu.c:2604 __direct_map arch/x86/kvm/mmu/mmu.c:2862 [inline] direct_page_fault+0x1f74/0x2b70 arch/x86/kvm/mmu/mmu.c:3769 kvm_mmu_do_page_fault arch/x86/kvm/mmu.h:124 [inline] kvm_mmu_page_fault+0x199/0x1440 arch/x86/kvm/mmu/mmu.c:5065 vmx_handle_exit+0x26/0x160 arch/x86/kvm/vmx/vmx.c:6122 vcpu_enter_guest+0x3bdd/0x9630 arch/x86/kvm/x86.c:9428 vcpu_run+0x416/0xc20 arch/x86/kvm/x86.c:9494 kvm_arch_vcpu_ioctl_run+0x4e8/0xa40 arch/x86/kvm/x86.c:9722 kvm_vcpu_ioctl+0x70f/0xbb0 arch/x86/kvm/../../../virt/kvm/kvm_main.c:3460 vfs_ioctl fs/ioctl.c:51 [inline] __do_sys_ioctl fs/ioctl.c:1069 [inline] __se_sys_ioctl+0xfb/0x170 fs/ioctl.c:1055 do_syscall_64+0x3f/0xb0 arch/x86/entry/common.c:47 entry_SYSCALL_64_after_hwframe+0x44/0xae RIP: 0033:0x440ce9 Cc: stable@vger.kernel.org Reported-by: syzbot+fb0b6a7e8713aeb0319c@syzkaller.appspotmail.com Fixes: 9ec19493fb86 ("KVM: x86: clear SMM flags before loading state while leaving SMM") Signed-off-by: Sean Christopherson Message-Id: <20210609185619.992058-2-seanjc@google.com> Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/x86.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -6279,7 +6279,10 @@ static unsigned emulator_get_hflags(stru static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags) { - emul_to_vcpu(ctxt)->arch.hflags = emul_flags; + struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); + + vcpu->arch.hflags = emul_flags; + kvm_mmu_reset_context(vcpu); } static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt,