scsi: ufs: core: disable exception work before removing hwmon
Subsystems (2)
PATCH 1/1 scsi: ufs: core: disable exception work before removing hwmon
ufshcd_remove() unregisters the hwmon device while hba->eeh_work can still be running. A SCSI completion can also queue hba->eeh_work later during scsi_remove_host(). When the worker runs ufs_hwmon_notify_event(), it can access the freed hwmon device and trigger a use-after-free.
Disable and drain hba->eeh_work in ufshcd_remove() before removing hwmon. This ensures any running handler finishes while hwmon is still present and prevents later SCSI completions during removal from requeuing the work.
---
Tested in QEMU/KVM (KASAN + PROVE_LOCKING) with an out-of-tree virtual UFS 3.1 platform controller driving the in-tree ufshcd-core.ko (no kprobes):
- Unbinding the device during an exception-event alert flood hits a KASAN slab-use-after-free in hwmon_notify_event() / kobject_uevent_env() from ufshcd_exception_event_handler() (freed by ufs_hwmon_remove() <- ufshcd_remove()) on the unpatched kernel, and passes 10-cycle A/B and 30-cycle stress runs with no KASAN, warning, or lockdep reports after the patch.
- Exception alerts on SYNCHRONIZE_CACHE during scsi_remove_host() requeue eeh_work on 10/10 cycles before the patch and 0/10 cycles after.
- Compile-tested with W=1 on x86_64 and arm64; verified clean apply on mkp/scsi.git for-next.
- Not tested on physical UFS hardware.
AI assistance: AI tools assisted with static code triage, writing the virtual UFS QEMU test module, and drafting the changelog; I reviewed and verified the root cause, teardown order, workqueue/lock behavior, and the final patch.
drivers/ufs/core/ufshcd.c | 1 + 1 file changed, 1 insertion(+)
drivers/ufs/core/ufshcd.c +1 -0
@@ -10988,6 +10988,7 @@ void ufshcd_remove(struct ufs_hba *hba) {if (hba->ufs_device_wlun)ufshcd_rpm_get_sync (hba); +disable_work_sync (&hba->eeh_work);ufs_hwmon_remove (hba);ufs_bsg_remove (hba);ufs_rpmb_remove (hba);
Signature · 1 line
2.34.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] scsi: ufs: core: use-after-free in…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] scsi: ufs: core: use-after-free in ufshcd_rpm_dev_flush_recheck_work() [1]
--
commit a1077f0758756bac493f25126be2d29218ae49d7
Author: Yaohan Li <[email protected]>
scsi: ufs: core: disable exception work before removing hwmon
This commit calls disable_work_sync(&hba->eeh_work) in ufshcd_remove() before unregistering hwmon so that running or newly queued exception work cannot access the freed hwmon device.
drivers/ufs/core/ufshcd.c void ufshcd_remove(struct ufs_hba *hba) · line 11027
Quoted 9 lines
drivers/ufs/core/ufshcd.c +1
-0@@ -11024,6 +11024,7 @@ void ufshcd_remove(struct ufs_hba *hba) {if (hba->ufs_device_wlun)ufshcd_rpm_get_sync (hba); +disable_work_sync (&hba->eeh_work);
[Severity: Critical]
This problem wasn't introduced by this patch, but can ufshcd_rpm_dev_flush_recheck_work() trigger a use-after-free on hba during ufshcd_remove() [1]?
ufshcd_rpm_get_sync() resumes the device via __ufshcd_wl_resume(), which only calls asynchronous cancel_delayed_work() on hba->rpm_dev_flush_recheck_work:
drivers/ufs/core/ufshcd.c:__ufshcd_wl_resume() {
...
if (hba->dev_info.b_rpm_dev_flush_capable) {
hba->dev_info.b_rpm_dev_flush_capable = false;
cancel_delayed_work(&hba->rpm_dev_flush_recheck_work);
}
...
}Because ufshcd_remove() does not call cancel_delayed_work_sync() on hba->rpm_dev_flush_recheck_work, an already running worker can outlive host teardown:
CPU0 CPU1ufshcd_remove()
ufshcd_rpm_dev_flush_recheck_work()
ufshcd_rpm_get_sync(hba)
__ufshcd_wl_resume()
cancel_delayed_work(...) ...
// hba is torn down and freed
ufshcd_rpm_put_sync(hba) <-- use-after-free
[1] https://sashiko.dev/#/bug/linux-6d2939f8-3e6b-4a28-a988-2094ecabbec6
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 5 lines · Yaohan Li
ufshcd_remove()unregisters the hwmon device whilehba->eeh_workcan still be running. A SCSI completion can also queuehba->eeh_worklater duringscsi_remove_host(). When the worker runsufs_hwmon_notify_event(), it can access the freed hwmon device and trigger a use-after-free.
Quoted 7 lines
Disable and drain
hba->eeh_workinufshcd_remove()before removing hwmon. This ensures any running handler finishes while hwmon is still present and prevents later SCSI completions during removal from requeuing the work.Fixes 66ec6d59407b ("[SCSI] ufs: Add support for host assisted backgroundoperations")
please double check this fix tag, should be e88e2d32200a, where hwmon came in?
Kind regards,
Bean
PATCH v2 1/1 scsi: ufs: core: disable exception work before removing hwmon
In reply to Yaohan Liufshcd_remove() unregisters the hwmon device while hba->eeh_work can still be running. A SCSI completion can also queue hba->eeh_work later during scsi_remove_host(). When the worker runs ufs_hwmon_notify_event(), it can access the freed hwmon device and trigger a use-after-free.
Disable and drain hba->eeh_work in ufshcd_remove() before removing hwmon. This ensures any running handler finishes while hwmon is still present and prevents later SCSI completions during removal from requeuing the work.
---
Changes in v2:
- Correct the Fixes tag to e88e2d32200a, as pointed out by Bean Huo.
- No code changes.
Tested in QEMU/KVM (KASAN + PROVE_LOCKING) with an out-of-tree virtual UFS 3.1 platform controller driving the in-tree ufshcd-core.ko (no kprobes):
- Unbinding the device during an exception-event alert flood hits a KASAN slab-use-after-free in hwmon_notify_event() / kobject_uevent_env() from ufshcd_exception_event_handler() (freed by ufs_hwmon_remove() <- ufshcd_remove()) on the unpatched kernel, and passes 10-cycle A/B and 30-cycle stress runs with no KASAN, warning, or lockdep reports after the patch.
- Exception alerts on SYNCHRONIZE_CACHE during scsi_remove_host() requeue eeh_work on 10/10 cycles before the patch and 0/10 cycles after.
- Compile-tested with W=1 on x86_64 and arm64; verified clean apply on mkp/scsi.git for-next.
- Not tested on physical UFS hardware.
AI assistance: AI tools assisted with static code triage, writing the virtual UFS QEMU test module, and drafting the changelog; I reviewed and verified the root cause, teardown order, workqueue/lock behavior, and the final patch.
drivers/ufs/core/ufshcd.c | 1 + 1 file changed, 1 insertion(+)
drivers/ufs/core/ufshcd.c +1 -0
@@ -10988,6 +10988,7 @@ void ufshcd_remove(struct ufs_hba *hba) {if (hba->ufs_device_wlun)ufshcd_rpm_get_sync (hba); +disable_work_sync (&hba->eeh_work);ufs_hwmon_remove (hba);ufs_bsg_remove (hba);ufs_rpmb_remove (hba);
Signature · 1 line
2.34.1