* [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* Re: [PATCH v3] md: fix use-after-free of mddev in md_start_sync workqueue
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
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-26 1:06 UTC (permalink / raw)
To: Shuai Liu; +Cc: linux-raid, yukuai
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] md: stop_sync_thread() skips flush_work() after
MD_RECOVERY_RUNNING clears
--
commit cae588baa1080f65953ef30de3a815480f1b5d39
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, checks
MD_RECOVERY_FROZEN in the read-only branch of md_check_recovery(), and
flushes sync_work unconditionally in stop_sync_thread() and raid_dtr().
> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index 47da9edc1c606..dfad785da26b5 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -5224,8 +5224,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) ||
[Severity: Medium]
Can stop_sync_thread() still return without calling flush_work() while
md_start_sync() is running?
stop_sync_thread() returns early when MD_RECOVERY_RUNNING is clear:
drivers/md/md.c:stop_sync_thread() {
if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
if (!locked)
mddev_unlock(mddev);
return;
}
...
}
In md_start_sync(), the not_running path clears MD_RECOVERY_RUNNING
before calling mddev_unlock(), __mddev_resume(), and
sysfs_notify_dirent_safe(mddev->sysfs_action) (and on the normal path,
the newly woken sync_thread can finish and be reaped before
md_start_sync() reaches sysfs_notify_dirent_safe()). Once
reconfig_mutex is released in mddev_unlock(), a concurrent stop or
reconfiguration caller sees MD_RECOVERY_RUNNING clear and skips
flush_work(&mddev->sync_work):
kworker (md_start_sync) CPU1 (do_md_stop, mode == 2)
----------------------- ----------------------------
not_running:
clear_bit(MD_RECOVERY_RUNNING)
mddev_unlock(mddev)
mddev_lock(mddev)
__md_stop_writes(mddev)
stop_sync_thread(mddev, true)
!test_bit(MD_RECOVERY_RUNNING)
return; <-- skips flush_work()
mddev->to_remove = &md_redundancy_group
mddev_unlock(mddev)
sysfs_put(mddev->sysfs_action)
if (test_and_clear_bit(MD_RECOVERY_RECOVER) &&
mddev->sysfs_action)
sysfs_notify_dirent_safe(mddev->sysfs_action) <-- use-after-free
mddev->sysfs_action = NULL
In addition to the mddev->sysfs_action use-after-free, if
md_start_sync() suspended the array and has not yet reached
__mddev_resume(mddev, false), __md_stop() -> mddev_detach() sees
is_md_suspended(mddev) true and skips mddev->pers->quiesce().
--
Sashiko AI review · https://sashiko.dev/#/patchset/tencent_B6137BEEFCD26A2CCBF27D4DF0B81D622608@qq.com?part=1
^ permalink raw reply [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