Linux RAID subsystem development
 help / color / mirror / Atom feed
* [syzbot] [raid?] KASAN: slab-use-after-free Read in mddev_unlock
@ 2026-11-24  1:28 syzbot
  2026-11-25 10:12 ` [PATCH] md: fix use-after-free of mddev in md_start_sync workqueue Shuai Liu
  2026-11-25 10:34 ` [PATCH v2] " Shuai Liu
  0 siblings, 2 replies; 5+ messages in thread
From: syzbot @ 2026-11-24  1:28 UTC (permalink / raw)
  To: linux-kernel, linux-raid, magiclinan, song, syzkaller-bugs, xiao,
	xni, yukuai3, yukuai

Hello,

syzbot found the following issue on:

HEAD commit:    4405c18c95d2 Merge tag 'mm-nonmm-hotfixes-stable-2026-11-1..
git tree:       upstream
console output: https://syzkaller.appspot.com/x/log.txt?x=1346e803580000
kernel config:  https://syzkaller.appspot.com/x/.config?x=1cf189394a856f11
dashboard link: https://syzkaller.appspot.com/bug?extid=58beac388fb46ea52592
compiler:       gcc (Debian 14.2.0-19) 14.2.0, GNU ld (GNU Binutils for Debian) 2.44

Unfortunately, I don't have any reproducer for this issue yet.

Downloadable assets:
disk image: https://storage.googleapis.com/syzbot-assets/fadfa8ddb6dc/disk-4405c18c.raw.xz
vmlinux: https://storage.googleapis.com/syzbot-assets/b709babe0a48/vmlinux-4405c18c.xz
kernel image: https://storage.googleapis.com/syzbot-assets/a837f4b1ee01/bzImage-4405c18c.xz

The issue was bisected by AI to:

commit db5e653d7c9fae8ed61da58ab5e9a8db2cd61a2b
Author: Yu Kuai <yukuai3@huawei.com>
Date:   Fri Aug 25 03:16:18 2023 +0000

    md: delay choosing sync action to md_start_sync()

AI reasoning:   https://syzkaller.appspot.com/ai_job?id=bc5d8ea1-cd26-4470-b538-405195b1bbbf

AI-generated root cause summary:
In `drivers/md/md.c`, `md_check_recovery()` queues `mddev->sync_work`
(`md_start_sync()`) on `md_misc_wq` without taking a reference on `mddev`, and
`mddev` teardown fails to wait for `md_start_sync()` to finish before freeing
`mddev`. Specifically, `stop_sync_thread()` skips flushing `sync_work` when
`MD_RECOVERY_RUNNING` is already cleared or when `md_start_sync()` is already
running (`work_pending(&mddev->sync_work)` returns false), and `md_start_sync()`
clears `MD_RECOVERY_RUNNING` before calling `mddev_unlock()`. Once
`mddev_unlock()` releases `mddev->reconfig_mutex`, `do_md_stop()` can
immediately proceed to stop and clean the array, allowing `mddev_put()` ->
`mddev_delayed_delete()` -> `md_kobj_release()` -> `disk_release()` ->
`mddev_free()` to `kfree(mddev)` concurrently while `md_start_sync()` is still
inside `mddev_unlock()`, causing a slab-use-after-free when reading
`mddev->thread`.

full analysis:  https://syzkaller.appspot.com/x/ai_root_cause?x=58beac388fb46ea52592

IMPORTANT: if you fix the issue, please add the following tag to the commit:
Reported-by: syzbot+58beac388fb46ea52592@syzkaller.appspotmail.com

The cause commit was identified by AI and may be wrong.
If you agree with it, please also add:
Fixes: db5e653d7c9f ("md: delay choosing sync action to md_start_sync()")

==================================================================
BUG: KASAN: slab-use-after-free in mddev_unlock+0x909/0x9e0 drivers/md/md.c:977
Read of size 8 at addr ffff8880367b21e8 by task kworker/1:2/1568

CPU: 1 UID: 0 PID: 1568 Comm: kworker/1:2 Tainted: G             L      syzkaller #0 PREEMPT(full) 
Tainted: [L]=SOFTLOCKUP
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
Workqueue: md_misc md_start_sync
Call Trace:
 <TASK>
 __dump_stack lib/dump_stack.c:94 [inline]
 dump_stack_lvl+0x100/0x190 lib/dump_stack.c:120
 print_address_description mm/kasan/report.c:378 [inline]
 print_report+0x13d/0x4b0 mm/kasan/report.c:482
 kasan_report+0xdf/0x1c0 mm/kasan/report.c:595
 mddev_unlock+0x909/0x9e0 drivers/md/md.c:977
 md_start_sync+0x681/0xea0 drivers/md/md.c:10373
 process_one_work+0xac7/0x1b10 kernel/workqueue.c:3495
 process_scheduled_works kernel/workqueue.c:3578 [inline]
 worker_thread+0x5ef/0xe50 kernel/workqueue.c:3659
 kthread+0x373/0x450 kernel/kthread.c:436
 ret_from_fork+0x730/0xd60 arch/x86/kernel/process.c:158
 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
 </TASK>

Allocated by task 9718:
 kasan_save_stack+0x30/0x50 mm/kasan/common.c:57
 kasan_save_track+0x14/0x40 mm/kasan/common.c:79
 poison_kmalloc_redzone mm/kasan/common.c:412 [inline]
 __kasan_kmalloc+0xaa/0xb0 mm/kasan/common.c:429
 kasan_kmalloc include/linux/kasan.h:267 [inline]
 __kmalloc_cache_noprof+0x2f8/0x6d0 mm/slub.c:5680
 _kmalloc_noprof include/linux/slab.h:991 [inline]
 _kzalloc_noprof include/linux/slab.h:1312 [inline]
 mddev_alloc drivers/md/md.c:877 [inline]
 md_alloc+0xae/0x10c0 drivers/md/md.c:6356
 md_alloc_and_put drivers/md/md.c:6452 [inline]
 md_probe drivers/md/md.c:6468 [inline]
 md_probe+0x73/0xf0 drivers/md/md.c:6463
 blk_probe_dev+0x149/0x1e0 block/genhd.c:888
 blk_request_module+0x16/0xc0 block/genhd.c:901
 blkdev_get_no_open+0x9b/0xf0 block/bdev.c:870
 blkdev_open+0x141/0x4f0 block/fops.c:673
 do_dentry_open+0x674/0x1470 fs/open.c:1000
 vfs_open_consume+0xcc/0x460 fs/open.c:1131
 do_open fs/namei.c:4851 [inline]
 path_openat+0x1547/0x23f0 fs/namei.c:5016
 do_file_open+0x20e/0x430 fs/namei.c:5045
 do_sys_openat2+0x163/0x1d0 fs/open.c:1443
 do_sys_open fs/open.c:1449 [inline]
 __do_sys_openat fs/open.c:1465 [inline]
 __se_sys_openat fs/open.c:1460 [inline]
 __x64_sys_openat+0x12d/0x210 fs/open.c:1460
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x123/0x7b0 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Freed by task 5627:
 kasan_save_stack+0x30/0x50 mm/kasan/common.c:57
 kasan_save_track+0x14/0x40 mm/kasan/common.c:79
 kasan_save_free_info+0x3b/0x60 mm/kasan/generic.c:599
 poison_slab_object mm/kasan/common.c:267 [inline]
 __kasan_slab_free+0x73/0x90 mm/kasan/common.c:299
 kasan_slab_free include/linux/kasan.h:239 [inline]
 slab_free_hook mm/slub.c:2780 [inline]
 slab_free mm/slub.c:6637 [inline]
 kfree+0x22d/0x6d0 mm/slub.c:6930
 disk_release+0x2ad/0x490 block/genhd.c:1319
 device_release+0xd2/0x270 drivers/base/core.c:2640
 kobject_cleanup lib/kobject.c:689 [inline]
 kobject_release lib/kobject.c:720 [inline]
 kref_put include/linux/kref.h:65 [inline]
 kobject_put+0x1f7/0x640 lib/kobject.c:737
 process_one_work+0xac7/0x1b10 kernel/workqueue.c:3495
 process_scheduled_works kernel/workqueue.c:3578 [inline]
 worker_thread+0x5ef/0xe50 kernel/workqueue.c:3659
 kthread+0x373/0x450 kernel/kthread.c:436
 ret_from_fork+0x730/0xd60 arch/x86/kernel/process.c:158
 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245

Last potentially related work creation:
 kasan_save_stack+0x30/0x50 mm/kasan/common.c:57
 __kasan_record_aux_stack+0xba/0xd0 mm/kasan/generic.c:561
 insert_work+0x36/0x230 kernel/workqueue.c:2356
 __queue_work+0xac1/0x16f0 kernel/workqueue.c:2546
 queue_work_on+0x180/0x1e0 kernel/workqueue.c:2599
 queue_work include/linux/workqueue.h:700 [inline]
 __mddev_put drivers/md/md.c:668 [inline]
 __mddev_put drivers/md/md.c:652 [inline]
 mddev_put.part.0+0x132/0x160 drivers/md/md.c:682
 mddev_put drivers/md/md.c:8659 [inline]
 md_release+0x90/0xb0 drivers/md/md.c:8658
 blkdev_put_whole+0xb0/0xf0 block/bdev.c:766
 bdev_release+0x485/0x6e0 block/bdev.c:1189
 blkdev_release+0x15/0x20 block/fops.c:690
 __fput+0x3fb/0xb50 fs/file_table.c:512
 fput_close_sync+0x118/0x250 fs/file_table.c:618
 filp_close_sync+0x75/0xa0 fs/open.c:1574
 __range_close+0x4a6/0x8b0 fs/file.c:919
 __do_sys_close_range fs/file.c:1003 [inline]
 __se_sys_close_range fs/file.c:954 [inline]
 __x64_sys_close_range+0x1f8/0x430 fs/file.c:954
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x123/0x7b0 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Second to last potentially related work creation:
 kasan_save_stack+0x30/0x50 mm/kasan/common.c:57
 __kasan_record_aux_stack+0xba/0xd0 mm/kasan/generic.c:561
 insert_work+0x36/0x230 kernel/workqueue.c:2356
 __queue_work+0xac1/0x16f0 kernel/workqueue.c:2546
 queue_work_on+0x180/0x1e0 kernel/workqueue.c:2599
 queue_work include/linux/workqueue.h:700 [inline]
 md_check_recovery+0xbb9/0x1100 drivers/md/md.c:10583
 raid1d+0x114/0x7040 drivers/md/raid1.c:2617
 md_thread+0x550/0x5d0 drivers/md/md.c:8732
 kthread+0x373/0x450 kernel/kthread.c:436
 ret_from_fork+0x730/0xd60 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 ffff8880367b2000
 which belongs to the cache kmalloc-4k of size 4096
