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

* Re: [PATCH] scsi: target: iscsi: hold cmd_lock and istate_lock across status SNACK recovery
  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
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-30 10:49 UTC (permalink / raw)
  To: Ziheng Wei; +Cc: linux-scsi, target-devel

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Critical] scsi: target: iscsi: incomplete i_state check in
  iscsit_handle_status_snack()
--

commit a8d8126cd528b9066379675b5773847632704322
Author: Ziheng Wei <zihengw03@163.com>

scsi: target: iscsi: hold cmd_lock and istate_lock across status SNACK recovery

This commit holds conn->cmd_lock across cmd->istate_lock and response
queue insertion in iscsit_handle_status_snack(), and rejects commands in
ISTATE_REMOVE to avoid racing with iscsit_ack_from_expstatsn().

> diff --git a/drivers/target/iscsi/iscsi_target_erl1.c b/drivers/target/iscsi/iscsi_target_erl1.c
> index d8ca06e697d63..22638e0919af3 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++;
>  	}

[Severity: Critical]
Should this check reject any state where cmd->i_state != ISTATE_SENT_STATUS
instead of only ISTATE_SEND_DATAIN and ISTATE_REMOVE?

Because ISTATE_SEND_STATUS_RECOVERY is not rejected, a second Status SNACK
for the same StatSN before the TX thread drains the first entry queues cmd
onto conn->response_queue_list a second time:

RX thread                              TX thread
iscsit_handle_status_snack()
  cmd->i_state = ISTATE_SEND_STATUS_RECOVERY
  iscsit_add_cmd_to_response_queue(cmd, ...)  [qr1]
iscsit_handle_status_snack()
  /* i_state is ISTATE_SEND_STATUS_RECOVERY */
  iscsit_add_cmd_to_response_queue(cmd, ...)  [qr2]
                                       iscsit_response_queue() [qr1]
                                         cmd->i_state = ISTATE_SENT_STATUS
iscsit_ack_from_expstatsn()
  /* sees ISTATE_SENT_STATUS */
  iscsit_free_cmd(cmd, false)
                                       iscsit_response_queue() [qr2]
                                         <-- use-after-free of cmd

When the TX thread completes qr1, iscsit_response_queue() resets
cmd->i_state to ISTATE_SENT_STATUS while qr2 is still queued.  A subsequent
PDU advancing ExpStatSN then causes iscsit_ack_from_expstatsn() to call
iscsit_free_cmd(cmd, false), which skips
iscsit_remove_cmd_from_response_queue() and frees cmd while qr2 remains on
conn->response_queue_list.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130103511.1384218-1-zihengw03@163.com?part=1

^ permalink raw reply	[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