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