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

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