Linux SCSI subsystem development
 help / color / mirror / Atom feed
* [PATCH scsi-fixes 0/3] scsi: target: iscsi: SNACK Reject and range fixes
@ 2026-11-30  6:28 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
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Andrew Stellman @ 2026-11-30  6:28 UTC (permalink / raw)
  To: Martin K. Petersen; +Cc: Mike Christie, target-devel, linux-scsi

From: Andrew Stellman <astellman@stellman-greene.com>

Patch 1 stops the Data and R2T SNACK handlers from rejecting the live
SCSI command and queues the Reject on a separate command without
closing the connection. Patch 2 checks those SNACK ranges without u32
wrap. Patch 3 rejects Status SNACKs for StatSNs the target hasn't sent,
also keeping the connection up. Each patch changes what the initiator
gets in the case it fixes, and each changelog says how.

This series is based on the two patches sent earlier:
https://lore.kernel.org/r/20261129155252.2217-1-bugs@stellman-greene.com
https://lore.kernel.org/r/20261129155252.2217-2-bugs@stellman-greene.com

It was built and tested on mainline bb13fcd7f438 plus both of those
patches, on arm64 QEMU/HVF with generic KASAN, loopback iSCSI, no CHAP
and ERL=1.

Andrew Stellman (3):
  scsi: target: iscsi: Don't drop live command refs on SNACK Reject
  scsi: target: iscsi: Check Data and R2T SNACK ranges without wrap
  scsi: target: iscsi: Reject Status SNACKs beyond sent StatSNs

 drivers/target/iscsi/iscsi_target.c       | 21 +++++++++++-----
 drivers/target/iscsi/iscsi_target.h       |  2 ++
 drivers/target/iscsi/iscsi_target_erl1.c  | 30 ++++++++++++++++-------
 drivers/target/iscsi/iscsi_target_erl1.h  |  5 ++--
 drivers/target/iscsi/iscsi_target_login.c |  1 +
 include/target/iscsi/iscsi_target_core.h  |  1 +
 6 files changed, 43 insertions(+), 17 deletions(-)


