Skip to contentSkip to conversation

libmultipath: fix memory leaks in coalesce_paths

PATCH Superseded lixiaokeng 3 participants 4 messages Oct 16, 2020 – Oct 19, 2020 Device mapper

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

lixiaokeng

author
#0 lore raw

PATCH 1/1 libmultipath: fix memory leaks in coalesce_paths

When multipath -F are executed firstly and multipath -v2 or -d are executed later, asan will warn memory leaks. The reason is that the mpp allocated in coalesce_paths isn't freed. Here we add newmp in configure(multipath) to store mpp and free it.

---
 libmultipath/configure.c | 12 ++++++++++--
 multipath/main.c         |  7 +++++--
 2 files changed, 15 insertions(+), 4 deletions(-)
libmultipath/configure.c +10 -2
index 6fb477fc..fb2c3f73 100644
@@ -1270,8 +1270,14 @@ int coalesce_paths (struct vectors * vecs, vector newmp, char * refwwid,
 				goto out;
 			}
 		}
-		if (r == DOMAP_DRY)
+		if (r == DOMAP_DRY) {
+			if (!vector_alloc_slot(newmp)) {
+				remove_map(mpp, vecs->pathvec, vecs->mpvec, KEEP_VEC);
+				goto out;
+			}
+			vector_set_slot(newmp, mpp);
 			continue;
+		}
 
 		if (r == DOMAP_EXIST && mpp->action == ACT_NOTHING &&
 		    force_reload == FORCE_RELOAD_WEAK)
