Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Andrew Stellman" <bugs@stellman-greene.com>
Cc: target-devel@vger.kernel.org, linux-scsi@vger.kernel.org
Subject: Re: [PATCH v3 scsi-fixes 1/3] scsi: target: iscsi: Don't drop live command refs on SNACK Reject
Date: Fri, 04 Dec 2026 06:10:57 +0000	[thread overview]
Message-ID: <sashiko-outbox-249187@kernel.org> (raw)
In-Reply-To: <20261204054748.45560-2-bugs@stellman-greene.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Critical] scsi: target: iscsi: uninitialized ITT/TTT in
  iscsit_add_reject_common()
- [High] scsi: target: iscsi: iscsit_add_reject_keep_conn() can exhaust
  tags for RX
- [Medium] scsi: target: iscsi: uninitialized immediate_cmd on Reject
  inflates MaxCmdSN
- [Medium] scsi: target: iscsi: ignored response-queue error leaks
  Reject cmd and tag
--

commit ff2a609861eafe929213c6e748e480ac3ee02194
Author: Andrew Stellman <astellman@stellman-greene.com>

scsi: target: iscsi: Don't drop live command refs on SNACK Reject

This commit introduces iscsit_add_reject_keep_conn() so out-of-range Data
and R2T SNACK errors send a Reject on a separate non-blocking command
allocation while keeping the connection open, instead of rejecting the
live SCSI command.

> diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c
> index 2b4956f46c132..ffa3432f595c8 100644
> --- a/drivers/target/iscsi/iscsi_target.c
> +++ b/drivers/target/iscsi/iscsi_target.c
> @@ -784,14 +784,13 @@ static void __exit iscsi_target_cleanup_module(void)
>  	kfree(iscsit_global);
>  }
>  
> -int iscsit_add_reject(
> -	struct iscsit_conn *conn,
> -	u8 reason,
> -	unsigned char *buf)
> +static int iscsit_add_reject_common(struct iscsit_conn *conn, u8 reason,
> +				    unsigned char *buf, bool keep_conn)
>  {
>  	struct iscsit_cmd *cmd;
>  
> -	cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
> +	cmd = iscsit_allocate_cmd(conn, keep_conn ? TASK_RUNNING :
> +					TASK_INTERRUPTIBLE);
>  	if (!cmd)
>  		return -1;

[Severity: High]
Can repeated bad SNACKs still deadlock iscsi_target_rx_thread even with
TASK_RUNNING here?

Passing TASK_RUNNING only closes the connection on the allocation after
se_sess->sess_tag_pool is already empty.  Each transmitted Reject stays on
conn->conn_cmd_list in ISTATE_SENT_STATUS holding its tag until
iscsit_ack_from_expstatsn() runs on the RX thread, and iscsit_handle_snack()
never calls it.

If an initiator sends enough out-of-range Data or R2T SNACKs to consume the
last free tag and then sends a normal PDU such as ISCSI_OP_SCSI_CMD,
iscsi_target_rx_opcode() calls iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE)
before the handler can call iscsit_ack_from_expstatsn().

The RX thread then sleeps permanently in iscsit_wait_for_tag() waiting for a
tag that only it can free.  When nopin_timer fires, iscsit_add_nopin() also
fails tag allocation and returns without arming nopin_response_timer or
re-arming nopin_timer, leaving the connection wedged.

[ ... ]

> @@ -812,10 +811,21 @@ int iscsit_add_reject(
>  	cmd->i_state = ISTATE_SEND_REJECT;
>  	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
>  
> -	return -1;
> +	return keep_conn ? 0 : -1;
> +}

[Severity: Critical]
Does leaving cmd->init_task_tag and cmd->targ_xfer_tag zeroed on a live
Reject command allow a use-after-free and conn->conn_cmd_list corruption?

Unlike iscsit_add_nopin(), which sets RESERVED_ITT and 0xFFFFFFFF,
iscsit_add_reject_common() leaves both tags at 0 after iscsit_allocate_cmd().
When keep_conn is true, the RX thread keeps reading PDUs while the Reject
command stays on conn->conn_cmd_list:

