tools: libxenstat: fix format string overflow
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
@@ -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
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().Signed-off-by Dario Faggioli <[email protected]>---
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@@ -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
-1d =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
-0strncpy (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
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().Signed-off-by Dario Faggioli <[email protected]>---
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@@ -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
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().Signed-off-by Dario Faggioli <[email protected]>---
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@@ -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_SOURCEat 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
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
-1if ((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
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
-1if ((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
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
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
-1if ((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