Re: [PATCH] scsi: target: iscsi: reject zero-length Extended CDB AHS

Carlos Bilbao
2026-04-09T02:23:50Z

Hello,

On 4/7/26 02:23, Dmitry Bogdanov wrote:
> On Fri, Apr 03, 2026 at 06:44:29PM -0700, carlos.bilbao@kernel.org wrote:
>> From: Carlos Bilbao <carlos.bilbao@kernel.org>
>>
>> If ecdb_ahdr->ahslength is zero, two bugs follow:
>>
>> kmalloc(be16_to_cpu(ecdb_ahdr->ahslength) + 15, ...)
>>
>> allocates 15 bytes, but the immediately following memcpy writes
>> ISCSI_CDB_SIZE (16) bytes into it, a one-byte heap overflow. Also:
>>
>> memcpy(cdb + ISCSI_CDB_SIZE, ecdb_ahdr->ecdb,
>> be16_to_cpu(ecdb_ahdr->ahslength) - 1);
>>
>> (u16)0 - 1 promotes to (int)-1 which converts to SIZE_MAX as size_t,
>> causing a massive out-of-bounds write.
>>
>> Reject ahslength == 0 with ISCSI_REASON_PROTOCOL_ERROR before the kmalloc.
>>
>> Fixes: 8f1f7d297bce ("scsi: target: iscsi: Add support for extended CDB AHS")
>> Signed-off-by: Carlos Bilbao (Lambda) <carlos.bilbao@kernel.org>
>> ---
>> drivers/target/iscsi/iscsi_target.c | 15 +++++++++++----
>> 1 file changed, 11 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c
>> index e80449f6ce15..8db24d35c762 100644
>> --- a/drivers/target/iscsi/iscsi_target.c
>> +++ b/drivers/target/iscsi/iscsi_target.c
>> @@ -1100,6 +1100,8 @@ int iscsit_setup_scsi_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
>> cdb = hdr->cdb;
>>
>> if (hdr->hlength) {
>> + u16 ahslength;
>> +
>> 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 +1110,19 @@ 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);
>> + ahslength = be16_to_cpu(ecdb_ahdr->ahslength);
>> + if (!ahslength) {
>> + pr_err("Extended CDB AHS with zero length, protocol error.\n");
>> + return iscsit_add_reject_cmd(cmd,
>> + ISCSI_REASON_PROTOCOL_ERROR, buf);
>> + }
>> +
> For a complete solution please add a check that AHS fits in the buffer.
> ahslength must be less or equal than ((hdr->hlength * 4) - 3).

For sure, I'll send a v2 including that.

>
>> + cdb = kmalloc(ahslength + 15, GFP_KERNEL);
> It took some time to recall what did 15 mean. May be make it clear for
> everyone too?
>
> u16 cdb_length = ahslength - 1 + ISCSI_CDB_SIZE;
> cdb = kmalloc(cdb_length, GFP_KERNEL);
> memcpy(cdb, hdr->cdb, ISCSI_CDB_SIZE);
> memcpy(cdb + ISCSI_CDB_SIZE, ecdb_ahdr->ecdb, cdb_length - ISCSI_CDB_SIZE);
>

Agreed!

>> 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, ahslength - 1);
>> }
>>
>> data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
>> --
>> 2.43.0
>>

Thanks,

Carlos