Skip to contentSkip to conversation

libmultipath: free pgp if add_pathgroup fails in disassemble_map func

PATCH In review Zhiqiang Liu 2 participants 3 messages Jul 22, 2020 – Jul 23, 2020 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libmultipath: free pgp if add_pathgroup fails in disassemble_map func

In disassemble_map func, pgp will be added to mpp->pg by calling add_pathgroup after allocing a pathgroup (pgp) successfully. However, if add_pathgroup fails, the pgp is actually not inserted into mpp->pg. So, calling free_pgvec(mpp->pg) cannot free the pgp, then memory leak problem occurs.

disassemble_map:
-> pgp = alloc_pathgroup()
-> if add_pathgroup(mpp, pgp) fails

	-> goto out

out:
free_pgvec(mpp->pg, KEEP_PATHS);

Here, we will call free_pathgroup(pgp) before going to out tag.

---
 libmultipath/dmparser.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libmultipath/dmparser.c +3 -1
index ac13ec06..6225838b 100644
@@ -268,8 +268,10 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 		if (!pgp)
 			goto out;
 
-		if (add_pathgroup(mpp, pgp))
+		if (add_pathgroup(mpp, pgp)) {
+			free_pathgroup(pgp, KEEP_PATHS);
 			goto out;
+		}
 
 		p += get_word(p, &word);
 
Signature · 1 line
2.24.0.windows.2
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Zhiqiang Liu

In disassemble_map func, pgp will be added to mpp->pg by calling add_pathgroup after allocing a pathgroup (pgp) successfully. However, if add_pathgroup fails, the pgp is actually not inserted into mpp->pg. So, calling free_pgvec(mpp->pg) cannot free the pgp, then memory leak problem occurs.

Quoted 9 lines

disassemble_map:
-> pgp = alloc_pathgroup()
-> if add_pathgroup(mpp, pgp) fails

	-> goto out

out:
free_pgvec(mpp->pg, KEEP_PATHS);

Here, we will call free_pathgroup(pgp) before going to out tag.

Quoted 25 lines
---
 libmultipath/dmparser.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libmultipath/dmparser.c +3 -1
index ac13ec06..6225838b 100644
@@ -268,8 +268,10 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 		if (!pgp)
 			goto out;
 
-		if (add_pathgroup(mpp, pgp))
+		if (add_pathgroup(mpp, pgp)) {
+			free_pathgroup(pgp, KEEP_PATHS);
 			goto out;
+		}
 
 		p += get_word(p, &word);
 
Signature · 1 line
2.24.0.windows.2
#2 lore raw

Thanks for your review.

Quoted 48 lines · Benjamin Marzinski
Quoted 6 lines · Zhiqiang Liu

In disassemble_map func, pgp will be added to mpp->pg by calling add_pathgroup after allocing a pathgroup (pgp) successfully. However, if add_pathgroup fails, the pgp is actually not inserted into mpp->pg. So, calling free_pgvec(mpp->pg) cannot free the pgp, then memory leak problem occurs.

Quoted 9 lines

disassemble_map:
-> pgp = alloc_pathgroup()
-> if add_pathgroup(mpp, pgp) fails

	-> goto out

out:
free_pgvec(mpp->pg, KEEP_PATHS);

Here, we will call free_pathgroup(pgp) before going to out tag.

Quoted 25 lines
---
 libmultipath/dmparser.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libmultipath/dmparser.c +3 -1
index ac13ec06..6225838b 100644
@@ -268,8 +268,10 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 		if (!pgp)
 			goto out;
 
-		if (add_pathgroup(mpp, pgp))
+		if (add_pathgroup(mpp, pgp)) {
+			free_pathgroup(pgp, KEEP_PATHS);
 			goto out;
+		}
 
 		p += get_word(p, &word);
 
Signature · 1 line
2.24.0.windows.2

.