Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: Andrew Stellman <bugs@stellman-greene.com>
To: "Martin K. Petersen" <mkp@kernel.org>
Cc: Mike Christie <michael.christie@oracle.com>,
	target-devel@vger.kernel.org, linux-scsi@vger.kernel.org
Subject: [PATCH v3 scsi-fixes 2/2] scsi: target: iscsi: Fix UAF on an immediate data digest error
Date: Sun, 29 Nov 2026 10:52:52 -0500	[thread overview]
Message-ID: <20261129155252.2217-2-bugs@stellman-greene.com> (raw)
In-Reply-To: <20261129155252.2217-1-bugs@stellman-greene.com>

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 {

  reply	other threads:[~2026-11-29 15:52 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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     ` Andrew Stellman [this message]
2026-11-29 16:12       ` [PATCH v3 scsi-fixes 2/2] scsi: target: iscsi: Fix UAF on an immediate data digest error sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261129155252.2217-2-bugs@stellman-greene.com \
    --to=bugs@stellman-greene.com \
    --cc=linux-scsi@vger.kernel.org \
    --cc=michael.christie@oracle.com \
    --cc=mkp@kernel.org \
    --cc=target-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox