* [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
2026-11-29 0:17 ` [PATCH v2 " Andrew Stellman
0 siblings, 2 replies; 7+ 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] 7+ 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 2026-11-29 0:17 ` [PATCH v2 " Andrew Stellman 1 sibling, 0 replies; 7+ 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] 7+ 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 2026-11-29 15:52 ` [PATCH v3 scsi-fixes 1/2] scsi: target: iscsi: Reserve tags on standalone Rejects Andrew Stellman 1 sibling, 2 replies; 7+ 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] 7+ messages in thread
* Re: [PATCH v2 scsi-fixes] scsi: target: iscsi: Fix UAF on an immediate data digest error 2026-11-29 0:17 ` [PATCH v2 " Andrew Stellman @ 2026-11-29 0:36 ` sashiko-bot 2026-11-29 15:52 ` [PATCH v3 scsi-fixes 1/2] scsi: target: iscsi: Reserve tags on standalone Rejects Andrew Stellman 1 sibling, 0 replies; 7+ messages in thread From: sashiko-bot @ 2026-11-29 0:36 UTC (permalink / raw) To: Andrew Stellman; +Cc: target-devel, linux-scsi Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] scsi: target: iscsi: UAF via zeroed tags in iscsit_add_reject() -- commit 6dd082a26a7d6aa60cc88c1fd9d419ea3195000a Author: Andrew Stellman <astellman@stellman-greene.com> scsi: target: iscsi: Fix UAF on an immediate data digest error This commit sends the DataDigest error Reject at ErrorRecoveryLevel >= 1 via iscsit_add_reject() using a separate command and frees the failed WRITE command directly instead of queueing it as ISTATE_REMOVE. > diff --git a/drivers/target/iscsi/cxgbit/cxgbit_target.c b/drivers/target/iscsi/cxgbit/cxgbit_target.c > index 3698f2eb097e3..0b0a3cc3361f3 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; > } [ ... ] > diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c > index 2b4956f46c132..c4d114ffa5c41 100644 > --- a/drivers/target/iscsi/iscsi_target.c > +++ b/drivers/target/iscsi/iscsi_target.c [ ... ] > @@ -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 { [Severity: High] Can calling iscsit_add_reject() when the connection stays up allow a subsequent PDU to match the Reject command on conn->conn_cmd_list by its zeroed tags and cause a use-after-free or double free? Existing callers of iscsit_add_reject() sit on fatal paths that close the connection right away. Unlike iscsit_add_nopin(), iscsit_add_reject() leaves cmd->init_task_tag and cmd->targ_xfer_tag at 0 when adding the new command to conn->conn_cmd_list: drivers/target/iscsi/iscsi_target.c:iscsit_add_reject() { cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); if (!cmd) return -1; cmd->iscsi_opcode = ISCSI_OP_REJECT; cmd->reject_reason = reason; ... spin_lock_bh(&conn->cmd_lock); list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); spin_unlock_bh(&conn->cmd_lock); Because the connection remains active at ErrorRecoveryLevel >= 1 and the Reject command stays on conn->conn_cmd_list until its StatSN is acknowledged via ExpStatSN, a NOP-Out with ITT 0xffffffff and TTT 0 in iscsit_process_nop_out() matches the Reject command via iscsit_find_cmd_from_ttt(conn, 0) and queues it on the immediate queue as ISTATE_REMOVE. The TX thread then frees the Reject command from the immediate queue before iscsit_response_queue() transmits cmd->buf_ptr, or both iscsit_immediate_queue() and iscsit_ack_from_expstatsn() free it. Any PDU looking up ITT 0 (such as a Text, Data-Out, SNACK, or ABORT_TASK PDU) will also match the Reject command via iscsit_find_cmd_from_itt(). Initializing cmd->init_task_tag = RESERVED_ITT and cmd->targ_xfer_tag = 0xFFFFFFFF in iscsit_add_reject(), as iscsit_add_nopin() does, would prevent ITT and TTT lookups from matching Reject commands. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261129001755.78909-1-bugs@stellman-greene.com?part=1 ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v3 scsi-fixes 1/2] scsi: target: iscsi: Reserve tags on standalone Rejects 2026-11-29 0:17 ` [PATCH v2 " Andrew Stellman 2026-11-29 0:36 ` sashiko-bot @ 2026-11-29 15:52 ` Andrew Stellman 2026-11-29 15:52 ` [PATCH v3 scsi-fixes 2/2] scsi: target: iscsi: Fix UAF on an immediate data digest error Andrew Stellman 1 sibling, 1 reply; 7+ messages in thread From: Andrew Stellman @ 2026-11-29 15:52 UTC (permalink / raw) To: Martin K. Petersen; +Cc: Mike Christie, target-devel, linux-scsi From: Andrew Stellman <astellman@stellman-greene.com> iscsit_add_reject() allocates a zeroed command, so the Reject's ITT and TTT are both 0, and the command stays on conn_cmd_list until the initiator acknowledges its StatSN. When a Data-Out's DataSegmentLength is greater than MaxXmitDataSegmentLength, iscsit_check_dataout_hdr() queues such a Reject and iscsit_handle_data_out() returns 0, so the connection stays up. A following Data-Out with ITT 0 then matches the Reject and is dumped as a Data-Out for a non-WRITE command. A following NOP-Out with ITT 0xffffffff and TTT 0 matches it in iscsit_find_cmd_from_ttt(), and iscsit_process_nop_out() queues it as ISTATE_REMOVE. The TX thread services the immediate queue first and frees the Reject header before sending it: BUG: KASAN: slab-use-after-free in _copy_from_iter+0x770/0x10d0 Read of size 48 at addr ffff0000e8094780 by task iscsi_ttx/381 iscsit_xmit_pdu+0x62c/0xbb8 iscsit_response_queue+0xa84/0x1340 Allocated by task 382: kmemdup_noprof+0x34/0x60 iscsit_add_reject+0x94/0x210 iscsit_check_dataout_hdr+0x294/0x350 Freed by task 381: iscsit_release_cmd+0xbc/0x2c8 iscsit_free_cmd+0x100/0x14c iscsit_immediate_queue+0x104/0x8ac Set the Reject's ITT to RESERVED_ITT and its TTT to 0xffffffff, as iscsit_add_nopin() does for a NOP-In that wants no response. This changes existing behaviour for every iscsit_add_reject() caller: while the Reject is queued, a Data-Out with ITT 0 is dumped as an unknown ITT and a NOP-Out with TTT 0 is an unknown TTT. Tested on arm64 QEMU with KASAN, iSCSI over TCP on loopback, ERL=1, DataDigest=CRC32C and InitialR2T=No, with a test-only 100 ms delay in the TX thread before it services its queues. The initiator sent a Data-Out with DataSegmentLength 16777215 and no payload, a Data-Out with ITT 0 and a NOP-Out with ITT 0xffffffff and TTT 0. Without this patch KASAN reported the above. With it the target logged both tags as unknown and closed the connection, with no KASAN, refcount or WARNING report. A valid WRITE completed in both cases. The issue was found by Claude Opus 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: 561bf1589237 ("iscsi-target: Fix iscsit_sequence_cmd reject handling for iser") Cc: stable@vger.kernel.org Signed-off-by: Andrew Stellman <astellman@stellman-greene.com> --- v3: New patch. Mainline already reaches the zero tags through the oversized Data-Out Reject, so the tag fix is its own patch, ahead of the v2 change (2/2). v2: https://lore.kernel.org/target-devel/20261129001755.78909-1-bugs@stellman-greene.com/ A reproducer is available on request. drivers/target/iscsi/iscsi_target.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c index 2b4956f46c132..359c66d1051ab 100644 --- a/drivers/target/iscsi/iscsi_target.c +++ b/drivers/target/iscsi/iscsi_target.c @@ -797,6 +797,8 @@ int iscsit_add_reject( cmd->iscsi_opcode = ISCSI_OP_REJECT; cmd->reject_reason = reason; + cmd->init_task_tag = RESERVED_ITT; + cmd->targ_xfer_tag = 0xFFFFFFFF; cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL); if (!cmd->buf_ptr) { ^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v3 scsi-fixes 2/2] scsi: target: iscsi: Fix UAF on an immediate data digest error 2026-11-29 15:52 ` [PATCH v3 scsi-fixes 1/2] scsi: target: iscsi: Reserve tags on standalone Rejects Andrew Stellman @ 2026-11-29 15:52 ` Andrew Stellman 2026-11-29 16:12 ` sashiko-bot 0 siblings, 1 reply; 7+ messages in thread From: Andrew Stellman @ 2026-11-29 15:52 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 connection stays up after this Reject, so this relies on the previous patch giving it reserved tags. 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> --- v3: Code unchanged from v2. Sashiko's review of v2 pointed out that the Reject from iscsit_add_reject() has ITT and TTT 0 while the connection stays up. With a test-only 100 ms TX delay, v2 gave a KASAN slab-use-after-free on the Reject header when a NOP-Out with TTT 0 followed the bad WRITE; with 1/2 applied it didn't. 1/2 reserves the tags. v2: https://lore.kernel.org/target-devel/20261129001755.78909-1-bugs@stellman-greene.com/ 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 3698f2eb097e3..0b0a3cc3361f3 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 359c66d1051ab..fafd3d31a2a5d 100644 --- a/drivers/target/iscsi/iscsi_target.c +++ b/drivers/target/iscsi/iscsi_target.c @@ -1360,8 +1360,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 @@ -1369,8 +1369,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; @@ -2656,9 +2659,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 { ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v3 scsi-fixes 2/2] scsi: target: iscsi: Fix UAF on an immediate data digest error 2026-11-29 15:52 ` [PATCH v3 scsi-fixes 2/2] scsi: target: iscsi: Fix UAF on an immediate data digest error Andrew Stellman @ 2026-11-29 16:12 ` sashiko-bot 0 siblings, 0 replies; 7+ messages in thread From: sashiko-bot @ 2026-11-29 16:12 UTC (permalink / raw) To: Andrew Stellman; +Cc: target-devel, linux-scsi Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] scsi: target: iscsi: RX thread self-deadlock in iscsit_add_reject() -- commit ee60b35a65525f8ccfdd012ce8a26204ba7f7593 Author: Andrew Stellman <astellman@stellman-greene.com> scsi: target: iscsi: Fix UAF on an immediate data digest error This commit fixes a use-after-free on immediate data digest errors at ErrorRecoveryLevel >= 1 by sending the Reject PDU with iscsit_add_reject() and freeing the failed WRITE command directly in the RX thread. > diff --git a/drivers/target/iscsi/cxgbit/cxgbit_target.c b/drivers/target/iscsi/cxgbit/cxgbit_target.c > index 3698f2eb097e3..0b0a3cc3361f3 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 359c66d1051ab..fafd3d31a2a5d 100644 > --- a/drivers/target/iscsi/iscsi_target.c > +++ b/drivers/target/iscsi/iscsi_target.c > @@ -1360,8 +1360,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 > @@ -1369,8 +1369,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; > > @@ -2656,9 +2659,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 { [Severity: High] Can calling iscsit_add_reject() before freeing cmd deadlock the RX thread in iscsit_wait_for_tag()? iscsit_add_reject() calls iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE) to allocate a new command tag from se_sess->sess_tag_pool while the failed WRITE cmd still holds its own tag until iscsit_get_immediate_data() (or cxgbit_get_immediate_data()) frees it. If cmd took the last available tag in se_sess->sess_tag_pool, and the remaining tags are held by commands waiting for the RX thread to process subsequent PDUs (for example, sent responses awaiting ExpStatSN acknowledgement via iscsit_ack_from_expstatsn(), or multi-PDU WRITEs awaiting Data-Out PDUs), the RX thread sleeps in iscsit_wait_for_tag() and cannot make progress to free cmd or read the PDUs needed to release other tags. Freeing cmd in iscsit_get_immediate_data() and cxgbit_get_immediate_data() before calling iscsit_add_reject() would return its tag to se_sess->sess_tag_pool first. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261129155252.2217-1-bugs@stellman-greene.com?part=2 ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-29 16:12 UTC | newest] Thread overview: 7+ 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 2026-11-29 0:17 ` [PATCH v2 " Andrew Stellman 2026-11-29 0:36 ` sashiko-bot 2026-11-29 15:52 ` [PATCH v3 scsi-fixes 1/2] scsi: target: iscsi: Reserve tags on standalone Rejects Andrew Stellman 2026-11-29 15:52 ` [PATCH v3 scsi-fixes 2/2] scsi: target: iscsi: Fix UAF on an immediate data digest error Andrew Stellman 2026-11-29 16:12 ` sashiko-bot
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox