Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3929175pxj; Mon, 21 Jun 2021 09:34:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxxuZFWa/7QGptkKJKa2S+q2iu0yNvVYs16IWzkRBHjvUR2PP8VYdlP2jayH2b9uLX6PrEH X-Received: by 2002:a17:906:d83:: with SMTP id m3mr11496705eji.63.1624293289266; Mon, 21 Jun 2021 09:34:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624293289; cv=none; d=google.com; s=arc-20160816; b=l0zFcyX9mJpKo6TJS8uyrNyYPBLVlgIhhkPtIaYpvij3bFFhznWHngGBY5lviOtDg5 H4s/Ey9et6V8hh9ipmdVRlkRRZ12/uXBTPD6wdPxjbstbKkrvZCTtD7lJo0CClY7Pm4u Dhx3fyoDAQhr1n+4h09UMtTV8Iqe6ck4gnszrRW5Yonpcdi1X9XkucyieepAO6LKDTZ/ GQoDMqap2NPlSpCm3PXRJkBfPnfoabfOajcq517GwtCJgPJR1qfu7Le9ueNvskds9FXC tV6h8kuO9QKnvcPabuE32afDmAtnfMxUsU7Gsy5lN3ojdUD/r8346TJT5qnKVA8HlRxU Vf+Q== 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=LIZQ3IHFliruoCT37YfxdeQ4wyD5yV/T8DvneT53pM8=; b=rCOgY5X8Ex0U2POKBpEE9p/NJWEqlrmB/QwZ66ZWuJ6PLhiMQLzgv0KuA0yFlE0Crv vbkY195te0D8C7PJAVrD6C0kRgT4hIlc5xvQbO+BlLJaeU/3q3XK0QMarkRHO4IkP7Zb sPqFDrwyyjOV6iD3ZLxyaC699ARuusB9OrlXNTtjp74J98ejbv4NISAiaUf+zwc7h1KS 19+4h32fBDQNo9xiKycf7Iu7N4Zy791W2bk9GySRJVigLtOOtkFhIhpxwSGfszJKPeKR qFoYMz7rABMtDfO13K2l1PqWvjgK2x3bkE+MuVYZqbSLo+rt+kn900N52oKQy8P5Bh/c K3EQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aW0eJZpe; 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 j3si4319954eds.548.2021.06.21.09.34.22; Mon, 21 Jun 2021 09:34:49 -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=aW0eJZpe; 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 S230217AbhFUQfk (ORCPT + 99 others); Mon, 21 Jun 2021 12:35:40 -0400 Received: from mail.kernel.org ([198.145.29.99]:54546 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232050AbhFUQbr (ORCPT ); Mon, 21 Jun 2021 12:31:47 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 6B99A613C3; Mon, 21 Jun 2021 16:25:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1624292743; bh=jvOsW68tgbkW5+x+IK3Kv5LjhYMgH/EU6PkD407QeIQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aW0eJZpeYeLDpOfLNiIHka5Z/7eijIzwju7tn9K7b9+0MIdZFrnr3b17L2zM6S3LS Rrmz6Q5x/caxAytSUtVzRbZV2cNdt8Hiv1qiawZYlyaeWyMa4F6U8U84mMbeo+3Xtf Qu4AJWaHJubcvlUmdWhEGGGAlJDcBbqE/evjtuao= 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.10 112/146] KVM: x86: Immediately reset the MMU context when the SMM flag is cleared Date: Mon, 21 Jun 2021 18:15:42 +0200 Message-Id: <20210621154918.477187591@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210621154911.244649123@linuxfoundation.org> References: <20210621154911.244649123@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 @@ -6876,7 +6876,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,