* [PATCH] scsi: target: configfs: Bound the output of target_dev_lba_map_show()
@ 2026-12-04 2:54 Anh Nguyen Hoang via B4 Relay
0 siblings, 0 replies; only message in thread
From: Anh Nguyen Hoang via B4 Relay @ 2026-12-04 2:54 UTC (permalink / raw)
To: Martin K. Petersen
Cc: Hannes Reinecke, linux-scsi, target-devel, linux-kernel,
Anh Nguyen Hoang
From: Anh Nguyen Hoang <anhnh.4work@gmail.com>
target_dev_lba_map_show() appends every map and member with sprintf()
and never checks the room left in the configfs page. The stored map can
print longer than the 4095 bytes it was written with. A pg_id above
INT_MAX is stored as a negative number, which prints one character
longer. Every map line also gets a newline even if the input had none.
Reading lba_map back then writes past the end of the page:
BUG: KASAN: slab-out-of-bounds in number+0x85d/0x8b0
Write of size 1 at addr ff11000002f69000 by task init/1
...
sprintf+0xb1/0xe0
target_dev_lba_map_show+0x20a/0x330
configfs_read_iter+0x2ec/0x5d0
Use scnprintf() with the remaining space instead.
Found by a static analysis tool.
Fixes: 229d4f112fd6 ("target_core_alua: Referrals configfs integration")
Signed-off-by: Anh Nguyen Hoang <anhnh.4work@gmail.com>
---
Tested in QEMU (x86_64, defconfig + TARGET_CORE, with and without KASAN)
on v7.4-rc4+ (9437ee8b6cf6), where this function is the same as on scsi
for-next. As root, a map was written to the lba_map attribute of an
rd_mcp device and read back.
Before: with 314 members whose pg_id is 2147483648 and up (4089 bytes
in, 4404 out), KASAN reports the write above. Without KASAN, the read
hits the WARN_ON_ONCE() in fill_read_buffer() and returns -EIO. With
valid pg_ids and a 4095-byte map that has no final newline, one NUL
byte lands past the buffer (seen with slub_debug=FZ).
After: no report in either case, and the output stops at 4095 bytes. A
small map reads back unchanged.
No Cc: stable: only root can write this attribute.
This patch was drafted with AI assistance (Claude, claude-fable-5-2).
Testing:
- Compile-tested on x86_64 (allmodconfig, W=1):
drivers/target/target_core_configfs.o; no new warnings.
- checkpatch.pl --strict: no errors or warnings.
---
drivers/target/target_core_configfs.c | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/drivers/target/target_core_configfs.c b/drivers/target/target_core_configfs.c
index 2b19a956007b..bb35d2dc48ad 100644
--- a/drivers/target/target_core_configfs.c
+++ b/drivers/target/target_core_configfs.c
@@ -2601,12 +2601,12 @@ static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
spin_lock(&dev->t10_alua.lba_map_lock);
if (!list_empty(&dev->t10_alua.lba_map_list))
- bl += sprintf(b + bl, "%u %u\n",
- dev->t10_alua.lba_map_segment_size,
- dev->t10_alua.lba_map_segment_multiplier);
+ bl += scnprintf(b + bl, PAGE_SIZE - bl, "%u %u\n",
+ dev->t10_alua.lba_map_segment_size,
+ dev->t10_alua.lba_map_segment_multiplier);
list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
- bl += sprintf(b + bl, "%llu %llu",
- map->lba_map_first_lba, map->lba_map_last_lba);
+ bl += scnprintf(b + bl, PAGE_SIZE - bl, "%llu %llu",
+ map->lba_map_first_lba, map->lba_map_last_lba);
list_for_each_entry(mem, &map->lba_map_mem_list,
lba_map_mem_list) {
switch (mem->lba_map_mem_alua_state) {
@@ -2626,10 +2626,10 @@ static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
state = '.';
break;
}
- bl += sprintf(b + bl, " %d:%c",
- mem->lba_map_mem_alua_pg_id, state);
+ bl += scnprintf(b + bl, PAGE_SIZE - bl, " %d:%c",
+ mem->lba_map_mem_alua_pg_id, state);
}
- bl += sprintf(b + bl, "\n");
+ bl += scnprintf(b + bl, PAGE_SIZE - bl, "\n");
}
spin_unlock(&dev->t10_alua.lba_map_lock);
return bl;
---
base-commit: 4ff20abe28d65a0c52f0b280008fcfb93c3ee2b2
change-id: 20261203-kbug-l986-e4975d0aef1a
Best regards,
--
Anh Nguyen Hoang <anhnh.4work@gmail.com>
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-12-04 2:54 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-04 2:54 [PATCH] scsi: target: configfs: Bound the output of target_dev_lba_map_show() Anh Nguyen Hoang 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®