Blame SOURCES/0018-Fix-possible-NULL-ptr-dereferences-and-memory-leaks.patch

c0f891
From 626bc45396c4959f2c4685c2faa7c4f553f4efdf Mon Sep 17 00:00:00 2001
c0f891
From: Mateusz Grzonka <mateusz.grzonka@intel.com>
c0f891
Date: Mon, 13 Jun 2022 11:59:34 +0200
37f2b0
Subject: [PATCH 18/83] Fix possible NULL ptr dereferences and memory leaks
c0f891
c0f891
In Assemble there was a NULL check for sra variable,
c0f891
which effectively didn't stop the execution in every case.
c0f891
That might have resulted in a NULL pointer dereference.
c0f891
c0f891
Also in super-ddf, mu variable was set to NULL for some condition,
c0f891
and then immidiately dereferenced.
c0f891
Additionally some memory wasn't freed as well.
c0f891
c0f891
Signed-off-by: Mateusz Grzonka <mateusz.grzonka@intel.com>
c0f891
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
c0f891
---
c0f891
 Assemble.c  | 7 ++++++-
c0f891
 super-ddf.c | 9 +++++++--
c0f891
 2 files changed, 13 insertions(+), 3 deletions(-)
c0f891
c0f891
diff --git a/Assemble.c b/Assemble.c
c0f891
index 9eac9ce0..4b213560 100644
c0f891
--- a/Assemble.c
c0f891
+++ b/Assemble.c
c0f891
@@ -1982,7 +1982,12 @@ int assemble_container_content(struct supertype *st, int mdfd,
c0f891
 	}
c0f891
 
c0f891
 	sra = sysfs_read(mdfd, NULL, GET_VERSION|GET_DEVS);
c0f891
-	if (sra == NULL || strcmp(sra->text_version, content->text_version) != 0) {
c0f891
+	if (sra == NULL) {
c0f891
+		pr_err("Failed to read sysfs parameters\n");
c0f891
+		return 1;
c0f891
+	}
c0f891
+
c0f891
+	if (strcmp(sra->text_version, content->text_version) != 0) {
c0f891
 		if (content->array.major_version == -1 &&
c0f891
 		    content->array.minor_version == -2 &&
c0f891
 		    c->readonly &&
c0f891
diff --git a/super-ddf.c b/super-ddf.c
c0f891
index 8cda23a7..abbc8b09 100644
c0f891
--- a/super-ddf.c
c0f891
+++ b/super-ddf.c
c0f891
@@ -5125,13 +5125,16 @@ static struct mdinfo *ddf_activate_spare(struct active_array *a,
c0f891
 	 */
c0f891
 	vc = find_vdcr(ddf, a->info.container_member, rv->disk.raid_disk,
c0f891
 		       &n_bvd, &vcl;;
c0f891
-	if (vc == NULL)
c0f891
+	if (vc == NULL) {
c0f891
+		free(rv);
c0f891
 		return NULL;
c0f891
+	}
c0f891
 
c0f891
 	mu = xmalloc(sizeof(*mu));
c0f891
 	if (posix_memalign(&mu->space, 512, sizeof(struct vcl)) != 0) {
c0f891
 		free(mu);
c0f891
-		mu = NULL;
c0f891
+		free(rv);
c0f891
+		return NULL;
c0f891
 	}
c0f891
 
c0f891
 	mu->len = ddf->conf_rec_len * 512 * vcl->conf.sec_elmnt_count;
c0f891
@@ -5161,6 +5164,8 @@ static struct mdinfo *ddf_activate_spare(struct active_array *a,
c0f891
 			pr_err("BUG: can't find disk %d (%d/%d)\n",
c0f891
 			       di->disk.raid_disk,
c0f891
 			       di->disk.major, di->disk.minor);
c0f891
+			free(mu);
c0f891
+			free(rv);
c0f891
 			return NULL;
c0f891
 		}
c0f891
 		vc->phys_refnum[i_prim] = ddf->phys->entries[dl->pdnum].refnum;
c0f891
-- 
37f2b0
2.38.1
c0f891