Skip to content

Commit ae302d6

Browse files
Mike Christiegregkh
authored andcommitted
iscsi target: fix session creation failure handling
commit 26abc91 upstream. The problem is that iscsi_login_zero_tsih_s1 sets conn->sess early in iscsi_login_set_conn_values. If the function fails later like when we alloc the idr it does kfree(sess) and leaves the conn->sess pointer set. iscsi_login_zero_tsih_s1 then returns -Exyz and we then call iscsi_target_login_sess_out and access the freed memory. This patch has iscsi_login_zero_tsih_s1 either completely setup the session or completely tear it down, so later in iscsi_target_login_sess_out we can just check for it being set to the connection. Cc: stable@vger.kernel.org Fixes: 0957627 ("iscsi-target: Fix sess allocation leak in...") Signed-off-by: Mike Christie <mchristi@redhat.com> Acked-by: Martin K. Petersen <martin.petersen@oracle.com> Signed-off-by: Matthew Wilcox <willy@infradead.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 5b55b24 commit ae302d6

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

drivers/target/iscsi/iscsi_target_login.c

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,7 @@ static int iscsi_login_zero_tsih_s1(
345345
pr_err("idr_alloc() for sess_idr failed\n");
346346
iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
347347
ISCSI_LOGIN_STATUS_NO_RESOURCES);
348-
kfree(sess);
349-
return -ENOMEM;
348+
goto free_sess;
350349
}
351350

352351
sess->creation_time = get_jiffies_64();
@@ -362,20 +361,28 @@ static int iscsi_login_zero_tsih_s1(
362361
ISCSI_LOGIN_STATUS_NO_RESOURCES);
363362
pr_err("Unable to allocate memory for"
364363
" struct iscsi_sess_ops.\n");
365-
kfree(sess);
366-
return -ENOMEM;
364+
goto remove_idr;
367365
}
368366

369367
sess->se_sess = transport_init_session(TARGET_PROT_NORMAL);
370368
if (IS_ERR(sess->se_sess)) {
371369
iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
372370
ISCSI_LOGIN_STATUS_NO_RESOURCES);
373-
kfree(sess->sess_ops);
374-
kfree(sess);
375-
return -ENOMEM;
371+
goto free_ops;
376372
}
377373

378374
return 0;
375+
376+
free_ops:
377+
kfree(sess->sess_ops);
378+
remove_idr:
379+
spin_lock_bh(&sess_idr_lock);
380+
idr_remove(&sess_idr, sess->session_index);
381+
spin_unlock_bh(&sess_idr_lock);
382+
free_sess:
383+
kfree(sess);
384+
conn->sess = NULL;
385+
return -ENOMEM;
379386
}
380387

381388
static int iscsi_login_zero_tsih_s2(
@@ -1162,13 +1169,13 @@ void iscsi_target_login_sess_out(struct iscsi_conn *conn,
11621169
ISCSI_LOGIN_STATUS_INIT_ERR);
11631170
if (!zero_tsih || !conn->sess)
11641171
goto old_sess_out;
1165-
if (conn->sess->se_sess)
1166-
transport_free_session(conn->sess->se_sess);
1167-
if (conn->sess->session_index != 0) {
1168-
spin_lock_bh(&sess_idr_lock);
1169-
idr_remove(&sess_idr, conn->sess->session_index);
1170-
spin_unlock_bh(&sess_idr_lock);
1171-
}
1172+
1173+
transport_free_session(conn->sess->se_sess);
1174+
1175+
spin_lock_bh(&sess_idr_lock);
1176+
idr_remove(&sess_idr, conn->sess->session_index);
1177+
spin_unlock_bh(&sess_idr_lock);
1178+
11721179
kfree(conn->sess->sess_ops);
11731180
kfree(conn->sess);
11741181
conn->sess = NULL;

0 commit comments

Comments
 (0)