* 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
2026-11-29 0:17 ` [PATCH v2 " Andrew Stellman
1 sibling, 0 replies; 4+ 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] 4+ messages in thread* [PATCH v2 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
@ 2026-11-29 0:17 ` Andrew Stellman
2026-11-29 0:36 ` sashiko-bot
1 sibling, 1 reply; 4+ messages in thread
From: Andrew Stellman @ 2026-11-29 0:17 UTC (permalink / raw)
To: Martin K. Petersen; +Cc: Mike Christie, 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 ffff0000e43f4b00 by task iscsi_ttx/376
iscsit_xmit_pdu+0x62c/0xbb8
iscsit_response_queue+0xa84/0x1340
Allocated by task 377:
kmemdup_noprof+0x34/0x60
iscsit_add_reject_from_cmd.isra.0+0xc4/0x2e0
Freed by task 376:
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. That commit removed the wait.
Send the Reject with iscsit_add_reject(), which copies the header into a
separate command, and free the failed WRITE directly, as the
ISTATE_REMOVE handler does, instead of queueing it. This changes
existing behaviour: the failed WRITE is now freed by the RX thread
before it reads the next PDU, and the Reject is sent from its own
command. The cxgbit code has the same paths and gets the same change.
Tested on arm64 QEMU with KASAN, iSCSI over TCP on loopback, ERL=1,
DataDigest=CRC32C and InitialR2T=No. Without this patch KASAN reported
the above. With it the Reject (reason Data Digest Error) was sent, and
there was no KASAN or refcount warning when the Reject was acknowledged,
when a Logout left it unacknowledged (also with a test-only 100 ms delay
added to connection cleanup), or when the initiator dropped the
connection. When the rejected WRITE had F=0, a Data-Out sent for its ITT
was dumped as an unknown ITT. The cxgbit change was compile-tested only.
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>
---
v2: Sashiko's review of v1 pointed out two problems with keeping the
failed WRITE on the connection, and both showed up in testing. With
F=0, a Data-Out for the rejected ITT was logged as outside the WRITE's
unsolicited sequence, and the initiator got a Reject or a reset instead
of the response to its next PDU. With a test-only pause added to
connection cleanup, a Logout before the Reject was acknowledged gave
"refcount_t: underflow; use-after-free". v2 sends the Reject from its
own command and frees the WRITE right away.
v1: https://lore.kernel.org/target-devel/20261128191722.30110-1-bugs@stellman-greene.com/
A reproducer is available on request.
drivers/target/iscsi/cxgbit/cxgbit_target.c | 13 ++++++++-----
drivers/target/iscsi/iscsi_target.c | 17 ++++++++++-------
2 files changed, 18 insertions(+), 12 deletions(-)
diff --git a/drivers/target/iscsi/cxgbit/cxgbit_target.c b/drivers/target/iscsi/cxgbit/cxgbit_target.c
index 3698f2eb097e37..0b0a3cc3361f36 100644
--- a/drivers/target/iscsi/cxgbit/cxgbit_target.c
+++ b/drivers/target/iscsi/cxgbit/cxgbit_target.c
@@ -871,7 +871,7 @@ cxgbit_handle_immediate_data(struct iscsit_cmd *cmd, struct iscsi_scsi_req *hdr,
return IMMEDIATE_DATA_CANNOT_RECOVER;
}
- iscsit_reject_cmd(cmd, ISCSI_REASON_DATA_DIGEST_ERROR,
+ iscsit_add_reject(conn, ISCSI_REASON_DATA_DIGEST_ERROR,
(unsigned char *)hdr);
return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
}
@@ -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 Reject uses a separate command, so this WRITE can be freed.
*
* FIXME: Send Unsolicited NOPIN with reserved
* TTT here to help the initiator figure out
@@ -955,8 +955,11 @@ 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);
+ spin_lock_bh(&conn->cmd_lock);
+ list_del_init(&cmd->i_conn_node);
+ spin_unlock_bh(&conn->cmd_lock);
+ target_put_sess_cmd(&cmd->se_cmd);
+ iscsit_free_cmd(cmd, false);
} 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..ec12852f3d498f 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 Reject uses a separate command, so this WRITE can be freed.
*
* FIXME: Send Unsolicited NOPIN with reserved
* TTT here to help the initiator figure out
@@ -1367,8 +1367,11 @@ 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);
+ spin_lock_bh(&cmd->conn->cmd_lock);
+ list_del_init(&cmd->i_conn_node);
+ spin_unlock_bh(&cmd->conn->cmd_lock);
+ target_put_sess_cmd(&cmd->se_cmd);
+ iscsit_free_cmd(cmd, false);
} else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
return -1;
@@ -2654,9 +2657,9 @@ static int iscsit_handle_immediate_data(
(unsigned char *)hdr);
return IMMEDIATE_DATA_CANNOT_RECOVER;
} else {
- iscsit_reject_cmd(cmd,
- ISCSI_REASON_DATA_DIGEST_ERROR,
- (unsigned char *)hdr);
+ iscsit_add_reject(conn,
+ ISCSI_REASON_DATA_DIGEST_ERROR,
+ (unsigned char *)hdr);
return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
}
} else {
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread