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 scsi-fixes 3/3] scsi: target: iscsi: Reject Status SNACKs beyond sent StatSNs
Date: Mon, 30 Nov 2026 06:42:41 +0000	[thread overview]
Message-ID: <sashiko-outbox-240519@kernel.org> (raw)
In-Reply-To: <20261130062812.56660-4-bugs@stellman-greene.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] scsi: target: iscsi: wrong initial_stat_sn and exp_statsn in
  Status SNACK
- [Medium] scsi: target: iscsi: Status SNACK bounded by initial_stat_sn
  not exp_statsn
- [Medium] scsi: target: iscsi: non-wrapping StatSN comparisons in
  Status SNACK handler
- [Medium] scsi: target: iscsi: unlocked multi-read of conn->stat_sn in
  Status SNACK
- [Low] scsi: target: iscsi: exp_statsn > begrun skips Reject in Status
  SNACK handler
--

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

scsi: target: iscsi: Reject Status SNACKs beyond sent StatSNs

This commit saves the connection's initial StatSN at login and rejects
Status SNACK requests whose range falls outside the StatSNs sent on the
connection with a Protocol Error Reject PDU.

> diff --git a/drivers/target/iscsi/iscsi_target_erl1.c b/drivers/target/iscsi/iscsi_target_erl1.c
> index 3562e9ad5e4c8..542467dd5960d 100644
> --- a/drivers/target/iscsi/iscsi_target_erl1.c
> +++ b/drivers/target/iscsi/iscsi_target_erl1.c
> @@ -468,6 +468,7 @@ int iscsit_handle_recovery_datain_or_r2t(
>  /* #warning FIXME: Status SNACK needs to be dependent on OPCODE!!! */
>  int iscsit_handle_status_snack(
>  	struct iscsit_conn *conn,
> +	unsigned char *buf,
>  	itt_t init_task_tag,
>  	u32 targ_xfer_tag,
>  	u32 begrun,
> @@ -475,8 +476,12 @@ int iscsit_handle_status_snack(
>  {
>  	struct iscsit_cmd *cmd = NULL;
>  	u32 last_statsn;
> +	u32 sent, offset;
>  	int found_cmd;
>  
> +	if (!begrun && !runlength && conn->exp_statsn == conn->stat_sn)
> +		return 0;
> +
>  	if (!begrun) {
>  		begrun = conn->exp_statsn;
>  	} else if (conn->exp_statsn > begrun) {
> @@ -486,6 +491,11 @@ int iscsit_handle_status_snack(
>  			conn->cid);
>  		return 0;
>  	}
> +	sent = conn->stat_sn - conn->initial_stat_sn;
> +	offset = begrun - conn->initial_stat_sn;
> +	if (offset >= sent || (runlength && runlength > sent - offset))
> +		return iscsit_add_reject_keep_conn(conn,
> +				ISCSI_REASON_PROTOCOL_ERROR, buf);

[Severity: Low]
Does returning 0 on conn->exp_statsn > begrun before checking sent and
offset skip the Reject for out-of-range requests?

When 0 < conn->initial_stat_sn <= conn->exp_statsn, a non-zero begrun
below conn->initial_stat_sn (a StatSN never sent on the connection), or a
begrun in [conn->initial_stat_sn, conn->exp_statsn) whose runlength
extends past conn->stat_sn, takes the early return 0 instead of sending a
Protocol Error Reject.

[Severity: Medium]
Should the requested range be bounded by conn->exp_statsn rather than
conn->initial_stat_sn?

iscsit_ack_from_expstatsn() removes and frees acknowledged commands from
conn->conn_cmd_list and assigns conn->exp_statsn = exp_statsn without
checking that exp_statsn moves forward:

drivers/target/iscsi/iscsi_target.c:iscsit_ack_from_expstatsn() {
	...
	conn->exp_statsn = exp_statsn;

	spin_lock_bh(&conn->cmd_lock);
	list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
		spin_lock(&cmd->istate_lock);
		if ((cmd->i_state == ISTATE_SENT_STATUS) &&
		    iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
			cmd->i_state = ISTATE_REMOVE;
			spin_unlock(&cmd->istate_lock);
			iscsit_free_cmd(cmd, false);
	...
}

After N responses have been acknowledged and freed, an initiator can send
a PDU with hdr->exp_statsn set back to conn->initial_stat_sn followed by a
Status SNACK for those N historical StatSNs.  That passes both
conn->exp_statsn > begrun and the conn->initial_stat_sn bounds check,
causing the loop below to walk conn->conn_cmd_list under conn->cmd_lock
and log "Unable to find StatSN" for every freed StatSN sent since login.

>  
>  	last_statsn = (!runlength) ? conn->stat_sn : (begrun + runlength);

[Severity: Medium]
Can reading conn->stat_sn multiple times without a single snapshot race
with the TX thread when runlength is 0?

The TX thread increments conn->stat_sn when building a response PDU before
transmitting it and before setting cmd->i_state to ISTATE_SENT_STATUS:

RX thread (iscsit_handle_status_snack)  TX thread (iscsit_response_queue)
--------------------------------------  ---------------------------------
sent = conn->stat_sn -
       conn->initial_stat_sn;
if (offset >= sent || ...)
                                        iscsit_build_rsp_pdu()
                                          cmd->stat_sn = conn->stat_sn++;
last_statsn = conn->stat_sn;
while (begrun < last_statsn)
  /* matches in-flight cmd */
  cmd->i_state =
    ISTATE_SEND_STATUS_RECOVERY;
  iscsit_add_cmd_to_response_queue()    <-- queued twice while in flight

If conn->stat_sn advances between the bounds check and the last_statsn
assignment, last_statsn includes the in-flight command whose i_state is
still ISTATE_SEND_STATUS, and the RX thread queues it onto
conn->response_queue_list a second time.

[Severity: Medium]
How does this handle 32-bit StatSN wrap-around across 0xffffffff?

sent and offset use wrapping u32 subtraction, so a range crossing
0xffffffff -> 0x00000000 passes the new bounds check.  However,
conn->exp_statsn > begrun and while (begrun < last_statsn) use raw
unsigned comparisons rather than serial number arithmetic.  When begrun is
0xffffffff and runlength is 1 (or 0 with conn->stat_sn wrapped past 0),
last_statsn wraps to 0x00000000 (or conn->stat_sn), so begrun < last_statsn
is immediately false and the loop exits without retransmitting the
unacknowledged response.  Likewise, when conn->exp_statsn is pre-wrap and
begrun is post-wrap, conn->exp_statsn > begrun is true and drops a valid
SNACK.

> diff --git a/drivers/target/iscsi/iscsi_target_login.c b/drivers/target/iscsi/iscsi_target_login.c
> index c282b6a702968..83e721075f8df 100644
> --- a/drivers/target/iscsi/iscsi_target_login.c
> +++ b/drivers/target/iscsi/iscsi_target_login.c
> @@ -177,6 +177,7 @@ static int iscsi_login_set_conn_values(
>  	ret = get_random_bytes_wait(&conn->stat_sn, sizeof(u32));
>  	if (unlikely(ret))
>  		return ret;
> +	conn->initial_stat_sn = conn->stat_sn;

[Severity: Medium]
Does saving conn->initial_stat_sn before Login Response PDUs are sent,
without initializing conn->exp_statsn, cause false Rejects and allow
SNACKs for login StatSNs?

iscsi_login_set_conn_values() runs on the first Login Request PDU, after
which every Login Response PDU increments conn->stat_sn without adding an
entry to conn->conn_cmd_list:

drivers/target/iscsi/iscsi_target_nego.c:iscsi_target_do_tx_login_io() {
	...
	login_rsp->statsn		= cpu_to_be32(conn->stat_sn++);
	...
}

When Full Feature Phase begins, conn->stat_sn has advanced past
conn->initial_stat_sn by the number of Login Response PDUs sent, while
conn->exp_statsn is still 0 until a command calls
iscsit_ack_from_expstatsn().  As a result, a Status SNACK with BegRun 0
and RunLength 0 sent right after login sees conn->exp_statsn !=
conn->stat_sn, sets begrun to 0, and is rejected with
ISCSI_REASON_PROTOCOL_ERROR instead of being treated as a no-op.
Conversely, a Status SNACK requesting the Login Response StatSNs in
[conn->initial_stat_sn, conn->stat_sn) passes the bounds check and logs
"Unable to find StatSN".

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

      reply	other threads:[~2026-11-30  6:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  6:28 [PATCH scsi-fixes 0/3] scsi: target: iscsi: SNACK Reject and range fixes Andrew Stellman
2026-11-30  6:28 ` [PATCH scsi-fixes 1/3] scsi: target: iscsi: Don't drop live command refs on SNACK Reject Andrew Stellman
2026-11-30  6:28 ` [PATCH scsi-fixes 2/3] scsi: target: iscsi: Check Data and R2T SNACK ranges without wrap Andrew Stellman
2026-11-30  6:28 ` [PATCH scsi-fixes 3/3] scsi: target: iscsi: Reject Status SNACKs beyond sent StatSNs Andrew Stellman
2026-11-30  6:42   ` sashiko-bot [this message]

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-240519@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