* [PATCH v2 scsi-fixes 1/3] scsi: target: iscsi: Don't drop live command refs on SNACK Reject
2026-12-02 16:57 [PATCH v2 scsi-fixes 0/3] scsi: target: iscsi: SNACK Reject and range fixes Andrew Stellman
@ 2026-12-02 16:57 ` Andrew Stellman
2026-12-02 17:21 ` sashiko-bot
2026-12-02 16:57 ` [PATCH v2 scsi-fixes 2/3] scsi: target: iscsi: Check Data and R2T SNACK ranges without wrap Andrew Stellman
2026-12-02 16:57 ` [PATCH v2 scsi-fixes 3/3] scsi: target: iscsi: Bound Status SNACKs to unacknowledged StatSNs Andrew Stellman
2 siblings, 1 reply; 6+ messages in thread
From: Andrew Stellman @ 2026-12-02 16:57 UTC (permalink / raw)
To: Martin K. Petersen
Cc: Mike Christie, Dmitry Bogdanov, Ziheng Wei, target-devel,
linux-scsi
From: Andrew Stellman <astellman@stellman-greene.com>
iscsit_handle_recovery_datain() and iscsit_handle_r2t_snack() call
iscsit_reject_cmd() on the live SCSI command that an out-of-range Data
or R2T SNACK names. That drops the command's reference while the
command is still in use, and returns -1, which closes the connection.
Freeing the command then underflows its refcount:
[ 23.364112] ------------[ cut here ]------------
[ 23.364117] refcount_t: underflow; use-after-free.
[ 23.364126] WARNING: lib/refcount.c:37 at refcount_warn_saturate+0x110/0x1a0, CPU#0: iscsi_trx/501
[ 23.365350] Modules linked in:
[ 23.365548] CPU: 0 UID: 0 PID: 501 Comm: iscsi_trx Tainted: G W 7.4.0-rc4 #1 PREEMPT(full)
[ 23.366156] Tainted: [W]=WARN
[ 23.366346] Hardware name: linux,dummy-virt (DT)
[ 23.366634] pstate: 61400005 (nZCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
[ 23.367060] pc : refcount_warn_saturate+0x110/0x1a0
[ 23.367364] lr : refcount_warn_saturate+0x110/0x1a0
[ 23.367679] sp : ffff800082c17ae0
[ 23.367892] x29: ffff800082c17ae0 x28: ffff800082c17b00 x27: ffff0000ee3db000
[ 23.368347] x26: ffff0000ed32a000 x25: ffff800082c17ce0 x24: ffff0000ee15ec00
[ 23.368796] x23: dfff800000000000 x22: 0000000000000000 x21: ffff0000ee3db304
[ 23.369262] x20: 0000000000000003 x19: ffff0000ee3db304 x18: 1ffff00010582eae
[ 23.369710] x17: ffffbd0874ecf564 x16: 00000000f1f1f1f1 x15: 00000000f3f3f3f3
[ 23.370212] x14: ffff700010582f3c x13: 0a2e656572662d72 x12: ffff77a10f045412
[ 23.370684] x11: 1ffff7a10f045411 x10: ffff77a10f045411 x9 : dfff800000000000
[ 23.371145] x8 : ffffbd087822a08b x7 : 0000000000000001 x6 : ffff77a10f045411
[ 23.371603] x5 : ffffbd087822a088 x4 : ffff77a10f045412 x3 : 1fffe0001dbd0aa1
[ 23.372068] x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff0000ede85500
[ 23.372521] Call trace:
[ 23.372685] refcount_warn_saturate+0x110/0x1a0 (P)
[ 23.373003] transport_generic_free_cmd+0x2b0/0x2e0
[ 23.373316] iscsit_free_cmd+0x80/0x14c
[ 23.373563] iscsit_close_connection+0xd48/0x1848
[ 23.373877] iscsit_take_action_for_connection_exit+0x17c/0x314
[ 23.374310] iscsi_target_rx_thread+0x1e8/0x260
[ 23.374607] kthread+0x36c/0x420
[ 23.374819] ret_from_fork+0x10/0x20
[ 23.375047] ---[ end trace 0000000000000000 ]---
Queue the Reject on a separate command with a new helper,
iscsit_add_reject_keep_conn(). This changes existing behavior: the
connection now stays open after these Rejects.
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>
---
v2: no code change.
v1: https://lore.kernel.org/r/20261130062812.56660-2-bugs@stellman-greene.com
Tested on 7.4-rc5 with this series (arm64 QEMU/HVF, KASAN, ERL=1, no
CHAP): out-of-range Data and R2T SNACKs got a Reject (reason 0x09) and a
following NOP-Out was answered; in-range ones were retransmitted.
Found by OpenAI GPT-6 (Codex) while reproducing another iscsi-target
bug; the fix was drafted with OpenAI GPT-6 (Codex).
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 a8d44ac00abb6..91c95ee314d1f 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] 6+ messages in thread* Re: [PATCH v2 scsi-fixes 1/3] scsi: target: iscsi: Don't drop live command refs on SNACK Reject
2026-12-02 16:57 ` [PATCH v2 scsi-fixes 1/3] scsi: target: iscsi: Don't drop live command refs on SNACK Reject Andrew Stellman
@ 2026-12-02 17:21 ` sashiko-bot
0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-12-02 17:21 UTC (permalink / raw)
To: Andrew Stellman; +Cc: target-devel, linux-scsi
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Critical] scsi: target: iscsi: uninitialized ITT/TTT in
iscsit_add_reject_common()
- [High] scsi: target: iscsi: tag pool exhaustion deadlock on repeated
SNACK rejects
--
commit ac7c5d5e7e222ab9ec1aecbb97fae0030d00adaa
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() and uses it in
iscsit_handle_r2t_snack() and iscsit_handle_recovery_datain() so that
rejected out-of-range Data and R2T SNACKs allocate a separate Reject
command and keep the connection open instead of dropping the live SCSI
command's reference.
> diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c
> index 2b4956f46c132..fbfd8a61ced51 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;
>
> @@ -812,10 +810,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 linking cmd onto conn->conn_cmd_list while keeping the connection
open leave cmd->init_task_tag and cmd->targ_xfer_tag at 0?
iscsit_allocate_cmd() zero-initializes cmd, and unlike target-originated
commands in iscsit_add_nopin() or iscsit_send_conn_drop_async_message(),
iscsit_add_reject_common() does not set cmd->init_task_tag = RESERVED_ITT,
cmd->targ_xfer_tag = 0xFFFFFFFF, or cmd->immediate_cmd = 1. When
keep_conn is true, cmd remains on conn->conn_cmd_list until ExpStatSN
acknowledges it, so subsequent PDUs on the live connection can look it up
by TTT 0 or ITT 0.
For example, a NOP-Out with ITT = RESERVED_ITT and TTT = 0 matches the
Reject cmd in iscsit_find_cmd_from_ttt() and frees it while it is still
queued on conn->response_queue_list:
RX thread (iscsi_target_rx_thread) TX thread (iscsi_target_tx_thread)
---------------------------------- ----------------------------------
iscsit_handle_snack()
...
iscsit_add_reject_keep_conn()
list_add_tail(&cmd->i_conn_node,
&conn->conn_cmd_list)
iscsit_add_cmd_to_response_queue(
cmd, conn, ISTATE_SEND_REJECT)
iscsit_process_nop_out() [TTT = 0]
iscsit_find_cmd_from_ttt(conn, 0)
cmd->i_state = ISTATE_REMOVE
iscsit_add_cmd_to_immediate_queue()
iscsit_handle_immediate_queue()
iscsit_immediate_queue()
/* ISTATE_REMOVE */
list_del_init(&cmd->i_conn_node)
iscsit_free_cmd(cmd, false)
iscsit_handle_response_queue()
iscsit_response_queue()
/* ISTATE_SEND_REJECT */
iscsit_send_reject(cmd, conn)
<-- use-after-free of cmd
and cmd->buf_ptr
A subsequent Text PDU with TTT != 0xFFFFFFFF and ITT = 0 also matches the
Reject cmd via iscsit_find_cmd_from_itt() in iscsi_target_rx_opcode() and
passes it to iscsit_process_text_cmd(), which re-adds cmd->i_conn_node to
conn->conn_cmd_list and corrupts the list. In addition, because
cmd->immediate_cmd is 0, closing the connection before the Reject is
acknowledged causes iscsit_increment_maxcmdsn() to increment
sess->max_cmd_sn for a Reject PDU that consumed no CmdSN.
> 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);
> }
[Severity: High]
Can repeated SNACK rejects exhaust se_sess->sess_tag_pool and deadlock
iscsi_target_rx_thread?
iscsit_add_reject_keep_conn() allocates a command tag via
iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE). Once the TX thread sends
the Reject, the command moves to ISTATE_SENT_STATUS and holds its tag on
conn->conn_cmd_list until iscsit_ack_from_expstatsn() frees it on the RX
thread.
However, SNACK PDUs are not gated by the CmdSN window and
iscsit_handle_snack() does not call iscsit_ack_from_expstatsn(). Every
opcode that does call iscsit_ack_from_expstatsn() (such as
ISCSI_OP_SCSI_CMD or ISCSI_OP_NOOP_OUT) first calls
iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE) in iscsi_target_rx_opcode():
drivers/target/iscsi/iscsi_target.c:iscsi_target_rx_opcode() {
...
switch (hdr->opcode & ISCSI_OPCODE_MASK) {
case ISCSI_OP_SCSI_CMD:
cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
if (!cmd)
goto reject;
ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
...
}
If an initiator sends enough rejected SNACKs to fill
se_sess->sess_tag_pool, the next rejected SNACK or incoming command blocks
iscsi_target_rx_thread indefinitely in iscsit_wait_for_tag(), waiting for
a tag that can only be released by iscsit_ack_from_expstatsn() running on
that same RX thread.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202165734.51018-1-bugs@stellman-greene.com?part=1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v2 scsi-fixes 2/3] scsi: target: iscsi: Check Data and R2T SNACK ranges without wrap
2026-12-02 16:57 [PATCH v2 scsi-fixes 0/3] scsi: target: iscsi: SNACK Reject and range fixes Andrew Stellman
2026-12-02 16:57 ` [PATCH v2 scsi-fixes 1/3] scsi: target: iscsi: Don't drop live command refs on SNACK Reject Andrew Stellman
@ 2026-12-02 16:57 ` Andrew Stellman
2026-12-02 16:57 ` [PATCH v2 scsi-fixes 3/3] scsi: target: iscsi: Bound Status SNACKs to unacknowledged StatSNs Andrew Stellman
2 siblings, 0 replies; 6+ messages in thread
From: Andrew Stellman @ 2026-12-02 16:57 UTC (permalink / raw)
To: Martin K. Petersen
Cc: Mike Christie, Dmitry Bogdanov, Ziheng Wei, target-devel,
linux-scsi
From: Andrew Stellman <astellman@stellman-greene.com>
In iscsit_handle_recovery_datain() and iscsit_handle_r2t_snack(),
BegRun + RunLength is a u32 sum, so a huge RunLength wraps and passes
the check against the last DataSN or R2TSN sent.
Do the addition in u64. This changes existing behavior: a wrapping
request now gets a Bookmark Invalid Reject.
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>
---
v2: no code change.
v1: https://lore.kernel.org/r/20261130062812.56660-3-bugs@stellman-greene.com
Tested on 7.4-rc5 with this series (arm64 QEMU/HVF, KASAN, ERL=1, no
CHAP): wrapping Data and R2T SNACKs got a Reject (reason 0x09) and a
following NOP-Out was answered; valid ones were retransmitted.
Found with Quality Playbook
(https://github.com/andrewstellman/quality-playbook) running Claude Opus
5.5, Claude Sonnet 5.5 and Claude Haiku 5.5; the fix was drafted with
OpenAI GPT-6 (Codex).
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] 6+ messages in thread* [PATCH v2 scsi-fixes 3/3] scsi: target: iscsi: Bound Status SNACKs to unacknowledged StatSNs
2026-12-02 16:57 [PATCH v2 scsi-fixes 0/3] scsi: target: iscsi: SNACK Reject and range fixes Andrew Stellman
2026-12-02 16:57 ` [PATCH v2 scsi-fixes 1/3] scsi: target: iscsi: Don't drop live command refs on SNACK Reject Andrew Stellman
2026-12-02 16:57 ` [PATCH v2 scsi-fixes 2/3] scsi: target: iscsi: Check Data and R2T SNACK ranges without wrap Andrew Stellman
@ 2026-12-02 16:57 ` Andrew Stellman
2026-12-02 17:15 ` sashiko-bot
2 siblings, 1 reply; 6+ messages in thread
From: Andrew Stellman @ 2026-12-02 16:57 UTC (permalink / raw)
To: Martin K. Petersen
Cc: Mike Christie, Dmitry Bogdanov, Ziheng Wei, target-devel,
linux-scsi
From: Andrew Stellman <astellman@stellman-greene.com>
iscsit_handle_status_snack() resends every StatSN in the requested range
without checking that it was sent and is unacknowledged. Its plain < and >
comparisons break across the 2^32 wrap, conn->exp_statsn is unset at login,
iscsit_ack_from_expstatsn() accepts an ExpStatSN that moves back or past
StatSN, and cmd_lock is dropped before i_state is checked, so a response
still being sent can be queued again.
Reject requests outside the unacknowledged StatSN window, set ExpStatSN at
login, ignore an ExpStatSN that moves back or past StatSN, and queue only
ISTATE_SENT_STATUS commands under cmd_lock. This changes existing behavior:
those requests now get a Protocol Error Reject, and responses in other
states aren't resent.
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>
---
v2: rewritten after the Sashiko review of v1.
v1: https://lore.kernel.org/r/20261130062812.56660-4-bugs@stellman-greene.com
Tested on 7.4-rc5 with this series (arm64 QEMU/HVF, KASAN, ERL=1, no
CHAP, 3 second reply timeout in the test peer): SNACKs for a Login
Response StatSN, and for an acknowledged StatSN after ExpStatSN was
moved back, got a Reject (reason 0x04) and BegRun 0 with RunLength 0
right after login got no reply; v1 gave no reply to the first two and
sent a Reject for the third.
Found with Quality Playbook
(https://github.com/andrewstellman/quality-playbook) running Claude Opus
5.5, Sonnet 5.5 and Haiku 5.5; the fix was drafted with OpenAI GPT-6
(Codex).
A reproducer is available on request.
drivers/target/iscsi/iscsi_target.c | 5 ++-
drivers/target/iscsi/iscsi_target_erl1.c | 54 +++++++++++++-----------
drivers/target/iscsi/iscsi_target_erl1.h | 5 ++-
drivers/target/iscsi/iscsi_target_nego.c | 2 +
4 files changed, 39 insertions(+), 27 deletions(-)
diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c
index 91c95ee314d1f..39ae63a4f8a78 100644
--- a/drivers/target/iscsi/iscsi_target.c
+++ b/drivers/target/iscsi/iscsi_target.c
@@ -962,6 +962,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;
if (conn->sess->sess_ops->RDMAExtensions)
@@ -2558,7 +2561,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..bf7d7dcf2e334 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_keep_conn(conn,
+ ISCSI_REASON_PROTOCOL_ERROR, buf);
- last_statsn = (!runlength) ? conn->stat_sn : (begrun + runlength);
+ count = runlength ? runlength : sent - offset;
- while (begrun < last_statsn) {
+ while (count--) {
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;
}
- spin_unlock_bh(&cmd->istate_lock);
-
cmd->i_state = ISTATE_SEND_STATUS_RECOVERY;
- iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
+ ret = iscsit_add_cmd_to_response_queue(cmd, conn,
+ ISTATE_SEND_STATUS_RECOVERY);
+ if (ret)
+ cmd->i_state = ISTATE_SENT_STATUS;
+ spin_unlock(&cmd->istate_lock);
+ spin_unlock_bh(&conn->cmd_lock);
+ if (ret)
+ return ret;
begrun++;
}
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_nego.c b/drivers/target/iscsi/iscsi_target_nego.c
index ccd468e26fb92..be79f0f9bfa02 100644
--- a/drivers/target/iscsi/iscsi_target_nego.c
+++ b/drivers/target/iscsi/iscsi_target_nego.c
@@ -356,6 +356,8 @@ static int iscsi_target_do_tx_login_io(struct iscsit_conn *conn, struct iscsi_lo
if (conn->conn_transport->iscsit_put_login_tx(conn, login,
login->rsp_length + padding) < 0)
goto err;
+ if (login->login_complete)
+ conn->exp_statsn = conn->stat_sn;
login->rsp_length = 0;
--
2.53.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH v2 scsi-fixes 3/3] scsi: target: iscsi: Bound Status SNACKs to unacknowledged StatSNs
2026-12-02 16:57 ` [PATCH v2 scsi-fixes 3/3] scsi: target: iscsi: Bound Status SNACKs to unacknowledged StatSNs Andrew Stellman
@ 2026-12-02 17:15 ` sashiko-bot
0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-12-02 17:15 UTC (permalink / raw)
To: Andrew Stellman; +Cc: target-devel, linux-scsi
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] scsi: target: iscsi: premature loop break in
iscsit_handle_status_snack()
- [Medium] scsi: target: iscsi: non-SCSI responses resent as SCSI RSP on
Status SNACK
--
commit 1e2b7340230a6d6cf159ffb169b465170253c238
Author: Andrew Stellman <astellman@stellman-greene.com>
scsi: target: iscsi: Bound Status SNACKs to unacknowledged StatSNs
This commit initializes conn->exp_statsn at login, ignores invalid ExpStatSN
updates, validates that Status SNACK ranges fall within the unacknowledged
StatSN window, and queues only ISTATE_SENT_STATUS commands under cmd_lock.
> diff --git a/drivers/target/iscsi/iscsi_target_erl1.c b/drivers/target/iscsi/iscsi_target_erl1.c
> index 3562e9ad5e4c8..bf7d7dcf2e334 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_keep_conn(conn,
> + ISCSI_REASON_PROTOCOL_ERROR, buf);
>
> - last_statsn = (!runlength) ? conn->stat_sn : (begrun + runlength);
> + count = runlength ? runlength : sent - offset;
>
> - while (begrun < last_statsn) {
> + while (count--) {
> 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 walk on the first entry with
cmd->stat_sn == begrun miss the actual ISTATE_SENT_STATUS command and skip
resending its status?
An unsolicited NOP-In queued by iscsit_add_nopin() copies conn->stat_sn
without advancing it and stays on conn->conn_cmd_list in
ISTATE_SENT_NOPIN_WANT_RESPONSE:
drivers/target/iscsi/iscsi_target.c:iscsit_build_nopin_rsp() {
...
cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
conn->stat_sn;
...
}
The next response on the connection consumes that same StatSN and enters
ISTATE_SENT_STATUS later in conn->conn_cmd_list. Because the loop breaks on
the earlier NOP-In before checking cmd->i_state, the cmd->i_state !=
ISTATE_SENT_STATUS check drops the SNACK for begrun instead of continuing the
list search, leaving the initiator to time out waiting for the status
retransmission.
> cmd->i_state = ISTATE_SEND_STATUS_RECOVERY;
> - iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
> + ret = iscsit_add_cmd_to_response_queue(cmd, conn,
> + ISTATE_SEND_STATUS_RECOVERY);
[Severity: Medium]
Does queuing any ISTATE_SENT_STATUS command as ISTATE_SEND_STATUS_RECOVERY
cause non-SCSI responses to be resent as ISCSI_OP_SCSI_CMD_RSP PDUs?
In iscsit_response_queue(), non-SCSI responses such as ISTATE_SEND_REJECT
(including the reject added above by iscsit_add_reject_keep_conn()),
ISTATE_SEND_NOPIN, ISTATE_SEND_TEXTRSP, ISTATE_SEND_TASKMGTRSP, and
ISTATE_SEND_ASYNCMSG also advance conn->stat_sn and transition to
ISTATE_SENT_STATUS:
drivers/target/iscsi/iscsi_target.c:iscsit_response_queue() {
...
case ISTATE_SEND_STATUS:
case ISTATE_SEND_ASYNCMSG:
case ISTATE_SEND_NOPIN:
case ISTATE_SEND_STATUS_RECOVERY:
case ISTATE_SEND_TEXTRSP:
case ISTATE_SEND_TASKMGTRSP:
case ISTATE_SEND_REJECT:
spin_lock_bh(&cmd->istate_lock);
cmd->i_state = ISTATE_SENT_STATUS;
spin_unlock_bh(&cmd->istate_lock);
break;
...
}
When a Status SNACK covers that StatSN, queuing the command as
ISTATE_SEND_STATUS_RECOVERY sends it through iscsit_send_response() ->
iscsit_build_rsp_pdu(), which transmits a bogus SCSI Command Response and
calls iscsit_increment_maxcmdsn() even for PDUs like Reject or Async Message
that never incremented MaxCmdSN, desynchronizing the session CmdSN window.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202165734.51018-1-bugs@stellman-greene.com?part=3
^ permalink raw reply [flat|nested] 6+ messages in thread