Skip to contentSkip to conversation

libmultipath: free pp if store_path fails in disassemble_map

PATCH In review Zhiqiang Liu 3 participants 6 messages Jul 24, 2020 – Aug 10, 2020 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 libmultipath: free pp if store_path fails in disassemble_map

In disassemble_map func, one pp will be allocated and stored in pgp->paths. However, if store_path fails, pp will not be freed, then memory leak problem occurs.

Here, we will call free_path to free pp when store_path fails.

---
V1->V2: update based on ups/submit-2007 branch.

 libmultipath/dmparser.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
libmultipath/dmparser.c +7 -2
index b9858fa5..8a0501ba 100644
@@ -143,6 +143,7 @@ int disassemble_map(const struct _vector *pathvec,
 	int def_minio = 0;
 	struct path * pp;
 	struct pathgroup * pgp;
+	int pp_alloc_flag = 0;
 
 	assert(pathvec != NULL);
 	p = params;
@@ -292,6 +293,7 @@ int disassemble_map(const struct _vector *pathvec,
 
 		for (j = 0; j < num_paths; j++) {
 			pp = NULL;
+			pp_alloc_flag = 0;
 			p += get_word(p, &word);
 
 			if (!word)
@@ -304,13 +306,16 @@ int disassemble_map(const struct _vector *pathvec,
 
 				if (!pp)
 					goto out1;
-
+				pp_alloc_flag = 1;
 				strlcpy(pp->dev_t, word, BLK_DEV_SIZE);
 			}
 			FREE(word);
 
-			if (store_path(pgp->paths, pp))
+			if (store_path(pgp->paths, pp)) {
+				if (pp_alloc_flag)
+					free_path(pp);
 				goto out;
+			}
 
 			pgp->id ^= (long)pp;
 			pp->pgindex = i + 1;
Signature · 1 line
2.24.0.windows.2
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · Zhiqiang Liu

In disassemble_map func, one pp will be allocated and stored in pgp->paths. However, if store_path fails, pp will not be freed, then memory leak problem occurs.

Here, we will call free_path to free pp when store_path fails.

Quoted 50 lines

---
V1->V2: update based on ups/submit-2007 branch.

 libmultipath/dmparser.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
libmultipath/dmparser.c +7 -2
index b9858fa5..8a0501ba 100644
@@ -143,6 +143,7 @@ int disassemble_map(const struct _vector *pathvec,
 	int def_minio = 0;
 	struct path * pp;
 	struct pathgroup * pgp;
+	int pp_alloc_flag = 0;
 
 	assert(pathvec != NULL);
 	p = params;
@@ -292,6 +293,7 @@ int disassemble_map(const struct _vector *pathvec,
 
 		for (j = 0; j < num_paths; j++) {
 			pp = NULL;
+			pp_alloc_flag = 0;
 			p += get_word(p, &word);
 
 			if (!word)
@@ -304,13 +306,16 @@ int disassemble_map(const struct _vector *pathvec,
 
 				if (!pp)
 					goto out1;
-
+				pp_alloc_flag = 1;
 				strlcpy(pp->dev_t, word, BLK_DEV_SIZE);
 			}
 			FREE(word);
 
-			if (store_path(pgp->paths, pp))
+			if (store_path(pgp->paths, pp)) {
+				if (pp_alloc_flag)
+					free_path(pp);
 				goto out;
+			}
 
 			pgp->id ^= (long)pp;
 			pp->pgindex = i + 1;
Signature · 1 line
2.24.0.windows.2
#2 lore raw
In reply to Zhiqiang Liu

Hello Liu,

Quoted 6 lines · Zhiqiang Liu

In disassemble_map func, one pp will be allocated and stored in pgp->paths. However, if store_path fails, pp will not be freed, then memory leak problem occurs.

Here, we will call free_path to free pp when store_path fails.

Quoted 8 lines

---
V1->V2: update based on ups/submit-2007 branch.

 libmultipath/dmparser.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

thanks for the patch. I'd suggest to do this without the pp_alloc_flag variable, by pulling the store_path() call into the if (!pp) conditional and treating failure differently in both branches of the conditional. (Side note: if we introduce a boolean like this, we should use "bool" as the variable type).

Unless you object, I'll add that change on top of my submit-2007 series, giving you appropriate credits.

@Ben, @Christophe: I've been considering for some time to start handling cases like this with __attribute__((cleanup(f)))) (
https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html).
That could reduce the amount of goto clauses for error handling significantly. It would be a major change in coding style though. What do you think?

Regards,
Martin

Quoted 42 lines
libmultipath/dmparser.c +1 -0
index b9858fa5..8a0501ba 100644
@@ -143,6 +143,7 @@ int disassemble_map(const struct _vector
 *pathvec,
 	int def_minio = 0;
 	struct path * pp;
 	struct pathgroup * pgp;
+	int pp_alloc_flag = 0;
 
 	assert(pathvec != NULL);
libmultipath/dmparser.c +1 -0
 	p = params;
@@ -292,6 +293,7 @@ int disassemble_map(const struct _vector
 *pathvec,
 
 		for (j = 0; j < num_paths; j++) {
 			pp = NULL;
+			pp_alloc_flag = 0;
 			p += get_word(p, &word);
 
libmultipath/dmparser.c +5 -2
 			if (!word)
@@ -304,13 +306,16 @@ int disassemble_map(const struct _vector
 *pathvec,
 
 				if (!pp)
 					goto out1;
-
+				pp_alloc_flag = 1;
 				strlcpy(pp->dev_t, word, BLK_DEV_SIZE);
 			}
 			FREE(word);
 
-			if (store_path(pgp->paths, pp))
+			if (store_path(pgp->paths, pp)) {
+				if (pp_alloc_flag)
+					free_path(pp);
 				goto out;
+			}
 
 			pgp->id ^= (long)pp;

pp->pgindex = i + 1;

#3 lore raw
Quoted 18 lines · Martin Wilck

Hello Liu,

Quoted 6 lines · Zhiqiang Liu

In disassemble_map func, one pp will be allocated and stored in pgp->paths. However, if store_path fails, pp will not be freed, then memory leak problem occurs.

Here, we will call free_path to free pp when store_path fails.

Quoted 8 lines

---
V1->V2: update based on ups/submit-2007 branch.

 libmultipath/dmparser.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
Quoted 9 lines

thanks for the patch. I'd suggest to do this without the pp_alloc_flag variable, by pulling the store_path() call into the if (!pp) conditional and treating failure differently in both branches of the conditional. (Side note: if we introduce a boolean like this, we should use "bool" as the variable type).

Unless you object, I'll add that change on top of my submit-2007 series, giving you appropriate credits.

Thanks for you review.

You are free to deal with the patch.

Quoted 58 lines

@Ben, @Christophe: I've been considering for some time to start handling cases like this with __attribute__((cleanup(f)))) (
https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html).
That could reduce the amount of goto clauses for error handling significantly. It would be a major change in coding style though. What do you think?

Regards,
Martin

Quoted 41 lines
libmultipath/dmparser.c +1 -0
index b9858fa5..8a0501ba 100644
@@ -143,6 +143,7 @@ int disassemble_map(const struct _vector
 *pathvec,
 	int def_minio = 0;
 	struct path * pp;
 	struct pathgroup * pgp;
+	int pp_alloc_flag = 0;
 
 	assert(pathvec != NULL);
libmultipath/dmparser.c +1 -0
 	p = params;
@@ -292,6 +293,7 @@ int disassemble_map(const struct _vector
 *pathvec,
 
 		for (j = 0; j < num_paths; j++) {
 			pp = NULL;
+			pp_alloc_flag = 0;
 			p += get_word(p, &word);
 
libmultipath/dmparser.c +5 -2
 			if (!word)
@@ -304,13 +306,16 @@ int disassemble_map(const struct _vector
 *pathvec,
 
 				if (!pp)
 					goto out1;
-
+				pp_alloc_flag = 1;
 				strlcpy(pp->dev_t, word, BLK_DEV_SIZE);
 			}
 			FREE(word);
 
-			if (store_path(pgp->paths, pp))
+			if (store_path(pgp->paths, pp)) {
+				if (pp_alloc_flag)
+					free_path(pp);
 				goto out;
+			}
 
 			pgp->id ^= (long)pp;
Quoted 1 line

pp->pgindex = i + 1;

.

#4 lore raw
In reply to Martin Wilck
Quoted 27 lines · Martin Wilck

Hello Liu,

Quoted 6 lines · Zhiqiang Liu

In disassemble_map func, one pp will be allocated and stored in pgp->paths. However, if store_path fails, pp will not be freed, then memory leak problem occurs.

Here, we will call free_path to free pp when store_path fails.

Quoted 8 lines

---
V1->V2: update based on ups/submit-2007 branch.

 libmultipath/dmparser.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

thanks for the patch. I'd suggest to do this without the pp_alloc_flag variable, by pulling the store_path() call into the if (!pp) conditional and treating failure differently in both branches of the conditional. (Side note: if we introduce a boolean like this, we should use "bool" as the variable type).

Unless you object, I'll add that change on top of my submit-2007 series, giving you appropriate credits.

Quoted 6 lines

@Ben, @Christophe: I've been considering for some time to start handling cases like this with __attribute__((cleanup(f)))) (
https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html).
That could reduce the amount of goto clauses for error handling significantly. It would be a major change in coding style though. What do you think?

So, I haven't really looked into the cleanup attribute. How would it work here? We only want to free the path if we didn't store it. We can't free it if it got stored. Can you disable the cleanup? If we have to make the cleanup function check if the path got stored, that seems like more work than simply using the multiple goto labels like we do now.

-Ben

Quoted 48 lines

Regards,
Martin

Quoted 42 lines
libmultipath/dmparser.c +1 -0
index b9858fa5..8a0501ba 100644
@@ -143,6 +143,7 @@ int disassemble_map(const struct _vector
 *pathvec,
 	int def_minio = 0;
 	struct path * pp;
 	struct pathgroup * pgp;
+	int pp_alloc_flag = 0;
 
 	assert(pathvec != NULL);
libmultipath/dmparser.c +1 -0
 	p = params;
@@ -292,6 +293,7 @@ int disassemble_map(const struct _vector
 *pathvec,
 
 		for (j = 0; j < num_paths; j++) {
 			pp = NULL;
+			pp_alloc_flag = 0;
 			p += get_word(p, &word);
 
libmultipath/dmparser.c +5 -2
 			if (!word)
@@ -304,13 +306,16 @@ int disassemble_map(const struct _vector
 *pathvec,
 
 				if (!pp)
 					goto out1;
-
+				pp_alloc_flag = 1;
 				strlcpy(pp->dev_t, word, BLK_DEV_SIZE);
 			}
 			FREE(word);
 
-			if (store_path(pgp->paths, pp))
+			if (store_path(pgp->paths, pp)) {
+				if (pp_alloc_flag)
+					free_path(pp);
 				goto out;
+			}
 
 			pgp->id ^= (long)pp;

pp->pgindex = i + 1;

#5 lore raw
Quoted 40 lines · Benjamin Marzinski
Quoted 30 lines · Martin Wilck

Hello Liu,

Quoted 6 lines · Zhiqiang Liu

In disassemble_map func, one pp will be allocated and stored in pgp->paths. However, if store_path fails, pp will not be freed, then memory leak problem occurs.

Here, we will call free_path to free pp when store_path fails.

Quoted 8 lines

---
V1->V2: update based on ups/submit-2007 branch.

 libmultipath/dmparser.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

thanks for the patch. I'd suggest to do this without the pp_alloc_flag
variable, by pulling the store_path() call into the if (!pp) conditional and treating failure differently in both branches of the
conditional. (Side note: if we introduce a boolean like this, we should
use "bool" as the variable type).

Unless you object, I'll add that change on top of my submit-2007 series, giving you appropriate credits.

Quoted 8 lines

@Ben, @Christophe: I've been considering for some time to start handling cases like this with __attribute__((cleanup(f)))) (
https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html)
.
That could reduce the amount of goto clauses for error handling significantly. It would be a major change in coding style though. What
do you think?

Quoted 7 lines

So, I haven't really looked into the cleanup attribute. How would it work here? We only want to free the path if we didn't store it. We can't
free it if it got stored. Can you disable the cleanup? If we have to make the cleanup function check if the path got stored, that seems like
more work than simply using the multiple goto labels like we do now.

I need to think it through in more detail for this particular use case.

The general pattern makes use of the steal_ptr() macro, and the fact that free(NULL) is a noop:

#define steal_ptr(x) ({ void *__p = x; x = NULL; __p; })

struct xyz;
void cleanup_xyz(struct xyz **p)

{
       free(*p);
}

struct xyz *func()

{
        struct xyz *xy __attribute__((cleanup(cleanup_xyz))) = NULL;

        xy = alloc_xyz();
        if (!xy)
                return;
        if (do_something_with(xy) == SUCCESS)
                /* avoid freeing xy by setting it to NULL */
                return steal_ptr(xy);
        else
                /* xy remains non-NULL and will be freed on return */
                return NULL;
}

More often than not, with this technique, gotos can be avoided completely, and the readability is IMO improved. systemd uses this pattern a lot.

Martin