nvme-multipath: delete the ANATT timer after draining ana_work
PATCH 1/1 nvme-multipath: delete the ANATT timer after draining ana_work
nvme_mpath_stop() deletes the ANATT timer before cancelling ana_work. An ana_work that is already past its controller state check can still read the ANA log and arm the timer after the timer was deleted. The controller is then freed with the timer armed, and nvme_anatt_timeout() dereferences the freed controller when it fires.
Cancel ana_work before deleting the timer so it cannot be armed again. Also delete the timer in nvme_mpath_uninit() after draining ana_work:
commit 9b657c565984 ("nvme: stop asynchronous event work before ANA teardown") added the work drain there because ana_work has multiple producers and nvme_mpath_stop() is skipped once ana_log_buf is NULL. The timer needs the same treatment before the ANA resources are freed.
---
Found by code inspection; reproduced in a KASAN guest with nvmet-tcp loopback, two paths and an ANA group in change state. With a test-only delay widening the ana_work window, deleting the controller while ana_work is in flight arms the timer after nvme_mpath_stop() deleted it, and twenty seconds after the controller is freed:
BUG: KASAN: slab-use-after-free in __run_timers+0x887/0x990 Write of size 8 at addr ffff8884ec888e00 by task swapper/24/0
With the fix the same recipe completes with no splat and both controllers are removed cleanly. ANA change/optimized transitions and multipath I/O were verified unaffected.
drivers/nvme/host/multipath.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/nvme/host/multipath.c +2 -1
@@ -1074,8 +1074,8 @@ void nvme_mpath_stop(struct nvme_ctrl *ctrl) {if (!nvme_ctrl_use_ana (ctrl))return ; -timer_delete_sync (&ctrl->anatt_timer);cancel_work_sync (&ctrl->ana_work); +timer_delete_sync (&ctrl->anatt_timer); }#define SUBSYS_ATTR_RW (_name, _mode, _show, _store) \
@@ -1647,6 +1647,7 @@ void nvme_mpath_uninit(struct nvme_ctrl *ctrl)* resources. */ cancel_work_sync (&ctrl->ana_work); +timer_delete_sync (&ctrl->anatt_timer);kvfree (ctrl->ana_log_buf); ctrl->ana_log_buf =NULL ;
Signature · 1 line
2.43.7
[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] nvme-multipath: 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] nvme-multipath: use-after-free in nvme_parse_ana_log() [1] --
commit e4ad87877e854ebed2991236e7f5c8c425e05678
Author: Guixin Liu <[email protected]>
nvme-multipath: delete the ANATT timer after draining ana_work
This commit cancels ana_work before deleting anatt_timer in nvme_mpath_stop() and also deletes the timer in nvme_mpath_uninit() after draining ana_work.
Quoted 4 lines
drivers/nvme/host/multipath.c +0
-0
[ ... ]
drivers/nvme/host/multipath.c void nvme_mpath_uninit(struct nvme_ctrl *ctrl) · line 1620
Quoted 8 lines
drivers/nvme/host/multipath.c +1
-0@@ -1614,6 +1614,7 @@ void nvme_mpath_uninit(struct nvme_ctrl *ctrl)* resources. */ cancel_work_sync (&ctrl->ana_work); +timer_delete_sync (&ctrl->anatt_timer);kvfree (ctrl->ana_log_buf); ctrl->ana_log_buf =NULL ;
[Severity: Critical]
This problem wasn't introduced by this patch, but can freeing ctrl->ana_log_buf here without holding ctrl->ana_lock cause a use-after-free in nvme_parse_ana_log() [1]?
When nvme_mpath_init_identify() sees a larger ana_log_size, it calls nvme_mpath_uninit() to free the old buffer before allocating a new one, without taking ctrl->ana_lock. A concurrent namespace scan in nvme_mpath_add_disk() takes ctrl->ana_lock and reads ctrl->ana_log_buf in nvme_parse_ana_log():
CPU0 CPU1nvme_mpath_init_identify() nvme_mpath_add_disk()
if (nvme_ctrl_use_ana(ns->ctrl))
mutex_lock(&ns->ctrl->ana_lock)
nvme_parse_ana_log()
base = ctrl->ana_log_buf
nvme_mpath_uninit()
kvfree(ctrl->ana_log_buf)
/* dereferences freed base */
<-- use-after-freeTaking ctrl->ana_lock around freeing and replacing ctrl->ana_log_buf would serialize this with concurrent readers.
[1] https://sashiko.dev/#/bug/linux-48603474-5139-4b24-a567-ce7f9706b201
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 51 lines · 在 2026/11/24 20:22, [email protected] 写道:
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] nvme-multipath: use-after-free innvme_parse_ana_log()[1] --commit
e4ad87877e854ebed2991236e7f5c8c425e05678
Author: Guixin Liu <[email protected]>nvme-multipath: delete the ANATT timer after draining
ana_workThis commit cancels
ana_workbefore deletinganatt_timerinnvme_mpath_stop()and also deletes the timer innvme_mpath_uninit()after drainingana_work.Quoted 4 lines
drivers/nvme/host/multipath.c +0
-0[ ... ]
drivers/nvme/host/multipath.c void nvme_mpath_uninit(struct nvme_ctrl *ctrl) · line 1620
Quoted 8 lines
drivers/nvme/host/multipath.c +1
-0@@ -1614,6 +1614,7 @@ void nvme_mpath_uninit(struct nvme_ctrl *ctrl)* resources. */ cancel_work_sync (&ctrl->ana_work); +timer_delete_sync (&ctrl->anatt_timer);kvfree (ctrl->ana_log_buf); ctrl->ana_log_buf =NULL ;[Severity: Critical]
This problem wasn't introduced by this patch, but can freeingctrl->ana_log_bufhere without holdingctrl->ana_lockcause a use-after-free innvme_parse_ana_log()[1]?When
nvme_mpath_init_identify()sees a largerana_log_size, it callsnvme_mpath_uninit()to free the old buffer before allocating a new one, without takingctrl->ana_lock. A concurrent namespace scan innvme_mpath_add_disk()takesctrl->ana_lockand readsctrl->ana_log_bufinnvme_parse_ana_log():CPU0 CPU1
nvme_mpath_init_identify()nvme_mpath_add_disk()if (nvme_ctrl_use_ana(ns->ctrl)) mutex_lock(&ns->ctrl->ana_lock) nvme_parse_ana_log() base = ctrl->ana_log_buf nvme_mpath_uninit() kvfree(ctrl->ana_log_buf) /* dereferences freed base */ <-- use-after-free
Quoted 4 lines
Taking
ctrl->ana_lockaround freeing and replacingctrl->ana_log_bufwould serialize this with concurrent readers.[1] https://sashiko.dev/#/bug/linux-48603474-5139-4b24-a567-ce7f9706b201
What Sashiko found is a pre-existing bug, not something introduced by this patch.
Let me check wheather I can send a separate patch for it.
Best Regards,
Guixin Liu
Quoted 1 line
Looks good:
Quoted 26 lines · Guixin Liu
nvme_mpath_stop()deletes the ANATT timer before cancellingana_work. Anana_workthat is already past its controller state check can still read the ANA log and arm the timer after the timer was deleted. The controller is then freed with the timer armed, andnvme_anatt_timeout()dereferences the freed controller when it fires.Cancel
ana_workbefore deleting the timer so it cannot be armed again. Also delete the timer innvme_mpath_uninit()after drainingana_work:
commit9b657c565984("nvme: stop asynchronous event work before ANA teardown") added the work drain there becauseana_workhas multiple producers andnvme_mpath_stop()is skipped onceana_log_bufis NULL. The timer needs the same treatment before the ANA resources are freed.Fixes 0d0b660f214d ("nvme: add ANA support")Signed-off-by Guixin Liu <[email protected]>---
Found by code inspection; reproduced in a KASAN guest with nvmet-tcp loopback, two paths and an ANA group in change state. With a test-only delay widening theana_workwindow, deleting the controller whileana_workis in flight arms the timer afternvme_mpath_stop()deleted it, and twenty seconds after the controller is freed:BUG: KASAN: slab-use-after-free in __run_timers+0x887/0x990 Write of size 8 at addr ffff8884ec888e00 by task swapper/24/0
Quoted 7 lines
With the fix the same recipe completes with no splat and both controllers are removed cleanly. ANA change/optimized transitions and multipath I/O were verified unaffected.
drivers/nvme/host/multipath.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
Quoted 13 lines · Guixin Liu
nvme_mpath_stop()deletes the ANATT timer before cancellingana_work. Anana_workthat is already past its controller state check can still read the ANA log and arm the timer after the timer was deleted. The controller is then freed with the timer armed, andnvme_anatt_timeout()dereferences the freed controller when it fires.Cancel
ana_workbefore deleting the timer so it cannot be armed again. Also delete the timer innvme_mpath_uninit()after drainingana_work:
commit9b657c565984("nvme: stop asynchronous event work before ANA teardown") added the work drain there becauseana_workhas multiple producers andnvme_mpath_stop()is skipped onceana_log_bufis NULL. The timer needs the same treatment before the ANA resources are freed.
Quoted 3 lines
Fixes 0d0b660f214d ("nvme: add ANA support")Signed-off-by Guixin Liu<[email protected]>
Looks good to me: