Skip to contentSkip to conversation

Memory issues found by coverity

PATCH Superseded Benjamin Marzinski 2 participants 9 messages May 14, 2021 Device mapper

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Benjamin Marzinski

author maintainer
#0 lore raw

v3 cover Memory issues found by coverity

This is collection of issues found by coverity. The first three patches deal with ev_remove_path() removing the path, but returning failure, causing a use-after-free error. The next two patches fix memory leaks. The final patch removes an unnecessary call to rescan_paths() from uev_update_path()

Changes from v2:
0003: Combined with v2 patch 0006, with changes changes in comments,
how the return codes are defined, and how they are handled, based on Martin's suggestions
0006: New patch based on Martin's suggestions about wwid change handling
in uev_update_path().

Changes from v1:
0001: changed comment based on Martin's suggestion 0004: moved location of atexit() call based on Martin's suggestion 0006: New patch, based on Martin's comments on patch 0003

Benjamin Marzinski (6):
multipathd: don't fail to remove path once the map is removed
multipathd: remove duplicate orphan_paths in flush_map
multipathd: fix ev_remove_path return code handling
multipath: free vectors in configure
kpartx: Don't leak memory when getblock returns NULL
multipathd: don't rescan_path on wwid change in uev_update_path

 kpartx/kpartx.c            |  2 ++
 libmultipath/structs_vec.c |  4 +--
 multipath/main.c           |  7 ++++-
 multipathd/cli_handlers.c  | 14 ++++++++--
 multipathd/main.c          | 56 ++++++++++++++++++++------------------
 multipathd/main.h          | 14 ++++++++++
 6 files changed, 65 insertions(+), 32 deletions(-)
Signature · 6 lines
2.17.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
author maintainer
#1 lore raw

PATCH v3 1/6 multipathd: don't fail to remove path once the map is removed

In ev_remove_path(), if update_mpp_paths() fails, we delete the entire map. However, since update_mpp_paths() happens before we call set_path_removed(), pp->initialized isn't set to INIT_REMOVED, so remove_map_and_stop_waiter() doesn't remove the path when in removes the map. But with the map removed, there's nothing to keep us from removing the path.

Call set_path_removed() before update_mpp_paths() to avoid the odd case of ev_remove_path() removing the map but not the path.

---
 libmultipath/structs_vec.c |  4 ++--
 multipathd/main.c          | 13 ++++++++-----
 2 files changed, 10 insertions(+), 7 deletions(-)
libmultipath/structs_vec.c +2 -2
index d242c06b..75390198 100644
@@ -45,8 +45,8 @@ int update_mpp_paths(struct multipath *mpp, vector pathvec)
 
 				/*
 				 * Avoid adding removed paths to the map again
-				 * when we reload it. Such paths may exist if
-				 * domap fails in ev_remove_path().
+				 * when we reload it. Such paths may exist in
+				 * ev_remove_paths() or if it returns failure.
 				 */
 				pp1 = find_path_by_devt(pathvec, pp->dev_t);
 				if (pp1 && pp->initialized != INIT_REMOVED &&
multipathd/main.c +8 -5
index 102946bf..449ce384 100644
@@ -1199,6 +1199,13 @@ ev_remove_path (struct path *pp, struct vectors * vecs, int need_do_map)
 	 * avoid referring to the map of an orphaned path
 	 */
 	if ((mpp = pp->mpp)) {
+		/*
+		 * Mark the path as removed. In case of success, we
+		 * will delete it for good. Otherwise, it will be deleted
+		 * later, unless all attempts to reload this map fail.
+		 */
+		set_path_removed(pp);
+
 		/*
 		 * transform the mp->pg vector of vectors of paths
 		 * into a mp->params string to feed the device-mapper
@@ -1210,13 +1217,9 @@ ev_remove_path (struct path *pp, struct vectors * vecs, int need_do_map)
 		}
 
 		/*
-		 * Mark the path as removed. In case of success, we
-		 * will delete it for good. Otherwise, it will be deleted
-		 * later, unless all attempts to reload this map fail.
-		 * Note: we have to explicitly remove pp from mpp->paths,
+		 * we have to explicitly remove pp from mpp->paths,
 		 * update_mpp_paths() doesn't do that.
 		 */
-		set_path_removed(pp);
 		i = find_slot(mpp->paths, pp);
 		if (i != -1)
 			vector_del_slot(mpp->paths, i);
Signature · 6 lines
2.17.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
author maintainer
#2 lore raw

PATCH v3 2/6 multipathd: remove duplicate orphan_paths in flush_map

In reply to Benjamin Marzinski

remove_map_and_stop_waiter() already calls orphan_paths() so flush_map() doesn't need to call orphan_paths() before calling remove_map_and_stop_waiter().

---
 multipathd/main.c | 1 -
 1 file changed, 1 deletion(-)
multipathd/main.c +0 -1
index 449ce384..6090434c 100644
@@ -660,7 +660,6 @@ flush_map(struct multipath * mpp, struct vectors * vecs, int nopaths)
 	else
 		condlog(2, "%s: map flushed", mpp->alias);
 
-	orphan_paths(vecs->pathvec, mpp, "map flushed");
 	remove_map_and_stop_waiter(mpp, vecs);
 
 	return 0;
Signature · 6 lines
2.17.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
author maintainer
#3 lore raw

PATCH v3 3/6 multipathd: fix ev_remove_path return code handling

In reply to Benjamin Marzinski

When ev_remove_path() returned success, callers assumed that the path (and possibly the map) had been removed. When ev_remove_path() returned failure, callers assumed that the path had not been removed. However, the path could be removed on both success or failure. This could cause callers to dereference the path after it was removed.

To deal with this, make ev_remove_path() return a different symbolic value for each outcome, and make the callers react appropriately for the different values. Found by coverity.

---
 multipathd/cli_handlers.c | 14 +++++++++++--
 multipathd/main.c         | 41 ++++++++++++++++++++-------------------
 multipathd/main.h         | 14 +++++++++++++
 3 files changed, 47 insertions(+), 22 deletions(-)
multipathd/cli_handlers.c +12 -2
index 1de6ad8e..1462ea84 100644
@@ -752,7 +752,8 @@ cli_add_path (void * v, char ** reply, int * len, void * data)
 				/* Have the checker reinstate this path asap */
 				pp->tick = 1;
 				return 0;
-			} else if (!ev_remove_path(pp, vecs, true))
+			} else if (ev_remove_path(pp, vecs, true) &
+				   REMOVE_PATH_SUCCESS)
 				/* Path removed in ev_remove_path() */
 				pp = NULL;
 			else {
@@ -813,6 +814,7 @@ cli_del_path (void * v, char ** reply, int * len, void * data)
 	struct vectors * vecs = (struct vectors *)data;
 	char * param = get_keyparam(v, PATH);
 	struct path *pp;
+	int ret;
 
 	param = convert_dev(param, 1);
 	condlog(2, "%s: remove path (operator)", param);
@@ -821,7 +823,15 @@ cli_del_path (void * v, char ** reply, int * len, void * data)
 		condlog(0, "%s: path already removed", param);
 		return 1;
 	}
-	return ev_remove_path(pp, vecs, 1);
+	ret = ev_remove_path(pp, vecs, 1);
+	if (ret == REMOVE_PATH_DELAY) {
+		*reply = strdup("delayed\n");
+		*len = strlen(*reply) + 1;
+	} else if (ret == REMOVE_PATH_MAP_ERROR) {
+		*reply = strdup("map reload error. removed\n");
+		*len = strlen(*reply) + 1;
+	}
+	return (ret == REMOVE_PATH_FAILURE);
 }
 
 int
multipathd/main.c +21 -20

115 lines not on this page · raw message

