mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: John Garry <john.g.garry@oracle.com>
To: Jeremy Erazo <mendozayt13@gmail.com>,
	target-devel@vger.kernel.org, linux-scsi@vger.kernel.org
Cc: "Martin K . Petersen" <martin.petersen@oracle.com>,
	Vincent Donnefort <vdonnefort@google.com>,
	Mike Christie <michael.christie@oracle.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH] scsi: target: iscsi: validate ECDB AHS length
Date: Tue, 2 Jun 2026 16:57:31 +0100	[thread overview]
Message-ID: <9a259155-594d-4242-b0c7-655665215cf6@oracle.com> (raw)
In-Reply-To: <20260602031654.3462944-1-mendozayt13@gmail.com>

On 02/06/2026 04:16, Jeremy Erazo wrote:
> 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"

if this is being fixed, then why not remove the comment?

> 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

Please post against latest mainline kernel / maintainer tree

> 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.
> 
> Fixes: e48354ce078c ("iscsi-target: Add iSCSI fabric support for target v4.1")
> Signed-off-by: Jeremy Erazo <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

please use standard kernel coding style for comments

> +		 * 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);

ISCSI_AHSTYPE_RLENGTH means a smaller structure (than ecdb_ahdr), right?

If so, for ISCSI_AHSTYPE_RLENGTH type handling, the return code would be 
ISCSI_REASON_PROTOCOL_ERROR and not ISCSI_REASON_CMD_NOT_SUPPORTED, 
which does not seem proper.

 > +>   		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))

please use {}

> +			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


  reply	other threads:[~2026-06-02 15:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-02  3:16 Jeremy Erazo
2026-06-02 15:57 ` John Garry [this message]
  -- strict thread matches above, loose matches on Subject: below --
2026-05-07 14:25 Jeremy Erazo (Devel Group)

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=9a259155-594d-4242-b0c7-655665215cf6@oracle.com \
    --to=john.g.garry@oracle.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    --cc=mendozayt13@gmail.com \
    --cc=michael.christie@oracle.com \
    --cc=stable@vger.kernel.org \
    --cc=target-devel@vger.kernel.org \
    --cc=vdonnefort@google.com \
    /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®