Skip to contentSkip to conversation

libmpathpersist fixes

PATCH Superseded Martin Wilck 1 participant 9 messages Jun 24, 2018 Device mapper

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover libmpathpersist fixes

Hi Christophe,

this is v2 of my small set of libmpathpersist fixes. It's based on upstream, thus it has a minor conflict with my previous patch series "multipath-tools: improve config file handling". I can of course provide a rebased patch set if you prefer.

Changes wrt v1:
- introduce and use get_unaligned_be64 (Bart van Assche)
- add test for (get|put)_unaligned_bexx functions

Note: we have both the "unaligned" and the "beXY_to_cpu" APIs now, for essentially the same purpose. I wonder if we should settle on a single API instead. I am unsure if beXY_to_cpu() has alignment issues on some architectures (I don't think there are issues on our current main target platforms). If yes, I believe that we'd be on the safe side using the "unaligned" API. Anyway, that could be done in a follow-up patch.

Thanks,
Martin

Martin Wilck (8):
libmpathpersist: remove duplicate test in readfullstatus
libmpathpersist: fix typo in mpath_format_readfullstatus
libmpathpersist: fix stack overflow in mpath_format_readfullstatus()
libmultipath: add (get|put)_unaligned_be64 multipath-tools/tests: add tests for get_unaligned_beXX
libmpathpersist: fix byte swapping for big endian systems (lib)mpathpersist: use O_RDONLY file descriptors
libmultipath: fix gcc 8.1 "truncated output" warnings

 libmpathpersist/mpath_persist.c  |  8 +--
 libmpathpersist/mpath_pr_ioctl.c | 75 +++++++++----------------
 libmultipath/sysfs.c             |  4 +-
 libmultipath/unaligned.h         | 16 ++++++
 libmultipath/util.c              |  4 +-
 mpathpersist/main.c              |  2 +-
 tests/Makefile                   |  2 +-
 tests/unaligned.c                | 96 ++++++++++++++++++++++++++++++++
 8 files changed, 146 insertions(+), 61 deletions(-)
 create mode 100644 tests/unaligned.c
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/8 libmpathpersist: remove duplicate test in readfullstatus

---
 libmpathpersist/mpath_pr_ioctl.c | 6 ------
 1 file changed, 6 deletions(-)
libmpathpersist/mpath_pr_ioctl.c +0 -6
index 6dd74031..dcdb530d 100644
@@ -216,12 +216,6 @@ void mpath_format_readfullstatus(struct prin_resp *pr_buff, int len, int noisy)
 	mpath_reverse_uint32_byteorder(&pr_buff->prin_descriptor.prin_readfd.prgeneration);
 	mpath_reverse_uint32_byteorder(&pr_buff->prin_descriptor.prin_readfd.number_of_descriptor);
 
-	if (0 == pr_buff->prin_descriptor.prin_readfd.number_of_descriptor)
-	{
-		return ;
-	}
-
-
 	if (pr_buff->prin_descriptor.prin_readfd.number_of_descriptor == 0)
 	{
 		condlog(3, "No registration or resrvation found.");
Signature · 1 line
2.17.1
#2 lore raw

PATCH v2 2/8 libmpathpersist: fix typo in mpath_format_readfullstatus

In reply to Martin Wilck
---
 libmpathpersist/mpath_pr_ioctl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libmpathpersist/mpath_pr_ioctl.c +1 -1
index dcdb530d..bcbb9691 100644
@@ -218,7 +218,7 @@ void mpath_format_readfullstatus(struct prin_resp *pr_buff, int len, int noisy)
 
 	if (pr_buff->prin_descriptor.prin_readfd.number_of_descriptor == 0)
 	{
-		condlog(3, "No registration or resrvation found.");
+		condlog(3, "No registration or reservation found.");
 		return;
 	}
 
Signature · 1 line
2.17.1
#3 lore raw

PATCH v2 3/8 libmpathpersist: fix stack overflow in mpath_format_readfullstatus()

In reply to Martin Wilck

Some storage arrays return corrupt data in response to READ FULL STATUS PRIN commands. This may lead to stack overflow if the values aren't sanitized.

---
 libmpathpersist/mpath_pr_ioctl.c | 9 +++++++++
 1 file changed, 9 insertions(+)
libmpathpersist/mpath_pr_ioctl.c +9 -0
index bcbb9691..347f21b2 100644
@@ -241,6 +241,13 @@ void mpath_format_readfullstatus(struct prin_resp *pr_buff, int len, int noisy)
 		fdesc.rtpi = get_unaligned_be16(&p[18]);
 
 		tid_len_len = get_unaligned_be32(&p[20]);
+		if (tid_len_len + 24 + k >= additional_length) {
+			condlog(0,
+				"%s: corrupt PRIN response: status descriptor end %d exceeds length %d",
+				__func__, tid_len_len + k + 24,
+				additional_length);
+			tid_len_len = additional_length - k - 24;
+		}
 
 		if (tid_len_len > 0)
 			decode_transport_id( &fdesc, &p[24], tid_len_len);
@@ -272,6 +279,8 @@ decode_transport_id(struct prin_fulldescr *fdesc, unsigned char * p, int length)
 			break;
 		case MPATH_PROTOCOL_ID_ISCSI:
 			num = get_unaligned_be16(&p[2]);
+			if (num >= sizeof(fdesc->trnptid.iscsi_name))
+				num = sizeof(fdesc->trnptid.iscsi_name);
 			memcpy(&fdesc->trnptid.iscsi_name, &p[4], num);
 			jump = (((num + 4) < 24) ? 24 : num + 4);
 			break;
Signature · 1 line
2.17.1
#4 lore raw

PATCH v2 4/8 libmultipath: add (get|put)_unaligned_be64

In reply to Martin Wilck
---
 libmultipath/unaligned.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
libmultipath/unaligned.h +16 -0
index 14ec8b23..68c07742 100644
@@ -17,6 +17,14 @@ static inline uint32_t get_unaligned_be32(void *ptr)
 	return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
 }
 
+static inline uint64_t get_unaligned_be64(void *ptr)
+{
+	uint32_t low = get_unaligned_be32(ptr + 4);
+	uint64_t high = get_unaligned_be32(ptr);
+
+	return high << 32 | low;
+}
+
 static inline void put_unaligned_be16(uint16_t val, void *ptr)
 {
 	uint8_t *p = ptr;
@@ -35,4 +43,12 @@ static inline void put_unaligned_be32(uint32_t val, void *ptr)
 	p[3] = val;
 }
 
+static inline void put_unaligned_be64(uint64_t val, void *ptr)
+{
+	uint8_t *p = ptr;
+
+	put_unaligned_be32(val >> 32, p);
+	put_unaligned_be32(val, p + 4);
+}
+
 #endif /* _UNALIGNED_H_ */
Signature · 1 line
2.17.1
#5 lore raw

PATCH v2 5/8 multipath-tools/tests: add tests for get_unaligned_beXX

In reply to Martin Wilck
---
 tests/Makefile    |  2 +-
 tests/unaligned.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 97 insertions(+), 1 deletion(-)
 create mode 100644 tests/unaligned.c
tests/Makefile +1 -1
index 1f364110..d7102682 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
+TESTS := uevent parser util dmevents unaligned
 
 .SILENT: $(TESTS:%=%.o)
 .PRECIOUS: $(TESTS:%=%-test)
tests/unaligned.c new file +96 -0

96 lines not on this page · raw message

Signature · 1 line
2.17.1
#6 lore raw

PATCH v2 6/8 libmpathpersist: fix byte swapping for big endian systems

In reply to Martin Wilck

The functions "mpath_reverse_uintXY_byteorder" were apparently meant to convert BE integers to native. This leads to wrong results on big-endian systems. Make these functions use the get_unaligned_beXY API, and rename.

Also, remove the unused endianness test in mpath_pr_ioctl.c.

---
 libmpathpersist/mpath_persist.c  |  8 ++---
 libmpathpersist/mpath_pr_ioctl.c | 54 +++++++++-----------------------
 2 files changed, 16 insertions(+), 46 deletions(-)
libmpathpersist/mpath_persist.c +2 -6
index 907a17c9..5b62cc26 100644
@@ -20,6 +20,7 @@
 #include <ctype.h>
 #include "propsel.h"
 #include "util.h"
+#include "unaligned.h"
 
 #include "mpath_persist.h"
 #include "mpathpr.h"
@@ -543,12 +544,7 @@ int mpath_prout_reg(struct multipath *mpp,int rq_servact, int rq_scope,
 		}
 		if (!rollback && (thread[i].param.status == MPATH_PR_RESERV_CONFLICT)){
 			rollback = 1;
-			sa_key = 0;
-			for (i = 0; i < 8; ++i){
-				if (i > 0)
-					sa_key <<= 8;
-				sa_key |= paramp->sa_key[i];
-			}
+			sa_key = get_unaligned_be64(&paramp->sa_key[0]);
 			status = MPATH_PR_RESERV_CONFLICT ;
 		}
 		if (!rollback && (status == MPATH_PR_SUCCESS)){
libmpathpersist/mpath_pr_ioctl.c +14 -40

93 lines not on this page · raw message

Signature · 1 line
2.17.1
#7 lore raw

PATCH v2 7/8 (lib)mpathpersist: use O_RDONLY file descriptors

In reply to Martin Wilck

udevd catches close-after-write inotify events and generates "change" uvents for such devices, which may cause extra unnecessary and unwanted udev activity.

Therefore use O_RDONLY file descriptors for PRIN and PROUT commands. This works just as well as O_WRONLY. sg_persist has supported the --readonly option for years.

---
 libmpathpersist/mpath_pr_ioctl.c | 4 ++--
 mpathpersist/main.c              | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
libmpathpersist/mpath_pr_ioctl.c +2 -2
index 8416a3bf..a222b1e1 100644
@@ -52,7 +52,7 @@ int prout_do_scsi_ioctl(char * dev, int rq_servact, int rq_scope,
 	int fd = -1;
 
 	snprintf(devname, FILE_NAME_SIZE, "/dev/%s",dev);
-	fd = open(devname, O_WRONLY);
+	fd = open(devname, O_RDONLY);
 	if(fd < 0){
 		condlog (1, "%s: unable to open device.", dev);
 		return MPATH_PR_FILE_ERROR;
@@ -308,7 +308,7 @@ int prin_do_scsi_ioctl(char * dev, int rq_servact, struct prin_resp * resp, int
 	{MPATH_PRIN_CMD, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 
 	snprintf(devname, FILE_NAME_SIZE, "/dev/%s",dev);
-	fd = open(devname, O_WRONLY);
+	fd = open(devname, O_RDONLY);
 	if(fd < 0){
 		condlog(0, "%s: Unable to open device ", dev);
 		return MPATH_PR_FILE_ERROR;
mpathpersist/main.c +1 -1
index 5b37f3ae..34caa16c 100644
@@ -380,7 +380,7 @@ int main (int argc, char * argv[])
 	}
 
 	/* open device */
-	if ((fd = open (device_name, O_WRONLY)) < 0)
+	if ((fd = open (device_name, O_RDONLY)) < 0)
 	{
 		fprintf (stderr, "%s: error opening file (rw) fd=%d\n",
 				device_name, fd);
Signature · 1 line
2.17.1
#8 lore raw

PATCH v2 8/8 libmultipath: fix gcc 8.1 "truncated output" warnings

In reply to Martin Wilck

gcc 8.1 was warning about a possibly truncated output in snprintf.

---
 libmultipath/sysfs.c | 4 ++--
 libmultipath/util.c  | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
libmultipath/sysfs.c +2 -2
index 16e0a737..b7dacaad 100644
@@ -239,7 +239,7 @@ sysfs_get_size (struct path *pp, unsigned long long * size)
 int sysfs_check_holders(char * check_devt, char * new_devt)
 {
 	unsigned int major, new_minor, table_minor;
-	char path[PATH_SIZE], check_dev[PATH_SIZE];
+	char path[PATH_MAX], check_dev[PATH_SIZE];
 	char * table_name;
 	DIR *dirfd;
 	struct dirent *holder;
@@ -256,7 +256,7 @@ int sysfs_check_holders(char * check_devt, char * new_devt)
 
 	condlog(3, "%s: checking holder", check_dev);
 
-	snprintf(path, PATH_SIZE, "/sys/block/%s/holders", check_dev);
+	snprintf(path, sizeof(path), "/sys/block/%s/holders", check_dev);
 	dirfd = opendir(path);
 	if (dirfd == NULL) {
 		condlog(3, "%s: failed to open directory %s (%d)",
libmultipath/util.c +2 -2
index 7251ad08..aff74281 100644
@@ -329,7 +329,7 @@ setup_thread_attr(pthread_attr_t *attr, size_t stacksize, int detached)
 
 int systemd_service_enabled_in(const char *dev, const char *prefix)
 {
-	char path[PATH_SIZE], file[PATH_SIZE], service[PATH_SIZE];
+	char path[PATH_SIZE], file[PATH_MAX], service[PATH_SIZE];
 	DIR *dirfd;
 	struct dirent *d;
 	int found = 0;
@@ -356,7 +356,7 @@ int systemd_service_enabled_in(const char *dev, const char *prefix)
 		p = d->d_name + strlen(d->d_name) - 6;
 		if (strcmp(p, ".wants"))
 			continue;
-		snprintf(file, PATH_SIZE, "%s/%s/%s",
+		snprintf(file, sizeof(file), "%s/%s/%s",
 			 path, d->d_name, service);
 		if (stat(file, &stbuf) == 0) {
 			condlog(3, "%s: found %s", dev, file);
Signature · 1 line
2.17.1