Skip to content

Commit 60d613b

Browse files
avihai1122gregkh
authored andcommitted
RDMA/ucma: Fix use-after-free bug in ucma_create_uevent
[ Upstream commit fe454dc ] ucma_process_join() allocates struct ucma_multicast mc and frees it if an error occurs during its run. Specifically, if an error occurs in copy_to_user(), a use-after-free might happen in the following scenario: 1. mc struct is allocated. 2. rdma_join_multicast() is called and succeeds. During its run, cma_iboe_join_multicast() enqueues a work that will later use the aforementioned mc struct. 3. copy_to_user() is called and fails. 4. mc struct is deallocated. 5. The work that was enqueued by cma_iboe_join_multicast() is run and calls ucma_create_uevent() which tries to access mc struct (which is freed by now). Fix this bug by cancelling the work enqueued by cma_iboe_join_multicast(). Since cma_work_handler() frees struct cma_work, we don't use it in cma_iboe_join_multicast() so we can safely cancel the work later. The following syzkaller report revealed it: BUG: KASAN: use-after-free in ucma_create_uevent+0x2dd/0x;3f0 drivers/infiniband/core/ucma.c:272 Read of size 8 at addr ffff88810b3ad110 by task kworker/u8:1/108 CPU: 1 PID: 108 Comm: kworker/u8:1 Not tainted 5.10.0-rc6+ #257 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.13.0-0-gf21b5a4aeb02-prebuilt.qemu.org 04/01/2014 Workqueue: rdma_cm cma_work_handler Call Trace: __dump_stack lib/dump_stack.c:77 [inline] dump_stack+0xbe/0xf9 lib/dump_stack.c:118 print_address_description.constprop.0+0x3e/0×60 mm/kasan/report.c:385 __kasan_report mm/kasan/report.c:545 [inline] kasan_report.cold+0x1f/0×37 mm/kasan/report.c:562 ucma_create_uevent+0x2dd/0×3f0 drivers/infiniband/core/ucma.c:272 ucma_event_handler+0xb7/0×3c0 drivers/infiniband/core/ucma.c:349 cma_cm_event_handler+0x5d/0×1c0 drivers/infiniband/core/cma.c:1977 cma_work_handler+0xfa/0×190 drivers/infiniband/core/cma.c:2718 process_one_work+0x54c/0×930 kernel/workqueue.c:2272 worker_thread+0x82/0×830 kernel/workqueue.c:2418 kthread+0x1ca/0×220 kernel/kthread.c:292 ret_from_fork+0x1f/0×30 arch/x86/entry/entry_64.S:296 Allocated by task 359: kasan_save_stack+0x1b/0×40 mm/kasan/common.c:48 kasan_set_track mm/kasan/common.c:56 [inline] __kasan_kmalloc mm/kasan/common.c:461 [inline] __kasan_kmalloc.constprop.0+0xc2/0xd0 mm/kasan/common.c:434 kmalloc include/linux/slab.h:552 [inline] kzalloc include/linux/slab.h:664 [inline] ucma_process_join+0x16e/0×3f0 drivers/infiniband/core/ucma.c:1453 ucma_join_multicast+0xda/0×140 drivers/infiniband/core/ucma.c:1538 ucma_write+0x1f7/0×280 drivers/infiniband/core/ucma.c:1724 vfs_write fs/read_write.c:603 [inline] vfs_write+0x191/0×4c0 fs/read_write.c:585 ksys_write+0x1a1/0×1e0 fs/read_write.c:658 do_syscall_64+0x2d/0×40 arch/x86/entry/common.c:46 entry_SYSCALL_64_after_hwframe+0x44/0xa9 Freed by task 359: kasan_save_stack+0x1b/0×40 mm/kasan/common.c:48 kasan_set_track+0x1c/0×30 mm/kasan/common.c:56 kasan_set_free_info+0x1b/0×30 mm/kasan/generic.c:355 __kasan_slab_free+0x112/0×160 mm/kasan/common.c:422 slab_free_hook mm/slub.c:1544 [inline] slab_free_freelist_hook mm/slub.c:1577 [inline] slab_free mm/slub.c:3142 [inline] kfree+0xb3/0×3e0 mm/slub.c:4124 ucma_process_join+0x22d/0×3f0 drivers/infiniband/core/ucma.c:1497 ucma_join_multicast+0xda/0×140 drivers/infiniband/core/ucma.c:1538 ucma_write+0x1f7/0×280 drivers/infiniband/core/ucma.c:1724 vfs_write fs/read_write.c:603 [inline] vfs_write+0x191/0×4c0 fs/read_write.c:585 ksys_write+0x1a1/0×1e0 fs/read_write.c:658 do_syscall_64+0x2d/0×40 arch/x86/entry/common.c:46 entry_SYSCALL_64_after_hwframe+0x44/0xa9 The buggy address belongs to the object at ffff88810b3ad100 which belongs to the cache kmalloc-192 of size 192 The buggy address is located 16 bytes inside of 192-byte region [ffff88810b3ad100, ffff88810b3ad1c0) Fixes: b5de0c6 ("RDMA/cma: Fix use after free race in roce multicast join") Link: https://lore.kernel.org/r/[email protected] Reported-by: Amit Matityahu <[email protected]> Signed-off-by: Avihai Horon <[email protected]> Signed-off-by: Leon Romanovsky <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 55bfe12 commit 60d613b

File tree

1 file changed

