Blame SOURCES/0015-Incremental-Fix-possible-memory-and-resource-leaks.patch

cd8c44
From de064c93e3819d72720e4fba6575265ba10e1553 Mon Sep 17 00:00:00 2001
cd8c44
From: Mateusz Grzonka <mateusz.grzonka@intel.com>
cd8c44
Date: Mon, 13 Jun 2022 12:11:25 +0200
2b63fb
Subject: [PATCH 15/83] Incremental: Fix possible memory and resource leaks
cd8c44
cd8c44
map allocated through map_by_uuid() is not freed if mdfd is invalid.
cd8c44
In addition mdfd is not closed, and mdinfo list is not freed too.
cd8c44
cd8c44
Signed-off-by: Mateusz Grzonka <mateusz.grzonka@intel.com>
cd8c44
Change-Id: I25e726f0e2502cf7e8ce80c2bd7944b3b1e2b9dc
cd8c44
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
cd8c44
---
cd8c44
 Incremental.c | 32 +++++++++++++++++++++++---------
cd8c44
 1 file changed, 23 insertions(+), 9 deletions(-)
cd8c44
cd8c44
diff --git a/Incremental.c b/Incremental.c
cd8c44
index a57fc323..4d0cd9d6 100644
cd8c44
--- a/Incremental.c
cd8c44
+++ b/Incremental.c
cd8c44
@@ -1499,7 +1499,7 @@ static int Incremental_container(struct supertype *st, char *devname,
cd8c44
 		return 0;
cd8c44
 	}
cd8c44
 	for (ra = list ; ra ; ra = ra->next) {
cd8c44
-		int mdfd;
cd8c44
+		int mdfd = -1;
cd8c44
 		char chosen_name[1024];
cd8c44
 		struct map_ent *mp;
cd8c44
 		struct mddev_ident *match = NULL;
cd8c44
@@ -1514,6 +1514,12 @@ static int Incremental_container(struct supertype *st, char *devname,
cd8c44
 
cd8c44
 		if (mp) {
cd8c44
 			mdfd = open_dev(mp->devnm);
cd8c44
+			if (!is_fd_valid(mdfd)) {
cd8c44
+				pr_err("failed to open %s: %s.\n",
cd8c44
+				       mp->devnm, strerror(errno));
cd8c44
+				rv = 2;
cd8c44
+				goto release;
cd8c44
+			}
cd8c44
 			if (mp->path)
cd8c44
 				strcpy(chosen_name, mp->path);
cd8c44
 			else
cd8c44
@@ -1573,21 +1579,25 @@ static int Incremental_container(struct supertype *st, char *devname,
cd8c44
 					    c->autof,
cd8c44
 					    trustworthy,
cd8c44
 					    chosen_name, 0);
cd8c44
+
cd8c44
+			if (!is_fd_valid(mdfd)) {
cd8c44
+				pr_err("create_mddev failed with chosen name %s: %s.\n",
cd8c44
+				       chosen_name, strerror(errno));
cd8c44
+				rv = 2;
cd8c44
+				goto release;
cd8c44
+			}
cd8c44
 		}
cd8c44
-		if (only && (!mp || strcmp(mp->devnm, only) != 0))
cd8c44
-			continue;
cd8c44
 
cd8c44
-		if (mdfd < 0) {
cd8c44
-			pr_err("failed to open %s: %s.\n",
cd8c44
-				chosen_name, strerror(errno));
cd8c44
-			return 2;
cd8c44
+		if (only && (!mp || strcmp(mp->devnm, only) != 0)) {
cd8c44
+			close_fd(&mdfd);
cd8c44
+			continue;
cd8c44
 		}
cd8c44
 
cd8c44
 		assemble_container_content(st, mdfd, ra, c,
cd8c44
 					   chosen_name, &result);
cd8c44
 		map_free(map);
cd8c44
 		map = NULL;
cd8c44
-		close(mdfd);
cd8c44
+		close_fd(&mdfd);
cd8c44
 	}
cd8c44
 	if (c->export && result) {
cd8c44
 		char sep = '=';
cd8c44
@@ -1610,7 +1620,11 @@ static int Incremental_container(struct supertype *st, char *devname,
cd8c44
 		}
cd8c44
 		printf("\n");
cd8c44
 	}
cd8c44
-	return 0;
cd8c44
+
cd8c44
+release:
cd8c44
+	map_free(map);
cd8c44
+	sysfs_free(list);
cd8c44
+	return rv;
cd8c44
 }
cd8c44
 
cd8c44
 static void run_udisks(char *arg1, char *arg2)
cd8c44
-- 
2b63fb
2.38.1
cd8c44