Skip to content

Commit 32f070a

Browse files
bonzinigregkh
authored andcommitted
KVM: do not assume PTE is writable after follow_pfn
commit bd2fae8 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 add6a0c ("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 2e2e373 ("KVM: Handle vma regions with no backing page", 2008-07-20), as they also did not check whether the PFN was writable. Fixes: 2e2e373 ("KVM: Handle vma regions with no backing page") Reported-by: David Stevens <stevensd@google.com> Cc: 3pvd@google.com Cc: Jann Horn <jannh@google.com> Cc: Jason Gunthorpe <jgg@ziepe.ca> Cc: stable@vger.kernel.org Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 78c7b24 commit 32f070a

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

virt/kvm/kvm_main.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,9 +1599,11 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma,
15991599
kvm_pfn_t *p_pfn)
16001600
{
16011601
unsigned long pfn;
1602+
pte_t *ptep;
1603+
spinlock_t *ptl;
16021604
int r;
16031605

1604-
r = follow_pfn(vma, addr, &pfn);
1606+
r = follow_pte(vma->vm_mm, addr, NULL, &ptep, NULL, &ptl);
16051607
if (r) {
16061608
/*
16071609
* get_user_pages fails for VM_IO and VM_PFNMAP vmas and does
@@ -1616,14 +1618,19 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma,
16161618
if (r)
16171619
return r;
16181620

1619-
r = follow_pfn(vma, addr, &pfn);
1621+
r = follow_pte(vma->vm_mm, addr, NULL, &ptep, NULL, &ptl);
16201622
if (r)
16211623
return r;
1624+
}
16221625

1626+
if (write_fault && !pte_write(*ptep)) {
1627+
pfn = KVM_PFN_ERR_RO_FAULT;
1628+
goto out;
16231629
}
16241630

16251631
if (writable)
1626-
*writable = true;
1632+
*writable = pte_write(*ptep);
1633+
pfn = pte_pfn(*ptep);
16271634

16281635
/*
16291636
* Get a reference here because callers of *hva_to_pfn* and
@@ -1638,6 +1645,8 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma,
16381645
*/
16391646
kvm_get_pfn(pfn);
16401647

1648+
out:
1649+
pte_unmap_unlock(ptep, ptl);
16411650
*p_pfn = pfn;
16421651
return 0;
16431652
}

0 commit comments

Comments
 (0)