[PATCH 1/4] nvme: guard nvme_strlen() against all-space strings

Guixin Liu kanie at linux.alibaba.com
Tue Dec 1 03:53:42 PST 2026


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 at vger.kernel.org
Signed-off-by: Guixin Liu <kanie at 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




More information about the Linux-nvme mailing list