Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp275905pxb; Thu, 25 Feb 2021 02:06:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJw1acNdgaCE9L5C1vg1xmOshmj8oXTMWxvKroi3DNne7YtfoM4cz/b4JkvHYxDP5saj4brI X-Received: by 2002:a17:906:4f96:: with SMTP id o22mr1853175eju.511.1614247588791; Thu, 25 Feb 2021 02:06:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614247588; cv=none; d=google.com; s=arc-20160816; b=Tcg8Kn28lPq3hUlhYVZX70P76erfBg9qCaZtfJ17f/RTxdYVIL028VdZIF6TwTtPiF FKe8Pblwz9sLPtLM+lNAFHaYlPBQESTGyxG46ZYGzevmhvOL9vdi03w8PtLbSAZ563RP aP3wQNn5/8T8NX0RbGi2AkTgi4ROujKszNX2ThNzG2OGeQnOyfM+H/N3uJSPR/YcMvgQ 8c24B3ZWPp7z/W52tiWtus8MnhOHzkJMrl20V9wdTYOBKvF5hZoJ2KTpqOrZ7nDzRP4e eJedml1rR7jEhzilbShO4zhDKV7voOY+rPJ5W5hqHRhwfrwuoCpfoOgcQe6ype3D+Ydw d1bw== 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=el2QveCoDTfGEjdvwawqiXoBgFQBMTZLohnXiyO/Axk=; b=GMQlrnxCpL2fa8G5VOefKDe6yUkIC91lHTaJd2DhhdUWNLWFqx7YXGagjcDtDAItoP llivpcXWW7X9iPYbvcoo2vvCou2IMIhNhj5CCJFbiQAtuloWY5XgN1P3o6rZGOOltScj pZ+brV6RUUSlSKMAtXpoqN140tYKGmWn8aB1oWeScdefhniZvWWEjwjvBQsUYA//cwFP 5WD/Y1oNpMrz7cOdsBsJcjmJSbi6DTaSWXX6GZmFnkXiIAbxsGRXbASalwPz/9qVTt0B rRtj2Tye0ZT3s41pye2kFOoxb93CdNT1C4fTQO8Q5z29ubcZLuRtI3KHyAmLAcN/dCp6 NkOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=DtLoh3ry; 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 e6si3324731edz.362.2021.02.25.02.06.06; Thu, 25 Feb 2021 02:06:28 -0800 (PST) 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=DtLoh3ry; 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 S234493AbhBYJ4S (ORCPT + 99 others); Thu, 25 Feb 2021 04:56:18 -0500 Received: from mail.kernel.org ([198.145.29.99]:60932 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235489AbhBYJyi (ORCPT ); Thu, 25 Feb 2021 04:54:38 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id A9D1664ECE; Thu, 25 Feb 2021 09:53:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614246837; bh=XbJE3ajrQhiIyoghJaJ8dsQbDIkBCIFomDioDXnxT4w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DtLoh3rytclzlm4cgCqr0RjQvEjc5XBAn6Yb/XzFgBIPTfUxH3G/JesuKEuZ6wx3M KeRPnS2HifimzlP9RcaSCEWH6OiMvZGNj1QOO5Ej0/9btw126spebF0ybA33kMhBrW Q5WDERaXgrQKv+awZqKPbQVwCWheuTuNlxtvfEd4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, David Stevens , 3pvd@google.com, Jann Horn , Jason Gunthorpe , Paolo Bonzini Subject: [PATCH 5.11 10/12] KVM: do not assume PTE is writable after follow_pfn Date: Thu, 25 Feb 2021 10:53:44 +0100 Message-Id: <20210225092515.478386370@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210225092515.015261674@linuxfoundation.org> References: <20210225092515.015261674@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: Paolo Bonzini commit bd2fae8da794b55bf2ac02632da3a151b10e664c upstream. In order to convert an HVA to a PFN, KVM usually tries to use the get_user_pages family of functinso. This however is not possible for VM_IO vmas; in that case, KVM instead uses follow_pfn. In doing this however KVM loses the information on whether the PFN is writable. That is usually not a problem because the main use of VM_IO vmas with KVM is for BARs in PCI device assignment, however it is a bug. To fix it, use follow_pte and check pte_write while under the protection of the PTE lock. The information can be used to fail hva_to_pfn_remapped or passed back to the caller via *writable. Usage of follow_pfn was introduced in commit add6a0cd1c5b ("KVM: MMU: try to fix up page faults before giving up", 2016-07-05); however, even older version have the same issue, all the way back to commit 2e2e3738af33 ("KVM: Handle vma regions with no backing page", 2008-07-20), as they also did not check whether the PFN was writable. Fixes: 2e2e3738af33 ("KVM: Handle vma regions with no backing page") Reported-by: David Stevens Cc: 3pvd@google.com Cc: Jann Horn Cc: Jason Gunthorpe Cc: stable@vger.kernel.org Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- virt/kvm/kvm_main.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1904,9 +1904,11 @@ static int hva_to_pfn_remapped(struct vm kvm_pfn_t *p_pfn) { unsigned long pfn; + pte_t *ptep; + spinlock_t *ptl; int r; - r = follow_pfn(vma, addr, &pfn); + r = follow_pte(vma->vm_mm, addr, NULL, &ptep, NULL, &ptl); if (r) { /* * get_user_pages fails for VM_IO and VM_PFNMAP vmas and does @@ -1921,14 +1923,19 @@ static int hva_to_pfn_remapped(struct vm if (r) return r; - r = follow_pfn(vma, addr, &pfn); + r = follow_pte(vma->vm_mm, addr, NULL, &ptep, NULL, &ptl); if (r) return r; + } + if (write_fault && !pte_write(*ptep)) { + pfn = KVM_PFN_ERR_RO_FAULT; + goto out; } if (writable) - *writable = true; + *writable = pte_write(*ptep); + pfn = pte_pfn(*ptep); /* * Get a reference here because callers of *hva_to_pfn* and @@ -1943,6 +1950,8 @@ static int hva_to_pfn_remapped(struct vm */ kvm_get_pfn(pfn); +out: + pte_unmap_unlock(ptep, ptl); *p_pfn = pfn; return 0; }