All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lin Tzu Yang <rongh7426@gmail.com>
To: Christoph Hellwig <hch@lst.de>, Sagi Grimberg <sagi@grimberg.me>,
	Chaitanya Kulkarni <kch@nvidia.com>
Cc: Keith Busch <kbusch@kernel.org>, Hannes Reinecke <hare@suse.de>,
	linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: [PATCH] nvmet: fix one-byte overflow in configfs string show callbacks
Date: Mon, 30 Nov 2026 19:47:11 +0800	[thread overview]
Message-ID: <20261130114711.842577-1-rongh7426@gmail.com> (raw)

The show callbacks for the namespace attribute device_path and the
host attributes dhchap_key and dhchap_ctrl_key print the stored string
with sprintf(page, "%s\n", ...). configfs accepts up to 4095 bytes in a
store, and the store side keeps them all, so with a 4095-byte value the
show writes the string, the newline and the terminating NUL, one byte
past the PAGE_SIZE buffer.

KASAN in a QEMU arm64 guest, after writing 4095 bytes to
namespaces/1/device_path and reading it back:

  BUG: KASAN: slab-out-of-bounds in vsnprintf+0xdd8/0xdfc
  Write of size 1 at addr ffff0000127cf000 by task cat/4329
   vsnprintf+0xdd8/0xdfc
   sprintf+0xb0/0xec
   nvmet_ns_device_path_show+0x44/0x68
   configfs_read_iter+0x284/0x46c
  The buggy address is located 0 bytes to the right of
   allocated 4096-byte region

dhchap_key and dhchap_ctrl_key with "DHHC-1:00:" followed by 4085
bytes give the same report.

Use scnprintf() with PAGE_SIZE, as the passthru device_path show
already does. A value that does not fit loses its trailing newline
instead of overflowing the buffer.

Fixes: a07b4970f464 ("nvmet: add a generic NVMe target")
Fixes: db1312dd9548 ("nvmet: implement basic In-Band Authentication")
Cc: stable@vger.kernel.org
Signed-off-by: Lin Tzu Yang <rongh7426@gmail.com>
---
Tested on configfs-next 2e0fad9d2eae in a QEMU/KVM arm64 guest with
KASAN and kasan_multi_shot. Writing 4095 bytes to each of the three
attributes and reading it back gave one KASAN report per attribute
before the patch, and none after; reads return 4095 bytes. Normal
values such as /dev/nullb0 and DHHC-1:00:abcd: still read back with
their newline, and enabling the namespace still works. Build-tested
with W=1 on nvme-7.4.

Found by writing boundary values to every nvmet configfs attribute.

This patch was written with the help of an AI coding assistant
(Claude). I ran the tests above and reviewed the change.

 drivers/nvme/target/configfs.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
index 27c50fa533..f88a295bc1 100644
--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -531,7 +531,8 @@ CONFIGFS_ATTR(nvmet_, addr_tsas);
  */
 static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
 {
-	return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
+	return scnprintf(page, PAGE_SIZE, "%s\n",
+			 to_nvmet_ns(item)->device_path);
 }
 
 static ssize_t nvmet_ns_device_path_store(struct config_item *item,
@@ -2242,7 +2243,7 @@ static ssize_t nvmet_host_dhchap_key_show(struct config_item *item,
 	if (!dhchap_secret)
 		ret = sprintf(page, "\n");
 	else
-		ret = sprintf(page, "%s\n", dhchap_secret);
+		ret = scnprintf(page, PAGE_SIZE, "%s\n", dhchap_secret);
 	up_read(&nvmet_config_sem);
 	return ret;
 }
@@ -2275,7 +2276,7 @@ static ssize_t nvmet_host_dhchap_ctrl_key_show(struct config_item *item,
 	if (!dhchap_secret)
 		ret = sprintf(page, "\n");
 	else
-		ret = sprintf(page, "%s\n", dhchap_secret);
+		ret = scnprintf(page, PAGE_SIZE, "%s\n", dhchap_secret);
 	up_read(&nvmet_config_sem);
 	return ret;
 }
-- 
2.47.3



             reply	other threads:[~2026-11-30 11:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 11:47 Lin Tzu Yang [this message]
2026-11-30 11:59 ` [PATCH] nvmet: fix one-byte overflow in configfs string show callbacks sashiko-bot
2026-12-01  8:01 ` 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=20261130114711.842577-1-rongh7426@gmail.com \
    --to=rongh7426@gmail.com \
    --cc=hare@suse.de \
    --cc=hch@lst.de \
    --cc=kbusch@kernel.org \
    --cc=kch@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.