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

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