syzbot
KASAN: slab-use-after-free Read in mddev_unlock
Status:
moderation: reported on 2026/11/24 00:19
Subsystems:
raid
Labels:
prio:normal
[Documentation on labels]
Reported-by: syzbot+063b40ea90dd20f9e2e1@syzkaller.appspotmail.com
First crash: 5d00h, last: 5d00h
✨ 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 AI root cause analysis
Root Cause Analysis
The slab-use-after-free in mddev_unlock() (drivers/md/md.c:977) occurs because struct mddev can be freed via mddev_delayed_delete() while md_start_sync() (mddev->sync_work) is still executing mddev_unlock() and the remainder of md_start_sync().
Technical Mechanism
-
Unreferenced sync_work queueing on a concurrent workqueue:
In drivers/md/md.c, md_misc_wq is allocated with alloc_workqueue("md_misc", WQ_PERCPU, 0) (allowing multiple work items to execute concurrently across workers). When md_check_recovery() (drivers/md/md.c:10527, 10583) queues &mddev->sync_work (md_start_sync) onto md_misc_wq, it does not acquire an mddev->active reference (mddev_get()).
-
Premature MD_RECOVERY_RUNNING clear and post-unlock accesses in md_start_sync():
On the not_running: error/no-op path of md_start_sync() (drivers/md/md.c:10367-10388), MD_RECOVERY_RUNNING is cleared before mddev_unlock(mddev):
clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); // line 10372
mddev_unlock(mddev); // line 10373
Inside mddev_unlock(mddev) (drivers/md/md.c:929-1001), mutex_unlock(&mddev->reconfig_mutex) is executed at line 975, after which mddev_unlock() still accesses mddev:
md_wakeup_thread(mddev->thread) (line 977, offset 488 into struct mddev — the crashing read)
wake_up(&mddev->sb_wait) (line 978)
mddev->flags / mddev->kobj / mddev->gendisk checks (lines 986-999)
And after mddev_unlock() returns, md_start_sync() still accesses mddev in __mddev_resume(mddev, false) (line 10382) and test_and_clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery) / mddev->sysfs_action (lines 10386-10388).
-
Failure of stop_sync_thread() and mddev teardown to flush mddev->sync_work:
When stopping/disassembling an array (do_md_stop(mddev, 0) via STOP_ARRAY ioctl or array_state="clear"), do_md_stop() calls stop_sync_thread(mddev, true) (drivers/md/md.c:5198-5226):
- If
md_start_sync() has already cleared MD_RECOVERY_RUNNING (line 10372) and released reconfig_mutex (line 975), stop_sync_thread() sees !test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) at line 5202 and returns immediately without flushing mddev->sync_work.
- Even if
stop_sync_thread() entered while MD_RECOVERY_RUNNING was still set, it only checks if (work_pending(&mddev->sync_work)) flush_work(&mddev->sync_work); (lines 5216-5217). Once a worker has dequeued sync_work to run md_start_sync(), work_pending() returns false, so flush_work(&mddev->sync_work) is skipped; wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || ...) can then immediately observe MD_RECOVERY_RUNNING == 0 once line 10372 runs and wait on reconfig_mutex (mddev_lock_nointr(mddev) at line 5225).
- Note also that
md_ioctl() (drivers/md/md.c:8422-8423) only calls flush_work(&mddev->sync_work) when !md_is_rdwr(mddev).
-
Concurrent mddev destruction while md_start_sync() is still in mddev_unlock():
As soon as md_start_sync() releases mddev->reconfig_mutex at drivers/md/md.c:975, do_md_stop() acquires reconfig_mutex, stops the personality (__md_stop_writes(), __md_stop()), cleans mddev (export_array(), md_clean(), clearing raid_disks, disks, ctime, and hold_active), and sets MD_DELETED. When the last open file descriptor on the block device closes, md_release() -> mddev_put() -> __mddev_put() (drivers/md/md.c:652-669) queues mddev->del_work (mddev_delayed_delete()) onto md_misc_wq. Because neither do_md_stop(), mddev_delayed_delete(), md_kobj_release(), nor mddev_free() flushes or cancels mddev->sync_work, mddev_delayed_delete() runs concurrently on another worker, calling kobject_put(&mddev->kobj) -> md_kobj_release() -> put_disk(mddev->gendisk) -> disk_release() -> md_free_disk() -> mddev_free(mddev) -> kfree(mddev) before kworker/1:2 executes md_wakeup_thread(mddev->thread) at drivers/md/md.c:977.
Sample crash report:
* Struck through repros no longer work on HEAD.