Linux SCSI subsystem development
 help / color / mirror / Atom feed
* [PATCH scsi-fixes] scsi: target: iscsi: Fix UAF on an immediate data digest error
@ 2026-11-28 19:17 Andrew Stellman
  2026-11-28 19:38 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Stellman @ 2026-11-28 19:17 UTC (permalink / raw)
  To: Martin K. Petersen; +Cc: target-devel, linux-scsi

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

At ErrorRecoveryLevel >= 1, when the DataDigest of a command's immediate
data doesn't match, iscsit_handle_immediate_data() calls
iscsit_reject_cmd(), which queues the command on the response queue as
ISTATE_SEND_REJECT with the Reject header in cmd->buf_ptr.
iscsit_get_immediate_data() then also queues the command on the
immediate queue as ISTATE_REMOVE. The TX thread services the immediate
queue first, so iscsit_free_cmd() frees cmd->buf_ptr before the Reject
is sent:

  BUG: KASAN: slab-use-after-free in _copy_from_iter+0x770/0x10d0
  Read of size 48 at addr ffff0000e3270d80 by task iscsi_ttx/400
   iscsit_xmit_pdu+0x62c/0xbb8
   iscsit_response_queue+0xa84/0x1340
  Allocated by task 401:
   kmemdup_noprof+0x34/0x60
   iscsit_add_reject_from_cmd.isra.0+0xc4/0x2e0
  Freed by task 400:
   iscsit_release_cmd+0xbc/0x2c8
   iscsit_free_cmd+0x80/0x14c
   iscsit_immediate_queue+0x104/0x8ac

Before commit ba159914086f ("iscsi-target: Fix iscsit_add_reject* usage
for iser"), iscsit_add_reject_from_cmd() waited on cmd->reject_comp
until the Reject had been sent, so the ISTATE_REMOVE entry was queued
only after that. That commit removed the wait.

Don't queue the ISTATE_REMOVE entry. This changes when the command is
freed: instead of right after the digest failure, it's freed when the
initiator acknowledges the Reject's StatSN or when the connection is
cleaned up. cxgbit_get_immediate_data() has the same code and gets the
same change.

Tested on arm64 QEMU with KASAN, iSCSI over TCP on loopback, ERL=1 and
DataDigest=CRC32C. Without this patch KASAN reported the above; with it
there was no KASAN report. A patched build with debug prints and
kmemleak added showed the command freed after a NOP-Out acknowledged
the Reject and, in another session, after the initiator dropped the
connection, and a kmemleak scan after target teardown reported nothing.
The cxgbit change was not tested.

The issue was found by Claude Opus 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 Opus 5.5, and both were reviewed by me.

Fixes: ba159914086f ("iscsi-target: Fix iscsit_add_reject* usage for iser")
Cc: stable@vger.kernel.org
Signed-off-by: Andrew Stellman <astellman@stellman-greene.com>
---
A reproducer is available on request.

 drivers/target/iscsi/cxgbit/cxgbit_target.c | 6 ++----
 drivers/target/iscsi/iscsi_target.c         | 6 ++----
 2 files changed, 4 insertions(+), 8 deletions(-)

diff --git a/drivers/target/iscsi/cxgbit/cxgbit_target.c b/drivers/target/iscsi/cxgbit/cxgbit_target.c
index 3698f2eb097e37..445bf5439964cc 100644
--- a/drivers/target/iscsi/cxgbit/cxgbit_target.c
+++ b/drivers/target/iscsi/cxgbit/cxgbit_target.c
@@ -946,8 +946,8 @@ cxgbit_get_immediate_data(struct iscsit_cmd *cmd, struct iscsi_scsi_req *hdr,
 	} else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
 		/*
 		 * Immediate Data failed DataCRC and ERL>=1,
-		 * silently drop this PDU and let the initiator
-		 * plug the CmdSN gap.
+		 * reject this PDU and let the initiator plug the CmdSN gap.
+		 * The queued Reject retains the command.
 		 *
 		 * FIXME: Send Unsolicited NOPIN with reserved
 		 * TTT here to help the initiator figure out
@@ -955,8 +955,6 @@ cxgbit_get_immediate_data(struct iscsit_cmd *cmd, struct iscsi_scsi_req *hdr,
 		 * intelligent enough to determine the missing
 		 * CmdSN and issue a retry to plug the sequence.
 		 */
-		cmd->i_state = ISTATE_REMOVE;
-		iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
 	} else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
 		return -1;
 
diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c
index 03d618cd767218..1ee6362d270062 100644
--- a/drivers/target/iscsi/iscsi_target.c
+++ b/drivers/target/iscsi/iscsi_target.c
@@ -1358,8 +1358,8 @@ iscsit_get_immediate_data(struct iscsit_cmd *cmd, struct iscsi_scsi_req *hdr,
 	} else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
 		/*
 		 * Immediate Data failed DataCRC and ERL>=1,
-		 * silently drop this PDU and let the initiator
-		 * plug the CmdSN gap.
+		 * reject this PDU and let the initiator plug the CmdSN gap.
+		 * The queued Reject retains the command.
 		 *
 		 * FIXME: Send Unsolicited NOPIN with reserved
 		 * TTT here to help the initiator figure out
@@ -1367,8 +1367,6 @@ iscsit_get_immediate_data(struct iscsit_cmd *cmd, struct iscsi_scsi_req *hdr,
 		 * intelligent enough to determine the missing
 		 * CmdSN and issue a retry to plug the sequence.
 		 */
-		cmd->i_state = ISTATE_REMOVE;
-		iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
 	} else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
 		return -1;
 
-- 
2.53.0


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

* Re: [PATCH scsi-fixes] scsi: target: iscsi: Fix UAF on an immediate data digest error
  2026-11-28 19:17 [PATCH scsi-fixes] scsi: target: iscsi: Fix UAF on an immediate data digest error Andrew Stellman
@ 2026-11-28 19:38 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-28 19:38 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:
- [High] scsi: target: iscsi: UAF on teardown of retained WRITE_PENDING
  reject cmd
- [High] scsi: target: iscsi: stale ITT on retained reject cmd breaks
  Data-Out/UAF
--

commit 8f8dd4e72853eebb81ea598563061a3153ff3d89
Author: Andrew Stellman <astellman@stellman-greene.com>

scsi: target: iscsi: Fix UAF on an immediate data digest error

This commit stops queuing ISTATE_REMOVE on the immediate queue when
immediate data fails its data digest check at ErrorRecoveryLevel >= 1 in
iscsit_get_immediate_data() and cxgbit_get_immediate_data(), so that the
command is not freed before the queued Reject PDU is transmitted.

> diff --git a/drivers/target/iscsi/cxgbit/cxgbit_target.c b/drivers/target/iscsi/cxgbit/cxgbit_target.c
> index 3698f2eb097e3..445bf5439964c 100644
> --- a/drivers/target/iscsi/cxgbit/cxgbit_target.c
> +++ b/drivers/target/iscsi/cxgbit/cxgbit_target.c
> @@ -946,8 +946,8 @@ cxgbit_get_immediate_data(struct iscsit_cmd *cmd, struct iscsi_scsi_req *hdr,
>  	} else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
>  		/*
>  		 * Immediate Data failed DataCRC and ERL>=1,
> -		 * silently drop this PDU and let the initiator
> -		 * plug the CmdSN gap.
> +		 * reject this PDU and let the initiator plug the CmdSN gap.
> +		 * The queued Reject retains the command.
>  		 *
>  		 * FIXME: Send Unsolicited NOPIN with reserved
>  		 * TTT here to help the initiator figure out
> @@ -955,8 +955,6 @@ cxgbit_get_immediate_data(struct iscsit_cmd *cmd, struct iscsi_scsi_req *hdr,
>  		 * intelligent enough to determine the missing
>  		 * CmdSN and issue a retry to plug the sequence.
>  		 */
> -		cmd->i_state = ISTATE_REMOVE;
> -		iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
>  	} else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
>  		return -1;
>  
> diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c
> index 2b4956f46c132..8e1dec7e96510 100644
> --- a/drivers/target/iscsi/iscsi_target.c
> +++ b/drivers/target/iscsi/iscsi_target.c
> @@ -1358,8 +1358,8 @@ iscsit_get_immediate_data(struct iscsit_cmd *cmd, struct iscsi_scsi_req *hdr,
>  	} else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
>  		/*
>  		 * Immediate Data failed DataCRC and ERL>=1,
> -		 * silently drop this PDU and let the initiator
> -		 * plug the CmdSN gap.
> +		 * reject this PDU and let the initiator plug the CmdSN gap.
> +		 * The queued Reject retains the command.
>  		 *
>  		 * FIXME: Send Unsolicited NOPIN with reserved
>  		 * TTT here to help the initiator figure out
> @@ -1367,8 +1367,6 @@ iscsit_get_immediate_data(struct iscsit_cmd *cmd, struct iscsi_scsi_req *hdr,
>  		 * intelligent enough to determine the missing
>  		 * CmdSN and issue a retry to plug the sequence.
>  		 */
> -		cmd->i_state = ISTATE_REMOVE;
> -		iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
>  	} else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
>  		return -1;

[Severity: High]
Can retaining cmd on conn->conn_cmd_list in both iscsit_get_immediate_data()
and cxgbit_get_immediate_data() cause a refcount underflow and
use-after-free if the connection closes before ExpStatSN acknowledges the
Reject?

