Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
* [PATCH v2] RDMA/core: Fix slab-use-after-free in kernfs_find_ns()
@ 2026-11-28 11:21 Habil Eren Türker
  2026-11-28 11:29 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Habil Eren Türker @ 2026-11-28 11:21 UTC (permalink / raw)
  To: Jason Gunthorpe, Leon Romanovsky
  Cc: Jiri Pirko, Michael Margolin, Parav Pandit,
	linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org,
	syzbot+8c2fa00c27ecafd148f5@syzkaller.appspotmail.com,
	Habil Eren Türker

In drivers/infiniband/core/device.c, remove_one_compat_dev() unlocks
device->compat_devs_mutex immediately after erasing cdev from
the xarray. device_del(&cdev->dev) is performed outside the lock.

Function-ladder:

Thread A (nldev_dellink)              Thread B (cleanup_net)
========================              ======================
nldev_dellink()
  ib_unregister_device_and_put()
    __ib_unregister_device()
      disable_device()
        remove_compat_devs()
          remove_one_compat_dev()
            mutex_lock()
            xa_erase()
            mutex_unlock()
                                      cleanup_net()
                                        ops_undo_list()
                                          rdma_dev_exit_net()
                                            remove_compat_devs()
                                              remove_one_compat_dev()
                                                mutex_lock()
                                                xa_erase() /* NULL */
                                                mutex_unlock()
                                                return
                                        net_complete_free()
                                          kmem_cache_free(net) /* freed */
            device_del()
              device_remove_class_symlinks()
                kernfs_remove_by_name_ns()
                  kernfs_find_ns()
                    kernfs_name_hash()
                      kernfs_ns_id()
                        ns->ns_id /* USE-AFTER-FREE */

KASAN report:

==================================================================
BUG: KASAN: slab-use-after-free in kernfs_ns_id fs/kernfs/dir.c:321 [inline]
BUG: KASAN: slab-use-after-free in kernfs_name_hash fs/kernfs/dir.c:334 [inline]
BUG: KASAN: slab-use-after-free in kernfs_find_ns+0x116/0x800 fs/kernfs/dir.c:915
Read of size 8 at addr ffff888038ebd168 by task syz.0.0/5354

CPU: 0 UID: 0 PID: 5354 Comm: syz.0.0 Not tainted syzkaller #0 PREEMPT(full)
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009),
BIOS 1.16.3-debian-1.16.3-2 04/01/2014

Call Trace:
 <TASK>
 dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120
 print_address_description+0x55/0x1e0 mm/kasan/report.c:378
 print_report+0x58/0x70 mm/kasan/report.c:482
 kasan_report+0x117/0x150 mm/kasan/report.c:595
 kernfs_ns_id fs/kernfs/dir.c:321 [inline]
 kernfs_name_hash fs/kernfs/dir.c:334 [inline]
 kernfs_find_ns+0x116/0x800 fs/kernfs/dir.c:915
 kernfs_remove_by_name_ns+0x5c/0x140 fs/kernfs/dir.c:1793
 device_remove_class_symlinks+0x178/0x190 drivers/base/core.c:3551
 device_del+0x400/0x8f0 drivers/base/core.c:3953
 remove_one_compat_dev drivers/infiniband/core/device.c:1036 [inline]
 remove_compat_devs drivers/infiniband/core/device.c:1047 [inline]
 disable_device+0x248/0x330 drivers/infiniband/core/device.c:1339
 __ib_unregister_device+0x2c9/0x400 drivers/infiniband/core/device.c:1571
 ib_unregister_device_and_put+0xb8/0xf0 drivers/infiniband/core/device.c:1636
 nldev_dellink+0x3a4/0x440 drivers/infiniband/core/nldev.c:1905
 rdma_nl_rcv_msg drivers/infiniband/core/netlink.c:-1 [inline]
 rdma_nl_rcv_skb drivers/infiniband/core/netlink.c:239 [inline]
 rdma_nl_rcv+0x6ef/0xa40 drivers/infiniband/core/netlink.c:259
 netlink_unicast_kernel net/netlink/af_netlink.c:1336 [inline]
 netlink_unicast+0x8ef/0xa80 net/netlink/af_netlink.c:1362
 netlink_sendmsg+0x7f0/0xb20 net/netlink/af_netlink.c:1921
 sock_sendmsg_nosec+0x14e/0x190 net/socket.c:800
 __sock_sendmsg net/socket.c:818 [inline]
 ____sys_sendmsg+0x49f/0x790 net/socket.c:2713
 ___sys_sendmsg+0x2f8/0x3d0 net/socket.c:2770
 __sys_sendmsg net/socket.c:2802 [inline]
 __do_sys_sendmsg net/socket.c:2807 [inline]
 __se_sys_sendmsg net/socket.c:2805 [inline]
 __x64_sys_sendmsg+0x1b1/0x290 net/socket.c:2805
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x171/0x540 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f7bccda94d9
Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89
      f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48>
      3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007f7bcdbfcfe8 EFLAGS: 00000246 ORIG_RAX: 000000000000002e
RAX: ffffffffffffffda RBX: 00007f7bcd026180 RCX: 00007f7bccda94d9
RDX: 0000000000000000 RSI: 0000200000000580 RDI: 0000000000000004
RBP: 00007f7bcce38136 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 00007f7bcd026218 R14: 00007f7bcd026180 R15: 00007ffe0b26c528
 </TASK>

Allocated by task 5352:
 kasan_save_stack mm/kasan/common.c:57 [inline]
 kasan_save_track+0x3e/0x80 mm/kasan/common.c:78
 unpoison_slab_object mm/kasan/common.c:351 [inline]
 __kasan_slab_alloc+0x6c/0x80 mm/kasan/common.c:377
 kasan_slab_alloc include/linux/kasan.h:253 [inline]
 slab_post_alloc_hook mm/slub.c:4720 [inline]
 slab_alloc_node mm/slub.c:5033 [inline]
 kmem_cache_alloc_noprof+0x2b4/0x600 mm/slub.c:5047
 net_alloc net/core/net_namespace.c:494 [inline]
 copy_net_ns+0x1de/0x780 net/core/net_namespace.c:571
 create_new_namespaces+0x3f0/0x6b0 kernel/nsproxy.c:132
 unshare_nsproxy_namespaces+0x144/0x190 kernel/nsproxy.c:234
 ksys_unshare+0x532/0x890 kernel/fork.c:3306
 __do_sys_unshare kernel/fork.c:3372 [inline]
 __se_sys_unshare kernel/fork.c:3370 [inline]
 __x64_sys_unshare+0x38/0x50 kernel/fork.c:3370
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x171/0x540 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Freed by task 45:
 kasan_save_stack mm/kasan/common.c:57 [inline]
 kasan_save_track+0x3e/0x80 mm/kasan/common.c:78
 kasan_save_free_info+0x40/0x50 mm/kasan/generic.c:584
 poison_slab_object mm/kasan/common.c:264 [inline]
 __kasan_slab_free+0x5c/0x80 mm/kasan/common.c:296
 kasan_slab_free include/linux/kasan.h:235 [inline]
 slab_free_hook mm/slub.c:2780 [inline]
 slab_free mm/slub.c:6630 [inline]
 kmem_cache_free+0x182/0x650 mm/slub.c:6757
 net_complete_free net/core/net_namespace.c:530 [inline]
 cleanup_net+0x5e3/0x810 net/core/net_namespace.c:722
 process_one_work kernel/workqueue.c:3495 [inline]
 process_scheduled_works+0xc3d/0x1630 kernel/workqueue.c:3578
 worker_thread+0xa47/0xfb0 kernel/workqueue.c:3659
 kthread+0x38b/0x480 kernel/kthread.c:436
 ret_from_fork+0x514/0xb70 arch/x86/kernel/process.c:158
 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245

The buggy address belongs to the object at ffff888038ebcf80
 which belongs to the cache net_namespace of size 9920
The buggy address is located 488 bytes inside of
 freed 9920-byte region [ffff888038ebcf80, ffff888038ebf640)

The buggy address belongs to the physical page:
page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x38eb8
head: order:3 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0
memcg:ffff888035b4a481
flags: 0x4fff00000000040(head|node=1|zone=1|lastcpupid=0x7ff)
page_type: f5(slab)
raw: 04fff00000000040 ffff88801dee6000 dead000000000122 0000000000000000
raw: 0000000000000000 0000000200030003 00000000f5000000 ffff888035b4a481
head: 04fff00000000040 ffff88801dee6000 dead000000000122 0000000000000000
head: 0000000000000000 0000000200030003 00000000f5000000 ffff888035b4a481
head: 04fff00000000003 fffffffffffffe01 00000000ffffffff 00000000ffffffff
head: ffffffffffffffff 0000000000000000 00000000ffffffff 0000000000000008
page dumped because: kasan: bad access detected
page_owner tracks the page as allocated
page last allocated via order 3, migratetype Unmovable,
 gfp_mask 0xd20c0(__GFP_IO|__GFP_FS|__GFP_NOWARN|__GFP_NORETRY|
  __GFP_COMP|__GFP_NOMEMALLOC), pid 5297, tgid 5297 (syz-executor),
  ts 105409296999

 set_page_owner include/linux/page_owner.h:33 [inline]
 post_alloc_hook+0x1f9/0x250 mm/page_alloc.c:1860
 prep_new_page mm/page_alloc.c:1868 [inline]
 get_page_from_freelist+0x225d/0x22e0 mm/page_alloc.c:3950
 __alloc_frozen_pages_noprof+0x211/0x450 mm/page_alloc.c:5492
 alloc_slab_page mm/slub.c:3384 [inline]
 allocate_slab+0x7d/0x5e0 mm/slub.c:3499
 new_slab mm/slub.c:3550 [inline]
 refill_objects+0x2d5/0x350 mm/slub.c:7539
 refill_sheaf mm/slub.c:2917 [inline]
 __pcs_replace_empty_main+0x2c8/0x6c0 mm/slub.c:4811
 alloc_from_pcs mm/slub.c:4887 [inline]
 slab_alloc_node mm/slub.c:5021 [inline]
 kmem_cache_alloc_noprof+0x394/0x600 mm/slub.c:5047
 net_alloc net/core/net_namespace.c:494 [inline]
 copy_net_ns+0x1de/0x780 net/core/net_namespace.c:571
 create_new_namespaces+0x3f0/0x6b0 kernel/nsproxy.c:132
 unshare_nsproxy_namespaces+0x144/0x190 kernel/nsproxy.c:234
 ksys_unshare+0x532/0x890 kernel/fork.c:3306
 __do_sys_unshare kernel/fork.c:3372 [inline]
 __se_sys_unshare kernel/fork.c:3370 [inline]
 __x64_sys_unshare+0x38/0x50 kernel/fork.c:3370
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x171/0x540 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
page last free pid 5011 tgid 5011 ts 69340115409 stack trace:
 reset_page_owner include/linux/page_owner.h:26 [inline]
 __free_pages_prepare mm/page_alloc.c:1402 [inline]
 __free_frozen_pages+0xd1a/0xe10 mm/page_alloc.c:2958
 __slab_free+0x269/0x2c0 mm/slub.c:5926
 qlink_free mm/kasan/quarantine.c:163 [inline]
 qlist_free_all+0x99/0x100 mm/kasan/quarantine.c:179
 kasan_quarantine_reduce+0x148/0x160 mm/kasan/quarantine.c:286
 __kasan_slab_alloc+0x22/0x80 mm/kasan/common.c:361
 kasan_slab_alloc include/linux/kasan.h:253 [inline]
 slab_post_alloc_hook mm/slub.c:4720 [inline]
 slab_alloc_node mm/slub.c:5033 [inline]
 __do_kmalloc_node mm/slub.c:5530 [inline]
 __kmalloc_noprof+0x302/0x710 mm/slub.c:5556
 _kmalloc_noprof include/linux/slab.h:995 [inline]
 tomoyo_realpath_from_path+0xf2/0x640 security/tomoyo/realpath.c:251
 tomoyo_get_realpath security/tomoyo/file.c:151 [inline]
 tomoyo_path_perm+0x283/0x560 security/tomoyo/file.c:827
 security_inode_getattr+0x12b/0x310 security/security.c:1918
 vfs_getattr fs/stat.c:263 [inline]
 vfs_fstat fs/stat.c:285 [inline]
 __do_sys_newfstat fs/stat.c:555 [inline]
 __se_sys_newfstat+0x133/0x440 fs/stat.c:550
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x171/0x540 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Memory state around the buggy address:
 ffff888038ebd000: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff888038ebd080: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