base-commit: bb13fcd7f4385fdf64340e5561a26bd951bc3d78
prerequisite-patch-id: cbbf444d664c0e0b3ed7d409e5c4a01980edd2e3
prerequisite-patch-id: 55cbe7d67b368dfb73231720b0adf91b4320a423
-- 
2.53.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH scsi-fixes 1/3] scsi: target: iscsi: Don't drop live command refs on SNACK Reject
  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 ` 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
  2 siblings, 0 replies; 5+ messages in thread
From: Andrew Stellman @ 2026-11-30  6:28 UTC (permalink / raw)
  To: Martin K. Petersen; +Cc: Mike Christie, target-devel, linux-scsi

From: Andrew Stellman <astellman@stellman-greene.com>

An out-of-range Data or R2T SNACK makes iscsit_handle_recovery_datain()
or iscsit_handle_r2t_snack() call iscsit_reject_cmd() on the live SCSI
command. That turns the command into a Reject and drops a reference it
still owns.

Queue the Reject on a separate command instead. iscsit_add_reject()
returns -1 after queueing, which closes the connection, so add
iscsit_add_reject_keep_conn(). It returns 0 after queueing and -1 if
allocation fails. Use it at these call sites. This changes behaviour:
the connection now stays up after these Rejects.

Tested on arm64 QEMU/HVF with generic KASAN, loopback iSCSI, no CHAP and
ERL=1. Before this patch, on mainline a30be8b2de97 plus the two patches
this series is based on, a Data SNACK with BegRun 0 and RunLength 64 hit
the WARN in iscsit_release_cmd() in 10/10 runs, one of which also
logged "refcount_t: underflow; use-after-free." An R2T SNACK
with the same range hit that WARN in 2/10 runs. With the whole series on
mainline bb13fcd7f438 plus those patches, both SNACKs got a reason 0x09
Reject and then a NOP-In for a following NOP-Out in 5/5 runs each, with
no warning, refcount or KASAN message. In-range Data and R2T SNACKs were
still answered with retransmissions.

The issue was found by OpenAI GPT-6 (Codex) while reproducing another
iscsi-target bug.
The fix was drafted with OpenAI GPT-6 (Codex) and this changelog with
Claude Sonnet 5.5, and both were reviewed by me.

Fixes: 3e1c81a95f0d ("iscsi-target: Refactor RX PDU logic + export request PDU handling")
Cc: stable@vger.kernel.org
Signed-off-by: Andrew Stellman <astellman@stellman-greene.com>
---
A reproducer is available on request.

 drivers/target/iscsi/iscsi_target.c      | 19 ++++++++++++++-----
 drivers/target/iscsi/iscsi_target.h      |  2 ++
 drivers/target/iscsi/iscsi_target_erl1.c | 16 +++++++++-------
 3 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c
index de59169775afd..93670172f4ebc 100644
--- a/drivers/target/iscsi/iscsi_target.c
+++ b/drivers/target/iscsi/iscsi_target.c
@@ -784,10 +784,8 @@ 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;
 
@@ -814,10 +812,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;
+}
+
+int iscsit_add_reject(struct iscsit_conn *conn, u8 reason, unsigned char *buf)
+{
+	return iscsit_add_reject_common(conn, reason, buf, false);
 }
 EXPORT_SYMBOL(iscsit_add_reject);
 
+int iscsit_add_reject_keep_conn(struct iscsit_conn *conn, u8 reason,
+				unsigned char *buf)
+{
+	return iscsit_add_reject_common(conn, reason, buf, true);
+}
+
 static int iscsit_add_reject_from_cmd(
 	struct iscsit_cmd *cmd,
 	u8 reason,
diff --git a/drivers/target/iscsi/iscsi_target.h b/drivers/target/iscsi/iscsi_target.h
index f4addae2aae45..6c31fbe58588e 100644
--- a/drivers/target/iscsi/iscsi_target.h
+++ b/drivers/target/iscsi/iscsi_target.h
@@ -30,6 +30,8 @@ extern int iscsit_reset_np_thread(struct iscsi_np *, struct iscsi_tpg_np *,
 				struct iscsi_portal_group *, bool);
 extern int iscsit_del_np(struct iscsi_np *);
 extern int iscsit_reject_cmd(struct iscsit_cmd *cmd, u8, unsigned char *);
+int iscsit_add_reject_keep_conn(struct iscsit_conn *conn, u8 reason,
+				unsigned char *buf);
 extern void iscsit_set_unsolicited_dataout(struct iscsit_cmd *);
 extern int iscsit_logout_closesession(struct iscsit_cmd *, struct iscsit_conn *);
 extern int iscsit_logout_closeconnection(struct iscsit_cmd *, struct iscsit_conn *);
diff --git a/drivers/target/iscsi/iscsi_target_erl1.c b/drivers/target/iscsi/iscsi_target_erl1.c
index 44b851e4a602d..312e0502b9f0e 100644
--- a/drivers/target/iscsi/iscsi_target_erl1.c
+++ b/drivers/target/iscsi/iscsi_target_erl1.c
@@ -129,7 +129,8 @@ static int iscsit_handle_r2t_snack(
 			" protocol error.\n", cmd->init_task_tag, begrun,
 			(begrun + runlength), cmd->acked_data_sn);
 
-		return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
+		return iscsit_add_reject_keep_conn(cmd->conn,
+				ISCSI_REASON_PROTOCOL_ERROR, buf);
 	}
 
 	if (runlength) {
@@ -138,7 +139,7 @@ static int iscsit_handle_r2t_snack(
 			" with BegRun: 0x%08x, RunLength: 0x%08x, exceeds"
 			" current R2TSN: 0x%08x, protocol error.\n",
 			cmd->init_task_tag, begrun, runlength, cmd->r2t_sn);
-			return iscsit_reject_cmd(cmd,
+			return iscsit_add_reject_keep_conn(cmd->conn,
 					ISCSI_REASON_BOOKMARK_INVALID, buf);
 		}
 		last_r2tsn = (begrun + runlength);
@@ -398,7 +399,8 @@ static int iscsit_handle_recovery_datain(
 			" protocol error.\n", cmd->init_task_tag, begrun,
 			(begrun + runlength), cmd->acked_data_sn);
 
-		return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
+		return iscsit_add_reject_keep_conn(conn,
+				ISCSI_REASON_PROTOCOL_ERROR, buf);
 	}
 
 	/*
@@ -409,14 +411,14 @@ static int iscsit_handle_recovery_datain(
 		pr_err("Initiator requesting BegRun: 0x%08x, RunLength"
 			": 0x%08x greater than maximum DataSN: 0x%08x.\n",
 				begrun, runlength, (cmd->data_sn - 1));
-		return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID,
-					 buf);
+		return iscsit_add_reject_keep_conn(conn,
+				ISCSI_REASON_BOOKMARK_INVALID, buf);
 	}
 
 	dr = iscsit_allocate_datain_req();
 	if (!dr)
-		return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_NO_RESOURCES,
-					 buf);
+		return iscsit_add_reject_keep_conn(conn,
+				ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
 
 	dr->data_sn = dr->begrun = begrun;
 	dr->runlength = runlength;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH scsi-fixes 2/3] scsi: target: iscsi: Check Data and R2T SNACK ranges without wrap
  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 ` Andrew Stellman
  2026-11-30  6:28 ` [PATCH scsi-fixes 3/3] scsi: target: iscsi: Reject Status SNACKs beyond sent StatSNs Andrew Stellman
  2 siblings, 0 replies; 5+ messages in thread
From: Andrew Stellman @ 2026-11-30  6:28 UTC (permalink / raw)
  To: Martin K. Petersen; +Cc: Mike Christie, target-devel, linux-scsi

From: Andrew Stellman <astellman@stellman-greene.com>

iscsit_handle_r2t_snack() and iscsit_handle_recovery_datain() compare
BegRun + RunLength with the last R2TSN or DataSN sent using u32
arithmetic. A sum that wraps passes the check even though the request
runs past the last PDU sent.

Do the addition in u64. This changes behaviour for wrapping requests
only: they now get a Bookmark Invalid Reject.

Tested with the whole series on arm64 QEMU/HVF with generic KASAN,
loopback iSCSI, no CHAP and ERL=1: Data and R2T SNACKs with BegRun 2 and
RunLength 0xffffffff each got a reason 0x09 Reject and then a NOP-In for
a following NOP-Out in 5/5 runs, with no warning, refcount or KASAN
message.

The issue was found by Claude Opus 5.5, Claude Sonnet 5.5 and Claude
Haiku 5.5 running Quality Playbook, an LLM-driven code review tool:
https://github.com/andrewstellman/quality-playbook
The fix was drafted with OpenAI GPT-6 (Codex) and this changelog with
Claude Sonnet 5.5, and both were reviewed by me.

Fixes: e48354ce078c ("iscsi-target: Add iSCSI fabric support for target v4.1")
Cc: stable@vger.kernel.org
Signed-off-by: Andrew Stellman <astellman@stellman-greene.com>
---
A reproducer is available on request.

 drivers/target/iscsi/iscsi_target_erl1.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/target/iscsi/iscsi_target_erl1.c b/drivers/target/iscsi/iscsi_target_erl1.c
index 312e0502b9f0e..3562e9ad5e4c8 100644
--- a/drivers/target/iscsi/iscsi_target_erl1.c
+++ b/drivers/target/iscsi/iscsi_target_erl1.c
@@ -134,7 +134,7 @@ static int iscsit_handle_r2t_snack(
 	}
 
 	if (runlength) {
-		if ((begrun + runlength) > cmd->r2t_sn) {
+		if ((u64)begrun + runlength > cmd->r2t_sn) {
 			pr_err("Command ITT: 0x%08x received R2T SNACK"
 			" with BegRun: 0x%08x, RunLength: 0x%08x, exceeds"
 			" current R2TSN: 0x%08x, protocol error.\n",
@@ -407,7 +407,7 @@ static int iscsit_handle_recovery_datain(
 	 * Make sure BegRun and RunLength in the Data SNACK are sane.
 	 * Note: (cmd->data_sn - 1) will carry the maximum DataSN sent.
 	 */
-	if ((begrun + runlength) > (cmd->data_sn - 1)) {
+	if ((u64)begrun + runlength > (cmd->data_sn - 1)) {
 		pr_err("Initiator requesting BegRun: 0x%08x, RunLength"
 			": 0x%08x greater than maximum DataSN: 0x%08x.\n",
 				begrun, runlength, (cmd->data_sn - 1));
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH scsi-fixes 3/3] scsi: target: iscsi: Reject Status SNACKs beyond sent StatSNs
  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 ` Andrew Stellman
  2026-11-30  6:42   ` sashiko-bot
  2 siblings, 1 reply; 5+ messages in thread
From: Andrew Stellman @ 2026-11-30  6:28 UTC (permalink / raw)
  To: Martin K. Petersen; +Cc: Mike Christie, target-devel, linux-scsi

From: Andrew Stellman <astellman@stellman-greene.com>

iscsit_handle_status_snack() doesn't bound the requested range by the
StatSNs sent on the connection. For each StatSN in the range that it
can't find, it logs "Unable to find StatSN" and moves on. With this
patch reverted from the series, a Status SNACK with BegRun 0 and
RunLength 64 logged 64 of these messages and got no Reject in 6/6 runs.

Save the StatSN at login and reject a range beyond the StatSNs sent
since then with a Protocol Error, using iscsit_add_reject_keep_conn() so
the connection stays up. BegRun 0 with RunLength 0 stays a no-op when
there are no unacknowledged responses. This changes behaviour: an
out-of-range Status SNACK used to be logged and ignored, and now gets a
Reject.

Tested with the whole series on arm64 QEMU/HVF with generic KASAN,
loopback iSCSI, no CHAP and ERL=1, with a 3-second peer timeout. Status
SNACKs with BegRun 0 and RunLength 64, and with BegRun 0 and RunLength
1, each got a reason 0x04 Reject and then a NOP-In for a following
NOP-Out in 10/10 runs, with no "Unable to find StatSN" messages. A
Status SNACK for a sent StatSN still got the SCSI response
retransmitted. With all responses acknowledged and a WRITE pending,
BegRun 0 with RunLength 0 got no reply within 3 seconds, and a following
NOP-Out got a NOP-In.

The issue was found by Claude Opus 5.5, Claude Sonnet 5.5 and Claude
Haiku 5.5 running Quality Playbook, an LLM-driven code review tool:
https://github.com/andrewstellman/quality-playbook
The fix was drafted with OpenAI GPT-6 (Codex) and this changelog with
Claude Sonnet 5.5, and both were reviewed by me.

Fixes: e48354ce078c ("iscsi-target: Add iSCSI fabric support for target v4.1")
Cc: stable@vger.kernel.org
Signed-off-by: Andrew Stellman <astellman@stellman-greene.com>
---
A reproducer is available on request.

 drivers/target/iscsi/iscsi_target.c       |  2 +-
 drivers/target/iscsi/iscsi_target_erl1.c  | 10 ++++++++++
 drivers/target/iscsi/iscsi_target_erl1.h  |  5 +++--
 drivers/target/iscsi/iscsi_target_login.c |  1 +
 include/target/iscsi/iscsi_target_core.h  |  1 +
 5 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c
index 93670172f4ebc..d32ecb9a8445b 100644
--- a/drivers/target/iscsi/iscsi_target.c
+++ b/drivers/target/iscsi/iscsi_target.c
@@ -2555,7 +2555,7 @@ int iscsit_handle_snack(
 			be32_to_cpu(hdr->begrun),
 			be32_to_cpu(hdr->runlength));
 	case ISCSI_FLAG_SNACK_TYPE_STATUS:
-		return iscsit_handle_status_snack(conn, hdr->itt,
+		return iscsit_handle_status_snack(conn, buf, hdr->itt,
 			be32_to_cpu(hdr->ttt),
 			be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
 	case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
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);
 
 	last_statsn = (!runlength) ? conn->stat_sn : (begrun + runlength);
 
diff --git a/drivers/target/iscsi/iscsi_target_erl1.h b/drivers/target/iscsi/iscsi_target_erl1.h
index 12472eefe5598..81e7f7ad6f93f 100644
--- a/drivers/target/iscsi/iscsi_target_erl1.h
+++ b/drivers/target/iscsi/iscsi_target_erl1.h
@@ -19,8 +19,9 @@ extern int iscsit_create_recovery_datain_values_datasequenceinorder_no(
 			struct iscsit_cmd *, struct iscsi_datain_req *);
 extern int iscsit_handle_recovery_datain_or_r2t(struct iscsit_conn *, unsigned char *,
 			itt_t, u32, u32, u32);
-extern int iscsit_handle_status_snack(struct iscsit_conn *, itt_t, u32,
-			u32, u32);
+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);
 extern int iscsit_handle_data_ack(struct iscsit_conn *, u32, u32, u32);
 extern int iscsit_dataout_datapduinorder_no_fbit(struct iscsit_cmd *, struct iscsi_pdu *);
 extern int iscsit_recover_dataout_sequence(struct iscsit_cmd *, u32, u32);
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;
 
 	mutex_lock(&auth_id_lock);
 	conn->auth_id		= iscsit_global->auth_id++;
diff --git a/include/target/iscsi/iscsi_target_core.h b/include/target/iscsi/iscsi_target_core.h
index 51ca80abacf7b..db57d88e9e81c 100644
--- a/include/target/iscsi/iscsi_target_core.h
+++ b/include/target/iscsi/iscsi_target_core.h
@@ -531,6 +531,7 @@ struct iscsit_conn {
 	u32			exp_statsn;
 	/* Per connection status sequence number */
 	u32			stat_sn;
+	u32			initial_stat_sn;
 	struct sockaddr_storage login_sockaddr;
 	struct sockaddr_storage local_sockaddr;
 	int			conn_usage_count;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH scsi-fixes 3/3] scsi: target: iscsi: Reject Status SNACKs beyond sent StatSNs
  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
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-30  6:42 UTC (permalink / raw)
  To: Andrew Stellman; +Cc: target-devel, linux-scsi

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-30  6:42 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox