[PATCH] nbd: use sysfs_emit() in backend_show()
From: Zijie Zhao via B4 Relay
Date: Tue Dec 01 2026 - 22:42:15 EST
From: Zijie Zhao <zzjas98@xxxxxxxxx>
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@xxxxxxxxx>
---
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@xxxxxxxxx>