Skip to contentSkip to conversation

multipath-tools: gcc9, VPD parsing, and get_uid fixes

PATCH Superseded Martin Wilck 3 participants 36 messages Jun 7, 2019 – Jun 24, 2019 Device mapper

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover multipath-tools: gcc9, VPD parsing, and get_uid fixes

Hi Christophe, hi Ben,

This series started out with me trying to fix a couple of warnings emitted by gcc 9, and grew substantially while I encountered other issues. Most of this series, except patch 25ff, are cleanups, corner case fixes, and unit tests.

The parts of the series with user visible impact are 25, 26, and 29. Patch 25/30 implements an earlier idea of mine
(https://www.redhat.com/archives/dm-devel/2019-April/msg00005.html).
The idea for patch 29/30 has been discussed before, too
(https://www.redhat.com/archives/dm-devel/2019-March/msg00201.html)
The fix for uid_attrs (26/30) occured to me while I was working on the get_uid() code path.

Patch 1/30 up to 8/30 are more or less straightforward fixes for gcc warnings related to string operations. See
https://developers.redhat.com/blog/2018/05/24/detecting-string-truncation-with-gcc-8/
(not sure why it says gcc 8 there, I'm seeing these warnings with gcc9 only). Most of the gcc warnings have been "solved" by replacing strncpy() calls with strlcpy(). That has the disadvantage that the compiler's static checking, which caused the warnings in the first place, is now simply disabled. Yet in those places where I did the replacement, I reckon that strlcpy() semantics is what we actually want. Most of the time, we try to ensure that "wwid" fields are properly 0-terminated. The previous code attempted to do this using constructs like

strncpy(x->wwid, y->wwid, WWID_SIZE - 1)

relying on x[WWID_SIZE-1] being 0 already. gcc 9 doesn't appreciate this style, and I tend to agree.

This lead me to check the code paths where wwid fields are originally set, resulting in patch 9/30 and the series from 14/30 to 24/30. In particular the VPD parsing code was full of small mistakes if the possibility of buffer overflow is taken into account (unlikely to hurt in practice, as our default WWID_SIZE is large enough to hold almost every WWID). In order not to break stuff, I added unit test code (10-13). The tests, in turn, made me find some more minor problems in the VPD parsing code (patches 14, 18, 21, 22, 24).

Reviews and comments welcome.

Regards,
Martin

Martin Wilck (30):
kpartx: dasd: fix -Waddress-of-packed-member warning from gcc9
libmultipath: fix gcc -Wstringop-truncation warning in set_value()
libmultipath: remove 'space' argument to merge_words()
libmultipath: fix -Wstringop-overflow warning in merge_words()
multipath-tools: fix more gcc 9 -Wstringop-truncation warnings
multipath-tools: Fix more strncpy(X, Y, size - 1) calls
libmpathcmd: use target length in strncpy() call
libmultipath: inline set_default()
libmultipath: add size argument to dm_get_uuid() multipath-tools tests: omit timestamp in log messages tests/hwtable: decrease log verbosity
multipath-tools tests: add strlcpy() tests multipath-tools tests: add test for VPD parsing
libmultipath: fix parsing of VPD 83 type 1 (T10 vendor ID)
libmultipath: Fix buffer overflow in parse_vpd_pg80()
libmultipath: fix possible WWID overflow in parse_vpd_pg83()
libmultipath: fix another WWID overflow in parse_vpd_pg83()
libmultipath: fix parsing of SCSI name string, iqn format
libmultipath: add consistent WWID overflow logging in parse_vpd_pg83
libmultipath: parse_vpd_pg83: common code for SCSI string parsing
libmultipath: allow zero-padded SCSI names in parse_vpd_pg83()
libmultipath: fix parsing of non-space-terminated T10 ID
libmultipath: parse_vpd_pg80: fix overflow output
libmultipath: parse_vpd_pg80: fix whitespace handling
libmultipath: get_uid: straighten the fallback logic
libmultipath: fix has_uid_fallback() logic
libmultipath: fix memory leak with "uid_attrs" config option multipath-tools tests: add test for uid_attrs parsing
libmultipath: more cautious blacklisting by missing property multipath.conf.5: Improve documentation of WWID determination

 kpartx/dasd.h                   |   2 +-
 libmpathcmd/mpath_cmd.c         |   2 +-
 libmpathpersist/mpath_persist.c |  10 +-
 libmultipath/blacklist.c        |  26 +-
 libmultipath/blacklist.h        |   2 +-
 libmultipath/config.c           |  51 ++-
 libmultipath/config.h           |   6 +-
 libmultipath/configure.c        |  17 +-
 libmultipath/debug.c            |   1 +
 libmultipath/defaults.c         |  17 -
 libmultipath/defaults.h         |  10 +-
 libmultipath/devmapper.c        |  28 +-
 libmultipath/devmapper.h        |   2 +-
 libmultipath/dict.c             |  36 +-
 libmultipath/discovery.c        | 162 ++++---
 libmultipath/dmparser.c         |  51 +--
 libmultipath/hwtable.c          |   2 +-
 libmultipath/parser.c           |  20 +-
 libmultipath/prio.c             |   3 +-
 libmultipath/propsel.c          |   4 +-
 libmultipath/structs_vec.c      |   3 +-
 libmultipath/uevent.c           |   5 +-
 libmultipath/util.c             |  44 +-
 libmultipath/util.h             |   1 -
 libmultipath/uxsock.c           |   2 +-
 libmultipath/wwids.c            |   3 +-
 multipath/multipath.conf.5      |  56 ++-
 multipathd/main.c               |   2 +-
 multipathd/waiter.c             |   3 +-
 tests/Makefile                  |   4 +-
 tests/blacklist.c               |  39 +-
 tests/globals.c                 |   3 +-
 tests/hwtable.c                 |   6 +-
 tests/uevent.c                  |  27 ++
 tests/util.c                    | 142 ++++++
 tests/vpd.c                     | 790 ++++++++++++++++++++++++++++++++
 36 files changed, 1332 insertions(+), 250 deletions(-)
 create mode 100644 tests/vpd.c
Signature · 1 line
2.21.0
#1 lore raw

PATCH 1/30 kpartx: dasd: fix -Waddress-of-packed-member warning from gcc9

Fixes the following warning:

dasd.c: In function ‘read_dasd_pt’:
dasd.c:206:3: warning: converting a packed ‘volume_label_t’ {aka ‘struct volume_label’} pointer (alignment 1) to a ‘label_ints_t’ {aka ‘unsigned int’} pointer (alignment 4) may result in an unaligned pointer value [-Waddress-of-packed-member]

  206 |   label_ints_t *label = (label_ints_t *) &vlabel;
      |   ^~~~~~~~~~~~

As volume_label_t is only used in read_dasd_pt(), and filled with memcpy(), increasing its alignment should be safe.

---
 kpartx/dasd.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kpartx/dasd.h +1 -1
index 749af49a..8ad5d620 100644
@@ -68,7 +68,7 @@ typedef struct volume_label
 	char res3[28];	        /* reserved                                  */
 	uint8_t ldl_version;    /* version number, valid for ldl format      */
 	uint64_t formatted_blocks; /* valid when ldl_version >= f2           */
-} __attribute__ ((packed)) volume_label_t;
+} __attribute__ ((packed, aligned(__alignof__(int)))) volume_label_t;
 
 
 typedef struct extent
Signature · 6 lines
2.21.0

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

PATCH 2/30 libmultipath: fix gcc -Wstringop-truncation warning in set_value()

In reply to Martin Wilck

Fixes the following warning:

In function ‘strncat’,
inlined from ‘set_value’ at parser.c:382:3:
/usr/include/bits/string_fortified.h:136:10: warning: ‘__builtin_strncat’
output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]

  136 |   return __builtin___strncat_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
parser.c: In function ‘set_value’:
parser.c:382:3: note: length computed here
  382 |   strncat(alloc, str, strlen(str));
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

gcc's stringop checker expects that the size argument of strncat() is derived from the destination, not source, side.
See https://developers.redhat.com/blog/2018/05/24/detecting-string-truncation-with-gcc-8/

Fix typo in error message along the way.

---
 libmultipath/parser.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)
libmultipath/parser.c +9 -11
index 92ef7cf5..e00c5fff 100644
@@ -345,17 +345,13 @@ set_value(vector strvec)
 		if (alloc)
 			memcpy(alloc, str, size);
 		else
-			condlog(0, "can't allocate memeory for option '%s'",
-				(char *)VECTOR_SLOT(strvec, 0));
+			goto oom;
 		return alloc;
 	}
 	/* Even empty quotes counts as a value (An empty string) */
 	alloc = (char *) MALLOC(sizeof (char));
-	if (!alloc) {
-		condlog(0, "can't allocate memeory for option '%s'",
-			(char *)VECTOR_SLOT(strvec, 0));
-		return NULL;
-	}
+	if (!alloc)
+		goto oom;
 	for (i = 2; i < VECTOR_SIZE(strvec); i++) {
 		str = VECTOR_SLOT(strvec, i);
 		if (!str) {
@@ -373,15 +369,17 @@ set_value(vector strvec)
 		alloc = REALLOC(alloc, sizeof (char) * len);
 		if (!alloc) {
 			FREE(tmp);
-			condlog(0, "can't allocate memeory for option '%s'",
-				(char *)VECTOR_SLOT(strvec, 0));
-			return NULL;
+			goto oom;
 		}
 		if (*alloc != '\0')
 			strncat(alloc, " ", 1);
-		strncat(alloc, str, strlen(str));
+		strncat(alloc, str, len - strlen(alloc) - 1);
 	}
 	return alloc;
+oom:
+	condlog(0, "can't allocate memory for option '%s'",
+		(char *)VECTOR_SLOT(strvec, 0));
+	return NULL;
 }
 
 /* non-recursive configuration stream handler */
Signature · 6 lines
2.21.0

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

PATCH 3/30 libmultipath: remove 'space' argument to merge_words()

In reply to Martin Wilck

merge_words() is always called with space = 1. Remove the argument.

---
 libmultipath/dmparser.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
libmultipath/dmparser.c +7 -11
index ac13ec06..04f675c1 100644
@@ -18,12 +18,12 @@
 #define WORD_SIZE 64
 
 static int
