Skip to content

Commit 2a737d3

Browse files
maurizio-lombardimartinkpetersen
authored andcommitted
scsi: target: iscsi: Prevent login threads from racing between each other
The tpg->np_login_sem is a semaphore that is used to serialize the login process when multiple login threads run concurrently against the same target portal group. The iscsi_target_locate_portal() function finds the tpg, calls iscsit_access_np() against the np_login_sem semaphore and saves the tpg pointer in conn->tpg; If iscsi_target_locate_portal() fails, the caller will check for the conn->tpg pointer and, if it's not NULL, then it will assume that iscsi_target_locate_portal() called iscsit_access_np() on the semaphore. Make sure that conn->tpg gets initialized only if iscsit_access_np() was successful, otherwise iscsit_deaccess_np() may end up being called against a semaphore we never took, allowing more than one thread to access the same tpg. Signed-off-by: Maurizio Lombardi <mlombard@redhat.com> Link: https://lore.kernel.org/r/20230508162219.1731964-4-mlombard@redhat.com Reviewed-by: Mike Christie <michael.christie@oracle.com> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
1 parent 98a8c2b commit 2a737d3

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

drivers/target/iscsi/iscsi_target_nego.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,7 @@ int iscsi_target_locate_portal(
11221122
iscsi_target_set_sock_callbacks(conn);
11231123

11241124
login->np = np;
1125+
conn->tpg = NULL;
11251126

11261127
login_req = (struct iscsi_login_req *) login->req;
11271128
payload_length = ntoh24(login_req->dlength);
@@ -1189,7 +1190,6 @@ int iscsi_target_locate_portal(
11891190
*/
11901191
sessiontype = strncmp(s_buf, DISCOVERY, 9);
11911192
if (!sessiontype) {
1192-
conn->tpg = iscsit_global->discovery_tpg;
11931193
if (!login->leading_connection)
11941194
goto get_target;
11951195

@@ -1206,9 +1206,11 @@ int iscsi_target_locate_portal(
12061206
* Serialize access across the discovery struct iscsi_portal_group to
12071207
* process login attempt.
12081208
*/
1209+
conn->tpg = iscsit_global->discovery_tpg;
12091210
if (iscsit_access_np(np, conn->tpg) < 0) {
12101211
iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
12111212
ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1213+
conn->tpg = NULL;
12121214
ret = -1;
12131215
goto out;
12141216
}

0 commit comments

Comments
 (0)