Blame SOURCES/CVE-2021-22543.patch

4a250b
From 710481f30b26856f462d3e5923bf69b32c12e097 Mon Sep 17 00:00:00 2001
4a250b
From: Artem Savkov <asavkov@redhat.com>
4a250b
Date: Fri, 3 Sep 2021 16:00:17 +0200
4a250b
Subject: [KPATCH CVE-2021-22543] KVM: do not allow mapping valid but
4a250b
 non-reference-counted pages
4a250b
4a250b
Kernels:
4a250b
3.10.0-1160.el7
4a250b
3.10.0-1160.2.1.el7
4a250b
3.10.0-1160.2.2.el7
4a250b
3.10.0-1160.6.1.el7
4a250b
3.10.0-1160.11.1.el7
4a250b
3.10.0-1160.15.2.el7
4a250b
3.10.0-1160.21.1.el7
4a250b
3.10.0-1160.24.1.el7
4a250b
3.10.0-1160.25.1.el7
4a250b
3.10.0-1160.31.1.el7
4a250b
3.10.0-1160.36.2.el7
4a250b
3.10.0-1160.41.1.el7
4a250b
4a250b
Changes since last build:
4a250b
[x86_64]:
4a250b
kvm_main.o: changed function: __gfn_to_pfn_memslot
4a250b
4a250b
[ppc64le]:
4a250b
kvm_main.o: changed function: gfn_to_page
4a250b
kvm_main.o: changed function: gfn_to_pfn
4a250b
kvm_main.o: changed function: gfn_to_pfn_memslot
4a250b
kvm_main.o: changed function: gfn_to_pfn_prot
4a250b
kvm_main.o: changed function: hva_to_pfn
4a250b
kvm_main.o: changed function: kvm_vcpu_gfn_to_page
4a250b
kvm_main.o: changed function: kvm_vcpu_gfn_to_pfn
4a250b
4a250b
---------------------------
4a250b
4a250b
Kernels:
4a250b
3.10.0-1160.2.1.el7
4a250b
3.10.0-1160.2.2.el7
4a250b
3.10.0-1160.6.1.el7
4a250b
3.10.0-1160.11.1.el7
4a250b
3.10.0-1160.15.2.el7
4a250b
3.10.0-1160.21.1.el7
4a250b
3.10.0-1160.24.1.el7
4a250b
3.10.0-1160.25.1.el7
4a250b
3.10.0-1160.31.1.el7
4a250b
3.10.0-1160.36.2.el7
4a250b
3.10.0-1160.41.1.el7
4a250b
3.10.0-1160.42.2.el7
4a250b
4a250b
Modifications: none
4a250b
Kpatch-MR: https://gitlab.com/kpatch-dev/rhel-7/-/merge_requests/7
4a250b
Approved-by: Yannick Cote (@ycote1)
4a250b
Approved-by: Joe Lawrence (@joe.lawrence)
4a250b
Z-MR: https://gitlab.com/redhat/rhel/src/kernel/rhel-7/-/merge_requests/259
4a250b
4a250b
commit 64b6dd5036622d9fab20cea237ae19402a1a2ee3
4a250b
Author: Jon Maloy <jmaloy@redhat.com>
4a250b
Date:   Tue Jul 13 15:28:38 2021 -0400
4a250b
4a250b
    KVM: do not allow mapping valid but non-reference-counted pages
4a250b
4a250b
    Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1975511
4a250b
    Upstream: commit f8be156be163a052a067306417cd0ff679068c97
4a250b
    CVE-2021-22543
4a250b
    Brew: https://brewweb.engineering.redhat.com/brew/taskinfo?taskID=39156005
4a250b
    Conflicts: The upstream version hva_to_pfn_remapped() has been upgraded
4a250b
               with lock support and a 'writeable' parameter. Those changes
4a250b
               entail a code conflict, but not functional conflict, with
4a250b
               this commit.
4a250b
4a250b
    commit f8be156be163a052a067306417cd0ff679068c97
4a250b
    Author: Nicholas Piggin <npiggin@gmail.com>
4a250b
    Date:   Thu Jun 24 08:29:04 2021 -0400
4a250b
4a250b
        KVM: do not allow mapping valid but non-reference-counted pages
4a250b
4a250b
        It's possible to create a region which maps valid but non-refcounted
4a250b
        pages (e.g., tail pages of non-compound higher order allocations). These
4a250b
        host pages can then be returned by gfn_to_page, gfn_to_pfn, etc., family
4a250b
        of APIs, which take a reference to the page, which takes it from 0 to 1.
4a250b
        When the reference is dropped, this will free the page incorrectly.
4a250b
4a250b
        Fix this by only taking a reference on valid pages if it was non-zero,
4a250b
        which indicates it is participating in normal refcounting (and can be
4a250b
        released with put_page).
4a250b
4a250b
        This addresses CVE-2021-22543.
4a250b
4a250b
        Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
4a250b
        Tested-by: Paolo Bonzini <pbonzini@redhat.com>
4a250b
        Cc: stable@vger.kernel.org
4a250b
        Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
4a250b
4a250b
    Signed-off-by: Jon Maloy <jmaloy@redhat.com>
4a250b
4a250b
Signed-off-by: Artem Savkov <asavkov@redhat.com>
4a250b
---
4a250b
 virt/kvm/kvm_main.c | 21 ++++++++++++++++++---
4a250b
 1 file changed, 18 insertions(+), 3 deletions(-)
4a250b
4a250b
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
4a250b
index 4b01a017b262..2f40d5fe257d 100644
4a250b
--- a/virt/kvm/kvm_main.c
4a250b
+++ b/virt/kvm/kvm_main.c
4a250b
@@ -1479,6 +1479,13 @@ static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault)
4a250b
 	return true;
4a250b
 }
4a250b
 
4a250b
+static int kvm_try_get_pfn(kvm_pfn_t pfn)
4a250b
+{
4a250b
+	if (kvm_is_reserved_pfn(pfn))
4a250b
+		return 1;
4a250b
+	return get_page_unless_zero(pfn_to_page(pfn));
4a250b
+}
4a250b
+
4a250b
 static int hva_to_pfn_remapped(struct vm_area_struct *vma,
4a250b
 			       unsigned long addr, bool *async,
4a250b
 			       bool write_fault, kvm_pfn_t *p_pfn)
4a250b
@@ -1514,11 +1521,19 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma,
4a250b
 	 * Whoever called remap_pfn_range is also going to call e.g.
4a250b
 	 * unmap_mapping_range before the underlying pages are freed,
4a250b
 	 * causing a call to our MMU notifier.
4a250b
-	 */ 
4a250b
-	kvm_get_pfn(pfn);
4a250b
+	 *
4a250b
+	 * Certain IO or PFNMAP mappings can be backed with valid
4a250b
+	 * struct pages, but be allocated without refcounting e.g.,
4a250b
+	 * tail pages of non-compound higher order allocations, which
4a250b
+	 * would then underflow the refcount when the caller does the
4a250b
+	 * required put_page. Don't allow those pages here.
4a250b
+	 */
4a250b
 
4a250b
+	if (!kvm_try_get_pfn(pfn))
4a250b
+		r = -EFAULT;
4a250b
 	*p_pfn = pfn;
4a250b
-	return 0;
4a250b
+
4a250b
+	return r;
4a250b
 }
4a250b
 
4a250b
 /*
4a250b
-- 
4a250b
2.26.3
4a250b
4a250b