mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] nvmet: use scnprintf() in the passthru device_path show
@ 2026-12-01 11:04 Lin Tzu Yang
  0 siblings, 0 replies; only message in thread
From: Lin Tzu Yang @ 2026-12-01 11:04 UTC (permalink / raw)
  To: Christoph Hellwig, Sagi Grimberg, Chaitanya Kulkarni
  Cc: linux-nvme, linux-kernel

nvmet_passthru_device_path_show() returns the value of snprintf(),
which is the length the output would have had. device_path keeps up
to 4095 bytes, and for such a path the string plus newline does not
fit: snprintf() stores 4095 characters and the NUL, and returns 4096.
configfs then hands all 4096 bytes to userspace, so the read ends
with a NUL byte instead of the last character of the path.

Use scnprintf(), which returns the number of characters actually
stored. A path that does not fit now loses only its trailing newline.

Fixes: cae5b01a2afc ("nvmet: introduce the passthru configfs interface")
Signed-off-by: Lin Tzu Yang <rongh7426@gmail.com>
---
Follow-up to "nvmet: fix one-byte overflow in configfs string show
callbacks" (20261130114711.842577-1-rongh7426@gmail.com), for the
attribute Sashiko pointed out there. The two patches touch different
functions and apply in either order. I checked the other "%s" show
callbacks in this file: the remaining ones print fixed-size or
length-checked strings.

Tested on configfs-next 2e0fad9d2eae in a QEMU/KVM arm64 guest.
Writing 4095 bytes to subsystems/<subsys>/passthru/device_path and
reading it back returned 4096 bytes ending in a NUL before the patch,
and the 4095 bytes that were written after it. A normal path such as
/dev/nvme0 reads back the same as before. Build-tested with W=1 on
nvme-7.4.

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 | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
index 27c50fa533..ad36bcd90a 100644
--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -915,9 +915,9 @@ static ssize_t nvmet_passthru_device_path_show(struct config_item *item,
 	ssize_t ret;
 
 	mutex_lock(&subsys->lock);
-	ret = snprintf(page, PAGE_SIZE, "%s\n",
-		       subsys->passthru && subsys->passthru->ctrl_path ?
-		       subsys->passthru->ctrl_path : "");
+	ret = scnprintf(page, PAGE_SIZE, "%s\n",
+			subsys->passthru && subsys->passthru->ctrl_path ?
+			subsys->passthru->ctrl_path : "");
 	mutex_unlock(&subsys->lock);
 	return ret;
 }
-- 
2.47.3


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-01 11:04 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-01 11:04 [PATCH] nvmet: use scnprintf() in the passthru device_path show 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®