Skip to contentSkip to conversation

target/iscsi: Fix detection of excess number of login exchanges

PATCH In review Petr Pavlu 3 participants 3 messages Feb 22, 2022 – Mar 11, 2022

Subsystems (2)
lore
Share & tools
lore

Petr Pavlu

author
#0 lore raw

PATCH 1/1 target/iscsi: Fix detection of excess number of login exchanges

From: Petr Pavlu <[email protected]>

Function iscsi_target_do_login() attempts to cancel a connection when a number of login exchanges reaches MAX_LOGIN_PDUS (7). This is done by having a local counter and incrementing+checking it as the function processes requests in a loop. A problem is that since the login rework in back in 2013, the function always processes only a single request and the loop is terminated at the end of the first iteration. This means the counter reaches only value 1 and any excess number of login requests is never rejected.

Fix the problem by introducing iscsi_login.negotiation_exchanges counter and update the logic to count exchanges per each login phase as described in RFC 7143:

Quoted 8 lines

6.2. Text Mode Negotiation:
[...]
In the Login Phase (see Section 6.3), every stage is a separate negotiation. [...]
[...]
An iSCSI initiator or target MAY terminate a negotiation that does not terminate within an implementation-specific reasonable time or number of exchanges but SHOULD allow at least six (6) exchanges.

Fixes d381a8010a05 ("iscsi-target: Add login negotiation multi-plexing support")
---
 drivers/target/iscsi/iscsi_target_nego.c | 92 ++++++++++++------------
 include/target/iscsi/iscsi_target_core.h |  1 +
 2 files changed, 47 insertions(+), 46 deletions(-)
drivers/target/iscsi/iscsi_target_nego.c +46 -46
index c0ed6f8e5c5b..a5077ea09f6c 100644
@@ -970,65 +970,65 @@ static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_log
 
 static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login)
 {
-	int pdu_count = 0;
 	struct iscsi_login_req *login_req;
 	struct iscsi_login_rsp *login_rsp;
 
 	login_req = (struct iscsi_login_req *) login->req;
 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
 
-	while (1) {
-		if (++pdu_count > MAX_LOGIN_PDUS) {
-			pr_err("MAX_LOGIN_PDUS count reached.\n");
-			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
-					ISCSI_LOGIN_STATUS_TARGET_ERROR);
+	switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
+	case 0:
+		login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
+		if (iscsi_target_handle_csg_zero(conn, login) < 0)
 			return -1;
-		}
-
-		switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
-		case 0:
-			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
-			if (iscsi_target_handle_csg_zero(conn, login) < 0)
-				return -1;
-			break;
-		case 1:
-			login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
-			if (iscsi_target_handle_csg_one(conn, login) < 0)
+		break;
+	case 1:
+		login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
+		if (iscsi_target_handle_csg_one(conn, login) < 0)
+			return -1;
+		if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
+			/*
+			 * Check to make sure the TCP connection has not dropped
+			 * asynchronously while session reinstatement was
+			 * occurring in this kthread context, before
+			 * transitioning to full feature phase operation.
+			 */
+			if (iscsi_target_sk_check_close(conn))
 				return -1;
-			if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
-				/*
-				 * Check to make sure the TCP connection has not
-				 * dropped asynchronously while session reinstatement
-				 * was occuring in this kthread context, before
-				 * transitioning to full feature phase operation.
-				 */
-				if (iscsi_target_sk_check_close(conn))
-					return -1;
 
-				login->tsih = conn->sess->tsih;
-				login->login_complete = 1;
-				iscsi_target_restore_sock_callbacks(conn);
-				if (iscsi_target_do_tx_login_io(conn,
-						login) < 0)
-					return -1;
-				return 1;
-			}
-			break;
-		default:
-			pr_err("Illegal CSG: %d received from"
-				" Initiator, protocol error.\n",
-				ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
-			break;
+			login->tsih = conn->sess->tsih;
+			login->login_complete = 1;
+			iscsi_target_restore_sock_callbacks(conn);
+			if (iscsi_target_do_tx_login_io(conn, login) < 0)
+				return -1;
+			return 1;
 		}
+		break;
+	default:
+		pr_err("Illegal CSG: %d received from Initiator,"
+			" protocol error.\n",
+			ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
+		break;
+	}
 
-		if (iscsi_target_do_tx_login_io(conn, login) < 0)
+	if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT)
+		login->negotiation_exchanges = 0;
+	else {
+		login->negotiation_exchanges++;
+		if (login->negotiation_exchanges >= MAX_LOGIN_PDUS) {
+			pr_err("MAX_LOGIN_PDUS count reached.\n");
+			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
+					ISCSI_LOGIN_STATUS_TARGET_ERROR);
 			return -1;
-
-		if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
-			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
-			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
 		}