>ffff888038ebd100: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
                                                          ^
 ffff888038ebd180: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff888038ebd200: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
==================================================================

Fix this by moving mutex_unlock(&device->compat_devs_mutex) after
device_del(&cdev->dev) finishes, while keeping put_device(&cdev->dev)
outside the lock.

Fixes: 4e0f7b907072 ("RDMA/core: Implement compat device/sysfs tree in net namespace")
Reported-by: syzbot+8c2fa00c27ecafd148f5@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=8c2fa00c27ecafd148f5
Signed-off-by: Habil Eren Türker <habilerenturker@hotmail.com>
---
Changes in v2:
- Add KASAN report to the commit message.
- Add function-ladder to the commit message.
- Link to v1: https://lore.kernel.org/lkml/20261124163151.50468-2-habilerenturker@hotmail.com/

Note: LLM assistance was used for editing the commit description.
All changes were reviewed by the submitter.

 drivers/infiniband/core/device.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/infiniband/core/device.c b/drivers/infiniband/core/device.c
index 29af9fe860e2..f2a52af64718 100644
--- a/drivers/infiniband/core/device.c
+++ b/drivers/infiniband/core/device.c
@@ -1030,12 +1030,13 @@ static void remove_one_compat_dev(struct ib_device *device, u32 id)
 
 	mutex_lock(&device->compat_devs_mutex);
 	cdev = xa_erase(&device->compat_devs, id);
-	mutex_unlock(&device->compat_devs_mutex);
 	if (cdev) {
 		ib_free_port_attrs(cdev);
 		device_del(&cdev->dev);
-		put_device(&cdev->dev);
 	}
+	mutex_unlock(&device->compat_devs_mutex);
+	if (cdev)
+		put_device(&cdev->dev);
 }
 
 static void remove_compat_devs(struct ib_device *device)
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] RDMA/core: Fix slab-use-after-free in kernfs_find_ns()
  2026-11-28 11:21 [PATCH v2] RDMA/core: Fix slab-use-after-free in kernfs_find_ns() Habil Eren Türker
@ 2026-11-28 11:29 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-28 11:29 UTC (permalink / raw)
  To: Habil Eren Türker; +Cc: linux-rdma@vger.kernel.org

> In drivers/infiniband/core/device.c, remove_one_compat_dev() unlocks
> device->compat_devs_mutex immediately after erasing cdev from
> the xarray. device_del(&cdev->dev) is performed outside the lock.
> 
> Function-ladder:
> 
> Thread A (nldev_dellink)              Thread B (cleanup_net)
> ========================              ======================
> nldev_dellink()
>   ib_unregister_device_and_put()
>     __ib_unregister_device()
>       disable_device()
>         remove_compat_devs()
>           remove_one_compat_dev()
>             mutex_lock()
> [ ... ]
> 
> Fixes: 4e0f7b907072 ("RDMA/core: Implement compat device/sysfs tree in net namespace")
> Reported-by: syzbot+8c2fa00c27ecafd148f5@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=8c2fa00c27ecafd148f5
> Signed-off-by: Habil Eren Türker <habilerenturker@hotmail.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261128112149.7118-1-habilerenturker@hotmail.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-28 11:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28 11:21 [PATCH v2] RDMA/core: Fix slab-use-after-free in kernfs_find_ns() Habil Eren Türker
2026-11-28 11:29 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox