Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/4] nvme: small fixes from a drivers/nvme audit
@ 2026-12-01 11:53 Guixin Liu
  2026-12-01 11:53 ` [PATCH 1/4] nvme: guard nvme_strlen() against all-space strings Guixin Liu
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Guixin Liu @ 2026-12-01 11:53 UTC (permalink / raw)
  To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
	Nilay Shroff, Daniel Wagner, Hannes Reinecke
  Cc: linux-nvme

A static audit of drivers/nvme turned up a set of small, independent
bugs. This series collects four of them; each patch has its own
reproducer and regression run, described below the --- line.

  nvme: guard nvme_strlen() against all-space strings
    An all-space Identify field drives the length negative and reads
    before the buffer.

  nvme: trace the Set Features Save bit from the right position
    The trace parser reads reserved bit 27 instead of the SV bit 31.

  nvmet-fc: clear the DMA address of the fod that failed mapping
    The failing fod keeps DMA_MAPPING_ERROR and is later passed to
    dma_unmap_single().

  nvme-fcloop: initialize port structures before publishing them
    A concurrent port delete can flush an uninitialized work_struct
    and test an uninitialized list head.

Testing: QEMU/KVM guest, 7.3.0 kernel with KASAN, UBSAN and
CONFIG_DMA_API_DEBUG, nvmet-tcp and nvme-fcloop loopback. Each fix was
reproduced on the unpatched code, verified with the same recipe after
patching, and regression-tested with the test-only injections removed
(connect/IO/disconnect and normal add/delete paths, no warnings). The
test-only injections and the DMA_API_DEBUG kernel build live in the
test environment only, not in these patches.

Guixin Liu (4):
  nvme: guard nvme_strlen() against all-space strings
  nvme: trace the Set Features Save bit from the right position
  nvmet-fc: clear the DMA address of the fod that failed mapping
  nvme-fcloop: initialize port structures before publishing them

 drivers/nvme/host/nvme.h     |  2 +-
 drivers/nvme/host/trace.c    |  2 +-
 drivers/nvme/target/fc.c     |  1 +
 drivers/nvme/target/fcloop.c | 28 ++++++++++++++++++----------
 4 files changed, 21 insertions(+), 12 deletions(-)

-- 
2.43.7



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

* [PATCH 1/4] nvme: guard nvme_strlen() against all-space strings
  2026-12-01 11:53 [PATCH 0/4] nvme: small fixes from a drivers/nvme audit Guixin Liu
@ 2026-12-01 11:53 ` Guixin Liu
  2026-12-02  6:32   ` Christoph Hellwig
  2026-12-01 11:53 ` [PATCH 2/4] nvme: trace the Set Features Save bit from the right position Guixin Liu
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: Guixin Liu @ 2026-12-01 11:53 UTC (permalink / raw)
  To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
	Nilay Shroff, Daniel Wagner, Hannes Reinecke
  Cc: linux-nvme

nvme_strlen() strips the space padding from Identify Controller string
fields without checking the remaining length. A device returning an
all-space Model Number or Firmware Revision, which the NVMe spec's
space padding rules allow, drives the index below zero and reads memory
before the start of the buffer; with an all-space serial number in
front of the model the walk keeps going into the preceding fields. The
negative length then reaches dev_err() as a %.*s precision, which the
kernel clamps to zero, so the field also prints empty.

Stop the loop at the start of the string, like wwid_show() already
does.

Fixes: 2f0dad1719cbb ("nvme: add bug report info for global duplicate id")
Cc: stable@vger.kernel.org
Signed-off-by: Guixin Liu <kanie@linux.alibaba.com>
---

Found by code inspection; reproduced in a QEMU/KVM guest (7.3.0,
KASAN + UBSAN) with nvmet-tcp loopback.

Reproducer: two nvmet subsystems sharing a namespace UUID trigger the
duplicate-ID check in nvme_init_ns_head(), which calls
nvme_print_device_info(). Set the second subsystem's attr_model to one
space and attr_serial to 20 spaces; the space-padded Identify data then
carries all-space model and serial fields.

