* [PATCH] nbd: use sysfs_emit() in backend_show()
@ 2026-12-02 3:41 Anh Nguyen Hoang via B4 Relay
0 siblings, 0 replies; 2+ messages in thread
From: Anh Nguyen Hoang via B4 Relay @ 2026-12-02 3:41 UTC (permalink / raw)
To: Josef Bacik, Jens Axboe
Cc: Prasanna Kumar Kalever, Xiubo Li, Ming Lei, linux-block, nbd,
linux-kernel, Anh Nguyen Hoang
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>
^ permalink raw reply [flat|nested] 2+ messages in thread* [PATCH] nbd: use sysfs_emit() in backend_show()
@ 2026-12-02 3:42 Zijie Zhao via B4 Relay
0 siblings, 0 replies; 2+ messages in thread
From: Zijie Zhao via B4 Relay @ 2026-12-02 3:42 UTC (permalink / raw)
To: Josef Bacik, Jens Axboe
Cc: Prasanna Kumar Kalever, Xiubo Li, Ming Lei, linux-block, nbd,
linux-kernel, Zijie Zhao
From: Zijie Zhao <zzjas98@gmail.com>
backend_show() prints nbd->backend into the sysfs buffer with an
unbounded sprintf(). The string is a copy of the
NBD_ATTR_BACKEND_IDENTIFIER netlink attribute made by nla_strdup() in
nbd_genl_connect(), and the attribute policy is a plain NLA_STRING
without a length limit. With an identifier longer than PAGE_SIZE - 2
bytes, each time sysfs formats /sys/block/nbdX/backend by calling
backend_show(), it writes past the end of the PAGE_SIZE buffer that
sysfs passes to the show callback.
Use sysfs_emit(), which bounds the output to PAGE_SIZE.
Found by a static analysis tool.
The fix and this changelog were drafted with AI assistance (Claude Code,
with a second review by OpenAI Codex), then reviewed by the submitter.
Fixes: 6497ef8df568 ("nbd: provide a way for userspace processes to identify device backends")
Signed-off-by: Zijie Zhao <zzjas98@gmail.com>
---
Testing:
- Compile-tested on x86_64 (allmodconfig, W=1): drivers/block/nbd.o; no
new warnings.
- checkpatch.pl --strict: no errors or warnings.
---
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-6ff3b7ee1deb
Best regards,
--
Zijie Zhao <zzjas98@gmail.com>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-02 3:42 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02 3:41 [PATCH] nbd: use sysfs_emit() in backend_show() Anh Nguyen Hoang via B4 Relay
2026-12-02 3:42 Zijie Zhao via B4 Relay
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®