Blame SOURCES/CVE-2021-22543.patch

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