Skip to contentSkip to conversation

multipath: coverity fixes

PATCH In review Hannes Reinecke 2 participants 42 messages Jun 16, 2016 – Jun 20, 2016 Device mapper

lore
Share & tools
lore
#0 lore raw

cover multipath: coverity fixes

Hi all,

here's a bunch of fixes from a recent coverity run. Nothing critical, mostly memory leaks and possible buffer overflows. And the usual missing return value check.

As usual, comments and reviews are welcome.

The whole patchset can be found at
github.com:/hreinecke/multipath-tools.git
branch coverity

Hannes Reinecke (39):
multipathd: correctly free refwwid in cli_add_map()
libmultipath: fixup string copy and comparison
libmultipath: fixup uninitialized return value in dm_reassign_table()
libmultipath: free pathgroup in group_by_prio()
libmultipath: memory leak in remove_feature()
libmultipath: memory leak in add_map_without_path()
libmultipath: missing NULL check in set_pgfailback()
libmultipath: remove stale references to fd_ep in uevent_listen() libmultipath/devmapper.c: Remove unused 'next' argument libmultipath/alias.c: check return value from strchr() libmultipath/devmapper.c: check for errors from sscanf() libmultipath/uevent.c: handle errors from setsockopt() libmultipath/discovery.c: handle errors from sscanf()
libmultipath: drop unused variable 'diop' in libsg.c
libmultipath: use fstat() when reading sysfs attributes
libmultipath: fixup possible buffer overflow in alua_rtpg.c
libmultipath: memory leak in iet.c
libmpathcmd: limit size of the CLI buffer
libmpathpersist: Fix possible overrun in devt2devname()
libmpathpersist: memory leak in mpath_persistent_reserve_(in,out)()
libmpathpersist: Pass sensebuffer as pointer
libmpathpersist: Handle send error
libmultipath: missing break in hds prioritizer
kpartx: fixup fd leakage in lopart.c
kpartx: Memory leak in get_hotplug_device()
kpartx: fixup filedescriptor leak in set_loop()
kpartx: do not use 'const' for mapname in get_hotplug_device()
kpartx: Handle errors from lseek()
kpartx/devmapper.c: Remove unused 'next' argument
kpartx: use fstat() when reading sysfs attributes kpartx/lopart.c: use fstat() when reading device nodes libmultipath/sysfs.c: always terminate value from
sysfs_attr_get_value()
multipathd: missing mpath_disconnect() in uxclnt()
multipathd: pid fd resource leak
libmultipath: mark return value from dup() as unused
libmultipath: Ignore errors from fcntl() in directio.c
libmultipath: add spin_lock in tur.c libmultipath/devmapper.c: fixup possible buffer overflow libmultipath/discovery.c: Fixup possible range overflow

 kpartx/devmapper.c                    |  5 ++-
 kpartx/gpt.c                          |  5 +--
 kpartx/kpartx.c                       | 13 ++++---
 kpartx/lopart.c                       | 39 ++++++++++-----------
 libmpathcmd/mpath_cmd.c               |  2 ++
 libmpathcmd/mpath_cmd.h               |  1 +
 libmpathpersist/mpath_persist.c       | 21 +++++++++---
 libmpathpersist/mpath_pr_ioctl.c      | 35 ++++++++++---------
 libmpathpersist/mpath_updatepr.c      |  8 +++--
 libmultipath/alias.c                  |  3 +-
 libmultipath/callout.c                |  4 ++-
 libmultipath/checkers/directio.c      |  4 ++-
 libmultipath/checkers/libsg.c         |  3 --
 libmultipath/checkers/tur.c           |  4 ++-
 libmultipath/configure.c              | 10 +++---
 libmultipath/devmapper.c              | 40 +++++++++++++---------
 libmultipath/dict.c                   |  2 ++
 libmultipath/discovery.c              | 13 +++----
 libmultipath/dmparser.c               | 16 +++++----
 libmultipath/pgpolicies.c             | 14 +++++---
 libmultipath/prio.c                   |  2 +-
 libmultipath/prioritizers/alua_rtpg.c |  8 ++++-
 libmultipath/prioritizers/hds.c       |  2 ++
 libmultipath/prioritizers/iet.c       |  3 +-
 libmultipath/structs.c                |  4 ++-
 libmultipath/structs_vec.c            | 10 ++++--
 libmultipath/sysfs.c                  | 64 ++++++++++++++++++++---------------
 libmultipath/uevent.c                 | 19 +++++++----
 libmultipath/waiter.c                 |  2 +-
 multipathd/cli_handlers.c             |  2 +-
 multipathd/main.c                     |  6 +++-
 multipathd/pidfile.c                  |  6 ++--
 multipathd/uxclnt.c                   |  2 +-
 33 files changed, 226 insertions(+), 146 deletions(-)
Signature · 1 line
2.6.6
#1 lore raw

PATCH 1/39 multipathd: correctly free refwwid in cli_add_map()

We need to free the refwwid only when present.
Found by coverity.

---
 multipathd/cli_handlers.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
multipathd/cli_handlers.c +1 -1
index a1b7052..52bcdaa 100644
@@ -715,6 +715,7 @@ cli_add_map (void * v, char ** reply, int * len, void * data)
 					condlog(2, "%s: coalesce_paths failed",
 									param);
 				dm_lib_release();
+				FREE(refwwid);
 			}
 		} /*we attempt to create device only once*/
 		count++;
@@ -726,7 +727,6 @@ cli_add_map (void * v, char ** reply, int * len, void * data)
 	}
 	rc = ev_add_map(dev_path, alias, vecs);
 	FREE(alias);
-	FREE(refwwid);
 	return rc;
 }
 
Signature · 1 line
2.6.6
#2 lore raw

PATCH 2/39 libmultipath: fixup string copy and comparison

In reply to Hannes Reinecke

When doing a strncpy or strncmp we need to omit the trailing NULL in the length to avoid any possible overflow. Found by coverity.

---
 libmpathpersist/mpath_persist.c | 11 +++++++----
 libmultipath/configure.c        | 10 +++++-----
 libmultipath/dmparser.c         | 16 ++++++++++------
 libmultipath/prio.c             |  2 +-
 libmultipath/structs_vec.c      |  4 ++--
 libmultipath/waiter.c           |  2 +-
 6 files changed, 26 insertions(+), 19 deletions(-)
libmpathpersist/mpath_persist.c +7 -4
index b23e116..d2c3e53 100644
@@ -375,7 +375,8 @@ get_mpvec (vector curmp, vector pathvec, char * refwwid)
 		/*
 		 * discard out of scope maps
 		 */
