Skip to contentSkip to conversation

log: fix double free on cleanup

PATCH Applied Robin Jarry 3 participants 5 messages Nov 29, 2024 – Dec 23, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 log: fix double free on cleanup

Fix the following crash when closing a log file after rte_eal_cleanup():

    double free or corruption (!prev)

    Thread 1 "grout" received signal SIGABRT, Aborted.
    __pthread_kill_implementation (threadid=<optimized out>,
    signo=signo@entry=6, no_tid=no_tid@entry=0) at pthread_kill.c:44
    ...
    #10 _IO_new_fclose (fp=0xb63090) at iofclose.c:74
    #11 0x000000000049c04e in dpdk_fini () at ../main/dpdk.c:204
    #12 0x0000000000402ab8 in main (...) at ../main/main.c:217
    (gdb) up 11
    #11 0x000000000049c04e in dpdk_fini () at ../main/dpdk.c:204
    202             rte_eal_cleanup();
    203             if (log_stream != NULL)
    204                     fclose(log_stream);

When the application has passed a custom file via rte_openlog_stream() DPDK should not call fclose() on it.

Add an internal is_internal_file field to track whether the file has been allocated by DPDK (syslog or journald) to determine if it should be closed or not.

Fixes 985130369be3 ("log: rework syslog handling")
---
 lib/log/log.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
