Blame libtracefs-Prevent-memory-leak-in-append_filer.patch

Jerome Marchand 3772ca
From 6b356651a3fdbb63531036941dd02fd60da5e52d Mon Sep 17 00:00:00 2001
Jerome Marchand 3772ca
From: Jerome Marchand <jmarchan@redhat.com>
Jerome Marchand 3772ca
Date: Thu, 6 Jun 2024 17:38:16 +0200
Jerome Marchand 3772ca
Subject: [PATCH 02/15] libtracefs: Prevent memory leak in append_filer()
Jerome Marchand 3772ca
Jerome Marchand 3772ca
The buffer containing the new filter isn't freed if we encounter an
Jerome Marchand 3772ca
error after it was allocated. Free tmp in the error path.
Jerome Marchand 3772ca
Jerome Marchand 3772ca
Fixes a RESSOURCE_LEAK error (CWE-772)
Jerome Marchand 3772ca
Jerome Marchand 3772ca
Link: https://lore.kernel.org/linux-trace-devel/20240606153830.2666120-3-jmarchan@redhat.com
Jerome Marchand 3772ca
Jerome Marchand 3772ca
Fixes: 24b856f0bcf3d ("libtracefs: Add filter creating and verify API")
Jerome Marchand 3772ca
Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
Jerome Marchand 3772ca
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
Jerome Marchand 3772ca
---
Jerome Marchand 3772ca
 src/tracefs-filter.c | 6 ++++--
Jerome Marchand 3772ca
 1 file changed, 4 insertions(+), 2 deletions(-)
Jerome Marchand 3772ca
Jerome Marchand 3772ca
diff --git a/src/tracefs-filter.c b/src/tracefs-filter.c
Jerome Marchand 3772ca
index afe3338..1b1c60e 100644
Jerome Marchand 3772ca
--- a/src/tracefs-filter.c
Jerome Marchand 3772ca
+++ b/src/tracefs-filter.c
Jerome Marchand 3772ca
@@ -250,12 +250,12 @@ static int append_filter(char **filter, unsigned int *state,
Jerome Marchand 3772ca
 	case TRACEFS_COMPARE_NE: tmp = append_string(tmp, NULL, " != "); break;
Jerome Marchand 3772ca
 	case TRACEFS_COMPARE_RE:
Jerome Marchand 3772ca
 		if (!is_string)
Jerome Marchand 3772ca
-			goto inval;
Jerome Marchand 3772ca
+			goto free;
Jerome Marchand 3772ca
 		tmp = append_string(tmp, NULL, "~");
Jerome Marchand 3772ca
 		break;
Jerome Marchand 3772ca
 	default:
Jerome Marchand 3772ca
 		if (is_string)
Jerome Marchand 3772ca
-			goto inval;
Jerome Marchand 3772ca
+			goto free;
Jerome Marchand 3772ca
 	}
Jerome Marchand 3772ca
 
Jerome Marchand 3772ca
 	switch (compare) {
Jerome Marchand 3772ca
@@ -277,6 +277,8 @@ static int append_filter(char **filter, unsigned int *state,
Jerome Marchand 3772ca
 	*state = S_COMPARE;
Jerome Marchand 3772ca
 
Jerome Marchand 3772ca
 	return 0;
Jerome Marchand 3772ca
+free:
Jerome Marchand 3772ca
+	free(tmp);
Jerome Marchand 3772ca
 inval:
Jerome Marchand 3772ca
 	errno = EINVAL;
Jerome Marchand 3772ca
 	return -1;
Jerome Marchand 3772ca
-- 
Jerome Marchand 3772ca
2.45.2
Jerome Marchand 3772ca