From 50fb64df91e4068ab8a152f753a00743690de5e2 Mon Sep 17 00:00:00 2001 From: Felipe Franciosi Date: Sun, 30 Sep 2018 11:51:02 +0100 Subject: [PATCH] iscsi_create_context: improve ISID randomness The current random seed for determining a new context's ISID is calculated by XOR'ing time(), getpid() and "iscsi". When invoked from iscsi_reconnect(), all three inputs are likely to be identical, resulting on identical ISIDs. That happens because iscsi_reconnect() malloc()s a temporary "iscsi" which is then free()d at the end of the call. Successive calls to malloc() (from that function) are therefore likely to reuse the same address for the context. When multiple sessions are used for different LUNs of the same target, and reconnects happen within the same second (the precision given by time()), then multiple login attempts will happen with identical values, violating the ISID RULE as described in Section 3.4.3 of RFC3270. This fixes the issue by introducing a sequence number to the ISID seed generation. Signed-off-by: Felipe Franciosi --- lib/init.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/init.c b/lib/init.c index 0bcf1b7..0c8dc7b 100644 --- a/lib/init.c +++ b/lib/init.c @@ -153,6 +153,7 @@ iscsi_create_context(const char *initiator_name) { struct iscsi_context *iscsi; size_t required = ISCSI_RAW_HEADER_SIZE + ISCSI_DIGEST_SIZE; + static uint32_t ctx_seq = 0; char *ca; if (!initiator_name[0]) { @@ -176,7 +177,7 @@ iscsi_create_context(const char *initiator_name) iscsi->fd = -1; - srand(time(NULL) ^ getpid() ^ (uint32_t) ((uintptr_t) iscsi)); + srand(time(NULL) ^ getpid() ^ (uint32_t) ((uintptr_t) iscsi) ^ ctx_seq++); /* initialize to a "random" isid */ iscsi_set_isid_random(iscsi, rand(), 0);