[PATCH v2] scsi: target: configfs: Bound the output of target_dev_lba_map_show()
From: Anh Nguyen Hoang via B4 Relay
Date: Thu Dec 03 2026 - 22:37:34 EST
From: Anh Nguyen Hoang <anhnh.4work@xxxxxxxxx>
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. Limit the output to
4096 bytes instead of PAGE_SIZE: configfs fails a read with a WARN and
-EIO when show() returns more than that, whatever the page size is.
Found by a static analysis tool.
Fixes: 229d4f112fd6 ("target_core_alua: Referrals configfs integration")
Signed-off-by: Anh Nguyen Hoang <anhnh.4work@xxxxxxxxx>
---
Changes in v2:
- Limit the output to 4096 bytes, the configfs maximum, not PAGE_SIZE (Sashiko)
- v1: https://lore.kernel.org/all/20261203-kbug-l986-v1-1-00584b0bc42a@xxxxxxxxx/
Tested in QEMU on v7.4-rc4+ (9437ee8b6cf6), where this function is the
same as on scsi for-next. As root, a map with 314 members whose pg_id
is 2147483648 and up (4089 bytes in, 4404 out) was written to the
lba_map attribute of an rd_mcp device and read back.
x86_64, 4K pages: before, KASAN reports the write above; without KASAN
the read hits the WARN_ON_ONCE() in fill_read_buffer() and returns -EIO.
arm64, 64K pages: before and with v1, no out-of-bounds write, but the
read hits the same WARN_ON_ONCE() and returns -EIO.
With v2, on both: no report, and the read returns the first 4095 bytes
of the map. Only maps with such pg_ids lose data this way: with pg_ids
that fit in an int, at most the final newline is cut. 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 | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/drivers/target/target_core_configfs.c b/drivers/target/target_core_configfs.c
index 2b19a956007b..e4bee3238086 100644
--- a/drivers/target/target_core_configfs.c
+++ b/drivers/target/target_core_configfs.c
@@ -2596,17 +2596,19 @@ static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
struct t10_alua_lba_map *map;
struct t10_alua_lba_map_member *mem;
char *b = page;
+ /* Derived from SIMPLE_ATTR_SIZE of fs/configfs/file.c */
+ const int size = 4096;
int bl = 0;
char state;
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, 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, 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 +2628,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, size - bl, " %d:%c",
+ mem->lba_map_mem_alua_pg_id, state);
}
- bl += sprintf(b + bl, "\n");
+ bl += scnprintf(b + bl, 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@xxxxxxxxx>