Skip to contentSkip to conversation

multipath-tools: coverity fixes

PATCH Superseded [email protected] 2 participants 35 messages Nov 18, 2021 – Nov 30, 2021 Device mapper

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover multipath-tools: coverity fixes

From: Martin Wilck <[email protected]>

Hi Christophe, hi Ben,

I have started a new attempt to fix defects reported by coverity. With this set (on top of the previously posted one), and a number of false positive classifications, I have been able to bring the defect count all the way down to zero.

Most of these patches fix defects reported by coverity. Some fix other things that occured to me while working on this.

Regards,
Martin

Martin Wilck (21):
multipath tools: github workflows: add coverity workflow multipathd (coverity): check atexit() return value multipathd (coverity): terminate uxlsnr when polls allocation fails
libmultipath: strbuf: add __get_strbuf_buf() libmultipath (coverity): improve input checking in parse_vpd_pg83
multipath-tools: add tests for broken VPD page 83
libmultipath: use strbuf in parse_vpd_pg83() libmultipath (coverity): fix tainted values in alua_rtpg.c multipath, multipathd: exit if bindings file is broken libmultipath (coverity): silence unchecked return value warning
multipath: remove pointless code from getopt processing libmultipath (coverity): set umask before mkstemp multipathd (coverity): simplify set_oom_adj()
kpartx: open /dev/loop-control only once
kpartx: use opened loop device immediately
kpartx: find_unused_loop_device(): add newlines multipathd (coverity): daemonize(): use dup2 libmultipath (coverity): avoid sleeping in dm_mapname() libmultipath (coverity): Revert "setup_map: wait for pending path
checkers to finish"
libmultipath (coverity): check return values in dm_get_multipath()
libmultipath: update_pathvec_from_dm: don't force DI_WWID

 .github/workflows/coverity.yaml       |  51 ++++++
 kpartx/kpartx.c                       |   4 +-
 kpartx/lopart.c                       | 100 +++++-----
 kpartx/lopart.h                       |   3 +-
 libmultipath/alias.c                  |   4 +
 libmultipath/configure.c              |  63 +------
 libmultipath/devmapper.c              |  23 +--
 libmultipath/discovery.c              | 252 ++++++++++++++------------
 libmultipath/prioritizers/alua_rtpg.c |  13 +-
 libmultipath/prioritizers/alua_spc3.h |  30 ++-
 libmultipath/propsel.c                |   2 +-
 libmultipath/strbuf.c                 |   5 +
 libmultipath/strbuf.h                 |  14 ++
 libmultipath/structs_vec.c            |   7 +-
 multipath/main.c                      |  13 +-
 multipathd/main.c                     |  91 ++++------
 multipathd/uxlsnr.c                   |   1 +
 tests/vpd.c                           | 108 +++++++++++
 18 files changed, 456 insertions(+), 328 deletions(-)
 create mode 100644 .github/workflows/coverity.yaml
Signature · 7 lines
2.33.1


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

PATCH 1/21 multipath tools: github workflows: add coverity workflow

From: Martin Wilck <[email protected]>

Add a workflow that triggers a coverity static analysis scan. For now, this will only be done on a special branch called "coverity". Pushing to that branch will trigger the workflow.

For this to work, 3 secrets need to be set in the Github repository:

COVERITY_SCAN_EMAIL: the email address for coverity/synopsis account
COVERITY_SCAN_TOKEN: the coverity / synopsis access token
COVERITY_SCAN_PROJECT: the coverity project, e.g. mwilck/multipath-tools

The workflow succeeds if upload of the coverity results was successful. The analysis result will be emailed to the given address.

---
 .github/workflows/coverity.yaml | 51 +++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
 create mode 100644 .github/workflows/coverity.yaml
.github/workflows/coverity.yaml new file +51 -0
new file mode 100644
index 0000000..a8b56d4
@@ -0,0 +1,51 @@
+name: coverity
+on:
+  push:
+    branches:
+      - coverity
+
+jobs:
+  upload-coverity-scan:
+    runs-on: ubuntu-20.04
+    steps:
+      - name: checkout
+        uses: actions/checkout@v2
+      - name: dependencies
+        run: >
+          sudo apt-get install --yes
+          gcc make pkg-config
+          libdevmapper-dev libreadline-dev libaio-dev libsystemd-dev
+          libudev-dev libjson-c-dev liburcu-dev libcmocka-dev
+      - name: download coverity
+        run: >
+          curl -o cov-analysis-linux64.tar.gz
+          --form token="$COV_TOKEN"
+          --form project="$COV_PROJECT"
+          https://scan.coverity.com/download/cxx/linux64
+        env:
+          COV_TOKEN: ${{ secrets.COVERITY_SCAN_TOKEN }}
+          COV_PROJECT:  ${{ secrets.COVERITY_SCAN_PROJECT }}
+      - name: unpack coverity
+        run: |
+          mkdir -p coverity
+          tar xfz cov-analysis-linux64.tar.gz --strip 1 -C coverity
+      - name: build with cov-build
+        run: >
+          PATH="$PWD/coverity/bin:$PATH"
+          cov-build --dir cov-int make -O -j"$(grep -c ^processor /proc/cpuinfo)"
+      - name: pack results
+        run: tar cfz multipath-tools.tgz cov-int
+      - name: submit results
+        run: >
+          curl
+          --form token="$COV_TOKEN"
+          --form email="$COV_EMAIL"
+          --form file="@multipath-tools.tgz"
+          --form version="${{ github.ref_name }}"
+          --form description="$(git describe --tags --match "0.*")"
+          --form project="$COV_PROJECT"
+          https://scan.coverity.com/builds
+        env:
+          COV_TOKEN: ${{ secrets.COVERITY_SCAN_TOKEN }}
+          COV_PROJECT:  ${{ secrets.COVERITY_SCAN_PROJECT }}
+          COV_EMAIL: ${{ secrets.COVERITY_SCAN_EMAIL }}
Signature · 7 lines
2.33.1


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

PATCH 2/21 multipathd (coverity): check atexit() return value

In reply to [email protected]

From: Martin Wilck <[email protected]>

---
 multipath/main.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
multipath/main.c +2 -1
index 47e799b..d6291f7 100644
@@ -838,7 +838,8 @@ main (int argc, char *argv[])
 	conf = get_multipath_config();
 	conf->retrigger_tries = 0;
 	conf->force_sync = 1;
-	atexit(cleanup_vecs);
+	if (atexit(cleanup_vecs))
+		condlog(1, "failed to register cleanup handler for vecs: %m");
 	while ((arg = getopt(argc, argv, ":adDcChl::eFfM:v:p:b:BrR:itTquUwW")) != EOF ) {
 		switch(arg) {
 		case 1: printf("optarg : %s\n",optarg);
Signature · 7 lines
2.33.1


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

PATCH 3/21 multipathd (coverity): terminate uxlsnr when polls allocation fails

In reply to [email protected]

From: Martin Wilck <[email protected]>

The polls array is dereferenced later on, so even if we are soon to be cancelled, we should return immediately here.

---
 multipathd/uxlsnr.c | 1 +
 1 file changed, 1 insertion(+)
multipathd/uxlsnr.c +1 -0
index 6355279..d42cb04 100644
@@ -607,6 +607,7 @@ void *uxsock_listen(long ux_sock, void *trigger_data)
 	if (!polls) {
 		condlog(0, "uxsock: failed to allocate poll fds");
 		exit_daemon();
+		return NULL;
 	}
 	notify_fd = inotify_init1(IN_NONBLOCK);
 	if (notify_fd == -1) /* it's fine if notifications fail */
Signature · 7 lines
2.33.1


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

PATCH 4/21 libmultipath: strbuf: add __get_strbuf_buf()

In reply to [email protected]

From: Martin Wilck <[email protected]>

Add a function for internal use that allows direct manipulation of the strbuf's content.

---
 libmultipath/strbuf.c |  5 +++++
 libmultipath/strbuf.h | 14 ++++++++++++++
 2 files changed, 19 insertions(+)
libmultipath/strbuf.c +5 -0
index a24a57d..f654594 100644
@@ -15,6 +15,11 @@
 
 static const char empty_str[] = "";
 
+char *__get_strbuf_buf(struct strbuf *buf)
+{
+	return buf->buf;
+}
+
 const char *get_strbuf_str(const struct strbuf *buf)
 {
 	return buf->buf ? buf->buf : empty_str;
libmultipath/strbuf.h +14 -0
index 5903572..41d7d54 100644
@@ -54,6 +54,20 @@ void free_strbuf(struct strbuf *buf);
  */
 struct strbuf *new_strbuf(void);
 
+/**
+ * get_strbuf_buf(): retrieve a pointer to the strbuf's buffer
+ * @param buf: a struct strbuf
+ * @returns: pointer to the string written to the strbuf so far.
+ *
+ * INTERNAL ONLY.
+ * DANGEROUS: Unlike the return value of get_strbuf_str(),
+ * this string can be written to, modifying the strbuf's content.
+ * USE WITH CAUTION.
+ * If @strbuf was never written to, the function returns NULL.
+ * The return value of this function must not be free()d.
+ */
+char *__get_strbuf_buf(struct strbuf *buf);
+
 /**
  * get_strbuf_str(): retrieve string from strbuf
  * @param buf: a struct strbuf
Signature · 7 lines
2.33.1


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

PATCH 5/21 libmultipath (coverity): improve input checking in parse_vpd_pg83

In reply to [email protected]

From: Martin Wilck <[email protected]>

Check offsets and other obvious errors in the VPD83 data.

The original reason to do this was to fix "tained scalar" warnings from coverity. But this doesn't suffice for coverity without using a constant boundary (WWID_SIZE) for "len" in parse_vpd_pg80() and for "vpd_len" in parse_vpd_pg83(), even though the computed boundaries are tighter than the constant ones.

---
 libmultipath/discovery.c | 139 +++++++++++++++++++++++++--------------
 1 file changed, 90 insertions(+), 49 deletions(-)
libmultipath/discovery.c +90 -49
index 5edf959..5ce9031 100644
@@ -37,6 +37,8 @@
 #include "print.h"
 #include "strbuf.h"
 
+#define VPD_BUFLEN 4096
+
 struct vpd_vendor_page vpd_vendor_pages[VPD_VP_ARRAY_SIZE] = {
 	[VPD_VP_UNDEF]	= { 0x00, "undef" },
 	[VPD_VP_HP3PAR]	= { 0xc0, "hp3par" },
@@ -1085,6 +1087,8 @@ parse_vpd_pg80(const unsigned char *in, char *out, size_t out_len)
 	if (out_len == 0)
 		return 0;
 
+	if (len > WWID_SIZE)
+		len = WWID_SIZE;
 	/*
 	 * Strip leading and trailing whitespace
 	 */
@@ -1114,84 +1118,122 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 	const unsigned char *d;
 	const unsigned char *vpd = NULL;
 	size_t len, vpd_len, i;
-	int vpd_type, prio = -1, naa_prio;
+	int vpd_type, prio = -1;
+	int err = -ENODATA;
 
-	d = in + 4;
-	while (d < in + in_len) {
-		/* Select 'association: LUN' */
-		if ((d[1] & 0x30) != 0) {
-			d += d[3] + 4;
-			continue;
-		}
-		switch (d[1] & 0xf) {
+        /* Need space at least for one digit */
+	if (out_len <= 1)
+		return 0;
+
+        d = in + 4;
+	while (d <= in + in_len - 4) {
+		bool invalid = false;
+		int new_prio = -1;
+
+                /* Select 'association: LUN' */
+		if ((d[1] & 0x30) == 0x30) {
+			invalid = true;
+			goto next_designator;
+		} else if ((d[1] & 0x30) != 0x00)
+			goto next_designator;
+
+                switch (d[1] & 0xf) {
+			unsigned char good_len;
 		case 0x3:
 			/* NAA: Prio 5 */
 			switch (d[4] >> 4) {
 			case 6:
 				/* IEEE Registered Extended: Prio 8 */
-				naa_prio = 8;
+				new_prio = 8;
+				good_len = 16;
 				break;
 			case 5:
 				/* IEEE Registered: Prio 7 */
-				naa_prio = 7;
+				new_prio = 7;
+				good_len = 8;
 				break;
 			case 2:
 				/* IEEE Extended: Prio 6 */
-				naa_prio = 6;
+				new_prio = 6;
+				good_len = 8;
 				break;
 			case 3:
 				/* IEEE Locally assigned: Prio 1 */
-				naa_prio = 1;
+				new_prio = 1;
+				good_len = 8;
 				break;
 			default:
 				/* Default: no priority */
-				naa_prio = -1;
+				good_len = 0xff;
 				break;
 			}
-			if (prio < naa_prio) {
-				prio = naa_prio;
-				vpd = d;
-			}
+
+                        invalid = good_len == 0xff || good_len != d[3];
 			break;
 		case 0x2:
 			/* EUI-64: Prio 4 */
-			if (prio < 4) {
-				prio = 4;
-				vpd = d;
-			}
+			invalid = (d[3] != 8 && d[3] != 12 && d[3] != 16);
+			new_prio = 4;
 			break;
 		case 0x8:
 			/* SCSI Name: Prio 3 */
-			if (memcmp(d + 4, "eui.", 4) &&
-			    memcmp(d + 4, "naa.", 4) &&
-			    memcmp(d + 4, "iqn.", 4))
-				break;
-			if (prio < 3) {
-				prio = 3;
-				vpd = d;
-			}
+			invalid = (d[3] < 4 || (memcmp(d + 4, "eui.", 4) &&
+						memcmp(d + 4, "naa.", 4) &&
+						memcmp(d + 4, "iqn.", 4)));
+			new_prio = 3;
 			break;
 		case 0x1:
 			/* T-10 Vendor ID: Prio 2 */
-			if (prio < 2) {
-				prio = 2;
-				vpd = d;
-			}
+			invalid = (d[3] < 8);
+			new_prio = 2;
 			break;
+		case 0xa:
+			condlog(2, "%s: UUID identifiers not yet supported",
+				__func__);
+			break;
+		default:
+			invalid = true;
+			break;
+		}
+
+	next_designator:
+		if (d + d[3] + 4 - in > (ssize_t)in_len) {
+			condlog(2, "%s: device descriptor length overflow: %zd > %zu",
+				__func__, d + d[3] + 4 - in, in_len);
+			err = -EOVERFLOW;
+			break;
+		}
+                if (invalid) {
+			condlog(2, "%s: invalid device designator at offset %zd: %02x%02x%02x%02x",
+				__func__, d - in, d[0], d[1], d[2], d[3]);
+			err = -EINVAL;
+			break;
+		}
+		if (new_prio > prio) {
+			vpd = d;
+			prio = new_prio;
 		}
 		d += d[3] + 4;
 	}
 
 	if (prio <= 0)
-		return -ENODATA;
-	/* Need space at least for one digit */
-	else if (out_len <= 1)
-		return 0;
+		return err;
+
+	if (d != in + in_len)
+		/* Should this be fatal? (overflow covered above) */
+		condlog(2, "%s: warning: last descriptor end %zd != VPD length %zu",
+			__func__, d - in, in_len);
 
 	len = 0;
 	vpd_type = vpd[1] & 0xf;
 	vpd_len = vpd[3];
 	vpd += 4;
+	/* untaint vpd_len for coverity */
+	if (vpd_len > WWID_SIZE) {
+		condlog(1, "%s: suspicious designator length %zu truncated to %u",
+			__func__, vpd_len, WWID_SIZE);
+		vpd_len = WWID_SIZE;
+	}
 	if (vpd_type == 0x2 || vpd_type == 0x3) {
 		size_t i;
 
@@ -1205,10 +1247,6 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 		for (i = 0; i < vpd_len; i++)
 			len += sprintf(out + len,
 				       "%02x", vpd[i]);
-	} else if (vpd_type == 0x8 && vpd_len < 4) {
-		condlog(1, "%s: VPD length %zu too small for designator type 8",
-			__func__, vpd_len);
-		return -EINVAL;
 	} else if (vpd_type == 0x8) {
 		if (!memcmp("eui.", vpd, 4))
 			out[0] =  '2';
@@ -1315,11 +1353,12 @@ parse_vpd_c0_hp3par(const unsigned char *in, size_t in_len,
 static int
 get_vpd_sysfs (struct udev_device *parent, int pg, char * str, int maxlen)
 {
-	int len, buff_len;
-	unsigned char buff[4096];
+	int len;
+	size_t buff_len;
+	unsigned char buff[VPD_BUFLEN];
 
-	memset(buff, 0x0, 4096);
-	if (!parent || sysfs_get_vpd(parent, pg, buff, 4096) <= 0) {
+	memset(buff, 0x0, VPD_BUFLEN);
+	if (!parent || sysfs_get_vpd(parent, pg, buff, VPD_BUFLEN) <= 0) {
 		condlog(3, "failed to read sysfs vpd pg%02x", pg);
 		return -EINVAL;
 	}
@@ -1330,8 +1369,10 @@ get_vpd_sysfs (struct udev_device *parent, int pg, char * str, int maxlen)
 		return -ENODATA;
 	}
 	buff_len = get_unaligned_be16(&buff[2]) + 4;
-	if (buff_len > 4096)
+	if (buff_len > VPD_BUFLEN) {
 		condlog(3, "vpd pg%02x page truncated", pg);
+		buff_len = VPD_BUFLEN;
+	}
 
 	if (pg == 0x80)
 		len = parse_vpd_pg80(buff, str, maxlen);
@@ -1375,7 +1416,7 @@ bool
 is_vpd_page_supported(int fd, int pg)
 {
 	int i, len;
-	unsigned char buff[4096];
+	unsigned char buff[VPD_BUFLEN];
 
 	len = fetch_vpd_page(fd, 0x00, buff, sizeof(buff));
 	if (len < 0)
@@ -1391,7 +1432,7 @@ int
 get_vpd_sgio (int fd, int pg, int vend_id, char * str, int maxlen)
 {
 	int len, buff_len;
-	unsigned char buff[4096];
+	unsigned char buff[VPD_BUFLEN];
 
 	buff_len = fetch_vpd_page(fd, pg, buff, sizeof(buff));
 	if (buff_len < 0)
Signature · 7 lines
2.33.1


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

Re: [dm-devel] [PATCH 05/21] libmultipath (coverity): improve input checking in parse_vpd_pg83

Quoted 11 lines · On Fri, Nov 19, 2021 at 12:13:22AM +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

Check offsets and other obvious errors in the VPD83 data.

The original reason to do this was to fix "tained scalar" warnings from coverity. But this doesn't suffice for coverity without using a constant boundary (WWID_SIZE) for "len" in parse_vpd_pg80() and for "vpd_len" in parse_vpd_pg83(), even though the computed boundaries are tighter than the constant ones.

This looks fine, but I do wonder if we are being too strict. I'm not sure we should be failing, if sg_inq wouldn't fail. The goal of the fallback code is to get the WWID the udev would have gotten, and being lenient with scsi devices that don't quite follow the standard is usually the best policy. Thoughts?

-Ben

Quoted 248 lines
---
 libmultipath/discovery.c | 139 +++++++++++++++++++++++++--------------
 1 file changed, 90 insertions(+), 49 deletions(-)
libmultipath/discovery.c +90 -49
index 5edf959..5ce9031 100644
@@ -37,6 +37,8 @@
 #include "print.h"
 #include "strbuf.h"
 
+#define VPD_BUFLEN 4096
+
 struct vpd_vendor_page vpd_vendor_pages[VPD_VP_ARRAY_SIZE] = {
 	[VPD_VP_UNDEF]	= { 0x00, "undef" },
 	[VPD_VP_HP3PAR]	= { 0xc0, "hp3par" },
@@ -1085,6 +1087,8 @@ parse_vpd_pg80(const unsigned char *in, char *out, size_t out_len)
 	if (out_len == 0)
 		return 0;
 
+	if (len > WWID_SIZE)
+		len = WWID_SIZE;
 	/*
 	 * Strip leading and trailing whitespace
 	 */
@@ -1114,84 +1118,122 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 	const unsigned char *d;
 	const unsigned char *vpd = NULL;
 	size_t len, vpd_len, i;
-	int vpd_type, prio = -1, naa_prio;
+	int vpd_type, prio = -1;
+	int err = -ENODATA;
 
-	d = in + 4;
-	while (d < in + in_len) {
-		/* Select 'association: LUN' */
-		if ((d[1] & 0x30) != 0) {
-			d += d[3] + 4;
-			continue;
-		}
-		switch (d[1] & 0xf) {
+        /* Need space at least for one digit */
+	if (out_len <= 1)
+		return 0;
+
+        d = in + 4;
+	while (d <= in + in_len - 4) {
+		bool invalid = false;
+		int new_prio = -1;
+
+                /* Select 'association: LUN' */
+		if ((d[1] & 0x30) == 0x30) {
+			invalid = true;
+			goto next_designator;
+		} else if ((d[1] & 0x30) != 0x00)
+			goto next_designator;
+
+                switch (d[1] & 0xf) {
+			unsigned char good_len;
 		case 0x3:
 			/* NAA: Prio 5 */
 			switch (d[4] >> 4) {
 			case 6:
 				/* IEEE Registered Extended: Prio 8 */
-				naa_prio = 8;
+				new_prio = 8;
+				good_len = 16;
 				break;
 			case 5:
 				/* IEEE Registered: Prio 7 */
-				naa_prio = 7;
+				new_prio = 7;
+				good_len = 8;
 				break;
 			case 2:
 				/* IEEE Extended: Prio 6 */
-				naa_prio = 6;
+				new_prio = 6;
+				good_len = 8;
 				break;
 			case 3:
 				/* IEEE Locally assigned: Prio 1 */
-				naa_prio = 1;
+				new_prio = 1;
+				good_len = 8;
 				break;
 			default:
 				/* Default: no priority */
-				naa_prio = -1;
+				good_len = 0xff;
 				break;
 			}
-			if (prio < naa_prio) {
-				prio = naa_prio;
-				vpd = d;
-			}
+
+                        invalid = good_len == 0xff || good_len != d[3];
 			break;
 		case 0x2:
 			/* EUI-64: Prio 4 */
-			if (prio < 4) {
-				prio = 4;
-				vpd = d;
-			}
+			invalid = (d[3] != 8 && d[3] != 12 && d[3] != 16);
+			new_prio = 4;
 			break;
 		case 0x8:
 			/* SCSI Name: Prio 3 */
-			if (memcmp(d + 4, "eui.", 4) &&
-			    memcmp(d + 4, "naa.", 4) &&
-			    memcmp(d + 4, "iqn.", 4))
-				break;
-			if (prio < 3) {
-				prio = 3;
-				vpd = d;
-			}
+			invalid = (d[3] < 4 || (memcmp(d + 4, "eui.", 4) &&
+						memcmp(d + 4, "naa.", 4) &&
+						memcmp(d + 4, "iqn.", 4)));
+			new_prio = 3;
 			break;
 		case 0x1:
 			/* T-10 Vendor ID: Prio 2 */
-			if (prio < 2) {
-				prio = 2;
-				vpd = d;
-			}
+			invalid = (d[3] < 8);
+			new_prio = 2;
 			break;
+		case 0xa:
+			condlog(2, "%s: UUID identifiers not yet supported",
+				__func__);
+			break;
+		default:
+			invalid = true;
+			break;
+		}
+
+	next_designator:
+		if (d + d[3] + 4 - in > (ssize_t)in_len) {
+			condlog(2, "%s: device descriptor length overflow: %zd > %zu",
+				__func__, d + d[3] + 4 - in, in_len);
+			err = -EOVERFLOW;
+			break;
+		}
+                if (invalid) {
+			condlog(2, "%s: invalid device designator at offset %zd: %02x%02x%02x%02x",
+				__func__, d - in, d[0], d[1], d[2], d[3]);
+			err = -EINVAL;
+			break;
+		}
+		if (new_prio > prio) {
+			vpd = d;
+			prio = new_prio;
 		}
 		d += d[3] + 4;
 	}
 
 	if (prio <= 0)
-		return -ENODATA;
-	/* Need space at least for one digit */
-	else if (out_len <= 1)
-		return 0;
+		return err;
+
+	if (d != in + in_len)
+		/* Should this be fatal? (overflow covered above) */
+		condlog(2, "%s: warning: last descriptor end %zd != VPD length %zu",
+			__func__, d - in, in_len);
 
 	len = 0;
 	vpd_type = vpd[1] & 0xf;
 	vpd_len = vpd[3];
 	vpd += 4;
+	/* untaint vpd_len for coverity */
+	if (vpd_len > WWID_SIZE) {
+		condlog(1, "%s: suspicious designator length %zu truncated to %u",
+			__func__, vpd_len, WWID_SIZE);
+		vpd_len = WWID_SIZE;
+	}
 	if (vpd_type == 0x2 || vpd_type == 0x3) {
 		size_t i;
 
@@ -1205,10 +1247,6 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 		for (i = 0; i < vpd_len; i++)
 			len += sprintf(out + len,
 				       "%02x", vpd[i]);
-	} else if (vpd_type == 0x8 && vpd_len < 4) {
-		condlog(1, "%s: VPD length %zu too small for designator type 8",
-			__func__, vpd_len);
-		return -EINVAL;
 	} else if (vpd_type == 0x8) {
 		if (!memcmp("eui.", vpd, 4))
 			out[0] =  '2';
@@ -1315,11 +1353,12 @@ parse_vpd_c0_hp3par(const unsigned char *in, size_t in_len,
 static int
 get_vpd_sysfs (struct udev_device *parent, int pg, char * str, int maxlen)
 {
-	int len, buff_len;
-	unsigned char buff[4096];
+	int len;
+	size_t buff_len;
+	unsigned char buff[VPD_BUFLEN];
 
-	memset(buff, 0x0, 4096);
-	if (!parent || sysfs_get_vpd(parent, pg, buff, 4096) <= 0) {
+	memset(buff, 0x0, VPD_BUFLEN);
+	if (!parent || sysfs_get_vpd(parent, pg, buff, VPD_BUFLEN) <= 0) {
 		condlog(3, "failed to read sysfs vpd pg%02x", pg);
 		return -EINVAL;
 	}
@@ -1330,8 +1369,10 @@ get_vpd_sysfs (struct udev_device *parent, int pg, char * str, int maxlen)
 		return -ENODATA;
 	}
 	buff_len = get_unaligned_be16(&buff[2]) + 4;
-	if (buff_len > 4096)
+	if (buff_len > VPD_BUFLEN) {
 		condlog(3, "vpd pg%02x page truncated", pg);
+		buff_len = VPD_BUFLEN;
+	}
 
 	if (pg == 0x80)
 		len = parse_vpd_pg80(buff, str, maxlen);
@@ -1375,7 +1416,7 @@ bool
 is_vpd_page_supported(int fd, int pg)
 {
 	int i, len;
-	unsigned char buff[4096];
+	unsigned char buff[VPD_BUFLEN];
 
 	len = fetch_vpd_page(fd, 0x00, buff, sizeof(buff));
 	if (len < 0)
@@ -1391,7 +1432,7 @@ int
 get_vpd_sgio (int fd, int pg, int vend_id, char * str, int maxlen)
 {
 	int len, buff_len;
-	unsigned char buff[4096];
+	unsigned char buff[VPD_BUFLEN];
 
 	buff_len = fetch_vpd_page(fd, pg, buff, sizeof(buff));
 	if (buff_len < 0)
Signature · 1 line
2.33.1

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

#7 lore raw

Re: [dm-devel] [PATCH 05/21] libmultipath (coverity): improve input checking in parse_vpd_pg83

Quoted 13 lines · Benjamin Marzinski
Quoted 11 lines · On Fri, Nov 19, 2021 at 12:13:22AM +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

Check offsets and other obvious errors in the VPD83 data.

The original reason to do this was to fix "tained scalar" warnings from coverity. But this doesn't suffice for coverity without using a constant boundary (WWID_SIZE) for "len" in parse_vpd_pg80() and for "vpd_len" in parse_vpd_pg83(), even though the computed boundaries are tighter than the constant ones.

Quoted 6 lines

This looks fine, but I do wonder if we are being too strict.  I'm not sure we should be failing, if sg_inq wouldn't fail.  The goal of the fallback code is to get the WWID the udev would have gotten, and being
lenient with scsi devices that don't quite follow the standard is usually the best policy. Thoughts?

If we encounter a broken VPD entry, IMO we can't continue reading further entries, because if the entry is broken, we can't trust the length value which is part of the entry. We may be reading total junk if we follow it.

We might simply discard the broken entry, stop iterating over the designators, see if we got a usable designator up to that point (i.e., previously), and if yes, use that entry, printing a big fat warning. That means we'd ultimately fail only if there was no usable designator before the broken one.

Would you prefer that strategy?

Martin

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

#8 lore raw

Re: [dm-devel] [PATCH 05/21] libmultipath (coverity): improve input checking in parse_vpd_pg83

Quoted 26 lines · Martin Wilck
Quoted 13 lines · Benjamin Marzinski
Quoted 11 lines · On Fri, Nov 19, 2021 at 12:13:22AM +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

Check offsets and other obvious errors in the VPD83 data.

The original reason to do this was to fix "tained scalar" warnings from coverity. But this doesn't suffice for coverity without using a constant boundary (WWID_SIZE) for "len" in parse_vpd_pg80() and for "vpd_len" in parse_vpd_pg83(), even though the computed boundaries are tighter than the constant ones.

Quoted 6 lines

This looks fine, but I do wonder if we are being too strict.  I'm not sure we should be failing, if sg_inq wouldn't fail.  The goal of the fallback code is to get the WWID the udev would have gotten, and being
lenient with scsi devices that don't quite follow the standard is usually the best policy. Thoughts?

If we encounter a broken VPD entry, IMO we can't continue reading further entries, because if the entry is broken, we can't trust the length value which is part of the entry. We may be reading total junk if we follow it.

Quoted 7 lines

We might simply discard the broken entry, stop iterating over the designators, see if we got a usable designator up to that point (i.e., previously), and if yes, use that entry, printing a big fat warning. That means we'd ultimately fail only if there was no usable designator before the broken one.

Would you prefer that strategy?

If you export scsi IDs from sg_inq, it will print them out until it hits an error, so I would prefer if we used the best designator we got before we hit the error, to match it.

-Ben

Quoted 2 lines

Martin

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

#9 lore raw

Re: [dm-devel] [PATCH 05/21] libmultipath (coverity): improve input checking in parse_vpd_pg83

Quoted 4 lines · Benjamin Marzinski

If you export scsi IDs from sg_inq, it will print them out until it hits an error, so I would prefer if we used the best designator we got before we hit the error, to match it.

That's what my code does (embarrassingly, I didn't realize that when I wrote my previous reply).

In shortened pseudo code:

	int prio = -1;
	int err = -ENODATA;

	d = first_designator();
	while (d <= in + in_len - 4) {
		bool invalid = false;
		int new_prio = -1;

                if (designator_is_invalid(d))
                     invalid = true;
                else if (lun_association(d))
                     new_prio = prio(d);

	next_designator:
		if (invalid) {
			err = -EINVAL;
			break;  /** see below **/
		}
		if (new_prio > prio) {
			vpd = d;
			prio = new_prio;
		}
		d = next_designator(d);
	}

        /* if we did find a valid designator, prio will be > 0, and we
           will not error out */
	if (prio <= 0)
		return err;

        convert_designator_to_wwid(d);

If you think we should use a different strategy, please explain. We *could* try to go on even after encountering broken designators, assuming the length field is correct if it's within the given limits, even if the rest is bogus. So baiscally instead of the break statement above, we'd continue the loop.

Would you prefer that?

Regards,
Martin

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

#10 lore raw

Re: [dm-devel] [PATCH 05/21] libmultipath (coverity): improve input checking in parse_vpd_pg83

Quoted 43 lines · Martin Wilck
Quoted 4 lines · Benjamin Marzinski

If you export scsi IDs from sg_inq, it will print them out until it hits an error, so I would prefer if we used the best designator we got before we hit the error, to match it.

That's what my code does (embarrassingly, I didn't realize that when I wrote my previous reply).

In shortened pseudo code:

	int prio = -1;
	int err = -ENODATA;

	d = first_designator();
	while (d <= in + in_len - 4) {
		bool invalid = false;
		int new_prio = -1;

                if (designator_is_invalid(d))
                     invalid = true;
                else if (lun_association(d))
                     new_prio = prio(d);

	next_designator:
		if (invalid) {
			err = -EINVAL;
			break;  /** see below **/
		}
		if (new_prio > prio) {
			vpd = d;
			prio = new_prio;
		}
		d = next_designator(d);
	}

        /* if we did find a valid designator, prio will be > 0, and we
           will not error out */
	if (prio <= 0)
		return err;

        convert_designator_to_wwid(d);
Quoted 7 lines

If you think we should use a different strategy, please explain. We *could* try to go on even after encountering broken designators, assuming the length field is correct if it's within the given limits, even if the rest is bogus. So baiscally instead of the break statement above, we'd continue the loop.

Would you prefer that?

Again looking at sg_inq, it will loop through all the descriptors, trusting the length field, until it gets to the whole data length. It prints a warning if it doesn't end up at exactly the data length, but still exports all the IDs it finds. If an individual descriptor doesn't make sense, it gets skipped. That would be my preference.

-Ben

Quoted 2 lines

Regards,
Martin

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

#11 lore raw

Re: [dm-devel] [PATCH 05/21] libmultipath (coverity): improve input checking in parse_vpd_pg83

Quoted 9 lines · Benjamin Marzinski
Quoted 9 lines

If you think we should use a different strategy, please explain. We *could* try to go on even after encountering broken designators, assuming the length field is correct if it's within the given limits,
even if the rest is bogus. So baiscally instead of the break statement
above, we'd continue the loop.

Quoted 8 lines
Quoted 1 line

Would you prefer that?

Again looking at sg_inq, it will loop through all the descriptors, trusting the length field, until it gets to the whole data length. It prints a warning if it doesn't end up at exactly the data length, but still exports all the IDs it finds.  If an individual descriptor doesn't
make sense, it gets skipped. That would be my preference.

Ok, I'll try to fix it up.

Martin

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

#12 lore raw

PATCH 6/21 multipath-tools: add tests for broken VPD page 83

In reply to [email protected]

From: Martin Wilck <[email protected]>

Add some tests for the consistency checks in the VPD.

---
 tests/vpd.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 108 insertions(+)
tests/vpd.c +108 -0
index 8e730d3..d42e109 100644
@@ -9,6 +9,7 @@
 #include <stdint.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <errno.h>
 #include <stdarg.h>
 #include <stddef.h>
 #include <setjmp.h>
@@ -500,6 +501,27 @@ static void test_vpd_naa_ ## naa ## _ ## wlen(void **state)             \
 			    test_id, vt->wwid);				\
 }
 
+/**
+ * test_cpd_naa_NAA_badlen_BAD() - test detection of bad length fields
+ * @NAA:	Network Name Authority (2, 3, 5, 16)
+ * @BAD:        Value for designator length field
+ * @ERR:        Expected error code
+ */
+#define make_test_vpd_naa_badlen(NAA, BAD, ERR)			\
+static void test_vpd_naa_##NAA##_badlen_##BAD(void **state)	\
+{								\
+	struct vpdtest *vt = *state;					\
+	int n, ret;							\
+									\
+	n = create_vpd83(vt->vpdbuf, sizeof(vt->vpdbuf), test_id, 3, NAA, 0); \
+									\
+	vt->vpdbuf[7] = BAD;						\
+	will_return(__wrap_ioctl, n);					\
+	will_return(__wrap_ioctl, vt->vpdbuf);				\
+	ret = get_vpd_sgio(10, 0x83, 0, vt->wwid, 40);			\
+	assert_int_equal(-ret, -ERR);					\
+}
+
 /**
  * test_vpd_eui_LEN_WLEN() - test code for VPD 83, EUI64
  * @LEN:	EUI64 length (8, 12, or 16)
@@ -532,6 +554,27 @@ static void test_vpd_eui_ ## len ## _ ## wlen ## _ ## sml(void **state)	\
 			    test_id, vt->wwid);				\
 }
 
+/**
+ * test_cpd_eui_LEN_badlen_BAD() - test detection of bad length fields
+ * @NAA:	correct length(8, 12, 16)
+ * @BAD:        value for designator length field
+ * @ERR:        expected error code
+ */
+#define make_test_vpd_eui_badlen(LEN, BAD, ERR)			\
+static void test_vpd_eui_badlen_##LEN##_##BAD(void **state)	\
+{								\
+	struct vpdtest *vt = *state;					\
+	int n, ret;							\
+									\
+	n = create_vpd83(vt->vpdbuf, sizeof(vt->vpdbuf), test_id, 2, 0, LEN); \
+									\
+	vt->vpdbuf[7] = BAD;						\
+	will_return(__wrap_ioctl, n);					\
+	will_return(__wrap_ioctl, vt->vpdbuf);				\
+	ret = get_vpd_sgio(10, 0x83, 0, vt->wwid, 40);			\
+	assert_int_equal(-ret, -ERR);					\
+}
+
 /**
  * test_vpd80_SIZE_LEN_WLEN() - test code for VPD 80
  * @SIZE, @LEN:	see create_vpd80()
@@ -621,6 +664,13 @@ make_test_vpd_eui(8, 17, 0);
 make_test_vpd_eui(8, 16, 0);
 make_test_vpd_eui(8, 10, 0);
 
+make_test_vpd_eui_badlen(8, 8, 17);
+make_test_vpd_eui_badlen(8, 12, -EOVERFLOW);
+make_test_vpd_eui_badlen(8, 9, -EOVERFLOW);
+make_test_vpd_eui_badlen(8, 7, -EINVAL);
+make_test_vpd_eui_badlen(8, 4, -EINVAL);
+make_test_vpd_eui_badlen(8, 0, -EINVAL);
+
 /* 96 bit, WWID size: 26 */
 make_test_vpd_eui(12, 32, 0);
 make_test_vpd_eui(12, 26, 0);
@@ -628,12 +678,27 @@ make_test_vpd_eui(12, 25, 0);
 make_test_vpd_eui(12, 20, 0);
 make_test_vpd_eui(12, 10, 0);
 
+make_test_vpd_eui_badlen(12, 12, 25);
+make_test_vpd_eui_badlen(12, 16, -EOVERFLOW);
+make_test_vpd_eui_badlen(12, 13, -EOVERFLOW);
+make_test_vpd_eui_badlen(12, 11, -EINVAL);
+/* non-fatal */
+make_test_vpd_eui_badlen(12, 8, 17);
+make_test_vpd_eui_badlen(12, 0, -EINVAL);
+
 /* 128 bit, WWID size: 34 */
 make_test_vpd_eui(16, 40, 0);
 make_test_vpd_eui(16, 34, 0);
 make_test_vpd_eui(16, 33, 0);
 make_test_vpd_eui(16, 20, 0);
 
+make_test_vpd_eui_badlen(16, 16, 33);
+make_test_vpd_eui_badlen(16, 17, -EOVERFLOW);
+make_test_vpd_eui_badlen(16, 15, -EINVAL);
+/* non-fatal */
+make_test_vpd_eui_badlen(16, 12, 25);
+make_test_vpd_eui_badlen(16, 0, -EINVAL);
+
 /* NAA IEEE registered extended (36), WWID size: 34 */
 make_test_vpd_naa(6, 40);
 make_test_vpd_naa(6, 34);
@@ -641,12 +706,27 @@ make_test_vpd_naa(6, 33);
 make_test_vpd_naa(6, 32);
 make_test_vpd_naa(6, 20);
 
+/* NAA IEEE registered extended with bad designator length */
+make_test_vpd_naa_badlen(6, 16, 33);
+make_test_vpd_naa_badlen(6, 15, -EINVAL);
+make_test_vpd_naa_badlen(6, 8, -EINVAL);
+make_test_vpd_naa_badlen(6, 17, -EOVERFLOW);
+make_test_vpd_naa_badlen(6, 0, -EINVAL);
+
 /* NAA IEEE registered (35), WWID size: 18 */
 make_test_vpd_naa(5, 20);
 make_test_vpd_naa(5, 18);
 make_test_vpd_naa(5, 17);
 make_test_vpd_naa(5, 16);
 
+/* NAA IEEE registered with bad designator length */
+make_test_vpd_naa_badlen(5, 8, 17);
+make_test_vpd_naa_badlen(5, 7, -EINVAL);
+make_test_vpd_naa_badlen(5, 4, -EINVAL);
+make_test_vpd_naa_badlen(5, 16, -EOVERFLOW);
+make_test_vpd_naa_badlen(5, 9, -EOVERFLOW);
+make_test_vpd_naa_badlen(5, 0, -EINVAL);
+
 /* NAA local (33), WWID size: 18 */
 make_test_vpd_naa(3, 20);
 make_test_vpd_naa(3, 18);
@@ -741,24 +821,52 @@ static int test_vpd(void)
 		cmocka_unit_test(test_vpd_eui_8_17_0),
 		cmocka_unit_test(test_vpd_eui_8_16_0),
 		cmocka_unit_test(test_vpd_eui_8_10_0),
+		cmocka_unit_test(test_vpd_eui_badlen_8_8),
+		cmocka_unit_test(test_vpd_eui_badlen_8_12),
+		cmocka_unit_test(test_vpd_eui_badlen_8_9),
+		cmocka_unit_test(test_vpd_eui_badlen_8_7),
+		cmocka_unit_test(test_vpd_eui_badlen_8_4),
+		cmocka_unit_test(test_vpd_eui_badlen_8_0),
 		cmocka_unit_test(test_vpd_eui_12_32_0),
 		cmocka_unit_test(test_vpd_eui_12_26_0),
 		cmocka_unit_test(test_vpd_eui_12_25_0),
 		cmocka_unit_test(test_vpd_eui_12_20_0),
 		cmocka_unit_test(test_vpd_eui_12_10_0),
+		cmocka_unit_test(test_vpd_eui_badlen_12_12),
+		cmocka_unit_test(test_vpd_eui_badlen_12_16),
+		cmocka_unit_test(test_vpd_eui_badlen_12_13),
+		cmocka_unit_test(test_vpd_eui_badlen_12_11),
+		cmocka_unit_test(test_vpd_eui_badlen_12_8),
+		cmocka_unit_test(test_vpd_eui_badlen_12_0),
 		cmocka_unit_test(test_vpd_eui_16_40_0),
 		cmocka_unit_test(test_vpd_eui_16_34_0),
 		cmocka_unit_test(test_vpd_eui_16_33_0),
 		cmocka_unit_test(test_vpd_eui_16_20_0),
+		cmocka_unit_test(test_vpd_eui_badlen_16_16),
+		cmocka_unit_test(test_vpd_eui_badlen_16_17),
+		cmocka_unit_test(test_vpd_eui_badlen_16_15),
+		cmocka_unit_test(test_vpd_eui_badlen_16_12),
+		cmocka_unit_test(test_vpd_eui_badlen_16_0),
 		cmocka_unit_test(test_vpd_naa_6_40),
 		cmocka_unit_test(test_vpd_naa_6_34),
 		cmocka_unit_test(test_vpd_naa_6_33),
 		cmocka_unit_test(test_vpd_naa_6_32),
 		cmocka_unit_test(test_vpd_naa_6_20),
+		cmocka_unit_test(test_vpd_naa_6_badlen_16),
+		cmocka_unit_test(test_vpd_naa_6_badlen_15),
+		cmocka_unit_test(test_vpd_naa_6_badlen_8),
+		cmocka_unit_test(test_vpd_naa_6_badlen_17),
+		cmocka_unit_test(test_vpd_naa_6_badlen_0),
 		cmocka_unit_test(test_vpd_naa_5_20),
 		cmocka_unit_test(test_vpd_naa_5_18),
 		cmocka_unit_test(test_vpd_naa_5_17),
 		cmocka_unit_test(test_vpd_naa_5_16),
+		cmocka_unit_test(test_vpd_naa_5_badlen_8),
+		cmocka_unit_test(test_vpd_naa_5_badlen_7),
+		cmocka_unit_test(test_vpd_naa_5_badlen_4),
+		cmocka_unit_test(test_vpd_naa_5_badlen_16),
+		cmocka_unit_test(test_vpd_naa_5_badlen_9),
+		cmocka_unit_test(test_vpd_naa_5_badlen_0),
 		cmocka_unit_test(test_vpd_naa_3_20),
 		cmocka_unit_test(test_vpd_naa_3_18),
 		cmocka_unit_test(test_vpd_naa_3_17),
Signature · 7 lines
2.33.1


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

PATCH 7/21 libmultipath: use strbuf in parse_vpd_pg83()

In reply to [email protected]

From: Martin Wilck <[email protected]>

By using the strbuf API, the code gets more readable, as we need less output size checks.

While at it, avoid a possible crash by passing a NULL pointer to memchr().

---
 libmultipath/discovery.c | 113 +++++++++++++++++----------------------
 1 file changed, 49 insertions(+), 64 deletions(-)
libmultipath/discovery.c +49 -64
index 5ce9031..16b6fae 100644
@@ -1120,6 +1120,7 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 	size_t len, vpd_len, i;
 	int vpd_type, prio = -1;
 	int err = -ENODATA;
+	STRBUF_ON_STACK(buf);
 
         /* Need space at least for one digit */
 	if (out_len <= 1)
@@ -1237,92 +1238,76 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 	if (vpd_type == 0x2 || vpd_type == 0x3) {
 		size_t i;
 
-		len = sprintf(out, "%d", vpd_type);
-		if (2 * vpd_len >= out_len - len) {
-			condlog(1, "%s: WWID overflow, type %d, %zu/%zu bytes required",
-				__func__, vpd_type,
-				2 * vpd_len + len + 1, out_len);
-			vpd_len = (out_len - len - 1) / 2;
-		}
+		if ((err = print_strbuf(&buf, "%d", vpd_type)) < 0)
+			return err;
 		for (i = 0; i < vpd_len; i++)
-			len += sprintf(out + len,
-				       "%02x", vpd[i]);
+			if ((err = print_strbuf(&buf, "%02x", vpd[i])) < 0)
+				return err;
 	} else if (vpd_type == 0x8) {
-		if (!memcmp("eui.", vpd, 4))
-			out[0] =  '2';
+		char type;
+
+                if (!memcmp("eui.", vpd, 4))
+			type =  '2';
 		else if (!memcmp("naa.", vpd, 4))
-			out[0] = '3';
+			type = '3';
 		else
-			out[0] = '8';
+			type = '8';
+		if ((err = fill_strbuf(&buf, type, 1)) < 0)
+			return err;
 
 		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, %zu/%zu bytes required",
-				__func__, out[0], len + 1, out_len);
-			len = out_len - 1;
+		if ((err = __append_strbuf_str(&buf, (const char *)vpd, len)) < 0)
+			return err;
+
+                /* The input is 0-padded, make sure the length is correct */
+		truncate_strbuf(&buf, strlen(get_strbuf_str(&buf)));
+		len = get_strbuf_len(&buf);
+		if (type != '8') {
+			char *buffer = __get_strbuf_buf(&buf);
+
+			for (i = 0; i < len; ++i)
+				buffer[i] = tolower(buffer[i]);
 		}
 
-		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;
 		size_t p_len;
 
-		out[0] = '1';
-		len = 1;
-		while ((p = memchr(vpd, ' ', vpd_len))) {
+		if ((err = fill_strbuf(&buf, '1', 1)) < 0)
+			return err;
+		while (vpd && (p = memchr(vpd, ' ', vpd_len))) {
 			p_len = p - vpd;
-			if (len + p_len > out_len - 1) {
-				condlog(1, "%s: WWID overflow, type 1, %zu/%zu 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) {
-				out[len] = '\0';
-				break;
-			}
-			out[len] = '_';
-			len ++;
-			if (len >= out_len - 1) {
-				out[len] = '\0';
-				break;
-			}
+			if ((err = __append_strbuf_str(&buf, (const char *)vpd,
+						       p_len)) < 0)
+				return err;
 			vpd = p;
 			vpd_len -= p_len;
-			while (vpd && *vpd == ' ') {
+			while (vpd && vpd_len > 0 && *vpd == ' ') {
 				vpd++;
 				vpd_len --;
 			}
+			if (vpd_len > 0 && (err = fill_strbuf(&buf, '_', 1)) < 0)
+				return err;
 		}
-		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, %zu/%zu 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--;
+		if (vpd_len > 0) {
+			if ((err = __append_strbuf_str(&buf, (const char *)vpd,
+						       vpd_len)) < 0)
+				return err;
 		}
 	}
+
+        len = get_strbuf_len(&buf);
+	if (len >= out_len) {
+		condlog(1, "%s: WWID overflow, type %d, %zu/%zu bytes required",
+			__func__, vpd_type, len, out_len);
+                if (vpd_type == 2 || vpd_type == 3)
+			/* designator must have an even number of characters */
+			len = 2 * (out_len / 2) - 1;
+		else
+			len = out_len - 1;
+	}
+	strlcpy(out, get_strbuf_str(&buf), len + 1);
 	return len;
 }
 
Signature · 7 lines
2.33.1


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

Re: [dm-devel] [PATCH 07/21] libmultipath: use strbuf in parse_vpd_pg83()

Quoted 90 lines · On Fri, Nov 19, 2021 at 12:13:24AM +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

By using the strbuf API, the code gets more readable, as we need less output size checks.

While at it, avoid a possible crash by passing a NULL pointer to memchr().

---
 libmultipath/discovery.c | 113 +++++++++++++++++----------------------
 1 file changed, 49 insertions(+), 64 deletions(-)
libmultipath/discovery.c +24 -29
index 5ce9031..16b6fae 100644
@@ -1120,6 +1120,7 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 	size_t len, vpd_len, i;
 	int vpd_type, prio = -1;
 	int err = -ENODATA;
+	STRBUF_ON_STACK(buf);
 
         /* Need space at least for one digit */
 	if (out_len <= 1)
@@ -1237,92 +1238,76 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 	if (vpd_type == 0x2 || vpd_type == 0x3) {
 		size_t i;
 
-		len = sprintf(out, "%d", vpd_type);
-		if (2 * vpd_len >= out_len - len) {
-			condlog(1, "%s: WWID overflow, type %d, %zu/%zu bytes required",
-				__func__, vpd_type,
-				2 * vpd_len + len + 1, out_len);
-			vpd_len = (out_len - len - 1) / 2;
-		}
+		if ((err = print_strbuf(&buf, "%d", vpd_type)) < 0)
+			return err;
 		for (i = 0; i < vpd_len; i++)
-			len += sprintf(out + len,
-				       "%02x", vpd[i]);
+			if ((err = print_strbuf(&buf, "%02x", vpd[i])) < 0)
+				return err;
 	} else if (vpd_type == 0x8) {
-		if (!memcmp("eui.", vpd, 4))
-			out[0] =  '2';
+		char type;
+
+                if (!memcmp("eui.", vpd, 4))
+			type =  '2';
 		else if (!memcmp("naa.", vpd, 4))
-			out[0] = '3';
+			type = '3';
 		else
-			out[0] = '8';
+			type = '8';
+		if ((err = fill_strbuf(&buf, type, 1)) < 0)
+			return err;
 
 		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, %zu/%zu bytes required",
-				__func__, out[0], len + 1, out_len);
-			len = out_len - 1;
+		if ((err = __append_strbuf_str(&buf, (const char *)vpd, len)) < 0)
+			return err;
+
+                /* The input is 0-padded, make sure the length is correct */
+		truncate_strbuf(&buf, strlen(get_strbuf_str(&buf)));
+		len = get_strbuf_len(&buf);
+		if (type != '8') {
+			char *buffer = __get_strbuf_buf(&buf);
+
+			for (i = 0; i < len; ++i)
+				buffer[i] = tolower(buffer[i]);
 		}
 
-		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';
-

libmultipath/discovery.c parse_vpd_pg83(const unsigned char *in, size_t in_len, · line 1279

Quoted 10 lines
libmultipath/discovery.c +3 -3
 	} else if (vpd_type == 0x1) {
 		const unsigned char *p;
 		size_t p_len;
 
-		out[0] = '1';
-		len = 1;
-		while ((p = memchr(vpd, ' ', vpd_len))) {
+		if ((err = fill_strbuf(&buf, '1', 1)) < 0)
+			return err;
+		while (vpd && (p = memchr(vpd, ' ', vpd_len))) {

vpd should never be zero here unless it wraps around, which seems very unlikely. Is this just to make coverity happy, or do you mean (*vpd)?

-Ben

Quoted 68 lines
libmultipath/discovery.c +22 -32
 			p_len = p - vpd;
-			if (len + p_len > out_len - 1) {
-				condlog(1, "%s: WWID overflow, type 1, %zu/%zu 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) {
-				out[len] = '\0';
-				break;
-			}
-			out[len] = '_';
-			len ++;
-			if (len >= out_len - 1) {
-				out[len] = '\0';
-				break;
-			}
+			if ((err = __append_strbuf_str(&buf, (const char *)vpd,
+						       p_len)) < 0)
+				return err;
 			vpd = p;
 			vpd_len -= p_len;
-			while (vpd && *vpd == ' ') {
+			while (vpd && vpd_len > 0 && *vpd == ' ') {
 				vpd++;
 				vpd_len --;
 			}
+			if (vpd_len > 0 && (err = fill_strbuf(&buf, '_', 1)) < 0)
+				return err;
 		}
-		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, %zu/%zu 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--;
+		if (vpd_len > 0) {
+			if ((err = __append_strbuf_str(&buf, (const char *)vpd,
+						       vpd_len)) < 0)
+				return err;
 		}
 	}
+
+        len = get_strbuf_len(&buf);
+	if (len >= out_len) {
+		condlog(1, "%s: WWID overflow, type %d, %zu/%zu bytes required",
+			__func__, vpd_type, len, out_len);
+                if (vpd_type == 2 || vpd_type == 3)
+			/* designator must have an even number of characters */
+			len = 2 * (out_len / 2) - 1;
+		else
+			len = out_len - 1;
+	}
+	strlcpy(out, get_strbuf_str(&buf), len + 1);
 	return len;
 }
 
Signature · 1 line
2.33.1

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

#15 lore raw

Re: [dm-devel] [PATCH 07/21] libmultipath: use strbuf in parse_vpd_pg83()

Quoted 109 lines · Benjamin Marzinski
Quoted 108 lines · On Fri, Nov 19, 2021 at 12:13:24AM +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

By using the strbuf API, the code gets more readable, as we need less output size checks.

While at it, avoid a possible crash by passing a NULL pointer to memchr().

---
 libmultipath/discovery.c | 113 +++++++++++++++++------------------
----

 1 file changed, 49 insertions(+), 64 deletions(-)
libmultipath/discovery.c +1 -0
index 5ce9031..16b6fae 100644
@@ -1120,6 +1120,7 @@ parse_vpd_pg83(const unsigned char *in,
 size_t in_len,
         size_t len, vpd_len, i;
         int vpd_type, prio = -1;
         int err = -ENODATA;
+       STRBUF_ON_STACK(buf);


         /* Need space at least for one digit */
        if (out_len <= 1)

libmultipath/discovery.c +0 -0
@@ -1237,92 +1238,76 @@ parse_vpd_pg83(const unsigned char *in,
 size_t in_len,
         if (vpd_type == 0x2 || vpd_type == 0x3) {
                 size_t i;


-               len = sprintf(out, "%d", vpd_type);
-               if (2 * vpd_len >= out_len - len) {
-                       condlog(1, "%s: WWID overflow, type %d, %zu/%zu bytes required",
-                               __func__, vpd_type,
-                               2 * vpd_len + len + 1, out_len); -                       vpd_len = (out_len - len - 1) / 2; -               }
+               if ((err = print_strbuf(&buf, "%d", vpd_type)) < 0) +                       return err;
                for (i = 0; i < vpd_len; i++)
-                       len += sprintf(out + len,
-                                      "%02x", vpd[i]); +                       if ((err = print_strbuf(&buf, "%02x", vpd[i])) < 0)
+                               return err;
        } else if (vpd_type == 0x8) {
-               if (!memcmp("eui.", vpd, 4))
-                       out[0] =  '2';
+               char type;
+
+                if (!memcmp("eui.", vpd, 4))
+                       type =  '2';
                else if (!memcmp("naa.", vpd, 4))
-                       out[0] = '3';
+                       type = '3';
                else
-                       out[0] = '8';
+                       type = '8';
+               if ((err = fill_strbuf(&buf, type, 1)) < 0) +                       return err;

                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, %zu/%zu bytes required",
-                               __func__, out[0], len + 1, out_len);
-                       len = out_len - 1;
+               if ((err = __append_strbuf_str(&buf, (const char *)vpd, len)) < 0)
+                       return err;
+
+                /* The input is 0-padded, make sure the length is correct */
+               truncate_strbuf(&buf, strlen(get_strbuf_str(&buf)));
+               len = get_strbuf_len(&buf);
+               if (type != '8') {
+                       char *buffer = __get_strbuf_buf(&buf); +
+                       for (i = 0; i < len; ++i)
+                               buffer[i] = tolower(buffer[i]); }

-               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;
                size_t p_len;

-               out[0] = '1';
-               len = 1;
-               while ((p = memchr(vpd, ' ', vpd_len))) {

Quoted 9 lines
Quoted 3 lines

+               if ((err = fill_strbuf(&buf, '1', 1)) < 0) +                       return err;
+               while (vpd && (p = memchr(vpd, ' ', vpd_len))) {

vpd should never be zero here unless it wraps around, which seems very
unlikely.  Is this just to make coverity happy, or do you mean (*vpd)?

Coverity likes arguments for memchr() to be NULL-checked beforehand, but that wasn't the main reason.

See below, there's "vpd = p". And p can (and will sooner or later) be NULL, because memchr() returns NULL if it doesn't find anything.

Quoted 27 lines

-Ben

Quoted 25 lines

                        p_len = p - vpd;
-                       if (len + p_len > out_len - 1) { -                               condlog(1, "%s: WWID overflow, type 1, %zu/%zu 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) {
-                               out[len] = '\0';
-                               break;
-                       }
-                       out[len] = '_';
-                       len ++;
-                       if (len >= out_len - 1) {
-                               out[len] = '\0';
-                               break;
-                       }
+                       if ((err = __append_strbuf_str(&buf, (const char *)vpd,
+                                                      p_len)) < 0) +                               return err;
                        vpd = p;

Here.

Regards,
Martin

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

#16 lore raw

Re: [dm-devel] [PATCH 07/21] libmultipath: use strbuf in parse_vpd_pg83()

Quoted 15 lines · Martin Wilck
Quoted 8 lines · Benjamin Marzinski
Quoted 6 lines · On Fri, Nov 19, 2021 at 12:13:24AM +0100, [email protected] wrote:

-               out[0] = '1';
-               len = 1;
-               while ((p = memchr(vpd, ' ', vpd_len))) { +               if ((err = fill_strbuf(&buf, '1', 1)) < 0) +                       return err;
+               while (vpd && (p = memchr(vpd, ' ', vpd_len))) {

Quoted 5 lines

vpd should never be zero here unless it wraps around, which seems very
unlikely.  Is this just to make coverity happy, or do you mean (*vpd)?

Quoted 6 lines

Coverity likes arguments for memchr() to be NULL-checked beforehand, but that wasn't the main reason.

See below, there's "vpd = p". And p can (and will sooner or later) be NULL, because memchr() returns NULL if it doesn't find anything.

Right, but don't you only execute this while loop if p isn't NULL? Not that it really matters. I'm fine with the code that way it is.

-Ben

Quoted 29 lines
Quoted 28 lines

-Ben

Quoted 25 lines

                        p_len = p - vpd;
-                       if (len + p_len > out_len - 1) { -                               condlog(1, "%s: WWID overflow, type 1, %zu/%zu 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) {
-                               out[len] = '\0';
-                               break;
-                       }
-                       out[len] = '_';
-                       len ++;
-                       if (len >= out_len - 1) {
-                               out[len] = '\0';
-                               break;
-                       }
+                       if ((err = __append_strbuf_str(&buf, (const char *)vpd,
+                                                      p_len)) < 0) +                               return err;
                        vpd = p;

Quoted 5 lines

Here.

Regards,
Martin

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

#17 lore raw

Re: [dm-devel] [PATCH 07/21] libmultipath: use strbuf in parse_vpd_pg83()

Quoted 21 lines · Benjamin Marzinski
Quoted 20 lines · Martin Wilck
Quoted 9 lines · Benjamin Marzinski
Quoted 7 lines · On Fri, Nov 19, 2021 at 12:13:24AM +0100, [email protected] wrote:

-               out[0] = '1';
-               len = 1;
-               while ((p = memchr(vpd, ' ', vpd_len))) { +               if ((err = fill_strbuf(&buf, '1', 1)) < 0) +                       return err;
+               while (vpd && (p = memchr(vpd, ' ', vpd_len))) {

Quoted 5 lines

vpd should never be zero here unless it wraps around, which seems very
unlikely.  Is this just to make coverity happy, or do you mean (*vpd)?

Coverity likes arguments for memchr() to be NULL-checked beforehand,
but that wasn't the main reason.

Quoted 8 lines
Quoted 4 lines

See below, there's "vpd = p". And p can (and will sooner or later) be
NULL, because memchr() returns NULL if it doesn't find anything.

Right, but don't you only execute this while loop if p isn't NULL? Not
that it really matters. I'm fine with the code that way it is.

Ah, I misinterpreted your comment. This one was for coverity, IIRC; to make it believe that it's legal to take *vpd.

Martin

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

#18 lore raw

PATCH 8/21 libmultipath (coverity): fix tainted values in alua_rtpg.c

In reply to [email protected]

From: Martin Wilck <[email protected]>

Coverity needs tainted values limited by constant expressions.

---
 libmultipath/prioritizers/alua_rtpg.c | 13 ++++++------
 libmultipath/prioritizers/alua_spc3.h | 30 ++++++++++++++++++++++-----
 2 files changed, 31 insertions(+), 12 deletions(-)
libmultipath/prioritizers/alua_rtpg.c +6 -7
index 420a2e3..3f9c0e7 100644
@@ -27,7 +27,6 @@
 #include "../structs.h"
 #include "../prio.h"
 #include "../discovery.h"
-#include "../unaligned.h"
 #include "../debug.h"
 #include "alua_rtpg.h"
 
@@ -252,12 +251,12 @@ int
 get_target_port_group(const struct path * pp, unsigned int timeout)
 {
 	unsigned char		*buf;
-	struct vpd83_data *	vpd83;
-	struct vpd83_dscr *	dscr;
+	const struct vpd83_data *	vpd83;
+	const struct vpd83_dscr *	dscr;
 	int			rc;
 	int			buflen, scsi_buflen;
 
-	buflen = 4096;
+	buflen = VPD_BUFLEN;
 	buf = (unsigned char *)malloc(buflen);
 	if (!buf) {
 		PRINT_DEBUG("malloc failed: could not allocate"
@@ -298,13 +297,13 @@ get_target_port_group(const struct path * pp, unsigned int timeout)
 	rc = -RTPG_NO_TPG_IDENTIFIER;
 	FOR_EACH_VPD83_DSCR(vpd83, dscr) {
 		if (vpd83_dscr_istype(dscr, IDTYPE_TARGET_PORT_GROUP)) {
-			struct vpd83_tpg_dscr *p;
+			const struct vpd83_tpg_dscr *p;
 			if (rc != -RTPG_NO_TPG_IDENTIFIER) {
 				PRINT_DEBUG("get_target_port_group: more "
 					    "than one TPG identifier found!");
 				continue;
 			}
-			p  = (struct vpd83_tpg_dscr *)dscr->data;
+			p  = (const struct vpd83_tpg_dscr *)dscr->data;
 			rc = get_unaligned_be16(p->tpg);
 		}
 	}
@@ -377,7 +376,7 @@ get_asymmetric_access_state(const struct path *pp, unsigned int tpg,
 	uint64_t		scsi_buflen;
 	int fd = pp->fd;
 
-	buflen = 4096;
+	buflen = VPD_BUFLEN;
 	buf = (unsigned char *)malloc(buflen);
 	if (!buf) {
 		PRINT_DEBUG ("malloc failed: could not allocate"
libmultipath/prioritizers/alua_spc3.h +25 -5
index 7ba2cf4..611d67e 100644
@@ -14,6 +14,7 @@
  */
 #ifndef __SPC3_H__
 #define __SPC3_H__
+#include "../unaligned.h"
 
 /*=============================================================================
  * Definitions to support the standard inquiry command as defined in SPC-3.
@@ -177,7 +178,7 @@ struct vpd83_dscr {
 } __attribute__((packed));
 
 static inline int
-vpd83_dscr_istype(struct vpd83_dscr *d, unsigned char type)
+vpd83_dscr_istype(const struct vpd83_dscr *d, unsigned char type)
 {
 	return ((d->b1 & 7) == type);
 }
@@ -190,6 +191,26 @@ struct vpd83_data {
 	struct vpd83_dscr	data[0];
 } __attribute__((packed));
 
+#define VPD_BUFLEN 4096
+
+static inline unsigned int vpd83_length(const struct vpd83_data *p)
+{
+	uint16_t len = get_unaligned_be16(p->length);
+
+	return len <= VPD_BUFLEN ? len : VPD_BUFLEN;
+}
+
+static inline const struct vpd83_dscr *
+vpd83_next_dscr(const struct vpd83_dscr *d, const struct vpd83_data *p)
+{
+	uint16_t pg_len = vpd83_length(p);
+	ptrdiff_t offs = ((const char *)d - (const char *)p) + d->length + 4;
+
+	offs = offs < 0 ? 0 : offs > pg_len ? pg_len : offs;
+	return (const struct vpd83_dscr *)((const char *)p + offs);
+}
+
+
 /*-----------------------------------------------------------------------------
  * This macro should be used to walk through all identification descriptors
  * defined in the code page 0x83.
@@ -200,10 +221,9 @@ struct vpd83_data {
 #define FOR_EACH_VPD83_DSCR(p, d) \
 		for( \
 			d = p->data; \
-			(((char *) d) - ((char *) p)) < \
-			get_unaligned_be16(p->length); \
-			d = (struct vpd83_dscr *) \
-				((char *) d + d->length + 4) \
+			(((const char *) d) - ((const char *) p)) \
+				< (ptrdiff_t)vpd83_length(p);	  \
+			d = vpd83_next_dscr(d, p) \
 		)
 
 /*=============================================================================
Signature · 7 lines
2.33.1


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

Re: [dm-devel] [PATCH 08/21] libmultipath (coverity): fix tainted values in alua_rtpg.c

Quoted 85 lines · On Fri, Nov 19, 2021 at 12:13:25AM +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

Coverity needs tainted values limited by constant expressions.

---
 libmultipath/prioritizers/alua_rtpg.c | 13 ++++++------
 libmultipath/prioritizers/alua_spc3.h | 30 ++++++++++++++++++++++-----
 2 files changed, 31 insertions(+), 12 deletions(-)
libmultipath/prioritizers/alua_rtpg.c +6 -7
index 420a2e3..3f9c0e7 100644
@@ -27,7 +27,6 @@
 #include "../structs.h"
 #include "../prio.h"
 #include "../discovery.h"
-#include "../unaligned.h"
 #include "../debug.h"
 #include "alua_rtpg.h"
 
@@ -252,12 +251,12 @@ int
 get_target_port_group(const struct path * pp, unsigned int timeout)
 {
 	unsigned char		*buf;
-	struct vpd83_data *	vpd83;
-	struct vpd83_dscr *	dscr;
+	const struct vpd83_data *	vpd83;
+	const struct vpd83_dscr *	dscr;
 	int			rc;
 	int			buflen, scsi_buflen;
 
-	buflen = 4096;
+	buflen = VPD_BUFLEN;
 	buf = (unsigned char *)malloc(buflen);
 	if (!buf) {
 		PRINT_DEBUG("malloc failed: could not allocate"
@@ -298,13 +297,13 @@ get_target_port_group(const struct path * pp, unsigned int timeout)
 	rc = -RTPG_NO_TPG_IDENTIFIER;
 	FOR_EACH_VPD83_DSCR(vpd83, dscr) {
 		if (vpd83_dscr_istype(dscr, IDTYPE_TARGET_PORT_GROUP)) {
-			struct vpd83_tpg_dscr *p;
+			const struct vpd83_tpg_dscr *p;
 			if (rc != -RTPG_NO_TPG_IDENTIFIER) {
 				PRINT_DEBUG("get_target_port_group: more "
 					    "than one TPG identifier found!");
 				continue;
 			}
-			p  = (struct vpd83_tpg_dscr *)dscr->data;
+			p  = (const struct vpd83_tpg_dscr *)dscr->data;
 			rc = get_unaligned_be16(p->tpg);
 		}
 	}
@@ -377,7 +376,7 @@ get_asymmetric_access_state(const struct path *pp, unsigned int tpg,
 	uint64_t		scsi_buflen;
 	int fd = pp->fd;
 
-	buflen = 4096;
+	buflen = VPD_BUFLEN;
 	buf = (unsigned char *)malloc(buflen);
 	if (!buf) {
 		PRINT_DEBUG ("malloc failed: could not allocate"
libmultipath/prioritizers/alua_spc3.h +2 -1
index 7ba2cf4..611d67e 100644
@@ -14,6 +14,7 @@
  */
 #ifndef __SPC3_H__
 #define __SPC3_H__
+#include "../unaligned.h"
 
 /*=============================================================================
  * Definitions to support the standard inquiry command as defined in SPC-3.
@@ -177,7 +178,7 @@ struct vpd83_dscr {
 } __attribute__((packed));
 
 static inline int
-vpd83_dscr_istype(struct vpd83_dscr *d, unsigned char type)
+vpd83_dscr_istype(const struct vpd83_dscr *d, unsigned char type)
 {
 	return ((d->b1 & 7) == type);
 }

libmultipath/prioritizers/alua_spc3.h struct vpd83_data { · line 200

Quoted 10 lines
libmultipath/prioritizers/alua_spc3.h +7 -0
@@ -190,6 +191,26 @@ struct vpd83_data {
 	struct vpd83_dscr	data[0];
 } __attribute__((packed));
 
+#define VPD_BUFLEN 4096
+
+static inline unsigned int vpd83_length(const struct vpd83_data *p)
+{
+	uint16_t len = get_unaligned_be16(p->length);
+
+	return len <= VPD_BUFLEN ? len : VPD_BUFLEN;

Isn't p->length the length of the page data after the header? So you either need to add 4 to len, or use (VPD_BUFLEN - 4)

Quoted 17 lines
libmultipath/prioritizers/alua_spc3.h +13 -0
+}
+
+static inline const struct vpd83_dscr *
+vpd83_next_dscr(const struct vpd83_dscr *d, const struct vpd83_data *p)
+{
+	uint16_t pg_len = vpd83_length(p);
+	ptrdiff_t offs = ((const char *)d - (const char *)p) + d->length + 4;
+
+	offs = offs < 0 ? 0 : offs > pg_len ? pg_len : offs;
+	return (const struct vpd83_dscr *)((const char *)p + offs);
+}
+
+
 /*-----------------------------------------------------------------------------
  * This macro should be used to walk through all identification descriptors
  * defined in the code page 0x83.

libmultipath/prioritizers/alua_spc3.h struct vpd83_data { · line 224

Quoted 8 lines
libmultipath/prioritizers/alua_spc3.h +1 -4
@@ -200,10 +221,9 @@ struct vpd83_data {
 #define FOR_EACH_VPD83_DSCR(p, d) \
 		for( \
 			d = p->data; \
-			(((char *) d) - ((char *) p)) < \
-			get_unaligned_be16(p->length); \
-			d = (struct vpd83_dscr *) \
-				((char *) d + d->length + 4) \
+			(((const char *) d) - ((const char *) p)) \

Unless you are adding 4 in vpd83_length(), shouldn't this be:

			(((const char *) d) - ((const char *) p->data)) \
Quoted 7 lines
libmultipath/prioritizers/alua_spc3.h +2 -0
+				< (ptrdiff_t)vpd83_length(p);	  \
+			d = vpd83_next_dscr(d, p) \
 		)
 
 /*=============================================================================
Signature · 1 line
2.33.1

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

#20 lore raw

PATCH 9/21 multipath, multipathd: exit if bindings file is broken

In reply to [email protected]

From: Martin Wilck <[email protected]>

If check_alias_settings() returns error, the bindings file is inconsistent and proceeding is potentially dangerous. Abort.

Found by coverity.

---
 multipath/main.c  |  5 ++++-
 multipathd/main.c | 15 ++++++++++++---
 2 files changed, 16 insertions(+), 4 deletions(-)
multipath/main.c +4 -1
index d6291f7..9625e97 100644
@@ -962,7 +962,10 @@ main (int argc, char *argv[])
 		exit(RTVL_FAIL);
 	}
 
-	check_alias_settings(conf);
+	if (check_alias_settings(conf)) {
+		fprintf(stderr, "fatal configuration error, aborting");
+		exit(RTVL_FAIL);
+	}
 
 	if (optind < argc) {
 		dev = MALLOC(FILE_NAME_SIZE);
multipathd/main.c +12 -3
index bffafe9..958d131 100644
@@ -2820,7 +2820,9 @@ reconfigure (struct vectors * vecs)
 	reset_checker_classes();
 	if (bindings_read_only)
 		conf->bindings_read_only = bindings_read_only;
-	check_alias_settings(conf);
+
+	if (check_alias_settings(conf))
+		return 1;
 
 	uxsock_timeout = conf->uxsock_timeout;
 
@@ -3336,15 +3338,22 @@ child (__attribute__((unused)) void *param)
 		if (state == DAEMON_SHUTDOWN)
 			break;
 		if (state == DAEMON_CONFIGURE) {
+			int rc = 0;
+
 			pthread_cleanup_push(cleanup_lock, &vecs->lock);
 			lock(&vecs->lock);
 			pthread_testcancel();
 			if (!need_to_delay_reconfig(vecs))
-				reconfigure(vecs);
+				rc = reconfigure(vecs);
 			else
 				enable_delayed_reconfig();
 			lock_cleanup_pop(vecs->lock);
-			post_config_state(DAEMON_IDLE);
+			if (!rc)
+				post_config_state(DAEMON_IDLE);
+			else {
+				condlog(0, "fatal error applying configuration - aborting");
+				exit_daemon();
+			}
 		}
 	}
 
Signature · 7 lines
2.33.1


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

PATCH 10/21 libmultipath (coverity): silence unchecked return value warning

In reply to [email protected]

From: Martin Wilck <[email protected]>

path_get_tpgs() returns pp->tpgs, which we do check.

---
 libmultipath/propsel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libmultipath/propsel.c +1 -1
index b287667..2984a5f 100644
@@ -525,7 +525,7 @@ int select_checker(struct config *conf, struct path *pp)
 			ckr_name = RDAC;
 			goto out;
 		}
-		path_get_tpgs(pp);
+		(void)path_get_tpgs(pp);
 		if (pp->tpgs != TPGS_NONE && pp->tpgs != TPGS_UNDEF) {
 			ckr_name = TUR;
 			goto out;
Signature · 7 lines
2.33.1


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

PATCH 11/21 multipath: remove pointless code from getopt processing

In reply to [email protected]

From: Martin Wilck <[email protected]>

getopt() never returns 1, and sizeof(optarg) is always sizeof(char *).

---
 multipath/main.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
multipath/main.c +1 -4
index 9625e97..b7986de 100644
@@ -842,11 +842,8 @@ main (int argc, char *argv[])
 		condlog(1, "failed to register cleanup handler for vecs: %m");
 	while ((arg = getopt(argc, argv, ":adDcChl::eFfM:v:p:b:BrR:itTquUwW")) != EOF ) {
 		switch(arg) {
-		case 1: printf("optarg : %s\n",optarg);
-			break;
 		case 'v':
-			if (sizeof(optarg) > sizeof(char *) ||
-			    !isdigit(optarg[0])) {
+			if (!isdigit(optarg[0])) {
 				usage (argv[0]);
 				exit(RTVL_FAIL);
 			}
Signature · 7 lines
2.33.1


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

PATCH 12/21 libmultipath (coverity): set umask before mkstemp

In reply to [email protected]

From: Martin Wilck <[email protected]>

Coverity SECURE_TEMP
(https://scan4.coverity.com/doc/en/cov_checker_ref.html#static_checker_SECURE_TEMP).
multipathd sets this umask anyway, but multipath doesn't.

---
 libmultipath/alias.c | 4 ++++
 1 file changed, 4 insertions(+)
libmultipath/alias.c +4 -0
index ad7e512..2640a60 100644
@@ -578,13 +578,17 @@ static int fix_bindings_file(const struct config *conf,
 	int rc;
 	long fd;
 	char tempname[PATH_MAX];
+	mode_t old_umask;
 
 	if (safe_sprintf(tempname, "%s.XXXXXX", conf->bindings_file))
 		return -1;
+	/* coverity: SECURE_TEMP */
+	old_umask = umask(0077);
 	if ((fd = mkstemp(tempname)) == -1) {
 		condlog(1, "%s: mkstemp: %m", __func__);
 		return -1;
 	}
+	umask(old_umask);
 	pthread_cleanup_push(close_fd, (void*)fd);
 	rc = write_bindings_file(bindings, fd);
 	pthread_cleanup_pop(1);
Signature · 7 lines
2.33.1


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

PATCH 13/21 multipathd (coverity): simplify set_oom_adj()

In reply to [email protected]

From: Martin Wilck <[email protected]>

Coverity discourages stat-before-open. So simplify this function.

---
 multipathd/main.c | 53 ++++++++++++++---------------------------------
 1 file changed, 15 insertions(+), 38 deletions(-)
multipathd/main.c +15 -38
index 958d131..b8d4fcf 100644
@@ -2953,51 +2953,28 @@ setscheduler (void)
 	return;
 }
 
-static void
-set_oom_adj (void)
+static void set_oom_adj(void)
 {
-#ifdef OOM_SCORE_ADJ_MIN
-	int retry = 1;
-	char *file = "/proc/self/oom_score_adj";
-	int score = OOM_SCORE_ADJ_MIN;
-#else
-	int retry = 0;
-	char *file = "/proc/self/oom_adj";
-	int score = OOM_ADJUST_MIN;
-#endif
 	FILE *fp;
-	struct stat st;
-	char *envp;
 
-	envp = getenv("OOMScoreAdjust");
-	if (envp) {
+	if (getenv("OOMScoreAdjust")) {
 		condlog(3, "Using systemd provided OOMScoreAdjust");
 		return;
 	}
-	do {
-		if (stat(file, &st) == 0){
-			fp = fopen(file, "w");
-			if (!fp) {
-				condlog(0, "couldn't fopen %s : %s", file,
-					strerror(errno));
-				return;
-			}
-			fprintf(fp, "%i", score);
-			fclose(fp);
-			return;
-		}
-		if (errno != ENOENT) {
-			condlog(0, "couldn't stat %s : %s", file,
-				strerror(errno));
-			return;
-		}
-#ifdef OOM_ADJUST_MIN
-		file = "/proc/self/oom_adj";
-		score = OOM_ADJUST_MIN;
-#else
-		retry = 0;
+#ifdef OOM_SCORE_ADJ_MIN
+	fp = fopen("/proc/self/oom_score_adj", "w");
+	if (fp) {
+		fprintf(fp, "%i", OOM_SCORE_ADJ_MIN);
+		fclose(fp);
+		return;
+	}
 #endif
-	} while (retry--);
+	fp = fopen("/proc/self/oom_adj", "w");
+	if (fp) {
+		fprintf(fp, "%i", OOM_ADJUST_MIN);
+		fclose(fp);
+		return;
+	}
 	condlog(0, "couldn't adjust oom score");
 }
 
Signature · 7 lines
2.33.1


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

PATCH 14/21 kpartx: open /dev/loop-control only once

In reply to [email protected]

From: Martin Wilck <[email protected]>

Opening the same file repeatedly in a loop seems wrong.

For unknown reason, this patch caused gcc to diagnose a possible buffer overflow for the device name, and I had to increase the buffer by one byte.

---
 kpartx/lopart.c | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)
kpartx/lopart.c +17 -12
index 9b65255..2661940 100644
@@ -159,26 +159,28 @@ char *find_loop_by_file(const char *filename)
 
 char *find_unused_loop_device(void)
 {
-	char dev[20], *next_loop_dev = NULL;
+	char dev[21], *next_loop_dev = NULL;
 	int fd, next_loop = 0, somedev = 0, someloop = 0, loop_known = 0;
+	int next_loop_fd;
 	struct stat statbuf;
 	struct loop_info loopinfo;
 	FILE *procdev;
 
+	next_loop_fd = open("/dev/loop-control", O_RDWR);
+	if (next_loop_fd < 0)
+		return NULL;
+
+	if (!(fstat(next_loop_fd, &statbuf) == 0 && S_ISCHR(statbuf.st_mode))) {
+		close(next_loop_fd);
+		return NULL;
+	}
+
 	while (next_loop_dev == NULL) {
-		if (stat("/dev/loop-control", &statbuf) == 0 &&
-		    S_ISCHR(statbuf.st_mode)) {
-			int next_loop_fd;
-
-			next_loop_fd = open("/dev/loop-control", O_RDWR);
-			if (next_loop_fd < 0)
-				return NULL;
-			next_loop = ioctl(next_loop_fd, LOOP_CTL_GET_FREE);
+		next_loop = ioctl(next_loop_fd, LOOP_CTL_GET_FREE);
+		if (next_loop < 0) {
 			close(next_loop_fd);
-			if (next_loop < 0)
-				return NULL;
+			return NULL;
 		}
-
 		sprintf(dev, "/dev/loop%d", next_loop);
 
 		fd = open (dev, O_RDONLY);
@@ -199,6 +201,9 @@ char *find_unused_loop_device(void)
 		}
 		break;
 	}
+
+	close(next_loop_fd);
+
 	if (next_loop_dev)
 		return next_loop_dev;
 
Signature · 7 lines
2.33.1


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

PATCH 15/21 kpartx: use opened loop device immediately

In reply to [email protected]

From: Martin Wilck <[email protected]>

The code in find_unused_loop_device() goes through circles to get an unused device, but it takes no care not to race with a different process opening the same loop device. Use the once-opened loop device for setup immediately instead of closing and re-opening it.

While at it, simplify the code somewhat.

---
 kpartx/kpartx.c |  4 +--
 kpartx/lopart.c | 83 ++++++++++++++++++++-----------------------------
 kpartx/lopart.h |  3 +-
 3 files changed, 35 insertions(+), 55 deletions(-)
kpartx/kpartx.c +1 -3
index 7bc6454..3c49999 100644
@@ -359,9 +359,7 @@ main(int argc, char **argv){
 			exit (0);
 
 		if (!loopdev) {
-			loopdev = find_unused_loop_device();
-
-			if (set_loop(loopdev, rpath, 0, &ro)) {
+			if (set_loop(&loopdev, rpath, 0, &ro)) {
 				fprintf(stderr, "can't set up loop\n");
 				exit (1);
 			}
kpartx/lopart.c +33 -50

152 lines not on this page · raw message

kpartx/lopart.h +1 -2
index d3bad10..c73ab23 100644
@@ -1,5 +1,4 @@
 extern int verbose;
-extern int set_loop (const char *, const char *, int, int *);
+extern int set_loop (char **, const char *, int, int *);
 extern int del_loop (const char *);
-extern char * find_unused_loop_device (void);
 extern char * find_loop_by_file (const char *);
Signature · 7 lines
2.33.1


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

PATCH 16/21 kpartx: find_unused_loop_device(): add newlines

In reply to [email protected]

From: Martin Wilck <[email protected]>

... to avoid these messages being joined with the error message from the caller.

---
 kpartx/lopart.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
kpartx/lopart.c +5 -5
index 7041ddf..512a59f 100644
@@ -210,26 +210,26 @@ no_loop_fd:
 	}
 
 	if (!somedev)
-		fprintf(stderr, "mount: could not find any device /dev/loop#");
+		fprintf(stderr, "mount: could not find any device /dev/loop#\n");
 
 	else if (!someloop) {
 		if (loop_known == 1)
 			fprintf(stderr,
 				"mount: Could not find any loop device.\n"
-				"       Maybe /dev/loop# has a wrong major number?");
+				"       Maybe /dev/loop# has a wrong major number?\n");
 		else if (loop_known == -1)
 			fprintf(stderr,
 				"mount: Could not find any loop device, and, according to %s,\n"
 				"       this kernel does not know about the loop device.\n"
-				"       (If so, then recompile or `modprobe loop'.)",
+				"       (If so, then recompile or `modprobe loop'.)\n",
 				PROC_DEVICES);
 		else
 			fprintf(stderr,
 				"mount: Could not find any loop device. Maybe this kernel does not know\n"
 				"       about the loop device (then recompile or `modprobe loop'), or\n"
-				"       maybe /dev/loop# has the wrong major number?");
+				"       maybe /dev/loop# has the wrong major number?\n");
 	} else
-		fprintf(stderr, "mount: could not find any free loop device");
+		fprintf(stderr, "mount: could not find any free loop device\n");
 	return NULL;
 }
 
Signature · 7 lines
2.33.1


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

PATCH 17/21 multipathd (coverity): daemonize(): use dup2

In reply to [email protected]

From: Martin Wilck <[email protected]>

Modify the file descriptors atomically usign dup2(), and make sure to cleanup properly even in case of an error, and to not close stdout/in/err if the program had been started with any of them closed.

---
 multipathd/main.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)
multipathd/main.c +13 -10
index b8d4fcf..8bd680d 100644
@@ -3341,11 +3341,18 @@ failed:
 	return sd_notify_exit(exit_code);
 }
 
+static void cleanup_close(int *pfd)
+{
+	if (*pfd != -1 && *pfd != STDIN_FILENO && *pfd != STDOUT_FILENO &&
+	    *pfd != STDERR_FILENO)
+		close(*pfd);
+}
+
 static int
 daemonize(void)
 {
 	int pid;
-	int dev_null_fd;
+	int dev_null_fd __attribute__((cleanup(cleanup_close))) = -1;
 
 	if( (pid = fork()) < 0){
 		fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
@@ -3371,25 +3378,21 @@ daemonize(void)
 		_exit(0);
 	}
 
-	close(STDIN_FILENO);
-	if (dup(dev_null_fd) < 0) {
-		fprintf(stderr, "cannot dup /dev/null to stdin : %s\n",
+	if (dup2(dev_null_fd, STDIN_FILENO) < 0) {
+		fprintf(stderr, "cannot dup2 /dev/null to stdin : %s\n",
 			strerror(errno));
 		_exit(0);
 	}
-	close(STDOUT_FILENO);
-	if (dup(dev_null_fd) < 0) {
-		fprintf(stderr, "cannot dup /dev/null to stdout : %s\n",
+	if (dup2(dev_null_fd, STDOUT_FILENO) < 0) {
+		fprintf(stderr, "cannot dup2 /dev/null to stdout : %s\n",
 			strerror(errno));
 		_exit(0);
 	}
-	close(STDERR_FILENO);
-	if (dup(dev_null_fd) < 0) {
+	if (dup2(dev_null_fd, STDERR_FILENO) < 0) {
 		fprintf(stderr, "cannot dup /dev/null to stderr : %s\n",
 			strerror(errno));
 		_exit(0);
 	}
-	close(dev_null_fd);
 	daemon_pid = getpid();
 	return 0;
 }
Signature · 7 lines
2.33.1


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

PATCH 18/21 libmultipath (coverity): avoid sleeping in dm_mapname()

In reply to [email protected]

From: Martin Wilck <[email protected]>

dm_mapname is called while holding the vecs lock, and shouldn't sleep. All callers handle a NULL return value.

This code dates back to the initial commit creating multipathd, 66b457b ("[multipathd]") from 2005. I am pretty certain that the comment "device map might not be ready when we get here from uevent trigger" doesn't hold any more (if it has ever been true). Perhaps the early code acted on "add" events for maps, which would indeed not necessarily have been set up. On the actually relevant "change" events, the map name has to be set.

---
 libmultipath/devmapper.c | 17 +----------------
 1 file changed, 1 insertion(+), 16 deletions(-)
libmultipath/devmapper.c +1 -16
index 3e1a726..6c3ca1f 100644
@@ -1390,7 +1390,6 @@ dm_mapname(int major, int minor)
 	const char *map;
 	struct dm_task *dmt;
 	int r;
-	int loop = MAX_WAIT * LOOPS_PER_SEC;
 
 	if (!(dmt = libmp_dm_task_create(DM_DEVICE_STATUS)))
 		return NULL;
@@ -1400,23 +1399,9 @@ dm_mapname(int major, int minor)
 		goto bad;
 
 	dm_task_no_open_count(dmt);
-
-	/*
-	 * device map might not be ready when we get here from
-	 * daemon uev_trigger -> uev_add_map
-	 */
-	while (--loop) {
-		r = libmp_dm_task_run(dmt);
-
-		if (r)
-			break;
-
-		usleep(1000 * 1000 / LOOPS_PER_SEC);
-	}
-
+	r = libmp_dm_task_run(dmt);
 	if (!r) {
 		dm_log_error(2, DM_DEVICE_STATUS, dmt);
-		condlog(0, "%i:%i: timeout fetching map name", major, minor);
 		goto bad;
 	}
 
Signature · 7 lines
2.33.1


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

PATCH 19/21 libmultipath (coverity): Revert "setup_map: wait for pending path checkers to finish"

In reply to [email protected]

From: Martin Wilck <[email protected]>

This reverts commit adf551f601596aac0774884c8a5b6b0f74721cfc.

Since 2b1fe13 ("libmultipath: allow force reload with no active paths"), reloading maps with pending paths is no issue any more. The path checker is going to set the correct status of the path quickly. If the paths are down, I/O will eventually fail.

Sleeping with the vecs lock held is undesirable though, so avoid waiting in setup_map().

---
 libmultipath/configure.c | 63 +---------------------------------------
 1 file changed, 1 insertion(+), 62 deletions(-)
libmultipath/configure.c +1 -62
index f1a890a..79670f9 100644
@@ -44,10 +44,6 @@
 #include "sysfs.h"
 #include "io_err_stat.h"
 
-/* Time in ms to wait for pending checkers in setup_map() */
-#define WAIT_CHECKERS_PENDING_MS 10
-#define WAIT_ALL_CHECKERS_PENDING_MS 90
-
 /* group paths in pg by host adapter
  */
 int group_by_host_adapter(struct pathgroup *pgp, vector adapters)
@@ -261,42 +257,11 @@ int rr_optimize_path_order(struct pathgroup *pgp)
 	return 0;
 }
 
-static int wait_for_pending_paths(struct multipath *mpp,
-				  struct config *conf,
-				  int n_pending, int goal, int wait_ms)
-{
-	static const struct timespec millisec =
-		{ .tv_sec = 0, .tv_nsec = 1000*1000 };
-	int i, j;
-	struct path *pp;
-	struct pathgroup *pgp;
-	struct timespec ts;
-
-	do {
-		vector_foreach_slot(mpp->pg, pgp, i) {
-			vector_foreach_slot(pgp->paths, pp, j) {
-				if (pp->state != PATH_PENDING)
-					continue;
-				pp->state = get_state(pp, conf,
-						      0, PATH_PENDING);
-				if (pp->state != PATH_PENDING &&
-				    --n_pending <= goal)
-					return 0;
-			}
-		}
-		ts = millisec;
-		while (nanosleep(&ts, &ts) != 0 && errno == EINTR)
-			/* nothing */;
-	} while (--wait_ms > 0);
-
-	return n_pending;
-}
-
 int setup_map(struct multipath *mpp, char **params, struct vectors *vecs)
 {
 	struct pathgroup * pgp;
 	struct config *conf;
-	int i, n_paths, marginal_pathgroups;
+	int i, marginal_pathgroups;
 	char *save_attr;
 
 	/*
@@ -395,7 +360,6 @@ int setup_map(struct multipath *mpp, char **params, struct vectors *vecs)
 	if (marginal_path_check_enabled(mpp))
 		start_io_err_stat_thread(vecs);
 
-	n_paths = VECTOR_SIZE(mpp->paths);
 	/*
 	 * assign paths to path groups -- start with no groups and all paths
 	 * in mpp->paths
@@ -410,31 +374,6 @@ int setup_map(struct multipath *mpp, char **params, struct vectors *vecs)
 	if (group_paths(mpp, marginal_pathgroups))
 		return 1;
 
-	/*
-	 * If async state detection is used, see if pending state checks
-	 * have finished, to get nr_active right. We can't wait until the
-	 * checkers time out, as that may take 30s or more, and we are
-	 * holding the vecs lock.
-	 */
-	if (conf->force_sync == 0 && n_paths > 0) {
-		int n_pending = pathcount(mpp, PATH_PENDING);
-
-		if (n_pending > 0)
-			n_pending = wait_for_pending_paths(
-				mpp, conf, n_pending, 0,
-				WAIT_CHECKERS_PENDING_MS);
-		/* ALL paths pending - wait some more, but be satisfied
-		   with only some paths finished */
-		if (n_pending == n_paths)
-			n_pending = wait_for_pending_paths(
-				mpp, conf, n_pending,
-				n_paths >= 4 ? 2 : 1,
-				WAIT_ALL_CHECKERS_PENDING_MS);
-		if (n_pending > 0)
-			condlog(2, "%s: setting up map with %d/%d path checkers pending",
-				mpp->alias, n_pending, n_paths);
-	}
-
 	/*
 	 * ponders each path group and determine highest prio pg
 	 * to switch over (default to first)
Signature · 7 lines
2.33.1


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

PATCH 20/21 libmultipath (coverity): check return values in dm_get_multipath()

In reply to [email protected]

From: Martin Wilck <[email protected]>

Coverity CID 374151.

---
 libmultipath/devmapper.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
libmultipath/devmapper.c +4 -2
index 6c3ca1f..1074992 100644
@@ -1295,8 +1295,10 @@ struct multipath *dm_get_multipath(const char *name)
 	if (dm_get_map(name, &mpp->size, NULL) != DMP_OK)
 		goto out;
 
-	dm_get_uuid(name, mpp->wwid, WWID_SIZE);
-	dm_get_info(name, &mpp->dmi);
+	if (dm_get_uuid(name, mpp->wwid, WWID_SIZE) != 0)
+		condlog(2, "%s: failed to get uuid for %s", __func__, name);
+	if (dm_get_info(name, &mpp->dmi) != 0)
+		condlog(2, "%s: failed to get info for %s", __func__, name);
 
 	return mpp;
 out:
Signature · 7 lines
2.33.1


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

PATCH 21/21 libmultipath: update_pathvec_from_dm: don't force DI_WWID

In reply to [email protected]

From: Martin Wilck <[email protected]>

For existing paths, we don't need to force setting DI_WWID. It's pointless because it would matter only in multipathd, and all callers of update_multipath_table() / update_pathvec_from_dm() in multipathd call it with flags=0, so this code won't be executed. And this makes sense, because the path would either have been cleanly initialized via path_discover() or an uevent, or pathinfo() would have been called in update_pathvec_from_dm() in an earlier invocation.

Also remove the misleading comment, and make coverity happy by checking the return value of pathinfo().

---

Note: I haven't fully made up my mind whether we should act on a pathinfo() failure here. For now, let the log message suffice. Because the pathinfo flags are usually clear and pp->udev exists, the only possible failure would be filtering by foreign libraries or by devnode, which seems highly unlikely if it hasn't happened beforehand.

---
 libmultipath/structs_vec.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
libmultipath/structs_vec.c +3 -4
index df5709a..bfec840 100644
@@ -149,16 +149,15 @@ bool update_pathvec_from_dm(vector pathvec, struct multipath *mpp,
 			 * uninitialized struct path to pgp->paths, with only
 			 * pp->dev_t filled in. Thus if pp->udev is set here,
 			 * we know that the path is in pathvec already.
-			 * However, it's possible that the path in pathvec is
-			 * different from the one the kernel still had in its
-			 * map.
 			 */
 			if (pp->udev) {
 				if (pathinfo_flags & ~DI_NOIO) {
 					conf = get_multipath_config();
 					pthread_cleanup_push(put_multipath_config,
 							     conf);
-					pathinfo(pp, conf, pathinfo_flags|DI_WWID);
+					if (pathinfo(pp, conf, pathinfo_flags) != PATHINFO_OK)
+						condlog(2, "%s: pathinfo failed for existing path %s (flags=0x%x)",
+							__func__, pp->dev, pathinfo_flags);
 					pthread_cleanup_pop(1);
 				}
 			} else {
Signature · 7 lines
2.33.1


--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
#33 lore raw
In reply to [email protected]
Quoted 9 lines · On Fri, 2021-11-19 at 00:13 +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

Hi Christophe, hi Ben,

I have started a new attempt to fix defects reported by coverity. With this set (on top of the previously posted one), and a number of false positive classifications, I have been able to bring the defect count all the way down to zero.

Quoted 6 lines

Most of these patches fix defects reported by coverity. Some fix other things that occured to me while working on this.

Regards,
Martin

There are a few whitespace issues in this series, like in my earlier submissions. I've fixed them locally. Please indicate if you want a resubmission of the series.

Regards,
Martin

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

maintainer Reviewed-by on the v1 series
#34 lore raw
In reply to [email protected]
Quoted 4 lines · On Fri, Nov 19, 2021 at 12:13:17AM +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

Hi Christophe, hi Ben,

Quoted 8 lines

I have started a new attempt to fix defects reported by coverity. With this set (on top of the previously posted one), and a number of false positive classifications, I have been able to bring the defect count all the way down to zero.

Most of these patches fix defects reported by coverity. Some fix other things that occured to me while working on this.

With the exception of 0005, 0007 and 0008,

Quoted 39 lines

Regards,
Martin

Martin Wilck (21):
multipath tools: github workflows: add coverity workflow multipathd (coverity): check atexit() return value multipathd (coverity): terminate uxlsnr when polls allocation fails
libmultipath: strbuf: add __get_strbuf_buf() libmultipath (coverity): improve input checking in parse_vpd_pg83
multipath-tools: add tests for broken VPD page 83
libmultipath: use strbuf in parse_vpd_pg83() libmultipath (coverity): fix tainted values in alua_rtpg.c multipath, multipathd: exit if bindings file is broken libmultipath (coverity): silence unchecked return value warning
multipath: remove pointless code from getopt processing libmultipath (coverity): set umask before mkstemp multipathd (coverity): simplify set_oom_adj()
kpartx: open /dev/loop-control only once
kpartx: use opened loop device immediately
kpartx: find_unused_loop_device(): add newlines multipathd (coverity): daemonize(): use dup2 libmultipath (coverity): avoid sleeping in dm_mapname() libmultipath (coverity): Revert "setup_map: wait for pending path
checkers to finish"
libmultipath (coverity): check return values in dm_get_multipath()
libmultipath: update_pathvec_from_dm: don't force DI_WWID

 .github/workflows/coverity.yaml       |  51 ++++++
 kpartx/kpartx.c                       |   4 +-
 kpartx/lopart.c                       | 100 +++++-----
 kpartx/lopart.h                       |   3 +-
 libmultipath/alias.c                  |   4 +
 libmultipath/configure.c              |  63 +------
 libmultipath/devmapper.c              |  23 +--
 libmultipath/discovery.c              | 252 ++++++++++++++------------
 libmultipath/prioritizers/alua_rtpg.c |  13 +-
 libmultipath/prioritizers/alua_spc3.h |  30 ++-
 libmultipath/propsel.c                |   2 +-
 libmultipath/strbuf.c                 |   5 +
Quoted 11 lines
 libmultipath/strbuf.h                 |  14 ++
 libmultipath/structs_vec.c            |   7 +-
 multipath/main.c                      |  13 +-
 multipathd/main.c                     |  91 ++++------
 multipathd/uxlsnr.c                   |   1 +
 tests/vpd.c                           | 108 +++++++++++
 18 files changed, 456 insertions(+), 328 deletions(-)
 create mode 100644 .github/workflows/coverity.yaml
Signature · 1 line
2.33.1

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