Skip to contentSkip to conversation

libtracefs: fix misc issues found by static analysis

PATCH New Jerome Marchand 1 participant 16 messages Jun 6, 2024

lore
Share & tools
lore
#0 lore raw

cover libtracefs: fix misc issues found by static analysis

A number of issues were found by running static analysers on the code of trace-cmd with openscanhub[1]. Mostly ressource leaks.

[1] https://fedoraproject.org/wiki/OpenScanHub

Jerome Marchand (15):
libtracefs: call va_end() before exiting tracefs_hist_set_sort_key()
libtracefs: prevent memory leak in append_filer()
libtracefs: prevent a memory leak in update_fields()
libtracefs: prevent a memory leak in tracefs_synth_add_end_field()
libtracefs: prevent memory leak in tracefs_event_systems()
libtracefs: don't leak socket file descriptor in open_vsock()
libtracefs: prevent a memory leak in add_func_str()
libtracefs: prevent a memory leak in tracefs_system_events()
libtracefs: prevent a memory leak in open_cpu_files()
libtracefs: prevent memory leak in tracefs_instance_create()
libtracefs: my_yyinput() should return 0 when no data can be read
libtracefs: prevent memory leak in tracefs_dynevent_get_all()
libtracefs: close dir in the error path in tracefs_system_events()
libtracefs: close dir in the error path in tracefs_event_systems()
libtracefs: initialize val in build_filter()

 src/tracefs-dynevents.c |  1 +
 src/tracefs-events.c    | 21 ++++++++++-----------
 src/tracefs-filter.c    |  6 ++++--
 src/tracefs-hist.c      | 10 ++++++----
 src/tracefs-instance.c  |  1 +
 src/tracefs-sqlhist.c   |  6 +++---
 src/tracefs-tools.c     |  4 +++-
 src/tracefs-vsock.c     |  4 +++-
 8 files changed, 31 insertions(+), 22 deletions(-)
Signature · 1 line
2.44.0
#1 lore raw

PATCH 1/15 libtracefs: call va_end() before exiting tracefs_hist_set_sort_key()

Each invocation of va_start() must be matched by a corresponding invocation of va_end() in the same function. If add_sort_key() fails, tracefs_hist_set_sort_key() exits without calling it. Call va_end after add_sort_key() fails.

Fixes a VARARGS error (CWE-237)

---
 src/tracefs-hist.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
src/tracefs-hist.c +3 -1
index 2b4f17f..87287b5 100644
@@ -596,8 +596,10 @@ int tracefs_hist_set_sort_key(struct tracefs_hist *hist,
 		if (!sort_key)
 			break;
 		tmp = add_sort_key(hist, sort_key, list);
-		if (!tmp)
+		if (!tmp) {
+			va_end(ap);
 			goto fail;
+		}
 		list = tmp;
 	}
 	va_end(ap);
Signature · 1 line
2.44.0
#2 lore raw

PATCH 2/15 libtracefs: prevent memory leak in append_filer()

In reply to Jerome Marchand

The buffer containing the new filter isn't freed if we encounter an error after it was allocated. Free tmp in the error path.

Fixes a RESSOURCE_LEAK error (CWE-772)

---
 src/tracefs-filter.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
