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

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