Skip to contentSkip to conversation

scsi: target: fix one-byte overflow in dbroot show

PATCH New Lin Tzu Yang 1 participant 1 message Dec 1

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: target: fix one-byte overflow in dbroot show

dbroot_store() accepts up to DB_ROOT_LEN - 1 = 4095 bytes, as long as the string names an existing directory. A path made of 4095 slashes resolves to "/", so it is accepted and stored as is.

dbroot_show() then prints it with sprintf(page, "%s\n", db_root), which writes 4095 characters, the newline and the terminating NUL into the PAGE_SIZE configfs buffer: 4097 bytes. KASAN reports it on the first read:

  BUG: KASAN: slab-out-of-bounds in vsnprintf+0xdd8/0xdfc
  Write of size 1 at addr ffff000012a37000 by task cat/109
  ...
   sprintf+0xb0/0xec
   target_core_item_dbroot_show+0x28/0x40
   configfs_read_iter+0x284/0x46c

Use sysfs_emit(), which is bounded by PAGE_SIZE. It is already used elsewhere in this file.

Fixes a96e9783e058 ("target: make target db location configurable")

---
The bug was found with a small configfs fuzzing script and the fix was written with AI assistance (Claude); I reviewed and tested it. The patch is against scsi for-next; it was tested in QEMU (arm64, KASAN) on configfs-next (2e0fad9d2eae), where this function is identical. Before: KASAN report on reading back a 4095-byte dbroot.
After: no report, the read returns the 4095 characters.

 drivers/target/target_core_configfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/target/target_core_configfs.c +1 -1
index 2b19a95600..199e5e79b7 100644
@@ -101,7 +101,7 @@ static char db_root_stage[DB_ROOT_LEN];
 static ssize_t target_core_item_dbroot_show(struct config_item *item,
 					    char *page)
 {
-	return sprintf(page, "%s\n", db_root);
+	return sysfs_emit(page, "%s\n", db_root);
 }
 
 static ssize_t target_core_item_dbroot_store(struct config_item *item,
Signature · 1 line
2.47.3