[PATCH v2 2/8] scsi: target: keep REPORT REFERRALS stores inside the buffer
From: Jia Jia
Date: Tue Nov 17 2026 - 23:14:11 EST
target_emulate_report_referrals() checks only that the start of an LBA
field is inside cmd->data_length before storing all eight bytes. A
short response buffer can therefore make the store run past its end.
For example, a nine-byte allocation reaches the LBA at buf[8].
The response builder must still visit the remaining LBA maps to compute
the reported length. Encode each LBA in a local array and copy only
the part that fits in the command buffer.
KASAN reports:
BUG: KASAN: use-after-free in target_emulate_report_referrals+0x100/0x380 [target_core_mod]
Write of size 8
target_emulate_report_referrals
__target_execute_cmd
target_execute_cmd
transport_generic_new_cmd
Fixes: 38edd7245771 ("target_core_alua: check for buffer overflow")
Signed-off-by: Jia Jia <physicalmtea@xxxxxxxxx>
---
drivers/target/target_core_alua.c | 23 +++++++++++++++++++----
1 file changed, 19 insertions(+), 4 deletions(-)
diff --git a/drivers/target/target_core_alua.c b/drivers/target/target_core_alua.c
index 140154d93c430..c110914b7b78b 100644
--- a/drivers/target/target_core_alua.c
+++ b/drivers/target/target_core_alua.c
@@ -49,6 +49,19 @@ static LIST_HEAD(lu_gps_list);
struct t10_alua_lu_gp *default_lu_gp;
+static void
+target_emulate_report_referrals_copy_lba(unsigned char *buf, u32 off,
+ u32 data_length, u64 lba)
+{
+ unsigned char lba_buf[sizeof(lba)];
+
+ if (off >= data_length)
+ return;
+ put_unaligned_be64(lba, lba_buf);
+ memcpy(&buf[off], lba_buf,
+ min_t(u32, sizeof(lba_buf), data_length - off));
+}
+
/*
* REPORT REFERRALS
*
@@ -88,11 +101,13 @@ target_emulate_report_referrals(struct se_cmd *cmd)
int pg_num;
off += 4;
- if (cmd->data_length > off)
- put_unaligned_be64(map->lba_map_first_lba, &buf[off]);
+ target_emulate_report_referrals_copy_lba(buf, off,
+ cmd->data_length,
+ map->lba_map_first_lba);
off += 8;
- if (cmd->data_length > off)
- put_unaligned_be64(map->lba_map_last_lba, &buf[off]);
+ target_emulate_report_referrals_copy_lba(buf, off,
+ cmd->data_length,
+ map->lba_map_last_lba);
off += 8;
rd_len += 20;
pg_num = 0;