mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Anh Nguyen Hoang via B4 Relay <devnull+anhnh.4work.gmail.com@kernel.org>
To: "Martin K. Petersen" <mkp@kernel.org>
Cc: Hannes Reinecke <hare@suse.de>,
	linux-scsi@vger.kernel.org,  target-devel@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	 Anh Nguyen Hoang <anhnh.4work@gmail.com>
Subject: [PATCH] scsi: target: configfs: Bound the output of target_dev_lba_map_show()
Date: Thu, 03 Dec 2026 20:54:12 -0600	[thread overview]
Message-ID: <20261203-kbug-l986-v1-1-00584b0bc42a@gmail.com> (raw)

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>



                 reply	other threads:[~2026-12-04  2:54 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203-kbug-l986-v1-1-00584b0bc42a@gmail.com \
    --to=devnull+anhnh.4work.gmail.com@kernel.org \
    --cc=anhnh.4work@gmail.com \
    --cc=hare@suse.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=mkp@kernel.org \
    --cc=target-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®