Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp281588pxb; Thu, 25 Feb 2021 02:18:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJzrvdxJn2ajqs0bSH2sLo3fWWwHLSsKTbbbFSQ8jYygckv2dG0Nqj02wRg8/iK/Gg2oJbDW X-Received: by 2002:a17:906:cb11:: with SMTP id lk17mr1866105ejb.405.1614248302675; Thu, 25 Feb 2021 02:18:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614248302; cv=none; d=google.com; s=arc-20160816; b=uoKqbcQfOfapk7zl5eAsjb0dQ9tmgskDEfusjpCBtQpcdVC4856+1T+RdU2hKujxNa o/yO3OGQYnPQJ8m2a9is9BuIpdqU1kwTHPEX9q5LJeq7NL3auust42ICTdDGSBD+rYoM m5WRmSJSyj+yYaKjEw1XGUSERUEcncZ9S6Pb3lEJ4VmDR999r1r2+qscgzu9au6vo0MM x+dw/hSmMgk8aLh5bHql1KB9xFO+PfMObEGHoD4pFIj4RbbOoqHH3zX5wEkMhRXoMfbl K2BaLSCTWulXCUcEODiaDp2VHG6J3vEJ1hAXzYp85V6gB+uUVgm0C7lEMFgIhTn8e+nU t92A== 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=nCjhEf7a+TOgjhvuhR8IzzZDMMZtctY2IKu9LLjoH1Q=; b=jNHJZ2dT0ftq2Z9PCU4RN35fmpVr1lBbor76iPP/6WDUhHd/d1TZ+K1CvZkARXuP9r s3eQOia3GxFeX5i3KsD7Y8eX+AcsaFoNxqCYxOwipf5T1VbG0epm+4z4V3XtFEcb+NIN 2Tx5hy8CiGTJobr3cBvetGsXaztlYMuGhGHKEBTh/4T6QkcNhEOI1CphHMkX8nAc8MMT VsBNcW917CRbiFr1KJk8VA8q0qNYY+UlizXAJquFp3i8NULYWM5NUeMX2t9dIVeBcauD La6gupiFDSf1uwhZmIz9QHcjaU8RwaKafzHj/Tk8B/Udk5vdmpSsfJQEGmQ8WUMtWsMl 72jw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=KoM6pJTE; 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 hr3si3219467ejc.380.2021.02.25.02.17.59; Thu, 25 Feb 2021 02:18:22 -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=KoM6pJTE; 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 S234435AbhBYKOe (ORCPT + 99 others); Thu, 25 Feb 2021 05:14:34 -0500 Received: from mail.kernel.org ([198.145.29.99]:35126 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233791AbhBYJ72 (ORCPT ); Thu, 25 Feb 2021 04:59:28 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 3776E64F1E; Thu, 25 Feb 2021 09:55:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614246917; bh=dc+mqs92eqr14fLxaCWbl3fx8EdlXbDOtaLnJ4ct3ic=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KoM6pJTEFo9KaU8edrRwxltCsZ94QejhsHXgo856bFx1KxZUKYrCbpHQdg/3UMCx9 tOlWa7dn6X436VaDCZ+E8iCcWFy0G+s5jcKw0SllNIt+mkjqwUHL7a3PvQvpmmXDBQ 5v8n8O5piCEJpIpZqO6K6IyvhjD/iEUFfOfnu6PE= 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.10 15/23] KVM: do not assume PTE is writable after follow_pfn Date: Thu, 25 Feb 2021 10:53:46 +0100 Message-Id: <20210225092517.255507506@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210225092516.531932232@linuxfoundation.org> References: <20210225092516.531932232@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 @@ -1889,9 +1889,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 @@ -1906,14 +1908,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 @@ -1928,6 +1935,8 @@ static int hva_to_pfn_remapped(struct vm */ kvm_get_pfn(pfn); +out: + pte_unmap_unlock(ptep, ptl); *p_pfn = pfn; return 0; }