mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Anh Nguyen Hoang via B4 Relay <devnull+anhnh.4work.gmail.com@kernel.org>
To: Josef Bacik <josef@toxicpanda.com>, Jens Axboe <axboe@kernel.dk>
Cc: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>,
	 Xiubo Li <xiubli@redhat.com>, Ming Lei <ming.lei@redhat.com>,
	 linux-block@vger.kernel.org, nbd@other.debian.org,
	 linux-kernel@vger.kernel.org,
	Anh Nguyen Hoang <anhnh.4work@gmail.com>
Subject: [PATCH] nbd: use sysfs_emit() in backend_show()
Date: Tue, 01 Dec 2026 21:41:14 -0600	[thread overview]
Message-ID: <20261201-kbug-l951-v1-1-dd4985088130@gmail.com> (raw)

From: Anh Nguyen Hoang <anhnh.4work@gmail.com>

backend_show() prints nbd->backend into the sysfs buffer with
sprintf(). The string is the NBD_ATTR_BACKEND_IDENTIFIER attribute
that nbd_genl_connect() stores with nla_strdup(), and the netlink
policy for that attribute is NLA_STRING without a maximum length, so
it can be much longer than a page. The sysfs buffer is only PAGE_SIZE
bytes. If a device was connected with an identifier longer than
PAGE_SIZE - 2 bytes, reading /sys/block/nbdX/backend writes past the
end of the buffer.

Use sysfs_emit(), which limits the output to PAGE_SIZE. An identifier
that does not fit is truncated when shown.

Found by an automated code-checking tool. Reproduced with KASAN, which
reports a slab-out-of-bounds write in backend_show() when the attribute
of a device connected with a 7999-byte identifier is read. With this
change the report is gone and the read returns 4095 bytes.

Fixes: 6497ef8df568 ("nbd: provide a way for userspace processes to identify device backends")
Signed-off-by: Anh Nguyen Hoang <anhnh.4work@gmail.com>
---
The NBD_ATTR_BACKEND_IDENTIFIER policy is left without a length limit, so
long identifiers are still accepted at connect time and only the sysfs
output is truncated. A .len in nbd_attr_policy could be added separately
if rejecting long identifiers at connect time is preferred.

Runtime test: QEMU, x86_64 defconfig + KASAN + BLK_DEV_NBD, on 348d25516523.
A 7999-byte identifier gives a KASAN slab-out-of-bounds report on read
before the patch, none after; a 12-byte one reads back unchanged in both.

Testing:
- Compile-tested on x86_64 (allmodconfig, W=1): drivers/block/nbd.o; no
  new warnings.
- checkpatch.pl --strict: no errors or warnings.

This patch was drafted with AI assistance (Claude, claude-fable-5-2).
---
 drivers/block/nbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index b2b27f90a7ed..f213ccd69463 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -254,7 +254,7 @@ static ssize_t backend_show(struct device *dev,
 	struct gendisk *disk = dev_to_disk(dev);
 	struct nbd_device *nbd = disk->private_data;
 
-	return sprintf(buf, "%s\n", nbd->backend ?: "");
+	return sysfs_emit(buf, "%s\n", nbd->backend ?: "");
 }
 
 static const struct device_attribute backend_attr = {

---
base-commit: 348d25516523c3edfec477d56abde2b2491dbf9d
change-id: 20261201-kbug-l951-277367bb8632

Best regards,
--  
Anh Nguyen Hoang <anhnh.4work@gmail.com>



             reply	other threads:[~2026-12-02  3:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  3:41 Anh Nguyen Hoang via B4 Relay [this message]
2026-12-02  3:42 Zijie Zhao via B4 Relay

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=20261201-kbug-l951-v1-1-dd4985088130@gmail.com \
    --to=devnull+anhnh.4work.gmail.com@kernel.org \
    --cc=anhnh.4work@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=josef@toxicpanda.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ming.lei@redhat.com \
    --cc=nbd@other.debian.org \
    --cc=prasanna.kalever@redhat.com \
    --cc=xiubli@redhat.com \
    /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

all inboxes | Powered by JetHome®