A TEST-ONLY printk in the test tree (not part of this patch) printing
the nvme_strlen() return value shows -20 unpatched (the walk runs
through the 40-byte model and the 20-byte serial into the subnqn NUL)
and 0 with this patch. A probe variant reading model[-1] also triggered
"UBSAN: array-index-out-of-bounds, index -1 out of range for char [40]".
The kernel clamps a negative %.*s precision to 0 (lib/vsprintf.c), so
the field prints empty rather than leaking forward memory.

Regression: duplicate-ID handling, normal model/serial printing,
nvme-tcp connect and IO unchanged.
 drivers/nvme/host/nvme.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/nvme/host/nvme.h b/drivers/nvme/host/nvme.h
index e069b6d661201..1a28055ac8bcb 100644
--- a/drivers/nvme/host/nvme.h
+++ b/drivers/nvme/host/nvme.h
@@ -747,7 +747,7 @@ static inline struct request *nvme_cid_to_rq(struct blk_mq_tags *tags,
  */
 static inline int nvme_strlen(char *s, int len)
 {
-	while (s[len - 1] == ' ')
+	while (len > 0 && s[len - 1] == ' ')
 		len--;
 	return len;
 }
-- 
2.43.7



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

* [PATCH 2/4] nvme: trace the Set Features Save bit from the right position
  2026-12-01 11:53 [PATCH 0/4] nvme: small fixes from a drivers/nvme audit Guixin Liu
  2026-12-01 11:53 ` [PATCH 1/4] nvme: guard nvme_strlen() against all-space strings Guixin Liu
@ 2026-12-01 11:53 ` Guixin Liu
  2026-12-02  6:32   ` Christoph Hellwig
  2026-12-01 11:53 ` [PATCH 3/4] nvmet-fc: clear the DMA address of the fod that failed mapping Guixin Liu
  2026-12-01 11:53 ` [PATCH 4/4] nvme-fcloop: initialize port structures before publishing them Guixin Liu
  3 siblings, 1 reply; 9+ messages in thread
From: Guixin Liu @ 2026-12-01 11:53 UTC (permalink / raw)
  To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
	Nilay Shroff, Daniel Wagner, Hannes Reinecke
  Cc: linux-nvme

The Save (SV) bit of Set Features is CDW10 bit 31. The trace parser
masks byte 3 of CDW10 with 0x8, which selects the reserved bit 27
instead, so the trace shows sv=0 for commands that save the feature
and a nonzero sv when a reserved bit is set.

Mask with 0x80 instead.

Fixes: a7b5e8d864b3 ("nvme: add set feature tracing support")
Cc: stable@vger.kernel.org
Signed-off-by: Guixin Liu <kanie@linux.alibaba.com>
---

Reproduced in a QEMU/KVM guest with nvmet-tcp loopback: send Set
Features (fid 0x0b) via NVME_IOCTL_ADMIN_CMD with CDW10 0x8000000b,
0x0000000b and 0x0800000b, and read the nvme_setup_cmd tracepoint.
Unpatched: sv=0x0 for the SV-set command and sv=0x8 for the
reserved-bit command. Patched: sv=0x80 and sv=0x0; the plain command
stays sv=0x0. fid 0x07 is rejected unconditionally by
nvme_admin_cmd_allowed(), so the test uses 0x0b.
 drivers/nvme/host/trace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/nvme/host/trace.c b/drivers/nvme/host/trace.c
index ad25ad1e40412..699c577818925 100644
--- a/drivers/nvme/host/trace.c
+++ b/drivers/nvme/host/trace.c
@@ -77,7 +77,7 @@ static const char *nvme_trace_admin_set_features(struct trace_seq *p,
 {
 	const char *ret = trace_seq_buffer_ptr(p);
 	u8 fid = cdw10[0];
-	u8 sv = cdw10[3] & 0x8;
+	u8 sv = cdw10[3] & 0x80;
 	u32 cdw11 = get_unaligned_le32(cdw10 + 4);
 
 	trace_seq_printf(p, "fid=0x%x, sv=0x%x, cdw11=0x%x", fid, sv, cdw11);
-- 
2.43.7



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

* [PATCH 3/4] nvmet-fc: clear the DMA address of the fod that failed mapping
  2026-12-01 11:53 [PATCH 0/4] nvme: small fixes from a drivers/nvme audit Guixin Liu
  2026-12-01 11:53 ` [PATCH 1/4] nvme: guard nvme_strlen() against all-space strings Guixin Liu
  2026-12-01 11:53 ` [PATCH 2/4] nvme: trace the Set Features Save bit from the right position Guixin Liu
@ 2026-12-01 11:53 ` Guixin Liu
  2026-12-02  6:32   ` Christoph Hellwig
  2026-12-01 11:53 ` [PATCH 4/4] nvme-fcloop: initialize port structures before publishing them Guixin Liu
  3 siblings, 1 reply; 9+ messages in thread
From: Guixin Liu @ 2026-12-01 11:53 UTC (permalink / raw)
  To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
	Nilay Shroff, Daniel Wagner, Hannes Reinecke
  Cc: linux-nvme

When fc_dma_map_single() fails for one fod, nvmet_fc_prep_fcp_iodlist()
unmaps the preceding entries and clears their rspdma, but leaves the
DMA error value in the failing fod itself. The queue is still set up,
and nvmet_fc_destroy_fcp_iodlist() walks the whole fod array, so the
non-zero error value passes the "if (fod->rspdma)" check and is handed
to dma_unmap_single().

Clear rspdma on the failing fod as well.

Fixes: c53432030d86 ("nvme-fabrics: Add target support for FC transport")
Cc: stable@vger.kernel.org
Signed-off-by: Guixin Liu <kanie@linux.alibaba.com>
---

Reproduced in a QEMU/KVM guest with nvme-fcloop and a test kernel
built with CONFIG_DMA_API_DEBUG=y. A TEST-ONLY injection in the test
tree (not part of this patch) fails the DMA mapping of the 4th fod in
nvmet_fc_prep_fcp_iodlist(). Because fcloop registers the target port
with dev=NULL, which turns the fc_dma_* wrappers into no-ops, the
injection also passes a real device with a 64-bit dma_mask so the
bogus unmap reaches the DMA API.

Unpatched: disconnect frees the queue and dma_unmap_single() is called
with DMA_MAPPING_ERROR; DMA-API reports "device driver tries to free
an invalid DMA memory address" (check_unmap). Patched, same injection:
the failing fod's rspdma is 0 at destroy time and error_count stays 0.
The connection stays live and IO passes in both cases, as the failing
fod is only removed from the free list.

Regression without the injection: fcloop connect, IO and disconnect
pass with no DMA-API errors.
 drivers/nvme/target/fc.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/nvme/target/fc.c b/drivers/nvme/target/fc.c
index 4f4b30f5df573..42f8b17dee07b 100644
--- a/drivers/nvme/target/fc.c
+++ b/drivers/nvme/target/fc.c
@@ -635,6 +635,7 @@ nvmet_fc_prep_fcp_iodlist(struct nvmet_fc_tgtport *tgtport,
 		fod->rspdma = fc_dma_map_single(tgtport->dev, &fod->rspiubuf,
 					sizeof(fod->rspiubuf), DMA_TO_DEVICE);
 		if (fc_dma_mapping_error(tgtport->dev, fod->rspdma)) {
+			fod->rspdma = 0;
 			list_del(&fod->fcp_list);
 			for (fod--, i--; i >= 0; fod--, i--) {
 				fc_dma_unmap_single(tgtport->dev, fod->rspdma,
-- 
2.43.7



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

* [PATCH 4/4] nvme-fcloop: initialize port structures before publishing them
  2026-12-01 11:53 [PATCH 0/4] nvme: small fixes from a drivers/nvme audit Guixin Liu
                   ` (2 preceding siblings ...)
  2026-12-01 11:53 ` [PATCH 3/4] nvmet-fc: clear the DMA address of the fod that failed mapping Guixin Liu
@ 2026-12-01 11:53 ` Guixin Liu
  2026-12-02  6:33   ` Christoph Hellwig
  3 siblings, 1 reply; 9+ messages in thread
From: Guixin Liu @ 2026-12-01 11:53 UTC (permalink / raw)
  To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
	Nilay Shroff, Daniel Wagner, Hannes Reinecke
  Cc: linux-nvme

fcloop_create_remote_port() and fcloop_create_target_port() publish the
new port in nport before initializing its flags, lock, LS work and LS
list, and do so without fcloop_lock. A concurrent del_remote_port or
del_target_port write can pick up the half-initialized port and call
flush_work() on an uninitialized work_struct and list_empty() on an
uninitialized list head. The private area comes from kmalloc(), so
these fields contain heap garbage.

Initialize everything before publishing, and publish under fcloop_lock
so the unlink side sees either NULL or a fully initialized port.

Fixes: 38803fcffb5b ("nvme-fcloop: fix deallocation of working context")
Fixes: 437c0b824dbd ("nvme-fcloop: add target to host LS request support")
Cc: stable@vger.kernel.org
Signed-off-by: Guixin Liu <kanie@linux.alibaba.com>
---

Reproduced in a QEMU/KVM guest with nvme-fcloop. A TEST-ONLY
msleep(2000) in the test tree (not part of this patch) widens the
window between publishing nport->rport / nport->tport and initializing
the port fields; a concurrent del_remote_port / del_target_port write
0.5s into the window picks up the half-initialized port.

Unpatched: every iteration hits "WARNING: __flush_work" (workqueue.c)
and "WARNING: fcloop_targetport_delete" (list_empty on a garbage list
head; LIST_POISON values observed). The private area is kmalloc'd, not
zeroed. Patched, same recipe with the sleep moved before the locked
publish: the in-window delete finds no port (-ENOENT), 16 iterations
produce no warnings, and the normal add/delete sequence still works.

A separate KASAN use-after-free in nvmet_fc_rcv_ls_req() showed up when
udev auto-connect (70-nvmf-autoconnect.rules) raced the port deletion;
it is a pre-existing tgtport/LS lifetime issue unrelated to this patch
and does not occur with auto-connect disabled.

Regression without the injection: add/delete and fcloop IO pass.
 drivers/nvme/target/fcloop.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/drivers/nvme/target/fcloop.c b/drivers/nvme/target/fcloop.c
index a9cde536c33ba..e0ce7c5d5ed5a 100644
--- a/drivers/nvme/target/fcloop.c
+++ b/drivers/nvme/target/fcloop.c
@@ -1445,6 +1445,7 @@ fcloop_create_remote_port(struct device *dev, struct device_attribute *attr,
 	struct fcloop_nport *nport;
 	struct fcloop_rport *rport;
 	struct nvme_fc_port_info pinfo;
+	unsigned long flags;
 	int ret;
 
 	nport = fcloop_alloc_nport(buf, count, true);
@@ -1467,19 +1468,22 @@ fcloop_create_remote_port(struct device *dev, struct device_attribute *attr,
 	/* success */
 	rport = remoteport->private;
 	rport->remoteport = remoteport;
-	rport->targetport = (nport->tport) ?  nport->tport->targetport : NULL;
-	if (nport->tport) {
-		nport->tport->remoteport = remoteport;
-		nport->tport->lport = nport->lport;
-	}
 	rport->nport = nport;
 	rport->lport = nport->lport;
-	nport->rport = rport;
 	rport->flags = 0;
 	spin_lock_init(&rport->lock);
 	INIT_WORK(&rport->ls_work, fcloop_rport_lsrqst_work);
 	INIT_LIST_HEAD(&rport->ls_list);
 
+	spin_lock_irqsave(&fcloop_lock, flags);
+	rport->targetport = (nport->tport) ?  nport->tport->targetport : NULL;
+	if (nport->tport) {
+		nport->tport->remoteport = remoteport;
+		nport->tport->lport = nport->lport;
+	}
+	nport->rport = rport;
+	spin_unlock_irqrestore(&fcloop_lock, flags);
+
 	return count;
 }
 
@@ -1547,6 +1551,7 @@ fcloop_create_target_port(struct device *dev, struct device_attribute *attr,
 	struct fcloop_nport *nport;
 	struct fcloop_tport *tport;
 	struct nvmet_fc_port_info tinfo;
+	unsigned long flags;
 	int ret;
 
 	nport = fcloop_alloc_nport(buf, count, false);
@@ -1567,17 +1572,20 @@ fcloop_create_target_port(struct device *dev, struct device_attribute *attr,
 	/* success */
 	tport = targetport->private;
 	tport->targetport = targetport;
-	tport->remoteport = (nport->rport) ?  nport->rport->remoteport : NULL;
-	if (nport->rport)
-		nport->rport->targetport = targetport;
 	tport->nport = nport;
 	tport->lport = nport->lport;
-	nport->tport = tport;
 	tport->flags = 0;
 	spin_lock_init(&tport->lock);
 	INIT_WORK(&tport->ls_work, fcloop_tport_lsrqst_work);
 	INIT_LIST_HEAD(&tport->ls_list);
 
+	spin_lock_irqsave(&fcloop_lock, flags);
+	tport->remoteport = (nport->rport) ?  nport->rport->remoteport : NULL;
+	if (nport->rport)
+		nport->rport->targetport = targetport;
+	nport->tport = tport;
+	spin_unlock_irqrestore(&fcloop_lock, flags);
+
 	return count;
 }
 
-- 
2.43.7



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

* Re: [PATCH 1/4] nvme: guard nvme_strlen() against all-space strings
  2026-12-01 11:53 ` [PATCH 1/4] nvme: guard nvme_strlen() against all-space strings Guixin Liu
@ 2026-12-02  6:32   ` Christoph Hellwig
  0 siblings, 0 replies; 9+ messages in thread
From: Christoph Hellwig @ 2026-12-02  6:32 UTC (permalink / raw)
  To: Guixin Liu
  Cc: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
	Nilay Shroff, Daniel Wagner, Hannes Reinecke, linux-nvme

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>



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

* Re: [PATCH 2/4] nvme: trace the Set Features Save bit from the right position
  2026-12-01 11:53 ` [PATCH 2/4] nvme: trace the Set Features Save bit from the right position Guixin Liu
@ 2026-12-02  6:32   ` Christoph Hellwig
  0 siblings, 0 replies; 9+ messages in thread
From: Christoph Hellwig @ 2026-12-02  6:32 UTC (permalink / raw)
  To: Guixin Liu
  Cc: Keith Busch, Jens Axboe, Sagi Grimberg, Nilay Shroff,
	Daniel Wagner, Hannes Reinecke, linux-nvme

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>


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

* Re: [PATCH 3/4] nvmet-fc: clear the DMA address of the fod that failed mapping
  2026-12-01 11:53 ` [PATCH 3/4] nvmet-fc: clear the DMA address of the fod that failed mapping Guixin Liu
@ 2026-12-02  6:32   ` Christoph Hellwig
  0 siblings, 0 replies; 9+ messages in thread
From: Christoph Hellwig @ 2026-12-02  6:32 UTC (permalink / raw)
  To: Guixin Liu
  Cc: Keith Busch, Jens Axboe, Sagi Grimberg, Nilay Shroff,
	Daniel Wagner, Hannes Reinecke, linux-nvme

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>



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

* Re: [PATCH 4/4] nvme-fcloop: initialize port structures before publishing them
  2026-12-01 11:53 ` [PATCH 4/4] nvme-fcloop: initialize port structures before publishing them Guixin Liu
@ 2026-12-02  6:33   ` Christoph Hellwig
  0 siblings, 0 replies; 9+ messages in thread
From: Christoph Hellwig @ 2026-12-02  6:33 UTC (permalink / raw)
  To: Guixin Liu
  Cc: Keith Busch, Jens Axboe, Sagi Grimberg, Nilay Shroff,
	Daniel Wagner, Hannes Reinecke, linux-nvme

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>



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

end of thread, other threads:[~2026-12-02  6:42 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 11:53 [PATCH 0/4] nvme: small fixes from a drivers/nvme audit Guixin Liu
2026-12-01 11:53 ` [PATCH 1/4] nvme: guard nvme_strlen() against all-space strings Guixin Liu
2026-12-02  6:32   ` Christoph Hellwig
2026-12-01 11:53 ` [PATCH 2/4] nvme: trace the Set Features Save bit from the right position Guixin Liu
2026-12-02  6:32   ` Christoph Hellwig
2026-12-01 11:53 ` [PATCH 3/4] nvmet-fc: clear the DMA address of the fod that failed mapping Guixin Liu
2026-12-02  6:32   ` Christoph Hellwig
2026-12-01 11:53 ` [PATCH 4/4] nvme-fcloop: initialize port structures before publishing them Guixin Liu
2026-12-02  6:33   ` Christoph Hellwig

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