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

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