SCSI target development
 help / color / mirror / Atom feed
* [PATCH scsi-fixes] scsi: target: iscsi: Fix the Discovery session opcode check
@ 2026-11-28 19:17 Andrew Stellman
  2026-11-28 19:26 ` sashiko-bot
  2026-11-29  0:17 ` [PATCH v2 " Andrew Stellman
  0 siblings, 2 replies; 3+ 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>

RFC 7143 section 4.3 limits a Discovery session to Text and Logout
requests. The TCP and cxgbit receive loops check this with bit tests,
which pass any opcode with bit 0x04 set, so a Data-Out PDU (0x05) sent
in a Discovery session passes the check and reaches the Data-Out
handler.

Compare the masked opcode with ISCSI_OP_TEXT and ISCSI_OP_LOGOUT
instead. This changes existing behaviour: in a Discovery session,
Data-Out and the other opcodes with bit 0x04 set now take the existing
illegal-opcode reject path. Normal sessions are not affected.

Tested over TCP on mainline in an arm64 QEMU guest. Before the fix, a
Data-Out sent after a Discovery login reached the ITT lookup in the
Data-Out handler; with the fix it was rejected at the Discovery check.
A SendTargets Text request got a Text Response in both cases. The
cxgbit change was compiled but not run.

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: e48354ce078c ("iscsi-target: Add iSCSI fabric support for target v4.1")
Signed-off-by: Andrew Stellman <astellman@stellman-greene.com>
---
A reproducer is available on request.

 drivers/target/iscsi/cxgbit/cxgbit_target.c | 3 +--
 drivers/target/iscsi/iscsi_target.c         | 3 +--
 2 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/target/iscsi/cxgbit/cxgbit_target.c b/drivers/target/iscsi/cxgbit/cxgbit_target.c
index 3698f2eb097e37..79fc3ff458dc34 100644
--- a/drivers/target/iscsi/cxgbit/cxgbit_target.c
+++ b/drivers/target/iscsi/cxgbit/cxgbit_target.c
@@ -1301,8 +1301,7 @@ static int cxgbit_rx_opcode(struct cxgbit_sock *csk)
 	opcode = hdr->opcode & ISCSI_OPCODE_MASK;
 
 	if (conn->sess->sess_ops->SessionType &&
-	    ((!(opcode & ISCSI_OP_TEXT)) ||
-	     (!(opcode & ISCSI_OP_LOGOUT)))) {
+	    opcode != ISCSI_OP_TEXT && opcode != ISCSI_OP_LOGOUT) {
 		pr_err("Received illegal iSCSI Opcode: 0x%02x"
 			" while in Discovery Session, rejecting.\n", opcode);
 		iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c
index 03d618cd767218..dcb4c4a8e829f1 100644
--- a/drivers/target/iscsi/iscsi_target.c
+++ b/drivers/target/iscsi/iscsi_target.c
@@ -4088,8 +4088,7 @@ static void iscsit_get_rx_pdu(struct iscsit_conn *conn)
 		opcode = buffer[0] & ISCSI_OPCODE_MASK;
 
 		if (conn->sess->sess_ops->SessionType &&
-		   ((!(opcode & ISCSI_OP_TEXT)) ||
-		    (!(opcode & ISCSI_OP_LOGOUT)))) {
+		    opcode != ISCSI_OP_TEXT && opcode != ISCSI_OP_LOGOUT) {
 			pr_err("Received illegal iSCSI Opcode: 0x%02x"
 			" while in Discovery Session, rejecting.\n", opcode);
 			iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
-- 
2.53.0


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

* Re: [PATCH scsi-fixes] scsi: target: iscsi: Fix the Discovery session opcode check
  2026-11-28 19:17 [PATCH scsi-fixes] scsi: target: iscsi: Fix the Discovery session opcode check Andrew Stellman
@ 2026-11-28 19:26 ` sashiko-bot
  2026-11-29  0:17 ` [PATCH v2 " Andrew Stellman
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-28 19:26 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:
- [Medium] scsi: target: iscsi: bogus Reject on HeaderDigest error in
  iscsit_get_rx_pdu()
--

commit 1fd1145d67663e7038dd80d77ad570d438916435
Author: Andrew Stellman <astellman@stellman-greene.com>

scsi: target: iscsi: Fix the Discovery session opcode check

This commit replaces the bitwise opcode checks for Discovery sessions in
iscsit_get_rx_pdu() and cxgbit_rx_opcode() with equality checks against
ISCSI_OP_TEXT and ISCSI_OP_LOGOUT.

> diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c
> index 2b4956f46c132..3f56c5495821e 100644
> --- a/drivers/target/iscsi/iscsi_target.c
> +++ b/drivers/target/iscsi/iscsi_target.c
> @@ -4088,8 +4088,7 @@ static void iscsit_get_rx_pdu(struct iscsit_conn *conn)
>  		opcode = buffer[0] & ISCSI_OPCODE_MASK;
>  
>  		if (conn->sess->sess_ops->SessionType &&
> -		   ((!(opcode & ISCSI_OP_TEXT)) ||
> -		    (!(opcode & ISCSI_OP_LOGOUT)))) {
> +		    opcode != ISCSI_OP_TEXT && opcode != ISCSI_OP_LOGOUT) {
>  			pr_err("Received illegal iSCSI Opcode: 0x%02x"
>  			" while in Discovery Session, rejecting.\n", opcode);
>  			iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,

[Severity: Medium]
On a HeaderDigest CRC32C failure in a Discovery session, does this cause
iscsit_get_rx_pdu() to queue a Reject PDU containing the synthetic 0xff
buffer instead of dropping the connection in iscsi_target_rx_opcode()?

Just above this check, the HeaderDigest error path overwrites buffer:

drivers/target/iscsi/iscsi_target.c:iscsit_get_rx_pdu() {
    ...
			if (digest != checksum) {
				pr_err("HeaderDigest CRC32C failed,"
					" received 0x%08x, computed 0x%08x\n",
					digest, checksum);
				/*
				 * Set the PDU to 0xff so it will intentionally
				 * hit default in the switch below.
				 */
				memset(buffer, 0xff, ISCSI_HDR_LEN);
				atomic_long_inc(&conn->sess->conn_digest_errors);
    ...
}

Previously, the resulting opcode 0x3f passed the bitwise check (since bit
0x04 was set) and reached the default case in iscsi_target_rx_opcode().
With the equality check, a digest failure in a Discovery session hits this
branch, logs a bogus illegal opcode 0x3f error, and passes the all-0xff
buffer to iscsit_add_reject().

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

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

* [PATCH v2 scsi-fixes] scsi: target: iscsi: Fix the Discovery session opcode check
  2026-11-28 19:17 [PATCH scsi-fixes] scsi: target: iscsi: Fix the Discovery session opcode check Andrew Stellman
  2026-11-28 19:26 ` sashiko-bot
@ 2026-11-29  0:17 ` Andrew Stellman
  1 sibling, 0 replies; 3+ 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>

RFC 7143 section 4.3 limits a Discovery session to Text and Logout
requests. The TCP and cxgbit receive loops check this with bit tests,
which pass any opcode with bit 0x04 set, so a Data-Out PDU (0x05) sent
in a Discovery session passes the check and reaches the Data-Out
handler.

Compare the masked opcode with ISCSI_OP_TEXT and ISCSI_OP_LOGOUT
instead. This changes existing behaviour: in a Discovery session,
Data-Out and the other opcodes with bit 0x04 set now take the existing
illegal-opcode reject path. The check still applies only to Discovery
sessions.

On TCP, a header that fails its HeaderDigest check is overwritten with
0xff so that it hits the default case of the opcode switch. Skip the
Discovery check for that header, so it still gets there instead of
being rejected as an illegal Discovery opcode. cxgbit handles header
CRC errors before its Discovery check.

Tested over TCP on mainline in an arm64 QEMU KASAN guest. With the
fix, a Data-Out sent after a Discovery login was rejected at the
Discovery check, a header with a bad HeaderDigest in a Discovery
session still closed the connection (ERL=0), and a SendTargets Text
request got a Text Response. The cxgbit change was compiled but not
run.

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: e48354ce078c ("iscsi-target: Add iSCSI fabric support for target v4.1")
Signed-off-by: Andrew Stellman <astellman@stellman-greene.com>
---
v2: Skip the Discovery check for a header that failed its HeaderDigest
check, so it reaches the default case of the opcode switch as it did
before v1 (reported by Sashiko). The cxgbit hunk is unchanged.

With v1 in the same setup, the bad HeaderDigest header was logged as
an illegal Discovery opcode 0x3f in all four runs, and in two of them
a Reject carrying the 0xff header reached the initiator.

v1: https://lore.kernel.org/target-devel/20261128191735.30260-1-bugs@stellman-greene.com/

A reproducer is available on request.

 drivers/target/iscsi/cxgbit/cxgbit_target.c | 3 +--
 drivers/target/iscsi/iscsi_target.c         | 8 +++++---
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/drivers/target/iscsi/cxgbit/cxgbit_target.c b/drivers/target/iscsi/cxgbit/cxgbit_target.c
index 3698f2eb097e37..79fc3ff458dc34 100644
--- a/drivers/target/iscsi/cxgbit/cxgbit_target.c
+++ b/drivers/target/iscsi/cxgbit/cxgbit_target.c
@@ -1301,8 +1301,7 @@ static int cxgbit_rx_opcode(struct cxgbit_sock *csk)
 	opcode = hdr->opcode & ISCSI_OPCODE_MASK;
 
 	if (conn->sess->sess_ops->SessionType &&
-	    ((!(opcode & ISCSI_OP_TEXT)) ||
-	     (!(opcode & ISCSI_OP_LOGOUT)))) {
+	    opcode != ISCSI_OP_TEXT && opcode != ISCSI_OP_LOGOUT) {
 		pr_err("Received illegal iSCSI Opcode: 0x%02x"
 			" while in Discovery Session, rejecting.\n", opcode);
 		iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c
index 03d618cd767218..6cdee9c5ea0a5b 100644
--- a/drivers/target/iscsi/iscsi_target.c
+++ b/drivers/target/iscsi/iscsi_target.c
@@ -4010,6 +4010,7 @@ static void iscsit_get_rx_pdu(struct iscsit_conn *conn)
 	int ret;
 	u8 *buffer, *tmp_buf, opcode;
 	u32 checksum = 0, digest = 0;
+	bool bad_hdr_digest;
 	struct iscsi_hdr *hdr;
 	struct kvec iov;
 
@@ -4024,6 +4025,7 @@ static void iscsit_get_rx_pdu(struct iscsit_conn *conn)
 		 */
 		iscsit_thread_check_cpumask(conn, current, 0);
 
+		bad_hdr_digest = false;
 		memset(&iov, 0, sizeof(struct kvec));
 
 		iov.iov_base	= buffer;
@@ -4075,6 +4077,7 @@ static void iscsit_get_rx_pdu(struct iscsit_conn *conn)
 				 * hit default in the switch below.
 				 */
 				memset(buffer, 0xff, ISCSI_HDR_LEN);
+				bad_hdr_digest = true;
 				atomic_long_inc(&conn->sess->conn_digest_errors);
 			} else {
 				pr_debug("Got HeaderDigest CRC32C"
@@ -4087,9 +4090,8 @@ static void iscsit_get_rx_pdu(struct iscsit_conn *conn)
 
 		opcode = buffer[0] & ISCSI_OPCODE_MASK;
 
-		if (conn->sess->sess_ops->SessionType &&
-		   ((!(opcode & ISCSI_OP_TEXT)) ||
-		    (!(opcode & ISCSI_OP_LOGOUT)))) {
+		if (!bad_hdr_digest && conn->sess->sess_ops->SessionType &&
+		    opcode != ISCSI_OP_TEXT && opcode != ISCSI_OP_LOGOUT) {
 			pr_err("Received illegal iSCSI Opcode: 0x%02x"
 			" while in Discovery Session, rejecting.\n", opcode);
 			iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
-- 
2.53.0


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

end of thread, other threads:[~2026-11-29  0:17 UTC | newest]

Thread overview: 3+ 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 the Discovery session opcode check Andrew Stellman
2026-11-28 19:26 ` sashiko-bot
2026-11-29  0:17 ` [PATCH v2 " Andrew Stellman

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