Before immediate data is read, iscsit_setup_scsi_cmd() takes a second
reference via target_get_sess_cmd() (cmd_kref = 2), and
iscsit_process_scsi_cmd() -> transport_generic_new_cmd() adds cmd->se_cmd to
se_dev->queues[cpuid].state_list and sets cmd->se_cmd.t_state =
TRANSPORT_WRITE_PENDING.  When the immediate data CRC fails,
iscsit_reject_cmd() -> iscsit_add_reject_from_cmd() calls
target_put_sess_cmd(), dropping cmd_kref to 1 while leaving t_state as
TRANSPORT_WRITE_PENDING.

If the connection is torn down before the Reject's StatSN is acknowledged,
iscsit_release_commands_from_conn() sees t_state == TRANSPORT_WRITE_PENDING
and calls target_complete_cmd(&cmd->se_cmd, SAM_STAT_TASK_ABORTED), which
schedules target_complete_ok_work, and then calls iscsit_free_cmd(cmd, true)
in its second loop:

iscsit_release_commands_from_conn()   kworker (target_completion_wq)
  target_complete_cmd(&cmd->se_cmd, ...)
    schedule_work(&cmd->work)
                                      target_complete_ok_work()
                                        lio_queue_status()
                                          iscsit_add_cmd_to_response_queue()
                                        transport_cmd_check_stop_to_fabric()
                                          lio_check_stop_free()
                                            target_put_sess_cmd()
                                              /* cmd_kref 1 -> 0, frees cmd */
  iscsit_free_cmd(cmd, true)
    transport_generic_free_cmd()
      target_put_sess_cmd()  <-- refcount underflow / use-after-free

Even if target_wait_free_cmd() sets CMD_T_STOP before
target_complete_ok_work finishes, target_complete_ok_work still queues a new
iscsi_queue_req onto conn->response_queue_list via lio_queue_status() after
the first __iscsit_free_cmd(cmd, true) has flushed the response queue.
Because cmd_kref was already 1, transport_generic_free_cmd() drops it to 0,
frees cmd, and returns rc = 1, so iscsit_free_cmd() skips the second
__iscsit_free_cmd(cmd, true) and leaves a dangling qr->cmd on
conn->response_queue_list that iscsit_free_queue_reqs_for_conn() later
dereferences.  In addition, because cmd->se_cmd remains on state_list, an
ABORT_TASK or LUN_RESET TMF can also find and abort it while cmd_kref is 1.

[Severity: High]
Does leaving the rejected command on conn->conn_cmd_list with its original
cmd->init_task_tag also allow subsequent Data-Out or SNACK PDUs for that ITT
to match the rejected command?

When the rejected WRITE command had F=0 (cmd->unsolicited_data == 1),
iscsit_get_immediate_data() and cxgbit_get_immediate_data() skip
iscsit_set_unsolicited_dataout(cmd), leaving cmd->seq_end_offset == 0 while
ICF_GOT_LAST_DATAOUT is unset.  If pipelined Unsolicited Data-Out PDUs from
the same first burst arrive, iscsit_find_cmd_from_itt_or_dump() matches the
retained cmd instead of dumping their payload:

drivers/target/iscsi/iscsi_target_erl0.c:iscsit_dataout_check_unsolicited_sequence() {
	if ((be32_to_cpu(hdr->offset) < cmd->seq_start_offset) ||
	   ((be32_to_cpu(hdr->offset) + payload_length) > cmd->seq_end_offset)) {
...
		return DATAOUT_CANNOT_RECOVER;
	}
}

Because seq_end_offset is 0, iscsit_check_dataout_hdr() returns -1 without
draining payload_length bytes from the socket, and iscsit_handle_data_out()
returns 0, leaving the unconsumed Data-Out payload in the TCP stream to be
parsed as the next iSCSI PDU header (or dropping the connection in cxgbit).

Furthermore, if a follow-up PDU for that ITT matches the retained cmd and
triggers iscsit_reject_cmd() (for example, a Data-Out PDU with offset +
payload_length > cmd->se_cmd.data_length in __iscsit_check_dataout_hdr()),
iscsit_add_reject_from_cmd() sees cmd->se_cmd.se_tfo != NULL and calls
target_put_sess_cmd(&cmd->se_cmd) a second time, dropping cmd_kref from 1 to
0 and freeing cmd while it is still linked on conn->conn_cmd_list and
conn->response_queue_list.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261128191722.30110-1-bugs@stellman-greene.com?part=1

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

end of thread, other threads:[~2026-11-28 19:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28 19:17 [PATCH scsi-fixes] scsi: target: iscsi: Fix UAF on an immediate data digest error Andrew Stellman
2026-11-28 19:38 ` sashiko-bot

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