Blame SOURCES/CVE-2021-22543.patch

d46c83
From 9cb8a03ebca7263af03701ef256248ca8e5c434d Mon Sep 17 00:00:00 2001
d46c83
From: Artem Savkov <asavkov@redhat.com>
d46c83
Date: Wed, 7 Jul 2021 17:44:40 +0200
d46c83
Subject: [PATCH] KVM: do not allow mapping valid but non-reference-counted
d46c83
 pages
d46c83
d46c83
Kernels:
d46c83
4.18.0-305.el8
d46c83
4.18.0-305.3.1.el8_4
d46c83
4.18.0-305.7.1.el8_4
d46c83
d46c83
Changes since last build:
d46c83
[x86_64]:
d46c83
kvm_main.o: changed function: __gfn_to_pfn_memslot
d46c83
d46c83
[ppc64le]:
d46c83
kvm_main.o: changed function: gfn_to_pfn_memslot
d46c83
kvm_main.o: changed function: hva_to_pfn
d46c83
d46c83
---------------------------
d46c83
d46c83
Kernels:
d46c83
4.18.0-305.el8
d46c83
4.18.0-305.3.1.el8_4
d46c83
4.18.0-305.7.1.el8_4
d46c83
4.18.0-305.10.2.el8_4
d46c83
d46c83
Modifications: none
d46c83
Z-MR: https://gitlab.com/redhat/rhel/src/kernel/rhel-8/-/merge_requests/898
d46c83
Testing: kernel_write.c reproducer
d46c83
d46c83
commit 9871aa051ac11ab8551be01d7394915a5530dbbd
d46c83
Author: Jon Maloy <jmaloy@redhat.com>
d46c83
Date:   Wed Jun 30 21:09:40 2021 -0400
d46c83
d46c83
    KVM: do not allow mapping valid but non-reference-counted pages
d46c83
d46c83
    Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1975514
d46c83
    Upstream: commit f8be156be163a052a067306417cd0ff679068c97
d46c83
    CVE: CVE-2021-22543
d46c83
    Brew: https://brewweb.engineering.redhat.com/brew/taskinfo?taskID=37829337
d46c83
d46c83
    commit f8be156be163a052a067306417cd0ff679068c97
d46c83
    Author: Nicholas Piggin <npiggin@gmail.com>
d46c83
    Date:   Thu Jun 24 08:29:04 2021 -0400
d46c83
d46c83
        KVM: do not allow mapping valid but non-reference-counted pages
d46c83
d46c83
        It's possible to create a region which maps valid but non-refcounted
d46c83
        pages (e.g., tail pages of non-compound higher order allocations). These
d46c83
        host pages can then be returned by gfn_to_page, gfn_to_pfn, etc., family
d46c83
        of APIs, which take a reference to the page, which takes it from 0 to 1.
d46c83
        When the reference is dropped, this will free the page incorrectly.
d46c83
d46c83
        Fix this by only taking a reference on valid pages if it was non-zero,
d46c83
        which indicates it is participating in normal refcounting (and can be
d46c83
        released with put_page).
d46c83
d46c83
        This addresses CVE-2021-22543.
d46c83
d46c83
        Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
d46c83
        Tested-by: Paolo Bonzini <pbonzini@redhat.com>
d46c83
        Cc: stable@vger.kernel.org
d46c83
        Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
d46c83
d46c83
    Signed-off-by: Jon Maloy <jmaloy@redhat.com>
d46c83
d46c83
Signed-off-by: Artem Savkov <asavkov@redhat.com>
d46c83
Acked-by: Yannick Cote <ycote@redhat.com>
d46c83
Acked-by: Joe Lawrence <joe.lawrence@redhat.com>
d46c83
---
d46c83
 virt/kvm/kvm_main.c | 19 +++++++++++++++++--
d46c83
 1 file changed, 17 insertions(+), 2 deletions(-)
d46c83
d46c83
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
d46c83
index d4ec73c304a31..9f1cf429954c7 100644
d46c83
--- a/virt/kvm/kvm_main.c
d46c83
+++ b/virt/kvm/kvm_main.c
d46c83
@@ -1894,6 +1894,13 @@ static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault)
d46c83
 	return true;
d46c83
 }
d46c83
 
d46c83
+static int kvm_try_get_pfn(kvm_pfn_t pfn)
d46c83
+{
d46c83
+	if (kvm_is_reserved_pfn(pfn))
d46c83
+		return 1;
d46c83
+	return get_page_unless_zero(pfn_to_page(pfn));
d46c83
+}
d46c83
+
d46c83
 static int hva_to_pfn_remapped(struct vm_area_struct *vma,
d46c83
 			       unsigned long addr, bool *async,
d46c83
 			       bool write_fault, bool *writable,
d46c83
@@ -1936,11 +1943,19 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma,
d46c83
 	 * Whoever called remap_pfn_range is also going to call e.g.
d46c83
 	 * unmap_mapping_range before the underlying pages are freed,
d46c83
 	 * causing a call to our MMU notifier.
d46c83
+	 *
d46c83
+	 * Certain IO or PFNMAP mappings can be backed with valid
d46c83
+	 * struct pages, but be allocated without refcounting e.g.,
d46c83
+	 * tail pages of non-compound higher order allocations, which
d46c83
+	 * would then underflow the refcount when the caller does the
d46c83
+	 * required put_page. Don't allow those pages here.
d46c83
 	 */ 
d46c83
-	kvm_get_pfn(pfn);
d46c83
+	if (!kvm_try_get_pfn(pfn))
d46c83
+		r = -EFAULT;
d46c83
 
d46c83
 	*p_pfn = pfn;
d46c83
-	return 0;
d46c83
+
d46c83
+	return r;
d46c83
 }
d46c83
 
d46c83
 /*
d46c83
-- 
d46c83
2.26.3
d46c83