-		if (mpp->alias && refwwid && strncmp (mpp->alias, refwwid, WWID_SIZE)){
+		if (mpp->alias && refwwid &&
+		    strncmp (mpp->alias, refwwid, WWID_SIZE - 1)){
 			free_multipath (mpp, KEEP_PATHS);
 			vector_del_slot (curmp, i);
 			i--;
@@ -485,7 +486,8 @@ int mpath_prout_reg(struct multipath *mpp,int rq_servact, int rq_scope,
 				condlog (1, "%s: %s path not up. Skip.", mpp->wwid, pp->dev);
 				continue;
 			}
-			strncpy(thread[count].param.dev, pp->dev, FILE_NAME_SIZE);
+			strncpy(thread[count].param.dev, pp->dev,
+				FILE_NAME_SIZE - 1);
 
 			if (count && (thread[count].param.paramp->sa_flags & MPATH_F_SPEC_I_PT_MASK)){
 				/*
@@ -602,7 +604,7 @@ int send_prout_activepath(char * dev, int rq_servact, int rq_scope,
 	int rc;
 
 	memset(&thread, 0, sizeof(thread));
-	strncpy(param.dev, dev, FILE_NAME_SIZE);
+	strncpy(param.dev, dev, FILE_NAME_SIZE - 1);
 	/* Initialize and set thread joinable attribute */
 	pthread_attr_init(&attr);
 	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
@@ -670,7 +672,8 @@ int mpath_prout_rel(struct multipath *mpp,int rq_servact, int rq_scope,
 				continue;
 			}
 
-			strncpy(thread[count].param.dev, pp->dev, FILE_NAME_SIZE);
+			strncpy(thread[count].param.dev, pp->dev,
+				FILE_NAME_SIZE - 1);
 			condlog (3, "%s: sending pr out command to %s", mpp->wwid, pp->dev);
 			rc = pthread_create (&thread[count].id, &attr, mpath_prout_pthread_fn,
 					(void *) (&thread[count].param));
libmultipath/configure.c +5 -5
index a4a2c44..8e938c0 100644
@@ -65,7 +65,7 @@ int group_by_host_adapter(struct pathgroup *pgp, vector adapters)
 			goto out;
 		agp->pgp = pgp;
 
-		strncpy(agp->adapter_name, adapter_name1, SLOT_NAME_SIZE);
+		strncpy(agp->adapter_name, adapter_name1, SLOT_NAME_SIZE - 1);
 		store_adaptergroup(adapters, agp);
 
 		/* create a new host port group
@@ -395,7 +395,7 @@ select_action (struct multipath * mpp, vector curmp, int force_reload)
 		if (cmpp) {
 			condlog(2, "%s: rename %s to %s", mpp->wwid,
 				cmpp->alias, mpp->alias);
-			strncpy(mpp->alias_old, cmpp->alias, WWID_SIZE);
+			strncpy(mpp->alias_old, cmpp->alias, WWID_SIZE - 1);
 			mpp->action = ACT_RENAME;
 			if (force_reload)
 				mpp->action = ACT_FORCERENAME;
@@ -410,7 +410,7 @@ select_action (struct multipath * mpp, vector curmp, int force_reload)
 	if (!cmpp) {
 		condlog(2, "%s: remove (wwid changed)", mpp->alias);
 		dm_flush_map(mpp->alias);
-		strncpy(cmpp_by_name->wwid, mpp->wwid, WWID_SIZE);
+		strncpy(cmpp_by_name->wwid, mpp->wwid, WWID_SIZE - 1);
 		drop_multipath(curmp, cmpp_by_name->wwid, KEEP_PATHS);
 		mpp->action = ACT_CREATE;
 		condlog(3, "%s: set ACT_CREATE (map wwid change)",
@@ -770,7 +770,7 @@ coalesce_paths (struct vectors * vecs, vector newmp, char * refwwid, int force_r
 		}
 
 		/* 4. path is out of scope */
-		if (refwwid && strncmp(pp1->wwid, refwwid, WWID_SIZE))
+		if (refwwid && strncmp(pp1->wwid, refwwid, WWID_SIZE - 1))
 			continue;
 
 		/* If find_multipaths was selected check if the path is valid */
@@ -896,7 +896,7 @@ coalesce_paths (struct vectors * vecs, vector newmp, char * refwwid, int force_r
 			if (!deadmap(mpp))
 				continue;
 
-			strncpy(alias, mpp->alias, WWID_SIZE);
+			strncpy(alias, mpp->alias, WWID_SIZE - 1);
 
 			if ((j = find_slot(newmp, (void *)mpp)) != -1)
 				vector_del_slot(newmp, j);
libmultipath/dmparser.c +10 -6
index 289be89..98fb559 100644
@@ -344,10 +344,11 @@ disassemble_map (vector pathvec, char * params, struct multipath * mpp)
 				if (!pp)
 					goto out1;
 
-				strncpy(pp->dev_t, word, BLK_DEV_SIZE);
-				strncpy(pp->dev, devname, FILE_NAME_SIZE);
+				strncpy(pp->dev_t, word, BLK_DEV_SIZE - 1);
+				strncpy(pp->dev, devname, FILE_NAME_SIZE - 1);
 				if (strlen(mpp->wwid)) {
-					strncpy(pp->wwid, mpp->wwid, WWID_SIZE);
+					strncpy(pp->wwid, mpp->wwid,
+						WWID_SIZE - 1);
 				}
 				/* Only call this in multipath client mode */
 				if (!conf->daemon && store_path(pathvec, pp))
@@ -355,7 +356,8 @@ disassemble_map (vector pathvec, char * params, struct multipath * mpp)
 			} else {
 				if (!strlen(pp->wwid) &&
 				    strlen(mpp->wwid))
-					strncpy(pp->wwid, mpp->wwid, WWID_SIZE);
+					strncpy(pp->wwid, mpp->wwid,
+						WWID_SIZE - 1);
 			}
 			FREE(word);
 
@@ -367,14 +369,16 @@ disassemble_map (vector pathvec, char * params, struct multipath * mpp)
 			 * in the get_dm_mpvec() code path
 			 */
 			if (!strlen(mpp->wwid))
-				strncpy(mpp->wwid, pp->wwid, WWID_SIZE);
+				strncpy(mpp->wwid, pp->wwid,
+					WWID_SIZE - 1);
 
 			/*
 			 * Update wwid for paths which may not have been
 			 * active at the time the getuid callout was run
 			 */
 			else if (!strlen(pp->wwid))
-				strncpy(pp->wwid, mpp->wwid, WWID_SIZE);
+				strncpy(pp->wwid, mpp->wwid,
+					WWID_SIZE - 1);
 
 			pgp->id ^= (long)pp;
 			pp->pgindex = i + 1;
libmultipath/prio.c +1 -1
index ab8eca9..fbf3190 100644
@@ -158,7 +158,7 @@ void prio_get (struct prio * dst, char * name, char * args)
 
 	strncpy(dst->name, src->name, PRIO_NAME_LEN);
 	if (args)
-		strncpy(dst->args, args, PRIO_ARGS_LEN);
+		strncpy(dst->args, args, PRIO_ARGS_LEN - 1);
 	dst->getprio = src->getprio;
 	dst->handle = NULL;
 
libmultipath/structs_vec.c +2 -2
index 7d46d42..e992b54 100644
@@ -447,8 +447,8 @@ find_existing_alias (struct multipath * mpp,
 	int i;
 
 	vector_foreach_slot (vecs->mpvec, mp, i)
-		if (strcmp(mp->wwid, mpp->wwid) == 0) {
-			strncpy(mpp->alias_old, mp->alias, WWID_SIZE);
+		if (strncmp(mp->wwid, mpp->wwid, WWID_SIZE - 1) == 0) {
+			strncpy(mpp->alias_old, mp->alias, WWID_SIZE - 1);
 			return;
 		}
 }
libmultipath/waiter.c +1 -1
index 7cedd4b..6937034 100644
@@ -191,7 +191,7 @@ int start_waiter_thread (struct multipath *mpp, struct vectors *vecs)
 	if (!wp)
 		goto out;
 
-	strncpy(wp->mapname, mpp->alias, WWID_SIZE);
+	strncpy(wp->mapname, mpp->alias, WWID_SIZE - 1);
 	wp->vecs = vecs;
 
 	if (pthread_create(&wp->thread, &waiter_attr, waitevent, wp)) {
Signature · 1 line
2.6.6
#3 lore raw

PATCH 3/39 libmultipath: fixup uninitialized return value in dm_reassign_table()

In reply to Hannes Reinecke

Found by coverity.

---
 libmultipath/devmapper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libmultipath/devmapper.c +1 -1
index ce884e2..6e675fe 100644
@@ -1447,7 +1447,7 @@ void dm_reassign_deps(char *table, char *dep, char *newdep)
 
 int dm_reassign_table(const char *name, char *old, char *new)
 {
-	int r, modified = 0;
+	int r = 0, modified = 0;
 	uint64_t start, length;
 	struct dm_task *dmt, *reload_dmt;
 	char *target, *params = NULL;
Signature · 1 line
2.6.6
#4 lore raw

PATCH 4/39 libmultipath: free pathgroup in group_by_prio()

In reply to Hannes Reinecke

If we encounter an error when storing the pathgroup or paths the pathgroup is never deallocated.
Found by coverity.

---
 libmultipath/pgpolicies.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
libmultipath/pgpolicies.c +10 -4
index 2981d51..f440441 100644
@@ -341,8 +341,10 @@ group_by_prio (struct multipath * mp)
 		if (!pgp)
 			goto out;
 
-		if (store_path(pgp->paths, VECTOR_SLOT(mp->paths, 0)))
-				goto out;
+		if (store_path(pgp->paths, VECTOR_SLOT(mp->paths, 0))) {
+			free_pathgroup(pgp, KEEP_PATHS);
+			goto out;
+		}
 
 		vector_del_slot(mp->paths, 0);
 
@@ -350,11 +352,15 @@ group_by_prio (struct multipath * mp)
 		 * Store the new path group into the vector.
 		 */
 		if (i < VECTOR_SIZE(mp->pg)) {
-			if (!vector_insert_slot(mp->pg, i, pgp))
+			if (!vector_insert_slot(mp->pg, i, pgp)) {
+				free_pathgroup(pgp, KEEP_PATHS);
 				goto out;
+			}
 		} else {
-			if (store_pathgroup(mp->pg, pgp))
+			if (store_pathgroup(mp->pg, pgp)) {
+				free_pathgroup(pgp, KEEP_PATHS);
 				goto out;
+			}
 		}
 
 		/*
Signature · 1 line
2.6.6
#5 lore raw

PATCH 5/39 libmultipath: memory leak in remove_feature()

In reply to Hannes Reinecke

Upon error the 'n' string was never freed.
Found by coverity.

---
 libmultipath/structs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libmultipath/structs.c +3 -1
index 502d98e..ed62b07 100644
@@ -653,9 +653,11 @@ remove_feature(char **f, char *o)
 	 * about to be removed
 	 */
 	p = strchr(*f, ' ');
-	if (!p)
+	if (!p) {
 		/* Internal error, feature string inconsistent */
+		FREE(n);
 		return 1;
+	}
 	while (*p == ' ')
 		p++;
 	p--;
Signature · 1 line
2.6.6
#6 lore raw

PATCH 6/39 libmultipath: memory leak in add_map_without_path()

In reply to Hannes Reinecke

If we call add_map_without_path() with an empty alias the mpp structure is never freed.
Found by coverity.

---
 libmultipath/structs_vec.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
libmultipath/structs_vec.c +5 -1
index e992b54..20a7457 100644
@@ -414,8 +414,12 @@ add_map_without_path (struct vectors * vecs, char * alias)
 {
 	struct multipath * mpp = alloc_multipath();
 
-	if (!mpp || !alias)
+	if (!mpp)
+		return NULL;
+	if (!alias) {
+		FREE(mpp);
 		return NULL;
+	}
 
 	mpp->alias = STRDUP(alias);
 
Signature · 1 line
2.6.6
#7 lore raw

PATCH 7/39 libmultipath: missing NULL check in set_pgfailback()

In reply to Hannes Reinecke

Found by coverity.

---
 libmultipath/dict.c | 2 ++
 1 file changed, 2 insertions(+)
libmultipath/dict.c +2 -0
index 7f912ec..9920a9b 100644
@@ -805,6 +805,8 @@ set_pgfailback(vector strvec, void *ptr)
 	char * buff;
 
 	buff = set_value(strvec);
+	if (!buff)
+		return 1;
 
 	if (strlen(buff) == 6 && !strcmp(buff, "manual"))
 		*int_ptr = -FAILBACK_MANUAL;
Signature · 1 line
2.6.6
#8 lore raw

PATCH 8/39 libmultipath: remove stale references to fd_ep in uevent_listen()

In reply to Hannes Reinecke

Found by coverity.

---
 libmultipath/uevent.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
libmultipath/uevent.c +1 -3
index 454b819..fb8b4a7 100644
@@ -474,7 +474,7 @@ int uevent_listen(struct udev *udev)
 {
 	int err = 2;
 	struct udev_monitor *monitor = NULL;
-	int fd, fd_ep = -1, socket_flags, events;
+	int fd, socket_flags, events;
 	int need_failback = 1;
 	int timeout = 30;
 	LIST_HEAD(uevlisten_tmp);
@@ -579,8 +579,6 @@ int uevent_listen(struct udev *udev)
 	}
 	need_failback = 0;
 out:
-	if (fd_ep >= 0)
-		close(fd_ep);
 	if (monitor)
 		udev_monitor_unref(monitor);
 	if (need_failback)
Signature · 1 line
2.6.6
#9 lore raw

PATCH 9/39 libmultipath/devmapper.c: Remove unused 'next' argument

In reply to Hannes Reinecke

Found by coverity.

---
 libmultipath/devmapper.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
libmultipath/devmapper.c +6 -9
index 6e675fe..f395481 100644
@@ -421,7 +421,6 @@ dm_get_map(const char * name, unsigned long long * size, char * outparams)
 {
 	int r = 1;
 	struct dm_task *dmt;
-	void *next = NULL;
 	uint64_t start, length;
 	char *target_type = NULL;
 	char *params = NULL;
@@ -438,8 +437,8 @@ dm_get_map(const char * name, unsigned long long * size, char * outparams)
 		goto out;
 
 	/* Fetch 1st target */
-	next = dm_get_next_target(dmt, next, &start, &length,
-				  &target_type, &params);
+	dm_get_next_target(dmt, NULL, &start, &length,
+			   &target_type, &params);
 
 	if (size)
 		*size = length;
@@ -530,7 +529,6 @@ dm_get_status(char * name, char * outstatus)
 {
 	int r = 1;
 	struct dm_task *dmt;
-	void *next = NULL;
 	uint64_t start, length;
 	char *target_type;
 	char *status;
@@ -547,8 +545,8 @@ dm_get_status(char * name, char * outstatus)
 		goto out;
 
 	/* Fetch 1st target */
-	next = dm_get_next_target(dmt, next, &start, &length,
-				  &target_type, &status);
+	dm_get_next_target(dmt, NULL, &start, &length,
+			   &target_type, &status);
 
 	if (snprintf(outstatus, PARAMS_SIZE, "%s", status) <= PARAMS_SIZE)
 		r = 0;
@@ -571,7 +569,6 @@ dm_type(const char * name, char * type)
 {
 	int r = 0;
 	struct dm_task *dmt;
-	void *next = NULL;
 	uint64_t start, length;
 	char *target_type = NULL;
 	char *params;
@@ -588,8 +585,8 @@ dm_type(const char * name, char * type)
 		goto out;
 
 	/* Fetch 1st target */
-	next = dm_get_next_target(dmt, next, &start, &length,
-				  &target_type, &params);
+	dm_get_next_target(dmt, NULL, &start, &length,
+			   &target_type, &params);
 
 	if (!target_type)
 		r = -1;
Signature · 1 line
2.6.6
#10 lore raw

PATCH 10/39 libmultipath/alias.c: check return value from strchr()

In reply to Hannes Reinecke

Found by coverity.

---
 libmultipath/alias.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
libmultipath/alias.c +2 -1
index 5fdcdb5..b86843a 100644
@@ -229,7 +229,8 @@ allocate_binding(int fd, char *wwid, int id, char *prefix)
 		return NULL;
 	}
 	c = strchr(buf, ' ');
-	*c = '\0';
+	if (c)
+		*c = '\0';
 	alias = strdup(buf);
 	if (alias == NULL)
 		condlog(0, "cannot copy new alias from bindings file : %s",
Signature · 1 line
2.6.6
#11 lore raw

PATCH 11/39 libmultipath/devmapper.c: check for errors from sscanf()

In reply to Hannes Reinecke

Found by coverity.

---
 libmultipath/devmapper.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
libmultipath/devmapper.c +4 -1
index f395481..0223e18 100644
@@ -116,7 +116,10 @@ dm_lib_prereq (void)
 
 	dm_get_library_version(version, sizeof(version));
 	condlog(3, "libdevmapper version %s", version);
-	sscanf(version, "%d.%d.%d ", &v[0], &v[1], &v[2]);
+	if (sscanf(version, "%d.%d.%d ", &v[0], &v[1], &v[2]) != 3) {
+		condlog(0, "invalid libdevmapper version %s", version);
+		return 1;
+	}
 
 	if VERSION_GE(v, minv)
 		return 0;
Signature · 1 line
2.6.6
#12 lore raw

PATCH 12/39 libmultipath/uevent.c: handle errors from setsockopt()

In reply to Hannes Reinecke

Found by coverity.

---
 libmultipath/uevent.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
libmultipath/uevent.c +11 -4
index fb8b4a7..ff4bc86 100644
@@ -291,8 +291,12 @@ int failback_listen(void)
 		}
 
 		/* enable receiving of the sender credentials */
-		setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
-			   &feature_on, sizeof(feature_on));
+		retval = setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
+				    &feature_on, sizeof(feature_on));
+		if (retval < 0) {
+			condlog(0, "failed to enable credential passing, exit");
+			goto exit;
+		}
 
 	} else {
 		/* Fallback to read kernel netlink events */
@@ -330,8 +334,11 @@ int failback_listen(void)
 		condlog(3, "receive buffer size for socket is %u.", rcvsz);
 
 		/* enable receiving of the sender credentials */
-		setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
-			   &feature_on, sizeof(feature_on));
+		if (setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
+			       &feature_on, sizeof(feature_on)) < 0) {
+			condlog(0, "error on enabling credential passing for socket");
+			exit(1);
+		}
 
 		retval = bind(sock, (struct sockaddr *) &snl,
 			      sizeof(struct sockaddr_nl));
Signature · 1 line
2.6.6
#13 lore raw

PATCH 13/39 libmultipath/discovery.c: handle errors from sscanf()

In reply to Hannes Reinecke

Found by coverity.

---
 libmultipath/discovery.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
libmultipath/discovery.c +6 -5
index db82b00..5d5441d 100644
@@ -1179,16 +1179,17 @@ ccw_sysfs_pathinfo (struct path * pp)
 	 */
 	attr_path = udev_device_get_sysname(parent);
 	pp->sg_id.lun = 0;
-	sscanf(attr_path, "%i.%i.%x",
-			&pp->sg_id.host_no,
-			&pp->sg_id.channel,
-			&pp->sg_id.scsi_id);
-	condlog(3, "%s: h:b:t:l = %i:%i:%i:%i",
+	if (sscanf(attr_path, "%i.%i.%x",
+		   &pp->sg_id.host_no,
+		   &pp->sg_id.channel,
+		   &pp->sg_id.scsi_id) == 3) {
+		condlog(3, "%s: h:b:t:l = %i:%i:%i:%i",
 			pp->dev,
 			pp->sg_id.host_no,
 			pp->sg_id.channel,
 			pp->sg_id.scsi_id,
 			pp->sg_id.lun);
+	}
 
 	return 0;
 }
Signature · 1 line
2.6.6
#14 lore raw

PATCH 14/39 libmultipath: drop unused variable 'diop' in libsg.c

In reply to Hannes Reinecke

Found by coverity.

---
 libmultipath/checkers/libsg.c | 3 ---
 1 file changed, 3 deletions(-)
libmultipath/checkers/libsg.c +0 -3
index 0d3af1f..958ea92 100644
@@ -19,7 +19,6 @@ sg_read (int sg_fd, unsigned char * buff, int buff_len,
 	long long start_block = 0;
 	int bs = 512;
 	int cdbsz = 10;
-	int * diop = NULL;
 
 	unsigned char rdCmd[cdbsz];
 	unsigned char *sbb = sense;
@@ -55,8 +54,6 @@ sg_read (int sg_fd, unsigned char * buff, int buff_len,
 	io_hdr.sbp = sense;
 	io_hdr.timeout = timeout * 1000;
 	io_hdr.pack_id = (int)start_block;
-	if (diop && *diop)
-	io_hdr.flags |= SG_FLAG_DIRECT_IO;
 
 retry:
 	memset(sense, 0, sense_len);
Signature · 1 line
2.6.6
#15 lore raw

PATCH 15/39 libmultipath: use fstat() when reading sysfs attributes

In reply to Hannes Reinecke

Coverity pointed out that we shouldn't be using stat()/open(), as the file might have vanished or changed in between those two calls. So modify it to open()/fstat() instead.

---
 libmultipath/sysfs.c | 59 +++++++++++++++++++++++++++++-----------------------
 1 file changed, 33 insertions(+), 26 deletions(-)
libmultipath/sysfs.c +33 -26
index de7df40..a74de9f 100644
@@ -57,30 +57,31 @@ ssize_t sysfs_attr_get_value(struct udev_device *dev, const char *attr_name,
 	snprintf(devpath, PATH_SIZE, "%s/%s", udev_device_get_syspath(dev),
 		 attr_name);
 	condlog(4, "open '%s'", devpath);
-	if (stat(devpath, &statbuf) != 0) {
+	/* read attribute value */
+	fd = open(devpath, O_RDONLY);
+	if (fd < 0) {
+		condlog(4, "attribute '%s' can not be opened: %s",
+			devpath, strerror(errno));
+		return -errno;
+	}
+	if (fstat(fd, &statbuf) < 0) {
 		condlog(4, "stat '%s' failed: %s", devpath, strerror(errno));
+		close(fd);
 		return -ENXIO;
 	}
-
 	/* skip directories */
 	if (S_ISDIR(statbuf.st_mode)) {
 		condlog(4, "%s is a directory", devpath);
+		close(fd);
 		return -EISDIR;
 	}
-
 	/* skip non-writeable files */
 	if ((statbuf.st_mode & S_IRUSR) == 0) {
 		condlog(4, "%s is not readable", devpath);
+		close(fd);
 		return -EPERM;
 	}
 
-	/* read attribute value */
-	fd = open(devpath, O_RDONLY);
-	if (fd < 0) {
-		condlog(4, "attribute '%s' can not be opened: %s",
-			devpath, strerror(errno));
-		return -errno;
-	}
 	size = read(fd, value, value_len);
 	if (size < 0) {
 		condlog(4, "read from %s failed: %s", devpath, strerror(errno));
@@ -112,30 +113,33 @@ ssize_t sysfs_bin_attr_get_value(struct udev_device *dev, const char *attr_name,
 	snprintf(devpath, PATH_SIZE, "%s/%s", udev_device_get_syspath(dev),
 		 attr_name);
 	condlog(4, "open '%s'", devpath);
-	if (stat(devpath, &statbuf) != 0) {
+	/* read attribute value */
+	fd = open(devpath, O_RDONLY);
+	if (fd < 0) {
+		condlog(4, "attribute '%s' can not be opened: %s",
+			devpath, strerror(errno));
+		return -errno;
+	}
+	if (fstat(fd, &statbuf) != 0) {
 		condlog(4, "stat '%s' failed: %s", devpath, strerror(errno));
+		close(fd);
 		return -ENXIO;
 	}
 
 	/* skip directories */
 	if (S_ISDIR(statbuf.st_mode)) {
 		condlog(4, "%s is a directory", devpath);
+		close(fd);
 		return -EISDIR;
 	}
 
 	/* skip non-writeable files */
 	if ((statbuf.st_mode & S_IRUSR) == 0) {
 		condlog(4, "%s is not readable", devpath);
+		close(fd);
 		return -EPERM;
 	}
 
-	/* read attribute value */
-	fd = open(devpath, O_RDONLY);
-	if (fd < 0) {
-		condlog(4, "attribute '%s' can not be opened: %s",
-			devpath, strerror(errno));
-		return -errno;
-	}
 	size = read(fd, value, value_len);
 	if (size < 0) {
 		condlog(4, "read from %s failed: %s", devpath, strerror(errno));
@@ -163,30 +167,33 @@ ssize_t sysfs_attr_set_value(struct udev_device *dev, const char *attr_name,
 	snprintf(devpath, PATH_SIZE, "%s/%s", udev_device_get_syspath(dev),
 		 attr_name);
 	condlog(4, "open '%s'", devpath);
-	if (stat(devpath, &statbuf) != 0) {
+	/* write attribute value */
+	fd = open(devpath, O_WRONLY);
+	if (fd < 0) {
+		condlog(4, "attribute '%s' can not be opened: %s",
+			devpath, strerror(errno));
+		return -errno;
+	}
+	if (fstat(fd, &statbuf) != 0) {
 		condlog(4, "stat '%s' failed: %s", devpath, strerror(errno));
+		close(fd);
 		return -errno;
 	}
 
 	/* skip directories */
 	if (S_ISDIR(statbuf.st_mode)) {
 		condlog(4, "%s is a directory", devpath);
+		close(fd);
 		return -EISDIR;
 	}
 
 	/* skip non-writeable files */
 	if ((statbuf.st_mode & S_IWUSR) == 0) {
 		condlog(4, "%s is not writeable", devpath);
+		close(fd);
 		return -EPERM;
 	}
 
-	/* write attribute value */
-	fd = open(devpath, O_WRONLY);
-	if (fd < 0) {
-		condlog(4, "attribute '%s' can not be opened: %s",
-			devpath, strerror(errno));
-		return -errno;
-	}
 	size = write(fd, value, value_len);
 	if (size < 0) {
 		condlog(4, "write to %s failed: %s", devpath, strerror(errno));
Signature · 1 line
2.6.6
#16 lore raw

PATCH 16/39 libmultipath: fixup possible buffer overflow in alua_rtpg.c

In reply to Hannes Reinecke

We need to reserve an additional 4 bytes for the length of the response buffer, so add a proper range check to avoid accidental wrap-arounds.
Found by coverity.

---
 libmultipath/prioritizers/alua_rtpg.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
libmultipath/prioritizers/alua_rtpg.c +7 -1
index 636aae5..22b0d4f 100644
@@ -15,6 +15,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <limits.h>
 #include <sys/ioctl.h>
 #include <inttypes.h>
 #include <libudev.h>
@@ -219,6 +220,9 @@ get_target_port_group(struct path * pp)
 			goto out;
 
 		scsi_buflen = (buf[2] << 8 | buf[3]) + 4;
+		/* Paranoia */
+		if (scsi_buflen >= USHRT_MAX)
+			scsi_buflen = USHRT_MAX;
 		if (buflen < scsi_buflen) {
 			free(buf);
 			buf = (unsigned char *)malloc(scsi_buflen);
@@ -303,7 +307,7 @@ get_asymmetric_access_state(int fd, unsigned int tpg)
 	struct rtpg_tpg_dscr *	dscr;
 	int			rc;
 	int			buflen;
-	uint32_t		scsi_buflen;
+	uint64_t		scsi_buflen;
 
 	buflen = 4096;
 	buf = (unsigned char *)malloc(buflen);
@@ -317,6 +321,8 @@ get_asymmetric_access_state(int fd, unsigned int tpg)
 	if (rc < 0)
 		goto out;
 	scsi_buflen = (buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3]) + 4;
+	if (scsi_buflen > UINT_MAX)
+		scsi_buflen = UINT_MAX;
 	if (buflen < scsi_buflen) {
 		free(buf);
 		buf = (unsigned char *)malloc(scsi_buflen);
Signature · 1 line
2.6.6
#17 lore raw

PATCH 17/39 libmultipath: memory leak in iet.c

In reply to Hannes Reinecke

'pmatch' was never freed.
Found by coverity.

---
 libmultipath/prioritizers/iet.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
libmultipath/prioritizers/iet.c +2 -1
index 0bcc48b..3549665 100644
@@ -59,10 +59,11 @@ char *find_regex(char * string, char * regex)
 				if (result) {
 					strncpy(result, &string[start], size);
 					result[size] = '\0';
+					free(pmatch);
 					return result;
 				}
 			}
-			else return NULL;
+			free(pmatch);
 		}
 	}
 	return NULL;
Signature · 1 line
2.6.6
#18 lore raw

PATCH 18/39 libmpathcmd: limit size of the CLI buffer

In reply to Hannes Reinecke

A malicious program might inject a large buffer via the CLI and causing the daemon to abort due to OOM.
So limit the maximum CLI buffer size to 64k.
Found by coverity.

---
 libmpathcmd/mpath_cmd.c | 2 ++
 libmpathcmd/mpath_cmd.h | 1 +
 2 files changed, 3 insertions(+)
libmpathcmd/mpath_cmd.c +2 -0
index 1aaf5ea..2290ecb 100644
@@ -142,6 +142,8 @@ int mpath_recv_reply(int fd, char **reply, unsigned int timeout)
 	len = mpath_recv_reply_len(fd, timeout);
 	if (len <= 0)
 		return len;
+	if (len > MAX_REPLY_LEN)
+		return -EINVAL;
 	*reply = malloc(len);
 	if (!*reply)
 		return -1;
libmpathcmd/mpath_cmd.h +1 -0
index 4ec0291..f33f000 100644
@@ -28,6 +28,7 @@ extern "C" {
 
 #define DEFAULT_SOCKET		"/org/kernel/linux/storage/multipathd"
 #define DEFAULT_REPLY_TIMEOUT	1000
+#define MAX_REPLY_LEN		65536
 
 
 /*
Signature · 1 line
2.6.6
#19 lore raw

PATCH 19/39 libmpathpersist: Fix possible overrun in devt2devname()

In reply to Hannes Reinecke

pp->dev is of size FILE_NAME_SIZE, not PATH_SIZE.
Found by coverity.

---
 libmpathpersist/mpath_persist.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libmpathpersist/mpath_persist.c +1 -1
index d2c3e53..c4b8521 100644
@@ -84,7 +84,7 @@ updatepaths (struct multipath * mpp)
 
 		vector_foreach_slot (pgp->paths, pp, j){
 			if (!strlen(pp->dev)){
-				if (devt2devname(pp->dev, PATH_SIZE,
+				if (devt2devname(pp->dev, FILE_NAME_SIZE,
 						 pp->dev_t)){
 					/*
 					 * path is not in sysfs anymore
Signature · 1 line
2.6.6
#20 lore raw

PATCH 20/39 libmpathpersist: memory leak in mpath_persistent_reserve_(in, out)()

In reply to Hannes Reinecke

When either curmp or pathvec couldn't be allocated we never free up the other one, too.
Found by coverity.

---
 libmpathpersist/mpath_persist.c | 8 ++++++++
 1 file changed, 8 insertions(+)
libmpathpersist/mpath_persist.c +8 -0
index c4b8521..cfc2f0f 100644
@@ -195,6 +195,10 @@ int mpath_persistent_reserve_in (int fd, int rq_servact,
 	if (!curmp || !pathvec){
 		condlog (0, "%s: vector allocation failed.", alias);
 		ret = MPATH_PR_DMMP_ERROR;
+		if (curmp)
+			vector_free(curmp);
+		if (pathvec)
+			vector_free(pathvec);
 		goto out;
 	}
 
@@ -285,6 +289,10 @@ int mpath_persistent_reserve_out ( int fd, int rq_servact, int rq_scope,
 	if (!curmp || !pathvec){
 		condlog (0, "%s: vector allocation failed.", alias);
 		ret = MPATH_PR_DMMP_ERROR;
+		if (curmp)
+			vector_free(curmp);
+		if (pathvec)
+			vector_free(pathvec);
 		goto out;
 	}
 
Signature · 1 line
2.6.6
#21 lore raw

PATCH 21/39 libmpathpersist: Pass sensebuffer as pointer

In reply to Hannes Reinecke

It makes no sense to pass the entire sense buffer as a value, we should rather pass the pointer to avoid exploding the stack. Found by coverity.

---
 libmpathpersist/mpath_pr_ioctl.c | 35 +++++++++++++++++------------------
 1 file changed, 17 insertions(+), 18 deletions(-)
libmpathpersist/mpath_pr_ioctl.c +17 -18
index c85fd10..c37c1e4 100644
@@ -24,7 +24,8 @@
 int prin_do_scsi_ioctl(char * dev, int rq_servact, struct prin_resp *resp, int noisy);
 void mpath_format_readkeys(struct prin_resp *pr_buff, int len , int noisy);
 void mpath_format_readfullstatus(struct prin_resp *pr_buff, int len, int noisy);
-int mpath_translate_response (char * dev, struct sg_io_hdr io_hdr, SenseData_t Sensedata, int noisy);
+int mpath_translate_response (char * dev, struct sg_io_hdr io_hdr,
+			      SenseData_t *Sensedata, int noisy);
 void dumpHex(const char* str, int len, int no_ascii);
 int prout_do_scsi_ioctl( char * dev, int rq_servact, int rq_scope,
                 unsigned int rq_type, struct prout_param_descriptor *paramp, int noisy);
@@ -113,7 +114,7 @@ retry :
 
 	condlog(2, "%s: Duration=%u (ms)", dev, io_hdr.duration);
 
-	status = mpath_translate_response(dev, io_hdr, Sensedata, noisy);
+	status = mpath_translate_response(dev, io_hdr, &Sensedata, noisy);
 	condlog(3, "%s: status = %d", dev, status);
 
 	if (status == MPATH_PR_SENSE_UNIT_ATTENTION && (retry > 0))
@@ -353,7 +354,7 @@ retry :
 	condlog(2, "%s: duration = %u (ms)", dev, io_hdr.duration);
 	condlog(2, "%s: persistent reservation in: requested %d bytes but got %d bytes)", dev, mx_resp_len, got);
 
-	status = mpath_translate_response(dev, io_hdr, Sensedata, noisy);
+	status = mpath_translate_response(dev, io_hdr, &Sensedata, noisy);
 
 	if (status == MPATH_PR_SENSE_UNIT_ATTENTION && (retry > 0))
 	{
@@ -398,26 +399,25 @@ out:
 	return status;
 }
 
-int mpath_translate_response (char * dev, struct sg_io_hdr io_hdr, SenseData_t Sensedata, int noisy)
+int mpath_translate_response (char * dev, struct sg_io_hdr io_hdr,
+			      SenseData_t *Sensedata, int noisy)
 {
-	condlog(3, "%s: status driver:%02x host:%02x scsi:%02x", dev, 
-			io_hdr.driver_status, io_hdr.host_status ,io_hdr.status);
+	condlog(3, "%s: status driver:%02x host:%02x scsi:%02x", dev,
+		io_hdr.driver_status, io_hdr.host_status, io_hdr.status);
 	io_hdr.status &= 0x7e;
-	if ((0 == io_hdr.status) && (0 == io_hdr.host_status) &&
-			(0 == io_hdr.driver_status))
-	{
+	if ((0 == io_hdr.status) &&
+	    (0 == io_hdr.host_status) &&
+	    (0 == io_hdr.driver_status))
 		return MPATH_PR_SUCCESS;
-	}
 
-	switch(io_hdr.status)
-	{
+	switch(io_hdr.status) {
 		case SAM_STAT_GOOD:
 			break;
 		case SAM_STAT_CHECK_CONDITION:
-			condlog(2, "%s: Sense_Key=%02x, ASC=%02x ASCQ=%02x", dev,
-					Sensedata.Sense_Key, Sensedata.ASC, Sensedata.ASCQ);
-			switch(Sensedata.Sense_Key)
-			{
+			condlog(2, "%s: Sense_Key=%02x, ASC=%02x ASCQ=%02x",
+				dev, Sensedata->Sense_Key,
+				Sensedata->ASC, Sensedata->ASCQ);
+			switch(Sensedata->Sense_Key) {
 				case NO_SENSE:
 					return MPATH_PR_NO_SENSE;
 				case RECOVERED_ERROR:
@@ -450,8 +450,7 @@ int mpath_translate_response (char * dev, struct sg_io_hdr io_hdr, SenseData_t S
 			return  MPATH_PR_OTHER;
 	}
 
-	switch(io_hdr.host_status)
-	{
+	switch(io_hdr.host_status) {
 		case DID_OK :
 			break;
 		default :
Signature · 1 line
2.6.6
#22 lore raw

PATCH 22/39 libmpathpersist: Handle send error

In reply to Hannes Reinecke

Found by coverity.

---
 libmpathpersist/mpath_updatepr.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
libmpathpersist/mpath_updatepr.c +6 -2
index bda8991..0529d13 100644
@@ -33,10 +33,14 @@ int update_prflag(char * arg1, char * arg2, int noisy)
 
 	snprintf(str,sizeof(str),"map %s %s", arg1, arg2);
 	condlog (2, "%s: pr flag message=%s", arg1, str);
-	send_packet(fd, str);
+	if (send_packet(fd, str) != 0) {
+		condlog(2, "%s: message=%s send error=%d", arg1, str, errno);
+		mpath_disconnect(fd);
+		return -2;
+	}
 	ret = recv_packet(fd, &reply, DEFAULT_REPLY_TIMEOUT);
 	if (ret < 0) {
-		condlog(2, "%s: message=%s error=%d", arg1, str, errno);
+		condlog(2, "%s: message=%s recv error=%d", arg1, str, errno);
 		ret = -2;
 	} else {
 		condlog (2, "%s: message=%s reply=%s", arg1, str, reply);
Signature · 1 line
2.6.6
#23 lore raw

PATCH 23/39 libmultipath: missing break in hds prioritizer

In reply to Hannes Reinecke

Found by coverity.

---
 libmultipath/prioritizers/hds.c | 2 ++
 1 file changed, 2 insertions(+)
libmultipath/prioritizers/hds.c +2 -0
index 8043b5b..db8c4c0 100644
@@ -151,6 +151,7 @@ int hds_modular_prio (const char *dev, int fd)
 			return 0;
 			break;
 		}
+		break;
 	case '1': case '3': case '5': case '7': case '9':
 		switch (ldev[3]) {
 		case '0': case '2': case '4': case '6': case '8': case 'A': case 'C': case 'E':
@@ -162,6 +163,7 @@ int hds_modular_prio (const char *dev, int fd)
 			return 1;
 			break;
 		}
+		break;
 	}
 	return -1;
 }
Signature · 1 line
2.6.6
#24 lore raw

PATCH 24/39 kpartx: fixup fd leakage in lopart.c

In reply to Hannes Reinecke

We're leaking filedescriptors when trying to figure out the loop device.
Found by coverity.

---
 kpartx/lopart.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
kpartx/lopart.c +7 -8
index 39add44..e7f6090 100644
@@ -154,13 +154,15 @@ find_unused_loop_device (void)
 	while (next_loop_dev == NULL) {
 		if (stat("/dev/loop-control", &statbuf) == 0 &&
 		    S_ISCHR(statbuf.st_mode)) {
-			fd = open("/dev/loop-control", O_RDWR);
-			if (fd < 0)
+			int next_loop_fd;
+
+			next_loop_fd = open("/dev/loop-control", O_RDWR);
+			if (next_loop_fd < 0)
 				return NULL;
-			next_loop = ioctl(fd, LOOP_CTL_GET_FREE);
+			next_loop = ioctl(next_loop_fd, LOOP_CTL_GET_FREE);
+			close(next_loop_fd);
 			if (next_loop < 0)
 				return NULL;
-			close(fd);
 		}
 
 		sprintf(dev, "/dev/loop%d", next_loop);
@@ -173,11 +175,8 @@ find_unused_loop_device (void)
 
 				if(ioctl (fd, LOOP_GET_STATUS, &loopinfo) == 0)
 					someloop++;		/* in use */
-
-				else if (errno == ENXIO) {
-					close (fd);
+				else if (errno == ENXIO)
 					next_loop_dev = xstrdup(dev);
-				}
 
 				close (fd);
 			}
Signature · 1 line
2.6.6
#25 lore raw

PATCH 25/39 kpartx: Memory leak in get_hotplug_device()

In reply to Hannes Reinecke

We allocate a string for 'device', but never free it on error. And the string 'mapname' is never freed at all. Found by coverity.

---
 kpartx/kpartx.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
kpartx/kpartx.c +8 -3
index e8c35d4..e1ebc96 100644
@@ -178,16 +178,21 @@ get_hotplug_device(void)
 	len = strlen(mapname);
 
 	/* Dirname + mapname + \0 */
-	if (!(device = (char *)malloc(sizeof(char) * (off + len + 1))))
+	if (!(device = (char *)malloc(sizeof(char) * (off + len + 1)))) {
+		free(mapname);
 		return NULL;
+	}
 
 	/* Create new device name. */
 	snprintf(device, off + 1, "%s", devname);
 	snprintf(device + off, len + 1, "%s", mapname);
 
-	if (strlen(device) != (off + len))
+	if (strlen(device) != (off + len)) {
+		free(device);
+		free(mapname);
 		return NULL;
-
+	}
+	free(mapname);
 	return device;
 }
 
Signature · 1 line
2.6.6
#26 lore raw

PATCH 26/39 kpartx: fixup filedescriptor leak in set_loop()

In reply to Hannes Reinecke

Upon error 'ffd' was never closed.
Found by coverity.

---
 kpartx/lopart.c | 1 +
 1 file changed, 1 insertion(+)
kpartx/lopart.c +1 -0
index e7f6090..5495e27 100644
@@ -252,6 +252,7 @@ set_loop (const char *device, const char *file, int offset, int *loopro)
 	}
 
 	if ((fd = open (device, mode)) < 0) {
+		close(ffd);
 		perror (device);
 		return 1;
 	}
Signature · 1 line
2.6.6
#27 lore raw

PATCH 27/39 kpartx: do not use 'const' for mapname in get_hotplug_device()

In reply to Hannes Reinecke
---
 kpartx/kpartx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kpartx/kpartx.c +1 -1
index e1ebc96..4de13fa 100644
@@ -150,7 +150,7 @@ static char *
 get_hotplug_device(void)
 {
 	unsigned int major, minor, off, len;
-	const char *mapname;
+	char *mapname;
 	char *devname = NULL;
 	char *device = NULL;
 	char *var = NULL;
Signature · 1 line
2.6.6
#28 lore raw

PATCH 28/39 kpartx: Handle errors from lseek()

In reply to Hannes Reinecke

Found by coverity.

---
 kpartx/gpt.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kpartx/gpt.c +3 -2
index 5a54970..3dac605 100644
@@ -206,9 +206,10 @@ read_lba(int fd, uint64_t lba, void *buffer, size_t bytes)
 	int sector_size = get_sector_size(fd);
 	off_t offset = lba * sector_size;
 	uint64_t lastlba;
-        ssize_t bytesread;
+	ssize_t bytesread;
 
-	lseek(fd, offset, SEEK_SET);
+	if (lseek(fd, offset, SEEK_SET) < 0)
+		return 0;
 	bytesread = read(fd, buffer, bytes);
 
 	lastlba = last_lba(fd);
Signature · 1 line
2.6.6
#29 lore raw

PATCH 29/39 kpartx/devmapper.c: Remove unused 'next' argument

In reply to Hannes Reinecke

Found by coverity.

---
 kpartx/devmapper.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
kpartx/devmapper.c +2 -3
index c5ba39d..382f511 100644
@@ -311,7 +311,6 @@ dm_get_map(int major, int minor, char * outparams)
 {
 	int r = 1;
 	struct dm_task *dmt;
-	void *next = NULL;
 	uint64_t start, length;
 	char *target_type = NULL;
 	char *params = NULL;
@@ -327,8 +326,8 @@ dm_get_map(int major, int minor, char * outparams)
 		goto out;
 
 	/* Fetch 1st target */
-	next = dm_get_next_target(dmt, next, &start, &length,
-				  &target_type, &params);
+	dm_get_next_target(dmt, NULL, &start, &length,
+			   &target_type, &params);
 
 	if (snprintf(outparams, PARAMS_SIZE, "%s", params) <= PARAMS_SIZE)
 		r = 0;
Signature · 1 line
2.6.6
#30 lore raw

PATCH 30/39 kpartx: use fstat() when reading sysfs attributes

In reply to Hannes Reinecke

Coverity pointed out that we shouldn't be using stat()/open(), as the file might have vanished or changed in between those two calls. So modify it to open()/fstat() instead.

---
 kpartx/lopart.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
kpartx/lopart.c +6 -4
index 5495e27..d4a2ab4 100644
@@ -117,14 +117,16 @@ find_loop_by_file (const char * filename)
 			continue;
 		sprintf(dev, "/dev/%s", dent->d_name);
 
-		if (stat (dev, &statbuf) != 0 ||
-		    !S_ISBLK(statbuf.st_mode))
-			continue;
-
 		fd = open (dev, O_RDONLY);
 		if (fd < 0)
 			break;
 
+		if (fstat (fd, &statbuf) != 0 ||
+		    !S_ISBLK(statbuf.st_mode)) {
+			close (fd);
+			continue;
+		}
+
 		if (ioctl (fd, LOOP_GET_STATUS, &loopinfo) != 0) {
 			close (fd);
 			continue;
Signature · 1 line
2.6.6
#31 lore raw

PATCH 31/39 kpartx/lopart.c: use fstat() when reading device nodes

In reply to Hannes Reinecke

Coverity pointed out that we shouldn't be using stat()/open(), as the file might have vanished or changed in between those two calls. So modify it to open()/fstat() instead.

---
 kpartx/lopart.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
kpartx/lopart.c +6 -7
index d4a2ab4..f29cfc1 100644
@@ -169,19 +169,18 @@ find_unused_loop_device (void)
 
 		sprintf(dev, "/dev/loop%d", next_loop);
 
-		if (stat (dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
-			somedev++;
-			fd = open (dev, O_RDONLY);
-
-			if (fd >= 0) {
-
+		fd = open (dev, O_RDONLY);
+		if (fd >= 0) {
+			if (fstat (fd, &statbuf) == 0 &&
+			    S_ISBLK(statbuf.st_mode)) {
+				somedev++;
 				if(ioctl (fd, LOOP_GET_STATUS, &loopinfo) == 0)
 					someloop++;		/* in use */
 				else if (errno == ENXIO)
 					next_loop_dev = xstrdup(dev);
 
-				close (fd);
 			}
+			close (fd);
 
 			/* continue trying as long as devices exist */
 			continue;
Signature · 1 line
2.6.6
#32 lore raw

PATCH 32/39 libmultipath/sysfs.c: always terminate value from sysfs_attr_get_value()

In reply to Hannes Reinecke

Coverity complained that the returned value is not always NULL terminated.

---
 libmultipath/sysfs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
libmultipath/sysfs.c +3 -2
index a74de9f..2c0b591 100644
@@ -86,16 +86,17 @@ ssize_t sysfs_attr_get_value(struct udev_device *dev, const char *attr_name,
 	if (size < 0) {
 		condlog(4, "read from %s failed: %s", devpath, strerror(errno));
 		size = -errno;
+		value[0] = '\0';
 	} else if (size == value_len) {
+		value[size - 1] = '\0';
 		condlog(4, "overflow while reading from %s", devpath);
 		size = 0;
 	} else {
 		value[size] = '\0';
+		size = strchop(value);
 	}
 
 	close(fd);
-	if (size > 0)
-		size = strchop(value);
 	return size;
 }
 
Signature · 1 line
2.6.6
#33 lore raw

PATCH 33/39 multipathd: missing mpath_disconnect() in uxclnt()

In reply to Hannes Reinecke

We should be calling mpath_disconnect() in uxclnt(). Found by coverity.

---
 multipathd/uxclnt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
multipathd/uxclnt.c +1 -1
index 06c1bf8..37afaac 100644
@@ -135,6 +135,6 @@ int uxclnt(char * inbuf, unsigned int timeout)
 		process_req(fd, inbuf, timeout);
 	else
 		process(fd, timeout);
-
+	mpath_disconnect(fd);
 	return 0;
 }
Signature · 1 line
2.6.6
#34 lore raw

PATCH 34/39 multipathd: pid fd resource leak

In reply to Hannes Reinecke

We open a PID filedescriptor, but never close them. While this is not an error per se coverity complains here. So be a good guy and close the PID fd properly.

---
 multipathd/main.c    | 6 +++++-
 multipathd/pidfile.c | 6 +++---
 2 files changed, 8 insertions(+), 4 deletions(-)
multipathd/main.c +5 -1
index 0eadd70..8285f0d 100644
@@ -2085,6 +2085,7 @@ child (void * param)
 	unsigned long checkint;
 #endif
 	int rc;
+	int pid_fd = -1;
 	char *envp;
 
 	mlockall(MCL_CURRENT | MCL_FUTURE);
@@ -2101,7 +2102,8 @@ child (void * param)
 		log_thread_start(&log_attr);
 		pthread_attr_destroy(&log_attr);
 	}
-	if (pidfile_create(DEFAULT_PIDFILE, daemon_pid)) {
+	pid_fd = pidfile_create(DEFAULT_PIDFILE, daemon_pid);
+	if (pid_fd < 0) {
 		condlog(1, "failed to create pidfile");
 		if (logsink == 1)
 			log_thread_stop();
@@ -2307,6 +2309,8 @@ failed:
 #ifdef USE_SYSTEMD
 	sd_notify(0, "ERRNO=1");
 #endif
+	if (pid_fd >= 0)
+		close(pid_fd);
 	exit(1);
 }
 
multipathd/pidfile.c +3 -3
index e3fb896..e911358 100644
@@ -22,7 +22,7 @@ int pidfile_create(const char *pidFile, pid_t pid)
 		       (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) < 0) {
 		condlog(0, "Cannot open pidfile [%s], error was [%s]",
 			pidFile, strerror(errno));
-		return 1;
+		return -errno;
 	}
 	lock.l_type = F_WRLCK;
 	lock.l_start = 0;
@@ -60,8 +60,8 @@ int pidfile_create(const char *pidFile, pid_t pid)
 			"error was [%s]", pidFile, strerror(errno));
 		goto fail;
 	}
-	return 0;
+	return fd;
 fail:
 	close(fd);
-	return 1;
+	return -errno;
 }
Signature · 1 line
2.6.6
#35 lore raw

PATCH 35/39 libmultipath: mark return value from dup() as unused

In reply to Hannes Reinecke

We are redirecting stderr, and can safely ignore any errors from dup() here. So mark the return value as unused.
Found by coverity.

---
 libmultipath/callout.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libmultipath/callout.c +3 -1
index d671b0c..4d1b067 100644
@@ -76,8 +76,10 @@ int execute_program(char *path, char *value, int len)
 		/* Ignore writes to stderr */
 		null_fd = open("/dev/null", O_WRONLY);
 		if (null_fd > 0) {
+			int err_fd __attribute__ ((unused));
+
 			close(STDERR_FILENO);
-			retval = dup(null_fd);
+			err_fd = dup(null_fd);
 			close(null_fd);
 		}
 
Signature · 1 line
2.6.6
#36 lore raw

PATCH 36/39 libmultipath: Ignore errors from fcntl() in directio.c

In reply to Hannes Reinecke

Mark the return value from fcntl() as unused to avoid compiler warnings.
Found by coverity.

---
 libmultipath/checkers/directio.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libmultipath/checkers/directio.c +3 -1
index 46fe6a7..94bf8f7 100644
@@ -104,9 +104,11 @@ void libcheck_free (struct checker * c)
 
 	if (ct->reset_flags) {
 		if ((flags = fcntl(c->fd, F_GETFL)) >= 0) {
+			int ret __attribute__ ((unused));
+
 			flags &= ~O_DIRECT;
 			/* No point in checking for errors */
-			fcntl(c->fd, F_SETFL, flags);
+			ret = fcntl(c->fd, F_SETFL, flags);
 		}
 	}
 
Signature · 1 line
2.6.6
#37 lore raw

PATCH 37/39 libmultipath: add spin_lock in tur.c

In reply to Hannes Reinecke

When failing to start the pthread we still should be taking the spin lock; there might be other threads pending. Found by coverity.

---
 libmultipath/checkers/tur.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libmultipath/checkers/tur.c +3 -1
index bd7372d..2edc8ad 100644
@@ -331,9 +331,11 @@ libcheck_check (struct checker * c)
 		setup_thread_attr(&attr, 32 * 1024, 1);
 		r = pthread_create(&ct->thread, &attr, tur_thread, ct);
 		if (r) {
+			pthread_spin_lock(&ct->hldr_lock);
+			ct->holders--;
+			pthread_spin_unlock(&ct->hldr_lock);
 			pthread_mutex_unlock(&ct->lock);
 			ct->thread = 0;
-			ct->holders--;
 			condlog(3, "%d:%d: failed to start tur thread, using"
 				" sync mode", TUR_DEVT(ct));
 			return tur_check(c->fd, c->timeout, c->message);
Signature · 1 line
2.6.6
#38 lore raw

PATCH 38/39 libmultipath/devmapper.c: fixup possible buffer overflow

In reply to Hannes Reinecke

Fixup possible buffer overflow in dm_reassign_table(). Found by coverity.

---
 libmultipath/devmapper.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
libmultipath/devmapper.c +13 -5
index 0223e18..926d2f5 100644
@@ -1434,15 +1434,18 @@ out:
 void dm_reassign_deps(char *table, char *dep, char *newdep)
 {
 	char *p, *n;
-	char newtable[PARAMS_SIZE];
+	char *newtable;
 
-	strcpy(newtable, table);
+	newtable = strdup(table);
+	if (!newtable)
+		return;
 	p = strstr(newtable, dep);
 	n = table + (p - newtable);
 	strcpy(n, newdep);
 	n += strlen(newdep);
 	p += strlen(dep);
 	strcat(n, p);
+	free(newtable);
 }
 
 int dm_reassign_table(const char *name, char *old, char *new)
@@ -1451,7 +1454,7 @@ int dm_reassign_table(const char *name, char *old, char *new)
 	uint64_t start, length;
 	struct dm_task *dmt, *reload_dmt;
 	char *target, *params = NULL;
-	char buff[PARAMS_SIZE];
+	char *buff;
 	void *next = NULL;
 
 	if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
@@ -1472,8 +1475,12 @@ int dm_reassign_table(const char *name, char *old, char *new)
 	do {
 		next = dm_get_next_target(dmt, next, &start, &length,
 					  &target, &params);
-		memset(buff, 0, PARAMS_SIZE);
-		strcpy(buff, params);
+		buff = strdup(params);
+		if (!buff) {
+			condlog(3, "%s: failed to replace target %s, "
+				"out of memory", name, target);
+			goto out_reload;
+		}
 		if (strcmp(target, TGT_MPATH) && strstr(params, old)) {
 			condlog(3, "%s: replace target %s %s",
 				name, target, buff);
@@ -1483,6 +1490,7 @@ int dm_reassign_table(const char *name, char *old, char *new)
 			modified++;
 		}
 		dm_task_add_target(reload_dmt, start, length, target, buff);
+		free(buff);
 	} while (next);
 
 	if (modified) {
Signature · 1 line
2.6.6
#39 lore raw

PATCH 39/39 libmultipath/discovery.c: Fixup possible range overflow

In reply to Hannes Reinecke

When checking the maximum value for dev_loss_tmo we need to use a larger type, otherwise the comparison will always be false. Found by coverity.

---
 libmultipath/discovery.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libmultipath/discovery.c +1 -1
index 5d5441d..126a54f 100644
@@ -670,7 +670,7 @@ sysfs_set_scsi_tmo (struct multipath *mpp)
 	int dev_loss_tmo = mpp->dev_loss;
 
 	if (mpp->no_path_retry > 0) {
-		int no_path_retry_tmo = mpp->no_path_retry * conf->checkint;
+		uint64_t no_path_retry_tmo = mpp->no_path_retry * conf->checkint;
 
 		if (no_path_retry_tmo > MAX_DEV_LOSS_TMO)
 			no_path_retry_tmo = MAX_DEV_LOSS_TMO;
Signature · 1 line
2.6.6
#40 lore raw
In reply to Hannes Reinecke
Quoted 9 lines · Hannes Reinecke

Hi all,

here's a bunch of fixes from a recent coverity run. Nothing critical, mostly memory leaks and possible buffer overflows. And the usual missing return value check.

As usual, comments and reviews are welcome.

Ping?
I've got another patchset (converting config accesses to RCU) which sort of relies on this one...

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke		   Teamlead Storage & Networking
[email protected]			               +49 911 74053 688
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: F. Imendörffer, J. Smithard, J. Guild, D. Upmanyu, G. Norton
HRB 21284 (AG Nürnberg)
#41 lore raw

I pulled the coverity branch today.
Thanks.

Quoted 24 lines · Hannes Reinecke
Quoted 9 lines · Hannes Reinecke

Hi all,

here's a bunch of fixes from a recent coverity run. Nothing critical, mostly memory leaks and possible buffer overflows. And the usual missing return value check.

As usual, comments and reviews are welcome.

Ping?
I've got another patchset (converting config accesses to RCU) which sort of relies on this one...

Cheers,

Hannes
--
Dr. Hannes Reinecke Teamlead Storage & Networking
[email protected] +49 911 74053 688
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: F. Imendörffer, J. Smithard, J. Guild, D. Upmanyu, G. Norton HRB 21284 (AG Nürnberg)