Skip to contentSkip to conversation

libmultipath: free pp if store_path fails in disassemble_map

PATCH Superseded Zhiqiang Liu 2 participants 5 messages Jul 22, 2020 – Jul 24, 2020 Device mapper

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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

In disassemble_map func, one pp will be allocated and stored in pathvec (only in client mode) and 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.

---
 libmultipath/dmparser.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
libmultipath/dmparser.c +13 -2
index 6225838b..3dc77242 100644
@@ -142,6 +142,7 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 	int def_minio = 0;
 	struct path * pp;
 	struct pathgroup * pgp;
+	int pp_need_free_flag = 0;
 
 	p = params;
 
@@ -293,6 +294,7 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 			char devname[FILE_NAME_SIZE];
 
 			pp = NULL;
+			pp_need_free_flag = 0;
 			p += get_word(p, &word);
 
 			if (!word)
@@ -323,9 +325,15 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 					strncpy(pp->wwid, mpp->wwid,
 						WWID_SIZE - 1);
 				}
+
+				if (is_daemon)
+					pp_need_free_flag = 1;
+
 				/* Only call this in multipath client mode */
-				if (!is_daemon && store_path(pathvec, pp))
+				if (!is_daemon && store_path(pathvec, pp)) {
+					free_path(pp);
 					goto out1;
+				}
 			} else {
 				if (!strlen(pp->wwid) &&
 				    strlen(mpp->wwid))
@@ -334,8 +342,11 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 			}
 			FREE(word);
 
-			if (store_path(pgp->paths, pp))
+			if (store_path(pgp->paths, pp)) {
+				if (pp_need_free_flag)
+					free_path(pp);
 				goto out;
+			}
 
 			/*
 			 * Update wwid for multipaths which are not setup
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, one pp will be allocated and stored in pathvec (only in client mode) and 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.

However, this will need to get reworked on top of Martin's patches (or incorporated into his next verion patchset).

-Ben

Quoted 59 lines
---
 libmultipath/dmparser.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
libmultipath/dmparser.c +13 -2
index 6225838b..3dc77242 100644
@@ -142,6 +142,7 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 	int def_minio = 0;
 	struct path * pp;
 	struct pathgroup * pgp;
+	int pp_need_free_flag = 0;
 
 	p = params;
 
@@ -293,6 +294,7 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 			char devname[FILE_NAME_SIZE];
 
 			pp = NULL;
+			pp_need_free_flag = 0;
 			p += get_word(p, &word);
 
 			if (!word)
@@ -323,9 +325,15 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 					strncpy(pp->wwid, mpp->wwid,
 						WWID_SIZE - 1);
 				}
+
+				if (is_daemon)
+					pp_need_free_flag = 1;
+
 				/* Only call this in multipath client mode */
-				if (!is_daemon && store_path(pathvec, pp))
+				if (!is_daemon && store_path(pathvec, pp)) {
+					free_path(pp);
 					goto out1;
+				}
 			} else {
 				if (!strlen(pp->wwid) &&
 				    strlen(mpp->wwid))
@@ -334,8 +342,11 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 			}
 			FREE(word);
 
-			if (store_path(pgp->paths, pp))
+			if (store_path(pgp->paths, pp)) {
+				if (pp_need_free_flag)
+					free_path(pp);
 				goto out;
+			}
 
 			/*
 			 * Update wwid for multipaths which are not setup
Signature · 1 line
2.24.0.windows.2
#2 lore raw
Quoted 5 lines · Benjamin Marzinski
Quoted 4 lines · Zhiqiang Liu

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

Quoted 8 lines
Quoted 2 lines

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

However, this will need to get reworked on top of Martin's patches (or incorporated into his next verion patchset).

Thanks for your suggestion.
Which branch should I choose to modify my patch?

Quoted 65 lines

-Ben

Quoted 59 lines
---
 libmultipath/dmparser.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
libmultipath/dmparser.c +13 -2
index 6225838b..3dc77242 100644
@@ -142,6 +142,7 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 	int def_minio = 0;
 	struct path * pp;
 	struct pathgroup * pgp;
+	int pp_need_free_flag = 0;
 
 	p = params;
 
@@ -293,6 +294,7 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 			char devname[FILE_NAME_SIZE];
 
 			pp = NULL;
+			pp_need_free_flag = 0;
 			p += get_word(p, &word);
 
 			if (!word)
@@ -323,9 +325,15 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 					strncpy(pp->wwid, mpp->wwid,
 						WWID_SIZE - 1);
 				}
+
+				if (is_daemon)
+					pp_need_free_flag = 1;
+
 				/* Only call this in multipath client mode */
-				if (!is_daemon && store_path(pathvec, pp))
+				if (!is_daemon && store_path(pathvec, pp)) {
+					free_path(pp);
 					goto out1;
+				}
 			} else {
 				if (!strlen(pp->wwid) &&
 				    strlen(mpp->wwid))
@@ -334,8 +342,11 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 			}
 			FREE(word);
 
