519c426
From a3634ce7eb5fc77e22c0d3722fb30e32b0d7d9fa Mon Sep 17 00:00:00 2001
6abf3a1
From: Vitaly Kuznetsov <vkuznets@redhat.com>
519c426
Date: Thu, 2 Jul 2015 17:21:02 +0200
8e8f9a7
Subject: [PATCH] Tools: hv: Fix for long file names from readdir
8e8f9a7
6abf3a1
From: Tomas Hozza <thozza@redhat.com>
6abf3a1
8e8f9a7
kvp_get_if_name and kvp_mac_to_if_name copy strings into statically
8e8f9a7
sized buffers which could be too small to store really long names.
8e8f9a7
8e8f9a7
Buffer sizes have been increased and length checks added via snprintf.
6abf3a1
6abf3a1
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
8e8f9a7
---
8e8f9a7
 hv_kvp_daemon.c | 26 +++++++++-----------------
8e8f9a7
 1 file changed, 9 insertions(+), 17 deletions(-)
8e8f9a7
8e8f9a7
diff --git a/hv_kvp_daemon.c b/hv_kvp_daemon.c
519c426
index 0d9f48e..ac89056 100644
8e8f9a7
--- a/hv_kvp_daemon.c
8e8f9a7
+++ b/hv_kvp_daemon.c
519c426
@@ -42,6 +42,7 @@
8e8f9a7
 #include <fcntl.h>
8e8f9a7
 #include <dirent.h>
8e8f9a7
 #include <net/if.h>
8e8f9a7
+#include <limits.h>
6abf3a1
 #include <getopt.h>
8e8f9a7
 
8e8f9a7
 /*
519c426
@@ -599,26 +600,22 @@ static char *kvp_get_if_name(char *guid)
8e8f9a7
 	DIR *dir;
8e8f9a7
 	struct dirent *entry;
8e8f9a7
 	FILE    *file;
8e8f9a7
-	char    *p, *q, *x;
8e8f9a7
+	char    *p, *x;
8e8f9a7
 	char    *if_name = NULL;
8e8f9a7
 	char    buf[256];
8e8f9a7
 	char *kvp_net_dir = "/sys/class/net/";
8e8f9a7
-	char dev_id[256];
8e8f9a7
+	char dev_id[PATH_MAX];
8e8f9a7
 
8e8f9a7
 	dir = opendir(kvp_net_dir);
8e8f9a7
 	if (dir == NULL)
8e8f9a7
 		return NULL;
8e8f9a7
 
8e8f9a7
-	snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir);
8e8f9a7
-	q = dev_id + strlen(kvp_net_dir);
8e8f9a7
-
8e8f9a7
 	while ((entry = readdir(dir)) != NULL) {
8e8f9a7
 		/*
8e8f9a7
 		 * Set the state for the next pass.
8e8f9a7
 		 */
8e8f9a7
-		*q = '\0';
8e8f9a7
-		strcat(dev_id, entry->d_name);
8e8f9a7
-		strcat(dev_id, "/device/device_id");
8e8f9a7
+		snprintf(dev_id, sizeof(dev_id), "%s%s/device/device_id", kvp_net_dir,
8e8f9a7
+				entry->d_name);
8e8f9a7
 
8e8f9a7
 		file = fopen(dev_id, "r");
8e8f9a7
 		if (file == NULL)
519c426
@@ -691,28 +688,23 @@ static char *kvp_mac_to_if_name(char *mac)
8e8f9a7
 	DIR *dir;
8e8f9a7
 	struct dirent *entry;
8e8f9a7
 	FILE    *file;
8e8f9a7
-	char    *p, *q, *x;
8e8f9a7
+	char    *p, *x;
8e8f9a7
 	char    *if_name = NULL;
8e8f9a7
 	char    buf[256];
8e8f9a7
 	char *kvp_net_dir = "/sys/class/net/";
8e8f9a7
-	char dev_id[256];
8e8f9a7
+	char dev_id[PATH_MAX];
519c426
 	unsigned int i;
8e8f9a7
 
8e8f9a7
 	dir = opendir(kvp_net_dir);
8e8f9a7
 	if (dir == NULL)
8e8f9a7
 		return NULL;
8e8f9a7
 
8e8f9a7
-	snprintf(dev_id, sizeof(dev_id), kvp_net_dir);
8e8f9a7
-	q = dev_id + strlen(kvp_net_dir);
8e8f9a7
-
8e8f9a7
 	while ((entry = readdir(dir)) != NULL) {
8e8f9a7
 		/*
8e8f9a7
 		 * Set the state for the next pass.
8e8f9a7
 		 */
8e8f9a7
-		*q = '\0';
8e8f9a7
-
8e8f9a7
-		strcat(dev_id, entry->d_name);
8e8f9a7
-		strcat(dev_id, "/address");
8e8f9a7
+		snprintf(dev_id, sizeof(dev_id), "%s%s/address", kvp_net_dir,
519c426
+			 entry->d_name);
8e8f9a7
 
8e8f9a7
 		file = fopen(dev_id, "r");
8e8f9a7
 		if (file == NULL)
8e8f9a7
-- 
519c426
2.4.3
8e8f9a7