Blame SOURCES/0028-iscsiadm-Correctly-check-for-invalid-hostno-and-flas.patch

786c6d
From 3256b93ee3025bf76757001ff3d24914c4c4af28 Mon Sep 17 00:00:00 2001
786c6d
From: Adheer Chandravanshi <adheer.chandravanshi@qlogic.com>
786c6d
Date: Tue, 9 Jul 2013 08:17:14 -0400
786c6d
Subject: [PATCH] iscsiadm: Correctly check for invalid hostno and flashnode
786c6d
 index
786c6d
786c6d
In host mode, correctly compare for invalid hostno and flashnode index.
786c6d
786c6d
Signed-off-by: Adheer Chandravanshi <adheer.chandravanshi@qlogic.com>
786c6d
Signed-off-by: Vikas Chaudhary <vikas.chaudhary@qlogic.com>
786c6d
---
786c6d
 usr/flashnode.h |  2 ++
786c6d
 usr/host.h      |  2 ++
786c6d
 usr/iscsiadm.c  | 48 ++++++++++++++++++++++++++++++------------------
786c6d
 usr/types.h     |  1 +
786c6d
 4 files changed, 35 insertions(+), 18 deletions(-)
786c6d
786c6d
diff --git a/usr/flashnode.h b/usr/flashnode.h
786c6d
index c1de9cc..2950fb5 100644
786c6d
--- a/usr/flashnode.h
786c6d
+++ b/usr/flashnode.h
786c6d
@@ -26,6 +26,8 @@
786c6d
 #include "config.h"
786c6d
 #include "auth.h"
786c6d
 
786c6d
+#define MAX_FLASHNODE_IDX UINT_MAX
786c6d
+
786c6d
 typedef enum portal_type {
786c6d
 	IPV4,
786c6d
 	IPV6,
786c6d
diff --git a/usr/host.h b/usr/host.h
786c6d
index 894ab91..db44cfa 100644
786c6d
--- a/usr/host.h
786c6d
+++ b/usr/host.h
786c6d
@@ -5,6 +5,8 @@
786c6d
 #include "types.h"
786c6d
 #include "config.h"
786c6d
 
786c6d
+#define MAX_HOST_NO UINT_MAX
786c6d
+
786c6d
 #define MAX_CHAP_BUF_SZ 4096
786c6d
 #define REQ_CHAP_BUF_SZ (MAX_CHAP_BUF_SZ + sizeof(struct iscsi_uevent))
786c6d
 
786c6d
diff --git a/usr/iscsiadm.c b/usr/iscsiadm.c
786c6d
index da0a3ec..c7337ae 100644
786c6d
--- a/usr/iscsiadm.c
786c6d
+++ b/usr/iscsiadm.c
786c6d
@@ -1744,20 +1744,22 @@ exit_logout_sid:
786c6d
 }
786c6d
 
786c6d
 static int exec_flashnode_op(int op, int info_level, uint32_t host_no,
786c6d
-			     uint32_t flashnode_idx, int type,
786c6d
+			     uint64_t fnode_idx, int type,
786c6d
 			     struct list_head *params)
786c6d
 {
786c6d
 	struct iscsi_transport *t = NULL;
786c6d
 	int rc = ISCSI_SUCCESS;
786c6d
 	char *portal_type;
786c6d
+	uint32_t flashnode_idx;
786c6d
 
786c6d
 	if (op != OP_SHOW && op != OP_NOOP && op != OP_NEW &&
786c6d
-	    flashnode_idx == 0xffffffff) {
786c6d
+	    fnode_idx > MAX_FLASHNODE_IDX) {
786c6d
 		log_error("Invalid flashnode index");
786c6d
 		rc = ISCSI_ERR_INVAL;
786c6d
 		goto exit_flashnode_op;
786c6d
 	}
786c6d
 
786c6d
+	flashnode_idx = (uint32_t)fnode_idx;
786c6d
 	t = iscsi_sysfs_get_transport_by_hba(host_no);
786c6d
 	if (!t) {
786c6d
 		log_error("Could not match hostno %u to transport.", host_no);
786c6d
@@ -1768,7 +1770,7 @@ static int exec_flashnode_op(int op, int info_level, uint32_t host_no,
786c6d
 	switch (op) {
786c6d
 	case OP_NOOP:
786c6d
 	case OP_SHOW:
786c6d
-		if (flashnode_idx == 0xffffffff)
786c6d
+		if (fnode_idx > MAX_FLASHNODE_IDX)
786c6d
 			rc = list_flashnodes(info_level, host_no);
786c6d
 		else
786c6d
 			rc = get_flashnode_info(host_no, flashnode_idx);
786c6d
@@ -1880,7 +1882,7 @@ static int verify_iface_params(struct list_head *params, struct node_rec *rec)
786c6d
 
786c6d
 /* TODO: merge iter helpers and clean them up, so we can use them here */
786c6d
 static int exec_iface_op(int op, int do_show, int info_level,
786c6d
-			 struct iface_rec *iface, uint32_t host_no,
786c6d
+			 struct iface_rec *iface, uint64_t host_no,
786c6d
 			 struct list_head *params)
786c6d
 {
786c6d
 	struct host_info hinfo;
786c6d
@@ -2001,9 +2003,9 @@ update_fail:
786c6d
 		printf("%s applied.\n", iface->name);
786c6d
 		break;
786c6d
 	case OP_APPLY_ALL:
786c6d
-		if (host_no == -1) {
786c6d
-			log_error("Applyall requires a host number or MAC "
786c6d
-				  "passed in with the --host argument.");
786c6d
+		if (host_no > MAX_HOST_NO) {
786c6d
+			log_error("Applyall requires a valid host number or MAC"
786c6d
+				  " passed in with the --host argument.");
786c6d
 			rc = ISCSI_ERR_INVAL;
786c6d
 			break;
786c6d
 		}
786c6d
@@ -2014,7 +2016,7 @@ update_fail:
786c6d
 		memset(&hinfo, 0, sizeof(struct host_info));
786c6d
 		hinfo.host_no = host_no;
786c6d
 		if (iscsi_sysfs_get_hostinfo_by_host_no(&hinfo)) {
786c6d
-			log_error("Could not match host%u to ifaces.", host_no);
786c6d
+			log_error("Could not match host%lu to ifaces.", host_no);
786c6d
 			rc = ISCSI_ERR_INVAL;
786c6d
 			break;
786c6d
 		}
786c6d
@@ -2025,7 +2027,7 @@ update_fail:
786c6d
 			break;
786c6d
 		}
786c6d
 
786c6d
-		printf("Applied settings to ifaces attached to host%u.\n",
786c6d
+		printf("Applied settings to ifaces attached to host%lu.\n",
786c6d
 		       host_no);
786c6d
 		break;
786c6d
 	default:
786c6d
@@ -2637,10 +2639,10 @@ done:
786c6d
 	return rc;
786c6d
 }
786c6d
 
786c6d
-static uint32_t parse_host_info(char *optarg, int *rc)
786c6d
+static uint64_t parse_host_info(char *optarg, int *rc)
786c6d
 {
786c6d
 	int err = 0;
786c6d
-	uint32_t host_no = -1;
786c6d
+	uint64_t host_no;
786c6d
 
786c6d
 	*rc = 0;
786c6d
 	if (strstr(optarg, ":")) {
786c6d
@@ -2653,8 +2655,11 @@ static uint32_t parse_host_info(char *optarg, int *rc)
786c6d
 			*rc = ISCSI_ERR_INVAL;
786c6d
 		}
786c6d
 	} else {
786c6d
-		host_no = strtoul(optarg, NULL, 10);
786c6d
-		if (errno) {
786c6d
+		host_no = strtoull(optarg, NULL, 10);
786c6d
+		if (errno || (host_no > MAX_HOST_NO)) {
786c6d
+			if (host_no > MAX_HOST_NO)
786c6d
+				errno = ERANGE;
786c6d
+
786c6d
 			log_error("Invalid host no %s. %s.",
786c6d
 				  optarg, strerror(errno));
786c6d
 			*rc = ISCSI_ERR_INVAL;
786c6d
@@ -2806,13 +2811,14 @@ main(int argc, char **argv)
786c6d
 	int tpgt = PORTAL_GROUP_TAG_UNKNOWN, killiscsid=-1, do_show=0;
786c6d
 	int packet_size=32, ping_count=1, ping_interval=0;
786c6d
 	int do_discover = 0, sub_mode = -1;
786c6d
-	int flashnode_idx = -1, portal_type = -1;
786c6d
+	int portal_type = -1;
786c6d
 	struct sigaction sa_old;
786c6d
 	struct sigaction sa_new;
786c6d
 	struct list_head ifaces;
786c6d
 	struct iface_rec *iface = NULL, *tmp;
786c6d
 	struct node_rec *rec = NULL;
786c6d
-	uint32_t host_no = -1;
786c6d
+	uint64_t host_no =  (uint64_t)MAX_HOST_NO + 1;
786c6d
+	uint64_t flashnode_idx = (uint64_t)MAX_FLASHNODE_IDX + 1;
786c6d
 	struct user_param *param;
786c6d
 	struct list_head params;
786c6d
 
786c6d
@@ -2956,7 +2962,13 @@ main(int argc, char **argv)
786c6d
 				ISCSI_VERSION_STR);
786c6d
 			return 0;
786c6d
 		case 'x':
786c6d
-			flashnode_idx = atoi(optarg);
786c6d
+			flashnode_idx = strtoull(optarg, NULL, 10);
786c6d
+			if (errno) {
786c6d
+				log_error("Invalid flashnode index %s. %s.",
786c6d
+					  optarg, strerror(errno));
786c6d
+				rc = ISCSI_ERR_INVAL;
786c6d
+				goto free_ifaces;
786c6d
+			}
786c6d
 			break;
786c6d
 		case 'A':
786c6d
 			portal_type = str_to_portal_type(optarg);
786c6d
@@ -3022,7 +3034,7 @@ main(int argc, char **argv)
786c6d
 		if (sub_mode != -1) {
786c6d
 			switch (sub_mode) {
786c6d
 			case MODE_CHAP:
786c6d
-				if (!op || !host_no) {
786c6d
+				if (!op || (host_no > MAX_HOST_NO)) {
786c6d
 					log_error("CHAP mode requires host "
786c6d
 						"no and valid operation");
786c6d
 					rc = ISCSI_ERR_INVAL;
786c6d
@@ -3032,7 +3044,7 @@ main(int argc, char **argv)
786c6d
 						       value);
786c6d
 				break;
786c6d
 			case MODE_FLASHNODE:
786c6d
-				if (!host_no) {
786c6d
+				if (host_no > MAX_HOST_NO) {
786c6d
 					log_error("FLASHNODE mode requires host no");
786c6d
 					rc = ISCSI_ERR_INVAL;
786c6d
 					break;
786c6d
diff --git a/usr/types.h b/usr/types.h
786c6d
index 77e3f97..9d9ba86 100644
786c6d
--- a/usr/types.h
786c6d
+++ b/usr/types.h
786c6d
@@ -10,6 +10,7 @@
786c6d
 #include <netinet/in.h>
786c6d
 #include <stdint.h>
786c6d
 #include <sys/types.h>
786c6d
+#include <limits.h>
786c6d
 
786c6d
 /*
786c6d
  * using the __be types allows stricter static
786c6d
-- 
786c6d
1.8.3.1
786c6d