-			if (store_path(pgp->paths, pp))
+			if (store_path(pgp->paths, pp)) {
+				if (pp_need_free_flag)
+					free_path(pp);
 				goto out;
+			}
 
 			/*
 			 * Update wwid for multipaths which are not setup
Signature · 1 line
2.24.0.windows.2

.

#3 lore raw
Quoted 11 lines · Zhiqiang Liu
Quoted 8 lines · Benjamin Marzinski
Quoted 6 lines · Zhiqiang Liu

In disassemble_map func, one pp will be allocated and stored in pathvec (only in client mode) and 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
Quoted 5 lines

However, this will need to get reworked on top of Martin's patches (or incorporated into his next verion patchset).

Thanks for your suggestion.
Which branch should I choose to modify my patch?

Martin's latest set of commits is available here:
https://github.com/mwilck/multipath-tools/tree/ups/submit-2007

Quoted 67 lines
Quoted 67 lines

-Ben

Quoted 59 lines
---
 libmultipath/dmparser.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
libmultipath/dmparser.c +13 -2
index 6225838b..3dc77242 100644
@@ -142,6 +142,7 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 	int def_minio = 0;
 	struct path * pp;
 	struct pathgroup * pgp;
+	int pp_need_free_flag = 0;
 
 	p = params;
 
@@ -293,6 +294,7 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 			char devname[FILE_NAME_SIZE];
 
 			pp = NULL;
+			pp_need_free_flag = 0;
 			p += get_word(p, &word);
 
 			if (!word)
@@ -323,9 +325,15 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 					strncpy(pp->wwid, mpp->wwid,
 						WWID_SIZE - 1);
 				}
+
+				if (is_daemon)
+					pp_need_free_flag = 1;
+
 				/* Only call this in multipath client mode */
-				if (!is_daemon && store_path(pathvec, pp))
+				if (!is_daemon && store_path(pathvec, pp)) {
+					free_path(pp);
 					goto out1;
+				}
 			} else {
 				if (!strlen(pp->wwid) &&
 				    strlen(mpp->wwid))
@@ -334,8 +342,11 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 			}
 			FREE(word);
 
-			if (store_path(pgp->paths, pp))
+			if (store_path(pgp->paths, pp)) {
+				if (pp_need_free_flag)
+					free_path(pp);
 				goto out;
+			}
 
 			/*
 			 * Update wwid for multipaths which are not setup
Signature · 1 line
2.24.0.windows.2

.

#4 lore raw
Quoted 18 lines · Benjamin Marzinski
Quoted 17 lines · Zhiqiang Liu
Quoted 13 lines · Benjamin Marzinski
Quoted 6 lines · Zhiqiang Liu

In disassemble_map func, one pp will be allocated and stored in pathvec (only in client mode) and 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.

However, this will need to get reworked on top of Martin's patches (or incorporated into his next verion patchset).

Quoted 8 lines
Quoted 2 lines

Thanks for your suggestion.
Which branch should I choose to modify my patch?

Martin's latest set of commits is available here:
https://github.com/mwilck/multipath-tools/tree/ups/submit-2007

Quoted 2 lines

Thanks for your patience.
I will send the V2 patch based on the branch: ups/submit-2007.

Quoted 69 lines
Quoted 56 lines
Quoted 56 lines

-Ben

Quoted 54 lines
---
 libmultipath/dmparser.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
libmultipath/dmparser.c +13 -2
index 6225838b..3dc77242 100644
@@ -142,6 +142,7 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 	int def_minio = 0;
 	struct path * pp;
 	struct pathgroup * pgp;
+	int pp_need_free_flag = 0;
 
 	p = params;
 
@@ -293,6 +294,7 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 			char devname[FILE_NAME_SIZE];
 
 			pp = NULL;
+			pp_need_free_flag = 0;
 			p += get_word(p, &word);
 
 			if (!word)
@@ -323,9 +325,15 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 					strncpy(pp->wwid, mpp->wwid,
 						WWID_SIZE - 1);
 				}
+
+				if (is_daemon)
+					pp_need_free_flag = 1;
+
 				/* Only call this in multipath client mode */
-				if (!is_daemon && store_path(pathvec, pp))
+				if (!is_daemon && store_path(pathvec, pp)) {
+					free_path(pp);
 					goto out1;
+				}
 			} else {
 				if (!strlen(pp->wwid) &&
 				    strlen(mpp->wwid))
@@ -334,8 +342,11 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 			}
 			FREE(word);
 
-			if (store_path(pgp->paths, pp))
+			if (store_path(pgp->paths, pp)) {
+				if (pp_need_free_flag)
+					free_path(pp);
 				goto out;
+			}
 
Quoted 9 lines
Quoted 9 lines
Quoted 5 lines
libmultipath/dmparser.c +0 -0
 			/*
 			 * Update wwid for multipaths which are not setup
Signature · 1 line
2.24.0.windows.2

.

.