[PATCH] nvmet: use scnprintf() in the passthru device_path show

From: Lin Tzu Yang

Date: Tue Dec 01 2026 - 06:04:58 EST


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@xxxxxxxxx>
---
Follow-up to "nvmet: fix one-byte overflow in configfs string show
callbacks" (20261130114711.842577-1-rongh7426@xxxxxxxxx), 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