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

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