Linux SCSI subsystem development
 help / color / mirror / Atom feed
* [PATCH v4 scsi-fixes 1/2] scsi: target: iscsi: Reserve tags on standalone Rejects
@ 2026-12-02 16:49 Andrew Stellman
  2026-12-02 16:49 ` [PATCH v4 scsi-fixes 2/2] scsi: target: iscsi: Fix UAF on an immediate data digest error Andrew Stellman
  2026-12-02 17:03 ` [PATCH v4 scsi-fixes 1/2] scsi: target: iscsi: Reserve tags on standalone Rejects sashiko-bot
  0 siblings, 2 replies; 3+ messages in thread
From: Andrew Stellman @ 2026-12-02 16:49 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 a Reject's ITT and
TTT are both 0 while it waits on conn_cmd_list for its StatSN to be
acknowledged. After an oversized Data-Out gets such a Reject (the
connection stays up), a 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, so the TX thread 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

  CPU: 0 UID: 0 PID: 381 Comm: iscsi_ttx Not tainted 7.4.0-rc4 #1 PREEMPT(full)
  Hardware name: linux,dummy-virt (DT)
  Call trace:
   show_stack+0x18/0x24 (C)
   dump_stack_lvl+0x60/0x80
   print_report+0x17c/0x4d8
   kasan_report+0xbc/0x124
   kasan_check_range+0x100/0x1a8
   __asan_memcpy+0x3c/0xa0
   _copy_from_iter+0x770/0x10d0
   tcp_sendmsg_locked+0x16dc/0x33dc
   tcp_sendmsg+0x38/0x60
   inet_sendmsg+0x90/0xdc
   sock_sendmsg+0x144/0x228
   tx_data+0x140/0x1e8
   iscsit_send_tx_data+0xb0/0x198
   iscsit_xmit_pdu+0x62c/0xbb8
   iscsit_response_queue+0xa84/0x1340
   iscsi_target_tx_thread+0x404/0x460
   kthread+0x36c/0x420
   ret_from_fork+0x10/0x20

  Allocated by task 382:
   kasan_save_stack+0x3c/0x70
   kasan_save_track+0x20/0x4c
   kasan_save_alloc_info+0x40/0x60
   __kasan_kmalloc+0xb8/0xbc
   __kmalloc_node_track_caller_noprof+0x1b0/0x458
   kmemdup_noprof+0x34/0x60
   iscsit_add_reject+0x94/0x210
   iscsit_check_dataout_hdr+0x294/0x350
   iscsit_get_rx_pdu+0x3d4/0x24a0
   iscsi_target_rx_thread+0x110/0x260
   kthread+0x36c/0x420
   ret_from_fork+0x10/0x20

  Freed by task 381:
   kasan_save_stack+0x3c/0x70
   kasan_save_track+0x20/0x4c
   kasan_save_free_info+0x4c/0x80
   __kasan_slab_free+0x78/0xa4
   kfree+0x124/0x410
   iscsit_release_cmd+0xbc/0x2c8
   iscsit_free_cmd+0x100/0x14c
   iscsit_immediate_queue+0x104/0x8ac
   iscsi_target_tx_thread+0x340/0x460
   kthread+0x36c/0x420
   ret_from_fork+0x10/0x20

  The buggy address belongs to the object at ffff0000e8094780
   which belongs to the cache kmalloc-64 of size 64
  The buggy address is located 0 bytes inside of
   freed 64-byte region [ffff0000e8094780, ffff0000e80947c0)

  The buggy address belongs to the physical page:
  page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x128094
  flags: 0xbfffe0000000000(node=0|zone=2|lastcpupid=0x1ffff)
  page_type: f5(slab)
  raw: 0bfffe0000000000 ffff0000c00028c0 dead000000000122 0000000000000000
  raw: 0000000000000000 0000000000200020 00000000f5000000 0000000000000000
  page dumped because: kasan: bad access detected

  Memory state around the buggy address:
   ffff0000e8094680: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
   ffff0000e8094700: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
  >ffff0000e8094780: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
                     ^
   ffff0000e8094800: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
   ffff0000e8094880: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
  ==================================================================

Set the Reject's ITT to RESERVED_ITT and its TTT to 0xffffffff, as
iscsit_add_nopin() does. This changes existing behaviour for every
iscsit_add_reject() caller.

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: https://lore.kernel.org/r/20261129155252.2217-1-bugs@stellman-greene.com/
v2: https://lore.kernel.org/target-devel/20261129001755.78909-1-bugs@stellman-greene.com/
Tested in arm64 QEMU with KASAN and a test-only 100 ms delay in the TX
thread: without this patch KASAN reported the above, and with it there
was no KASAN, refcount or WARNING report.
Found with Quality Playbook
(https://github.com/andrewstellman/quality-playbook) running Claude Opus
5.5; the fix was drafted with OpenAI GPT-6 (Codex).
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) {
-- 
2.53.0


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

* [PATCH v4 scsi-fixes 2/2] scsi: target: iscsi: Fix UAF on an immediate data digest error
  2026-12-02 16:49 [PATCH v4 scsi-fixes 1/2] scsi: target: iscsi: Reserve tags on standalone Rejects Andrew Stellman
@ 2026-12-02 16:49 ` Andrew Stellman
  2026-12-02 17:03 ` [PATCH v4 scsi-fixes 1/2] scsi: target: iscsi: Reserve tags on standalone Rejects sashiko-bot
  1 sibling, 0 replies; 3+ messages in thread
From: Andrew Stellman @ 2026-12-02 16:49 UTC (permalink / raw)
  To: Martin K. Petersen; +Cc: Mike Christie, target-devel, linux-scsi

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

At ErrorRecoveryLevel >= 1, iscsit_handle_immediate_data() queues a
Reject on a WRITE whose immediate data fails the DataDigest check, and
iscsit_get_immediate_data() then queues the WRITE as ISTATE_REMOVE, so
the TX thread frees the Reject header in cmd->buf_ptr before sending it:

  ==================================================================
  BUG: KASAN: slab-use-after-free in _copy_from_iter+0x770/0x10d0
  Read of size 48 at addr ffff0000e43f4b00 by task iscsi_ttx/376

  CPU: 0 UID: 0 PID: 376 Comm: iscsi_ttx Not tainted 7.4.0-rc4-gf4585067150b #15 PREEMPT(full)
  Hardware name: linux,dummy-virt (DT)
  Call trace:
   show_stack+0x18/0x24 (C)
   dump_stack_lvl+0x60/0x80
   print_report+0x17c/0x4d8
   kasan_report+0xbc/0x124
   kasan_check_range+0x100/0x1a8
   __asan_memcpy+0x3c/0xa0
   _copy_from_iter+0x770/0x10d0
   tcp_sendmsg_locked+0x16dc/0x33dc
   tcp_sendmsg+0x38/0x60
   inet_sendmsg+0x90/0xdc
   sock_sendmsg+0x144/0x228
   tx_data+0x140/0x1e8
   iscsit_send_tx_data+0xb0/0x198
   iscsit_xmit_pdu+0x62c/0xbb8
   iscsit_response_queue+0xa84/0x1340
   iscsi_target_tx_thread+0x224/0x420
   kthread+0x36c/0x420
   ret_from_fork+0x10/0x20

  Allocated by task 377:
   kasan_save_stack+0x3c/0x70
   kasan_save_track+0x20/0x4c
   kasan_save_alloc_info+0x40/0x60
   __kasan_kmalloc+0xb8/0xbc
   __kmalloc_node_track_caller_noprof+0x1b0/0x458
   kmemdup_noprof+0x34/0x60
   iscsit_add_reject_from_cmd.isra.0+0xc4/0x2e0
   iscsit_get_rx_pdu+0x2410/0x24a0
   iscsi_target_rx_thread+0x110/0x260
   kthread+0x36c/0x420
   ret_from_fork+0x10/0x20

  Freed by task 376:
   kasan_save_stack+0x3c/0x70
   kasan_save_track+0x20/0x4c
   kasan_save_free_info+0x4c/0x80
   __kasan_slab_free+0x78/0xa4
   kfree+0x124/0x410
   iscsit_release_cmd+0xbc/0x2c8
   lio_release_cmd+0x14/0x20
   target_release_cmd_kref+0x2e0/0x87c
   transport_generic_free_cmd+0x200/0x2e0
   iscsit_free_cmd+0x80/0x14c
   iscsit_immediate_queue+0x104/0x8ac
   iscsi_target_tx_thread+0x194/0x420
   kthread+0x36c/0x420
   ret_from_fork+0x10/0x20

  The buggy address belongs to the object at ffff0000e43f4b00
   which belongs to the cache kmalloc-64 of size 64
  The buggy address is located 0 bytes inside of
   freed 64-byte region [ffff0000e43f4b00, ffff0000e43f4b40)

  The buggy address belongs to the physical page:
  page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x1243f4
  flags: 0xbfffe0000000000(node=0|zone=2|lastcpupid=0x1ffff)
  page_type: f5(slab)
  raw: 0bfffe0000000000 ffff0000c00028c0 dead000000000122 0000000000000000
  raw: 0000000000000000 0000000000200020 00000000f5000000 0000000000000000
  page dumped because: kasan: bad access detected

  Memory state around the buggy address:
   ffff0000e43f4a00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
   ffff0000e43f4a80: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
  >ffff0000e43f4b00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
                     ^
   ffff0000e43f4b80: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
   ffff0000e43f4c00: fa fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
  ==================================================================

Free the failed WRITE in iscsit_get_immediate_data() and send the Reject
from its own command with iscsit_add_reject(), in the TCP and cxgbit
paths. This changes existing behaviour: the RX thread now frees the
WRITE before it reads the next PDU.

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: https://lore.kernel.org/r/20261129155252.2217-2-bugs@stellman-greene.com/
Tested on mainline 7.4-rc5 with 1/2 and this patch in arm64 QEMU with
KASAN: the bad WRITE got a reason-2 Reject with no KASAN, WARNING or
refcount report (the trace above is from 7.4.0-rc4 without this series).
Found with Quality Playbook
(https://github.com/andrewstellman/quality-playbook) running Claude Opus
5.5 and Claude Haiku 5.5; the fix was drafted with OpenAI GPT-6 (Codex).
A reproducer is available on request.

 drivers/target/iscsi/cxgbit/cxgbit_target.c | 15 +++++++++------
 drivers/target/iscsi/iscsi_target.c         | 17 ++++++++++-------
 2 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/drivers/target/iscsi/cxgbit/cxgbit_target.c b/drivers/target/iscsi/cxgbit/cxgbit_target.c
index 3698f2eb097e3..a3814438af621 100644
--- a/drivers/target/iscsi/cxgbit/cxgbit_target.c
+++ b/drivers/target/iscsi/cxgbit/cxgbit_target.c
@@ -871,8 +871,6 @@ 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,
-				  (unsigned char *)hdr);
 		return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
 	}
 
@@ -946,8 +944,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 +953,13 @@ 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);
+		iscsit_add_reject(conn, ISCSI_REASON_DATA_DIGEST_ERROR,
+				  (unsigned char *)hdr);
 	} 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..a8d44ac00abb6 100644
--- a/drivers/target/iscsi/iscsi_target.c
+++ b/drivers/target/iscsi/iscsi_target.c
@@ -1317,6 +1317,7 @@ static int
 iscsit_get_immediate_data(struct iscsit_cmd *cmd, struct iscsi_scsi_req *hdr,
 			  bool dump_payload)
 {
+	struct iscsit_conn *conn = cmd->conn;
 	int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
 	int rc;
 
@@ -1360,8 +1361,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 +1370,13 @@ 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);
+		iscsit_add_reject(conn, ISCSI_REASON_DATA_DIGEST_ERROR,
+				  (unsigned char *)hdr);
 	} else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
 		return -1;
 
@@ -2656,9 +2662,6 @@ 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);
 				return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
 			}
 		} else {
-- 
2.53.0


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

* Re: [PATCH v4 scsi-fixes 1/2] scsi: target: iscsi: Reserve tags on standalone Rejects
  2026-12-02 16:49 [PATCH v4 scsi-fixes 1/2] scsi: target: iscsi: Reserve tags on standalone Rejects Andrew Stellman
  2026-12-02 16:49 ` [PATCH v4 scsi-fixes 2/2] scsi: target: iscsi: Fix UAF on an immediate data digest error Andrew Stellman
@ 2026-12-02 17:03 ` sashiko-bot
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-12-02 17:03 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:
- [Critical] scsi: target: iscsi: iscsit_find_cmd_from_itt() matches
  Reject RESERVED_ITT
--

commit 0c7a31e09b4e80ec4c1b3312cdddc1bc8f4f0667
Author: Andrew Stellman <astellman@stellman-greene.com>

scsi: target: iscsi: Reserve tags on standalone Rejects

This commit sets init_task_tag to RESERVED_ITT and targ_xfer_tag to
0xFFFFFFFF on standalone Reject commands in iscsit_add_reject() so they do
not stay zeroed on conn_cmd_list.

> 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) {

[Severity: Critical]
Can an ISCSI_OP_TEXT PDU with hdr->ttt != 0xFFFFFFFF and hdr->itt ==
RESERVED_ITT match this Reject command via iscsit_find_cmd_from_itt() and
cause the same use-after-free on cmd->buf_ptr?

When hdr->ttt != 0xFFFFFFFF, the ISCSI_OP_TEXT handler looks up hdr->itt
without rejecting RESERVED_ITT or checking cmd->iscsi_opcode:

drivers/target/iscsi/iscsi_target.c:iscsi_target_rx_opcode() {
    ...
	case ISCSI_OP_TEXT:
		if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
			cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
			if (!cmd)
				goto reject;
    ...
}

If the initiator follows a non-fatal Reject (such as an oversized
Data-Out) with an empty non-immediate ISCSI_OP_TEXT PDU carrying hdr->ttt
== 0, hdr->itt == RESERVED_ITT, and hdr->cmdsn < ExpCmdSN,
iscsit_process_text_cmd() overwrites cmd->targ_xfer_tag on the matched
Reject command and passes it to iscsit_sequence_cmd().  Because CmdSN is
lower than ExpCmdSN, iscsit_sequence_cmd() queues the Reject command onto
conn->immed_queue_list with ISTATE_REMOVE, freeing cmd->buf_ptr on the TX
thread before iscsit_response_queue() sends the Reject PDU.

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

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

end of thread, other threads:[~2026-12-02 17:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 16:49 [PATCH v4 scsi-fixes 1/2] scsi: target: iscsi: Reserve tags on standalone Rejects Andrew Stellman
2026-12-02 16:49 ` [PATCH v4 scsi-fixes 2/2] scsi: target: iscsi: Fix UAF on an immediate data digest error Andrew Stellman
2026-12-02 17:03 ` [PATCH v4 scsi-fixes 1/2] scsi: target: iscsi: Reserve tags on standalone Rejects sashiko-bot

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