8e8f9a7
From 396ee697e4d5c7d10bacf6d4670fb4ddab357330 Mon Sep 17 00:00:00 2001
8e8f9a7
From: Tomas Hozza <thozza@redhat.com>
8e8f9a7
Date: Mon, 5 Nov 2012 13:56:02 +0100
8e8f9a7
Subject: [PATCH] Tools: hv: Fix for long file names from readdir
8e8f9a7
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.
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
c426bbd
index 3ea3af2..4c2ab6a 100644
8e8f9a7
--- a/hv_kvp_daemon.c
8e8f9a7
+++ b/hv_kvp_daemon.c
8e8f9a7
@@ -44,6 +44,7 @@
8e8f9a7
 #include <fcntl.h>
8e8f9a7
 #include <dirent.h>
8e8f9a7
 #include <net/if.h>
8e8f9a7
+#include <limits.h>
8e8f9a7
 
8e8f9a7
 /*
8e8f9a7
  * KVP protocol: The user mode component first registers with the
8e8f9a7
@@ -588,26 +589,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)
8e8f9a7
@@ -680,28 +677,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];
8e8f9a7
 	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,
8e8f9a7
+                entry->d_name);
8e8f9a7
 
8e8f9a7
 		file = fopen(dev_id, "r");
8e8f9a7
 		if (file == NULL)
8e8f9a7
-- 
8e8f9a7
1.7.11.7
8e8f9a7