src/tracefs-filter.c +4 -2
index afe3338..1b1c60e 100644
@@ -250,12 +250,12 @@ static int append_filter(char **filter, unsigned int *state,
 	case TRACEFS_COMPARE_NE: tmp = append_string(tmp, NULL, " != "); break;
 	case TRACEFS_COMPARE_RE:
 		if (!is_string)
-			goto inval;
+			goto free;
 		tmp = append_string(tmp, NULL, "~");
 		break;
 	default:
 		if (is_string)
-			goto inval;
+			goto free;
 	}
 
 	switch (compare) {
@@ -277,6 +277,8 @@ static int append_filter(char **filter, unsigned int *state,
 	*state = S_COMPARE;
 
 	return 0;
+free:
+	free(tmp);
 inval:
 	errno = EINVAL;
 	return -1;
Signature · 1 line
2.44.0
#3 lore raw

PATCH 3/15 libtracefs: prevent a memory leak in update_fields()

In reply to Jerome Marchand

In update_fields() p isn't freed is store_str fails. Since p is to be freed whatever the result of store_str() might be, move the call to free() before checking field_name.

Fixes a RESSOURCE_LEAK error (CWE-772)

---
 src/tracefs-sqlhist.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/tracefs-sqlhist.c +1 -1
index ecf09ce..c7b9eff 100644
@@ -810,9 +810,9 @@ static int update_fields(struct tep_handle *tep,
 			if (!p)
 				return -1;
 			field_name = store_str(sb, p);
+			free((char *)p);
 			if (!field_name)
 				return -1;
-			free((char *)p);
 		}
 
 		tfield = tep_find_any_field(event, field_name);
Signature · 1 line
2.44.0
#4 lore raw

PATCH 4/15 libtracefs: prevent a memory leak in tracefs_synth_add_end_field()

In reply to Jerome Marchand

Free tmp_var in the error path.

Fixes a RESSOURCE_LEAK error (CWE-772)

---
 src/tracefs-hist.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
src/tracefs-hist.c +3 -3
index 87287b5..4f4971e 100644
@@ -1576,7 +1576,7 @@ int tracefs_synth_add_end_field(struct tracefs_synth *synth,
 	const struct tep_format_field *field;
 	const char *hname = NULL;
 	char *tmp_var = NULL;
-	int ret;
+	int ret = -1;
 
 	if (!synth || !end_field) {
 		errno = EINVAL;
@@ -1594,15 +1594,15 @@ int tracefs_synth_add_end_field(struct tracefs_synth *synth,
 		tmp_var = new_arg(synth);
 
 	if (!trace_verify_event_field(synth->end_event, end_field, &field))
-		return -1;
+		goto out;
 
 	ret = add_var(&synth->end_vars, name ? hname : tmp_var, end_field, false);
 	if (ret)
 		goto out;
 
 	ret = add_synth_fields(synth, field, name, hname ? : tmp_var);
-	free(tmp_var);
  out:
+	free(tmp_var);
 	return ret;
 }
 
Signature · 1 line
2.44.0
#5 lore raw

PATCH 5/15 libtracefs: prevent memory leak in tracefs_event_systems()

In reply to Jerome Marchand

If an error is encountered in the loop, enable and sys might not get freed. Move the calls to free() before possibly exiting the loop.

Fixes a RESSOURCE_LEAK error (CWE-772)

---
 src/tracefs-events.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/tracefs-events.c +2 -2
index 1b1693c..906dbe8 100644
@@ -854,12 +854,12 @@ char **tracefs_event_systems(const char *tracing_dir)
 		enable = trace_append_file(sys, "enable");
 
 		ret = stat(enable, &st);
+		free(enable);
+		free(sys);
 		if (ret >= 0) {
 			if (add_list_string(&systems, name) < 0)
 				goto out_free;
 		}
-		free(enable);
-		free(sys);
 	}
 
 	closedir(dir);
Signature · 1 line
2.44.0
#6 lore raw

PATCH 6/15 libtracefs: don't leak socket file descriptor in open_vsock()

In reply to Jerome Marchand

Close the socket file descriptor if connect() fails.

Fixes a RESSOURCE_LEAK error (CWE-772)

---
 src/tracefs-vsock.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
src/tracefs-vsock.c +3 -1
index e171382..9171321 100644
@@ -19,8 +19,10 @@ static int open_vsock(unsigned int cid, unsigned int port)
 	if (sd < 0)
 		return -1;
 
-	if (connect(sd, (struct sockaddr *)&addr, sizeof(addr)))
+	if (connect(sd, (struct sockaddr *)&addr, sizeof(addr))) {
+		close(sd);
 		return -1;
+	}
 
 	return sd;
 }
Signature · 1 line
2.44.0
#7 lore raw

PATCH 7/15 libtracefs: prevent a memory leak in add_func_str()

In reply to Jerome Marchand

Free func_list if strdup() fails.

Fixes a RESSOURCE_LEAK error (CWE-772)

---
 src/tracefs-tools.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
src/tracefs-tools.c +3 -1
index 8e7b46d..74cfe91 100644
@@ -559,8 +559,10 @@ static int add_func_str(struct func_list ***next_func_ptr, const char *func)
 		if (!func_list)
 			return -1;
 		func_list->func = strdup(func);
-		if (!func_list->func)
+		if (!func_list->func) {
+			free(func_list);
 			return -1;
+		}
 		*next_func = func_list;
 		return 0;
 	}
Signature · 1 line
2.44.0
#8 lore raw

PATCH 8/15 libtracefs: prevent a memory leak in tracefs_system_events()

In reply to Jerome Marchand

If add_list_string() fails, event isn't freed. Free event before calling add_list_string();

Fixes a RESSOURCE_LEAK error (CWE-772)

---
 src/tracefs-events.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
src/tracefs-events.c +1 -2
index 906dbe8..d65837e 100644
@@ -919,11 +919,10 @@ char **tracefs_system_events(const char *tracing_dir, const char *system)
 			free(event);
 			continue;
 		}
