All of lore.kernel.org
 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 v4 scsi-fixes 3/3] scsi: target: iscsi: Bound Status SNACKs to unacknowledged StatSNs
Date: Fri, 04 Dec 2026 19:56:40 +0000	[thread overview]
Message-ID: <sashiko-outbox-250578@kernel.org> (raw)
In-Reply-To: <20261204184320.71059-4-bugs@stellman-greene.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] scsi: target: iscsi: Status SNACK count unbounded via reused
  Text cmds
- [Medium] scsi: target: iscsi: premature loop break in
  iscsit_handle_status_snack()
- [Medium] scsi: target: iscsi: iscsit_handle_status_snack() skips
  in-flight status
- [Medium] scsi: target: iscsi: iscsit_handle_status_snack() rejects
  BegRun == stat_sn
- [Low] scsi: target: iscsi: missing WRITE_ONCE() on TX conn->stat_sn
  increments
--

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

scsi: target: iscsi: Bound Status SNACKs to unacknowledged StatSNs

This commit initializes conn->exp_statsn at login completion, validates
ExpStatSN updates against the sent StatSN window, bounds Status SNACK
ranges to unacknowledged StatSNs, and holds conn->cmd_lock while
requeueing sent statuses for recovery.

> diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c
> index 2b4956f46c132..5e46c6e2dc2dd 100644
> --- a/drivers/target/iscsi/iscsi_target.c
> +++ b/drivers/target/iscsi/iscsi_target.c
> @@ -951,6 +951,9 @@ static void iscsit_ack_from_expstatsn(struct iscsit_conn *conn, u32 exp_statsn)
>  	LIST_HEAD(ack_list);
>  	struct iscsit_cmd *cmd, *cmd_p;
>  
> +	if (iscsi_sna_lt(exp_statsn, conn->exp_statsn) ||
> +	    iscsi_sna_gt(exp_statsn, READ_ONCE(conn->stat_sn)))
> +		return;
>  	conn->exp_statsn = exp_statsn;

[Severity: Low]
Should the TX-side increments of conn->stat_sn also use WRITE_ONCE()?

This patch adds lockless READ_ONCE(conn->stat_sn) on the RX thread here
and in iscsit_handle_status_snack(), while the concurrent TX-thread
increments in iscsit_build_rsp_pdu() (and the other response builders in
iscsi_target.c and cxgbit_tx_datain_iso()) still do plain conn->stat_sn++.
That leaves a KCSAN data race on the RX/TX hot path.

[ ... ]

> diff --git a/drivers/target/iscsi/iscsi_target_erl1.c b/drivers/target/iscsi/iscsi_target_erl1.c
> index 5d59f409b2256..b4502bdafa0df 100644
> --- a/drivers/target/iscsi/iscsi_target_erl1.c
> +++ b/drivers/target/iscsi/iscsi_target_erl1.c
> @@ -468,28 +468,32 @@ 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,
>  	u32 runlength)
>  {
>  	struct iscsit_cmd *cmd = NULL;
> -	u32 last_statsn;
> -	int found_cmd;
> -
> -	if (!begrun) {
> -		begrun = conn->exp_statsn;
> -	} else if (conn->exp_statsn > begrun) {
> -		pr_err("Got Status SNACK Begrun: 0x%08x, RunLength:"
> -			" 0x%08x but already got ExpStatSN: 0x%08x on CID:"
> -			" %hu.\n", begrun, runlength, conn->exp_statsn,
> -			conn->cid);
> -		return 0;
> +	u32 next_statsn = READ_ONCE(conn->stat_sn);
> +	u32 exp_statsn = READ_ONCE(conn->exp_statsn);
> +	u32 sent, offset, count;
> +	int found_cmd, ret;
> +
> +	if (!begrun && !runlength) {
> +		if (exp_statsn == next_statsn)
> +			return 0;
> +		begrun = exp_statsn;
>  	}
> +	sent = next_statsn - exp_statsn;
> +	offset = begrun - exp_statsn;
> +	if (offset >= sent || (runlength && runlength > sent - offset))
> +		return iscsit_add_reject(conn,
> +				ISCSI_REASON_PROTOCOL_ERROR, buf);

[Severity: Medium]
Can offset >= sent reject a valid Status SNACK when begrun equals
next_statsn and runlength is 0?

Because conn->stat_sn is randomly seeded, an initiator sending an
explicit BegRun = ExpStatSN (equal to conn->stat_sn) with RunLength = 0
when no unacknowledged statuses exist skips the !begrun && !runlength
branch and hits offset == sent.  iscsit_add_reject() then returns -1 and
iscsit_get_rx_pdu() tears down the connection instead of returning 0 with
nothing to resend.

Changing offset >= sent to offset > sent allows offset == sent with
runlength == 0 to produce count == 0 (while still rejecting nonzero
runlength) and removes the need for the exp_statsn == next_statsn check.

> -	last_statsn = (!runlength) ? conn->stat_sn : (begrun + runlength);
> +	count = runlength ? runlength : sent - offset;
>  
> -	while (begrun < last_statsn) {
> +	while (count--) {

[Severity: High]
Can a remote initiator still make count arbitrarily large by reusing a
Text command?

In iscsi_target_rx_opcode(), an immediate empty ISCSI_OP_TEXT PDU with
ttt != 0xFFFFFFFF reuses an existing iscsit_cmd on conn->conn_cmd_list
without consuming a session tag, and iscsit_build_text_rsp() increments
conn->stat_sn++ on each response while overwriting cmd->stat_sn.  With
ExpStatSN held fixed, next_statsn - exp_statsn can grow well past the
command depth.

A single 48-byte Status SNACK with BegRun = 0 and RunLength = 0 then runs
count iterations scanning conn->conn_cmd_list under
spin_lock_bh(&conn->cmd_lock) with no cond_resched() and prints an
unratelimited pr_err() for every missing StatSN, repeatable on each
subsequent SNACK to starve CPU and flood the kernel log.  Scanning
conn->conn_cmd_list once for StatSNs in [begrun, begrun + count) (or
capping count and ratelimiting pr_err()) would avoid this.

Separately, the new iscsi_sna_gt() check in iscsit_ack_from_expstatsn()
also wraps and drops valid ExpStatSN updates if conn->stat_sn advances
more than 2^31 + 1 ahead of conn->exp_statsn.

>  		found_cmd = 0;
>  
>  		spin_lock_bh(&conn->cmd_lock);
> @@ -499,9 +503,9 @@ int iscsit_handle_status_snack(
>  				break;
>  			}
>  		}
> -		spin_unlock_bh(&conn->cmd_lock);
>  
>  		if (!found_cmd) {
> +			spin_unlock_bh(&conn->cmd_lock);
>  			pr_err("Unable to find StatSN: 0x%08x for"
>  				" a Status SNACK, assuming this was a"
>  				" protactic SNACK for an untransmitted"
> @@ -510,20 +514,22 @@ int iscsit_handle_status_snack(
>  			continue;
>  		}
>  
> -		spin_lock_bh(&cmd->istate_lock);
> -		if (cmd->i_state == ISTATE_SEND_DATAIN) {
> -			spin_unlock_bh(&cmd->istate_lock);
> -			pr_err("Ignoring Status SNACK for BegRun:"
> -				" 0x%08x, RunLength: 0x%08x, assuming this was"
> -				" a protactic SNACK for an untransmitted"
> -				" StatSN\n", begrun, runlength);
> +		spin_lock(&cmd->istate_lock);
> +		if (cmd->i_state != ISTATE_SENT_STATUS) {
> +			spin_unlock(&cmd->istate_lock);
> +			spin_unlock_bh(&conn->cmd_lock);
>  			begrun++;
>  			continue;
>  		}

[Severity: Medium]
Can breaking out of the conn->conn_cmd_list scan before checking
cmd->i_state skip a valid StatSN?

An earlier in-flight command allocated by iscsit_allocate_cmd() has
cmd->stat_sn == 0 until status transmission, and an unsolicited NOP-In
keepalive in ISTATE_SENT_NOPIN_WANT_RESPONSE copies conn->stat_sn in
iscsit_build_nopin_rsp() without incrementing it.  In either case the
loop breaks on the non-ISTATE_SENT_STATUS entry, skips begrun, and
silently fails to retransmit the later command's status, stalling
initiator error recovery.

Reading cmd->stat_sn before taking cmd->istate_lock also races with
concurrent TX-thread assignment of cmd->stat_sn.  Acquiring
cmd->istate_lock inside the list_for_each_entry() loop and matching
cmd->i_state == ISTATE_SENT_STATUS && cmd->stat_sn == begrun (as
iscsit_ack_from_expstatsn() does) avoids both problems.

[Severity: Medium]
Can checking cmd->i_state != ISTATE_SENT_STATUS silently drop a Status
SNACK for an already-transmitted status PDU?

The TX thread advances conn->stat_sn and transmits the status PDU before
updating cmd->i_state to ISTATE_SENT_STATUS:

TX thread                         RX thread
iscsit_response_queue()
  iscsit_send_response()
    iscsit_build_rsp_pdu()
      cmd->stat_sn = conn->stat_sn++
    iscsit_xmit_pdu()
                                  iscsit_handle_status_snack()
                                    next_statsn = READ_ONCE(conn->stat_sn)
                                    ...
                                    cmd->i_state != ISTATE_SENT_STATUS
                                      <-- skips status, returns 0
  spin_lock_bh(&cmd->istate_lock)
  cmd->i_state = ISTATE_SENT_STATUS
  spin_unlock_bh(&cmd->istate_lock)

If the initiator sees a digest error on that PDU and its Status SNACK
arrives in this window, iscsit_handle_status_snack() accepts the range
but skips cmd while it is still in ISTATE_SEND_STATUS.  The SNACK is then
lost and recovery stalls until a command timeout or connection recovery.

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

      reply	other threads:[~2026-12-04 19:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 18:43 [PATCH v4 scsi-fixes 0/3] scsi: target: iscsi: SNACK Reject and range fixes Andrew Stellman
2026-12-04 18:43 ` [PATCH v4 scsi-fixes 1/3] scsi: target: iscsi: Don't drop live command refs on SNACK Reject Andrew Stellman
2026-12-04 19:12   ` sashiko-bot
2026-12-04 18:43 ` [PATCH v4 scsi-fixes 2/3] scsi: target: iscsi: Check Data and R2T SNACK ranges without wrap Andrew Stellman
2026-12-04 18:43 ` [PATCH v4 scsi-fixes 3/3] scsi: target: iscsi: Bound Status SNACKs to unacknowledged StatSNs Andrew Stellman
2026-12-04 19:56   ` 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-250578@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.