scsi: target: iscsi: reject zero-length Extended CDB AHS
Subsystems (2)
PATCH 1/1 scsi: target: iscsi: reject zero-length Extended CDB AHS
From: Carlos Bilbao <[email protected]>
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.
--- drivers/target/iscsi/iscsi_target.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/target/iscsi/iscsi_target.c +11 -4
@@ -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); + } + + cdb =kmalloc (ahslength +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, ahslength -1 ); } data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
Signature · 1 line
2.43.0
Quoted 39 lines · On Fri, Apr 03, 2026 at 06:44:29PM -0700, [email protected] wrote:
From: Carlos Bilbao <[email protected]>
If
ecdb_ahdr->ahslengthis 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_MAXassize_t, causing a massive out-of-bounds write.Reject ahslength == 0 with
ISCSI_REASON_PROTOCOL_ERRORbefore the kmalloc.Fixes 8f1f7d297bce ("scsi: target: iscsi: Add support for extended CDB AHS")Signed-off-by Carlos Bilbao (Lambda) <[email protected]>--- drivers/target/iscsi/iscsi_target.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)drivers/target/iscsi/iscsi_target.c +2
-0@@ -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); }
drivers/target/iscsi/iscsi_target.c int iscsit_setup_scsi_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd, · line 1119
Quoted 10 lines
drivers/target/iscsi/iscsi_target.c +7
-2- 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).
drivers/target/iscsi/iscsi_target.c int iscsit_setup_scsi_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd, · line 1120
Quoted 1 line
drivers/target/iscsi/iscsi_target.c +1
-0+ 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);
Quoted 13 lines
drivers/target/iscsi/iscsi_target.c +1
-3if (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
Hello,
Quoted 41 lines · Dmitry Bogdanov
Quoted 40 lines · On Fri, Apr 03, 2026 at 06:44:29PM -0700, [email protected] wrote:
From: Carlos Bilbao <[email protected]>
If
ecdb_ahdr->ahslengthis 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_MAXassize_t, causing a massive out-of-bounds write.Reject ahslength == 0 with
ISCSI_REASON_PROTOCOL_ERRORbefore the kmalloc.Fixes 8f1f7d297bce ("scsi: target: iscsi: Add support for extended CDB AHS")Signed-off-by Carlos Bilbao (Lambda) <[email protected]>--- drivers/target/iscsi/iscsi_target.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)drivers/target/iscsi/iscsi_target.c +2
-0@@ -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); }
Quoted 11 lines
drivers/target/iscsi/iscsi_target.c int iscsit_setup_scsi_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd, · line 1119
Quoted 9 lines
drivers/target/iscsi/iscsi_target.c +7
-2- 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.
Quoted 10 lines
drivers/target/iscsi/iscsi_target.c int iscsit_setup_scsi_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd, · line 1120
Quoted 2 lines
drivers/target/iscsi/iscsi_target.c +1
-0+ 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!
Quoted 9 lines
Quoted 9 lines
drivers/target/iscsi/iscsi_target.c +1
-2if (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 ); }
Quoted 4 lines
Quoted 4 lines
drivers/target/iscsi/iscsi_target.c +0
-1data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE : --2.43.0
Thanks,
Carlos
PATCH v2 1/1 scsi: target: iscsi: reject invalid size Extended CDB AHS
In reply to [email protected]From: Carlos Bilbao <[email protected]>
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. Also reject ahslength values that exceed the actual AHS buffer advertised.
Changes in v2:
- Add bounds check: ahslength must not exceed (hdr->hlength * 4) - 3.
- Replace opaque ahslength + 15 with explicit cdb_length variable.
--- drivers/target/iscsi/iscsi_target.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)
drivers/target/iscsi/iscsi_target.c +19 -4
@@ -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,27 @@ 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); + } +if (ahslength > (hdr->hlength *4 ) -3 ) { +pr_err ("Extended CDB AHS length %u exceeds available buffer.\n" , + ahslength); +return iscsit_add_reject_cmd (cmd, +ISCSI_REASON_PROTOCOL_ERROR , buf); + } + +u16 cdb_length = ahslength -1 +ISCSI_CDB_SIZE ; + + cdb =kmalloc (cdb_length,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, cdb_length -ISCSI_CDB_SIZE ); } data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
Signature · 1 line
2.43.0
Re: [PATCH v2] scsi: target: iscsi: reject invalid size Extended CDB AHS
Quoted 19 lines · On Wed, Apr 08, 2026 at 07:42:53PM -0700, [email protected] wrote:
From: Carlos Bilbao <[email protected]>
If
ecdb_ahdr->ahslengthis 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_MAXassize_t, causing a massive out-of-bounds write.Reject ahslength == 0 with
ISCSI_REASON_PROTOCOL_ERRORbefore the kmalloc. Also reject ahslength values that exceed the actual AHS buffer advertised.
Quoted 7 lines
Changes in v2:
- Add bounds check: ahslength must not exceed (
hdr->hlength* 4) - 3.
- Replace opaque ahslength + 15 with explicitcdb_lengthvariable.Fixes 8f1f7d297bce ("scsi: target: iscsi: Add support for extended CDB AHS")Signed-off-by Carlos Bilbao (Lambda) <[email protected]>
Quoted 27 lines
--- drivers/target/iscsi/iscsi_target.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)drivers/target/iscsi/iscsi_target.c +6
-2@@ -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,27 @@ 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,
drivers/target/iscsi/iscsi_target.c int iscsit_setup_scsi_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd, · line 1126
Quoted 10 lines
drivers/target/iscsi/iscsi_target.c +10
-0+ ISCSI_REASON_PROTOCOL_ERROR, buf); + } +if (ahslength > (hdr->hlength *4 ) -3 ) { +pr_err ("Extended CDB AHS length %u exceeds available buffer.\n" , + ahslength); +return iscsit_add_reject_cmd (cmd, +ISCSI_REASON_PROTOCOL_ERROR , buf); + } + +u16 cdb_length = ahslength -1 +ISCSI_CDB_SIZE ;
AFAIK, a variable declarationis allowed to be in the beginning of code block only.
Quoted 15 lines
drivers/target/iscsi/iscsi_target.c +3
-3+ + cdb =kmalloc (cdb_length,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, cdb_length -ISCSI_CDB_SIZE ); } data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE : --2.43.0
Re: [PATCH v2] scsi: target: iscsi: reject invalid size Extended CDB AHS
Hello,
Quoted 58 lines · Dmitry Bogdanov
Quoted 18 lines · On Wed, Apr 08, 2026 at 07:42:53PM -0700, [email protected] wrote:
From: Carlos Bilbao <[email protected]>
If
ecdb_ahdr->ahslengthis 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_MAXassize_t, causing a massive out-of-bounds write.Reject ahslength == 0 with
ISCSI_REASON_PROTOCOL_ERRORbefore the kmalloc. Also reject ahslength values that exceed the actual AHS buffer advertised.Quoted 7 lines
Changes in v2:
- Add bounds check: ahslength must not exceed (
hdr->hlength* 4) - 3.
- Replace opaque ahslength + 15 with explicitcdb_lengthvariable.Fixes 8f1f7d297bce ("scsi: target: iscsi: Add support for extended CDB AHS")Signed-off-by Carlos Bilbao (Lambda) <[email protected]>Reviewed-by Dmitry Bogdanov <[email protected]>Quoted 30 lines
--- drivers/target/iscsi/iscsi_target.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)drivers/target/iscsi/iscsi_target.c +9
-2@@ -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,27 @@ 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); + } +if (ahslength > (hdr->hlength *4 ) -3 ) {
Quoted 8 lines
drivers/target/iscsi/iscsi_target.c int iscsit_setup_scsi_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd, · line 1126
Quoted 7 lines
drivers/target/iscsi/iscsi_target.c +7
-0+pr_err ("Extended CDB AHS length %u exceeds available buffer.\n" , + ahslength); +return iscsit_add_reject_cmd (cmd, +ISCSI_REASON_PROTOCOL_ERROR , buf); + } + +u16 cdb_length = ahslength -1 +ISCSI_CDB_SIZE ;AFAIK, a variable declarationis allowed to be in the beginning of code block only.
You're absolutely right, happy to send v3 if the maintainer prefers.
Quoted 12 lines
Quoted 12 lines
drivers/target/iscsi/iscsi_target.c +3
-2+ + cdb =kmalloc (cdb_length,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, cdb_length -ISCSI_CDB_SIZE ); }
Quoted 4 lines
Quoted 4 lines
drivers/target/iscsi/iscsi_target.c +0
-1data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE : --2.43.0
Thanks,
Carlos
Re: [PATCH v2] scsi: target: iscsi: reject invalid size Extended CDB AHS
Carlos,
Quoted 4 lines
Quoted 2 lines
Quoted 1 line
+ u16
cdb_length= ahslength - 1 +ISCSI_CDB_SIZE;AFAIK, a variable declarationis allowed to be in the beginning of code block only.
You're absolutely right, happy to send v3 if the maintainer prefers.
Yes, please. Best to stay consistent with the existing coding style in a given file.
Signature · 1 line
Martin K. Petersen
PATCH v3 1/1 scsi: target: iscsi: reject invalid size Extended CDB AHS
From: Carlos Bilbao <[email protected]>
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. Also reject ahslength values that exceed the actual AHS buffer advertised.
Changes in v3:
- Move ahslength and cdb_length declarations to function scope following the reverse christmas tree ordering of existing locals.
Changes in v2:
- Add bounds check: ahslength must not exceed (hdr->hlength * 4) - 3.
- Replace opaque ahslength + 15 with explicit cdb_length variable.
--- drivers/target/iscsi/iscsi_target.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-)
drivers/target/iscsi/iscsi_target.c +18 -4
@@ -995,6 +995,7 @@ int iscsit_setup_scsi_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd,int data_direction, payload_length;struct iscsi_ecdb_ahdr *ecdb_ahdr;struct iscsi_scsi_req *hdr; +u16 ahslength, cdb_length;int iscsi_task_attr;unsigned char *cdb;int sam_task_attr;
@@ -1108,14 +1109,27 @@ 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); + } +if (ahslength > (hdr->hlength *4 ) -3 ) { +pr_err ("Extended CDB AHS length %u exceeds available PDU buffer.\n" , + ahslength); +return iscsit_add_reject_cmd (cmd, +ISCSI_REASON_PROTOCOL_ERROR , buf); + } + + cdb_length = ahslength -1 +ISCSI_CDB_SIZE ; + + cdb =kmalloc (cdb_length,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, cdb_length -ISCSI_CDB_SIZE ); } data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
Signature · 1 line
2.43.0
Martin K. Petersen
AppliedRe: [PATCH v3] scsi: target: iscsi: reject invalid size Extended CDB AHS
Carlos,
Quoted 6 lines
If
ecdb_ahdr->ahslengthis 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:
Applied to 7.1/scsi-staging, thanks!
Signature · 1 line
Martin K. Petersen
Re: [PATCH v2] scsi: target: iscsi: reject invalid size Extended CDB AHS
In reply to Martin K. PetersenQuoted 7 lines · Martin K. Petersen
Carlos,
Quoted 3 lines
Quoted 2 lines
Quoted 1 line
+ u16
cdb_length= ahslength - 1 +ISCSI_CDB_SIZE;AFAIK, a variable declarationis allowed to be in the beginning of code block only.
You're absolutely right, happy to send v3 if the maintainer prefers.
Yes, please. Best to stay consistent with the existing coding style in a given file.
Sure, v3 sent!
Quoted 1 line
Thanks,
Carlos