Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp8098139imu; Tue, 4 Dec 2018 02:59:14 -0800 (PST) X-Google-Smtp-Source: AFSGD/WQXEL8v29mYk/JsQeG9wqtd6KTe/GHwxVIXYyEW7qsO2MKABvJE0ykGNNR5/VTYrql0dme X-Received: by 2002:a17:902:145:: with SMTP id 63mr7505360plb.256.1543921153918; Tue, 04 Dec 2018 02:59:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543921153; cv=none; d=google.com; s=arc-20160816; b=UA59rFmMhaoU/iKC5cUx6wvn++2qvazdD4yOunZYLY0Xln0LiLIy1MJTpcylv5KDLA mABqOWECwK0GWn/bBGql9dkGcJfdR7agQT25r4wiH5i1LF+9nyE7KTnawmP1VZyXpvh/ Z76yP2qQYwC7Vgd7TBR/OgGRMCFtTpjfLsoQH0bs7bJ16+DdnlUxost3PIda3LEhO/1q xRlGxggngJJRLfvnOp++uy8dTHkcwE70vM1gehh7Zo6PLICmgxd6vV5XAJo7bTTGSqri 8jUPAjoKPqVa9SZO/9NyQY7v1OuwurpQLUB+f9nf/N39db/6aYswRAdMEH+DbZigDk0W 50zA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=2NGyjMBLACkJt0Wi9RKRWf2tePOVyT2sK6HMM7hZWRc=; b=uchFCxZpQzE5uN6MNOf5aUvGYmQZwsAdzxQ9a/0pjxlVHGjCWuX/dU0AZCJqSv82Er frNlVELisxcOmjLycuvBFfpBnZD0jqc9R/YDL3EvKyKbYCPCe55dVnci22/cs19CNKKK 9M3YnYH794CluoDifiAJrXoYcB5mshuy+MGPRfWFby7UEImq9OaAxmA+OEa3cJXEN9iy 9uGAhTuVH+FPrubYzhI5Nkz7JB0ZYlvEpeFiv3nglsctjbOG3BlgppFe94THhhZT++A4 qjKPic0027/ppKd31Km12exJ2xyPK+6szUqoEqSKhMNGMFIKuCvRrdT4BR3mD36rGJFL B5wQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=pcTlY8ZK; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 6si7056784plc.241.2018.12.04.02.58.58; Tue, 04 Dec 2018 02:59:13 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=pcTlY8ZK; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726799AbeLDK5j (ORCPT + 99 others); Tue, 4 Dec 2018 05:57:39 -0500 Received: from mail.kernel.org ([198.145.29.99]:41668 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726779AbeLDK5i (ORCPT ); Tue, 4 Dec 2018 05:57:38 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A5539214ED; Tue, 4 Dec 2018 10:57:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1543921057; bh=PaA2CqYsNYaISFZSki5ZPqSEOt8LyVWDbxRA/A/3zrA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pcTlY8ZKk3mCYQj+N6nMvcmGS6OjSzF4fuenmVW3SythGmem76nhjOnfntjt3vuKz TF1Q9J4tlmbw4vm+c0qoykxgPMMpM6tdni6U+Fl5xg1LiFNCVDuSbezYeNTmOVhOZd fhpxp0k1/YxEOdloYgYBF9pDRXHWN1GGmb4ZSkHw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Junaid Shahid , Wanpeng Li , Paolo Bonzini Subject: [PATCH 4.19 063/139] kvm: mmu: Fix race in emulated page table writes Date: Tue, 4 Dec 2018 11:49:04 +0100 Message-Id: <20181204103652.612267666@linuxfoundation.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181204103649.950154335@linuxfoundation.org> References: <20181204103649.950154335@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Junaid Shahid commit 0e0fee5c539b61fdd098332e0e2cc375d9073706 upstream. When a guest page table is updated via an emulated write, kvm_mmu_pte_write() is called to update the shadow PTE using the just written guest PTE value. But if two emulated guest PTE writes happened concurrently, it is possible that the guest PTE and the shadow PTE end up being out of sync. Emulated writes do not mark the shadow page as unsync-ed, so this inconsistency will not be resolved even by a guest TLB flush (unless the page was marked as unsync-ed at some other point). This is fixed by re-reading the current value of the guest PTE after the MMU lock has been acquired instead of just using the value that was written prior to calling kvm_mmu_pte_write(). Signed-off-by: Junaid Shahid Reviewed-by: Wanpeng Li Cc: stable@vger.kernel.org Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/mmu.c | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) --- a/arch/x86/kvm/mmu.c +++ b/arch/x86/kvm/mmu.c @@ -5013,9 +5013,9 @@ static bool need_remote_flush(u64 old, u } static u64 mmu_pte_write_fetch_gpte(struct kvm_vcpu *vcpu, gpa_t *gpa, - const u8 *new, int *bytes) + int *bytes) { - u64 gentry; + u64 gentry = 0; int r; /* @@ -5027,22 +5027,12 @@ static u64 mmu_pte_write_fetch_gpte(stru /* Handle a 32-bit guest writing two halves of a 64-bit gpte */ *gpa &= ~(gpa_t)7; *bytes = 8; - r = kvm_vcpu_read_guest(vcpu, *gpa, &gentry, 8); - if (r) - gentry = 0; - new = (const u8 *)&gentry; } - switch (*bytes) { - case 4: - gentry = *(const u32 *)new; - break; - case 8: - gentry = *(const u64 *)new; - break; - default: - gentry = 0; - break; + if (*bytes == 4 || *bytes == 8) { + r = kvm_vcpu_read_guest_atomic(vcpu, *gpa, &gentry, *bytes); + if (r) + gentry = 0; } return gentry; @@ -5146,8 +5136,6 @@ static void kvm_mmu_pte_write(struct kvm pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes); - gentry = mmu_pte_write_fetch_gpte(vcpu, &gpa, new, &bytes); - /* * No need to care whether allocation memory is successful * or not since pte prefetch is skiped if it does not have @@ -5156,6 +5144,9 @@ static void kvm_mmu_pte_write(struct kvm mmu_topup_memory_caches(vcpu); spin_lock(&vcpu->kvm->mmu_lock); + + gentry = mmu_pte_write_fetch_gpte(vcpu, &gpa, &bytes); + ++vcpu->kvm->stat.mmu_pte_write; kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);