All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Tools: hv: Fix for long file names from readdir
@ 2012-11-08 14:13 Tomas Hozza
  2012-11-08 14:19 ` Olaf Hering
  0 siblings, 1 reply; 6+ messages in thread
From: Tomas Hozza @ 2012-11-08 14:13 UTC (permalink / raw)
  To: gregkh, linux-kernel, devel, olaf, apw, jasowang, kys; +Cc: Tomas Hozza

kvp_get_if_name and kvp_mac_to_if_name copy strings into statically
sized buffers which could be too small to store really long names.

Buffer sizes have been increased and length checks added via snprintf.

Signed-off-by: Tomas Hozza <thozza@redhat.com>
---
 tools/hv/hv_kvp_daemon.c | 25 ++++++++-----------------
 1 file changed, 8 insertions(+), 17 deletions(-)

diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c
index 13c2a14..bbd426c 100644
--- a/tools/hv/hv_kvp_daemon.c
+++ b/tools/hv/hv_kvp_daemon.c
@@ -592,26 +592,22 @@ static char *kvp_get_if_name(char *guid)
 	DIR *dir;
 	struct dirent *entry;
 	FILE    *file;
-	char    *p, *q, *x;
+	char    *p, *x;
 	char    *if_name = NULL;
 	char    buf[256];
 	char *kvp_net_dir = "/sys/class/net/";
-	char dev_id[256];
+	char dev_id[512];
 
 	dir = opendir(kvp_net_dir);
 	if (dir == NULL)
 		return NULL;
 
-	snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir);
-	q = dev_id + strlen(kvp_net_dir);
-
 	while ((entry = readdir(dir)) != NULL) {
 		/*
 		 * Set the state for the next pass.
 		 */
-		*q = '\0';
-		strcat(dev_id, entry->d_name);
-		strcat(dev_id, "/device/device_id");
+		snprintf(dev_id, sizeof(dev_id), "%s%s/device/device_id", kvp_net_dir,
+				entry->d_name);
 
 		file = fopen(dev_id, "r");
 		if (file == NULL)
@@ -684,28 +680,23 @@ static char *kvp_mac_to_if_name(char *mac)
 	DIR *dir;
 	struct dirent *entry;
 	FILE    *file;
-	char    *p, *q, *x;
+	char    *p, *x;
 	char    *if_name = NULL;
 	char    buf[256];
 	char *kvp_net_dir = "/sys/class/net/";
-	char dev_id[256];
+	char dev_id[512];
 	int i;
 
 	dir = opendir(kvp_net_dir);
 	if (dir == NULL)
 		return NULL;
 
-	snprintf(dev_id, sizeof(dev_id), kvp_net_dir);
-	q = dev_id + strlen(kvp_net_dir);
-
 	while ((entry = readdir(dir)) != NULL) {
 		/*
 		 * Set the state for the next pass.
 		 */
-		*q = '\0';
-
-		strcat(dev_id, entry->d_name);
-		strcat(dev_id, "/address");
+		snprintf(dev_id, sizeof(dev_id), "%s%s/address", kvp_net_dir,
+                entry->d_name);
 
 		file = fopen(dev_id, "r");
 		if (file == NULL)
-- 
1.7.11.7


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] Tools: hv: Fix for long file names from readdir
  2012-11-08 14:13 [PATCH] Tools: hv: Fix for long file names from readdir Tomas Hozza
@ 2012-11-08 14:19 ` Olaf Hering
  2012-11-08 14:52   ` Tomas Hozza
  0 siblings, 1 reply; 6+ messages in thread
From: Olaf Hering @ 2012-11-08 14:19 UTC (permalink / raw)
  To: Tomas Hozza; +Cc: gregkh, linux-kernel, devel, apw, jasowang, kys

On Thu, Nov 08, Tomas Hozza wrote:

> kvp_get_if_name and kvp_mac_to_if_name copy strings into statically
> sized buffers which could be too small to store really long names.

> -	char dev_id[256];
> +	char dev_id[512];

Shouldnt that be PATH_MAX or similar?

Olaf

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Tools: hv: Fix for long file names from readdir
  2012-11-08 14:19 ` Olaf Hering
@ 2012-11-08 14:52   ` Tomas Hozza
  2012-11-08 15:51     ` KY Srinivasan
  0 siblings, 1 reply; 6+ messages in thread
From: Tomas Hozza @ 2012-11-08 14:52 UTC (permalink / raw)
  To: Olaf Hering; +Cc: gregkh, linux-kernel, devel, apw, jasowang, kys

> > -        char dev_id[256];
> > +        char dev_id[512];
> 
> Shouldnt that be PATH_MAX or similar?

dirent->d_name should be PATH_MAX, but it is mostly 
not guaranteed. And then the dev_id is concatenated
with two strings so it can exceed 256 bytes.

After discussion with K. Y. Srinivasan I just doubled
the size and added size checks for sanity.

Tomas

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [PATCH] Tools: hv: Fix for long file names from readdir
  2012-11-08 14:52   ` Tomas Hozza
@ 2012-11-08 15:51     ` KY Srinivasan
  2012-11-09 12:47       ` Tomas Hozza
  0 siblings, 1 reply; 6+ messages in thread
From: KY Srinivasan @ 2012-11-08 15:51 UTC (permalink / raw)
  To: Tomas Hozza, Olaf Hering
  Cc: gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org,
	devel@linuxdriverproject.org, apw@canonical.com,
	jasowang@redhat.com

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="utf-8", Size: 870 bytes --]



> -----Original Message-----
> From: Tomas Hozza [mailto:thozza@redhat.com]
> Sent: Thursday, November 08, 2012 9:53 AM
> To: Olaf Hering
> Cc: gregkh@linuxfoundation.org; linux-kernel@vger.kernel.org;
> devel@linuxdriverproject.org; apw@canonical.com; jasowang@redhat.com; KY
> Srinivasan
> Subject: Re: [PATCH] Tools: hv: Fix for long file names from readdir
> 
> > > -        char dev_id[256];
> > > +        char dev_id[512];
> >
> > Shouldnt that be PATH_MAX or similar?
> 
> dirent->d_name should be PATH_MAX, but it is mostly
> not guaranteed. And then the dev_id is concatenated
> with two strings so it can exceed 256 bytes.

PATH_MAX (currently 4096 bytes) I think should suffice.

Regards,

K. Y

ÿôèº{.nÇ+‰·Ÿ®‰­†+%ŠËÿ±éݶ\x17¥Šwÿº{.nÇ+‰·¥Š{±þG«éÿŠ{ayº\x1dʇڙë,j\a­¢f£¢·hšïêÿ‘êçz_è®\x03(­éšŽŠÝ¢j"ú\x1a¶^[m§ÿÿ¾\a«þG«éÿ¢¸?™¨è­Ú&£ø§~á¶iO•æ¬z·švØ^\x14\x04\x1a¶^[m§ÿÿÃ\fÿ¶ìÿ¢¸?–I¥

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH] Tools: hv: Fix for long file names from readdir
  2012-11-08 15:51     ` KY Srinivasan
@ 2012-11-09 12:47       ` Tomas Hozza
  2012-11-26 20:43         ` KY Srinivasan
  0 siblings, 1 reply; 6+ messages in thread
From: Tomas Hozza @ 2012-11-09 12:47 UTC (permalink / raw)
  To: olaf, kys; +Cc: gregkh, linux-kernel, devel, apw, jasowang, Tomas Hozza

kvp_get_if_name and kvp_mac_to_if_name copy strings into statically
sized buffers which could be too small to store really long names.

Buffer sizes have been changed to PATH_MAX, include "limits.h" where
PATH_MAX is defined was added and length checks ware added via snprintf.

Signed-off-by: Tomas Hozza <thozza@redhat.com>
---
 tools/hv/hv_kvp_daemon.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)

diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c
index 13c2a14..54ecb95 100644
--- a/tools/hv/hv_kvp_daemon.c
+++ b/tools/hv/hv_kvp_daemon.c
@@ -44,6 +44,7 @@
 #include <fcntl.h>
 #include <dirent.h>
 #include <net/if.h>
+#include <limits.h>
 
 /*
  * KVP protocol: The user mode component first registers with the
@@ -592,26 +593,22 @@ static char *kvp_get_if_name(char *guid)
 	DIR *dir;
 	struct dirent *entry;
 	FILE    *file;
-	char    *p, *q, *x;
+	char    *p, *x;
 	char    *if_name = NULL;
 	char    buf[256];
 	char *kvp_net_dir = "/sys/class/net/";
-	char dev_id[256];
+	char dev_id[PATH_MAX];
 
 	dir = opendir(kvp_net_dir);
 	if (dir == NULL)
 		return NULL;
 
-	snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir);
-	q = dev_id + strlen(kvp_net_dir);
-
 	while ((entry = readdir(dir)) != NULL) {
 		/*
 		 * Set the state for the next pass.
 		 */
-		*q = '\0';
-		strcat(dev_id, entry->d_name);
-		strcat(dev_id, "/device/device_id");
+		snprintf(dev_id, sizeof(dev_id), "%s%s/device/device_id", kvp_net_dir,
+				entry->d_name);
 
 		file = fopen(dev_id, "r");
 		if (file == NULL)
@@ -684,28 +681,23 @@ static char *kvp_mac_to_if_name(char *mac)
 	DIR *dir;
 	struct dirent *entry;
 	FILE    *file;
-	char    *p, *q, *x;
+	char    *p, *x;
 	char    *if_name = NULL;
 	char    buf[256];
 	char *kvp_net_dir = "/sys/class/net/";
-	char dev_id[256];
+	char dev_id[PATH_MAX];
 	int i;
 
 	dir = opendir(kvp_net_dir);
 	if (dir == NULL)
 		return NULL;
 
-	snprintf(dev_id, sizeof(dev_id), kvp_net_dir);
-	q = dev_id + strlen(kvp_net_dir);
-
 	while ((entry = readdir(dir)) != NULL) {
 		/*
 		 * Set the state for the next pass.
 		 */
-		*q = '\0';
-
-		strcat(dev_id, entry->d_name);
-		strcat(dev_id, "/address");
+		snprintf(dev_id, sizeof(dev_id), "%s%s/address", kvp_net_dir,
+                entry->d_name);
 
 		file = fopen(dev_id, "r");
 		if (file == NULL)
-- 
1.7.11.7


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* RE: [PATCH] Tools: hv: Fix for long file names from readdir
  2012-11-09 12:47       ` Tomas Hozza
@ 2012-11-26 20:43         ` KY Srinivasan
  0 siblings, 0 replies; 6+ messages in thread
From: KY Srinivasan @ 2012-11-26 20:43 UTC (permalink / raw)
  To: Tomas Hozza, olaf@aepfle.de
  Cc: gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org,
	devel@linuxdriverproject.org, apw@canonical.com,
	jasowang@redhat.com



> -----Original Message-----
> From: Tomas Hozza [mailto:thozza@redhat.com]
> Sent: Friday, November 09, 2012 7:47 AM
> To: olaf@aepfle.de; KY Srinivasan
> Cc: gregkh@linuxfoundation.org; linux-kernel@vger.kernel.org;
> devel@linuxdriverproject.org; apw@canonical.com; jasowang@redhat.com;
> Tomas Hozza
> Subject: [PATCH] Tools: hv: Fix for long file names from readdir
> 
> kvp_get_if_name and kvp_mac_to_if_name copy strings into statically
> sized buffers which could be too small to store really long names.
> 
> Buffer sizes have been changed to PATH_MAX, include "limits.h" where
> PATH_MAX is defined was added and length checks ware added via snprintf.
> 
> Signed-off-by: Tomas Hozza <thozza@redhat.com>
Acked-by:  K. Y. Srinivasan <kys@microsoft.com>

> ---
>  tools/hv/hv_kvp_daemon.c | 26 +++++++++-----------------
>  1 file changed, 9 insertions(+), 17 deletions(-)
> 
> diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c
> index 13c2a14..54ecb95 100644
> --- a/tools/hv/hv_kvp_daemon.c
> +++ b/tools/hv/hv_kvp_daemon.c
> @@ -44,6 +44,7 @@
>  #include <fcntl.h>
>  #include <dirent.h>
>  #include <net/if.h>
> +#include <limits.h>
> 
>  /*
>   * KVP protocol: The user mode component first registers with the
> @@ -592,26 +593,22 @@ static char *kvp_get_if_name(char *guid)
>  	DIR *dir;
>  	struct dirent *entry;
>  	FILE    *file;
> -	char    *p, *q, *x;
> +	char    *p, *x;
>  	char    *if_name = NULL;
>  	char    buf[256];
>  	char *kvp_net_dir = "/sys/class/net/";
> -	char dev_id[256];
> +	char dev_id[PATH_MAX];
> 
>  	dir = opendir(kvp_net_dir);
>  	if (dir == NULL)
>  		return NULL;
> 
> -	snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir);
> -	q = dev_id + strlen(kvp_net_dir);
> -
>  	while ((entry = readdir(dir)) != NULL) {
>  		/*
>  		 * Set the state for the next pass.
>  		 */
> -		*q = '\0';
> -		strcat(dev_id, entry->d_name);
> -		strcat(dev_id, "/device/device_id");
> +		snprintf(dev_id, sizeof(dev_id), "%s%s/device/device_id",
> kvp_net_dir,
> +				entry->d_name);
> 
>  		file = fopen(dev_id, "r");
>  		if (file == NULL)
> @@ -684,28 +681,23 @@ static char *kvp_mac_to_if_name(char *mac)
>  	DIR *dir;
>  	struct dirent *entry;
>  	FILE    *file;
> -	char    *p, *q, *x;
> +	char    *p, *x;
>  	char    *if_name = NULL;
>  	char    buf[256];
>  	char *kvp_net_dir = "/sys/class/net/";
> -	char dev_id[256];
> +	char dev_id[PATH_MAX];
>  	int i;
> 
>  	dir = opendir(kvp_net_dir);
>  	if (dir == NULL)
>  		return NULL;
> 
> -	snprintf(dev_id, sizeof(dev_id), kvp_net_dir);
> -	q = dev_id + strlen(kvp_net_dir);
> -
>  	while ((entry = readdir(dir)) != NULL) {
>  		/*
>  		 * Set the state for the next pass.
>  		 */
> -		*q = '\0';
> -
> -		strcat(dev_id, entry->d_name);
> -		strcat(dev_id, "/address");
> +		snprintf(dev_id, sizeof(dev_id), "%s%s/address", kvp_net_dir,
> +                entry->d_name);
> 
>  		file = fopen(dev_id, "r");
>  		if (file == NULL)
> --
> 1.7.11.7




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2012-11-26 20:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-11-08 14:13 [PATCH] Tools: hv: Fix for long file names from readdir Tomas Hozza
2012-11-08 14:19 ` Olaf Hering
2012-11-08 14:52   ` Tomas Hozza
2012-11-08 15:51     ` KY Srinivasan
2012-11-09 12:47       ` Tomas Hozza
2012-11-26 20:43         ` KY Srinivasan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.