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

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