[PATCH v2] scsi: target: iscsi: hold cmd_lock and require ISTATE_SENT_STATUS in status SNACK
From: Ziheng Wei
Date: Mon Nov 30 2026 - 08:43:29 EST
In iscsit_handle_status_snack(), conn->cmd_lock is released after finding
cmd and before acquiring cmd->istate_lock, and cmd->istate_lock is
released before setting cmd->i_state = ISTATE_SEND_STATUS_RECOVERY and
queueing cmd onto conn->response_queue_list. Concurrently, the TX thread
handling ISTATE_REMOVE or iscsit_aborted_task() can take conn->cmd_lock,
unlink cmd from conn->conn_cmd_list, and free it.
In addition, only checking cmd->i_state == ISTATE_SEND_DATAIN allows
duplicate Status SNACKs for the same StatSN (when cmd->i_state is already
ISTATE_SEND_STATUS_RECOVERY) to queue cmd onto conn->response_queue_list
multiple times. When the TX thread completes the first entry and resets
cmd->i_state to ISTATE_SENT_STATUS, a subsequent ExpStatSN ack calls
iscsit_free_cmd(cmd, false) while the duplicate queue entry is still
pending, causing a use-after-free.
Keep conn->cmd_lock held through cmd->istate_lock and
iscsit_add_cmd_to_response_queue(), require cmd->i_state ==
ISTATE_SENT_STATUS before transitioning to ISTATE_SEND_STATUS_RECOVERY
under cmd->istate_lock, and pass ISTATE_SEND_STATUS_RECOVERY explicitly.
If iscsit_add_cmd_to_response_queue() fails (e.g. -ENOMEM), restore
cmd->i_state to ISTATE_SENT_STATUS under cmd->istate_lock and return the
error so the command is not stranded in ISTATE_SEND_STATUS_RECOVERY.
Found by LLM-assisted static analysis and compile-tested on x86_64.
Fixes: e48354ce078c ("iscsi-target: Add iSCSI fabric support for target v4.1")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Ziheng Wei <zihengw03@xxxxxxx>
---
v1 -> v2:
- Require cmd->i_state == ISTATE_SENT_STATUS instead of only rejecting
ISTATE_SEND_DATAIN and ISTATE_REMOVE so duplicate Status SNACKs cannot
queue the same command onto conn->response_queue_list twice and trigger
a UAF (thanks to Sashiko AI review).
- Check the return value of iscsit_add_cmd_to_response_queue() and restore
cmd->i_state to ISTATE_SENT_STATUS on allocation failure before returning
the error.
- Add Cc: stable@xxxxxxxxxxxxxxx.
diff --git a/drivers/target/iscsi/iscsi_target_erl1.c b/drivers/target/iscsi/iscsi_target_erl1.c
--- a/drivers/target/iscsi/iscsi_target_erl1.c
+++ b/drivers/target/iscsi/iscsi_target_erl1.c
@@ -473,7 +473,7 @@
{
struct iscsit_cmd *cmd = NULL;
u32 last_statsn;
- int found_cmd;
+ int found_cmd, ret;
if (!begrun) {
begrun = conn->exp_statsn;
@@ -497,9 +497,9 @@
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"
@@ -508,9 +508,10 @@
continue;
}
- spin_lock_bh(&cmd->istate_lock);
- if (cmd->i_state == ISTATE_SEND_DATAIN) {
- spin_unlock_bh(&cmd->istate_lock);
+ spin_lock(&cmd->istate_lock);
+ if (cmd->i_state != ISTATE_SENT_STATUS) {
+ spin_unlock(&cmd->istate_lock);
+ spin_unlock_bh(&conn->cmd_lock);
pr_err("Ignoring Status SNACK for BegRun:"
" 0x%08x, RunLength: 0x%08x, assuming this was"
" a protactic SNACK for an untransmitted"
@@ -518,10 +519,20 @@
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);
+ spin_unlock(&cmd->istate_lock);
+
+ ret = iscsit_add_cmd_to_response_queue(cmd, conn,
+ ISTATE_SEND_STATUS_RECOVERY);
+ if (ret < 0) {
+ spin_lock(&cmd->istate_lock);
+ if (cmd->i_state == ISTATE_SEND_STATUS_RECOVERY)
+ cmd->i_state = ISTATE_SENT_STATUS;
+ spin_unlock(&cmd->istate_lock);
+ spin_unlock_bh(&conn->cmd_lock);
+ return ret;
+ }
+ spin_unlock_bh(&conn->cmd_lock);
begrun++;
}
base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa