Linux RAID subsystem development
 help / color / mirror / Atom feed
* [PATCH v3] md: fix use-after-free of mddev in md_start_sync workqueue
@ 2026-11-26  0:45 Shuai Liu
  2026-11-26  1:06 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Shuai Liu @ 2026-11-26  0:45 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. mddev can be freed by another path that never
flushes sync_work, leading to use-after-free in md_start_sync().

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
  Freed by task 5627: disk_release -> mddev_free -> kfree(mddev)
  Last related work creation:
    md_check_recovery() queue_work(md_misc_wq, sync_work)

commit db5e653d7c9f ("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. Prior to this the sync_work function barely touched
mddev and the missing reference was harmless.

Fix this by taking an mddev reference before queueing sync_work and
releasing it in md_start_sync(). Three complementary fixes close
remaining race windows:

 - stop_sync_thread(): remove work_pending() guard so that running
   sync_work is always flushed (workqueue core clears PENDING before
   invoking callbacks, so work_pending() is false for running work).
   The !MD_RECOVERY_RUNNING early return is preserved to avoid setting
   MD_RECOVERY_INTR on idle arrays and to handle locked=false correctly.

 - md_check_recovery(): add MD_RECOVERY_FROZEN guard to the read-only
   branch so it matches the main branch.

 - dm-raid raid_dtr(): flush mddev->sync_work before raid_set_free().
   dm-raid embeds mddev in raid_set and frees it directly, bypassing
   mddev_free()'s mddev->active accounting. raid_dtr also does not
   invoke stop_sync_thread() via md_stop(), so the original race
   is not protected by either mechanism.

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/dm-raid.c |  1 +
 drivers/md/md.c      | 10 +++++++---
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/md/dm-raid.c b/drivers/md/dm-raid.c
index e44d92462affc..2b7cd9488eec0 100644
--- a/drivers/md/dm-raid.c
+++ b/drivers/md/dm-raid.c
@@ -3336,6 +3336,7 @@ static void raid_dtr(struct dm_target *ti)
 
 	if (rs->md.event_work.func)
 		flush_work(&rs->md.event_work);
+	flush_work(&rs->md.sync_work);
 	raid_set_free(rs);
 }
 
diff --git a/drivers/md/md.c b/drivers/md/md.c
index 03e35386a8028..28f436fb1bad7 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -5213,8 +5213,7 @@ 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);
+	flush_work(&mddev->sync_work);
 
 	wait_event(resync_wait,
 		   !test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
@@ -10362,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:
@@ -10386,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);
 }
 
 static void unregister_sync_thread(struct mddev *mddev)
@@ -10522,8 +10523,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 +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] 2+ messages in thread

end of thread, other threads:[~2026-11-26  1:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  0:45 [PATCH v3] md: fix use-after-free of mddev in md_start_sync workqueue Shuai Liu
2026-11-26  1:06 ` sashiko-bot

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