Skip to contentSkip to conversation

nvme-multipath: protect ana_log_buf teardown with ana_lock

PATCH In review Guixin Liu 4 participants 6 messages Nov 25 NVMe

lore
Share & tools
lore

Guixin Liu

author
#0 lore raw

PATCH 1/1 nvme-multipath: protect ana_log_buf teardown with ana_lock

nvme_parse_ana_log() reads ctrl->ana_log_buf under ctrl->ana_lock, but nvme_mpath_uninit() frees the buffer without holding the lock. When nvme_mpath_init_identify() reallocates a larger ANA log it frees the old buffer through nvme_mpath_uninit(), while a concurrent namespace scan in nvme_mpath_add_disk() may still be parsing that buffer under ana_lock, leading to a use-after-free.

Take ana_lock around the buffer release in nvme_mpath_uninit() so it serializes with concurrent parsers. Keep the ana_work drain outside the lock: ana_work itself takes ana_lock, so cancelling it under the lock would deadlock.

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

---
Reported by Sashiko AI review as a pre-existing issue (not introduced by the ANATT timer fix). Confirmed by source analysis and reproduced in a KASAN guest with nvmet-tcp loopback and ANA enabled.

The race is in the reallocation path only: nvme_mpath_init_identify() frees the old buffer through nvme_mpath_uninit() before taking ana_lock in nvme_read_ana_log(). The out_uninit path (failed ANA log read) takes ana_lock first and is already serialized against parsers. The FROZEN guard does not close the window: a tcp reset runs nvme_init_identify() (in configure_admin_queue) before nvme_start_freeze() (in configure_io_queues), and reset does not flush scan_work.

Reproduction: with a test-only delay in the add_disk parse path and a forced ANA-log growth, buffer address tracking shows nvme_mpath_uninit() freeing the old ana_log_buf while nvme_parse_ana_log() still dereferences it; the reallocated buffer lands at a different address, so the parser reads freed memory. With this fix the free waits for ana_lock and happens only after the parser completes (free timestamp moves after the parse dereference), and the use-after-free disappears. ANA change/optimized transitions and multipath I/O remain unaffected.

 drivers/nvme/host/multipath.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/nvme/host/multipath.c +2 -0
index 3685c792181b..28ed3a677c10 100644
@@ -1648,7 +1648,9 @@ void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
 	 */
 	cancel_work_sync(&ctrl->ana_work);
 
+	mutex_lock(&ctrl->ana_lock);
 	kvfree(ctrl->ana_log_buf);
 	ctrl->ana_log_buf = NULL;
 	ctrl->ana_log_size = 0;
+	mutex_unlock(&ctrl->ana_lock);
 }
Signature · 1 line
2.43.7
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] nvme-multipath: NULL deref and uninit read in nvme_parse_ana_log() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] nvme-multipath: NULL deref and uninit read in nvme_parse_ana_log()
--

commit d312e34ea57a3e103965fa1cad1dd210a8ca3b3a
Author: Guixin Liu <[email protected]>

nvme-multipath: protect ana_log_buf teardown with ana_lock

This commit acquires ctrl->ana_lock around freeing and clearing ctrl->ana_log_buf in nvme_mpath_uninit() to serialize against concurrent ANA log parsers.

Quoted 5 lines
drivers/nvme/host/multipath.c +0 -0
index 27facdbb3d4c0..bc0abd1f30274 100644

drivers/nvme/host/multipath.c void nvme_mpath_uninit(struct nvme_ctrl *ctrl) · line 1623

Quoted 9 lines
drivers/nvme/host/multipath.c +2 -0
@@ -1615,7 +1615,9 @@ void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
 	 */
 	cancel_work_sync(&ctrl->ana_work);
 
+	mutex_lock(&ctrl->ana_lock);
 	kvfree(ctrl->ana_log_buf);
 	ctrl->ana_log_buf = NULL;
 	ctrl->ana_log_size = 0;
+	mutex_unlock(&ctrl->ana_lock);
 }

[Severity: High]
Can a concurrent parser in nvme_mpath_add_disk() still hit a NULL pointer dereference or read uninitialized memory in nvme_parse_ana_log() when nvme_mpath_uninit() unlocks ctrl->ana_lock?

In the concurrent nvme_mpath_init_identify() and nvme_mpath_add_disk() path, nvme_mpath_add_disk() checks nvme_ctrl_use_ana() (which tests ctrl->ana_log_buf != NULL) before acquiring ctrl->ana_lock, and nvme_parse_ana_log() unconditionally dereferences ctrl->ana_log_buf->ngrps:

CPU0 CPU1
nvme_mpath_init_identify() nvme_mpath_add_disk()
if (nvme_ctrl_use_ana(ctrl))
nvme_mpath_uninit()

    mutex_lock(&ctrl->ana_lock)
    kvfree(ctrl->ana_log_buf)
    ctrl->ana_log_buf = NULL
    mutex_unlock(&ctrl->ana_lock)
                                        mutex_lock(&ctrl->ana_lock)
                                        nvme_parse_ana_log()
                                          ctrl->ana_log_buf->ngrps
                                            <-- NULL pointer dereference

ctrl->ana_log_buf = kvmalloc(...)
nvme_read_ana_log()

Even if CPU1 acquires ctrl->ana_lock after kvmalloc() runs in nvme_mpath_init_identify(), ctrl->ana_log_buf and ctrl->ana_log_size are assigned before nvme_read_ana_log() retakes ctrl->ana_lock to populate the buffer, so nvme_parse_ana_log() can parse uninitialized memory from kvmalloc(). In addition, while ctrl->ana_log_buf is NULL between nvme_mpath_uninit() and kvmalloc(), concurrent callers of nvme_ctrl_use_ana() transiently observe the controller as non-ANA.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#2 lore raw
Quoted 55 lines · 在 2026/11/25 14:10, [email protected] 写道:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] nvme-multipath: NULL deref and uninit read in nvme_parse_ana_log()
--

commit d312e34ea57a3e103965fa1cad1dd210a8ca3b3a
Author: Guixin Liu <[email protected]>

nvme-multipath: protect ana_log_buf teardown with ana_lock

This commit acquires ctrl->ana_lock around freeing and clearing ctrl->ana_log_buf in nvme_mpath_uninit() to serialize against concurrent ANA log parsers.

Quoted 5 lines
drivers/nvme/host/multipath.c +0 -0
index 27facdbb3d4c0..bc0abd1f30274 100644

drivers/nvme/host/multipath.c void nvme_mpath_uninit(struct nvme_ctrl *ctrl) · line 1623

Quoted 9 lines
drivers/nvme/host/multipath.c +2 -0
@@ -1615,7 +1615,9 @@ void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
 	 */
 	cancel_work_sync(&ctrl->ana_work);
 
+	mutex_lock(&ctrl->ana_lock);
 	kvfree(ctrl->ana_log_buf);
 	ctrl->ana_log_buf = NULL;
 	ctrl->ana_log_size = 0;
+	mutex_unlock(&ctrl->ana_lock);
 }

[Severity: High]
Can a concurrent parser in nvme_mpath_add_disk() still hit a NULL pointer dereference or read uninitialized memory in nvme_parse_ana_log() when nvme_mpath_uninit() unlocks ctrl->ana_lock?

In the concurrent nvme_mpath_init_identify() and nvme_mpath_add_disk() path, nvme_mpath_add_disk() checks nvme_ctrl_use_ana() (which tests ctrl->ana_log_buf != NULL) before acquiring ctrl->ana_lock, and nvme_parse_ana_log() unconditionally dereferences ctrl->ana_log_buf->ngrps:

CPU0 CPU1
nvme_mpath_init_identify() nvme_mpath_add_disk()
if (nvme_ctrl_use_ana(ctrl))
nvme_mpath_uninit()

    mutex_lock(&ctrl->ana_lock)
    kvfree(ctrl->ana_log_buf)
    ctrl->ana_log_buf = NULL
    mutex_unlock(&ctrl->ana_lock)
                                        mutex_lock(&ctrl->ana_lock)
                                        nvme_parse_ana_log()
                                          ctrl->ana_log_buf->ngrps
                                            <-- NULL pointer dereference

ctrl->ana_log_buf = kvmalloc(...)
nvme_read_ana_log()

Quoted 7 lines

Even if CPU1 acquires ctrl->ana_lock after kvmalloc() runs in nvme_mpath_init_identify(), ctrl->ana_log_buf and ctrl->ana_log_size are assigned before nvme_read_ana_log() retakes ctrl->ana_lock to populate the buffer, so nvme_parse_ana_log() can parse uninitialized memory from kvmalloc(). In addition, while ctrl->ana_log_buf is NULL between nvme_mpath_uninit() and kvmalloc(), concurrent callers of nvme_ctrl_use_ana() transiently observe the controller as non-ANA.

Oh, Sashiko is right, I didn't fix all race condition.

1. We should check NULL under lock to prevent NULL reference.
2. Should use kvzalloc instead kvmalloc.

Changed in v2.

Best Regards,
Guixin Liu

Quoted 1 line
#3 lore raw
In reply to Guixin Liu
Quoted 3 lines · Guixin Liu

Reported by Sashiko AI review as a pre-existing issue (not introduced by the ANATT timer fix). Confirmed by source analysis and reproduced in a KASAN guest with nvmet-tcp loopback and ANA enabled.

Sashiko thinks this is still not enough, and it's probably right.

#4 lore raw
Quoted 7 lines · Christoph Hellwig
Quoted 3 lines · Guixin Liu

Reported by Sashiko AI review as a pre-existing issue (not introduced by the ANATT timer fix). Confirmed by source analysis and reproduced in a KASAN guest with nvmet-tcp loopback and ANA enabled.

Sashiko thinks this is still not enough, and it's probably right.

Yes and I think we should add __guarded_by(&ctrl->ana_lock) context annotation around ctrl->ana_log_buf and ctrl->ana_log_size.

Thanks,
--Nilay

author
#5 lore raw
Quoted 10 lines · Nilay Shroff
Quoted 7 lines · Christoph Hellwig
Quoted 3 lines · Guixin Liu

Reported by Sashiko AI review as a pre-existing issue (not introduced by the ANATT timer fix). Confirmed by source analysis and reproduced in a KASAN guest with nvmet-tcp loopback and ANA enabled.

Sashiko thinks this is still not enough, and it's probably right.

Yes and I think we should add __guarded_by(&ctrl->ana_lock) context annotation around ctrl->ana_log_buf and ctrl->ana_log_size.

True, I will do the full fix in v2, thanks.

Best Regards,
Guixin Liu

Quoted 3 lines

Thanks,
--Nilay