1 From 5fe23f262e0548ca7f19fb79f89059a60d087d22 Mon Sep 17 00:00:00 2001
2 From: Cong Wang <xiyou.wangcong@gmail.com>
3 Date: Wed, 12 Sep 2018 16:27:44 -0700
4 Subject: ucma: fix a use-after-free in ucma_resolve_ip()
6 From: Cong Wang <xiyou.wangcong@gmail.com>
8 commit 5fe23f262e0548ca7f19fb79f89059a60d087d22 upstream.
10 There is a race condition between ucma_close() and ucma_resolve_ip():
13 ucma_resolve_ip(): ucma_close():
15 ctx = ucma_get_ctx(file, cmd.id);
17 list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) {
19 idr_remove(&ctx_idr, ctx->id);
25 rdma_destroy_id(ctx->cm_id);
29 ret = rdma_resolve_addr();
32 Before idr_remove(), ucma_get_ctx() could still find the ctx
33 and after rdma_destroy_id(), rdma_resolve_addr() may still
34 access id_priv pointer. Also, ucma_put_ctx() may use ctx after
37 ucma_close() should call ucma_put_ctx() too which tests the
38 refcnt and waits for the last one releasing it. The similar
39 pattern is already used by ucma_destroy_id().
41 Reported-and-tested-by: syzbot+da2591e115d57a9cbb8b@syzkaller.appspotmail.com
42 Reported-by: syzbot+cfe3c1e8ef634ba8964b@syzkaller.appspotmail.com
43 Cc: Jason Gunthorpe <jgg@mellanox.com>
44 Cc: Doug Ledford <dledford@redhat.com>
45 Cc: Leon Romanovsky <leon@kernel.org>
46 Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
47 Reviewed-by: Leon Romanovsky <leonro@mellanox.com>
48 Signed-off-by: Doug Ledford <dledford@redhat.com>
49 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
52 drivers/infiniband/core/ucma.c | 2 ++
53 1 file changed, 2 insertions(+)
55 --- a/drivers/infiniband/core/ucma.c
56 +++ b/drivers/infiniband/core/ucma.c
57 @@ -1759,6 +1759,8 @@ static int ucma_close(struct inode *inod
62 + wait_for_completion(&ctx->comp);
63 /* rdma_destroy_id ensures that no event handlers are
64 * inflight for that id before releasing it.