Skip to contentSkip to conversation

nvme-multipath: delete the ANATT timer after draining ana_work

PATCH In review Guixin Liu 5 participants 6 messages Nov 24 – Nov 25 NVMe

lore
Share & tools
lore

Guixin Liu

author
#0 lore raw

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.

Fixes 0d0b660f214d ("nvme: add ANA support")

---
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
index 3685c792181b..bd27a5355bab 100644
@@ -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
#1 lore raw
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…

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
index 27facdbb3d4c0..40f6cab3db47c 100644

[ ... ]

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 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

Taking 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
author
#2 lore raw
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 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
index 27facdbb3d4c0..40f6cab3db47c 100644

[ ... ]

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 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_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

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
Reviewed-by on v1 1/1
#4 lore raw
In reply to Guixin Liu
Quoted 26 lines · Guixin Liu

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.

Fixes 0d0b660f214d ("nvme: add ANA support")

---
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
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
Reviewed-by on v1 1/1
#5 lore raw
In reply to Guixin Liu
Quoted 13 lines · Guixin Liu

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.

Quoted 3 lines
Fixes 0d0b660f214d ("nvme: add ANA support")

Looks good to me: