Skip to contentSkip to conversation

Incremental: Fix possible memory and resource leaks

PATCH Superseded Mateusz Grzonka 3 participants 6 messages Nov 24, 2021 – Jan 3, 2022 MD RAID

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Incremental: Fix possible memory and resource leaks

map allocated through map_by_uuid() is not freed if mdfd is invalid. In addition mdfd is not closed, and mdinfo list is not freed too.

---
 Incremental.c | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)
Incremental.c +23 -9
index cd9cc0fc..01554efc 100644
@@ -1498,7 +1498,7 @@ static int Incremental_container(struct supertype *st, char *devname,
 		return 0;
 	}
 	for (ra = list ; ra ; ra = ra->next) {
-		int mdfd;
+		int mdfd = -1;
 		char chosen_name[1024];
 		struct map_ent *mp;
 		struct mddev_ident *match = NULL;
@@ -1513,6 +1513,12 @@ static int Incremental_container(struct supertype *st, char *devname,
 
 		if (mp) {
 			mdfd = open_dev(mp->devnm);
+			if (!is_fd_valid(mdfd)) {
+				pr_err("failed to open %s: %s.\n",
+				       mp->devnm, strerror(errno));
+				rv = 2;
+				goto release;
+			}
 			if (mp->path)
 				strcpy(chosen_name, mp->path);
 			else
@@ -1572,21 +1578,25 @@ static int Incremental_container(struct supertype *st, char *devname,
 					    c->autof,
 					    trustworthy,
 					    chosen_name, 0);
+
+			if (!is_fd_valid(mdfd)) {
+				pr_err("create_mddev failed with chosen name %s: %s.\n",
+				       chosen_name, strerror(errno));
+				rv = 2;
+				goto release;
+			}
 		}
-		if (only && (!mp || strcmp(mp->devnm, only) != 0))
-			continue;
 
-		if (mdfd < 0) {
-			pr_err("failed to open %s: %s.\n",
-				chosen_name, strerror(errno));
-			return 2;
+		if (only && (!mp || strcmp(mp->devnm, only) != 0)) {
+			close_fd(&mdfd);
+			continue;
 		}
 
 		assemble_container_content(st, mdfd, ra, c,
 					   chosen_name, &result);
 		map_free(map);
 		map = NULL;
-		close(mdfd);
+		close_fd(&mdfd);
 	}
 	if (c->export && result) {
 		char sep = '=';
@@ -1609,7 +1619,11 @@ static int Incremental_container(struct supertype *st, char *devname,
 		}
 		printf("\n");
 	}
-	return 0;
+
+release:
+	map_free(map);
+	sysfs_free(list);
+	return rv;
 }
 
 static void run_udisks(char *arg1, char *arg2)
Signature · 1 line
2.26.2
#1 lore raw
Quoted 7 lines · Mateusz Grzonka

map allocated through map_by_uuid() is not freed if mdfd is invalid. In addition mdfd is not closed, and mdinfo list is not freed too.

---
 Incremental.c | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)

I already applied the previous version. Could you please send an updated version on top of current tree.

Thanks,
Jes

Quoted 73 lines
Incremental.c +23 -9
index cd9cc0fc..01554efc 100644
@@ -1498,7 +1498,7 @@ static int Incremental_container(struct supertype *st, char *devname,
 		return 0;
 	}
 	for (ra = list ; ra ; ra = ra->next) {
-		int mdfd;
+		int mdfd = -1;
 		char chosen_name[1024];
 		struct map_ent *mp;
 		struct mddev_ident *match = NULL;
@@ -1513,6 +1513,12 @@ static int Incremental_container(struct supertype *st, char *devname,
 
 		if (mp) {
 			mdfd = open_dev(mp->devnm);
+			if (!is_fd_valid(mdfd)) {
+				pr_err("failed to open %s: %s.\n",
+				       mp->devnm, strerror(errno));
+				rv = 2;
+				goto release;
+			}
 			if (mp->path)
 				strcpy(chosen_name, mp->path);
 			else
@@ -1572,21 +1578,25 @@ static int Incremental_container(struct supertype *st, char *devname,
 					    c->autof,
 					    trustworthy,
 					    chosen_name, 0);
+
+			if (!is_fd_valid(mdfd)) {
+				pr_err("create_mddev failed with chosen name %s: %s.\n",
+				       chosen_name, strerror(errno));
+				rv = 2;
+				goto release;
+			}
 		}
-		if (only && (!mp || strcmp(mp->devnm, only) != 0))
-			continue;
 
-		if (mdfd < 0) {
-			pr_err("failed to open %s: %s.\n",
-				chosen_name, strerror(errno));
-			return 2;
+		if (only && (!mp || strcmp(mp->devnm, only) != 0)) {
+			close_fd(&mdfd);
+			continue;
 		}
 
 		assemble_container_content(st, mdfd, ra, c,
 					   chosen_name, &result);
 		map_free(map);
 		map = NULL;
-		close(mdfd);
+		close_fd(&mdfd);
 	}
 	if (c->export && result) {
 		char sep = '=';
@@ -1609,7 +1619,11 @@ static int Incremental_container(struct supertype *st, char *devname,
 		}
 		printf("\n");
 	}
-	return 0;
+
+release:
+	map_free(map);
+	sysfs_free(list);
+	return rv;
 }
 
 static void run_udisks(char *arg1, char *arg2)
#2 lore raw
Quoted 9 lines · Jes Sorensen
Quoted 7 lines · Mateusz Grzonka

map allocated through map_by_uuid() is not freed if mdfd is invalid. In addition mdfd is not closed, and mdinfo list is not freed too.

---
  Incremental.c | 32 +++++++++++++++++++++++---------
  1 file changed, 23 insertions(+), 9 deletions(-)
Quoted 6 lines

I already applied the previous version. Could you please send an updated version on top of current tree.

Thanks,
Jes

Hi Jes,
I cannot see previous version in mdadm tree.
Could you verify?

Thanks,
Mariusz

#3 lore raw
Quoted 13 lines · Mariusz
Quoted 12 lines · Jes Sorensen
Quoted 7 lines · Mateusz Grzonka

map allocated through map_by_uuid() is not freed if mdfd is invalid. In addition mdfd is not closed, and mdinfo list is not freed too.

---
  Incremental.c | 32 +++++++++++++++++++++++---------

  1 file changed, 23 insertions(+), 9 deletions(-)

I already applied the previous version. Could you please send an updated version on top of current tree.

Quoted 7 lines
Quoted 3 lines

Thanks,
Jes

Hi Jes,
I cannot see previous version in mdadm tree.
Could you verify?

You may have been too quick and it hadn't propagated yet. Mind trying one more time?

Thanks,
Jes

#4 lore raw
Quoted 18 lines · Jes Sorensen
Quoted 17 lines · Mariusz
Quoted 15 lines · Jes Sorensen
Quoted 7 lines · Mateusz Grzonka

map allocated through map_by_uuid() is not freed if mdfd is invalid. In addition mdfd is not closed, and mdinfo list is not freed too.

---
  Incremental.c | 32 +++++++++++++++++++++++---------

  1 file changed, 23 insertions(+), 9 deletions(-)

I already applied the previous version. Could you please send an updated version on top of current tree.

Thanks,
Jes

Quoted 8 lines
Quoted 3 lines

Hi Jes,
I cannot see previous version in mdadm tree.
Could you verify?

You may have been too quick and it hadn't propagated yet. Mind trying one more time?

Thanks,
Jes

Hi Jes,
I split previous "[PATCH] Incremental: Fix possible memory and resource leaks" into 3 separate patches as suggested.
Two of them, that were created as a result of cutting things from "[PATCH] Incremental: Fix possible memory and resource leaks", were merged yesterday. However this patch (as well as its previous version) was not.

Regards,
Mateusz

#5 lore raw
Quoted 28 lines · Mateusz Grzonka
Quoted 18 lines · Jes Sorensen
Quoted 17 lines · Mariusz
Quoted 15 lines · Jes Sorensen
Quoted 7 lines · Mateusz Grzonka

map allocated through map_by_uuid() is not freed if mdfd is invalid. In addition mdfd is not closed, and mdinfo list is not freed too.

---
  Incremental.c | 32 +++++++++++++++++++++++---------

  1 file changed, 23 insertions(+), 9 deletions(-)

I already applied the previous version. Could you please send an updated version on top of current tree.

Thanks,
Jes

Quoted 8 lines
Quoted 3 lines

Hi Jes,
I cannot see previous version in mdadm tree.
Could you verify?

You may have been too quick and it hadn't propagated yet. Mind trying one more time?

Thanks,
Jes

Quoted 7 lines

Hi Jes,
I split previous "[PATCH] Incremental: Fix possible memory and resource leaks" into 3 separate patches as suggested.
Two of them, that were created as a result of cutting things from "[PATCH] Incremental: Fix possible memory and resource leaks", were merged yesterday. However this patch (as well as its previous version) was not.

Regards,
Mateusz

Hi Jes,
any updates on this one?