Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp799673imu; Tue, 11 Dec 2018 07:47:12 -0800 (PST) X-Google-Smtp-Source: AFSGD/XHGtIZgYK2RN34Xn7LWvThK6yDApofsc6Yl9IcM9Bqpa1WFfhgtQ94O4zY9Dm2Yz1IBpjM X-Received: by 2002:a62:2a4b:: with SMTP id q72mr16547738pfq.61.1544543232743; Tue, 11 Dec 2018 07:47:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544543232; cv=none; d=google.com; s=arc-20160816; b=FVwkUmSD5lpERI6uefKjK5Ez6IonaEP9I6LeqAyozJsdavXL3e7MWsVV9fbmLjgQI/ TmLBmE20CmyJOg7UxbYu9fUILl4IAzOzzRgnVscbT77VhspBLQilTo23nDyVIIpu43ZK QC0r+SI4tMAfMG6ez+gyine1viP/uOLi0ejh0s8kVgyW02ElHKr4Djb2fnfywf5+uozm anmRVCps9lj8L05bzXnz/oZOk9nsQvY4XuxOytOmEUChMrc/rCxZze0UTka6Yfy5KovE h2I/+lVcWrVlvoaU6I3/bbD4SZSnsG1+TYim7CxKr3zp9pKAg39/YXnZ7AccnEOdnEc+ Cs/w== 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=vEjC4E6HJYqcGTLM612mdjXboAsiJFRjQAtlK8v9z0Y=; b=A1pIYlju+L4dLj3wxanNgbPQBb4VUgGM9jZsT4+h+ggsycHuOg7gvvDoC0K9ZsyFAU 2m9Otgc7jxkV9CHp7HaVSi2Zf31R+cwZyB5LaXgh0+4iP66o7Pa3pAKk7taA4d2aRFgD DvPHjqsujxhYP7uyBqFw9SU06VZukPmiRBW+02lUV67tOp4xiy7ydszKuODuyMR6nMHY m8BuF3POZEfiBaKrk1hvb9EPIx55w7wpKjyf+UPwHilx0fQT5AflgKN11qkrLe6lmksa +8LVWFom7ikl+5ReEEzioEIn05x1bUtfIaASWRFlLO0bWTue1UhdU/TV4qcFQzUfviw9 Yyww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=QTP9d5vU; 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 j34si12077862pgj.557.2018.12.11.07.46.58; Tue, 11 Dec 2018 07:47:12 -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=QTP9d5vU; 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 S1728054AbeLKPpP (ORCPT + 99 others); Tue, 11 Dec 2018 10:45:15 -0500 Received: from mail.kernel.org ([198.145.29.99]:33616 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728041AbeLKPpM (ORCPT ); Tue, 11 Dec 2018 10:45:12 -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 C38F3208E7; Tue, 11 Dec 2018 15:45:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1544543111; bh=pWtg1dJWwmJSsgr6uzjsILhBGLxJdFl+t2MJegVWmvo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QTP9d5vUDzaSe8m269VV+Z4r/rNGurwtxb9GUrYt2qzm4hYOP7HxmjqrdDkv19Z08 Q+rzUnqqA3iq0myr8G/YE/wrcFdZC1HDM01Vo2sqCRLz10fDjF/AGNJ0gfHjBVNRxU ST//EuIXRWjlWsTJNTBpXAOlgUmrATtTTu73tu4o= 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.4 06/91] kvm: mmu: Fix race in emulated page table writes Date: Tue, 11 Dec 2018 16:40:25 +0100 Message-Id: <20181211151606.515019216@linuxfoundation.org> X-Mailer: git-send-email 2.20.0 In-Reply-To: <20181211151606.026852373@linuxfoundation.org> References: <20181211151606.026852373@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.4-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 @@ -4174,9 +4174,9 @@ static void mmu_pte_write_flush_tlb(stru } 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; /* @@ -4188,22 +4188,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; @@ -4313,8 +4303,6 @@ void kvm_mmu_pte_write(struct kvm_vcpu * 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 @@ -4323,6 +4311,9 @@ void kvm_mmu_pte_write(struct kvm_vcpu * 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);