From a45094b7a7684cd0f859ea0707bfbd8d366a7937 Mon Sep 17 00:00:00 2001 From: Peter Lieven Date: Thu, 26 Mar 2015 10:07:52 +0100 Subject: [PATCH] init: fix segfaul in iscsi_parse_url We allowed iscsi to be NULL in iscsi_parse_url. Especially qemu does this and currently segfaults at start. Change the usage guidelines for target username/password to be the same as for chap username/password. Signed-off-by: Peter Lieven --- include/iscsi.h | 2 ++ lib/connect.c | 8 ++------ lib/init.c | 43 +++++++++++++++++++------------------------ 3 files changed, 23 insertions(+), 30 deletions(-) diff --git a/include/iscsi.h b/include/iscsi.h index cf3309e..7fff12c 100644 --- a/include/iscsi.h +++ b/include/iscsi.h @@ -91,6 +91,8 @@ struct iscsi_url { char target[MAX_STRING_SIZE + 1]; char user[MAX_STRING_SIZE + 1]; char passwd[MAX_STRING_SIZE + 1]; + char target_user[MAX_STRING_SIZE + 1]; + char target_passwd[MAX_STRING_SIZE + 1]; int lun; struct iscsi_context *iscsi; }; diff --git a/lib/connect.c b/lib/connect.c index 308e906..837e4c9 100644 --- a/lib/connect.c +++ b/lib/connect.c @@ -282,12 +282,8 @@ try_again: iscsi_set_header_digest(iscsi, old_iscsi->want_header_digest); - if (old_iscsi->user[0]) { - iscsi_set_initiator_username_pwd(iscsi, old_iscsi->user, old_iscsi->passwd); - } - if (old_iscsi->target_user[0]) { - iscsi_set_target_username_pwd(iscsi, old_iscsi->target_user, old_iscsi->target_passwd); - } + iscsi_set_initiator_username_pwd(iscsi, old_iscsi->user, old_iscsi->passwd); + iscsi_set_target_username_pwd(iscsi, old_iscsi->target_user, old_iscsi->target_passwd); iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL); diff --git a/lib/init.c b/lib/init.c index afe88e7..20bfb86 100644 --- a/lib/init.c +++ b/lib/init.c @@ -453,6 +453,8 @@ iscsi_parse_url(struct iscsi_context *iscsi, const char *url, int full) char *portal; char *user = NULL; char *passwd = NULL; + char *target_user = NULL; + char *target_passwd = NULL; char *target = NULL; char *lun; char *tmp; @@ -474,12 +476,10 @@ iscsi_parse_url(struct iscsi_context *iscsi, const char *url, int full) strncpy(str,url + 8, MAX_STRING_SIZE); portal = str; - iscsi_set_target_username_pwd(iscsi, - getenv("LIBISCSI_CHAP_TARGET_USERNAME"), - getenv("LIBISCSI_CHAP_TARGET_PASSWORD")); - - user = getenv("LIBISCSI_CHAP_USERNAME"); - passwd = getenv("LIBISCSI_CHAP_PASSWORD"); + user = getenv("LIBISCSI_CHAP_USERNAME"); + passwd = getenv("LIBISCSI_CHAP_PASSWORD"); + target_user = getenv("LIBISCSI_CHAP_TARGET_USERNAME"); + target_passwd = getenv("LIBISCSI_CHAP_TARGET_PASSWORD"); tmp = strchr(portal, '?'); if (tmp) { @@ -487,7 +487,6 @@ iscsi_parse_url(struct iscsi_context *iscsi, const char *url, int full) while (tmp && *tmp) { char *next = strchr(tmp, '&'); char *key, *value; - if (next != NULL) { *next++ = 0; } @@ -497,15 +496,9 @@ iscsi_parse_url(struct iscsi_context *iscsi, const char *url, int full) *value++ = 0; } if (!strcmp(key, "target_user")) { - if (value) { - strncpy(iscsi->target_user, - value, MAX_STRING_SIZE); - } + target_user = value; } else if (!strcmp(key, "target_password")) { - if (value) { - strncpy(iscsi->target_passwd, - value, MAX_STRING_SIZE); - } + target_passwd = value; } tmp = next; } @@ -587,19 +580,16 @@ iscsi_parse_url(struct iscsi_context *iscsi, const char *url, int full) strncpy(iscsi_url->portal,portal,MAX_STRING_SIZE); - if (!iscsi->target_user[0] || !iscsi->target_passwd[0]) { - iscsi->target_user[0] = 0; - iscsi->target_passwd[0] = 0; - } - if (user != NULL && passwd != NULL) { + if (user && passwd && user[0] && passwd[0]) { strncpy(iscsi_url->user, user, MAX_STRING_SIZE); strncpy(iscsi_url->passwd, passwd, MAX_STRING_SIZE); - } else { /* if we do not have normal CHAP, that means we do not have * bidirectional either. */ - iscsi->target_user[0] = 0; - iscsi->target_passwd[0] = 0; + if (target_user && target_passwd && target_user[0] && target_passwd[0]) { + strncpy(iscsi_url->target_user, target_user, MAX_STRING_SIZE); + strncpy(iscsi_url->target_passwd, target_passwd, MAX_STRING_SIZE); + } } if (full) { @@ -640,6 +630,11 @@ int iscsi_set_initiator_username_pwd(struct iscsi_context *iscsi, const char *user, const char *passwd) { + if (!user || !passwd || !user[0] || !passwd[0]) { + iscsi->user[0] = 0; + iscsi->passwd[0] = 0; + return 0; + } strncpy(iscsi->user,user,MAX_STRING_SIZE); strncpy(iscsi->passwd,passwd,MAX_STRING_SIZE); return 0; @@ -650,7 +645,7 @@ int iscsi_set_target_username_pwd(struct iscsi_context *iscsi, const char *user, const char *passwd) { - if (!user || !passwd) { + if (!user || !passwd || !user[0] || !passwd[0]) { iscsi->target_user[0] = 0; iscsi->target_passwd[0] = 0; return 0;