Skip to contentSkip to conversation

uuidparse: fix stack-buffer-overflow [asan]

PATCH In review Sami Kerola 3 participants 11 messages Sep 11, 2017 – Sep 18, 2017 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 uuidparse: fix stack-buffer-overflow [asan]

ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffee13b8f05 at pc 0x55debcc9fda4 bp 0x7ffee13b8d70 sp 0x7ffee13b8520
WRITE of size 38 at 0x7ffee13b8f05 thread T0

    #0 0x55debcc9fda3 in scanf_common(void*, int, bool, char const*, __va_list_tag*) (/home/src/util-linux/.libs/lt-uuidparse+0x63da3)
    #1 0x55debcca077b in vscanf (/home/src/util-linux/.libs/lt-uuidparse+0x6477b)
    #2 0x55debcca085f in scanf (/home/src/util-linux/.libs/lt-uuidparse+0x6485f)
    #3 0x55debcd50fad in print_output /home/src/util-linux/misc-utils/uuidparse.c:284:10
    #4 0x55debcd4fcc5 in main /home/src/util-linux/misc-utils/uuidparse.c:355:2
---
 misc-utils/uuidparse.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
misc-utils/uuidparse.c +1 -1
index 5fa34b23d..3347dc0af 100644
@@ -279,7 +279,7 @@ static void print_output(struct control const *const ctrl, int argc,
 		fill_table_row(tb, argv[i]);
 
 	if (i == 0) {
-		char uuid[UUID_STR_LEN];
+		char uuid[UUID_STR_LEN + 1];
 
 		while (scanf(" %" stringify_value(UUID_STR_LEN)
 			     "[^ \t\n]%*c", uuid) && !feof(stdin))
Signature · 1 line
2.14.1
#1 lore raw

PATCH 2/4 libsmartcols: fix heap-buffer-overflow [asan]

This gives room for element 0 in array of column cells.

ERROR: AddressSanitizer: heap-buffer-overflow on address 0x608000000080 at pc 0x5596bfaaca26 bp 0x7ffd8352a550 sp 0x7ffd83529d00
READ of size 64 at 0x608000000080 thread T0

    #0 0x5596bfaaca25 in __asan_memmove (/home/src/util-linux/.libs/lt-column+0xc2a25)
    #1 0x7f8d53660d1b in scols_line_move_cells /home/src/util-linux/libsmartcols/src/line.c:164:2
    #2 0x7f8d5366502c in scols_table_move_column /home/src/util-linux/libsmartcols/src/table.c:307:3
    #3 0x5596bfb0214b in reorder_table /home/src/util-linux/text-utils/column.c:303:3
    #4 0x5596bfaff7ec in modify_table /home/src/util-linux/text-utils/column.c:393:3
    #5 0x5596bfafdbb6 in main /home/src/util-linux/text-utils/column.c:770:4
    #6 0x7f8d52718f69 in __libc_start_main (/usr/lib/libc.so.6+0x20f69)
    #7 0x5596bfa089b9 in _start (/home/src/util-linux/.libs/lt-column+0x1e9b9)

0x608000000080 is located 0 bytes to the right of 96-byte region [0x608000000020,0x608000000080)

---
 libsmartcols/src/line.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsmartcols/src/line.c +1 -1
index aa339ce38..a041eeb60 100644
@@ -133,7 +133,7 @@ int scols_line_alloc_cells(struct libscols_line *ln, size_t n)
 
 	DBG(LINE, ul_debugobj(ln, "alloc %zu cells", n));
 
-	ce = realloc(ln->cells, n * sizeof(struct libscols_cell));
+	ce = realloc(ln->cells, (n + 1) * sizeof(struct libscols_cell));
 	if (!ce)
 		return -errno;
 
Signature · 1 line
2.14.1
#3 lore raw

PATCH 3/4 logger: fix memory leaks [asan]

In reply to Sami Kerola
---
 misc-utils/logger.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
misc-utils/logger.c +9 -3
index f6fb350cc..4eba778b9 100644
@@ -606,25 +606,30 @@ static char *strdup_structured_data(struct structured_data *sd)
 
 	xasprintf(&res, "[%s %s]", sd->id,
 			(tmp = strv_join(sd->params, " ")));
+	free(sd->id);
+	strv_free(sd->params);
 	free(tmp);
 	return res;
 }
 
 static char *strdup_structured_data_list(struct list_head *ls)
 {
-	struct list_head *p;
+	struct list_head *p, *pnext;
 	char *res = NULL;
 
-	list_for_each(p, ls) {
+	list_for_each_safe(p, pnext, ls) {
 		struct structured_data *sd = list_entry(p, struct structured_data, sds);
 		char *one = strdup_structured_data(sd);
 		char *tmp = res;
 
-		if (!one)
+		if (!one) {
+			free(tmp);
 			continue;
+		}
 		res = strappend(tmp, one);
 		free(tmp);
 		free(one);
+		free(sd);
 	}
 
 	return res;
@@ -979,6 +984,7 @@ static void logger_stdin(struct logger_ctl *ctl)
 		if (c == '\n')	/* discard line terminator */
 			c = getchar();
 	}
+	free(buf);
 }
 
 static void logger_close(const struct logger_ctl *ctl)
Signature · 1 line
2.14.1
#9 lore raw

PATCH 4/4 partx: fix memory leak [asan]

In reply to Sami Kerola
---
 disk-utils/partx.c | 2 ++
 1 file changed, 2 insertions(+)
disk-utils/partx.c +2 -0
index 43a6448db..0d66abadb 100644
@@ -1054,6 +1054,8 @@ int main(int argc, char **argv)
 	if (loopdev)
 		loopcxt_deinit(&lc);
 
+	free(wholedisk);
+
 	if (close_fd(fd) != 0)
 		err(EXIT_FAILURE, _("write failed"));
 
Signature · 1 line
2.14.1
maintainer
#4 lore raw

Re: [PATCH 3/4] logger: fix memory leaks [asan]

In reply to Sami Kerola
Quoted 10 lines · Sami Kerola
---
 misc-utils/logger.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
misc-utils/logger.c +0 -0
index f6fb350cc..4eba778b9 100644

misc-utils/logger.c static char *strdup_structured_data(struct structured_data *sd) · line 613

Quoted 8 lines
misc-utils/logger.c +2 -0
@@ -606,25 +606,30 @@ static char *strdup_structured_data(struct structured_data *sd)
 
 	xasprintf(&res, "[%s %s]", sd->id,
 			(tmp = strv_join(sd->params, " ")));
+	free(sd->id);
+	strv_free(sd->params);
 	free(tmp);
 	return res;
 }