-		break;
+	}
+
+	if (iscsi_target_do_tx_login_io(conn, login) < 0)
+		return -1;
+
+	if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
+		login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
+		login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
 	}
 
 	return 0;
include/target/iscsi/iscsi_target_core.h +1 -0
index 1eccb2ac7d02..b6a5e1cf3f77 100644
@@ -705,6 +705,7 @@ struct iscsi_login {
 	u32 rsp_length;
 	u16 cid;
 	u16 tsih;
+	u8 negotiation_exchanges;
 	char req[ISCSI_HDR_LEN];
 	char rsp[ISCSI_HDR_LEN];
 	char *req_buf;
Signature · 1 line
2.35.1
#1 lore raw
Quoted 11 lines · Petr Pavlu

From: Petr Pavlu <[email protected]>

Function iscsi_target_do_login() attempts to cancel a connection when a number of login exchanges reaches MAX_LOGIN_PDUS (7). This is done by having a local counter and incrementing+checking it as the function processes requests in a loop. A problem is that since the login rework in back in 2013, the function always processes only a single request and the loop is terminated at the end of the first iteration. This means the counter reaches only value 1 and any excess number of login requests is never rejected.

Quoted 12 lines

Fix the problem by introducing iscsi_login.negotiation_exchanges counter and update the logic to count exchanges per each login phase as described in RFC 7143:

Quoted 8 lines

6.2. Text Mode Negotiation:
[...]
In the Login Phase (see Section 6.3), every stage is a separate negotiation. [...]
[...]
An iSCSI initiator or target MAY terminate a negotiation that does not terminate within an implementation-specific reasonable time or number of exchanges but SHOULD allow at least six (6) exchanges.

It wasn't clear to me what this fixes. Today, are initiators sending more than 6 exchanges and if so what happens to the target? Is it crashing or annoying to user or cause some sort of endless login so we run out of resources? Or is this more of code cleanup?

When does this happen and with what initiators?

#2 lore raw
Quoted 25 lines · Mike Christie
Quoted 11 lines · Petr Pavlu

From: Petr Pavlu <[email protected]>

Function iscsi_target_do_login() attempts to cancel a connection when a number of login exchanges reaches MAX_LOGIN_PDUS (7). This is done by having a local counter and incrementing+checking it as the function processes requests in a loop. A problem is that since the login rework in back in 2013, the function always processes only a single request and the loop is terminated at the end of the first iteration. This means the counter reaches only value 1 and any excess number of login requests is never rejected.

Quoted 12 lines

Fix the problem by introducing iscsi_login.negotiation_exchanges counter and update the logic to count exchanges per each login phase as described in RFC 7143:

Quoted 8 lines

6.2. Text Mode Negotiation:
[...]
In the Login Phase (see Section 6.3), every stage is a separate negotiation. [...]
[...]
An iSCSI initiator or target MAY terminate a negotiation that does not terminate within an implementation-specific reasonable time or number of exchanges but SHOULD allow at least six (6) exchanges.

Quoted 6 lines

It wasn't clear to me what this fixes. Today, are initiators sending more than 6 exchanges and if so what happens to the target? Is it crashing or annoying to user or cause some sort of endless login so we run out of resources? Or is this more of code cleanup?

When does this happen and with what initiators?

This issue is only something that I noticed while reading through the target code because of some different problem. In that sense, the patch is more a code cleanup. My tests to verify the patch were also artificial (attached).

I have now additionally tried some simple examples with sending extensive number of Login requests in a loop to the target and did not observe any immediate problem with running out of resources. A possible alternative might be therefore to remove this logic, not sure.

Thanks,
Petr

#!/usr/bin/python3

import socket

# Test excess number of exchanges during security negotiation. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:

    s.connect(('192.168.122.30', 3260))
    send = (
        b'\x43'                      # 00: -> Immediate marker + Login request
        b'\x00'                      # 01: -> Continue security negotiation
        b'\x00'                      # 02: VersionMax
        b'\x00'                      # 03: VersionMin
        b'\x00'                      # 04: TotalAHSLength
        b'\x00\x00\x8c'              # 05: DataSegmentLength
        b'\x0c\xfd\x37\x00\x00\x01'  # 08: ISID -> OUI, SUSE, Qualifier=1
        b'\x00\x00'                  # 14: TSIH
        b'\x00\x00\x00\x00'          # 16: InitiatorTaskTag
        b'\x00\x00'                  # 20: CID
        b'\x00\x00'                  # 22: Reserved
        b'\x00\x00\x00\x00'          # 24: CmdSN
        b'\x00\x00\x00\x00'          # 28: ExpStatSN
        b'\x00\x00\x00\x00'          # 32: Reserved
        b'\x00\x00\x00\x00'          # 36: Reserved
        b'\x00\x00\x00\x00'          # 40: Reserved
        b'\x00\x00\x00\x00'          # 44: Reserved
        # 48: Login parameters
        b'InitiatorName=iqn.1996-04.de.suse:01:6fd07434abf9\x00'
        b'SessionType=Normal\x00'
        b'TargetName=iqn.2003-01.org.linux-iscsi.localhost.x8664:sn.8726b6baa6b9\x00'
        # 188: Padding to 4 bytes
        b''
        # 188:
    )
    for i in range(1, 8):
        print(f"Send #{i}: {send}")
        s.sendall(send)
        data = s.recv(1024)
        print(f"Response: {data}")
        assert data[0] == 0x23  # Opcode
        assert data[36] == (0 if i < 7 else 3)  # Status-Class
        assert data[37] == 0  # Status-Detail

print()

# Test excess number of exchanges during operation parameter negotiation. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:

    s.connect(('192.168.122.30', 3260))
    send = (
        b'\x43'                      # 00: -> Immediate marker + Login request
        b'\x81'                      # 01: -> Transfer to operational parameter negotiation
        b'\x00'                      # 02: VersionMax
        b'\x00'                      # 03: VersionMin
        b'\x00'                      # 04: TotalAHSLength
        b'\x00\x00\x9c'              # 05: DataSegmentLength
        b'\x0c\xfd\x37\x00\x00\x01'  # 08: ISID -> OUI, SUSE, Qualifier=1
        b'\x00\x00'                  # 14: TSIH
        b'\x00\x00\x00\x00'          # 16: InitiatorTaskTag
        b'\x00\x00'                  # 20: CID
        b'\x00\x00'                  # 22: Reserved
        b'\x00\x00\x00\x00'          # 24: CmdSN
        b'\x00\x00\x00\x00'          # 28: ExpStatSN
        b'\x00\x00\x00\x00'          # 32: Reserved
        b'\x00\x00\x00\x00'          # 36: Reserved
        b'\x00\x00\x00\x00'          # 40: Reserved
        b'\x00\x00\x00\x00'          # 44: Reserved
        # 48: Login parameters
        b'InitiatorName=iqn.1996-04.de.suse:01:6fd07434abf9\x00'
        b'SessionType=Normal\x00'
        b'TargetName=iqn.2003-01.org.linux-iscsi.localhost.x8664:sn.8726b6baa6b9\x00'
        b'AuthMethod=None\x00'
        # 204: Padding to 4 bytes
        b''
        # 204:
    )
    print(f"Send #1: {send}")
    s.sendall(send)
    data = s.recv(1024)
    print(f"Response: {data}")
    assert data[0] == 0x23  # Opcode
    assert data[1] == 0x81  # -> Transfer to operational parameter negotiation
    assert data[36] == 0  # Status-Class
    assert data[37] == 0  # Status-Detail

    send = (
        b'\x43'                      # 00: -> Immediate marker + Login request
        b'\x04'                      # 01: -> Continue operational parameter negotiation
        b'\x00'                      # 02: VersionMax
        b'\x00'                      # 03: VersionMin
        b'\x00'                      # 04: TotalAHSLength
        b'\x00\x00\x12'              # 05: DataSegmentLength
        b'\x0c\xfd\x37\x00\x00\x01'  # 08: ISID -> OUI, SUSE, Qualifier=1
        b'\x00\x00'                  # 14: TSIH
        b'\x00\x00\x00\x00'          # 16: InitiatorTaskTag
        b'\x00\x00'                  # 20: CID
        b'\x00\x00'                  # 22: Reserved
        b'\x00\x00\x00\x00'          # 24: CmdSN
        b'\x00\x00\x00\x00'          # 28: ExpStatSN
        b'\x00\x00\x00\x00'          # 32: Reserved
        b'\x00\x00\x00\x00'          # 36: Reserved
        b'\x00\x00\x00\x00'          # 40: Reserved
        b'\x00\x00\x00\x00'          # 44: Reserved
        # 48: Login parameters
        b'HeaderDigest=None\x00'
        # 66: Padding to 4 bytes
        b'\x00\x00'
        # 68:
    )
    for i in range(2, 9):
        print(f"Send #{i}: {send}")
        s.sendall(send)
        data = s.recv(1024)
        print(f"Response: {data}")
        assert data[0] == 0x23
        assert data[36] == (0 if i < 8 else 3)  # Status-Class
        assert data[37] == 0  # Status-Detail