* [PATCH] scsi: target: configfs: Bound the output of target_dev_lba_map_show()
@ 2026-12-04 2:54 Anh Nguyen Hoang via B4 Relay
2026-12-04 3:02 ` sashiko-bot
0 siblings, 1 reply; 3+ messages 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 related [flat|nested] 3+ messages in thread
* Re: [PATCH] scsi: target: configfs: Bound the output of target_dev_lba_map_show()
2026-12-04 2:54 [PATCH] scsi: target: configfs: Bound the output of target_dev_lba_map_show() Anh Nguyen Hoang via B4 Relay
@ 2026-12-04 3:02 ` sashiko-bot
[not found] ` <CABjBT5U5DUoT2UyZVT6SSxT4VAdSDna-RmnrAwf2kZZnQE86xg@mail.gmail.com>
0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-12-04 3:02 UTC (permalink / raw)
To: Anh Nguyen Hoang; +Cc: linux-scsi, target-devel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] scsi: target: target_dev_lba_map_show() exceeds 4KB configfs
limit
--
commit 3c3acef60382791667727ab0d330848e93b99d18
Author: Anh Nguyen Hoang <anhnh.4work@gmail.com>
scsi: target: configfs: Bound the output of target_dev_lba_map_show()
This commit replaces sprintf() with scnprintf() bounded by PAGE_SIZE - bl
in target_dev_lba_map_show() so formatting the LBA map cannot write past
the configfs page buffer.
> diff --git a/drivers/target/target_core_configfs.c b/drivers/target/target_core_configfs.c
> index 2b19a956007b7..bb35d2dc48add 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;
[Severity: Medium]
Can these scnprintf() calls still exceed configfs's 4096-byte attribute
limit on kernels where PAGE_SIZE > 4096?
In fs/configfs/file.c, fill_read_buffer() caps simple attributes at
SIMPLE_ATTR_SIZE (4096) regardless of PAGE_SIZE. On architectures with
larger pages such as 16KB or 64KB, an expanding LBA map can still format
past 4096 bytes, triggering WARN_ON_ONCE() and returning -EIO.
Bounding the output to min_t(size_t, PAGE_SIZE, 4096) (or exposing
SIMPLE_ATTR_SIZE) would avoid that on non-4KB-page kernels.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203-kbug-l986-v1-1-00584b0bc42a@gmail.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] scsi: target: configfs: Bound the output of target_dev_lba_map_show()
[not found] ` <CABjBT5U5DUoT2UyZVT6SSxT4VAdSDna-RmnrAwf2kZZnQE86xg@mail.gmail.com>
@ 2026-12-04 3:43 ` Anh Nguyen Hoang
0 siblings, 0 replies; 3+ messages in thread
From: Anh Nguyen Hoang @ 2026-12-04 3:43 UTC (permalink / raw)
To: sashiko-reviews; +Cc: linux-scsi, target-devel
On Fri, 04 Dec 2026 03:02:23 +0000, sashiko-bot@kernel.org wrote:
> Can these scnprintf() calls still exceed configfs's 4096-byte attribute
> limit on kernels where PAGE_SIZE > 4096?
Yes. On arm64 with 64K pages, v1 still hits the WARN_ON_ONCE() in
fill_read_buffer() and the read returns -EIO, the same as without the
patch.
v2 limits the output to 4096 bytes:
https://lore.kernel.org/all/20261203-kbug-l986-v2-1-a30c20afbc2f@gmail.com/
Anh
Vào Thứ 6, 4 thg 12, 2026 vào lúc 10:40 Anh Nguyen Hoang
<anhnh.4work@gmail.com> đã viết:
>
> On Fri, 04 Dec 2026 03:02:23 +0000, sashiko-bot@kernel.org wrote:
> > Can these scnprintf() calls still exceed configfs's 4096-byte attribute
> > limit on kernels where PAGE_SIZE > 4096?
>
> Yes. On arm64 with 64K pages, v1 still hits the WARN_ON_ONCE() in
> fill_read_buffer() and the read returns -EIO, the same as without the
> patch.
>
> v2 limits the output to 4096 bytes:
> https://lore.kernel.org/all/20261203-kbug-l986-v2-1-a30c20afbc2f@gmail.com/
>
> Anh
>
> Vào Thứ 6, 4 thg 12, 2026 vào lúc 10:02 <sashiko-bot@kernel.org> đã viết:
>>
>> Thank you for your contribution! Sashiko AI review found 1 potential
>> issue to consider:
>> - [Medium] scsi: target: target_dev_lba_map_show() exceeds 4KB configfs
>> limit
>> --
>>
>> commit 3c3acef60382791667727ab0d330848e93b99d18
>> Author: Anh Nguyen Hoang <anhnh.4work@gmail.com>
>>
>> scsi: target: configfs: Bound the output of target_dev_lba_map_show()
>>
>> This commit replaces sprintf() with scnprintf() bounded by PAGE_SIZE - bl
>> in target_dev_lba_map_show() so formatting the LBA map cannot write past
>> the configfs page buffer.
>>
>> > diff --git a/drivers/target/target_core_configfs.c b/drivers/target/target_core_configfs.c
>> > index 2b19a956007b7..bb35d2dc48add 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;
>>
>> [Severity: Medium]
>> Can these scnprintf() calls still exceed configfs's 4096-byte attribute
>> limit on kernels where PAGE_SIZE > 4096?
>>
>> In fs/configfs/file.c, fill_read_buffer() caps simple attributes at
>> SIMPLE_ATTR_SIZE (4096) regardless of PAGE_SIZE. On architectures with
>> larger pages such as 16KB or 64KB, an expanding LBA map can still format
>> past 4096 bytes, triggering WARN_ON_ONCE() and returning -EIO.
>>
>> Bounding the output to min_t(size_t, PAGE_SIZE, 4096) (or exposing
>> SIMPLE_ATTR_SIZE) would avoid that on non-4KB-page kernels.
>>
>> --
>> Sashiko AI review · https://sashiko.dev/#/patchset/20261203-kbug-l986-v1-1-00584b0bc42a@gmail.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-04 3:43 UTC | newest]
Thread overview: 3+ messages (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
2026-12-04 3:02 ` sashiko-bot
[not found] ` <CABjBT5U5DUoT2UyZVT6SSxT4VAdSDna-RmnrAwf2kZZnQE86xg@mail.gmail.com>
2026-12-04 3:43 ` Anh Nguyen Hoang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox