Linux SCSI subsystem development
 help / color / mirror / Atom feed
* [PATCH v3] scsi: target: iscsi: reject Text data on a continuation request
@ 2026-12-03 14:02 James Sawyer via B4 Relay
  2026-12-03 14:26 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: James Sawyer via B4 Relay @ 2026-12-03 14:02 UTC (permalink / raw)
  To: Martin K. Petersen
  Cc: linux-scsi, target-devel, linux-kernel, security, James Sawyer

From: James Sawyer <james@jamessawyer.co.uk>

A Text Request with TTT != 0xFFFFFFFF continues an earlier multi-PDU
SendTargets response. iscsit_find_cmd_from_itt() looks the command up by
ITT on conn->conn_cmd_list, so it returns a command that is already
linked there. Such a continuation is only valid when it is empty. If it
carries a "SendTargets=" payload, iscsit_process_text_cmd() runs a new
request on the reused command and calls list_add_tail(&cmd->i_conn_node,
&conn->conn_cmd_list) on it a second time.

A Text command stays on conn_cmd_list until the initiator's ExpStatSN
acknowledges it, so the initiator controls whether it is still listed,
and discovery sessions accept Text Requests without authentication by
default (iscsit_load_discovery_tpg()).

With another command after it (H -> X -> Y), re-adding X passes the
CONFIG_LIST_HARDENED checks and leaves H->next == X, X->next == H,
X->prev == Y. When X is later acknowledged and freed, list_del_init(X)
also passes the checks and H->next still points to the freed command,
so the next walk of conn_cmd_list uses freed memory. If X is the tail,
hardened kernels warn and skip the add; others create a self-loop.

Reject a data-carrying continuation in iscsit_setup_text_cmd(), before
the looked-up command is mutated, next to the existing Text rejects and
using the same mechanism. This covers iscsi_target, isert and cxgbit,
which all call iscsit_setup_text_cmd() before iscsit_process_text_cmd().

Found by source review only; no reproducer was built. Compile-tested
only (W=1, drivers/target/iscsi/iscsi_target.o, v7.4-rc1, no warnings);
no iSCSI traffic was run.

This issue was found and the fix written with AI assistance.

Fixes: e4f4e8016e68 ("iscsi/iser-target: Support multi-sequence sendtargets text response")
Signed-off-by: James Sawyer <james@jamessawyer.co.uk>
---
Notes for reviewers (not for the commit):
- Found by source review with AI assistance; not reproduced, compile-tested only.
- The looked-up command for a TTT != 0xFFFFFFFF continuation may still be
  in flight on the TX thread and may even be a non-Text command (the ITT
  lookup has no type check). This patch rejects before iscsit_setup_text_cmd()
  mutates it, via the existing iscsit_reject_cmd() path already used for the
  other Text rejects. The broader question of whether that reject path is
  itself safe on an in-flight or non-Text command is pre-existing and not
  addressed here; happy to follow up if you want it handled.

Changes in v3:
- v2 rejected the continuation in iscsit_process_text_cmd(), AFTER
  iscsit_setup_text_cmd() had already mutated the looked-up command and
  freed its text_in_ptr, then repurposed that command through
  iscsit_reject_cmd(). As the Sashiko review of v2 noted, that is unsafe:
  the command may be in flight, iscsit_add_reject_from_cmd() overwrites
  cmd->buf_ptr without freeing the prior buffer, and it drops a
  target_put_sess_cmd() reference if the ITT matched a SCSI/TMR command.
- v3 rejects in iscsit_setup_text_cmd() before any mutation, so the
  looked-up command is left untouched. The list_add_tail() guard from v1
  is no longer needed and is dropped.
- v1: https://lore.kernel.org/r/20261203-iscsi-text-double-listadd-v1-1-dff966a10cf0@jamessawyer.co.uk
- v2: https://lore.kernel.org/r/20261203-iscsi-text-double-listadd-v2-1-36ebee74657d@jamessawyer.co.uk
---
 drivers/target/iscsi/iscsi_target.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c
index 124ff269b8e7..d33b563ee83e 100644
--- a/drivers/target/iscsi/iscsi_target.c
+++ b/drivers/target/iscsi/iscsi_target.c
@@ -2172,6 +2172,20 @@ iscsit_setup_text_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
 					(unsigned char *)hdr);
 	}
 
+	/*
+	 * A Text Request with TTT != 0xFFFFFFFF continues an earlier
+	 * SendTargets response and reuses the command found by ITT, which is
+	 * still linked on conn_cmd_list. Such a continuation must be empty.
+	 * Reject one that carries data here, before the looked-up command is
+	 * mutated, rather than process a new request on it.
+	 */
+	if (hdr->ttt != cpu_to_be32(0xFFFFFFFF) && payload_length) {
+		pr_err("Text data on a continuation request is not supported, TTT: 0x%08x\n",
+		       be32_to_cpu(hdr->ttt));
+		return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
+					 (unsigned char *)hdr);
+	}
+
 	pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
 		" ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
 		hdr->exp_statsn, payload_length);

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261203-iscsi-text-double-listadd-2030d9087986

Best regards,
--  
James Sawyer <james@jamessawyer.co.uk>



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

