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

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