-merge_words (char ** dst, char * word, int space)
+merge_words(char **dst, char *word)
 {
 	char * p = *dst;
 	int len;
 
-	len = strlen(*dst) + strlen(word) + space;
+	len = strlen(*dst) + strlen(word) + 1;
 	*dst = REALLOC(*dst, len + 1);
 
 	if (!*dst) {
@@ -36,11 +36,8 @@ merge_words (char ** dst, char * word, int space)
 	while (*p != '\0')
 		p++;
 
-	while (space) {
-		*p = ' ';
-		p++;
-		space--;
-	}
+	*p = ' ';
+	++p;
 	strncpy(p, word, strlen(word) + 1);
 
 	return 0;
@@ -163,7 +160,7 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 		if (!word)
 			return 1;
 
-		if (merge_words(&mpp->features, word, 1)) {
+		if (merge_words(&mpp->features, word)) {
 			FREE(word);
 			return 1;
 		}
@@ -187,7 +184,7 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 		if (!word)
 			return 1;
 
-		if (merge_words(&mpp->hwhandler, word, 1)) {
+		if (merge_words(&mpp->hwhandler, word)) {
 			FREE(word);
 			return 1;
 		}
@@ -248,9 +245,8 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 
 			num_pg_args = atoi(word);
 
-			if (merge_words(&mpp->selector, word, 1)) {
+			if (merge_words(&mpp->selector, word))
 				goto out1;
-			}
 			FREE(word);
 		} else {
 			p += get_word(p, NULL);
Signature · 1 line
2.21.0
#4 lore raw

PATCH 4/30 libmultipath: fix -Wstringop-overflow warning in merge_words()

In reply to Martin Wilck

Fixes the following warning from gcc 9:

In file included from /usr/include/string.h:494,
from dmparser.c:8:
In function ‘strncpy’,
inlined from ‘merge_words’ at dmparser.c:41:2:
/usr/include/bits/string_fortified.h:106:10: warning: ‘__builtin_strncpy’
specified bound depends on the length of the source argument [-Wstringop-overflow=]

  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dmparser.c: In function ‘merge_words’:
dmparser.c:41:19: note: length computed here
   41 |  strncpy(p, word, strlen(word) + 1);
      |                   ^~~~~~~~~~~~
---
 libmultipath/dmparser.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
libmultipath/dmparser.c +6 -9
index 04f675c1..a8b0b71a 100644
@@ -21,24 +21,21 @@ static int
 merge_words(char **dst, char *word)
 {
 	char * p = *dst;
-	int len;
+	int len, dstlen;
 
-	len = strlen(*dst) + strlen(word) + 1;
-	*dst = REALLOC(*dst, len + 1);
+	dstlen = strlen(*dst);
+	len = dstlen + strlen(word) + 2;
+	*dst = REALLOC(*dst, len);
 
 	if (!*dst) {
 		free(p);
 		return 1;
 	}
 
-	p = *dst;
-
-	while (*p != '\0')
-		p++;
-
+	p = *dst + dstlen;
 	*p = ' ';
 	++p;
-	strncpy(p, word, strlen(word) + 1);
+	strncpy(p, word, len - dstlen - 1);
 
 	return 0;
 }
Signature · 6 lines
2.21.0

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

PATCH 5/30 multipath-tools: fix more gcc 9 -Wstringop-truncation warnings

In reply to Martin Wilck

More often than not, this means replacing strncpy() by strlcpy().

---
 libmpathpersist/mpath_persist.c |  8 ++++----
 libmultipath/configure.c        |  7 ++++---
 libmultipath/discovery.c        | 12 ++++++------
 libmultipath/dmparser.c         | 20 +++++++++-----------
 libmultipath/util.c             |  2 +-
 libmultipath/uxsock.c           |  2 +-
 6 files changed, 25 insertions(+), 26 deletions(-)
libmpathpersist/mpath_persist.c +4 -4
index 599c5e9e..4abcaed5 100644
@@ -497,8 +497,8 @@ int mpath_prout_reg(struct multipath *mpp,int rq_servact, int rq_scope,
 				if (k < count)
 					continue;
 			}
-			strncpy(thread[count].param.dev, pp->dev,
-				FILE_NAME_SIZE - 1);
+			strlcpy(thread[count].param.dev, pp->dev,
+				FILE_NAME_SIZE);
 
 			if (count && (thread[count].param.paramp->sa_flags & MPATH_F_SPEC_I_PT_MASK)){
 				/*
@@ -686,8 +686,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 - 1);
+			strlcpy(thread[count].param.dev, pp->dev,
+				FILE_NAME_SIZE);
 			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 +4 -3
index af4d78de..dfee7d24 100644
@@ -73,7 +73,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 - 1);
+		strlcpy(agp->adapter_name, adapter_name1, SLOT_NAME_SIZE);
 		store_adaptergroup(adapters, agp);
 
 		/* create a new host port group
@@ -667,7 +667,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 - 1);
+		strlcpy(cmpp_by_name->wwid, mpp->wwid, WWID_SIZE);
 		drop_multipath(curmp, cmpp_by_name->wwid, KEEP_PATHS);
 		mpp->action = ACT_CREATE;
 		condlog(3, "%s: set ACT_CREATE (map wwid change)",
@@ -1451,7 +1451,8 @@ int get_refwwid(enum mpath_cmds cmd, char *dev, enum devtypes dev_type,
 
 		conf = get_multipath_config();
 		pthread_cleanup_push(put_multipath_config, conf);
-		if (((dm_get_uuid(dev, tmpwwid)) == 0) && (strlen(tmpwwid))) {
+		if (((dm_get_uuid(dev, tmpwwid, WWID_SIZE)) == 0)
+		    && (strlen(tmpwwid))) {
 			refwwid = tmpwwid;
 			goto check;
 		}
libmultipath/discovery.c +6 -6
index 51ca2306..407e64a0 100644
@@ -297,8 +297,8 @@ sysfs_get_timeout(const struct path *pp, unsigned int *timeout)
 	return 1;
 }
 
-int
-sysfs_get_tgt_nodename (struct path *pp, char * node)
+static int
+sysfs_get_tgt_nodename(struct path *pp, char *node)
 {
 	const char *tgtname, *value;
 	struct udev_device *parent, *tgtdev;
@@ -322,7 +322,7 @@ sysfs_get_tgt_nodename (struct path *pp, char * node)
 		if (tgtid >= 0) {
 			pp->sg_id.proto_id = SCSI_PROTOCOL_SAS;
 			pp->sg_id.transport_id = tgtid;
-			strncpy(node, value, NODE_NAME_SIZE);
+			strlcpy(node, value, NODE_NAME_SIZE);
 			return 0;
 		}
 	}
@@ -334,7 +334,7 @@ sysfs_get_tgt_nodename (struct path *pp, char * node)
 		if (value && !strcmp(value, "usb")) {
 			pp->sg_id.proto_id = SCSI_PROTOCOL_UNSPEC;
 			tgtname = udev_device_get_sysname(tgtdev);
-			strncpy(node, tgtname, strlen(tgtname));
+			strlcpy(node, tgtname, NODE_NAME_SIZE);
 			condlog(3, "%s: skip USB device %s", pp->dev, node);
 			return 1;
 		}
@@ -361,7 +361,7 @@ sysfs_get_tgt_nodename (struct path *pp, char * node)
 			if (value) {
 				pp->sg_id.proto_id = SCSI_PROTOCOL_FCP;
 				pp->sg_id.transport_id = tgtid;
-				strncpy(node, value, NODE_NAME_SIZE);
+				strlcpy(node, value, NODE_NAME_SIZE);
 				udev_device_unref(tgtdev);
 				return 0;
 			} else
@@ -390,7 +390,7 @@ sysfs_get_tgt_nodename (struct path *pp, char * node)
 			if (value) {
 				pp->sg_id.proto_id = SCSI_PROTOCOL_ISCSI;
 				pp->sg_id.transport_id = tgtid;
-				strncpy(node, value, NODE_NAME_SIZE);
+				strlcpy(node, value, NODE_NAME_SIZE);
 				udev_device_unref(tgtdev);
 				return 0;
 			}
libmultipath/dmparser.c +9 -11
index a8b0b71a..b856a07f 100644
@@ -308,11 +308,11 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 				if (!pp)
 					goto out1;
 
-				strncpy(pp->dev_t, word, BLK_DEV_SIZE - 1);
-				strncpy(pp->dev, devname, FILE_NAME_SIZE - 1);
+				strlcpy(pp->dev_t, word, BLK_DEV_SIZE);
+				strlcpy(pp->dev, devname, FILE_NAME_SIZE);
 				if (strlen(mpp->wwid)) {
-					strncpy(pp->wwid, mpp->wwid,
-						WWID_SIZE - 1);
+					strlcpy(pp->wwid, mpp->wwid,
+						WWID_SIZE);
 				}
 				/* Only call this in multipath client mode */
 				if (!is_daemon && store_path(pathvec, pp))
@@ -320,8 +320,8 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 			} else {
 				if (!strlen(pp->wwid) &&
 				    strlen(mpp->wwid))
-					strncpy(pp->wwid, mpp->wwid,
-						WWID_SIZE - 1);
+					strlcpy(pp->wwid, mpp->wwid,
+						WWID_SIZE);
 			}
 			FREE(word);
 
@@ -333,23 +333,21 @@ int 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 - 1);
+				strlcpy(mpp->wwid, pp->wwid, WWID_SIZE);
 
 			/*
 			 * 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 - 1);
+				strlcpy(pp->wwid, mpp->wwid, WWID_SIZE);
 
 			/*
 			 * Do not allow in-use patch to change wwid
 			 */
 			else if (strcmp(pp->wwid, mpp->wwid) != 0) {
 				condlog(0, "%s: path wwid appears to have changed. Using map wwid.\n", pp->dev_t);
-				strncpy(pp->wwid, mpp->wwid, WWID_SIZE);
+				strlcpy(pp->wwid, mpp->wwid, WWID_SIZE);
 			}
 
 			pgp->id ^= (long)pp;
libmultipath/util.c +1 -1
index 5b838d51..8a4be787 100644
@@ -188,7 +188,7 @@ int devt2devname(char *devname, int devname_len, char *devt)
 					return 1;
 				}
 				p++;
-				strncpy(devname, p, devname_len);
+				strlcpy(devname, p, devname_len);
 				return 0;
 			}
 		}
libmultipath/uxsock.c +1 -1
index 7e5a1449..1ec4549b 100644
@@ -67,7 +67,7 @@ int ux_socket_listen(const char *name)
 	addr.sun_family = AF_LOCAL;
 	addr.sun_path[0] = '\0';
 	len = strlen(name) + 1 + sizeof(sa_family_t);
-	strncpy(&addr.sun_path[1], name, len);
+	strncpy(&addr.sun_path[1], name, sizeof(addr.sun_path) - 1);
 
 	if (bind(fd, (struct sockaddr *)&addr, len) == -1) {
 		condlog(3, "Couldn't bind to ux_socket, error %d", errno);
Signature · 1 line
2.21.0
#6 lore raw

Re: [PATCH 05/30] multipath-tools: fix more gcc 9 -Wstringop-truncation warnings

Quoted 1 line · Martin Wilck

More often than not, this means replacing strncpy() by strlcpy().

This depends on "libmultipath: add size argument to dm_get_uuid()" for the the extra argument in the call to dm_get_uuid() from get_refwwid(). Otherwise, it looks fine.

-Ben

Quoted 206 lines
---
 libmpathpersist/mpath_persist.c |  8 ++++----
 libmultipath/configure.c        |  7 ++++---
 libmultipath/discovery.c        | 12 ++++++------
 libmultipath/dmparser.c         | 20 +++++++++-----------
 libmultipath/util.c             |  2 +-
 libmultipath/uxsock.c           |  2 +-
 6 files changed, 25 insertions(+), 26 deletions(-)
libmpathpersist/mpath_persist.c +4 -4
index 599c5e9e..4abcaed5 100644
@@ -497,8 +497,8 @@ int mpath_prout_reg(struct multipath *mpp,int rq_servact, int rq_scope,
 				if (k < count)
 					continue;
 			}
-			strncpy(thread[count].param.dev, pp->dev,
-				FILE_NAME_SIZE - 1);
+			strlcpy(thread[count].param.dev, pp->dev,
+				FILE_NAME_SIZE);
 
 			if (count && (thread[count].param.paramp->sa_flags & MPATH_F_SPEC_I_PT_MASK)){
 				/*
@@ -686,8 +686,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 - 1);
+			strlcpy(thread[count].param.dev, pp->dev,
+				FILE_NAME_SIZE);
 			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 +4 -3
index af4d78de..dfee7d24 100644
@@ -73,7 +73,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 - 1);
+		strlcpy(agp->adapter_name, adapter_name1, SLOT_NAME_SIZE);
 		store_adaptergroup(adapters, agp);
 
 		/* create a new host port group
@@ -667,7 +667,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 - 1);
+		strlcpy(cmpp_by_name->wwid, mpp->wwid, WWID_SIZE);
 		drop_multipath(curmp, cmpp_by_name->wwid, KEEP_PATHS);
 		mpp->action = ACT_CREATE;
 		condlog(3, "%s: set ACT_CREATE (map wwid change)",
@@ -1451,7 +1451,8 @@ int get_refwwid(enum mpath_cmds cmd, char *dev, enum devtypes dev_type,
 
 		conf = get_multipath_config();
 		pthread_cleanup_push(put_multipath_config, conf);
-		if (((dm_get_uuid(dev, tmpwwid)) == 0) && (strlen(tmpwwid))) {
+		if (((dm_get_uuid(dev, tmpwwid, WWID_SIZE)) == 0)
+		    && (strlen(tmpwwid))) {
 			refwwid = tmpwwid;
 			goto check;
 		}
libmultipath/discovery.c +6 -6
index 51ca2306..407e64a0 100644
@@ -297,8 +297,8 @@ sysfs_get_timeout(const struct path *pp, unsigned int *timeout)
 	return 1;
 }
 
-int
-sysfs_get_tgt_nodename (struct path *pp, char * node)
+static int
+sysfs_get_tgt_nodename(struct path *pp, char *node)
 {
 	const char *tgtname, *value;
 	struct udev_device *parent, *tgtdev;
@@ -322,7 +322,7 @@ sysfs_get_tgt_nodename (struct path *pp, char * node)
 		if (tgtid >= 0) {
 			pp->sg_id.proto_id = SCSI_PROTOCOL_SAS;
 			pp->sg_id.transport_id = tgtid;
-			strncpy(node, value, NODE_NAME_SIZE);
+			strlcpy(node, value, NODE_NAME_SIZE);
 			return 0;
 		}
 	}
@@ -334,7 +334,7 @@ sysfs_get_tgt_nodename (struct path *pp, char * node)
 		if (value && !strcmp(value, "usb")) {
 			pp->sg_id.proto_id = SCSI_PROTOCOL_UNSPEC;
 			tgtname = udev_device_get_sysname(tgtdev);
-			strncpy(node, tgtname, strlen(tgtname));
+			strlcpy(node, tgtname, NODE_NAME_SIZE);
 			condlog(3, "%s: skip USB device %s", pp->dev, node);
 			return 1;
 		}
@@ -361,7 +361,7 @@ sysfs_get_tgt_nodename (struct path *pp, char * node)
 			if (value) {
 				pp->sg_id.proto_id = SCSI_PROTOCOL_FCP;
 				pp->sg_id.transport_id = tgtid;
-				strncpy(node, value, NODE_NAME_SIZE);
+				strlcpy(node, value, NODE_NAME_SIZE);
 				udev_device_unref(tgtdev);
 				return 0;
 			} else
@@ -390,7 +390,7 @@ sysfs_get_tgt_nodename (struct path *pp, char * node)
 			if (value) {
 				pp->sg_id.proto_id = SCSI_PROTOCOL_ISCSI;
 				pp->sg_id.transport_id = tgtid;
-				strncpy(node, value, NODE_NAME_SIZE);
+				strlcpy(node, value, NODE_NAME_SIZE);
 				udev_device_unref(tgtdev);
 				return 0;
 			}
libmultipath/dmparser.c +9 -11
index a8b0b71a..b856a07f 100644
@@ -308,11 +308,11 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 				if (!pp)
 					goto out1;
 
-				strncpy(pp->dev_t, word, BLK_DEV_SIZE - 1);
-				strncpy(pp->dev, devname, FILE_NAME_SIZE - 1);
+				strlcpy(pp->dev_t, word, BLK_DEV_SIZE);
+				strlcpy(pp->dev, devname, FILE_NAME_SIZE);
 				if (strlen(mpp->wwid)) {
-					strncpy(pp->wwid, mpp->wwid,
-						WWID_SIZE - 1);
+					strlcpy(pp->wwid, mpp->wwid,
+						WWID_SIZE);
 				}
 				/* Only call this in multipath client mode */
 				if (!is_daemon && store_path(pathvec, pp))
@@ -320,8 +320,8 @@ int disassemble_map(vector pathvec, char *params, struct multipath *mpp,
 			} else {
 				if (!strlen(pp->wwid) &&
 				    strlen(mpp->wwid))
-					strncpy(pp->wwid, mpp->wwid,
-						WWID_SIZE - 1);
+					strlcpy(pp->wwid, mpp->wwid,
+						WWID_SIZE);
 			}
 			FREE(word);
 
@@ -333,23 +333,21 @@ int 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 - 1);
+				strlcpy(mpp->wwid, pp->wwid, WWID_SIZE);
 
 			/*
 			 * 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 - 1);
+				strlcpy(pp->wwid, mpp->wwid, WWID_SIZE);
 
 			/*
 			 * Do not allow in-use patch to change wwid
 			 */
 			else if (strcmp(pp->wwid, mpp->wwid) != 0) {
 				condlog(0, "%s: path wwid appears to have changed. Using map wwid.\n", pp->dev_t);
-				strncpy(pp->wwid, mpp->wwid, WWID_SIZE);
+				strlcpy(pp->wwid, mpp->wwid, WWID_SIZE);
 			}
 
 			pgp->id ^= (long)pp;
libmultipath/util.c +1 -1
index 5b838d51..8a4be787 100644
@@ -188,7 +188,7 @@ int devt2devname(char *devname, int devname_len, char *devt)
 					return 1;
 				}
 				p++;
