Blame SOURCES/openscap-1.3.4-rpmverifyfile_leak-PR_1565.patch

901647
From 4ef60df7edfdd7a49a565494142f86d93f9268b3 Mon Sep 17 00:00:00 2001
901647
From: =?UTF-8?q?Jan=20=C4=8Cern=C3=BD?= <jcerny@redhat.com>
901647
Date: Fri, 31 Jul 2020 10:38:17 +0200
901647
Subject: [PATCH] Plug a memory leak
901647
901647
==12029==    at 0x483A809: malloc (vg_replace_malloc.c:307)
901647
==12029==    by 0x51F1386: realpath@@GLIBC_2.3 (in /usr/lib64/libc-2.31.so)
901647
==12029==    by 0x489F8CA: oscap_realpath (util.c:251)
901647
==12029==    by 0x495E6EF: rpmverify_collect (rpmverifyfile_probe.c:248)
901647
==12029==    by 0x495F461: rpmverifyfile_probe_main (rpmverifyfile_probe.c:543)
901647
==12029==    by 0x4935598: probe_worker (worker.c:1090)
901647
==12029==    by 0x4932F10: probe_worker_runfn (worker.c:81)
901647
==12029==    by 0x4CDA431: start_thread (in /usr/lib64/libpthread-2.31.so)
901647
==12029==    by 0x52A8912: clone (in /usr/lib64/libc-2.31.so)
901647
901647
==12029==    at 0x483CCE8: realloc (vg_replace_malloc.c:834)
901647
==12029==    by 0x4D9DCD8: rrealloc (in /usr/lib64/librpmio.so.9.0.1)
901647
==12029==    by 0x4D25B88: headerFormat (in /usr/lib64/librpm.so.9.0.1)
901647
==12029==    by 0x495E467: rpmverify_collect (rpmverifyfile_probe.c:230)
901647
==12029==    by 0x495F461: rpmverifyfile_probe_main
901647
(rpmverifyfile_probe.c:543)
901647
==12029==    by 0x4935598: probe_worker (worker.c:1090)
901647
==12029==    by 0x4932F10: probe_worker_runfn (worker.c:81)
901647
==12029==    by 0x4CDA431: start_thread (in
901647
/usr/lib64/libpthread-2.31.so)
901647
==12029==    by 0x52A8912: clone (in /usr/lib64/libc-2.31.so)
901647
901647
Resolves: RHBZ#1861301
901647
---
901647
 .../probes/unix/linux/rpmverifyfile_probe.c   | 24 ++++++++++++++-----
901647
 1 file changed, 18 insertions(+), 6 deletions(-)
901647
901647
diff --git a/src/OVAL/probes/unix/linux/rpmverifyfile_probe.c b/src/OVAL/probes/unix/linux/rpmverifyfile_probe.c
901647
index c86818e72..57d69f552 100644
901647
--- a/src/OVAL/probes/unix/linux/rpmverifyfile_probe.c
901647
+++ b/src/OVAL/probes/unix/linux/rpmverifyfile_probe.c
901647
@@ -61,10 +61,10 @@
901647
 
901647
 struct rpmverify_res {
901647
 	char *name;  /**< package name */
901647
-	const char *epoch;
901647
-	const char *version;
901647
-	const char *release;
901647
-	const char *arch;
901647
+	char *epoch;
901647
+	char *version;
901647
+	char *release;
901647
+	char *arch;
901647
 	char *file;  /**< filepath */
901647
 	char extended_name[1024];
901647
 	rpmVerifyAttrs vflags; /**< rpm verify flags */
901647
@@ -272,14 +272,14 @@ static int rpmverify_collect(probe_ctx *ctx,
901647
 						free(current_file_realpath);
901647
 						continue;
901647
 					}
901647
-					res.file = current_file_realpath ? current_file_realpath : strdup(current_file);
901647
+					res.file = current_file_realpath ? oscap_strdup(current_file_realpath) : oscap_strdup(current_file);
901647
 		      break;
901647
 		    case OVAL_OPERATION_PATTERN_MATCH:
901647
 					ret = pcre_exec(re, NULL, current_file, strlen(current_file), 0, 0, NULL, 0);
901647
 
901647
 		      switch(ret) {
901647
 		      case 0: /* match */
901647
-						res.file = strdup(current_file);
901647
+						res.file = oscap_strdup(current_file);
901647
 			break;
901647
 		      case -1:
901647
 			/* mismatch */
901647
@@ -299,12 +299,18 @@ static int rpmverify_collect(probe_ctx *ctx,
901647
 						free(current_file_realpath);
901647
 		      goto ret;
901647
 		    }
901647
+		    free(current_file_realpath);
901647
 
901647
 			if (rpmVerifyFile(g_rpm->rpmts, fi, &res.vflags, omit) != 0)
901647
 		      res.vflags = RPMVERIFY_FAILURES;
901647
 
901647
 		    if (callback(ctx, &res) != 0) {
901647
 			    ret = 0;
901647
+					free(res.name);
901647
+					free(res.epoch);
901647
+					free(res.version);
901647
+					free(res.release);
901647
+					free(res.arch);
901647
 					free(res.file);
901647
 			    goto ret;
901647
 		    }
901647
@@ -313,6 +319,12 @@ static int rpmverify_collect(probe_ctx *ctx,
901647
 
901647
 		  rpmfiFree(fi);
901647
 		}
901647
+
901647
+		free(res.name);
901647
+		free(res.epoch);
901647
+		free(res.version);
901647
+		free(res.release);
901647
+		free(res.arch);
901647
 	}
901647
 
901647
 	match = rpmdbFreeIterator (match);
901647
-- 
901647
2.26.2
901647