* Re: [PATCH v3] scsi: target: iscsi: reject Text data on a continuation request
  2026-12-03 14:02 [PATCH v3] scsi: target: iscsi: reject Text data on a continuation request James Sawyer via B4 Relay
@ 2026-12-03 14:26 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-03 14:26 UTC (permalink / raw)
  To: James Sawyer; +Cc: linux-scsi, target-devel

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Critical] scsi: target: iscsi: iscsit_setup_text_cmd() hijacks live
  cmd on reject
- [High] scsi: target: iscsi: early Text reject leaves unread TCP
  payload
--

commit 16b6a243b5153b553b7782bb0663c15e0f491e40
Author: James Sawyer <james@jamessawyer.co.uk>

scsi: target: iscsi: reject Text data on a continuation request

This commit adds a check in iscsit_setup_text_cmd() to reject Text Requests
that carry a non-zero payload when TTT != 0xFFFFFFFF, before the rest of
iscsit_setup_text_cmd() updates the looked-up command.

> diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c
> index 124ff269b8e75..d33b563ee83eb 100644
> --- a/drivers/target/iscsi/iscsi_target.c
> +++ b/drivers/target/iscsi/iscsi_target.c
> @@ -2172,6 +2172,20 @@ iscsit_setup_text_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
>  					(unsigned char *)hdr);
>  	}
>  
> +	/*
> +	 * A Text Request with TTT != 0xFFFFFFFF continues an earlier
> +	 * SendTargets response and reuses the command found by ITT, which is
> +	 * still linked on conn_cmd_list. Such a continuation must be empty.
> +	 * Reject one that carries data here, before the looked-up command is
> +	 * mutated, rather than process a new request on it.
> +	 */
> +	if (hdr->ttt != cpu_to_be32(0xFFFFFFFF) && payload_length) {
> +		pr_err("Text data on a continuation request is not supported, TTT: 0x%08x\n",
> +		       be32_to_cpu(hdr->ttt));
> +		return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
> +					 (unsigned char *)hdr);
> +	}

[Severity: Critical]
Does passing the looked-up cmd to iscsit_reject_cmd() really leave it
untouched?

When hdr->ttt != 0xFFFFFFFF, all three transports obtain cmd via
iscsit_find_cmd_from_itt(), which returns any live command on
conn->conn_cmd_list matching that ITT regardless of opcode or state.
iscsit_reject_cmd() calls iscsit_add_reject_from_cmd(), which mutates it:

drivers/target/iscsi/iscsi_target.c:iscsit_add_reject_from_cmd() {
	...
	const bool do_put = cmd->se_cmd.se_tfo != NULL;
	...
	cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
	if (!cmd->buf_ptr) {
		...
		iscsit_free_cmd(cmd, false);
		return -1;
	}
	...
	cmd->i_state = ISTATE_SEND_REJECT;
	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
	if (do_put) {
		...
		target_put_sess_cmd(&cmd->se_cmd);
	}
}

If the ITT matches an active SCSI or TMR command, target_put_sess_cmd()
drops a reference it does not own, causing a use-after-free while target
core and conn->conn_cmd_list still reference cmd.

Overwriting cmd->buf_ptr also leaks any existing buffer (such as a
SendTargets response), and if TX is concurrently running
iscsit_send_text_rsp() it can read text_length bytes past the 48-byte
allocation.  On OOM, iscsit_free_cmd(cmd, false) frees a linked cmd.

Writing cmd->i_state without cmd->istate_lock and queueing a second
response entry can also race with iscsit_ack_from_expstatsn() freeing cmd
with check_queues false, leaving a use-after-free on response_queue_list.

Finally, iscsit_build_reject() ORs ISCSI_FLAG_CMD_FINAL into the reused
header without zeroing it, leaving stale ISCSI_FLAG_TEXT_CONTINUE set.
On iSER this prevents isert_put_cmd() from releasing cmd, and
isert_put_reject() reuses isert_cmd->tx_desc while a send may be active.

Rejecting a data-carrying continuation with a newly allocated reject
command (or closing the connection) instead of reusing cmd would leave
the looked-up command untouched.

[Severity: High]
Can returning early here leave the Text payload unread on the TCP socket?

On TCP, iscsit_get_rx_pdu() reads only the 48-byte BHS header before
calling iscsit_handle_text_cmd(), leaving ALIGN(payload_length, 4) bytes
(plus optional DataDigest) unread until rx_data().  When this check
triggers, payload_length is non-zero, yet the error return is swallowed:

drivers/target/iscsi/iscsi_target.c:iscsit_handle_text_cmd() {
	...
	rc = iscsit_setup_text_cmd(conn, cmd, hdr);
	if (rc < 0)
		return 0;
	...
}

Because iscsi_target_rx_opcode() then returns 0, iscsit_get_rx_pdu()
continues its loop and parses the unread Text payload bytes as the next
iSCSI PDU header, desynchronizing the TCP stream and keeping the
connection open for repeated pr_err() log flooding and buffer leaks.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203-iscsi-text-double-listadd-v3-1-75dd3ef9ce30@jamessawyer.co.uk?part=1

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

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

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 14:02 [PATCH v3] scsi: target: iscsi: reject Text data on a continuation request James Sawyer via B4 Relay
2026-12-03 14:26 ` sashiko-bot

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