lib/log/log.c +8 -6
index eb087d601e8b..e1c18a8e5351 100644
@@ -38,6 +38,7 @@ static struct rte_logs {
 	uint32_t type;  /**< Bitfield with enabled logs. */
 	uint32_t level; /**< Log level. */
 	FILE *file;     /**< Output file set by rte_openlog_stream, or NULL. */
+	bool is_internal_file;
 	log_print_t print_func;
 	size_t dynamic_types_len;
 	struct rte_log_dynamic_type *dynamic_types;
@@ -80,8 +81,11 @@ static RTE_DEFINE_PER_LCORE(struct log_cur_msg, log_cur_msg);
 int
 rte_openlog_stream(FILE *f)
 {
+	if (rte_logs.is_internal_file && rte_logs.file != NULL)
+		fclose(rte_logs.file);
 	rte_logs.file = f;
 	rte_logs.print_func = vfprintf;
+	rte_logs.is_internal_file = false;
 	return 0;
 }
 
@@ -520,6 +524,7 @@ eal_log_init(const char *id)
 		/* if either syslog or journal is used, then no special handling */
 		if (logf) {
 			rte_openlog_stream(logf);
+			rte_logs.is_internal_file = true;
 		} else {
 			bool is_terminal = isatty(fileno(stderr));
 			bool use_color = log_color_enabled(is_terminal);
@@ -550,11 +555,8 @@ eal_log_init(const char *id)
 void
 rte_eal_log_cleanup(void)
 {
-	FILE *log_stream = rte_logs.file;
-
-	/* don't close stderr on the application */
-	if (log_stream != NULL)
-		fclose(log_stream);
-
+	if (rte_logs.is_internal_file && rte_logs.file != NULL)
+		fclose(rte_logs.file);
 	rte_logs.file = NULL;
+	rte_logs.is_internal_file = false;
 }
Signature · 1 line
2.47.1
#1 lore raw
Quoted 5 lines · On Fri, 29 Nov 2024 17:10:14 +0100 Robin Jarry <[email protected]> wrote:
lib/log/log.c +0 -1
@@ -550,11 +555,8 @@ eal_log_init(const char *id)
 void
 rte_eal_log_cleanup(void)
 {
-	FILE *log_stream = rte_logs.file;

lib/log/log.c eal_log_init(const char *id) · line 562

Quoted 10 lines
lib/log/log.c +3 -5
-
-	/* don't close stderr on the application */
-	if (log_stream != NULL)
-		fclose(log_stream);
-
+	if (rte_logs.is_internal_file && rte_logs.file != NULL)
+		fclose(rte_logs.file);
 	rte_logs.file = NULL;
+	rte_logs.is_internal_file = false;
 }

The internal flag is ok, but we still don't want to close stderr in cleanup. Only places where syslog or journal wrapper is used.

#2 lore raw

Stephen Hemminger, Nov 29, 2024 at 17:54:

Quoted 13 lines
Quoted 10 lines · On Fri, 29 Nov 2024 17:10:14 +0100 Robin Jarry <[email protected]> wrote:
lib/log/log.c +0 -6
@@ -550,11 +555,8 @@ eal_log_init(const char *id)
 void
 rte_eal_log_cleanup(void)
 {
-	FILE *log_stream = rte_logs.file;
-
-	/* don't close stderr on the application */
-	if (log_stream != NULL)
-		fclose(log_stream);
-
Quoted 8 lines

lib/log/log.c eal_log_init(const char *id) · line 562

Quoted 5 lines
lib/log/log.c +3 -0
+	if (rte_logs.is_internal_file && rte_logs.file != NULL)
+		fclose(rte_logs.file);
 	rte_logs.file = NULL;
+	rte_logs.is_internal_file = false;
 }

The internal flag is ok, but we still don't want to close stderr in cleanup. Only places where syslog or journal wrapper is used.

There is no chance closing stderr will happen with my patch. stderr is only returned by rte_log_get_stream() when rte_logs.file is NULL, but rte_logs.file is not modified. It is only modified in eal_log_init() when --syslog or when running with journald. In both these cases, a new file is opened, not stderr.

Reviewed-by on v1 1/1
#3 lore raw
In reply to Robin Jarry
Quoted 20 lines · On Fri, 29 Nov 2024 17:10:14 +0100 Robin Jarry <[email protected]> wrote:

Fix the following crash when closing a log file after rte_eal_cleanup():

    double free or corruption (!prev)

    Thread 1 "grout" received signal SIGABRT, Aborted.
    __pthread_kill_implementation (threadid=<optimized out>,
    signo=signo@entry=6, no_tid=no_tid@entry=0) at pthread_kill.c:44
    ...
    #10 _IO_new_fclose (fp=0xb63090) at iofclose.c:74
    #11 0x000000000049c04e in dpdk_fini () at ../main/dpdk.c:204
    #12 0x0000000000402ab8 in main (...) at ../main/main.c:217
    (gdb) up 11
    #11 0x000000000049c04e in dpdk_fini () at ../main/dpdk.c:204
    202             rte_eal_cleanup();
    203             if (log_stream != NULL)
    204                     fclose(log_stream);

When the application has passed a custom file via rte_openlog_stream() DPDK should not call fclose() on it.

Quoted 6 lines

Add an internal is_internal_file field to track whether the file has been allocated by DPDK (syslog or journald) to determine if it should be closed or not.

Fixes 985130369be3 ("log: rework syslog handling")

Looks good, will look into adding more test cases for this in later release.

#4 lore raw
Quoted 20 lines · Stephen Hemminger
Quoted 17 lines · On Fri, 29 Nov 2024 17:10:14 +0100 Robin Jarry <[email protected]> wrote:

Fix the following crash when closing a log file after rte_eal_cleanup():

    double free or corruption (!prev)

    Thread 1 "grout" received signal SIGABRT, Aborted.
    __pthread_kill_implementation (threadid=<optimized out>,
    signo=signo@entry=6, no_tid=no_tid@entry=0) at pthread_kill.c:44
    ...
    #10 _IO_new_fclose (fp=0xb63090) at iofclose.c:74
    #11 0x000000000049c04e in dpdk_fini () at ../main/dpdk.c:204
    #12 0x0000000000402ab8 in main (...) at ../main/main.c:217
    (gdb) up 11
    #11 0x000000000049c04e in dpdk_fini () at ../main/dpdk.c:204
    202             rte_eal_cleanup();
    203             if (log_stream != NULL)
    204                     fclose(log_stream);
Quoted 8 lines
Quoted 8 lines

When the application has passed a custom file via rte_openlog_stream() DPDK should not call fclose() on it.

Add an internal is_internal_file field to track whether the file has been allocated by DPDK (syslog or journald) to determine if it should be closed or not.

Fixes 985130369be3 ("log: rework syslog handling")

Added Cc: stable for 24.11 LTS.

Quoted 2 lines
Quoted 1 line

Applied, thanks for the fix Robin.

Signature · 1 line
David Marchand