The buggy address is located 488 bytes inside of
 freed 4096-byte region [ffff8880367b2000, ffff8880367b3000)

The buggy address belongs to the physical page:
page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x367b0
head: order:3 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0
flags: 0xfff00000000040(head|node=0|zone=1|lastcpupid=0x7ff)
page_type: f5(slab)
raw: 00fff00000000040 ffff88813ff0e140 dead000000000100 dead000000000122
raw: 0000000000000000 0000000000040004 00000000f5000000 0000000000000000
head: 00fff00000000040 ffff88813ff0e140 dead000000000100 dead000000000122
head: 0000000000000000 0000000000040004 00000000f5000000 0000000000000000
head: 00fff00000000003 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 0xd2820(GFP_ATOMIC|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_NOMEMALLOC), pid 144, tgid 144 (kworker/u8:5), ts 201563767110
 set_page_owner include/linux/page_owner.h:33 [inline]
 post_alloc_hook+0xfd/0x120 mm/page_alloc.c:1873
 prep_new_page mm/page_alloc.c:1881 [inline]
 get_page_from_freelist+0xdae/0x2c80 mm/page_alloc.c:3963
 __alloc_frozen_pages_noprof+0x2ad/0x31e0 mm/page_alloc.c:5505
 alloc_slab_page mm/slub.c:3384 [inline]
 allocate_slab mm/slub.c:3499 [inline]
 new_slab+0xb1/0x6d0 mm/slub.c:3550
 refill_objects+0xe3/0x410 mm/slub.c:7546
 refill_sheaf mm/slub.c:2917 [inline]
 __pcs_replace_empty_main+0x35e/0x650 mm/slub.c:4811
 alloc_from_pcs mm/slub.c:4887 [inline]
 slab_alloc_node mm/slub.c:5021 [inline]
 __do_kmalloc_node mm/slub.c:5530 [inline]
 __kmalloc_node_track_caller_noprof+0x6a3/0x870 mm/slub.c:5662
 kmalloc_reserve+0xe8/0x350 net/core/skbuff.c:638
 __alloc_skb+0x185/0x710 net/core/skbuff.c:716
 alloc_skb include/linux/skbuff.h:1390 [inline]
 nsim_dev_trap_skb_build drivers/net/netdevsim/dev.c:744 [inline]
 nsim_dev_trap_report drivers/net/netdevsim/dev.c:801 [inline]
 nsim_dev_trap_report_work+0x2af/0xd20 drivers/net/netdevsim/dev.c:847
 process_one_work+0xac7/0x1b10 kernel/workqueue.c:3495
 process_scheduled_works kernel/workqueue.c:3578 [inline]
 worker_thread+0x5ef/0xe50 kernel/workqueue.c:3659
 kthread+0x373/0x450 kernel/kthread.c:436
 ret_from_fork+0x730/0xd60 arch/x86/kernel/process.c:158
 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
page last free pid 9267 tgid 9267 ts 195396699479 stack trace:
 reset_page_owner include/linux/page_owner.h:26 [inline]
 __free_pages_prepare mm/page_alloc.c:1415 [inline]
 __free_frozen_pages+0x7b1/0x10f0 mm/page_alloc.c:2971
 qlink_free mm/kasan/quarantine.c:163 [inline]
 qlist_free_all+0x47/0xf0 mm/kasan/quarantine.c:179
 kasan_quarantine_reduce+0x1a0/0x1f0 mm/kasan/quarantine.c:286
 __kasan_slab_alloc+0x69/0x90 mm/kasan/common.c:364
 kasan_slab_alloc include/linux/kasan.h:257 [inline]
 slab_post_alloc_hook mm/slub.c:4720 [inline]
 slab_alloc_node mm/slub.c:5033 [inline]
 kmem_cache_alloc_noprof+0x269/0x6a0 mm/slub.c:5047
 new_handle fs/jbd2/transaction.c:457 [inline]
 jbd2__journal_start+0x194/0x6b0 fs/jbd2/transaction.c:485
 __ext4_journal_start_sb+0x314/0x610 fs/ext4/ext4_jbd2.c:126
 __ext4_journal_start fs/ext4/ext4_jbd2.h:238 [inline]
 ext4_dirty_inode+0xa1/0x130 fs/ext4/inode.c:7650
 __mark_inode_dirty+0x1f3/0x16e0 fs/fs-writeback.c:2751
 generic_update_time fs/inode.c:2274 [inline]
 file_update_time_flags+0x49c/0x520 fs/inode.c:2504
 ext4_page_mkwrite+0x30e/0x19e0 fs/ext4/inode.c:7838
 do_page_mkwrite+0x17a/0x430 mm/memory.c:3908
 do_shared_fault mm/memory.c:6212 [inline]
 do_fault+0x5f8/0x1780 mm/memory.c:6274
 do_pte_missing mm/memory.c:4797 [inline]
 handle_pte_fault mm/memory.c:6689 [inline]
 __handle_mm_fault+0x1858/0x2f40 mm/memory.c:6841
 handle_mm_fault+0x363/0xa30 mm/memory.c:7008
 do_user_addr_fault+0x59c/0x12f0 arch/x86/mm/fault.c:1343

Memory state around the buggy address:
 ffff8880367b2080: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff8880367b2100: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
>ffff8880367b2180: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
                                                          ^
 ffff8880367b2200: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff8880367b2280: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
==================================================================