@@ -1309,8 +1315,10 @@ int coalesce_paths (struct vectors * vecs, vector newmp, char * refwwid,
 
 		if (newmp) {
 			if (mpp->action != ACT_REJECT) {
-				if (!vector_alloc_slot(newmp))
+				if (!vector_alloc_slot(newmp)) {
+					remove_map(mpp, vecs->pathvec, vecs->mpvec, KEEP_VEC);
 					goto out;
+				}
 				vector_set_slot(newmp, mpp);
 			}
 			else
multipath/main.c +5 -2
index 9e920d89..5f5b435a 100644
@@ -472,6 +472,7 @@ configure (struct config *conf, enum mpath_cmds cmd,
 {
 	vector curmp = NULL;
 	vector pathvec = NULL;
+	vector newmp = NULL;
 	struct vectors vecs;
 	int r = RTVL_FAIL, rc;
 	int di_flag = 0;
@@ -483,8 +484,9 @@ configure (struct config *conf, enum mpath_cmds cmd,
 	 */
 	curmp = vector_alloc();
 	pathvec = vector_alloc();
+	newmp = vector_alloc();
 
-	if (!curmp || !pathvec) {
+	if (!curmp || !pathvec || !newmp) {
 		condlog(0, "can not allocate memory");
 		goto out;
 	}
@@ -586,7 +588,7 @@ configure (struct config *conf, enum mpath_cmds cmd,
 	/*
 	 * core logic entry point
 	 */
-	rc = coalesce_paths(&vecs, NULL, refwwid,
+	rc = coalesce_paths(&vecs, newmp, refwwid,
 			   conf->force_reload, cmd);
 	r = rc == CP_RETRY ? RTVL_RETRY : rc == CP_OK ? RTVL_OK : RTVL_FAIL;
 
@@ -595,6 +597,7 @@ out:
 		FREE(refwwid);
 
 	free_multipathvec(curmp, KEEP_PATHS);
+	free_multipathvec(newmp, KEEP_PATHS);
 	free_pathvec(pathvec, FREE_PATHS);
 
 	return r;
Signature · 4 lines
--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel
#1 lore raw

multipath/main.c

Quoted 50 lines · lixiaokeng

When multipath -F are executed firstly and multipath -v2 or -d are executed later, asan will warn memory leaks. The reason is that the mpp allocated in coalesce_paths isn't freed. Here we add newmp in configure(multipath) to store mpp and free it.

---
 libmultipath/configure.c | 12 ++++++++++--
 multipath/main.c         |  7 +++++--
 2 files changed, 15 insertions(+), 4 deletions(-)
libmultipath/configure.c +10 -2
index 6fb477fc..fb2c3f73 100644
@@ -1270,8 +1270,14 @@ int coalesce_paths (struct vectors * vecs, vector newmp, char * refwwid,
 				goto out;
 			}
 		}
-		if (r == DOMAP_DRY)
+		if (r == DOMAP_DRY) {
+			if (!vector_alloc_slot(newmp)) {
+				remove_map(mpp, vecs->pathvec, vecs->mpvec, KEEP_VEC);
+				goto out;
+			}
+			vector_set_slot(newmp, mpp);
 			continue;
+		}
 
 		if (r == DOMAP_EXIST && mpp->action == ACT_NOTHING &&
 		    force_reload == FORCE_RELOAD_WEAK)
@@ -1309,8 +1315,10 @@ int coalesce_paths (struct vectors * vecs, vector newmp, char * refwwid,
 
 		if (newmp) {
 			if (mpp->action != ACT_REJECT) {
-				if (!vector_alloc_slot(newmp))
+				if (!vector_alloc_slot(newmp)) {
+					remove_map(mpp, vecs->pathvec, vecs->mpvec, KEEP_VEC);
 					goto out;
+				}
 				vector_set_slot(newmp, mpp);
 			}
 			else
multipath/main.c +0 -0
index 9e920d89..5f5b435a 100644

You should rebase this on top of Martin's "libmultipath: improve cleanup on exit" patchset, since it conflicts with those patches. He's just posted another version of them.

-Ben

Quoted 30 lines
multipath/main.c +4 -2
@@ -472,6 +472,7 @@ configure (struct config *conf, enum mpath_cmds cmd,
 {
 	vector curmp = NULL;
 	vector pathvec = NULL;
+	vector newmp = NULL;
 	struct vectors vecs;
 	int r = RTVL_FAIL, rc;
 	int di_flag = 0;
@@ -483,8 +484,9 @@ configure (struct config *conf, enum mpath_cmds cmd,
 	 */
 	curmp = vector_alloc();
 	pathvec = vector_alloc();
+	newmp = vector_alloc();
 
-	if (!curmp || !pathvec) {
+	if (!curmp || !pathvec || !newmp) {
 		condlog(0, "can not allocate memory");
 		goto out;
 	}
@@ -586,7 +588,7 @@ configure (struct config *conf, enum mpath_cmds cmd,
 	/*
 	 * core logic entry point
 	 */
-	rc = coalesce_paths(&vecs, NULL, refwwid,
+	rc = coalesce_paths(&vecs, newmp, refwwid,
 			   conf->force_reload, cmd);
 	r = rc == CP_RETRY ? RTVL_RETRY : rc == CP_OK ? RTVL_OK : RTVL_FAIL;
 

multipath/main.c out: · line 603

Quoted 7 lines
multipath/main.c +1 -0
@@ -595,6 +597,7 @@ out:
 		FREE(refwwid);
 
 	free_multipathvec(curmp, KEEP_PATHS);
+	free_multipathvec(newmp, KEEP_PATHS);
 	free_pathvec(pathvec, FREE_PATHS);
 
 	return r;

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel

#2 lore raw
In reply to lixiaokeng
Quoted 9 lines · lixiaokeng

When multipath -F are executed firstly and multipath -v2 or -d are executed later, asan will warn memory leaks. The reason is that the mpp allocated in coalesce_paths isn't freed. Here we add newmp in configure(multipath) to store mpp and free it.

Besides what Ben noted already, I think you shouldn't force callers to pass a non-NULL "newmp". The tricky part is to make sure that paths aren't handled repeatedly in the CMD_DRY_RUN case. Currently pp->mpp != NULL is the condition used by coalesce_paths() to check if a path has already been dealt with; if you simply call remove_map(), that won't work any more. I suppose you realized that, and that's why you introduced the non-NULL newmp in multipath (you should have mentioned that in the changelog message).

I suggest to have callers pass a "vector *pnewmp" instead of "vector newmp", always allocate newmp in coalesce_paths(), and upon return, either free newmp, or assign it to the pointer passed by the caller:

     if (pnewmp)
	    *pnewmp = newmp;
     else
            free_multipathvec(newmp, KEEP_PATHS);

Regards,
Martin

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel

author
#3 lore raw
Quoted 24 lines · Martin Wilck
Quoted 9 lines · lixiaokeng

When multipath -F are executed firstly and multipath -v2 or -d are executed later, asan will warn memory leaks. The reason is that the mpp allocated in coalesce_paths isn't freed. Here we add newmp in configure(multipath) to store mpp and free it.

Besides what Ben noted already, I think you shouldn't force callers to pass a non-NULL "newmp". The tricky part is to make sure that paths aren't handled repeatedly in the CMD_DRY_RUN case. Currently pp->mpp != NULL is the condition used by coalesce_paths() to check if a path has already been dealt with; if you simply call remove_map(), that won't work any more. I suppose you realized that, and that's why you introduced the non-NULL newmp in multipath (you should have mentioned that in the changelog message).

I suggest to have callers pass a "vector *pnewmp" instead of "vector newmp", always allocate newmp in coalesce_paths(), and upon return, either free newmp, or assign it to the pointer passed by the caller:

Quoted 8 lines
     if (pnewmp)
	    *pnewmp = newmp;
     else
            free_multipathvec(newmp, KEEP_PATHS);

Regards,
Martin

Hi Martin,
Thanks for your review. It is a great idea with your suggestion.
I think it's better that callers pass a "vector mpvec" instead of "vector newmp", either copy mpvec to newmp or allocate newmp at the beginning of coalesce_paths, and free newmp or not at the end:

int coalesce_paths (struct vectors *vecs, vector mpvec, char *refwwid,

		    int force_reload, enum mpath_cmds cmd)
	......

if (mpvec)

		newmp = mpvec;
	else
		newmp = vector_alloc();
	if (!newmp) {
		condlog(0, "can not allocate newmp");
		return ret;
	}
	......

if (!mpvec)

		free_multipathvec(newmp, KEEP_PATHS);

About conflict, can you give me the url of the code with your patchset? If I just change coalease_paths, will it have some conflicts?

Regards,
Lixiaokeng

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel