Linux-NVME Archive on 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: linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: [PATCH] nvmet: use scnprintf() in the passthru device_path show
Date: Tue,  1 Dec 2026 19:04:48 +0800	[thread overview]
Message-ID: <20261201110448.893469-1-rongh7426@gmail.com> (raw)

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



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

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261201110448.893469-1-rongh7426@gmail.com \
    --to=rongh7426@gmail.com \
    --cc=hch@lst.de \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox