Blame SOURCES/CVE-2020-36385.patch

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