RX thread                         TX thread
iscsit_add_reject_keep_conn()
  list_add_tail(cmd, conn_cmd_list)
  queue ISTATE_SEND_REJECT
iscsit_process_nop_out(ttt=0)
  iscsit_find_cmd_from_ttt(0)
  queue ISTATE_REMOVE (immed)
                                  iscsit_handle_immediate_queue()
                                    list_del_init(cmd)
                                    iscsit_free_cmd(cmd, false)
                                      kfree(cmd->buf_ptr)
                                      target_free_tag()
                                  iscsit_handle_response_queue()
                                    iscsit_send_reject(cmd)
                                      xmit cmd->buf_ptr  <-- use-after-free

Because iscsit_free_cmd() is called with shutdown=false, the queued
ISTATE_SEND_REJECT stays on conn->response_queue_list and iscsit_send_reject()
transmits 48 bytes from freed cmd->buf_ptr (and repeated NOP-Outs with ttt=0
can double free it).

Because 0 is also a valid ITT, a Text PDU with itt=0 and ttt!=0xffffffff
matches the Reject command in iscsi_target_rx_opcode() and
iscsit_process_text_cmd() re-adds cmd->i_conn_node to conn->conn_cmd_list,
corrupting the list.

Initializing cmd->init_task_tag = RESERVED_ITT and cmd->targ_xfer_tag =
0xFFFFFFFF before linking cmd onto conn->conn_cmd_list would avoid both.

[Severity: Medium]
Can leaving cmd->immediate_cmd at 0 on a live Reject command inflate
sess->max_cmd_sn beyond the session CmdSN window?

A target-generated Reject never consumed an initiator CmdSN, yet
iscsit_add_reject_common() leaves cmd->immediate_cmd and cmd->maxcmdsn_inc
at 0.  If a Status SNACK matches the Reject's StatSN,
iscsit_handle_status_snack() matches it without checking cmd->iscsi_opcode.

That queues ISTATE_SEND_STATUS_RECOVERY, causing iscsit_build_rsp_pdu() to
send a bogus ISCSI_OP_SCSI_CMD_RSP and call iscsit_increment_maxcmdsn().
iscsit_release_commands_from_conn() also increments sess->max_cmd_sn for
unacknowledged Rejects on connection teardown in a multi-connection session.

Setting cmd->immediate_cmd = 1 here and skipping ISCSI_OP_REJECT in
iscsit_handle_status_snack() would prevent both.

[Severity: Medium]
If iscsit_add_cmd_to_response_queue() returns -ENOMEM when keep_conn is true,
does this leak cmd, cmd->buf_ptr, and the session tag?

iscsit_add_cmd_to_response_queue() uses GFP_ATOMIC, which is already under
pressure when iscsit_handle_recovery_datain() calls this after
iscsit_allocate_datain_req() fails.  Ignoring -ENOMEM leaves cmd stranded on
conn->conn_cmd_list in ISTATE_SEND_REJECT, never reaped until teardown.

Checking the return value and unlinking and freeing cmd (returning -1) on
failure would avoid silently dropping the Reject and leaking the tag.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204054748.45560-1-bugs@stellman-greene.com?part=1

  reply	other threads:[~2026-12-04  6:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  5:47 [PATCH v3 scsi-fixes 0/3] scsi: target: iscsi: SNACK Reject and range fixes Andrew Stellman
2026-12-04  5:47 ` [PATCH v3 scsi-fixes 1/3] scsi: target: iscsi: Don't drop live command refs on SNACK Reject Andrew Stellman
2026-12-04  6:10   ` sashiko-bot [this message]
2026-12-04  5:47 ` [PATCH v3 scsi-fixes 2/3] scsi: target: iscsi: Check Data and R2T SNACK ranges without wrap Andrew Stellman
2026-12-04  6:06   ` sashiko-bot
2026-12-04  5:47 ` [PATCH v3 scsi-fixes 3/3] scsi: target: iscsi: Bound Status SNACKs to unacknowledged StatSNs Andrew Stellman
2026-12-04  6:06   ` sashiko-bot

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=sashiko-outbox-249187@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bugs@stellman-greene.com \
    --cc=linux-scsi@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=target-devel@vger.kernel.org \
    /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