+		free(event);
 
 		if (add_list_string(&events, name) < 0)
 			goto out_free;
-
-		free(event);
 	}
 
 	closedir(dir);
Signature · 1 line
2.44.0
#9 lore raw

PATCH 9/15 libtracefs: prevent a memory leak in open_cpu_files()

In reply to Jerome Marchand

In open_cpu_files(), if realloc() fails the latest allocated tcpu isn't freed. Rearrange the loop to prevent that.

---
 src/tracefs-events.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
src/tracefs-events.c +5 -5
index d65837e..88325e1 100644
@@ -275,9 +275,12 @@ static int open_cpu_files(struct tracefs_instance *instance, cpu_set_t *cpus,
 			tcpu = tracefs_cpu_snapshot_open(instance, cpu, true);
 		else
 			tcpu = tracefs_cpu_open_mapped(instance, cpu, true);
+		if (!tcpu)
+			goto error;
+
 		tmp = realloc(*all_cpus, (i + 1) * sizeof(*tmp));
 		if (!tmp) {
-			i--;
+			tracefs_cpu_close(tcpu);
 			goto error;
 		}
 
@@ -285,9 +288,6 @@ static int open_cpu_files(struct tracefs_instance *instance, cpu_set_t *cpus,
 
 		memset(tmp + i, 0, sizeof(*tmp));
 
-		if (!tcpu)
-			goto error;
-
 		tmp[i].tcpu = tcpu;
 		tmp[i].cpu = cpu;
 		i++;
@@ -296,7 +296,7 @@ static int open_cpu_files(struct tracefs_instance *instance, cpu_set_t *cpus,
 	return 0;
  error:
 	tmp = *all_cpus;
-	for (; i >= 0; i--) {
+	for (i--; i >= 0; i--) {
 		tracefs_cpu_close(tmp[i].tcpu);
 	}
 	free(tmp);
Signature · 1 line
2.44.0
#10 lore raw

PATCH 10/15 libtracefs: prevent memory leak in tracefs_instance_create()

In reply to Jerome Marchand

Free the path of the instance directory in the error path.

Fixes a RESSOURCE_LEAK error (CWE-772)

---
 src/tracefs-instance.c | 1 +
 1 file changed, 1 insertion(+)
src/tracefs-instance.c +1 -0
index 9a26708..dd7decd 100644
@@ -217,6 +217,7 @@ struct tracefs_instance *tracefs_instance_create(const char *name)
 	return inst;
 
 error:
+	tracefs_put_tracing_file(path);
 	tracefs_instance_free(inst);
 	return NULL;
 }
Signature · 1 line
2.44.0
#11 lore raw

PATCH 11/15 libtracefs: my_yyinput() should return 0 when no data can be read

In reply to Jerome Marchand

YY_INPUT() is redefined in sqlhist.l and basically just call my_yyinput() to do the work. However, YY_INPUT is supposed to return YY_NULL (0 on Unix system) when no data can be read, not -1. This can cause an overflow error in the generated sqlhist-lex.c file.

Have my_yyinput() returns zero when no buffer is found.

---
 src/tracefs-sqlhist.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/tracefs-sqlhist.c +1 -1
index c7b9eff..0f678c1 100644
@@ -121,7 +121,7 @@ __hidden int my_yyinput(void *extra, char *buf, int max)
 	struct sqlhist_bison *sb = extra;
 
 	if (!sb || !sb->buffer)
-		return -1;
+		return 0;
 
 	if (sb->buffer_idx + max > sb->buffer_size)
 		max = sb->buffer_size - sb->buffer_idx;
Signature · 1 line
2.44.0
#12 lore raw

PATCH 12/15 libtracefs: prevent memory leak in tracefs_dynevent_get_all()

In reply to Jerome Marchand

Free events in the error path of tracefs_dynevent_get_all().

Fixes a RESSOURCE_LEAK error (CWE-772)

---
 src/tracefs-dynevents.c | 1 +
 1 file changed, 1 insertion(+)
src/tracefs-dynevents.c +1 -0
index 7a3c45c..85c1fcd 100644
@@ -589,6 +589,7 @@ tracefs_dynevent_get_all(unsigned int types, const char *system)
 	return all_events;
 
 error:
+	free(events);
 	if (all_events) {
 		for (i = 0; i < all; i++)
 			free(all_events[i]);
Signature · 1 line
2.44.0
#13 lore raw

PATCH 13/15 libtracefs: close dir in the error path in tracefs_system_events()

In reply to Jerome Marchand

In tracefs_system_events(), we don't close dir if add_list_string() fails. At this point, just breaking out of the loop fixes that.

Fixes a RESSOURCE_LEAK error (CWE-772)

---
 src/tracefs-events.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/tracefs-events.c +1 -1
index 88325e1..1fa3f2f 100644
@@ -922,7 +922,7 @@ char **tracefs_system_events(const char *tracing_dir, const char *system)
 		free(event);
 
 		if (add_list_string(&events, name) < 0)
-			goto out_free;
+			break;
 	}
 
 	closedir(dir);
Signature · 1 line
2.44.0
#14 lore raw

PATCH 14/15 libtracefs: close dir in the error path in tracefs_event_systems()

In reply to Jerome Marchand

In tracefs_event_systems, we don't close dir if add_list_string() fails. At this point, just breaking out of the loop fixes that.

Fixes a RESSOURCE_LEAK error (CWE-772)

---
 src/tracefs-events.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/tracefs-events.c +1 -1
index 1fa3f2f..83069aa 100644
@@ -858,7 +858,7 @@ char **tracefs_event_systems(const char *tracing_dir)
 		free(sys);
 		if (ret >= 0) {
 			if (add_list_string(&systems, name) < 0)
-				goto out_free;
+				break;
 		}
 	}
 
Signature · 1 line
2.44.0
#15 lore raw

PATCH 15/15 libtracefs: initialize val in build_filter()

In reply to Jerome Marchand

In build_filter(), val is only initialized if filter->rval->type is either EXPR_NUMBER or EXPR_STRING. AS far as I can understand that is expected at that point. Howvever, it doesn't cost much to initialize it in case the parser let some bogus request pass by and it should prevent the static analyser to complain.

---
 src/tracefs-sqlhist.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/tracefs-sqlhist.c +1 -1
index 0f678c1..08bd0fa 100644
@@ -1109,7 +1109,7 @@ static int build_filter(struct tep_handle *tep, struct sqlhist_bison *sb,
 			     const char *val);
 	struct filter *filter = &expr->filter;
 	enum tracefs_compare cmp;
-	const char *val;
+	const char *val = NULL;
 	int and_or = TRACEFS_FILTER_AND;
 	char num[64];
 	int ret;
Signature · 1 line
2.44.0