Skip to contentSkip to conversation

tools: libxenstat: fix format string overflow

PATCH Superseded Dario Faggioli 3 participants 8 messages Feb 16, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools: libxenstat: fix format string overflow

With gcc 7.3.0, the build fails like this:

src/xenstat_linux.c: In function ‘getBridge’
src/xenstat_linux.c:78:34: warning: ‘%s’ directive writing up to 255 bytes into a region of size 241 [-Wformat-overflow=]

     sprintf(tmp, "/sys/class/net/%s/bridge", de->d_name);
                                  ^~
src/xenstat_linux.c:78:5: note: ‘sprintf’ output between 23 and 278 bytes into a destination of size 256
     sprintf(tmp, "/sys/class/net/%s/bridge", de->d_name);
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix using asprintf().

---
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>
---
In case no one else have noticed and fixed this (I have checked xen-devel and found nothing)

---
 tools/xenstat/libxenstat/src/xenstat_linux.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/xenstat/libxenstat/src/xenstat_linux.c +5 -3
index 907d65fa63..396357511b 100644
@@ -69,18 +69,20 @@ void getBridge(char *excludeName, char *result, size_t resultLen)
 	struct dirent *de;
 	DIR *d;
 
-	char tmp[256] = { 0 };
-
 	d = opendir("/sys/class/net");
 	while ((de = readdir(d)) != NULL) {
 		if ((strlen(de->d_name) > 0) && (de->d_name[0] != '.')
 			&& (strstr(de->d_name, excludeName) == NULL)) {
-				sprintf(tmp, "/sys/class/net/%s/bridge", de->d_name);
+				char *tmp;
+
+				asprintf(&tmp, "/sys/class/net/%s/bridge", de->d_name);
 
 				if (access(tmp, F_OK) == 0) {
 					strncpy(result, de->d_name, resultLen - 1);
 					result[resultLen - 1] = 0;
 				}
+
+				free(tmp);
 		}
 	}
 

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#1 lore raw
Quoted 33 lines · Dario Faggioli

With gcc 7.3.0, the build fails like this:

src/xenstat_linux.c: In function ‘getBridge’
src/xenstat_linux.c:78:34: warning: ‘%s’ directive writing up to 255 bytes into a region of size 241 [-Wformat-overflow=]

     sprintf(tmp, "/sys/class/net/%s/bridge", de->d_name);
                                  ^~
src/xenstat_linux.c:78:5: note: ‘sprintf’ output between 23 and 278 bytes into a destination of size 256
     sprintf(tmp, "/sys/class/net/%s/bridge", de->d_name);
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix using asprintf().

---
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>
---
In case no one else have noticed and fixed this (I have checked xen-devel and found nothing)

---
 tools/xenstat/libxenstat/src/xenstat_linux.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/xenstat/libxenstat/src/xenstat_linux.c +0 -2
index 907d65fa63..396357511b 100644
@@ -69,18 +69,20 @@ void getBridge(char *excludeName, char *result, size_t resultLen)
 	struct dirent *de;
 	DIR *d;
 
-	char tmp[256] = { 0 };
-

tools/xenstat/libxenstat/src/xenstat_linux.c void getBridge(char *excludeName, char *result, size_t resultLen) · line 80

Quoted 10 lines
tools/xenstat/libxenstat/src/xenstat_linux.c +3 -1
 	d = opendir("/sys/class/net");
 	while ((de = readdir(d)) != NULL) {
 		if ((strlen(de->d_name) > 0) && (de->d_name[0] != '.')
 			&& (strstr(de->d_name, excludeName) == NULL)) {
-				sprintf(tmp, "/sys/class/net/%s/bridge", de->d_name);
+				char *tmp;
+
+				asprintf(&tmp, "/sys/class/net/%s/bridge", de->d_name);
 
 				if (access(tmp, F_OK) == 0) {

Possible NULL dereference.

IIRC, you need to set tmp to NULL first, otherwise you may free a spurious pointer on failure, and check for asprintf() returning < 0.

~Andrew

Quoted 10 lines
tools/xenstat/libxenstat/src/xenstat_linux.c +2 -0
 					strncpy(result, de->d_name, resultLen - 1);
 					result[resultLen - 1] = 0;
 				}
+
+				free(tmp);
 		}
 	}
 
Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
In reply to Dario Faggioli
Quoted 31 lines · Dario Faggioli

