* [PATCH] scsi: target: iscsi: validate ECDB AHS length
@ 2026-05-07 14:25 Jeremy Erazo (Devel Group)
0 siblings, 0 replies; only message in thread
From: Jeremy Erazo (Devel Group) @ 2026-05-07 14:25 UTC (permalink / raw)
To: Martin K . Petersen
Cc: linux-scsi, target-devel, linux-kernel,
Jeremy Erazo (Devel Group), stable
iscsit_setup_scsi_cmd() processes the Extended-CDB Additional Header
Segment (AHS) of a SCSI Command PDU without bounding AHSLength,
despite the long-standing "FIXME; Add checks for AdditionalHeaderSegment"
comment a few lines above in the same function.
A SCSI Command PDU sent after iSCSI Login with hlength=1,
ahstype=ISCSI_AHSTYPE_CDB and ahslength=0 reaches:
cdb = kmalloc(0 + 15, GFP_KERNEL); /* 15-byte alloc */
memcpy(cdb, hdr->cdb, ISCSI_CDB_SIZE); /* 16 -> 15 */
memcpy(cdb + ISCSI_CDB_SIZE, ecdb_ahdr->ecdb,
be16_to_cpu(ecdb_ahdr->ahslength) - 1); /* (size_t)-1 */
On CONFIG_FORTIFY_SOURCE=y kernels the first memcpy is rejected by
__fortify_panic() because the declared destination size is 15:
memcpy: detected buffer overflow: 16 byte write of buffer size 15
kernel BUG at lib/string_helpers.c:1044!
Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI
RIP: 0010:__fortify_panic+0xd/0xf
Call Trace:
iscsit_setup_scsi_cmd.cold+0x8c/0x224
iscsit_get_rx_pdu+0x9ec/0x1740
iscsi_target_rx_thread+0xf7/0x1f0
kthread+0x1b4/0x200
Kernel panic - not syncing: Fatal exception
On kernels without CONFIG_FORTIFY_SOURCE the first memcpy fits in the
kmalloc-16 slab object and execution reaches the second memcpy whose
size argument has wrapped to (size_t)-1.
Reproduced on Linux 7.0 with a malformed Command PDU sent after a
completed iSCSI Login. The trigger is reachable post-Login by any
initiator that successfully logged in (anonymous on demo-mode targets,
authenticated on CHAP-protected targets). No claim of RCE, LPE or
controlled write is made.
Validate, before any dereference and any allocation:
- the AHS area received from the socket holds at least the 4-byte
iscsi_ecdb_ahdr header,
- AHSLength is at least 1 (RFC 7143 §10.2.2.3 minimum for the ECDB
AHS, which carries one reserved byte),
- the declared AHSLength does not exceed the AHS bytes that were
actually received.
Reported-by: Jeremy Erazo (trexnegr0) <mendozayt13@gmail.com>
Signed-off-by: Jeremy Erazo (Devel Group) <mendozayt13@gmail.com>
Cc: stable@vger.kernel.org
---
drivers/target/iscsi/iscsi_target.c | 33 +++++++++++++++++++++++++----
1 file changed, 29 insertions(+), 4 deletions(-)
diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c
index e80449f6c..de291eb6f 100644
--- a/drivers/target/iscsi/iscsi_target.c
+++ b/drivers/target/iscsi/iscsi_target.c
@@ -1100,6 +1100,16 @@ int iscsit_setup_scsi_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
cdb = hdr->cdb;
if (hdr->hlength) {
+ u16 ahslen;
+ unsigned int ahs_area_bytes = hdr->hlength * 4;
+
+ /* The AHS area must hold at least the iscsi_ecdb_ahdr
+ * header before any of its fields may be dereferenced.
+ */
+ if (ahs_area_bytes < sizeof(struct iscsi_ecdb_ahdr))
+ return iscsit_add_reject_cmd(cmd,
+ ISCSI_REASON_PROTOCOL_ERROR, buf);
+
ecdb_ahdr = (struct iscsi_ecdb_ahdr *) (hdr + 1);
if (ecdb_ahdr->ahstype != ISCSI_AHSTYPE_CDB) {
pr_err("Additional Header Segment type %d not supported!\n",
@@ -1108,14 +1118,29 @@ int iscsit_setup_scsi_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
ISCSI_REASON_CMD_NOT_SUPPORTED, buf);
}
- cdb = kmalloc(be16_to_cpu(ecdb_ahdr->ahslength) + 15,
- GFP_KERNEL);
+ /* Per RFC 7143 §10.2.2.3 AHSLength counts the bytes of
+ * the AHS that follow the AHSType/AHSLength fields; for
+ * the ECDB AHS it includes one reserved byte, so the
+ * smallest legal value is 1. Rejecting 0 prevents the
+ * "ahslen - 1" memcpy size below from underflowing to
+ * (size_t)-1, and ensures the kmalloc(ahslen + 15) below
+ * is at least ISCSI_CDB_SIZE (16) so the first memcpy
+ * does not overflow. Also reject any AHSLength larger
+ * than the AHS bytes that actually reached us.
+ */
+ ahslen = be16_to_cpu(ecdb_ahdr->ahslength);
+ if (ahslen < 1 ||
+ ahslen - 1 > ahs_area_bytes -
+ offsetof(struct iscsi_ecdb_ahdr, ecdb))
+ return iscsit_add_reject_cmd(cmd,
+ ISCSI_REASON_PROTOCOL_ERROR, buf);
+
+ cdb = kmalloc(ahslen + 15, GFP_KERNEL);
if (cdb == NULL)
return iscsit_add_reject_cmd(cmd,
ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
memcpy(cdb, hdr->cdb, ISCSI_CDB_SIZE);
- memcpy(cdb + ISCSI_CDB_SIZE, ecdb_ahdr->ecdb,
- be16_to_cpu(ecdb_ahdr->ahslength) - 1);
+ memcpy(cdb + ISCSI_CDB_SIZE, ecdb_ahdr->ecdb, ahslen - 1);
}
data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
base-commit: a293ec25d59dd96309058c70df5a4dd0f889a1e4
--
2.53.0
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-05-07 14:33 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-07 14:25 [PATCH] scsi: target: iscsi: validate ECDB AHS length Jeremy Erazo (Devel Group)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox