md: fix use-after-free of mddev in md_start_sync workqueue
Subsystems (2)
PATCH v3 1/1 md: fix use-after-free of mddev in md_start_sync workqueue
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.
--- drivers/md/dm-raid.c | 1 + drivers/md/md.c | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-)
drivers/md/dm-raid.c +1 -0
@@ -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); }
drivers/md/md.c +7 -3
@@ -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);
Signature · 1 line
2.43.0
[email protected]
AI reviewThank 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 --
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 <[email protected]>
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().
Quoted 5 lines
drivers/md/md.c +0
-0
drivers/md/md.c static void stop_sync_thread(struct mddev *mddev, bool locked) · line 5230
Quoted 9 lines
drivers/md/md.c +1
-2@@ -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 = NULLIn 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().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1