* [PATCH] nvmet: fix one-byte overflow in configfs string show callbacks
@ 2026-11-30 11:47 Lin Tzu Yang
0 siblings, 0 replies; only message in thread
From: Lin Tzu Yang @ 2026-11-30 11:47 UTC (permalink / raw)
To: Christoph Hellwig, Sagi Grimberg, Chaitanya Kulkarni
Cc: Keith Busch, Hannes Reinecke, linux-nvme, linux-kernel
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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-30 11:47 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30 11:47 [PATCH] nvmet: fix one-byte overflow in configfs string show callbacks Lin Tzu Yang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®