[PATCH] scsi: target: iscsi: match the login keys exactly in iscsi_target_locate_portal()
From: Yehyeong Lee
Date: Sat Nov 14 2026 - 09:13:27 EST
iscsi_target_locate_portal() scans the first Login Request for
InitiatorName, SessionType and TargetName, and the values it picks decide
which portal group the connection is attached to and which node ACL
authorises it. It picks them differently from the parameter list that the
rest of the login uses:
- the key is matched with strncmp() over the key's own length, so
"SessionTypeX" is taken for "SessionType", while
iscsi_find_param_from_key() matches with strcmp() and answers
"SessionTypeX" with NotUnderstood;
- the loop assigns on every match, so a repeated key leaves the last
occurrence, while iscsi_check_key() rejects the second occurrence and
leaves the first value in the parameter list.
Either difference lets an initiator make the two disagree about
SessionType. iscsi_target_locate_portal() takes the discovery path, which
attaches the connection to iscsit_global->discovery_tpg and jumps to
alloc_tags, so core_tpg_check_initiator_node_acl() never runs and
sess->se_sess->se_node_acl stays NULL, while iscsi_post_login_handler()
applies the parameter list on top of that decision:
iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
iscsi_release_param_list(conn->param_list);
conn->param_list = NULL;
iscsit_determine_maxcmdsn(sess);
SessionType therefore goes back to 0, and iscsit_determine_maxcmdsn() stops
treating the session as a discovery session and dereferences the NULL node
ACL:
if (sess->sess_ops->SessionType)
return;
se_nacl = sess->se_sess->se_node_acl;
sess->cmdsn_window = se_nacl->queue_depth;
A first Login Request carrying
InitiatorName=iqn.2003-01.org.example:init
TargetName=iqn.2003-01.org.example:tgt
SessionType=Normal
[operational keys]
SessionTypeX=Discovery
or the same login with SessionType declared twice, as Normal and then
Discovery, faults on a QEMU guest:
Unable to locate key "SessionTypeX".
Oops: general protection fault, probably for non-canonical address 0xdffffc000000001c: 0000 [#1] SMP KASAN NOPTI
KASAN: null-ptr-deref in range [0x00000000000000e0-0x00000000000000e7]
CPU: 2 UID: 0 PID: 128 Comm: iscsi_np Not tainted 7.4.0-rc2-00563-g97d73c825122 #1 PREEMPT(lazy)
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
RIP: 0010:iscsit_determine_maxcmdsn+0xd4/0x170
Code: 48 c1 ea 03 80 3c 02 00 0f 85 a2 00 00 00 48 b8 00 00 00 00 00 fc ff df 48 8b 6d 10 48 8d bd e4 00 00 00 48 89 fa 48 c1 ea 03 <0f> b6 14 02 48 89 f8 83 e0 07 83 c0 03 38 d0 7c 04 84 d2 75 7b 48
RSP: 0018:ffffc9000087fcd8 EFLAGS: 00010203
RAX: dffffc0000000000 RBX: ffff88800eb20000 RCX: 0000000000000000
RDX: 000000000000001c RSI: 0000000000000000 RDI: 00000000000000e4
RBP: 0000000000000000 R08: 0000000000000001 R09: 0000000000000000
R10: 0000000000000001 R11: 0000000000008000 R12: ffff88800eb20468
R13: ffff88800e9fdd88 R14: ffff88800ebd4000 R15: ffff88800eb20000
FS: 0000000000000000(0000) GS:ffff8880e5a9d000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00000000005a1b6b CR3: 000000000588e004 CR4: 0000000000770ef0
PKRU: 55555554
Call Trace:
<TASK>
iscsi_post_login_handler+0x1ee/0xc90
iscsi_target_login_thread+0x1e10/0x3290
kthread+0x2ce/0x3b0
ret_from_fork+0x52e/0x780
ret_from_fork_asm+0x1a/0x30
</TASK>
Modules linked in:
---[ end trace 0000000000000000 ]---
The fault is taken by the portal's iscsi_np login thread, which does not
come back, so the target stops accepting logins on that portal until it is
restarted. No credentials are needed: iscsit_load_discovery_tpg() clears
tpg_attrib.authentication on the discovery portal group, so the crafted
login is accepted on a target whose normal portal group requires CHAP and
which answers the same login with AUTH_FAILED when SessionType is declared
only once. The global discovery_auth and enforce_discovery_auth
attributes do close that door, but they are 0 by default.
InitiatorName behaves the same way: with the name declared twice, the node
ACL that authorises the session is the one named last, while
sess->sess_ops->InitiatorName keeps the one named first. On a target with
an ACL for each name and LUN 1 mapped in the first name's ACL only, such a
session is refused LUN 1 with NON_EXISTENT_LUN naming the second name, is
listed under the second name's ACL as having the first name, and reinstates
- drops - the first name's session with the same ISID.
Match the keys with strcmp() and keep the first occurrence of each, which
is what the parameter list ends up holding, and compare the SessionType
value with strcmp() as iscsi_check_string_or_list_value() does.
Fixes: e48354ce078c ("iscsi-target: Add iSCSI fabric support for target v4.1")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Yehyeong Lee <yhlee@xxxxxxxxxxxxxxxxxx>
---
drivers/target/iscsi/iscsi_target_nego.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/drivers/target/iscsi/iscsi_target_nego.c b/drivers/target/iscsi/iscsi_target_nego.c
index b03ed154ca34e9..619a843e3c849c 100644
--- a/drivers/target/iscsi/iscsi_target_nego.c
+++ b/drivers/target/iscsi/iscsi_target_nego.c
@@ -1151,11 +1151,15 @@ int iscsi_target_locate_portal(
goto out;
}
- if (!strncmp(key, "InitiatorName", 13))
+ /*
+ * Match keys exactly and keep the first occurrence, as
+ * iscsi_decode_text_input() does for the parameter list.
+ */
+ if (!i_buf && !strcmp(key, INITIATORNAME))
i_buf = value;
- else if (!strncmp(key, "SessionType", 11))
+ else if (!s_buf && !strcmp(key, SESSIONTYPE))
s_buf = value;
- else if (!strncmp(key, "TargetName", 10))
+ else if (!t_buf && !strcmp(key, TARGETNAME))
t_buf = value;
start += strlen(key) + strlen(value) + 2;
@@ -1193,7 +1197,7 @@ int iscsi_target_locate_portal(
/*
* Use default portal group for discovery sessions.
*/
- sessiontype = strncmp(s_buf, DISCOVERY, 9);
+ sessiontype = strcmp(s_buf, DISCOVERY);
if (!sessiontype) {
if (!login->leading_connection)
goto get_target;
--
2.43.0