---
This report is generated by a bot. It may contain errors.
See https://goo.gl/tpsmEJ for more information about syzbot.
syzbot engineers can be reached at syzkaller@googlegroups.com.

syzbot will keep track of this issue. See:
https://goo.gl/tpsmEJ#status for how to communicate with syzbot.
For information about AI bisection see: https://goo.gl/tpsmEJ#ai-bisection

If the report is already addressed, let syzbot know by replying with:
#syz fix: exact-commit-title

If you want to overwrite report's subsystems, reply with:
#syz set subsystems: new-subsystem
(See the list of subsystem names on the web dashboard)

If the report is a duplicate of another one, reply with:
#syz dup: exact-subject-of-another-report

If you want to undo deduplication, reply with:
#syz undup

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

* [PATCH] md: fix use-after-free of mddev in md_start_sync workqueue
  2026-11-24  1:28 [syzbot] [raid?] KASAN: slab-use-after-free Read in mddev_unlock syzbot
@ 2026-11-25 10:12 ` Shuai Liu
  2026-11-25 10:28   ` sashiko-bot
  2026-11-25 10:34 ` [PATCH v2] " Shuai Liu
  1 sibling, 1 reply; 5+ messages in thread
From: Shuai Liu @ 2026-11-25 10:12 UTC (permalink / raw)
  To: song, yukuai
  Cc: magiclinan, xiao, syzbot+58beac388fb46ea52592, linux-raid,
	Shuai Liu

md_check_recovery() queues mddev->sync_work on md_misc_wq without taking
a reference on mddev. The mddev can be freed by another path that does
not go through do_md_stop() -> stop_sync_thread() and therefore never
flushes sync_work.

KASAN reports:
  BUG: KASAN: slab-use-after-free Read in mddev_unlock+0x909/0x9e0
  Read of size 8 at addr ffff8880367b21e8
    (offset 488 inside kmalloc-4k)
  by task kworker/1:2/1568, workqueue md_misc md_start_sync

  Allocated by task 9718:
    md_alloc -> md_probe -> blkdev_open (open /dev/mdX)
  Freed by task 5627:
    disk_release -> kobject_put -> mddev_free -> kfree(mddev)
  Last related work creation:
    md_check_recovery() queue_work(md_misc_wq, sync_work)

The UAF happens because the mddev-free path does not serialize with
sync_work. md_release() calls mddev_put() which may queue
mddev->del_work when mddev->active hits zero. del_work runs on
md_misc_wq and proceeds via
  kobject_release -> disk_release -> mddev_free -> kfree(mddev)
Meanwhile md_start_sync() computes mddev via container_of() on the
work_struct it was handed, and after mddev_unlock() returns it still
dereferences mddev members such as mddev->thread.

Crucially, mddev_delayed_delete() does not wait for md_misc_wq work
to drain: __mddev_put() just queues del_work with queue_work(). The
only place that flushes sync_work is stop_sync_thread(), which is
only invoked from do_md_stop() and the md_idle/md_frozen_sync_thread
helpers - all of which require holding reconfig_mutex. md_release()
does not take reconfig_mutex at all, so it can race with an
in-flight sync_work.

commit db5e653d7c9fae8ed61da58ab5e9a8db2cd61a2b
  ("md: delay choosing sync action to md_start_sync()")
moved md_choose_sync_action(), mddev_lock_nointr() and
mddev_unlock() into md_start_sync() so that array reconfiguration
became independent of the daemon thread. This change caused
md_start_sync() to actually dereference mddev members after
acquiring the lock. Prior to that commit the sync_work function
barely touched mddev and the lack of a bump was harmless.

Fix this by taking an mddev reference before queueing sync_work in
both md_check_recovery() paths that use it, and releasing that
reference at every return point inside md_start_sync().

Checkpatch: 0 errors, 0 warnings on drivers/md/md.c.
Kernel build: full make on top of Linux 7.4-rc5 succeeds
              (bzImage ready). A syzbot-supplied C reproducer for this
              issue is unavailable, so this patch is verified by code
              review of the reference-accounting pattern and the
              upstream build.

Fixes: db5e653d7c9f ("md: delay choosing sync action to md_start_sync()")
Reported-by: syzbot+58beac388fb46ea52592@syzkaller.appspotmail.com
Closes: https://syzbot.org/bug?extid=58beac388fb46ea52592
Signed-off-by: Shuai Liu <847807867@qq.com>
---
 drivers/md/md.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/md/md.c b/drivers/md/md.c
index 03e35386a8028..4a40b4f358d84 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -10362,6 +10362,7 @@ static void md_start_sync(struct work_struct *ws)
 	md_wakeup_thread(mddev->sync_thread);
 	sysfs_notify_dirent_safe(mddev->sysfs_action);
 	md_new_event();
+	mddev_put(mddev);
 	return;
 
 not_running:
@@ -10386,6 +10387,7 @@ static void md_start_sync(struct work_struct *ws)
 	if (test_and_clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
 	    mddev->sysfs_action)
 		sysfs_notify_dirent_safe(mddev->sysfs_action);
+	mddev_put(mddev);
 }
 
 static void unregister_sync_thread(struct mddev *mddev)
@@ -10524,6 +10526,7 @@ void md_check_recovery(struct mddev *mddev)
 			 */
 			if (md_spares_need_change(mddev)) {
 				set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
+				atomic_inc(&mddev->active);
 				queue_work(md_misc_wq, &mddev->sync_work);
 			}
 
@@ -10580,6 +10583,7 @@ void md_check_recovery(struct mddev *mddev)
 
 		if (test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) &&
 		    !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
+			atomic_inc(&mddev->active);
 			queue_work(md_misc_wq, &mddev->sync_work);
 		} else {
 			clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
-- 
2.43.0


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

* Re: [PATCH] md: fix use-after-free of mddev in md_start_sync workqueue
  2026-11-25 10:12 ` [PATCH] md: fix use-after-free of mddev in md_start_sync workqueue Shuai Liu
@ 2026-11-25 10:28   ` sashiko-bot
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-25 10:28 UTC (permalink / raw)
  To: Shuai Liu; +Cc: linux-raid, yukuai

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] md: stop_sync_thread() still races with in-flight
  md_start_sync()
--

commit 956e002b3dd32e5ec98ffbeb74b91b8f0d27187f
Author: Shuai Liu <847807867@qq.com>

md: fix use-after-free of mddev in md_start_sync workqueue

This commit increments mddev->active before queueing mddev->sync_work in
md_check_recovery() and drops the reference via mddev_put() when
md_start_sync() finishes.

> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index 47da9edc1c606..4b9d7ba63efa6 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -10361,6 +10361,7 @@ static void md_start_sync(struct work_struct *ws)
>  	md_wakeup_thread(mddev->sync_thread);
>  	sysfs_notify_dirent_safe(mddev->sysfs_action);
>  	md_new_event();
> +	mddev_put(mddev);
>  	return;
>  
>  not_running:
> @@ -10385,6 +10386,7 @@ static void md_start_sync(struct work_struct *ws)
>  	if (test_and_clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
>  	    mddev->sysfs_action)
>  		sysfs_notify_dirent_safe(mddev->sysfs_action);
> +	mddev_put(mddev);
>  }

[Severity: High]
Can stop_sync_thread() still return while md_start_sync() is executing its
post-unlock tail?

drivers/md/md.c:stop_sync_thread() {
	...
	if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
		if (!locked)
			mddev_unlock(mddev);
		return;
	}

	mddev_unlock(mddev);

	set_bit(MD_RECOVERY_INTR, &mddev->recovery);\
	...
	if (work_pending(&mddev->sync_work))
		flush_work(&mddev->sync_work);
	...
}

Because the workqueue core clears WORK_STRUCT_PENDING_BIT before invoking
md_start_sync(), work_pending(&mddev->sync_work) is false once the work
callback is already running.  When md_start_sync() takes the not_running
path, it clears MD_RECOVERY_RUNNING before calling mddev_unlock(mddev), and
stop_sync_thread() also returns early without flushing sync_work whenever
MD_RECOVERY_RUNNING is already clear:

CPU0                                  CPU1 (md_misc_wq)
                                      md_start_sync()
                                        mddev_lock_nointr(mddev)
stop_sync_thread()
  mddev_unlock(mddev)
  set_bit(MD_RECOVERY_INTR, ...)
  work_pending(&mddev->sync_work)
    -> false
  wait_event(resync_wait,
    !test_bit(MD_RECOVERY_RUNNING))
                                        md_choose_sync_action() -> false
                                        goto not_running
                                        clear_bit(MD_RECOVERY_RUNNING, ...)
                                        mddev_unlock(mddev)
                                          mutex_unlock(&reconfig_mutex)
  mddev_lock_nointr(mddev)
  /* returns to caller */
  raid_dtr() / do_md_stop()
    raid_set_free(rs)
                                        wake_up(&resync_wait)
                                        mddev_put(mddev)  <-- use-after-free

In dm-raid, struct mddev is embedded in struct raid_set, and raid_dtr()
frees the raid_set via raid_set_free() without waiting on mddev->active, so
the in-flight md_start_sync() and the added mddev_put() access freed memory.
Even on standard md arrays where mddev->active delays freeing struct mddev
itself, do_md_stop() proceeds while md_start_sync() is still running, so
md_start_sync() can race with do_md_stop() tearing down mddev->sysfs_action
or setting MD_DELETED before md_start_sync() finishes mddev_unlock() and
__mddev_resume().  In addition, on a read-only array, md_check_recovery()
queues sync_work when md_spares_need_change() is true without checking
MD_RECOVERY_FROZEN, allowing a new sync_work to be queued while
stop_sync_thread() drops reconfig_mutex.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/tencent_FD9B3C56D33F6E8298C044857BB7342DCB06@qq.com?part=1

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

* [PATCH v2] md: fix use-after-free of mddev in md_start_sync workqueue
  2026-11-24  1:28 [syzbot] [raid?] KASAN: slab-use-after-free Read in mddev_unlock syzbot
  2026-11-25 10:12 ` [PATCH] md: fix use-after-free of mddev in md_start_sync workqueue Shuai Liu
@ 2026-11-25 10:34 ` Shuai Liu
  2026-11-25 10:52   ` sashiko-bot
  1 sibling, 1 reply; 5+ messages in thread
From: Shuai Liu @ 2026-11-25 10:34 UTC (permalink / raw)
  To: song, yukuai
  Cc: magiclinan, xiao, syzbot+58beac388fb46ea52592, linux-raid,
	Shuai Liu

md_check_recovery() queues mddev->sync_work on md_misc_wq without taking
a reference on mddev. The mddev can be freed by another path that does
not go through do_md_stop() -> stop_sync_thread() and therefore never
flushes sync_work.

KASAN reports:
  BUG: KASAN: slab-use-after-free Read in mddev_unlock+0x909/0x9e0
  Read of size 8 at addr ffff8880367b21e8
    (offset 488 inside kmalloc-4k)
  by task kworker/1:2/1568, workqueue md_misc md_start_sync

  Allocated by task 9718:
    md_alloc -> md_probe -> blkdev_open (open /dev/mdX)
  Freed by task 5627:
    disk_release -> kobject_put -> mddev_free -> kfree(mddev)
  Last related work creation:
    md_check_recovery() queue_work(md_misc_wq, sync_work)

The UAF happens because the mddev-free path does not serialize with
sync_work. md_release() calls mddev_put() which may queue
mddev->del_work when mddev->active hits zero. del_work runs on
md_misc_wq and proceeds via
  kobject_release -> disk_release -> mddev_free -> kfree(mddev)
Meanwhile md_start_sync() computes mddev via container_of() on the
work_struct it was handed, and after mddev_unlock() returns it still
dereferences mddev members such as mddev->thread.

Crucially, mddev_delayed_delete() does not wait for md_misc_wq work
to drain: __mddev_put() just queues del_work with queue_work(). The
only place that flushes sync_work is stop_sync_thread(), which is
only invoked from do_md_stop() and the md_idle/md_frozen_sync_thread
helpers - all of which require holding reconfig_mutex. md_release()
does not take reconfig_mutex at all, so it can race with an
in-flight sync_work.

commit db5e653d7c9fae8ed61da58ab5e9a8db2cd61a2b
  ("md: delay choosing sync action to md_start_sync()")
moved md_choose_sync_action(), mddev_lock_nointr() and
mddev_unlock() into md_start_sync() so that array reconfiguration
became independent of the daemon thread. This change caused
md_start_sync() to actually dereference mddev members after
acquiring the lock. Prior to that commit the sync_work function
barely touched mddev and the lack of a bump was harmless.

The core fix: take an mddev reference before queueing sync_work and
release it at every return point in md_start_sync().

Two additional races surfaced when this patch was reviewed. They are
addressed in this same revision:

 1. stop_sync_thread() short-circuits on !MD_RECOVERY_RUNNING and
    on !work_pending(&mddev->sync_work). But the workqueue core
    clears WORK_STRUCT_PENDING_BIT before invoking the callback,
    so work_pending() is false while the work is running. And
    md_start_sync() used to clear MD_RECOVERY_RUNNING in the
    not_running path before mddev_unlock() completed, meaning
    stop_sync_thread() could observe !RUNNING and return while
    md_start_sync() was still mid-execution. Fix by removing the
    early return, making flush_work() unconditional, and moving
    clear_bit(MD_RECOVERY_RUNNING) + wake_up(&resync_wait) to the
    very end of md_start_sync() (immediately before mddev_put).

 2. The read-only branch of md_check_recovery() queued sync_work
    without checking MD_RECOVERY_FROZEN, unlike the main branch.
    Add the matching frozen guard.

Together these changes eliminate the original KASAN UAF and close
two remaining race windows that could otherwise be exploited via
do_md_stop(), raid_dtr (dm-raid uses raid_set which embeds mddev),
or md_stop_writes.

Checkpatch: 0 errors, 0 warnings on the diff.
Kernel build: make drivers/md/md.o succeeds on top of Linux 7.4-rc5.
A syzbot-supplied C reproducer is unavailable; this patch is
verified by the reference-accounting fix pattern and by the
compiler.

v2 changes since v1:
 - unconditional flush_work() in stop_sync_thread() instead of
   !RUNNING early return + work_pending() guard
 - move clear_bit(MD_RECOVERY_RUNNING) + wake_up into
   md_start_sync()'s not_running tail
 - add MD_RECOVERY_FROZEN guard to md_check_recovery's read-only
   branch

Fixes: db5e653d7c9f ("md: delay choosing sync action to md_start_sync()")
Reported-by: syzbot+58beac388fb46ea52592@syzkaller.appspotmail.com
Closes: https://syzbot.org/bug?extid=58beac388fb46ea52592
Signed-off-by: Shuai Liu <847807867@qq.com>
---
 drivers/md/md.c | 45 +++++++++++++++++++++++++++++++++------------
 1 file changed, 33 insertions(+), 12 deletions(-)

diff --git a/drivers/md/md.c b/drivers/md/md.c
index 03e35386a8028..d64aacbe51773 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -5199,13 +5199,13 @@ static void stop_sync_thread(struct mddev *mddev, bool locked)
 {
 	int sync_seq = atomic_read(&mddev->sync_seq);
 
-	if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
-		if (!locked)
-			mddev_unlock(mddev);
-		return;
-	}
-
-	mddev_unlock(mddev);
+	/*
+	 * Release reconfig_mutex before flushing sync_work or waiting on
+	 * resync_wait. md_start_sync() needs reconfig_mutex via
+	 * mddev_lock_nointr(), so holding it here would deadlock.
+	 */
+	if (locked)
+		mddev_unlock(mddev);
 
 	set_bit(MD_RECOVERY_INTR, &mddev->recovery);
 	/*
@@ -5213,8 +5213,18 @@ static void stop_sync_thread(struct mddev *mddev, bool locked)
 	 * never happen
 	 */
 	md_wakeup_thread_directly(&mddev->sync_thread);
-	if (work_pending(&mddev->sync_work))
-		flush_work(&mddev->sync_work);
+	/*
+	 * Always flush sync_work so that even an in-flight invocation
+	 * (already removed from the workqueue's pending list but not yet
+	 * returned from md_start_sync()) is drained. Previously this was
+	 * gated on work_pending() which is false for running work, and the
+	 * whole path could be short-circuited by MD_RECOVERY_RUNNING being
+	 * already clear — but md_start_sync() now only clears RUNNING after
+	 * its full tail, and unconditionally flushing handles any remaining
+	 * race where stop_sync_thread() was entered while sync_work was
+	 * already in flight.
+	 */
+	flush_work(&mddev->sync_work);
 
 	wait_event(resync_wait,
 		   !test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
@@ -10362,6 +10372,7 @@ static void md_start_sync(struct work_struct *ws)
 	md_wakeup_thread(mddev->sync_thread);
 	sysfs_notify_dirent_safe(mddev->sysfs_action);
 	md_new_event();
+	mddev_put(mddev);
 	return;
 
 not_running:
@@ -10369,7 +10380,6 @@ static void md_start_sync(struct work_struct *ws)
 	clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
 	clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
 	clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
-	clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
 	mddev_unlock(mddev);
 	/*
 	 * md_start_sync was triggered by MD_RECOVERY_NEEDED, so we should
@@ -10382,10 +10392,18 @@ static void md_start_sync(struct work_struct *ws)
 		__mddev_resume(mddev, false);
 	}
 
-	wake_up(&resync_wait);
 	if (test_and_clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
 	    mddev->sysfs_action)
 		sysfs_notify_dirent_safe(mddev->sysfs_action);
+	/*
+	 * Drop RUNNING only after the full tail of md_start_sync()
+	 * completes so that stop_sync_thread() can't observe !RUNNING and
+	 * return while md_start_sync() is still mid-execution. The put
+	 * follows so mddev remains alive while the flag is cleared.
+	 */
+	clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
+	wake_up(&resync_wait);
+	mddev_put(mddev);
 }
 
 static void unregister_sync_thread(struct mddev *mddev)
@@ -10522,8 +10540,10 @@ void md_check_recovery(struct mddev *mddev)
 			 * Let md_start_sync() to remove and add rdevs to the
 			 * array.
 			 */
-			if (md_spares_need_change(mddev)) {
+			if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery) &&
+			    md_spares_need_change(mddev)) {
 				set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
+				atomic_inc(&mddev->active);
 				queue_work(md_misc_wq, &mddev->sync_work);
 			}
 
@@ -10580,6 +10600,7 @@ void md_check_recovery(struct mddev *mddev)
 
 		if (test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) &&
 		    !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
+			atomic_inc(&mddev->active);
 			queue_work(md_misc_wq, &mddev->sync_work);
 		} else {
 			clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
-- 
2.43.0


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

* Re: [PATCH v2] md: fix use-after-free of mddev in md_start_sync workqueue
  2026-11-25 10:34 ` [PATCH v2] " Shuai Liu
@ 2026-11-25 10:52   ` sashiko-bot
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-25 10:52 UTC (permalink / raw)
  To: Shuai Liu; +Cc: linux-raid, yukuai

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] md: stop_sync_thread() skips mddev_unlock() when locked is
  false
- [Medium] md: clearing MD_RECOVERY_RUNNING unlocked in md_start_sync()
  loses recovery
- [Medium] md: stop_sync_thread() sets MD_RECOVERY_INTR when idle,
  breaking dm-raid
--

commit caec665a679c8ea640cd24d1ebf83696517d0cc9
Author: Shuai Liu <847807867@qq.com>

md: fix use-after-free of mddev in md_start_sync workqueue

This commit takes an mddev reference when queueing sync_work, flushes
sync_work unconditionally in stop_sync_thread(), moves the clearing of
MD_RECOVERY_RUNNING to the end of md_start_sync(), and checks
MD_RECOVERY_FROZEN in the read-only path of md_check_recovery().

> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index 47da9edc1c606..29b8eb3082ff5 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -5210,13 +5210,13 @@ static void stop_sync_thread(struct mddev *mddev, bool locked)
>  {
>  	int sync_seq = atomic_read(&mddev->sync_seq);
>  
> -	if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
> -		if (!locked)
> -			mddev_unlock(mddev);
> -		return;
> -	}
> -
> -	mddev_unlock(mddev);
> +	/*
> +	 * Release reconfig_mutex before flushing sync_work or waiting on
> +	 * resync_wait. md_start_sync() needs reconfig_mutex via
> +	 * mddev_lock_nointr(), so holding it here would deadlock.
> +	 */
> +	if (locked)
> +		mddev_unlock(mddev);

[Severity: High]
Does gating mddev_unlock(mddev) on locked leave reconfig_mutex held when
locked is false?

All callers of stop_sync_thread() enter with reconfig_mutex held; the
locked parameter indicates whether reconfig_mutex should be re-acquired
before returning or left unlocked.  When md_set_readonly() calls
stop_sync_thread(mddev, false), reconfig_mutex is never released:

drivers/md/md.c:md_set_readonly() {
    ...
	stop_sync_thread(mddev, false);
	wait_event(mddev->sb_wait,
		   !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
	mddev_lock_nointr(mddev);
    ...
}

Besides holding reconfig_mutex across flush_work(&mddev->sync_work) and
wait_event(resync_wait, ...), stop_sync_thread(mddev, false) returns with
reconfig_mutex still held and md_set_readonly() immediately deadlocks on
mddev_lock_nointr(mddev).  mddev_unlock(mddev) needs to be unconditional
here.

>  
>  	set_bit(MD_RECOVERY_INTR, &mddev->recovery);
>  	/*
> @@ -5224,8 +5224,18 @@ static void stop_sync_thread(struct mddev *mddev, bool locked)
>  	 * never happen
>  	 */
>  	md_wakeup_thread_directly(&mddev->sync_thread);
> -	if (work_pending(&mddev->sync_work))
> -		flush_work(&mddev->sync_work);
> +	/*
> +	 * Always flush sync_work so that even an in-flight invocation
> +	 * (already removed from the workqueue's pending list but not yet
> +	 * returned from md_start_sync()) is drained. Previously this was
> +	 * gated on work_pending() which is false for running work, and the
> +	 * whole path could be short-circuited by MD_RECOVERY_RUNNING being
> +	 * already clear — but md_start_sync() now only clears RUNNING after
> +	 * its full tail, and unconditionally flushing handles any remaining
> +	 * race where stop_sync_thread() was entered while sync_work was
> +	 * already in flight.
> +	 */
> +	flush_work(&mddev->sync_work);

[Severity: Medium]
Does removing the !MD_RECOVERY_RUNNING early return leave
MD_RECOVERY_INTR set on an idle array?

In dm-raid, rs_get_progress() checks MD_RECOVERY_INTR when the array is
idle to distinguish an in-sync array from an interrupted sync:

drivers/md/dm-raid.c:rs_get_progress() {
    ...
		if (state == st_idle && !test_bit(MD_RECOVERY_INTR, &recovery))
			r = mddev->resync_offset;
		else
			r = mddev->curr_resync_completed;
    ...
}

When an in-sync, idle dm-raid array is suspended via raid_postsuspend() ->
md_stop_writes() -> stop_sync_thread(), setting MD_RECOVERY_INTR with no
sync running leaves the bit set and causes dm-raid status to report
curr_resync_completed (0 if no sync ran since assembly) instead of
resync_offset.

[ ... ]

> @@ -10368,7 +10379,6 @@ static void md_start_sync(struct work_struct *ws)
>  	clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
>  	clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
>  	clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
> -	clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
>  	mddev_unlock(mddev);
>  	/*
>  	 * md_start_sync was triggered by MD_RECOVERY_NEEDED, so we should
> @@ -10381,10 +10391,18 @@ static void md_start_sync(struct work_struct *ws)
>  		__mddev_resume(mddev, false);
>  	}
>  
> -	wake_up(&resync_wait);
>  	if (test_and_clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
>  	    mddev->sysfs_action)
>  		sysfs_notify_dirent_safe(mddev->sysfs_action);
> +	/*
> +	 * Drop RUNNING only after the full tail of md_start_sync()
> +	 * completes so that stop_sync_thread() can't observe !RUNNING and
> +	 * return while md_start_sync() is still mid-execution. The put
> +	 * follows so mddev remains alive while the flag is cleared.
> +	 */
> +	clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
> +	wake_up(&resync_wait);
> +	mddev_put(mddev);
>  }

[Severity: Medium]
Can clearing MD_RECOVERY_RUNNING after mddev_unlock() and
__mddev_resume() race with md_check_recovery() and drop
MD_RECOVERY_NEEDED?

Commit db5e653d7c9f ("md: delay choosing sync action to md_start_sync()")
cleared MD_RECOVERY_RUNNING before mddev_unlock() in the not_running path
so that it was cleared under reconfig_mutex.  With clear_bit() moved to
the end of md_start_sync(), both mddev_unlock() and __mddev_resume() wake
mddev->thread while MD_RECOVERY_RUNNING is still set:

kworker (md_start_sync)           mddev->thread (md_check_recovery)
not_running:
  mddev_unlock(mddev)
    mutex_unlock(&reconfig_mutex)
    md_wakeup_thread(mddev->thread)
                                  mddev_trylock(mddev)
                                  if (test_bit(MD_RECOVERY_RUNNING, ...))
                                    unregister_sync_thread(mddev)
                                      if (!test_bit(MD_RECOVERY_DONE, ...))
                                        clear_bit(MD_RECOVERY_NEEDED, ...)
                                  mddev_unlock(mddev)
  clear_bit(MD_RECOVERY_RUNNING, ...)

Because MD_RECOVERY_DONE is clear, unregister_sync_thread() assumes a
sync thread is running and clears MD_RECOVERY_NEEDED, expecting
md_reap_sync_thread() to set it again when that thread finishes.  Since
md_start_sync() took the not_running path and never started a sync
thread, the pending recovery is lost.  Since stop_sync_thread() now
calls flush_work(&mddev->sync_work) before waiting on
MD_RECOVERY_RUNNING, MD_RECOVERY_RUNNING can stay cleared before
mddev_unlock().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/tencent_72E3228BE54C78B91F4B5BB1A4C84B34090A@qq.com?part=1

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

end of thread, other threads:[~2026-11-25 10:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24  1:28 [syzbot] [raid?] KASAN: slab-use-after-free Read in mddev_unlock syzbot
2026-11-25 10:12 ` [PATCH] md: fix use-after-free of mddev in md_start_sync workqueue Shuai Liu
2026-11-25 10:28   ` sashiko-bot
2026-11-25 10:34 ` [PATCH v2] " Shuai Liu
2026-11-25 10:52   ` sashiko-bot

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