Blame SOURCES/CVE-2020-36385.patch

8ccb1f
From e77f229ecaf387f9f54430dbd277baf8c60b2716 Mon Sep 17 00:00:00 2001
8ccb1f
From: Joe Lawrence <joe.lawrence@redhat.com>
8ccb1f
Date: Wed, 27 Oct 2021 13:17:13 -0400
8ccb1f
Subject: [KPATCH CVE-2020-36385] RDMA/ucma: kpatch fixes for CVE-2020-36385
8ccb1f
8ccb1f
Kernels:
8ccb1f
3.10.0-1160.6.1.el7
8ccb1f
3.10.0-1160.11.1.el7
8ccb1f
3.10.0-1160.15.2.el7
8ccb1f
3.10.0-1160.21.1.el7
8ccb1f
3.10.0-1160.24.1.el7
8ccb1f
3.10.0-1160.25.1.el7
8ccb1f
3.10.0-1160.31.1.el7
8ccb1f
3.10.0-1160.36.2.el7
8ccb1f
3.10.0-1160.41.1.el7
8ccb1f
3.10.0-1160.42.2.el7
8ccb1f
3.10.0-1160.45.1.el7
8ccb1f
8ccb1f
Changes since last build:
8ccb1f
arches: x86_64 ppc64le
8ccb1f
ucma.o: changed function: ucma_migrate_id
8ccb1f
---------------------------
8ccb1f
8ccb1f
Kpatch-MR: https://gitlab.com/redhat/prdsc/rhel/src/kpatch/rhel-7/-/merge_requests/12
8ccb1f
Approved-by: Artem Savkov (@artem.savkov)
8ccb1f
Modifications:
8ccb1f
- Avoid the complications of reworking all the locks (and preceding
8ccb1f
commits) and apply a minimal patch to avoid the CVE condition.
8ccb1f
- Always inline ucma_unlock_files() to avoid new function on x64_64
8ccb1f
8ccb1f
Z-MR: https://gitlab.com/redhat/rhel/src/kernel/rhel-7/-/merge_requests/231
8ccb1f
8ccb1f
KT0 test PASS: https://beaker.engineering.redhat.com/jobs/5948342
8ccb1f
for kpatch-patch-3_10_0-1160_6_1-1-11.el7 scratch build:
8ccb1f
https://brewweb.engineering.redhat.com/brew/taskinfo?taskID=40661892
8ccb1f
8ccb1f
commit c71835cc23a3793651a693ea6cb1100e0eb9a0b1
8ccb1f
Author: Kamal Heib <kheib@redhat.com>
8ccb1f
Date:   Sun Aug 1 10:49:07 2021 +0300
8ccb1f
8ccb1f
    RDMA/ucma: Rework ucma_migrate_id() to avoid races with destroy
8ccb1f
8ccb1f
    Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1978075
8ccb1f
    CVE: CVE-2020-36385
8ccb1f
    Conflicts:
8ccb1f
    Adjust the patch to use "mut" mutext instead xa_lock due to the missing
8ccb1f
    of: afcafe07af0e ("ucma: Convert ctx_idr to XArray").
8ccb1f
8ccb1f
    commit f5449e74802c1112dea984aec8af7a33c4516af1
8ccb1f
    Author: Jason Gunthorpe <jgg@nvidia.com>
8ccb1f
    Date:   Mon Sep 14 08:59:56 2020 -0300
8ccb1f
8ccb1f
        RDMA/ucma: Rework ucma_migrate_id() to avoid races with destroy
8ccb1f
8ccb1f
        ucma_destroy_id() assumes that all things accessing the ctx will do so via
8ccb1f
        the xarray. This assumption violated only in the case the FD is being
8ccb1f
        closed, then the ctx is reached via the ctx_list. Normally this is OK
8ccb1f
        since ucma_destroy_id() cannot run concurrenty with release(), however
8ccb1f
        with ucma_migrate_id() is involved this can violated as the close of the
8ccb1f
        2nd FD can run concurrently with destroy on the first:
8ccb1f
8ccb1f
                        CPU0                      CPU1
8ccb1f
                ucma_destroy_id(fda)
8ccb1f
                                          ucma_migrate_id(fda -> fdb)
8ccb1f
                                               ucma_get_ctx()
8ccb1f
                xa_lock()
8ccb1f
                 _ucma_find_context()
8ccb1f
                 xa_erase()
8ccb1f
                xa_unlock()
8ccb1f
                                               xa_lock()
8ccb1f
                                                ctx->file = new_file
8ccb1f
                                                list_move()
8ccb1f
                                               xa_unlock()
8ccb1f
                                              ucma_put_ctx()
8ccb1f
8ccb1f
                                           ucma_close(fdb)
8ccb1f
                                              _destroy_id()
8ccb1f
                                              kfree(ctx)
8ccb1f
8ccb1f
                _destroy_id()
8ccb1f
                  wait_for_completion()
8ccb1f
                  // boom, ctx was freed
8ccb1f
8ccb1f
        The ctx->file must be modified under the handler and xa_lock, and prior to
8ccb1f
        modification the ID must be rechecked that it is still reachable from
8ccb1f
        cur_file, ie there is no parallel destroy or migrate.
8ccb1f
8ccb1f
        To make this work remove the double locking and streamline the control
8ccb1f
        flow. The double locking was obsoleted by the handler lock now directly
8ccb1f
        preventing new uevents from being created, and the ctx_list cannot be read
8ccb1f
        while holding fgets on both files. Removing the double locking also
8ccb1f
        removes the need to check for the same file.
8ccb1f
8ccb1f
        Fixes: 88314e4dda1e ("RDMA/cma: add support for rdma_migrate_id()")
8ccb1f
        Link: https://lore.kernel.org/r/0-v1-05c5a4090305+3a872-ucma_syz_migrate_jgg@nvidia.com
8ccb1f
        Reported-and-tested-by: syzbot+cc6fc752b3819e082d0c@syzkaller.appspotmail.com
8ccb1f
        Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
8ccb1f
8ccb1f
    Signed-off-by: Kamal Heib <kheib@redhat.com>
8ccb1f
8ccb1f
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
8ccb1f
---
8ccb1f
 drivers/infiniband/core/ucma.c | 11 ++++++++++-
8ccb1f
 1 file changed, 10 insertions(+), 1 deletion(-)
8ccb1f
8ccb1f
diff --git a/drivers/infiniband/core/ucma.c b/drivers/infiniband/core/ucma.c
8ccb1f
index 608a780d9ebb..72e7eb893d03 100644
8ccb1f
--- a/drivers/infiniband/core/ucma.c
8ccb1f
+++ b/drivers/infiniband/core/ucma.c
8ccb1f
@@ -1547,7 +1547,7 @@ static void ucma_lock_files(struct ucma_file *file1, struct ucma_file *file2)
8ccb1f
 	}
8ccb1f
 }
8ccb1f
 
8ccb1f
-static void ucma_unlock_files(struct ucma_file *file1, struct ucma_file *file2)
8ccb1f
+static __always_inline void ucma_unlock_files(struct ucma_file *file1, struct ucma_file *file2)
8ccb1f
 {
8ccb1f
 	if (file1 < file2) {
8ccb1f
 		mutex_unlock(&file2->mut);
8ccb1f
@@ -1610,6 +1610,14 @@ static ssize_t ucma_migrate_id(struct ucma_file *new_file,
8ccb1f
 	ucma_lock_files(cur_file, new_file);
8ccb1f
 	mutex_lock(&mut;;
8ccb1f
 
8ccb1f
+	/* CVE-2020-36385 kpatch: double check the context one last time */
8ccb1f
+	if (_ucma_find_context(cmd.id, cur_file) != ctx) {
8ccb1f
+		mutex_unlock(&mut;;
8ccb1f
+		ucma_unlock_files(cur_file, new_file);
8ccb1f
+		ret = -ENOENT;
8ccb1f
+		goto err_unlock;
8ccb1f
+	}
8ccb1f
+
8ccb1f
 	list_move_tail(&ctx->list, &new_file->ctx_list);
8ccb1f
 	ucma_move_events(ctx, new_file);
8ccb1f
 	ctx->file = new_file;
8ccb1f
@@ -1623,6 +1631,7 @@ response:
8ccb1f
 			 &resp, sizeof(resp)))
8ccb1f
 		ret = -EFAULT;
8ccb1f
 
8ccb1f
+err_unlock:
8ccb1f
 	ucma_put_ctx(ctx);
8ccb1f
 file_put:
8ccb1f
 	fdput(f);
8ccb1f
-- 
8ccb1f
2.26.3
8ccb1f
8ccb1f