This is strdup-like function, why do you touch the argument 'sd' ? It's has to copy and nothing else.

Quoted 5 lines
misc-utils/logger.c +1 -1
 static char *strdup_structured_data_list(struct list_head *ls)
 {
-	struct list_head *p;
+	struct list_head *p, *pnext;
 	char *res = NULL;

misc-utils/logger.c static char *strdup_structured_data(struct structured_data *sd) · line 626

Quoted 10 lines
misc-utils/logger.c +3 -2
 
-	list_for_each(p, ls) {
+	list_for_each_safe(p, pnext, ls) {
 		struct structured_data *sd = list_entry(p, struct structured_data, sds);
 		char *one = strdup_structured_data(sd);
 		char *tmp = res;
 
-		if (!one)
+		if (!one) {
+			free(tmp);

tmp contains result from the previous loop

misc-utils/logger.c static char *strdup_structured_data(struct structured_data *sd) · line 633

Quoted 7 lines
misc-utils/logger.c +2 -0
 			continue;
+		}
 		res = strappend(tmp, one);
 		free(tmp);
 		free(one);
+		free(sd);
 	}

again, I don't see reason to deallocate 'sd' in strdup-like function. It just converts the list to string and nothing else. It's also crazy to use free() for linked list member.

The list contains information specified by user on command line, if you deallocate the list then the next attempt to generate a header will produce incomplete information.

All the story seems like free-before-exit. We don't care about it.

The mem-leaks are bad thing in daemons, libs, libs tests, and allocation in loop is dangerous, otherwise for command line utils you waste time with this kind of free() calls, because kernel on exit() will be faster to remove all the program from memory.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
maintainer
#10 lore raw
In reply to Sami Kerola
Quoted 19 lines · Sami Kerola

ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffee13b8f05 at pc 0x55debcc9fda4 bp 0x7ffee13b8d70 sp 0x7ffee13b8520
WRITE of size 38 at 0x7ffee13b8f05 thread T0

    #0 0x55debcc9fda3 in scanf_common(void*, int, bool, char const*, __va_list_tag*) (/home/src/util-linux/.libs/lt-uuidparse+0x63da3)
    #1 0x55debcca077b in vscanf (/home/src/util-linux/.libs/lt-uuidparse+0x6477b)
    #2 0x55debcca085f in scanf (/home/src/util-linux/.libs/lt-uuidparse+0x6485f)
    #3 0x55debcd50fad in print_output /home/src/util-linux/misc-utils/uuidparse.c:284:10
    #4 0x55debcd4fcc5 in main /home/src/util-linux/misc-utils/uuidparse.c:355:2
---
 misc-utils/uuidparse.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
misc-utils/uuidparse.c +0 -0
index 5fa34b23d..3347dc0af 100644

misc-utils/uuidparse.c static void print_output(struct control const *const ctrl, int argc, · line 284

Quoted 7 lines
misc-utils/uuidparse.c +1 -1
@@ -279,7 +279,7 @@ static void print_output(struct control const *const ctrl, int argc,
 		fill_table_row(tb, argv[i]);
 
 	if (i == 0) {
-		char uuid[UUID_STR_LEN];
+		char uuid[UUID_STR_LEN + 1];
 
 		while (scanf(" %" stringify_value(UUID_STR_LEN)

It seems better to use "%36[" because the uuid size is really 36+\0.

Fixed, good catch.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
maintainer
#2 lore raw

Re: [PATCH 2/4] libsmartcols: fix heap-buffer-overflow [asan]

In reply to Sami Kerola
Quoted 26 lines · Sami Kerola

This gives room for element 0 in array of column cells.

ERROR: AddressSanitizer: heap-buffer-overflow on address 0x608000000080 at pc 0x5596bfaaca26 bp 0x7ffd8352a550 sp 0x7ffd83529d00
READ of size 64 at 0x608000000080 thread T0

    #0 0x5596bfaaca25 in __asan_memmove (/home/src/util-linux/.libs/lt-column+0xc2a25)
    #1 0x7f8d53660d1b in scols_line_move_cells /home/src/util-linux/libsmartcols/src/line.c:164:2
    #2 0x7f8d5366502c in scols_table_move_column /home/src/util-linux/libsmartcols/src/table.c:307:3
    #3 0x5596bfb0214b in reorder_table /home/src/util-linux/text-utils/column.c:303:3
    #4 0x5596bfaff7ec in modify_table /home/src/util-linux/text-utils/column.c:393:3
    #5 0x5596bfafdbb6 in main /home/src/util-linux/text-utils/column.c:770:4
    #6 0x7f8d52718f69 in __libc_start_main (/usr/lib/libc.so.6+0x20f69)
    #7 0x5596bfa089b9 in _start (/home/src/util-linux/.libs/lt-column+0x1e9b9)

0x608000000080 is located 0 bytes to the right of 96-byte region [0x608000000020,0x608000000080)

---
 libsmartcols/src/line.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsmartcols/src/line.c +0 -0
index aa339ce38..a041eeb60 100644

libsmartcols/src/line.c int scols_line_alloc_cells(struct libscols_line *ln, size_t n) · line 136

Quoted 5 lines
libsmartcols/src/line.c +1 -1
@@ -133,7 +133,7 @@ int scols_line_alloc_cells(struct libscols_line *ln, size_t n)
 
 	DBG(LINE, ul_debugobj(ln, "alloc %zu cells", n));
 
-	ce = realloc(ln->cells, n * sizeof(struct libscols_cell));
+	ce = realloc(ln->cells, (n + 1) * sizeof(struct libscols_cell));

The issue is scols_line_move_cells(). Fixed. Thanks!

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#5 lore raw

Re: [PATCH 3/4] logger: fix memory leaks [asan]

In reply to Karel Zak
Quoted 1 line · Karel Zak

On Mon, Sep 11, 2017 at 08:57:16PM +0100, Sami Kerola wrote:

8< ---

Quoted 4 lines

The mem-leaks are bad thing in daemons, libs, libs tests, and allocation in loop is dangerous, otherwise for command line utils you waste time with this kind of free() calls, because kernel on exit() will be faster to remove all the program from memory.

Does this rule apply to the free() calls in hwclock.c?

Quoted 3 lines

Karel

#6 lore raw

Re: [PATCH 3/4] logger: fix memory leaks [asan]

Quoted 8 lines · J William Piggott
Quoted 5 lines · Karel Zak

On Mon, Sep 11, 2017 at 08:57:16PM +0100, Sami Kerola wrote:
The mem-leaks are bad thing in daemons, libs, libs tests, and allocation in loop is dangerous, otherwise for command line utils you waste time with this kind of free() calls, because kernel on exit() will be faster to remove all the program from memory.

Does this rule apply to the free() calls in hwclock.c?

What I was trying to to achieve was as few ASAN failures as possible. That makes seeing memory corruptions, buffer overflows and other things that do matter easier. Maybe adding supressions is good enough way to deal the rest.

https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer

Thank you Karel making my patches better. Here's what I can still see:

https://pastebin.com/raw/U4CSUrxS

Compiled with these:

export CC=clang
export CFLAGS='-fsanitize=address -ggdb' export ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer

And disk-utils/partx.c needs atexit(close_stdout); commented out to display errors.

Signature · 2 lines
Sami Kerola
http://www.iki.fi/kerolasa/
maintainer
#7 lore raw

Re: [PATCH 3/4] logger: fix memory leaks [asan]

Quoted 15 lines · Sami Kerola
Quoted 8 lines · J William Piggott
Quoted 5 lines · Karel Zak

On Mon, Sep 11, 2017 at 08:57:16PM +0100, Sami Kerola wrote:
The mem-leaks are bad thing in daemons, libs, libs tests, and allocation in loop is dangerous, otherwise for command line utils you waste time with this kind of free() calls, because kernel on exit() will be faster to remove all the program from memory.

Does this rule apply to the free() calls in hwclock.c?

What I was trying to to achieve was as few ASAN failures as possible. That makes seeing memory corruptions, buffer overflows and other things that do matter easier. Maybe adding supressions is good enough way to deal the rest.

Quoted 6 lines

https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer

Thank you Karel making my patches better. Here's what I can still see:

https://pastebin.com/raw/U4CSUrxS

Fixed. Thanks!

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
maintainer
#8 lore raw

Re: [PATCH 3/4] logger: fix memory leaks [asan]

In reply to Sami Kerola
Quoted 5 lines · Sami Kerola

Compiled with these:

export CC=clang
export CFLAGS='-fsanitize=address -ggdb' export ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer

An idea for the next release v2.32:

* add a new --enable-clang-asan to ./configure
* add a new --asan option to tests/run.sh
* rename tests/run.sh --memcheck to --valgrind
* rename tests/functions.sh:ts_valgrind() to ts_run()
* improve ts_run() to be sensitive to --asan or --valgrind (etc.)
* use export ASAN_OPTIONS='detect_leaks=0' by default
* add TS_ENABLE_MEMLEAK_CHECK=yes to tests where we want to check for memory leaks (library tests, daemons, etc.)

and then we can use

    $ ./configure --enable-clang-asan
    $ cd tests
    $ ./run.sh --asan

to be sure we're without memory corruptions or relevant memory leaks.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com