Skip to content

Commit 6787ab8

Browse files
zx2c4tytso
authored andcommitted
iscsi: ensure RNG is seeded before use
It's not safe to use weak random data here, especially for the challenge response randomness. Since we're always in process context, it's safe to simply wait until we have enough randomness to carry out the authentication correctly. While we're at it, we clean up a small memleak during an error condition. Signed-off-by: Jason A. Donenfeld <[email protected]> Cc: "Nicholas A. Bellinger" <[email protected]> Cc: Lee Duncan <[email protected]> Cc: Chris Leech <[email protected]> Signed-off-by: Theodore Ts'o <[email protected]>
1 parent 51b0817 commit 6787ab8

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

drivers/target/iscsi/iscsi_target_auth.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,21 @@ static void chap_binaryhex_to_asciihex(char *dst, char *src, int src_len)
4747
}
4848
}
4949

50-
static void chap_gen_challenge(
50+
static int chap_gen_challenge(
5151
struct iscsi_conn *conn,
5252
int caller,
5353
char *c_str,
5454
unsigned int *c_len)
5555
{
56+
int ret;
5657
unsigned char challenge_asciihex[CHAP_CHALLENGE_LENGTH * 2 + 1];
5758
struct iscsi_chap *chap = conn->auth_protocol;
5859

5960
memset(challenge_asciihex, 0, CHAP_CHALLENGE_LENGTH * 2 + 1);
6061

61-
get_random_bytes(chap->challenge, CHAP_CHALLENGE_LENGTH);
62+
ret = get_random_bytes_wait(chap->challenge, CHAP_CHALLENGE_LENGTH);
63+
if (unlikely(ret))
64+
return ret;
6265
chap_binaryhex_to_asciihex(challenge_asciihex, chap->challenge,
6366
CHAP_CHALLENGE_LENGTH);
6467
/*
@@ -69,6 +72,7 @@ static void chap_gen_challenge(
6972

7073
pr_debug("[%s] Sending CHAP_C=0x%s\n\n", (caller) ? "server" : "client",
7174
challenge_asciihex);
75+
return 0;
7276
}
7377

7478
static int chap_check_algorithm(const char *a_str)
@@ -143,6 +147,7 @@ static struct iscsi_chap *chap_server_open(
143147
case CHAP_DIGEST_UNKNOWN:
144148
default:
145149
pr_err("Unsupported CHAP_A value\n");
150+
kfree(conn->auth_protocol);
146151
return NULL;
147152
}
148153

@@ -156,7 +161,10 @@ static struct iscsi_chap *chap_server_open(
156161
/*
157162
* Generate Challenge.
158163
*/
159-
chap_gen_challenge(conn, 1, aic_str, aic_len);
164+
if (chap_gen_challenge(conn, 1, aic_str, aic_len) < 0) {
165+
kfree(conn->auth_protocol);
166+
return NULL;
167+
}
160168

161169
return chap;
162170
}

drivers/target/iscsi/iscsi_target_login.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,22 +245,26 @@ int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
245245
return 0;
246246
}
247247

248-
static void iscsi_login_set_conn_values(
248+
static int iscsi_login_set_conn_values(
249249
struct iscsi_session *sess,
250250
struct iscsi_conn *conn,
251251
__be16 cid)
252252
{
253+
int ret;
253254
conn->sess = sess;
254255
conn->cid = be16_to_cpu(cid);
255256
/*
256257
* Generate a random Status sequence number (statsn) for the new
257258
* iSCSI connection.
258259
*/
259-
get_random_bytes(&conn->stat_sn, sizeof(u32));
260+
ret = get_random_bytes_wait(&conn->stat_sn, sizeof(u32));
261+
if (unlikely(ret))
262+
return ret;
260263

261264
mutex_lock(&auth_id_lock);
262265
conn->auth_id = iscsit_global->auth_id++;
263266
mutex_unlock(&auth_id_lock);
267+
return 0;
264268
}
265269

266270
__printf(2, 3) int iscsi_change_param_sprintf(
@@ -306,7 +310,11 @@ static int iscsi_login_zero_tsih_s1(
306310
return -ENOMEM;
307311
}
308312

309-
iscsi_login_set_conn_values(sess, conn, pdu->cid);
313+
ret = iscsi_login_set_conn_values(sess, conn, pdu->cid);
314+
if (unlikely(ret)) {
315+
kfree(sess);
316+
return ret;
317+
}
310318
sess->init_task_tag = pdu->itt;
311319
memcpy(&sess->isid, pdu->isid, 6);
312320
sess->exp_cmd_sn = be32_to_cpu(pdu->cmdsn);
@@ -497,8 +505,7 @@ static int iscsi_login_non_zero_tsih_s1(
497505
{
498506
struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
499507

500-
iscsi_login_set_conn_values(NULL, conn, pdu->cid);
501-
return 0;
508+
return iscsi_login_set_conn_values(NULL, conn, pdu->cid);
502509
}
503510

504511
/*
@@ -554,9 +561,8 @@ static int iscsi_login_non_zero_tsih_s2(
554561
atomic_set(&sess->session_continuation, 1);
555562
spin_unlock_bh(&sess->conn_lock);
556563

557-
iscsi_login_set_conn_values(sess, conn, pdu->cid);
558-
559-
if (iscsi_copy_param_list(&conn->param_list,
564+
if (iscsi_login_set_conn_values(sess, conn, pdu->cid) < 0 ||
565+
iscsi_copy_param_list(&conn->param_list,
560566
conn->tpg->param_list, 0) < 0) {
561567
iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
562568
ISCSI_LOGIN_STATUS_NO_RESOURCES);

0 commit comments

Comments
 (0)