-				strncpy(devname, p, devname_len);
+				strlcpy(devname, p, devname_len);
 				return 0;
 			}
 		}
libmultipath/uxsock.c +1 -1
index 7e5a1449..1ec4549b 100644
@@ -67,7 +67,7 @@ int ux_socket_listen(const char *name)
 	addr.sun_family = AF_LOCAL;
 	addr.sun_path[0] = '\0';
 	len = strlen(name) + 1 + sizeof(sa_family_t);
-	strncpy(&addr.sun_path[1], name, len);
+	strncpy(&addr.sun_path[1], name, sizeof(addr.sun_path) - 1);
 
 	if (bind(fd, (struct sockaddr *)&addr, len) == -1) {
 		condlog(3, "Couldn't bind to ux_socket, error %d", errno);
Signature · 1 line
2.21.0
#7 lore raw

Re: [PATCH 05/30] multipath-tools: fix more gcc 9 -Wstringop-truncation warnings

Quoted 8 lines · Benjamin Marzinski
Quoted 1 line · Martin Wilck

More often than not, this means replacing strncpy() by strlcpy().

This depends on "libmultipath: add size argument to dm_get_uuid()" for
the the extra argument in the call to dm_get_uuid() from get_refwwid().
Otherwise, it looks fine.

Sorry, this mistake slipped in during my attempt to reorder the patch series into similar-topic groups. I'll fix it and your other issue with patc 07/30, and resubmit.

Martin

#8 lore raw

PATCH 6/30 multipath-tools: Fix more strncpy(X, Y, size - 1) calls

In reply to Martin Wilck

The compiler emitted no warnings for these, but for consistency it makes sense to fix them, too.

---
 libmpathpersist/mpath_persist.c | 2 +-
 libmultipath/configure.c        | 4 ++--
 libmultipath/prio.c             | 3 ++-
 libmultipath/structs_vec.c      | 3 ++-
 multipathd/waiter.c             | 3 ++-
 5 files changed, 9 insertions(+), 6 deletions(-)
libmpathpersist/mpath_persist.c +1 -1
index 4abcaed5..53022f5b 100644
@@ -618,7 +618,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 - 1);
+	strlcpy(param.dev, dev, FILE_NAME_SIZE);
 	/* Initialize and set thread joinable attribute */
 	pthread_attr_init(&attr);
 	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
libmultipath/configure.c +2 -2
index dfee7d24..c8dd69b0 100644
@@ -650,7 +650,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 - 1);
+			strlcpy(mpp->alias_old, cmpp->alias, WWID_SIZE);
 			mpp->action = ACT_RENAME;
 			if (force_reload) {
 				mpp->force_udev_reload = 1;
@@ -1257,7 +1257,7 @@ int coalesce_paths (struct vectors * vecs, vector newmp, char * refwwid,
 			if (!deadmap(mpp))
 				continue;
 
-			strncpy(alias, mpp->alias, WWID_SIZE - 1);
+			strlcpy(alias, mpp->alias, WWID_SIZE);
 
 			vector_del_slot(newmp, i);
 			i--;
libmultipath/prio.c +2 -1
index 0590218d..87de1f97 100644
@@ -5,6 +5,7 @@
 #include <sys/stat.h>
 
 #include "debug.h"
+#include "util.h"
 #include "prio.h"
 
 static LIST_HEAD(prioritizers);
@@ -166,7 +167,7 @@ void prio_get (char *multipath_dir, struct prio * dst, char * name, char * args)
 
 	strncpy(dst->name, src->name, PRIO_NAME_LEN);
 	if (args)
-		strncpy(dst->args, args, PRIO_ARGS_LEN - 1);
+		strlcpy(dst->args, args, PRIO_ARGS_LEN);
 	dst->getprio = src->getprio;
 	dst->handle = NULL;
 
libmultipath/structs_vec.c +2 -1
index db5d19da..c43b58fb 100644
@@ -2,6 +2,7 @@
 #include <string.h>
 #include <unistd.h>
 
+#include "util.h"
 #include "checkers.h"
 #include "vector.h"
 #include "defaults.h"
@@ -350,7 +351,7 @@ find_existing_alias (struct multipath * mpp,
 
 	vector_foreach_slot (vecs->mpvec, mp, i)
 		if (strncmp(mp->wwid, mpp->wwid, WWID_SIZE - 1) == 0) {
-			strncpy(mpp->alias_old, mp->alias, WWID_SIZE - 1);
+			strlcpy(mpp->alias_old, mp->alias, WWID_SIZE);
 			return;
 		}
 }
multipathd/waiter.c +2 -1
index 5895f157..eb8d6996 100644
@@ -11,6 +11,7 @@
 #include <signal.h>
 #include <urcu.h>
 
+#include "util.h"
 #include "vector.h"
 #include "memory.h"
 #include "checkers.h"
@@ -210,7 +211,7 @@ int start_waiter_thread (struct multipath *mpp, struct vectors *vecs)
 	if (!wp)
 		goto out;
 
-	strncpy(wp->mapname, mpp->alias, WWID_SIZE - 1);
+	strlcpy(wp->mapname, mpp->alias, WWID_SIZE);
 	wp->vecs = vecs;
 
 	if (pthread_create(&wp->thread, &waiter_attr, waitevent, wp)) {
Signature · 1 line
2.21.0
#9 lore raw

PATCH 7/30 libmpathcmd: use target length in strncpy() call

In reply to Martin Wilck
---
 libmpathcmd/mpath_cmd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libmpathcmd/mpath_cmd.c +1 -1
index b681311b..05b69316 100644
@@ -104,7 +104,7 @@ int __mpath_connect(int nonblocking)
 	addr.sun_family = AF_LOCAL;
 	addr.sun_path[0] = '\0';
 	len = strlen(DEFAULT_SOCKET) + 1 + sizeof(sa_family_t);
-	strncpy(&addr.sun_path[1], DEFAULT_SOCKET, len);
+	strncpy(&addr.sun_path[1], DEFAULT_SOCKET, sizeof(addr.sun_path) - 1);
 
 	fd = socket(AF_LOCAL, SOCK_STREAM, 0);
 	if (fd == -1)
Signature · 1 line
2.21.0
#10 lore raw

Re: [PATCH 07/30] libmpathcmd: use target length in strncpy() call

On Fri, Jun 07, 2019 at 03:05:29PM +0200, Martin Wilck wrote:

Not a big deal since default socket is a defined string, but since we are writing to &addr.sun_path[1], if DEFAULT_SOCKET were of size >= "sizeof(addr.sun_path) - 1", the strncpy() would fill all of addr.sun_path, without leaving any space for the the null byte at the end. I assume you meant to change this to be strlcpy() instead of leaving it as strncpy().

-Ben

Quoted 20 lines
---
 libmpathcmd/mpath_cmd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libmpathcmd/mpath_cmd.c +1 -1
index b681311b..05b69316 100644
@@ -104,7 +104,7 @@ int __mpath_connect(int nonblocking)
 	addr.sun_family = AF_LOCAL;
 	addr.sun_path[0] = '\0';
 	len = strlen(DEFAULT_SOCKET) + 1 + sizeof(sa_family_t);
-	strncpy(&addr.sun_path[1], DEFAULT_SOCKET, len);
+	strncpy(&addr.sun_path[1], DEFAULT_SOCKET, sizeof(addr.sun_path) - 1);
 
 	fd = socket(AF_LOCAL, SOCK_STREAM, 0);
 	if (fd == -1)
Signature · 1 line
2.21.0
#11 lore raw

Re: [PATCH 07/30] libmpathcmd: use target length in strncpy() call

Quoted 8 lines · Benjamin Marzinski

On Fri, Jun 07, 2019 at 03:05:29PM +0200, Martin Wilck wrote:

Not a big deal since default socket is a defined string, but since we are writing to &addr.sun_path[1], if DEFAULT_SOCKET were of size >= "sizeof(addr.sun_path) - 1", the strncpy() would fill all of addr.sun_path, without leaving any space for the the null byte at the end. I assume you meant to change this to be strlcpy() instead of leaving it as strncpy().

I double-checked the man pages unix(7), and I found that abstract unix socket names do not have to be zero-terminated ("null bytes in the name have no special significance"). So strncpy() is actually correct in this case. But the length argument to bind() should be checked.

Thanks,
Martin

#12 lore raw

PATCH 8/30 libmultipath: inline set_default()

In reply to Martin Wilck

This is nothing but a reimplementation of strdup(), and it causes gcc 9 warnings. Remove it.

---
 libmultipath/debug.c    |  1 +
 libmultipath/defaults.c | 17 -----------------
 libmultipath/defaults.h |  9 ++++++++-
 3 files changed, 9 insertions(+), 18 deletions(-)
libmultipath/debug.c +1 -0
index cbf1e570..4128cb90 100644
@@ -4,6 +4,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
+#include <string.h>
 
 #include "log_pthread.h"
 #include <sys/types.h>
libmultipath/defaults.c +0 -17
index c20bb0d2..082640d3 100644
@@ -7,20 +7,3 @@
 #include "memory.h"
 
 const char * const default_partition_delim = DEFAULT_PARTITION_DELIM;
-
-char *
-set_default (char * str)
-{
-	int len;
-	char * p;
-
-	len = strlen(str);
-	p = MALLOC(len + 1);
-
-	if (!p)
-		return NULL;
-
-	strncat(p, str, len);
-
-	return p;
-}
libmultipath/defaults.h +8 -1
index 65769398..83f89f37 100644
@@ -1,3 +1,5 @@
+#ifndef _DEFAULTS_H
+#define _DEFAULTS_H
 /*
  * If you add or modify a value also update multipath/multipath.conf.5
  * and the TEMPLATE in libmultipath/hwtable.c
@@ -60,5 +62,10 @@
 #define DEFAULT_CONFIG_DIR	"/etc/multipath/conf.d"
 #define MULTIPATH_SHM_BASE	"/dev/shm/multipath/"
 
-char * set_default (char * str);
+
+static inline char *set_default(char *str)
+{
+	return strdup(str);
+}
 extern const char *const default_partition_delim;
+#endif /* _DEFAULTS_H */
Signature · 1 line
2.21.0
#13 lore raw

PATCH 9/30 libmultipath: add size argument to dm_get_uuid()

In reply to Martin Wilck

The length of the uuid field in libdm is DM_UUID_LEN, which happens to be one byte more than our WWID_SIZE. Handle this cleanly.

---
 libmultipath/devmapper.c | 28 +++++++++++++++++-----------
 libmultipath/devmapper.h |  2 +-
 libmultipath/wwids.c     |  3 ++-
 multipathd/main.c        |  2 +-
 4 files changed, 21 insertions(+), 14 deletions(-)
libmultipath/devmapper.c +17 -11
index 2e79667d..0f0c3a34 100644
@@ -13,7 +13,9 @@
 #include <unistd.h>
 #include <errno.h>
 #include <sys/sysmacros.h>
+#include <linux/dm-ioctl.h>
 
+#include "util.h"
 #include "checkers.h"
 #include "vector.h"
 #include "structs.h"
@@ -554,7 +556,7 @@ out:
 }
 
 static int
-dm_get_prefixed_uuid(const char *name, char *uuid)
+dm_get_prefixed_uuid(const char *name, char *uuid, int uuid_len)
 {
 	struct dm_task *dmt;
 	const char *uuidtmp;
@@ -572,7 +574,7 @@ dm_get_prefixed_uuid(const char *name, char *uuid)
 
 	uuidtmp = dm_task_get_uuid(dmt);
 	if (uuidtmp)
-		strcpy(uuid, uuidtmp);
+		strlcpy(uuid, uuidtmp, uuid_len);
 	else
 		uuid[0] = '\0';
 
@@ -582,14 +584,18 @@ uuidout:
 	return r;
 }
 
-int dm_get_uuid(const char *name, char *uuid)
+int dm_get_uuid(const char *name, char *uuid, int uuid_len)
 {
-	if (dm_get_prefixed_uuid(name, uuid))
+	char tmp[DM_UUID_LEN];
+
+	if (dm_get_prefixed_uuid(name, tmp, sizeof(tmp)))
 		return 1;
 
-	if (!strncmp(uuid, UUID_PREFIX, UUID_PREFIX_LEN))
-		memmove(uuid, uuid + UUID_PREFIX_LEN,
-			strlen(uuid + UUID_PREFIX_LEN) + 1);
+	if (!strncmp(tmp, UUID_PREFIX, UUID_PREFIX_LEN))
+		strlcpy(uuid, tmp + UUID_PREFIX_LEN, uuid_len);
+	else
+		uuid[0] = '\0';
+
 	return 0;
 }
 
@@ -597,12 +603,12 @@ static int
 is_mpath_part(const char *part_name, const char *map_name)
 {
 	char *p;
-	char part_uuid[WWID_SIZE], map_uuid[WWID_SIZE];
+	char part_uuid[DM_UUID_LEN], map_uuid[DM_UUID_LEN];
 
-	if (dm_get_prefixed_uuid(part_name, part_uuid))
+	if (dm_get_prefixed_uuid(part_name, part_uuid, sizeof(part_uuid)))
 		return 0;
 
-	if (dm_get_prefixed_uuid(map_name, map_uuid))
+	if (dm_get_prefixed_uuid(map_name, map_uuid, sizeof(map_uuid)))
 		return 0;
 
 	if (strncmp(part_uuid, "part", 4) != 0)
@@ -1066,7 +1072,7 @@ struct multipath *dm_get_multipath(const char *name)
 	if (dm_get_map(name, &mpp->size, NULL))
 		goto out;
 
-	dm_get_uuid(name, mpp->wwid);
+	dm_get_uuid(name, mpp->wwid, WWID_SIZE);
 	dm_get_info(name, &mpp->dmi);
 
 	return mpp;
libmultipath/devmapper.h +1 -1
index db75526c..7557a86b 100644
@@ -63,7 +63,7 @@ int dm_get_major_minor (const char *name, int *major, int *minor);
 char * dm_mapname(int major, int minor);
 int dm_remove_partmaps (const char * mapname, int need_sync,
 			int deferred_remove);
-int dm_get_uuid(const char *name, char *uuid);
+int dm_get_uuid(const char *name, char *uuid, int uuid_len);
 int dm_get_info (const char * mapname, struct dm_info ** dmi);
 int dm_rename (const char * old, char * new, char * delim, int skip_kpartx);
 int dm_reassign(const char * mapname);
libmultipath/wwids.c +2 -1
index 53e79511..ef748125 100644
@@ -294,7 +294,8 @@ should_multipath(struct path *pp1, vector pathvec, vector mpvec)
 		char tmp_wwid[WWID_SIZE];
 		struct multipath *mp = find_mp_by_wwid(mpvec, pp1->wwid);
 
-		if (mp != NULL && dm_get_uuid(mp->alias, tmp_wwid) == 0 &&
+		if (mp != NULL &&
+		    dm_get_uuid(mp->alias, tmp_wwid, WWID_SIZE) == 0 &&
 		    !strncmp(tmp_wwid, pp1->wwid, WWID_SIZE)) {
 			condlog(3, "wwid %s is already multipathed, keeping it",
 				pp1->wwid);
multipathd/main.c +1 -1
index eef84a39..2e4973d7 100644
@@ -406,7 +406,7 @@ set_multipath_wwid (struct multipath * mpp)
 	if (strlen(mpp->wwid))
 		return;
 
-	dm_get_uuid(mpp->alias, mpp->wwid);
+	dm_get_uuid(mpp->alias, mpp->wwid, WWID_SIZE);
 }
 
 static void set_no_path_retry(struct multipath *mpp)
Signature · 1 line
2.21.0
#14 lore raw

PATCH 10/30 multipath-tools tests: omit timestamp in log messages

In reply to Martin Wilck
---
 tests/globals.c | 2 +-
 tests/hwtable.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
tests/globals.c +1 -1
index 07d970ea..aeb7723f 100644
@@ -3,7 +3,7 @@
 
 /* Required globals */
 struct udev *udev;
-int logsink = 0;
+int logsink = -1;
 struct config conf = {
 	.uid_attrs = "sd:ID_BOGUS",
 	.verbosity = 4,
tests/hwtable.c +1 -1
index ad863b08..13da1d99 100644
@@ -54,7 +54,7 @@ struct hwt_state {
 
 static struct config *_conf;
 struct udev *udev;
-int logsink;
+int logsink = -1;
 
 struct config *get_multipath_config(void)
 {
Signature · 1 line
2.21.0
#15 lore raw

PATCH 11/30 tests/hwtable: decrease log verbosity

In reply to Martin Wilck
---
 tests/hwtable.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/hwtable.c +1 -1
index 13da1d99..f436f52d 100644
@@ -457,7 +457,7 @@ static void replicate_config(const struct hwt_state *hwt, bool local)
 	vector hwtable;
 	struct config *conf;
 
-	condlog(1, "--- %s: replicating %s configuration", __func__,
+	condlog(3, "--- %s: replicating %s configuration", __func__,
 		local ? "local" : "full");
 
 	conf = get_multipath_config();
Signature · 1 line
2.21.0
#16 lore raw

PATCH 12/30 multipath-tools tests: add strlcpy() tests

In reply to Martin Wilck

As we're using strlcpy quite a bit, make sure it works as designed.

---
 tests/util.c | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 142 insertions(+)
tests/util.c +142 -0
index e6d4b9ab..4e04a480 100644
@@ -258,10 +258,152 @@ int test_basenamecpy(void)
 	return cmocka_run_group_tests(tests, NULL, NULL);
 }
 
+static const char src_str[] = "Hello";
+
+/* strlcpy with length 0 */
+static void test_strlcpy_0(void **state)
+{
+	char tst[] = "word";
+	int rc;
+
+	rc = strlcpy(tst, src_str, 0);
+	assert_int_equal(rc, strlen(src_str));
+	assert_string_equal(tst, "word");
+}
+
+/* strlcpy with length 1 */
+static void test_strlcpy_1(void **state)
+{
+	char tst[] = "word";
+	int rc;
+
+	rc = strlcpy(tst, src_str, 1);
+	assert_int_equal(rc, strlen(src_str));
+	assert_int_equal(tst[0], '\0');
+	assert_string_equal(tst + 1, "ord");
+}
+
+/* strlcpy with length 2 */
+static void test_strlcpy_2(void **state)
+{
+	char tst[] = "word";
+	int rc;
+
+	rc = strlcpy(tst, src_str, 2);
+	assert_int_equal(rc, strlen(src_str));
+	assert_int_equal(tst[0], src_str[0]);
+	assert_int_equal(tst[1], '\0');
+	assert_string_equal(tst + 2, "rd");
+}
+
+/* strlcpy with dst length < src length */
+static void test_strlcpy_3(void **state)
+{
+	char tst[] = "word";
+	int rc;
+
+	rc = strlcpy(tst, src_str, sizeof(tst));
+	assert_int_equal(rc, strlen(src_str));
+	assert_int_equal(sizeof(tst) - 1, strlen(tst));
+	assert_true(strncmp(tst, src_str, sizeof(tst) - 1) == 0);
+}
+
+/* strlcpy with dst length > src length */
+static void test_strlcpy_4(void **state)
+{
+	static const char old[] = "0123456789";
+	char *tst;
+	int rc;
+
+	tst = strdup(old);
+	rc = strlcpy(tst, src_str, sizeof(old));
+	assert_int_equal(rc, strlen(src_str));
+	assert_string_equal(src_str, tst);
+	assert_string_equal(tst + sizeof(src_str), old + sizeof(src_str));
+	free(tst);
+}
+
+/* strlcpy with dst length = src length, dst not terminated */
+static void test_strlcpy_5(void **state)
+{
+	char *tst;
+	int rc;
+
+	tst = malloc(sizeof(src_str));
+	memset(tst, 'f', sizeof(src_str));
+
+	rc = strlcpy(tst, src_str, sizeof(src_str));
+	assert_int_equal(rc, strlen(src_str));
+	assert_string_equal(src_str, tst);
+
+	free(tst);
+}
+
+/* strlcpy with dst length > src length, dst not terminated */
+static void test_strlcpy_6(void **state)
+{
+	char *tst;
+	int rc;
+
+	tst = malloc(sizeof(src_str) + 2);
+	memset(tst, 'f', sizeof(src_str) + 2);
+
+	rc = strlcpy(tst, src_str, sizeof(src_str) + 2);
+	assert_int_equal(rc, strlen(src_str));
+	assert_string_equal(src_str, tst);
+	assert_int_equal(tst[sizeof(src_str)], 'f');
+	assert_int_equal(tst[sizeof(src_str) + 1], 'f');
+
+	free(tst);
+}
+
+/* strlcpy with empty src */
+static void test_strlcpy_7(void **state)
+{
+	char tst[] = "word";
+	static const char empty[] = "";
+	int rc;
+
+	rc = strlcpy(tst, empty, sizeof(tst));
+	assert_int_equal(rc, strlen(empty));
+	assert_string_equal(empty, tst);
+	assert_string_equal(tst + 1, "ord");
+}
+
+/* strlcpy with empty src, length 0 */
+static void test_strlcpy_8(void **state)
+{
+	char tst[] = "word";
+	static const char empty[] = "";
+	int rc;
+
+	rc = strlcpy(tst, empty, 0);
+	assert_int_equal(rc, strlen(empty));
+	assert_string_equal("word", tst);
+}
+
+static int test_strlcpy(void)
+{
+	const struct CMUnitTest tests[] = {
+		cmocka_unit_test(test_strlcpy_0),
+		cmocka_unit_test(test_strlcpy_1),
+		cmocka_unit_test(test_strlcpy_2),
+		cmocka_unit_test(test_strlcpy_3),
+		cmocka_unit_test(test_strlcpy_4),
+		cmocka_unit_test(test_strlcpy_5),
+		cmocka_unit_test(test_strlcpy_6),
+		cmocka_unit_test(test_strlcpy_7),
+		cmocka_unit_test(test_strlcpy_8),
+	};
+
+	return cmocka_run_group_tests(tests, NULL, NULL);
+}
+
 int main(void)
 {
 	int ret = 0;
 
 	ret += test_basenamecpy();
+	ret += test_strlcpy();
 	return ret;
 }
Signature · 1 line
2.21.0
#17 lore raw

PATCH 13/30 multipath-tools tests: add test for VPD parsing

In reply to Martin Wilck

Add a test for parsing the WWID from VPDs, with focus on WWID buffer overflow detection and handling.

---
 tests/Makefile |   4 +-
 tests/vpd.c    | 790 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 793 insertions(+), 1 deletion(-)
 create mode 100644 tests/vpd.c
tests/Makefile +3 -1
index ef900866..bf159b2d 100644
@@ -3,7 +3,7 @@ include ../Makefile.inc
 CFLAGS += $(BIN_CFLAGS) -I$(multipathdir) -I$(mpathcmddir)
 LIBDEPS += -L$(multipathdir) -lmultipath -lcmocka
 
-TESTS := uevent parser util dmevents hwtable blacklist unaligned
+TESTS := uevent parser util dmevents hwtable blacklist unaligned vpd
 
 .SILENT: $(TESTS:%=%.o)
 .PRECIOUS: $(TESTS:%=%-test)
@@ -25,6 +25,8 @@ hwtable-test_OBJDEPS := ../libmultipath/discovery.o ../libmultipath/blacklist.o
 hwtable-test_LIBDEPS := -ludev -lpthread -ldl
 blacklist-test_OBJDEPS := ../libmultipath/blacklist.o
 blacklist-test_LIBDEPS := -ludev
+vpd-test_OBJDEPS :=  ../libmultipath/discovery.o
+vpd-test_LIBDEPS := -ludev -lpthread -ldl
 
 lib/libchecktur.so:
 	mkdir lib
tests/vpd.c new file +790 -0

790 lines not on this page · raw message

Signature · 1 line
2.21.0
#18 lore raw

PATCH 14/30 libmultipath: fix parsing of VPD 83 type 1 (T10 vendor ID)

In reply to Martin Wilck

In the buffer overflow case, the code would set p_len = out_len - len - 2, then len = len + plen = out_len - 2, and check if len >= out_len - 1, which is never the case. Rather, set p_len = out_len - len -1, and check the length again before appending the underscore.

Fixes 18176202e75c "Read wwid from sysfs vpg_pg83 attribute"
---
 libmultipath/discovery.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
libmultipath/discovery.c +9 -2
index 407e64a0..f360e306 100644
@@ -1065,8 +1065,11 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 			p = vpd;
 			while ((p = memchr(vpd, ' ', vpd_len))) {
 				p_len = p - vpd;
-				if (len + p_len > out_len - 1)
-					p_len = out_len - len - 2;
+				if (len + p_len > out_len - 1) {
+					condlog(1, "%s: WWID overflow, type 1, %d/%lu bytes required",
+						__func__, len + p_len, out_len);
+					p_len = out_len - len - 1;
+				}
 				memcpy(out + len, vpd, p_len);
 				len += p_len;
 				if (len >= out_len - 1) {
@@ -1075,6 +1078,10 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 				}
 				out[len] = '_';
 				len ++;
+				if (len >= out_len - 1) {
+					out[len] = '\0';
+					break;
+				}
 				vpd = p;
 				vpd_len -= p_len;
 				while (vpd && *vpd == ' ') {
Signature · 1 line
2.21.0
#19 lore raw

PATCH 15/30 libmultipath: Fix buffer overflow in parse_vpd_pg80()

In reply to Martin Wilck

We set out[len] = '\0' later, thus we should set len to no more then out_len - 1.

Fixes 756ef73b7197 "Separate out vpd parsing functions"
---
 libmultipath/discovery.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libmultipath/discovery.c +1 -1
index f360e306..89c4d2ad 100644
@@ -913,7 +913,7 @@ parse_vpd_pg80(const unsigned char *in, char *out, size_t out_len)
 	if (len >= out_len) {
 		condlog(2, "vpd pg80 overflow, %d/%d bytes required",
 			len, (int)out_len);
-		len = out_len;
+		len = out_len - 1;
 	}
 	if (len > 0) {
 		memcpy(out, in + 4, len);
Signature · 1 line
2.21.0
#20 lore raw

PATCH 16/30 libmultipath: fix possible WWID overflow in parse_vpd_pg83()

In reply to Martin Wilck

We have to check the remaining length before printing to the output buffer, not afterwards.

Fixes 18176202e75c "Read wwid from sysfs vpg_pg83 attribute"
---
 libmultipath/discovery.c | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)
libmultipath/discovery.c +3 -13
index 89c4d2ad..eed35284 100644
@@ -1012,11 +1012,9 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 			int i;
 
 			len = sprintf(out, "%d", vpd_type);
-			for (i = 0; i < vpd_len; i++) {
+			for (i = 0; i < vpd_len && len < out_len - 2; i++) {
 				len += sprintf(out + len,
 					       "%02x", vpd[i]);
-				if (len >= out_len)
-					break;
 			}
 		} else if (vpd_type == 0x8) {
 			if (!memcmp("eui.", vpd, 4)) {
@@ -1024,27 +1022,19 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 				len = 1;
 				vpd += 4;
 				vpd_len -= 4;
-				for (i = 0; i < vpd_len; i++) {
+				for (i = 0; i < vpd_len && len < out_len - 1; i++) {
 					len += sprintf(out + len, "%c",
 						       tolower(vpd[i]));
-					if (len >= out_len)
-						break;
 				}
-				len = vpd_len + 1;
-				out[len] = '\0';
 			} else if (!memcmp("naa.", vpd, 4)) {
 				out[0] = '3';
 				len = 1;
 				vpd += 4;
 				vpd_len -= 4;
-				for (i = 0; i < vpd_len; i++) {
+				for (i = 0; i < vpd_len && len < out_len - 1; i++) {
 					len += sprintf(out + len, "%c",
 						       tolower(vpd[i]));
-					if (len >= out_len)
-						break;
 				}
-				len = vpd_len + 1;
-				out[len] = '\0';
 			} else {
 				out[0] = '8';
 				len = 1;
Signature · 1 line
2.21.0
#21 lore raw

PATCH 17/30 libmultipath: fix another WWID overflow in parse_vpd_pg83()

In reply to Martin Wilck

This one is an obvious typo.

Fixes 18176202e75c "Read wwid from sysfs vpg_pg83 attribute"
---
 libmultipath/discovery.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libmultipath/discovery.c +1 -1
index eed35284..10306b22 100644
@@ -1040,7 +1040,7 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 				len = 1;
 				vpd += 4;
 				vpd_len -= 4;
-				if (vpd_len > out_len + 2)
+				if (vpd_len > out_len - 2)
 					vpd_len = out_len - 2;
 				memcpy(out, vpd, vpd_len);
 				len = vpd_len + 1;
Signature · 1 line
2.21.0
#22 lore raw

PATCH 18/30 libmultipath: fix parsing of SCSI name string, iqn format

In reply to Martin Wilck

Do not overwrite the leading '8'.

---
 libmultipath/discovery.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
libmultipath/discovery.c +1 -2
index 10306b22..45c2931e 100644
@@ -1037,12 +1037,11 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 				}
 			} else {
 				out[0] = '8';
-				len = 1;
 				vpd += 4;
 				vpd_len -= 4;
 				if (vpd_len > out_len - 2)
 					vpd_len = out_len - 2;
-				memcpy(out, vpd, vpd_len);
+				memcpy(out + 1, vpd, vpd_len);
 				len = vpd_len + 1;
 				out[len] = '\0';
 			}
Signature · 1 line
2.21.0
#23 lore raw

PATCH 19/30 libmultipath: add consistent WWID overflow logging in parse_vpd_pg83

In reply to Martin Wilck
---
 libmultipath/discovery.c | 49 ++++++++++++++++++++++++++--------------
 1 file changed, 32 insertions(+), 17 deletions(-)
libmultipath/discovery.c +32 -17
index 45c2931e..e484ec8d 100644
@@ -1011,38 +1011,53 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 		if (vpd_type == 0x2 || vpd_type == 0x3) {
 			int i;
 
+			assert(out_len >= 2);
 			len = sprintf(out, "%d", vpd_type);
-			for (i = 0; i < vpd_len && len < out_len - 2; i++) {
+			if (2 * vpd_len >= out_len - len) {
+				condlog(1, "%s: WWID overflow, type %d, %d/%lu bytes required",
+					__func__, vpd_type,
+					2 * vpd_len + len + 1, out_len);
+				vpd_len = (out_len - len - 1) / 2;
+			}
+			for (i = 0; i < vpd_len; i++)
 				len += sprintf(out + len,
 					       "%02x", vpd[i]);
-			}
 		} else if (vpd_type == 0x8) {
 			if (!memcmp("eui.", vpd, 4)) {
 				out[0] =  '2';
-				len = 1;
 				vpd += 4;
-				vpd_len -= 4;
-				for (i = 0; i < vpd_len && len < out_len - 1; i++) {
-					len += sprintf(out + len, "%c",
-						       tolower(vpd[i]));
+				len = vpd_len - 4;
+				if (len > out_len - 1) {
+					condlog(1, "%s: WWID overflow, type 8/%c, %d/%lu bytes required",
+						__func__, out[0], len + 1, out_len);
+					len = out_len - 1;
 				}
+				for (i = 0; i < len; ++i)
+					out[1 + i] = tolower(vpd[i]);
+				/* designator should be 0-terminated, but let's make sure */
+				out[len] = '\0';
 			} else if (!memcmp("naa.", vpd, 4)) {
 				out[0] = '3';
-				len = 1;
 				vpd += 4;
-				vpd_len -= 4;
-				for (i = 0; i < vpd_len && len < out_len - 1; i++) {
-					len += sprintf(out + len, "%c",
-						       tolower(vpd[i]));
+				len = vpd_len - 4;
+				if (len > out_len - 1) {
+					condlog(1, "%s: WWID overflow, type 8/%c, %d/%lu bytes required",
+						__func__, out[0], len + 1, out_len);
+					len = out_len - 1;
 				}
+				for (i = 0; i < len; ++i)
+					out[1 + i] = tolower(vpd[i]);
+				out[len] = '\0';
 			} else {
 				out[0] = '8';
 				vpd += 4;
-				vpd_len -= 4;
-				if (vpd_len > out_len - 2)
-					vpd_len = out_len - 2;
-				memcpy(out + 1, vpd, vpd_len);
-				len = vpd_len + 1;
+				len = vpd_len - 4;
+				if (len > out_len - 1) {
+					condlog(1, "%s: WWID overflow, type 8/%c, %d/%lu bytes required",
+						__func__, out[0], len + 1, out_len);
+					len = out_len - 1;
+				}
+				memcpy(out + 1, vpd, len);
 				out[len] = '\0';
 			}
 		} else if (vpd_type == 0x1) {
Signature · 1 line
2.21.0
#24 lore raw

PATCH 20/30 libmultipath: parse_vpd_pg83: common code for SCSI string parsing

In reply to Martin Wilck

The three cases for eui, naa, and iqn parsing share almost all code.

---
 libmultipath/discovery.c | 54 ++++++++++++++++------------------------
 1 file changed, 21 insertions(+), 33 deletions(-)
libmultipath/discovery.c +21 -33
index e484ec8d..32e3f9d3 100644
@@ -1023,43 +1023,31 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 				len += sprintf(out + len,
 					       "%02x", vpd[i]);
 		} else if (vpd_type == 0x8) {
-			if (!memcmp("eui.", vpd, 4)) {
+			if (!memcmp("eui.", vpd, 4))
 				out[0] =  '2';
-				vpd += 4;
-				len = vpd_len - 4;
-				if (len > out_len - 1) {
-					condlog(1, "%s: WWID overflow, type 8/%c, %d/%lu bytes required",
-						__func__, out[0], len + 1, out_len);
-					len = out_len - 1;
-				}
-				for (i = 0; i < len; ++i)
-					out[1 + i] = tolower(vpd[i]);
-				/* designator should be 0-terminated, but let's make sure */
-				out[len] = '\0';
-			} else if (!memcmp("naa.", vpd, 4)) {
+			else if (!memcmp("naa.", vpd, 4))
 				out[0] = '3';
-				vpd += 4;
-				len = vpd_len - 4;
-				if (len > out_len - 1) {
-					condlog(1, "%s: WWID overflow, type 8/%c, %d/%lu bytes required",
-						__func__, out[0], len + 1, out_len);
-					len = out_len - 1;
-				}
-				for (i = 0; i < len; ++i)
-					out[1 + i] = tolower(vpd[i]);
-				out[len] = '\0';
-			} else {
+			else
 				out[0] = '8';
-				vpd += 4;
-				len = vpd_len - 4;
-				if (len > out_len - 1) {
-					condlog(1, "%s: WWID overflow, type 8/%c, %d/%lu bytes required",
-						__func__, out[0], len + 1, out_len);
-					len = out_len - 1;
-				}
-				memcpy(out + 1, vpd, len);
-				out[len] = '\0';
+
+			vpd += 4;
+			len = vpd_len - 4;
+			if (len > out_len - 1) {
+				condlog(1, "%s: WWID overflow, type 8/%c, %d/%lu bytes required",
+					__func__, out[0], len + 1, out_len);
+				len = out_len - 1;
 			}
+
+			if (out[0] == '8')
+				for (i = 0; i < len; ++i)
+					out[1 + i] = vpd[i];
+			else
+				for (i = 0; i < len; ++i)
+					out[1 + i] = tolower(vpd[i]);
+
+			/* designator should be 0-terminated, but let's make sure */
+			out[len] = '\0';
+
 		} else if (vpd_type == 0x1) {
 			const unsigned char *p;
 			int p_len;
Signature · 1 line
2.21.0
#25 lore raw

PATCH 21/30 libmultipath: allow zero-padded SCSI names in parse_vpd_pg83()

In reply to Martin Wilck

The spec says that SCSI name strings designator length must be a multiple of 4, and that strings must be zero-terminated and zero-padded.
Fix the returned string length if the VPD input ends with multiple 0-bytes.

---
 libmultipath/discovery.c | 2 ++
 1 file changed, 2 insertions(+)
libmultipath/discovery.c +2 -0
index 32e3f9d3..3558e559 100644
@@ -1032,6 +1032,8 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 
 			vpd += 4;
 			len = vpd_len - 4;
+			while (len > 2 && vpd[len - 2] == '\0')
+				--len;
 			if (len > out_len - 1) {
 				condlog(1, "%s: WWID overflow, type 8/%c, %d/%lu bytes required",
 					__func__, out[0], len + 1, out_len);
Signature · 1 line
2.21.0
#26 lore raw

PATCH 22/30 libmultipath: fix parsing of non-space-terminated T10 ID

In reply to Martin Wilck

If the T10 vendor specific ID doesn't end with spaces, the last part won't be parsed. Fix it.

Fixes 18176202e75c "Read wwid from sysfs vpg_pg83 attribute"
---
 libmultipath/discovery.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
libmultipath/discovery.c +11 -0
index 3558e559..efcea468 100644
@@ -1083,6 +1083,17 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 					vpd_len --;
 				}
 			}
+			p_len = vpd_len;
+			if (p_len > 0 && len < out_len - 1) {
+				if (len + p_len > out_len - 1) {
+					condlog(1, "%s: WWID overflow, type 1, %d/%lu bytes required",
+						__func__, len + p_len + 1, out_len);
+					p_len = out_len - len - 1;
+				}
+				memcpy(out + len, vpd, p_len);
+				len += p_len;
+				out[len] = '\0';
+			}
 			if (len > 1 && out[len - 1] == '_') {
 				out[len - 1] = '\0';
 				len--;
Signature · 1 line
2.21.0
#27 lore raw

PATCH 23/30 libmultipath: parse_vpd_pg80: fix overflow output

In reply to Martin Wilck

"vpd pg80 overflow, 20/20 bytes required" looks weird. Make it clear that actually 21 bytes are required in this case.

---
 libmultipath/discovery.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libmultipath/discovery.c +1 -1
index efcea468..931a676f 100644
@@ -912,7 +912,7 @@ parse_vpd_pg80(const unsigned char *in, char *out, size_t out_len)
 
 	if (len >= out_len) {
 		condlog(2, "vpd pg80 overflow, %d/%d bytes required",
-			len, (int)out_len);
+			len + 1, (int)out_len);
 		len = out_len - 1;
 	}
 	if (len > 0) {
Signature · 1 line
2.21.0
#28 lore raw

PATCH 24/30 libmultipath: parse_vpd_pg80: fix whitespace handling

In reply to Martin Wilck

According to the SPC spec, the unit serial number is right-aligned ASCII. Thus leading whitespace must be skipped. Moreover, trailing whitespace should be discarded before doing the length check.

Fixes 756ef73b7197 "Separate out vpd parsing functions"
---
 libmultipath/discovery.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
libmultipath/discovery.c +10 -10
index 931a676f..b10afdef 100644
@@ -907,9 +907,18 @@ get_geometry(struct path *pp)
 static int
 parse_vpd_pg80(const unsigned char *in, char *out, size_t out_len)
 {
-	char *p = NULL;
 	int len = get_unaligned_be16(&in[2]);
 
+	/*
+	 * Strip leading and trailing whitespace
+	 */
+	while (len > 0 && in[len + 3] == ' ')
+		--len;
+	while (len > 0 && in[4] == ' ') {
+		++in;
+		--len;
+	}
+
 	if (len >= out_len) {
 		condlog(2, "vpd pg80 overflow, %d/%d bytes required",
 			len + 1, (int)out_len);
@@ -919,15 +928,6 @@ parse_vpd_pg80(const unsigned char *in, char *out, size_t out_len)
 		memcpy(out, in + 4, len);
 		out[len] = '\0';
 	}
-	/*
-	 * Strip trailing whitspaces
-	 */
-	p = out + len - 1;
-	while (p > out && *p == ' ') {
-		*p = '\0';
-		p--;
-		len --;
-	}
 	return len;
 }
 
Signature · 1 line
2.21.0
#29 lore raw

PATCH 25/30 libmultipath: get_uid: straighten the fallback logic

In reply to Martin Wilck

The code had two distinct fallback paths, with non-obvious logic. Try to straighten it out. The "allow_fallback" logic from ffaf9477d115 "multipathd: Don't use fallback code after getting wwid" should only apply in the case where get_udev_uid() failed, not if it wasn't even tried. Moreover, uid_attribute is hardly ever NULL since ffaf9477; allow the user to set it to the empty string to skip udev-based WWID determination. This is rather not meant for production, but it's useful for testing. Finally, only set "origin" to "udev" if a WWID was actually retrieved from udev.

---
 libmultipath/discovery.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
libmultipath/discovery.c +7 -7
index b10afdef..d7eaee68 100644
@@ -1876,20 +1876,20 @@ get_uid (struct path * pp, int path_state, struct udev_device *udev,
 			len = strlen(pp->wwid);
 		origin = "callout";
 	} else {
+		bool udev_available = udev && pp->uid_attribute
+			&& *pp->uid_attribute;
 
-		if (udev && pp->uid_attribute) {
+		if (udev_available) {
 			len = get_udev_uid(pp, pp->uid_attribute, udev);
-			origin = "udev";
 			if (len <= 0)
 				condlog(1,
 					"%s: failed to get udev uid: %s",
 					pp->dev, strerror(-len));
-
-		} else if (pp->bus == SYSFS_BUS_SCSI) {
-			len = get_vpd_uid(pp);
-			origin = "sysfs";
+			else
+				origin = "udev";
 		}
-		if (len <= 0 && allow_fallback && has_uid_fallback(pp)) {
+		if ((!udev_available || (len <= 0 && allow_fallback))
+		    && has_uid_fallback(pp)) {
 			used_fallback = 1;
 			len = uid_fallback(pp, path_state, &origin);
 		}
Signature · 1 line
2.21.0
#30 lore raw

PATCH 26/30 libmultipath: fix has_uid_fallback() logic

In reply to Martin Wilck

The idea of 061daf40 "Do not automatically fall back to vpd uid generation" applies not only to SCSI. Use the same logic for NVMe. Allow fallback in two cases:
- uid_attribute has the default value for the bus in question
- uid_attribute has been set to "" to disable udev-based WWID checking
As uid_fallback() has only one caller, no need to check the conditions there again.

---
 libmultipath/defaults.h  |  1 +
 libmultipath/discovery.c | 20 +++++++++++++++-----
 libmultipath/hwtable.c   |  2 +-
 tests/hwtable.c          |  2 +-
 4 files changed, 18 insertions(+), 7 deletions(-)
libmultipath/defaults.h +1 -0
index 83f89f37..decc9335 100644
@@ -5,6 +5,7 @@
  * and the TEMPLATE in libmultipath/hwtable.c
  */
 #define DEFAULT_UID_ATTRIBUTE	"ID_SERIAL"
+#define DEFAULT_NVME_UID_ATTRIBUTE	"ID_WWN"
 #define DEFAULT_UDEVDIR		"/dev"
 #define DEFAULT_MULTIPATHDIR	"/" LIB_STRING "/multipath"
 #define DEFAULT_SELECTOR	"service-time 0"
libmultipath/discovery.c +15 -5
index d7eaee68..15f5cd4b 100644
@@ -1802,8 +1802,7 @@ static ssize_t uid_fallback(struct path *pp, int path_state,
 {
 	ssize_t len = -1;
 
-	if (pp->bus == SYSFS_BUS_SCSI &&
-	    !strcmp(pp->uid_attribute, DEFAULT_UID_ATTRIBUTE)) {
+	if (pp->bus == SYSFS_BUS_SCSI) {
 		len = get_vpd_uid(pp);
 		*origin = "sysfs";
 		if (len < 0 && path_state == PATH_UP) {
@@ -1833,11 +1832,22 @@ static ssize_t uid_fallback(struct path *pp, int path_state,
 	return len;
 }
 
-static int has_uid_fallback(struct path *pp)
+static bool has_uid_fallback(struct path *pp)
 {
+	/*
+	 * Falling back to direct WWID determination is dangerous
+	 * if uid_attribute is set to something non-standard.
+	 * Allow it only if it's either the default, or if udev
+	 * has been disabled by setting 'uid_attribute ""'.
+	 */
+	if (!pp->uid_attribute)
+		return false;
 	return ((pp->bus == SYSFS_BUS_SCSI &&
-		 !strcmp(pp->uid_attribute, DEFAULT_UID_ATTRIBUTE)) ||
-		pp->bus == SYSFS_BUS_NVME);
+		 (!strcmp(pp->uid_attribute, DEFAULT_UID_ATTRIBUTE) ||
+		  !strcmp(pp->uid_attribute, ""))) ||
+		(pp->bus == SYSFS_BUS_NVME &&
+		 (!strcmp(pp->uid_attribute, DEFAULT_NVME_UID_ATTRIBUTE) ||
+		  !strcmp(pp->uid_attribute, ""))));
 }
 
 int
libmultipath/hwtable.c +1 -1
index 1d964333..46caaf91 100644
@@ -88,7 +88,7 @@ static struct hwentry default_hw[] = {
 		/* Generic NVMe */
 		.vendor        = "NVME",
 		.product       = ".*",
-		.uid_attribute = "ID_WWN",
+		.uid_attribute = DEFAULT_NVME_UID_ATTRIBUTE,
 		.checker_name  = NONE,
 		.retain_hwhandler = RETAIN_HWHANDLER_OFF,
 	},
tests/hwtable.c +1 -1
index f436f52d..977a5663 100644
@@ -571,7 +571,7 @@ static void test_internal_nvme(const struct hwt_state *hwt)
 	mp = mock_multipath(pp);
 	assert_ptr_not_equal(mp, NULL);
 	TEST_PROP(checker_name(&pp->checker), NONE);
-	TEST_PROP(pp->uid_attribute, "ID_WWN");
+	TEST_PROP(pp->uid_attribute, DEFAULT_NVME_UID_ATTRIBUTE);
 	assert_int_equal(mp->pgpolicy, DEFAULT_PGPOLICY);
 	assert_int_equal(mp->no_path_retry, DEFAULT_NO_PATH_RETRY);
 	assert_int_equal(mp->retain_hwhandler, RETAIN_HWHANDLER_OFF);
Signature · 1 line
2.21.0
#31 lore raw

PATCH 27/30 libmultipath: fix memory leak with "uid_attrs" config option

In reply to Martin Wilck

pp->uid_attribute is normally just a pointer to memory that belongs to the configuration. But if "uid_attrs" is used, it's a pointer to strdup()d memory returned by parse_uid_attribute_by_attrs(). Consequently, this strdup()'d memory is never freed.

Fix this by splitting the uid_attrs string when the configuration is read, and using just a refererence to this memory in pp->uid_attribute. A side effect is that this makes the code more efficient in both memory and CPU terms.

This requires a change for the uevents test, as uid_attrs must now be set up differently.

---
 libmultipath/config.c  | 51 +++++++++++++++++++++++++++++++++++++++---
 libmultipath/config.h  |  6 ++++-
 libmultipath/dict.c    | 36 ++++++++++++++++++++++++++---
 libmultipath/propsel.c |  4 ++--
 libmultipath/uevent.c  |  5 ++---
 libmultipath/util.c    | 42 ----------------------------------
 libmultipath/util.h    |  1 -
 tests/globals.c        |  1 -
 tests/uevent.c         |  9 ++++++++
 9 files changed, 99 insertions(+), 56 deletions(-)
libmultipath/config.c +48 -3
index 141f092b..20e3b8bf 100644
@@ -585,8 +585,7 @@ free_config (struct config * conf)
 	if (conf->uid_attribute)
 		FREE(conf->uid_attribute);
 
-	if (conf->uid_attrs)
-		FREE(conf->uid_attrs);
+	vector_reset(&conf->uid_attrs);
 
 	if (conf->getuid)
 		FREE(conf->getuid);
@@ -718,7 +717,6 @@ load_config (char * file)
 	conf->remove_retries = 0;
 	conf->ghost_delay = DEFAULT_GHOST_DELAY;
 	conf->all_tg_pt = DEFAULT_ALL_TG_PT;
-
 	/*
 	 * preload default hwtable
 	 */
@@ -853,3 +851,50 @@ out:
 	free_config(conf);
 	return NULL;
 }
+
+char *get_uid_attribute_by_attrs(struct config *conf,
+				 const char *path_dev)
+{
+	vector uid_attrs = &conf->uid_attrs;
+	int j;
+	char *att, *col;
+
+	vector_foreach_slot(uid_attrs, att, j) {
+		col = strrchr(att, ':');
+		if (!col)
+			continue;
+		if (!strncmp(path_dev, att, col - att))
+			return col + 1;
+	}
+	return NULL;
+}
+
+int parse_uid_attrs(char *uid_attrs, struct config *conf)
+{
+	vector attrs  = &conf->uid_attrs;
+	char *uid_attr_record, *tmp;
+	int  ret = 0, count;
+
+	if (!uid_attrs)
+		return 1;
+
+	count = get_word(uid_attrs, &uid_attr_record);
+	while (uid_attr_record) {
+		tmp = strchr(uid_attr_record, ':');
+		if (!tmp) {
+			condlog(2, "invalid record in uid_attrs: %s",
+				uid_attr_record);
+			free(uid_attr_record);
+			ret = 1;
+		} else if (!vector_alloc_slot(attrs)) {
+			free(uid_attr_record);
+			ret = 1;
+		} else
+			vector_set_slot(attrs, uid_attr_record);
+		if (!count)
+			break;
+		uid_attrs += count;
+		count = get_word(uid_attrs, &uid_attr_record);
+	}
+	return ret;
+}
libmultipath/config.h +5 -1
index f5bf5b1b..ff2b4e86 100644
@@ -190,7 +190,7 @@ struct config {
 
 	char * multipath_dir;
 	char * selector;
-	char * uid_attrs;
+	struct _vector uid_attrs;
 	char * uid_attribute;
 	char * getuid;
 	char * features;
@@ -250,4 +250,8 @@ void free_config (struct config * conf);
 extern struct config *get_multipath_config(void);
 extern void put_multipath_config(void *);
 
+int parse_uid_attrs(char *uid_attrs, struct config *conf);
+char *get_uid_attribute_by_attrs(struct config *conf,
+				 const char *path_dev);
+
 #endif
libmultipath/dict.c +33 -3
index 96815f8a..c6eba0f6 100644
@@ -374,8 +374,38 @@ declare_ovr_snprint(selector, print_str)
 declare_mp_handler(selector, set_str)
 declare_mp_snprint(selector, print_str)
 
-declare_def_handler(uid_attrs, set_str)
-declare_def_snprint(uid_attrs, print_str)
+static int snprint_uid_attrs(struct config *conf, char *buff, int len,
+			     const void *dummy)
+{
+	char *p = buff;
+	int n, j;
+	const char *att;
+
+	vector_foreach_slot(&conf->uid_attrs, att, j) {
+		n = snprintf(p, len, "%s%s", j == 0 ? "" : " ", att);
+		if (n >= len)
+			return (p - buff) + n;
+		p += n;
+		len -= n;
+	}
+	return p - buff;
+}
+
+static int uid_attrs_handler(struct config *conf, vector strvec)
+{
+	char *val;
+
+	vector_reset(&conf->uid_attrs);
+	val = set_value(strvec);
+	if (!val)
+		return 1;
+	if (parse_uid_attrs(val, conf))
+		condlog(1, "error parsing uid_attrs: \"%s\"", val);
+	condlog(3, "parsed %d uid_attrs", VECTOR_SIZE(&conf->uid_attrs));
+	FREE(val);
+	return 0;
+}
+
 declare_def_handler(uid_attribute, set_str)
 declare_def_snprint_defstr(uid_attribute, print_str, DEFAULT_UID_ATTRIBUTE)
 declare_ovr_handler(uid_attribute, set_str)
@@ -1618,7 +1648,7 @@ init_keywords(vector keywords)
 	install_keyword("multipath_dir", &def_multipath_dir_handler, &snprint_def_multipath_dir);
 	install_keyword("path_selector", &def_selector_handler, &snprint_def_selector);
 	install_keyword("path_grouping_policy", &def_pgpolicy_handler, &snprint_def_pgpolicy);
-	install_keyword("uid_attrs", &def_uid_attrs_handler, &snprint_def_uid_attrs);
+	install_keyword("uid_attrs", &uid_attrs_handler, &snprint_uid_attrs);
 	install_keyword("uid_attribute", &def_uid_attribute_handler, &snprint_def_uid_attribute);
 	install_keyword("getuid_callout", &def_getuid_handler, &snprint_def_getuid);
 	install_keyword("prio", &def_prio_name_handler, &snprint_def_prio_name);
libmultipath/propsel.c +2 -2
index e6263e9b..6af2513d 100644
@@ -536,9 +536,9 @@ int select_getuid(struct config *conf, struct path *pp)
 {
 	const char *origin;
 
-	pp->uid_attribute = parse_uid_attribute_by_attrs(conf->uid_attrs, pp->dev);
+	pp->uid_attribute = get_uid_attribute_by_attrs(conf, pp->dev);
 	if (pp->uid_attribute) {
-		origin = "(setting: multipath.conf defaults section)";
+		origin = "(setting: multipath.conf defaults section / uid_attrs)";
 		goto out;
 	}
 
libmultipath/uevent.c +2 -3
index f73de8cc..8f7b2ef5 100644
@@ -163,13 +163,12 @@ uevent_get_wwid(struct uevent *uev)
 
 	conf = get_multipath_config();
 	pthread_cleanup_push(put_multipath_config, conf);
-	uid_attribute = parse_uid_attribute_by_attrs(conf->uid_attrs, uev->kernel);
+	uid_attribute = get_uid_attribute_by_attrs(conf, uev->kernel);
 	pthread_cleanup_pop(1);
 
 	val = uevent_get_env_var(uev, uid_attribute);
 	if (val)
 		uev->wwid = val;
-	FREE(uid_attribute);
 }
 
 bool
@@ -179,7 +178,7 @@ uevent_need_merge(void)
 	bool need_merge = false;
 
 	conf = get_multipath_config();
-	if (conf->uid_attrs)
+	if (VECTOR_SIZE(&conf->uid_attrs) > 0)
 		need_merge = true;
 	put_multipath_config(conf);
 
libmultipath/util.c +0 -42

48 lines not on this page · raw message

libmultipath/util.h +0 -1
index 1e0d832c..693991c1 100644
@@ -15,7 +15,6 @@ size_t strlcat(char *dst, const char *src, size_t size);
 int devt2devname (char *, int, char *);
 dev_t parse_devt(const char *dev_t);
 char *convert_dev(char *dev, int is_path_device);
-char *parse_uid_attribute_by_attrs(char *uid_attrs, char *path_dev);
 void setup_thread_attr(pthread_attr_t *attr, size_t stacksize, int detached);
 int systemd_service_enabled(const char *dev);
 int get_linux_version_code(void);
tests/globals.c +0 -1
index aeb7723f..8add5eb7 100644
@@ -5,7 +5,6 @@
 struct udev *udev;
 int logsink = -1;
 struct config conf = {
-	.uid_attrs = "sd:ID_BOGUS",
 	.verbosity = 4,
 };
 
tests/uevent.c +9 -0
index b0d0bfda..215d97ad 100644
@@ -43,7 +43,11 @@ void uevent_get_wwid(struct uevent *uev);
 
 static int setup_uev(void **state)
 {
+	static char test_uid_attrs[] =
+		"dasd:ID_SPAM   sd:ID_BOGUS nvme:ID_EGGS    ";
+
 	struct uevent *uev = alloc_uevent();
+	struct config *conf;
 
 	if (uev == NULL)
 		return -1;
@@ -51,11 +55,16 @@ static int setup_uev(void **state)
 	*state = uev;
 	uev->kernel = "sdo";
 	uev->envp[0] = "MAJOR=" str(MAJOR);
+	uev->envp[1] = "ID_SPAM=nonsense";
 	uev->envp[1] = "ID_BOGUS=" WWID;
 	uev->envp[2] = "MINOR=" str(MINOR);
 	uev->envp[3] = "DM_NAME=" DM_NAME;
 	uev->envp[4] = "DISK_RO=" str(DISK_RO);
 	uev->envp[5] = NULL;
+
+	conf = get_multipath_config();
+	parse_uid_attrs(test_uid_attrs, conf);
+	put_multipath_config(conf);
 	return 0;
 }
 
Signature · 1 line
2.21.0
#32 lore raw

PATCH 28/30 multipath-tools tests: add test for uid_attrs parsing

In reply to Martin Wilck
---
 tests/uevent.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
tests/uevent.c +18 -0
index 215d97ad..f4afd9b4 100644
@@ -95,6 +95,23 @@ static void test_ro_good(void **state)
 	assert_int_equal(uevent_get_disk_ro(uev), DISK_RO);
 }
 
+static void test_uid_attrs(void **state)
+{
+	/* see test_uid_attrs above */
+	struct config *conf = get_multipath_config();
+	vector attrs = &conf->uid_attrs;
+
+	assert_int_equal(VECTOR_SIZE(attrs), 3);
+	assert_null(get_uid_attribute_by_attrs(conf, "hda"));
+	assert_string_equal("ID_BOGUS",
+			    get_uid_attribute_by_attrs(conf, "sdaw"));
+	assert_string_equal("ID_SPAM",
+			    get_uid_attribute_by_attrs(conf, "dasdu"));
+	assert_string_equal("ID_EGGS",
+			    get_uid_attribute_by_attrs(conf, "nvme2n4"));
+	put_multipath_config(conf);
+}
+
 static void test_wwid(void **state)
 {
 	struct uevent *uev = *state;
@@ -280,6 +297,7 @@ int test_uevent_get_XXX(void)
 		cmocka_unit_test(test_minor_good),
 		cmocka_unit_test(test_ro_good),
 		cmocka_unit_test(test_dm_name_good),
+		cmocka_unit_test(test_uid_attrs),
 		cmocka_unit_test(test_wwid),
 		cmocka_unit_test(test_major_bad_0),
 		cmocka_unit_test(test_major_bad_1),
Signature · 1 line
2.21.0
#33 lore raw

PATCH 29/30 libmultipath: more cautious blacklisting by missing property

In reply to Martin Wilck

If a tool likk sg_inq or scsi_id fails to access a device during uevent processing, the required properties for whitelisting a device may be missing. This causes the device to be blacklisted and permanently orphaned, which is not desired.

Rather, blacklisting by missing properties is meant to ensure that the WWID determined from udev properties is _reliable_. Therefore, blacklist only devices that exhibit ID_SERIAL (or more generally, the configured uid_attribute), but do not have the required whitelist properties set. This will avoid the above-mentioned problem, because if failed device I/O was causing properties to be missing, ID_SERIAL will most likely also not be set.

---
 libmultipath/blacklist.c   | 26 +++++++++++++++++++++++--
 libmultipath/blacklist.h   |  2 +-
 libmultipath/configure.c   |  6 +++---
 libmultipath/discovery.c   |  2 +-
 multipath/multipath.conf.5 |  8 ++++++++
 tests/blacklist.c          | 39 +++++++++++++++++++++++++++++++++-----
 6 files changed, 71 insertions(+), 12 deletions(-)
libmultipath/blacklist.c +24 -2
index e0d02794..00e8dbdb 100644
@@ -366,7 +366,7 @@ filter_path (struct config * conf, struct path * pp)
 {
 	int r;
 
-	r = filter_property(conf, pp->udev, 3);
+	r = filter_property(conf, pp->udev, 3, pp->uid_attribute);
 	if (r > 0)
 		return r;
 	r = filter_devnode(conf->blist_devnode, conf->elist_devnode, pp->dev);
@@ -384,7 +384,8 @@ filter_path (struct config * conf, struct path * pp)
 }
 
 int
-filter_property(struct config *conf, struct udev_device *udev, int lvl)
+filter_property(struct config *conf, struct udev_device *udev, int lvl,
+		const char *uid_attribute)
 {
 	const char *devname = udev_device_get_sysname(udev);
 	struct udev_list_entry *list_entry;
@@ -395,7 +396,21 @@ filter_property(struct config *conf, struct udev_device *udev, int lvl)
 		/*
 		 * This is the inverse of the 'normal' matching;
 		 * the environment variable _has_ to match.
+		 * But only if the uid_attribute used for determining the WWID
+		 * of the path is is present in the environment
+		 * (uid_attr_seen). If this is not the case, udev probably
+		 * just failed to access the device, which should not cause the
+		 * device to be blacklisted (it won't be used by multipath
+		 * anyway without WWID).
+		 * Likewise, if no uid attribute is defined, udev-based WWID
+		 * determination is effectively off, and devices shouldn't be
+		 * blacklisted by missing properties (check_missing_prop).
 		 */
+
+		bool check_missing_prop = uid_attribute != NULL &&
+			*uid_attribute != '\0';
+		bool uid_attr_seen = false;
+
 		r = MATCH_PROPERTY_BLIST_MISSING;
 		udev_list_entry_foreach(list_entry,
 				udev_device_get_properties_list_entry(udev)) {
@@ -403,6 +418,10 @@ filter_property(struct config *conf, struct udev_device *udev, int lvl)
 			env = udev_list_entry_get_name(list_entry);
 			if (!env)
 				continue;
+
+			if (check_missing_prop && !strcmp(env, uid_attribute))
+				uid_attr_seen = true;
+
 			if (_blacklist_exceptions(conf->elist_property, env)) {
 				r = MATCH_PROPERTY_BLIST_EXCEPT;
 				break;
@@ -413,6 +432,9 @@ filter_property(struct config *conf, struct udev_device *udev, int lvl)
 			}
 			env = NULL;
 		}
+		if (r == MATCH_PROPERTY_BLIST_MISSING &&
+		    (!check_missing_prop || !uid_attr_seen))
+			r = MATCH_NOTHING;
 	}
 
 	log_filter(devname, NULL, NULL, NULL, env, NULL, r, lvl);
libmultipath/blacklist.h +1 -1
index 4c8ec99e..2d721f60 100644
@@ -37,7 +37,7 @@ int filter_devnode (vector, vector, char *);
 int filter_wwid (vector, vector, char *, char *);
 int filter_device (vector, vector, char *, char *, char *);
 int filter_path (struct config *, struct path *);
-int filter_property(struct config *, struct udev_device *, int);
+int filter_property(struct config *, struct udev_device *, int, const char*);
 int filter_protocol(vector, vector, struct path *);
 int store_ble (vector, char *, int);
 int set_ble_device (vector, char *, char *, int);
libmultipath/configure.c +3 -3
index c8dd69b0..b09ef529 100644
@@ -1369,7 +1369,7 @@ int get_refwwid(enum mpath_cmds cmd, char *dev, enum devtypes dev_type,
 		conf = get_multipath_config();
 		pthread_cleanup_push(put_multipath_config, conf);
 		if (pp->udev && pp->uid_attribute &&
-		    filter_property(conf, pp->udev, 3) > 0)
+		    filter_property(conf, pp->udev, 3, pp->uid_attribute) > 0)
 			invalid = 1;
 		pthread_cleanup_pop(1);
 		if (invalid)
@@ -1409,7 +1409,7 @@ int get_refwwid(enum mpath_cmds cmd, char *dev, enum devtypes dev_type,
 		conf = get_multipath_config();
 		pthread_cleanup_push(put_multipath_config, conf);
 		if (pp->udev && pp->uid_attribute &&
-		    filter_property(conf, pp->udev, 3) > 0)
+		    filter_property(conf, pp->udev, 3, pp->uid_attribute) > 0)
 			invalid = 1;
 		pthread_cleanup_pop(1);
 		if (invalid)
@@ -1438,7 +1438,7 @@ int get_refwwid(enum mpath_cmds cmd, char *dev, enum devtypes dev_type,
 		conf = get_multipath_config();
 		pthread_cleanup_push(put_multipath_config, conf);
 		if (pp->udev && pp->uid_attribute &&
-		    filter_property(conf, pp->udev, 3) > 0)
+		    filter_property(conf, pp->udev, 3, pp->uid_attribute) > 0)
 			invalid = 1;
 		pthread_cleanup_pop(1);
 		if (invalid)
libmultipath/discovery.c +1 -1
index 15f5cd4b..acca466c 100644
@@ -1945,7 +1945,7 @@ int pathinfo(struct path *pp, struct config *conf, int mask)
 			return PATHINFO_SKIPPED;
 		}
 		if (is_claimed_by_foreign(pp->udev) ||
-		    filter_property(conf, pp->udev, 4) > 0)
+		    filter_property(conf, pp->udev, 4, pp->uid_attribute) > 0)
 			return PATHINFO_SKIPPED;
 	}
 
multipath/multipath.conf.5 +8 -0
index 6f08980a..d5fe38ac 100644
@@ -1241,6 +1241,14 @@ otherwise they're treated as blacklisted, and the message
 .
 .RS
 .PP
+.B Note:
+The behavior of this option has changed in \fBmultipath-tools\fR 0.8.2
+compared to previous versions.
+Blacklisting by missing properties is only applied to devices which do have the
+property specified by \fIuid_attribute\fR (e.g. \fIID_SERIAL\fR)
+set. Previously, it was applied to every device, possibly causing devices to be
+blacklisted because of temporary I/O error conditions.
+.PP
 The default \fIblacklist exception\fR is: \fB(SCSI_IDENT_|ID_WWN)\fR, causing
 well-behaved SCSI devices and devices that provide a WWN (World Wide Number)
 to be included, and all others to be excluded.
tests/blacklist.c +34 -5
index 54d568f5..362c44d9 100644
@@ -267,7 +267,7 @@ static void test_property_blacklist(void **state)
 	static struct udev_device udev = { "sdb", { "ID_FOO", "ID_WWN", "ID_BAR", NULL } };
 	conf.blist_property = blist_property_wwn;
 	expect_condlog(3, "sdb: udev property ID_WWN blacklisted\n");
-	assert_int_equal(filter_property(&conf, &udev, 3),
+	assert_int_equal(filter_property(&conf, &udev, 3, "ID_SERIAL"),
 			 MATCH_PROPERTY_BLIST);
 }
 
@@ -281,17 +281,23 @@ static void test_property_whitelist(void **state)
 	static struct udev_device udev = { "sdb", { "ID_FOO", "ID_WWN", "ID_BAR", NULL } };
 	conf.elist_property = blist_property_wwn;
 	expect_condlog(3, "sdb: udev property ID_WWN whitelisted\n");
-	assert_int_equal(filter_property(&conf, &udev, 3),
+	assert_int_equal(filter_property(&conf, &udev, 3, "ID_SERIAL"),
 			 MATCH_PROPERTY_BLIST_EXCEPT);
 }
 
 static void test_property_missing(void **state)
 {
-	static struct udev_device udev = { "sdb", { "ID_FOO", "ID_BAZ", "ID_BAR", NULL } };
+	static struct udev_device udev = { "sdb", { "ID_FOO", "ID_BAZ", "ID_BAR", "ID_SERIAL", NULL } };
 	conf.blist_property = blist_property_wwn;
 	expect_condlog(3, "sdb: blacklisted, udev property missing\n");
-	assert_int_equal(filter_property(&conf, &udev, 3),
+	assert_int_equal(filter_property(&conf, &udev, 3, "ID_SERIAL"),
 			 MATCH_PROPERTY_BLIST_MISSING);
+	assert_int_equal(filter_property(&conf, &udev, 3, "ID_BLAH"),
+			 MATCH_NOTHING);
+	assert_int_equal(filter_property(&conf, &udev, 3, ""),
+			 MATCH_NOTHING);
+	assert_int_equal(filter_property(&conf, &udev, 3, NULL),
+			 MATCH_NOTHING);
 }
 
 struct udev_device test_udev = { "sdb", { "ID_FOO", "ID_WWN", "ID_BAR", NULL } };
@@ -347,16 +353,25 @@ static void test_filter_path_wwid(void **state)
 	assert_int_equal(filter_path(&conf, &test_pp), MATCH_WWID_BLIST);
 }
 
-struct udev_device miss_udev = { "sdb", { "ID_FOO", "ID_BAZ", "ID_BAR", NULL } };
+struct udev_device miss_udev = { "sdb", { "ID_FOO", "ID_BAZ", "ID_BAR", "ID_SERIAL", NULL } };
 
 struct path miss1_pp = { .dev = "sdc", .bus = SYSFS_BUS_SCSI,
 			.udev = &miss_udev,
+			 .uid_attribute = "ID_SERIAL",
 			.sg_id.proto_id = SCSI_PROTOCOL_ISCSI,
 			.vendor_id = "foo", .product_id = "baz",
 			.wwid = "plugh" };
 
 struct path miss2_pp = { .dev = "sdc", .bus = SYSFS_BUS_SCSI,
 			.udev = &test_udev,
+			 .uid_attribute = "ID_SERIAL",
+			.sg_id.proto_id = SCSI_PROTOCOL_ISCSI,
+			.vendor_id = "foo", .product_id = "baz",
+			.wwid = "plugh" };
+
+struct path miss3_pp = { .dev = "sdc", .bus = SYSFS_BUS_SCSI,
+			.udev = &miss_udev,
+			 .uid_attribute = "ID_EGGS",
 			.sg_id.proto_id = SCSI_PROTOCOL_ISCSI,
 			.vendor_id = "foo", .product_id = "baz",
 			.wwid = "plugh" };
@@ -387,6 +402,19 @@ static void test_filter_path_missing2(void **state)
 			 MATCH_NOTHING);
 }
 
+/* Here we use a different uid_attribute which is also missing, thus
+   the path is not blacklisted */
+static void test_filter_path_missing3(void **state)
+{
+	conf.blist_property = blist_property_wwn;
+	conf.blist_devnode = blist_devnode_sdb;
+	conf.blist_device = blist_device_foo_bar;
+	conf.blist_protocol = blist_protocol_fcp;
+	conf.blist_wwid = blist_wwid_xyzzy;
+	assert_int_equal(filter_path(&conf, &miss3_pp),
+			 MATCH_NOTHING);
+}
+
 static void test_filter_path_whitelist(void **state)
 {
 	conf.elist_property = blist_property_wwn;
@@ -495,6 +523,7 @@ int test_blacklist(void)
 		test_and_reset(test_filter_path_wwid),
 		test_and_reset(test_filter_path_missing1),
 		test_and_reset(test_filter_path_missing2),
+		test_and_reset(test_filter_path_missing3),
 		test_and_reset(test_filter_path_whitelist),
 		test_and_reset(test_filter_path_whitelist_property),
 		test_and_reset(test_filter_path_whitelist_devnode),
Signature · 1 line
2.21.0
#34 lore raw

PATCH 30/30 multipath.conf.5: Improve documentation of WWID determination

In reply to Martin Wilck

Clarify the effect of setting uid_attrs, and document the effect of setting uid_attribute to an empty string. Replace the arcane "vpd_pg83" with something easier to understand. Tell the user that the defaults are usually good enough.

Cc Tang Junhui <[email protected]>
Cc Xose Vazquez Perez <[email protected]>
---
 multipath/multipath.conf.5 | 48 ++++++++++++++++++++++++--------------
 1 file changed, 31 insertions(+), 17 deletions(-)
multipath/multipath.conf.5 +31 -17
index d5fe38ac..f7d21b4c 100644
@@ -241,23 +241,32 @@ The default is: \fBfailover\fR
 .
 .TP
 .B uid_attrs
-The udev attribute providing a unique path identifier for corresponding
-type of path devices. If this field is configured and matched with type
-of device, it would override any other methods providing for device
-unique identifier in config file, and it would activate merging uevents
-according to the identifier to promote effiecncy in processing uevents.
-It has no default value, if you want to identify path by udev attribute
-and to activate merging uevents for SCSI and DASD devices, you can set
-its value as: \fIuid_attrs "sd:ID_SERIAL dasd:ID_UID"\fR.
+.
+Setting this option activates \fBmerging uevents\fR by WWID, which may improve
+uevent processing effiency. Moreover, it's an alternative method to configure
+the udev properties to use for determining unique path identifiers (WWIDs).
 .RS
-.TP
-The default is: \fB<unset>\fR
+.PP
+The value of this option is a space separated list of records like
+\(dq\fItype:ATTR\fR\(dq, where \fItype\fR is matched against the beginning
+of the device node name (e.g. \fIsd:ATTR\fR matches \fIsda\fR), and
+\fIATTR\fR is the name of the udev property to use for matching devices.
+.PP
+If this option is configured and matches the device
+node name of a device, it overrides any other configured  methods for
+determining the WWID for this device.
+.PP
+The default is: \fB<unset>\fR. To enable uevent merging, set it e.g. to
+\(dqsd:ID_SERIAL dasd:ID_UID nvme:ID_WWN\(dq.
 .RE
 .
 .
 .TP
 .B uid_attribute
-The udev attribute providing a unique path identifier.
+The udev attribute providing a unique path identifier (WWID). If
+\fIuid_attribute\fR is set to the empty string, WWID determination is done
+using the \fIsysfs\fR method rather then using udev (not recommended in
+production; see \fBWWID generation\fR below).
 .RS
 .TP
 The default is: \fBID_SERIAL\fR, for SCSI devices
@@ -1635,8 +1644,8 @@ WWID is assumed to point to the same device.
 The WWID is generated by four methods (in the order of preference):
 .TP 17
 .B uid_attrs
-The WWID is derived from udev attributes by matching the device node name. See
-description of \fIuid_attrs\fR in the defaults section above.
+The WWID is derived from udev attributes by matching the device node name; cf
+\fIuid_attrs\fR above.
 .TP
 .B getuid_callout
 Use the specified external program; cf \fIgetuid_callout\fR above.
@@ -1651,10 +1660,15 @@ does not need to call any external programs here. However, under
 certain circumstances udev might not be able to generate the requested
 variable.
 .TP
-.B vpd_pg83
-If none of the \fIgetuid_callout\fR or \fIuid_attribute\fR parameters
-are present multipath will try to use the sysfs attribute
-\fIvpd_pg83\fR to generate the WWID.
+.B sysfs
+Try to determine the WWID from sysfs attributes.
+For SCSI devices, this means reading the Vital Product Data (VPD) page
+\(dqDevice Identification\(dq (0x83).
+.PP
+The default settings (using udev and \fBuid_attribute\fR configured from
+the built-in hardware table) should work fine
+in most scenarios. Users who want to enable uevent merging must set
+\fBuid_attrs\fR.
 .
 .
 .\" ----------------------------------------------------------------------------
Signature · 1 line
2.21.0
#35 lore raw
In reply to Martin Wilck
Quoted 1 line · Martin Wilck

Hi Christophe, hi Ben,

ACK for everything, except my nitpicks for patches 5 and 7. I don't know how much we care about patch 5 temporarily breaking compilation, but it no one else is bothered by it, I would be fine with simply tacking on a patch at the end to fix my issues with patch 7.

-Ben

Quoted 116 lines

This series started out with me trying to fix a couple of warnings emitted by gcc 9, and grew substantially while I encountered other issues. Most of this series, except patch 25ff, are cleanups, corner case fixes, and unit tests.

The parts of the series with user visible impact are 25, 26, and 29. Patch 25/30 implements an earlier idea of mine
(https://www.redhat.com/archives/dm-devel/2019-April/msg00005.html).
The idea for patch 29/30 has been discussed before, too
(https://www.redhat.com/archives/dm-devel/2019-March/msg00201.html)
The fix for uid_attrs (26/30) occured to me while I was working on the get_uid() code path.

Patch 1/30 up to 8/30 are more or less straightforward fixes for gcc warnings related to string operations. See
https://developers.redhat.com/blog/2018/05/24/detecting-string-truncation-with-gcc-8/
(not sure why it says gcc 8 there, I'm seeing these warnings with gcc9 only). Most of the gcc warnings have been "solved" by replacing strncpy() calls with strlcpy(). That has the disadvantage that the compiler's static checking, which caused the warnings in the first place, is now simply disabled. Yet in those places where I did the replacement, I reckon that strlcpy() semantics is what we actually want. Most of the time, we try to ensure that "wwid" fields are properly 0-terminated. The previous code attempted to do this using constructs like

strncpy(x->wwid, y->wwid, WWID_SIZE - 1)

relying on x[WWID_SIZE-1] being 0 already. gcc 9 doesn't appreciate this style, and I tend to agree.

This lead me to check the code paths where wwid fields are originally set, resulting in patch 9/30 and the series from 14/30 to 24/30. In particular the VPD parsing code was full of small mistakes if the possibility of buffer overflow is taken into account (unlikely to hurt in practice, as our default WWID_SIZE is large enough to hold almost every WWID). In order not to break stuff, I added unit test code (10-13). The tests, in turn, made me find some more minor problems in the VPD parsing code (patches 14, 18, 21, 22, 24).

Reviews and comments welcome.

Regards,
Martin

Martin Wilck (30):
kpartx: dasd: fix -Waddress-of-packed-member warning from gcc9
libmultipath: fix gcc -Wstringop-truncation warning in set_value()
libmultipath: remove 'space' argument to merge_words()
libmultipath: fix -Wstringop-overflow warning in merge_words()
multipath-tools: fix more gcc 9 -Wstringop-truncation warnings
multipath-tools: Fix more strncpy(X, Y, size - 1) calls
libmpathcmd: use target length in strncpy() call
libmultipath: inline set_default()
libmultipath: add size argument to dm_get_uuid() multipath-tools tests: omit timestamp in log messages tests/hwtable: decrease log verbosity
multipath-tools tests: add strlcpy() tests multipath-tools tests: add test for VPD parsing
libmultipath: fix parsing of VPD 83 type 1 (T10 vendor ID)
libmultipath: Fix buffer overflow in parse_vpd_pg80()
libmultipath: fix possible WWID overflow in parse_vpd_pg83()
libmultipath: fix another WWID overflow in parse_vpd_pg83()
libmultipath: fix parsing of SCSI name string, iqn format
libmultipath: add consistent WWID overflow logging in parse_vpd_pg83
libmultipath: parse_vpd_pg83: common code for SCSI string parsing
libmultipath: allow zero-padded SCSI names in parse_vpd_pg83()
libmultipath: fix parsing of non-space-terminated T10 ID
libmultipath: parse_vpd_pg80: fix overflow output
libmultipath: parse_vpd_pg80: fix whitespace handling
libmultipath: get_uid: straighten the fallback logic
libmultipath: fix has_uid_fallback() logic
libmultipath: fix memory leak with "uid_attrs" config option multipath-tools tests: add test for uid_attrs parsing
libmultipath: more cautious blacklisting by missing property multipath.conf.5: Improve documentation of WWID determination

 kpartx/dasd.h                   |   2 +-
 libmpathcmd/mpath_cmd.c         |   2 +-
 libmpathpersist/mpath_persist.c |  10 +-
 libmultipath/blacklist.c        |  26 +-
 libmultipath/blacklist.h        |   2 +-
 libmultipath/config.c           |  51 ++-
 libmultipath/config.h           |   6 +-
 libmultipath/configure.c        |  17 +-
 libmultipath/debug.c            |   1 +
 libmultipath/defaults.c         |  17 -
 libmultipath/defaults.h         |  10 +-
 libmultipath/devmapper.c        |  28 +-
 libmultipath/devmapper.h        |   2 +-
 libmultipath/dict.c             |  36 +-
 libmultipath/discovery.c        | 162 ++++---
 libmultipath/dmparser.c         |  51 +--
 libmultipath/hwtable.c          |   2 +-
 libmultipath/parser.c           |  20 +-
 libmultipath/prio.c             |   3 +-
 libmultipath/propsel.c          |   4 +-
 libmultipath/structs_vec.c      |   3 +-
 libmultipath/uevent.c           |   5 +-
 libmultipath/util.c             |  44 +-
 libmultipath/util.h             |   1 -
 libmultipath/uxsock.c           |   2 +-
 libmultipath/wwids.c            |   3 +-
 multipath/multipath.conf.5      |  56 ++-
 multipathd/main.c               |   2 +-
 multipathd/waiter.c             |   3 +-
 tests/Makefile                  |   4 +-
 tests/blacklist.c               |  39 +-
 tests/globals.c                 |   3 +-
 tests/hwtable.c                 |   6 +-
 tests/uevent.c                  |  27 ++
 tests/util.c                    | 142 ++++++
 tests/vpd.c                     | 790 ++++++++++++++++++++++++++++++++
 36 files changed, 1332 insertions(+), 250 deletions(-)
 create mode 100644 tests/vpd.c
Signature · 1 line
2.21.0