multipathd/main.h +14 -0
index ddd953f9..bc1f938f 100644
@@ -13,6 +13,20 @@ enum daemon_status {
 	DAEMON_STATUS_SIZE,
 };
 
+enum remove_path_result {
+	REMOVE_PATH_FAILURE = 0x0, /* path could not be removed. It is still
+				    * part of the kernel map, but its state
+				    * is set to INIT_REMOVED, and it will be
+				    * removed at the next possible occassion */
+	REMOVE_PATH_SUCCESS = 0x1, /* path was removed */
+	REMOVE_PATH_DELAY = 0x2, /* path is set to be removed later. it
+			          * currently still exists and is part of the
+			          * kernel map */
+	REMOVE_PATH_MAP_ERROR = 0x5, /* map was removed because of error. value
+				      * includes REMOVE_PATH_SUCCESS bit
+				      * because the path was also removed */
+};
+
 struct prout_param_descriptor;
 struct prin_resp;
 
Signature · 6 lines
2.17.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
#4 lore raw

Re: [dm-devel] [PATCH v3 3/6] multipathd: fix ev_remove_path return code handling

Quoted 8 lines · Benjamin Marzinski

When ev_remove_path() returned success, callers assumed that the path (and possibly the map) had been removed.  When ev_remove_path() returned
failure, callers assumed that the path had not been removed. However, the path could be removed on both success or failure. This could cause
callers to dereference the path after it was removed.

Quoted 5 lines

To deal with this, make ev_remove_path() return a different symbolic value for each outcome, and make the callers react appropriately for the different values. Found by coverity.

Almost perfect - I still have one nit below.

Quoted 37 lines

---
 multipathd/cli_handlers.c | 14 +++++++++++-- multipathd/main.c         | 41 ++++++++++++++++++++----------------- --
 multipathd/main.h         | 14 +++++++++++++

 3 files changed, 47 insertions(+), 22 deletions(-)
multipathd/cli_handlers.c +0 -0
index 1de6ad8e..1462ea84 100644
@@ -752,7 +752,8 @@ cli_add_path (void * v, char ** reply, int * len,
 void * data)
                                 /* Have the checker reinstate this

path asap */
                                pp->tick = 1;
                                return 0;
-                       } else if (!ev_remove_path(pp, vecs, true)) +                       } else if (ev_remove_path(pp, vecs, true) & +                                  REMOVE_PATH_SUCCESS) /* Path removed in ev_remove_path() */
                                pp = NULL;
                        else {

multipathd/cli_handlers.c +1 -0
@@ -813,6 +814,7 @@ cli_del_path (void * v, char ** reply, int * len,
 void * data)
         struct vectors * vecs = (struct vectors *)data;
         char * param = get_keyparam(v, PATH);
         struct path *pp;
+       int ret;


        param = convert_dev(param, 1); condlog(2, "%s: remove path (operator)", param);

multipathd/cli_handlers.c +0 -0
@@ -821,7 +823,15 @@ cli_del_path (void * v, char ** reply, int *
 len, void * data)
                 condlog(0, "%s: path already removed", param);
                 return 1;

multipathd/cli_handlers.c cli_del_path (void * v, char ** reply, int * · line 834

Quoted 10 lines
multipathd/cli_handlers.c +8 -1
         }
-       return ev_remove_path(pp, vecs, 1);
+       ret = ev_remove_path(pp, vecs, 1);
+       if (ret == REMOVE_PATH_DELAY) {
+               *reply = strdup("delayed\n");
+               *len = strlen(*reply) + 1;
+       } else if (ret == REMOVE_PATH_MAP_ERROR) {
+               *reply = strdup("map reload error. removed\n");
+               *len = strlen(*reply) + 1;
+       }

So you went for the new replies. Fine with me, let's see if anyone complains. But you should check the result of strdup() before calling strlen():

                 *reply = strdup("delayed\n");
                 if (*reply)
                        *len = strlen(*reply) + 1;                    
                 else {
                        *len = 0;
                        ret = REMOVE_PATH_FAILURE;
                 }

Regards,
Martin

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

author maintainer
#5 lore raw

PATCH v3 4/6 multipath: free vectors in configure

In reply to Benjamin Marzinski

configure() can retry multiple times, each time reallocing a maps and paths vector, and leaking the previous ones. Fix this by always freeing the vectors before configure() exits. Found by coverity.

---
 multipath/main.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
multipath/main.c +6 -1
index ef89c7cf..8fc0e15f 100644
@@ -466,7 +466,6 @@ configure (struct config *conf, enum mpath_cmds cmd,
 	 */
 	curmp = vector_alloc();
 	pathvec = vector_alloc();
-	atexit(cleanup_vecs);
 
 	if (!curmp || !pathvec) {
 		condlog(0, "can not allocate memory");
@@ -578,6 +577,11 @@ out:
 	if (refwwid)
 		FREE(refwwid);
 
+	free_multipathvec(curmp, KEEP_PATHS);
+	vecs.mpvec = NULL;
+	free_pathvec(pathvec, FREE_PATHS);
+	vecs.pathvec = NULL;
+
 	return r;
 }
 
@@ -823,6 +827,7 @@ main (int argc, char *argv[])
 	conf = get_multipath_config();
 	conf->retrigger_tries = 0;
 	conf->force_sync = 1;
+	atexit(cleanup_vecs);
 	while ((arg = getopt(argc, argv, ":adDcChl::eFfM:v:p:b:BrR:itTquUwW")) != EOF ) {
 		switch(arg) {
 		case 1: printf("optarg : %s\n",optarg);
Signature · 6 lines
2.17.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
author maintainer
#6 lore raw

PATCH v3 5/6 kpartx: Don't leak memory when getblock returns NULL

In reply to Benjamin Marzinski

If a new block was allocated, but couldn't be filled, getblock will discard it. When it does so, it needs to free the block to avoid leaking memory. Found by coverity.

---
 kpartx/kpartx.c | 2 ++
 1 file changed, 2 insertions(+)
kpartx/kpartx.c +2 -0
index 8ff116b8..7bc64543 100644
@@ -766,6 +766,8 @@ getblock (int fd, unsigned int blknr) {
 	if (read(fd, bp->block, secsz) != secsz) {
 		fprintf(stderr, "read error, sector %d\n", secnr);
 		blockhead = bp->next;
+		free(bp->block);
+		free(bp);
 		return NULL;
 	}
 
Signature · 6 lines
2.17.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
author maintainer
#7 lore raw

PATCH v3 6/6 multipathd: don't rescan_path on wwid change in uev_update_path

In reply to Benjamin Marzinski

If get_uid() is returning a different wwid in uev_update_path(), then the uid_attribute must have already gotten updated, which was the purpose behind calling rescan_path() in the first place.

---
 multipathd/main.c | 1 -
 1 file changed, 1 deletion(-)
multipathd/main.c +0 -1
index 8e2beddd..2750f5e9 100644
@@ -1359,7 +1359,6 @@ uev_update_path (struct uevent *uev, struct vectors * vecs)
 			condlog(0, "%s: path wwid changed from '%s' to '%s'",
 				uev->kernel, wwid, pp->wwid);
 			ev_remove_path(pp, vecs, 1);
-			rescan_path(uev->udev);
 			needs_reinit = 1;
 			goto out;
 		} else {
Signature · 6 lines
2.17.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
Reviewed-by on v3 6/6
#8 lore raw

Re: [dm-devel] [PATCH v3 6/6] multipathd: don't rescan_path on wwid change in uev_update_path

Quoted 5 lines · Benjamin Marzinski

If get_uid() is returning a different wwid in uev_update_path(), then the uid_attribute must have already gotten updated, which was the purpose behind calling rescan_path() in the first place.

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