With gcc 7.3.0, the build fails like this:

src/xenstat_linux.c: In function ‘getBridge’
src/xenstat_linux.c:78:34: warning: ‘%s’ directive writing up to 255 bytes into a region of size 241 [-Wformat-overflow=]

     sprintf(tmp, "/sys/class/net/%s/bridge", de->d_name);
                                  ^~
src/xenstat_linux.c:78:5: note: ‘sprintf’ output between 23 and 278 bytes into a destination of size 256
     sprintf(tmp, "/sys/class/net/%s/bridge", de->d_name);
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix using asprintf().

---
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>
---
In case no one else have noticed and fixed this (I have checked xen-devel and found nothing)

---
 tools/xenstat/libxenstat/src/xenstat_linux.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/xenstat/libxenstat/src/xenstat_linux.c +0 -0
index 907d65fa63..396357511b 100644
@@ -69,18 +69,20 @@ void getBridge(char *excludeName, char *result, size_t resultLen)
 	struct dirent *de;
 	DIR *d;
 

tools/xenstat/libxenstat/src/xenstat_linux.c void getBridge(char *excludeName, char *result, size_t resultLen) · line 78

Quoted 10 lines
tools/xenstat/libxenstat/src/xenstat_linux.c +3 -3
-	char tmp[256] = { 0 };
-
 	d = opendir("/sys/class/net");
 	while ((de = readdir(d)) != NULL) {
 		if ((strlen(de->d_name) > 0) && (de->d_name[0] != '.')
 			&& (strstr(de->d_name, excludeName) == NULL)) {
-				sprintf(tmp, "/sys/class/net/%s/bridge", de->d_name);
+				char *tmp;
+
+				asprintf(&tmp, "/sys/class/net/%s/bridge", de->d_name);

Need to check the return value of asprintf.

Preferably you also need to define _GNU_SOURCE at the beginning of this file. This file is Linux only makes it less of a problem.

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 45 lines · Wei Liu
Quoted 31 lines · Dario Faggioli

With gcc 7.3.0, the build fails like this:

src/xenstat_linux.c: In function ‘getBridge’
src/xenstat_linux.c:78:34: warning: ‘%s’ directive writing up to 255 bytes into a region of size 241 [-Wformat-overflow=]

     sprintf(tmp, "/sys/class/net/%s/bridge", de->d_name);
                                  ^~
src/xenstat_linux.c:78:5: note: ‘sprintf’ output between 23 and 278 bytes into a destination of size 256
     sprintf(tmp, "/sys/class/net/%s/bridge", de->d_name);
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix using asprintf().

---
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>
---
In case no one else have noticed and fixed this (I have checked xen-devel and found nothing)

---
 tools/xenstat/libxenstat/src/xenstat_linux.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/xenstat/libxenstat/src/xenstat_linux.c +0 -0
index 907d65fa63..396357511b 100644
@@ -69,18 +69,20 @@ void getBridge(char *excludeName, char *result, size_t resultLen)
 	struct dirent *de;
 	DIR *d;
 

tools/xenstat/libxenstat/src/xenstat_linux.c void getBridge(char *excludeName, char *result, size_t resultLen) · line 78

Quoted 10 lines
tools/xenstat/libxenstat/src/xenstat_linux.c +3 -3
-	char tmp[256] = { 0 };
-
 	d = opendir("/sys/class/net");
 	while ((de = readdir(d)) != NULL) {
 		if ((strlen(de->d_name) > 0) && (de->d_name[0] != '.')
 			&& (strstr(de->d_name, excludeName) == NULL)) {
-				sprintf(tmp, "/sys/class/net/%s/bridge", de->d_name);
+				char *tmp;
+
+				asprintf(&tmp, "/sys/class/net/%s/bridge", de->d_name);

Need to check the return value of asprintf.

Quoted 2 lines

Preferably you also need to define _GNU_SOURCE at the beginning of this file. This file is Linux only makes it less of a problem.

To be precise: not necessarily at the beginning of this file, just before the actual inclusion of stdio.h.

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
In reply to Wei Liu
Quoted 9 lines · Wei Liu
Quoted 8 lines · Dario Faggioli
tools/xenstat/libxenstat/src/xenstat_linux.c +0 -0
@@ -69,18 +69,20 @@ void getBridge(char *excludeName, char *result,
 size_t resultLen)
 	struct dirent *de;
 	DIR *d;
 
Quoted 17 lines
Quoted 4 lines
tools/xenstat/libxenstat/src/xenstat_linux.c +0 -2
-	char tmp[256] = { 0 };
-
 	d = opendir("/sys/class/net");
 	while ((de = readdir(d)) != NULL) {

tools/xenstat/libxenstat/src/xenstat_linux.c void getBridge(char *excludeName, char *result, · line 83

Quoted 10 lines
tools/xenstat/libxenstat/src/xenstat_linux.c +3 -1
 		if ((strlen(de->d_name) > 0) && (de->d_name[0] !=
 '.')
 			&& (strstr(de->d_name, excludeName) ==
 NULL)) {
-				sprintf(tmp,
 "/sys/class/net/%s/bridge", de->d_name);
+				char *tmp;
+
+				asprintf(&tmp,
 "/sys/class/net/%s/bridge", de->d_name);

Need to check the return value of asprintf.

Right! And what do I do if it fails, 'continue' the while(), I guess?

Thanks and Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Software Engineer @ SUSE https://www.suse.com/

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 27 lines · Dario Faggioli
Quoted 9 lines · Wei Liu
Quoted 8 lines · Dario Faggioli
tools/xenstat/libxenstat/src/xenstat_linux.c +0 -0
@@ -69,18 +69,20 @@ void getBridge(char *excludeName, char *result,
 size_t resultLen)
 	struct dirent *de;
 	DIR *d;
 
Quoted 17 lines
Quoted 4 lines
tools/xenstat/libxenstat/src/xenstat_linux.c +0 -2
-	char tmp[256] = { 0 };
-
 	d = opendir("/sys/class/net");
 	while ((de = readdir(d)) != NULL) {

tools/xenstat/libxenstat/src/xenstat_linux.c void getBridge(char *excludeName, char *result, · line 83

Quoted 10 lines
tools/xenstat/libxenstat/src/xenstat_linux.c +3 -1
 		if ((strlen(de->d_name) > 0) && (de->d_name[0] !=
 '.')
 			&& (strstr(de->d_name, excludeName) ==
 NULL)) {
-				sprintf(tmp,
 "/sys/class/net/%s/bridge", de->d_name);
+				char *tmp;
+
+				asprintf(&tmp,
 "/sys/class/net/%s/bridge", de->d_name);

Need to check the return value of asprintf.

Quoted 2 lines

Right! And what do I do if it fails, 'continue' the while(), I guess?

Looking at the error message again, can you just increase the buffer size to 512? That should do the job?

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw
Quoted 10 lines · Wei Liu
Quoted 5 lines · Dario Faggioli
Quoted 1 line · Wei Liu

Right! And what do I do if it fails, 'continue' the while(), I guess?

Looking at the error message again, can you just increase the buffer size to 512?

Sure I can.

Quoted 2 lines

That should do the job?

Yeah, less "elegant", IMHO, but less tricky indeed. :-D

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Software Engineer @ SUSE https://www.suse.com/

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw
In reply to Wei Liu
Quoted 9 lines · Wei Liu
Quoted 8 lines · Dario Faggioli
tools/xenstat/libxenstat/src/xenstat_linux.c +0 -0
@@ -69,18 +69,20 @@ void getBridge(char *excludeName, char *result,
 size_t resultLen)
 	struct dirent *de;
 	DIR *d;
 
Quoted 17 lines
Quoted 4 lines
tools/xenstat/libxenstat/src/xenstat_linux.c +0 -2
-	char tmp[256] = { 0 };
-
 	d = opendir("/sys/class/net");
 	while ((de = readdir(d)) != NULL) {

tools/xenstat/libxenstat/src/xenstat_linux.c void getBridge(char *excludeName, char *result, · line 83

Quoted 10 lines
tools/xenstat/libxenstat/src/xenstat_linux.c +3 -1
 		if ((strlen(de->d_name) > 0) && (de->d_name[0] !=
 '.')
 			&& (strstr(de->d_name, excludeName) ==
 NULL)) {
-				sprintf(tmp,
 "/sys/class/net/%s/bridge", de->d_name);
+				char *tmp;
+
+				asprintf(&tmp,
 "/sys/class/net/%s/bridge", de->d_name);

Need to check the return value of asprintf.

Right! And what do I do if it fails, 'continue' the while(), I guess?

Thanks and Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Software Engineer @ SUSE https://www.suse.com/

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel