Olaf Hering 29f3cf
From: Vitaly Kuznetsov <vkuznets@redhat.com>
Olaf Hering 29f3cf
Date: Sun, 30 Apr 2017 16:21:15 -0700
Olaf Hering 29f3cf
Patch-mainline: v4.13-rc1
Olaf Hering 29f3cf
Subject: tools: hv: properly handle long paths
Olaf Hering 29f3cf
Git-commit: a1a7ea6bd6d55b6620c9c0a43bf684d6c427172a
Olaf Hering 29f3cf
References: fate#323887
Olaf Hering 29f3cf
Olaf Hering 29f3cf
Paths can be up to PATH_MAX long and PATH_MAX is usually greater than 256.
Olaf Hering 29f3cf
While on it, simplify path reconstruction to a simple snprintf(), define
Olaf Hering 29f3cf
and reuse KVP_NET_DIR.
Olaf Hering 29f3cf
Olaf Hering 29f3cf
Suggested-by: Tomas Hozza <thozza@redhat.com>
Olaf Hering 29f3cf
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
Olaf Hering 29f3cf
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Olaf Hering 29f3cf
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Olaf Hering 29f3cf
Acked-by: Olaf Hering <ohering@suse.de>
Olaf Hering 29f3cf
---
Olaf Hering 29f3cf
 tools/hv/hv_kvp_daemon.c | 44 ++++++++++++++++++--------------------------
Olaf Hering 29f3cf
 1 file changed, 18 insertions(+), 26 deletions(-)
Olaf Hering 29f3cf
Olaf Hering 29f3cf
diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c
Olaf Hering 29f3cf
--- a/tools/hv/hv_kvp_daemon.c
Olaf Hering 29f3cf
+++ b/tools/hv/hv_kvp_daemon.c
Olaf Hering 29f3cf
@@ -39,6 +39,7 @@
Olaf Hering 29f3cf
 #include <fcntl.h>
Olaf Hering 29f3cf
 #include <dirent.h>
Olaf Hering 29f3cf
 #include <net/if.h>
Olaf Hering 29f3cf
+#include <limits.h>
Olaf Hering 29f3cf
 #include <getopt.h>
Olaf Hering 29f3cf
 
Olaf Hering 29f3cf
 /*
Olaf Hering 29f3cf
@@ -97,6 +98,8 @@ static struct utsname uts_buf;
Olaf Hering 29f3cf
 #define KVP_SCRIPTS_PATH "/usr/libexec/hypervkvpd/"
Olaf Hering 29f3cf
 #endif
Olaf Hering 29f3cf
 
Olaf Hering 29f3cf
+#define KVP_NET_DIR "/sys/class/net/"
Olaf Hering 29f3cf
+
Olaf Hering 29f3cf
 #define MAX_FILE_NAME 100
Olaf Hering 29f3cf
 #define ENTRIES_PER_BLOCK 50
Olaf Hering 29f3cf
 
Olaf Hering 29f3cf
@@ -596,26 +599,21 @@ static char *kvp_get_if_name(char *guid)
Olaf Hering 29f3cf
 	DIR *dir;
Olaf Hering 29f3cf
 	struct dirent *entry;
Olaf Hering 29f3cf
 	FILE    *file;
Olaf Hering 29f3cf
-	char    *p, *q, *x;
Olaf Hering 29f3cf
+	char    *p, *x;
Olaf Hering 29f3cf
 	char    *if_name = NULL;
Olaf Hering 29f3cf
 	char    buf[256];
Olaf Hering 29f3cf
-	char *kvp_net_dir = "/sys/class/net/";
Olaf Hering 29f3cf
-	char dev_id[256];
Olaf Hering 29f3cf
+	char dev_id[PATH_MAX];
Olaf Hering 29f3cf
 
Olaf Hering 29f3cf
-	dir = opendir(kvp_net_dir);
Olaf Hering 29f3cf
+	dir = opendir(KVP_NET_DIR);
Olaf Hering 29f3cf
 	if (dir == NULL)
Olaf Hering 29f3cf
 		return NULL;
Olaf Hering 29f3cf
 
Olaf Hering 29f3cf
-	snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir);
Olaf Hering 29f3cf
-	q = dev_id + strlen(kvp_net_dir);
Olaf Hering 29f3cf
-
Olaf Hering 29f3cf
 	while ((entry = readdir(dir)) != NULL) {
Olaf Hering 29f3cf
 		/*
Olaf Hering 29f3cf
 		 * Set the state for the next pass.
Olaf Hering 29f3cf
 		 */
Olaf Hering 29f3cf
-		*q = '\0';
Olaf Hering 29f3cf
-		strcat(dev_id, entry->d_name);
Olaf Hering 29f3cf
-		strcat(dev_id, "/device/device_id");
Olaf Hering 29f3cf
+		snprintf(dev_id, sizeof(dev_id), "%s%s/device/device_id",
Olaf Hering 29f3cf
+			 KVP_NET_DIR, entry->d_name);
Olaf Hering 29f3cf
 
Olaf Hering 29f3cf
 		file = fopen(dev_id, "r");
Olaf Hering 29f3cf
 		if (file == NULL)
Olaf Hering 29f3cf
@@ -653,12 +651,12 @@ static char *kvp_if_name_to_mac(char *if_name)
Olaf Hering 29f3cf
 	FILE    *file;
Olaf Hering 29f3cf
 	char    *p, *x;
Olaf Hering 29f3cf
 	char    buf[256];
Olaf Hering 29f3cf
-	char addr_file[256];
Olaf Hering 29f3cf
+	char addr_file[PATH_MAX];
Olaf Hering 29f3cf
 	unsigned int i;
Olaf Hering 29f3cf
 	char *mac_addr = NULL;
Olaf Hering 29f3cf
 
Olaf Hering 29f3cf
-	snprintf(addr_file, sizeof(addr_file), "%s%s%s", "/sys/class/net/",
Olaf Hering 29f3cf
-		if_name, "/address");
Olaf Hering 29f3cf
+	snprintf(addr_file, sizeof(addr_file), "%s%s%s", KVP_NET_DIR,
Olaf Hering 29f3cf
+		 if_name, "/address");
Olaf Hering 29f3cf
 
Olaf Hering 29f3cf
 	file = fopen(addr_file, "r");
Olaf Hering 29f3cf
 	if (file == NULL)
Olaf Hering 29f3cf
@@ -688,28 +686,22 @@ static char *kvp_mac_to_if_name(char *mac)
Olaf Hering 29f3cf
 	DIR *dir;
Olaf Hering 29f3cf
 	struct dirent *entry;
Olaf Hering 29f3cf
 	FILE    *file;
Olaf Hering 29f3cf
-	char    *p, *q, *x;
Olaf Hering 29f3cf
+	char    *p, *x;
Olaf Hering 29f3cf
 	char    *if_name = NULL;
Olaf Hering 29f3cf
 	char    buf[256];
Olaf Hering 29f3cf
-	char *kvp_net_dir = "/sys/class/net/";
Olaf Hering 29f3cf
-	char dev_id[256];
Olaf Hering 29f3cf
+	char dev_id[PATH_MAX];
Olaf Hering 29f3cf
 	unsigned int i;
Olaf Hering 29f3cf
 
Olaf Hering 29f3cf
-	dir = opendir(kvp_net_dir);
Olaf Hering 29f3cf
+	dir = opendir(KVP_NET_DIR);
Olaf Hering 29f3cf
 	if (dir == NULL)
Olaf Hering 29f3cf
 		return NULL;
Olaf Hering 29f3cf
 
Olaf Hering 29f3cf
-	snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir);
Olaf Hering 29f3cf
-	q = dev_id + strlen(kvp_net_dir);
Olaf Hering 29f3cf
-
Olaf Hering 29f3cf
 	while ((entry = readdir(dir)) != NULL) {
Olaf Hering 29f3cf
 		/*
Olaf Hering 29f3cf
 		 * Set the state for the next pass.
Olaf Hering 29f3cf
 		 */
Olaf Hering 29f3cf
-		*q = '\0';
Olaf Hering 29f3cf
-
Olaf Hering 29f3cf
-		strcat(dev_id, entry->d_name);
Olaf Hering 29f3cf
-		strcat(dev_id, "/address");
Olaf Hering 29f3cf
+		snprintf(dev_id, sizeof(dev_id), "%s%s/address", KVP_NET_DIR,
Olaf Hering 29f3cf
+			 entry->d_name);
Olaf Hering 29f3cf
 
Olaf Hering 29f3cf
 		file = fopen(dev_id, "r");
Olaf Hering 29f3cf
 		if (file == NULL)
Olaf Hering 29f3cf
@@ -1218,9 +1210,9 @@ static int process_ip_string(FILE *f, char *ip_string, int type)
Olaf Hering 29f3cf
 static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
Olaf Hering 29f3cf
 {
Olaf Hering 29f3cf
 	int error = 0;
Olaf Hering 29f3cf
-	char if_file[128];
Olaf Hering 29f3cf
+	char if_file[PATH_MAX];
Olaf Hering 29f3cf
 	FILE *file;
Olaf Hering 29f3cf
-	char cmd[512];
Olaf Hering 29f3cf
+	char cmd[PATH_MAX];
Olaf Hering 29f3cf
 	char *mac_addr;
Olaf Hering 29f3cf
 
Olaf Hering 29f3cf
 	/*