Linux SCSI subsystem development
 help / color / mirror / Atom feed
* [PATCH] scsi: target: iscsi: hold cmd_lock and istate_lock across status SNACK recovery
@ 2026-11-30 10:35 Ziheng Wei
  2026-11-30 10:49 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Ziheng Wei @ 2026-11-30 10:35 UTC (permalink / raw)
  To: Martin K . Petersen; +Cc: linux-scsi, target-devel, linux-kernel

In iscsit_handle_status_snack(), conn->cmd_lock is released right after
finding cmd, before acquiring cmd->istate_lock, and cmd->istate_lock is
released before writing cmd->i_state = ISTATE_SEND_STATUS_RECOVERY and
queueing the command. A concurrent iscsit_ack_from_expstatsn() (which
locks cmd_lock -> istate_lock) can transition the command to
ISTATE_REMOVE, unlink it, and free it in that window, causing a
use-after-free or overwriting ISTATE_REMOVE.

Keep conn->cmd_lock held from lookup through cmd->istate_lock and
iscsit_add_cmd_to_response_queue(), reject ISTATE_REMOVE as well as
ISTATE_SEND_DATAIN, update cmd->i_state under cmd->istate_lock, and pass
ISTATE_SEND_STATUS_RECOVERY explicitly.

Found by LLM-assisted static analysis and compile-tested on x86_64.

Fixes: e48354ce078c ("iscsi-target: Add iSCSI fabric support for target v4.1")
Signed-off-by: Ziheng Wei <zihengw03@163.com>
---
 drivers/target/iscsi/iscsi_target_erl1.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/drivers/target/iscsi/iscsi_target_erl1.c b/drivers/target/iscsi/iscsi_target_erl1.c
index d8ca06e69..22638e091 100644
--- a/drivers/target/iscsi/iscsi_target_erl1.c
+++ b/drivers/target/iscsi/iscsi_target_erl1.c
@@ -497,9 +497,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"
@@ -508,9 +508,11 @@ 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);
+		spin_lock(&cmd->istate_lock);
+		if (cmd->i_state == ISTATE_SEND_DATAIN ||
+		    cmd->i_state == ISTATE_REMOVE) {
+			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 +520,12 @@ int iscsit_handle_status_snack(
 			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);
+
+		iscsit_add_cmd_to_response_queue(cmd, conn,
+						 ISTATE_SEND_STATUS_RECOVERY);
+		spin_unlock_bh(&conn->cmd_lock);
 		begrun++;
 	}
 

base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa
-- 
2.43.0


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

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

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 10:35 [PATCH] scsi: target: iscsi: hold cmd_lock and istate_lock across status SNACK recovery Ziheng Wei
2026-11-30 10:49 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox