Blame SOURCES/CVE-2021-22543.patch

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