Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Guixin Liu <kanie@linux.alibaba.com>
To: Keith Busch <kbusch@kernel.org>, Jens Axboe <axboe@kernel.dk>,
	Christoph Hellwig <hch@lst.de>, Sagi Grimberg <sagi@grimberg.me>,
	Nilay Shroff <nilay@linux.ibm.com>,
	Daniel Wagner <dwagner@suse.de>, Hannes Reinecke <hare@suse.de>
Cc: linux-nvme@lists.infradead.org
Subject: [PATCH 1/4] nvme: guard nvme_strlen() against all-space strings
Date: Tue,  1 Dec 2026 19:53:42 +0800	[thread overview]
Message-ID: <20261201115345.2349875-2-kanie@linux.alibaba.com> (raw)
In-Reply-To: <20261201115345.2349875-1-kanie@linux.alibaba.com>

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



  reply	other threads:[~2026-12-01 11:54 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-12-02  6:32   ` [PATCH 1/4] nvme: guard nvme_strlen() against all-space strings 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201115345.2349875-2-kanie@linux.alibaba.com \
    --to=kanie@linux.alibaba.com \
    --cc=axboe@kernel.dk \
    --cc=dwagner@suse.de \
    --cc=hare@suse.de \
    --cc=hch@lst.de \
    --cc=kbusch@kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=nilay@linux.ibm.com \
    --cc=sagi@grimberg.me \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox