tracefs utils: Do not free a the buffer on a zero size str_read_file()
PATCH 1/1 tracefs utils: Do not free a the buffer on a zero size str_read_file()
From: "Steven Rostedt (VMware)" <[email protected]>
If a file has no size (nothing is read), then str_read_file() frees the buffer and returns zero. The problem is that all callers of str_read_file() uses the buffer supplied if the value returned is not a negative. This causes the freed buffer being used by the callers if the file read existed but had no content.
This is apparent when using a copy of the tracefs directory, where some file exist, but have no content, then loading the events would cause a segfault.
--- tracefs-utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tracefs-utils.c +1 -1
@@ -220,7 +220,7 @@ __hidden int str_read_file(const char *file, char **buffer) }while (r >0 );close (fd); -if (r ==0 && size >0 ) { +if (r ==0 ) { buf[size] ='\0' ; *buffer = buf; }else
Signature · 1 line
2.25.4
Quoted 22 lines · Steven Rostedt
From: "Steven Rostedt (VMware)" <[email protected]>
If a file has no size (nothing is read), then
str_read_file()frees the buffer and returns zero. The problem is that all callers ofstr_read_file()uses the buffer supplied if the value returned is not a negative. This causes the freed buffer being used by the callers if the file read existed but had no content.This is apparent when using a copy of the tracefs directory, where some file exist, but have no content, then loading the events would cause a segfault.
Signed-off-by Steven Rostedt (VMware) <[email protected]>--- tracefs-utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tracefs-utils.c +0
-0
tracefs-utils.c __hidden int str_read_file(const char *file, char **buffer) · line 224
Quoted 6 lines
tracefs-utils.c +1
-1@@ -220,7 +220,7 @@ __hidden int str_read_file(const char *file, char **buffer) }while (r >0 );close (fd); -if (r ==0 && size >0 ) { +if (r ==0 ) { buf[size] ='\0' ;
If size is 0, in case of an empty file, then buf should also be NULL and this assignment
will be on invalid memory.
Quoted 5 lines
tracefs-utils.c +0
-1*buffer = buf; }else --2.25.4
Signature · 2 lines
Tzvetomir (Ceco) Stoyanov VMware Open Source Technology Center
Quoted 6 lines · On Tue, 24 Nov 2020 07:44:14 +0200 Tzvetomir Stoyanov <[email protected]>…
Quoted 6 lines
index 326b455..690506c 100644
tracefs-utils.c +0
-0@@ -220,7 +220,7 @@ __hidden int str_read_file(const char *file, char **buffer) }while (r >0 );
Quoted 8 lines
tracefs-utils.c __hidden int str_read_file(const char *file, char **buffer) · line 224
Quoted 4 lines
tracefs-utils.c +1
-1close (fd); -if (r ==0 && size >0 ) { +if (r ==0 ) { buf[size] ='\0' ;If size is 0, in case of an empty file, then buf should also be NULL and this assignment
will be on invalid memory.
I quickly realized that, and sent out a v2 ;-)
https://lore.kernel.org/r/[email protected]
-- Steve
Quoted 4 lines
Quoted 4 lines
tracefs-utils.c +0
-1*buffer = buf; }else --