Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nvme-multipath: protect ana_log_buf teardown with ana_lock
@ 2026-11-25  6:01 Guixin Liu
  2026-11-25  6:10 ` sashiko-bot
  2026-11-25  7:23 ` Christoph Hellwig
  0 siblings, 2 replies; 4+ messages in thread
From: Guixin Liu @ 2026-11-25  6:01 UTC (permalink / raw)
  To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
	Nilay Shroff, Daniel Wagner, Hannes Reinecke
  Cc: linux-nvme

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")
Cc: stable@vger.kernel.org
Signed-off-by: Guixin Liu <kanie@linux.alibaba.com>
---
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(+)

diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c
index 3685c792181b..28ed3a677c10 100644
--- a/drivers/nvme/host/multipath.c
+++ b/drivers/nvme/host/multipath.c
@@ -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);
 }
-- 
2.43.7



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] nvme-multipath: protect ana_log_buf teardown with ana_lock
  2026-11-25  6:01 [PATCH] nvme-multipath: protect ana_log_buf teardown with ana_lock Guixin Liu
@ 2026-11-25  6:10 ` sashiko-bot
  2026-11-25  6:33   ` Guixin Liu
  2026-11-25  7:23 ` Christoph Hellwig
  1 sibling, 1 reply; 4+ messages in thread
From: sashiko-bot @ 2026-11-25  6:10 UTC (permalink / raw)
  To: Guixin Liu; +Cc: linux-nvme

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 <kanie@linux.alibaba.com>

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.

> diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c
> index 27facdbb3d4c0..bc0abd1f30274 100644
> --- a/drivers/nvme/host/multipath.c
> +++ b/drivers/nvme/host/multipath.c
> @@ -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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125060153.206699-1-kanie@linux.alibaba.com?part=1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] nvme-multipath: protect ana_log_buf teardown with ana_lock
  2026-11-25  6:10 ` sashiko-bot
@ 2026-11-25  6:33   ` Guixin Liu
  0 siblings, 0 replies; 4+ messages in thread
From: Guixin Liu @ 2026-11-25  6:33 UTC (permalink / raw)
  To: sashiko-reviews; +Cc: linux-nvme



在 2026/11/25 14:10, sashiko-bot@kernel.org 写道:
> 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 <kanie@linux.alibaba.com>
>
> 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.
>
>> diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c
>> index 27facdbb3d4c0..bc0abd1f30274 100644
>> --- a/drivers/nvme/host/multipath.c
>> +++ b/drivers/nvme/host/multipath.c
>> @@ -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.
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
>



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] nvme-multipath: protect ana_log_buf teardown with ana_lock
  2026-11-25  6:01 [PATCH] nvme-multipath: protect ana_log_buf teardown with ana_lock Guixin Liu
  2026-11-25  6:10 ` sashiko-bot
@ 2026-11-25  7:23 ` Christoph Hellwig
  1 sibling, 0 replies; 4+ messages in thread
From: Christoph Hellwig @ 2026-11-25  7:23 UTC (permalink / raw)
  To: Guixin Liu
  Cc: Keith Busch, Jens Axboe, Sagi Grimberg, Nilay Shroff,
	Daniel Wagner, Hannes Reinecke, linux-nvme

On Wed, Nov 25, 2026 at 02:01:53PM +0800, Guixin Liu wrote:
> 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.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-25  7:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  6:01 [PATCH] nvme-multipath: protect ana_log_buf teardown with ana_lock Guixin Liu
2026-11-25  6:10 ` sashiko-bot
2026-11-25  6:33   ` Guixin Liu
2026-11-25  7:23 ` Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox