Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3556371pxb; Mon, 24 Jan 2022 12:08:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJwU8+FWXPJwsmGNAt770SkeLoDcJkkiMmOfey9CJRhRWf29K3RBhikC6fr2D7agofMVnQ6V X-Received: by 2002:a17:90a:2bcd:: with SMTP id n13mr3491373pje.155.1643054918323; Mon, 24 Jan 2022 12:08:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643054918; cv=none; d=google.com; s=arc-20160816; b=ribb+0YI80XYmZ0hmC5wdQSFZBEpsY9xgOGHX8hrwWaUHtzFNcD6BtyGcFycC0EW92 qgJYSIaXqhuHxVmzrrWVsBQf0/ESTgpXsK8zzg5hdt+Fgc3qn+7owvT6JWkpo8OeupfZ YpQe9hebijpcrn3WUsXdAAHq3oBri/UMW6aCmmAm+4TbvU7eV28jw4li/ty6bODqd7DI +vnabS2k8L1h9OBdxwNEO1FRm3cUvR9dFHkmK8HJGzQHzrXjH2CuEhoCJm94TXQCUKAQ SBOhJ1aZaDliyBCkqUuWE536zJ/tsrt6FYz8lHpeUoOnyFo9reV5Mj0aspOCXkszwXQD ZJwA== 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=Sckr0Z7mxv8Qc8ieKOCvc7hyffL/mdRGJx34bjGGFSY=; b=U/MZhoUrrN3VOqtklo7HCiyR1u4/Q2Zcjf/5dyxzj0bBQV2fVmuU8BT2vgxeO+eB54 fEY1oWRajZ9bkzw0i3ZlRQVeBs/9JMhpt3YgBScNgu8qbczHoBIssujcZoVM1N5cY6ly 6hLACPi1tlDiAWl7dsV3+89Kz4HNb1HFBhIvbupe2DJ7w9BXfuhddbl9Ni7ZncNufd4R Lv1kS7blxjXzqIOZQaRvXyq7Hs7iqIeUYWXthbBWQOe/ymz8vheGckiUZDmiqAMJ8VR8 RHSJNfcBFUgT+c1oGxme5TWBkS8EpJyqDb3gWgf0W4Gq99POs3nLtHDTigkngFdTgHEb XS5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=o2Eb4J0e; 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 j69si7882567pgd.507.2022.01.24.12.08.25; Mon, 24 Jan 2022 12:08:38 -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=o2Eb4J0e; 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 S1345238AbiAXTIx (ORCPT + 99 others); Mon, 24 Jan 2022 14:08:53 -0500 Received: from ams.source.kernel.org ([145.40.68.75]:59288 "EHLO ams.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345003AbiAXTCb (ORCPT ); Mon, 24 Jan 2022 14:02:31 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 2A3A1B8122C; Mon, 24 Jan 2022 19:02:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 36388C340E7; Mon, 24 Jan 2022 19:02:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643050948; bh=QaK4PqTesDZysa0f8zUBvmQkPai+vOO0lmowdkVMUL0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=o2Eb4J0eLdwaZfd2Ns5P2lNx43B5GVA9lI7+2eUYpl6e4ceeM3kcG8tPsEAHQRYhJ hOHUwAvOZRELv3FaNi3Jso6S1NWQVlu9GVnY0OXZEm0bCuOsaE3yFFC+tgJFotAM0T gpy8UCjn4UnI6ANhW66vxygr5EVkStSfMT2yVFUc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , David Stevens , 3pvd@google.com, Jann Horn , Jason Gunthorpe , Paolo Bonzini , Ovidiu Panait , Ben Hutchings Subject: [PATCH 4.9 155/157] KVM: do not assume PTE is writable after follow_pfn Date: Mon, 24 Jan 2022 19:44:05 +0100 Message-Id: <20220124183937.673291168@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124183932.787526760@linuxfoundation.org> References: <20220124183932.787526760@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 [OP: backport to 4.19, adjust follow_pte() -> follow_pte_pmd()] Signed-off-by: Ovidiu Panait Signed-off-by: Greg Kroah-Hartman [bwh: Backport to 4.9: follow_pte_pmd() does not take start or end parameters] Signed-off-by: Ben Hutchings 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 @@ -1519,9 +1519,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_pmd(vma->vm_mm, addr, &ptep, NULL, &ptl); if (r) { /* * get_user_pages fails for VM_IO and VM_PFNMAP vmas and does @@ -1536,14 +1538,19 @@ static int hva_to_pfn_remapped(struct vm if (r) return r; - r = follow_pfn(vma, addr, &pfn); + r = follow_pte_pmd(vma->vm_mm, addr, &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 @@ -1558,6 +1565,8 @@ static int hva_to_pfn_remapped(struct vm */ kvm_get_pfn(pfn); +out: + pte_unmap_unlock(ptep, ptl); *p_pfn = pfn; return 0; }