+41
-29
lines changed
  • drivers/infiniband/core

1 file changed

+41
-29
lines changed

drivers/infiniband/core/cma.c

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,13 @@ struct ib_device *cma_get_ib_dev(struct cma_device *cma_dev)
352352

353353
struct cma_multicast {
354354
struct rdma_id_private *id_priv;
355-
struct ib_sa_multicast *sa_mc;
355+
union {
356+
struct ib_sa_multicast *sa_mc;
357+
struct {
358+
struct work_struct work;
359+
struct rdma_cm_event event;
360+
} iboe_join;
361+
};
356362
struct list_head list;
357363
void *context;
358364
struct sockaddr_storage addr;
@@ -1823,6 +1829,8 @@ static void destroy_mc(struct rdma_id_private *id_priv,
18231829
cma_igmp_send(ndev, &mgid, false);
18241830
dev_put(ndev);
18251831
}
1832+
1833+
cancel_work_sync(&mc->iboe_join.work);
18261834
}
18271835
kfree(mc);
18281836
}
@@ -2683,6 +2691,28 @@ static int cma_query_ib_route(struct rdma_id_private *id_priv,
26832691
return (id_priv->query_id < 0) ? id_priv->query_id : 0;
26842692
}
26852693

2694+
static void cma_iboe_join_work_handler(struct work_struct *work)
2695+
{
2696+
struct cma_multicast *mc =
2697+
container_of(work, struct cma_multicast, iboe_join.work);
2698+
struct rdma_cm_event *event = &mc->iboe_join.event;
2699+
struct rdma_id_private *id_priv = mc->id_priv;
2700+
int ret;
2701+
2702+
mutex_lock(&id_priv->handler_mutex);
2703+
if (READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING ||
2704+
READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL)
2705+
goto out_unlock;
2706+
2707+
ret = cma_cm_event_handler(id_priv, event);
2708+
WARN_ON(ret);
2709+
2710+
out_unlock:
2711+
mutex_unlock(&id_priv->handler_mutex);
2712+
if (event->event == RDMA_CM_EVENT_MULTICAST_JOIN)
2713+
rdma_destroy_ah_attr(&event->param.ud.ah_attr);
2714+
}
2715+
26862716
static void cma_work_handler(struct work_struct *_work)
26872717
{
26882718
struct cma_work *work = container_of(_work, struct cma_work, work);
@@ -4478,10 +4508,7 @@ static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
44784508
cma_make_mc_event(status, id_priv, multicast, &event, mc);
44794509
ret = cma_cm_event_handler(id_priv, &event);
44804510
rdma_destroy_ah_attr(&event.param.ud.ah_attr);
4481-
if (ret) {
4482-
destroy_id_handler_unlock(id_priv);
4483-
return 0;
4484-
}
4511+
WARN_ON(ret);
44854512

44864513
out:
44874514
mutex_unlock(&id_priv->handler_mutex);
@@ -4604,7 +4631,6 @@ static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid,
46044631
static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
46054632
struct cma_multicast *mc)
46064633
{
4607-
struct cma_work *work;
46084634
struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
46094635
int err = 0;
46104636
struct sockaddr *addr = (struct sockaddr *)&mc->addr;
@@ -4618,10 +4644,6 @@ static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
46184644
if (cma_zero_addr(addr))
46194645
return -EINVAL;
46204646

4621-
work = kzalloc(sizeof *work, GFP_KERNEL);
4622-
if (!work)
4623-
return -ENOMEM;
4624-
46254647
gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
46264648
rdma_start_port(id_priv->cma_dev->device)];
46274649
cma_iboe_set_mgid(addr, &ib.rec.mgid, gid_type);
@@ -4632,10 +4654,9 @@ static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
46324654

46334655
if (dev_addr->bound_dev_if)
46344656
ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
4635-
if (!ndev) {
4636-
err = -ENODEV;
4637-
goto err_free;
4638-
}
4657+
if (!ndev)
4658+
return -ENODEV;
4659+
46394660
ib.rec.rate = iboe_get_rate(ndev);
46404661
ib.rec.hop_limit = 1;
46414662
ib.rec.mtu = iboe_get_mtu(ndev->mtu);
@@ -4653,24 +4674,15 @@ static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
46534674
err = -ENOTSUPP;
46544675
}
46554676
dev_put(ndev);
4656-
if (err || !ib.rec.mtu) {
4657-
if (!err)
4658-
err = -EINVAL;
4659-
goto err_free;
4660-
}
4677+
if (err || !ib.rec.mtu)
4678+
return err ?: -EINVAL;
4679+
46614680
rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
46624681
&ib.rec.port_gid);
4663-
work->id = id_priv;
4664-
INIT_WORK(&work->work, cma_work_handler);
4665-
cma_make_mc_event(0, id_priv, &ib, &work->event, mc);
4666-
/* Balances with cma_id_put() in cma_work_handler */
4667-
cma_id_get(id_priv);
4668-
queue_work(cma_wq, &work->work);
4682+
INIT_WORK(&mc->iboe_join.work, cma_iboe_join_work_handler);
4683+
cma_make_mc_event(0, id_priv, &ib, &mc->iboe_join.event, mc);
4684+
queue_work(cma_wq, &mc->iboe_join.work);
46694685
return 0;
4670-
4671-
err_free:
4672-
kfree(work);
4673-
return err;
46744686
}
46754687

46764688
int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,

0 commit comments

Comments
 (0)