Blame SOURCES/CVE-2020-36385.patch

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