Blame SOURCES/0064-libmultipath-check-udev_device_get_-return-value-to-.patch

50e627
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
50e627
From: lixiaokeng <lixiaokeng@huawei.com>
50e627
Date: Mon, 21 Sep 2020 12:00:39 +0800
50e627
Subject: [PATCH] libmultipath: check udev_device_get_* return value to avoid
50e627
 segfault
50e627
50e627
The udev_device_get_* function may return NULL, and it will be
50e627
deregerenced in str* and sscanf func. We check the return value
50e627
to avoid segfault. Fix all.
50e627
50e627
Reviewed-by: Martin Wilck <mwilck@suse.com>
50e627
Signed-off-by:Lixiaokeng<lixiaokeng@huawei.com>
50e627
Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
50e627
Signed-off-by: Linfeilong <linfeilong@huawei.com>
50e627
Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
50e627
---
50e627
 libmultipath/configure.c    |  4 +++-
50e627
 libmultipath/discovery.c    |  9 +++++++--
50e627
 libmultipath/foreign/nvme.c | 10 +++++++---
50e627
 3 files changed, 17 insertions(+), 6 deletions(-)
50e627
50e627
diff --git a/libmultipath/configure.c b/libmultipath/configure.c
50e627
index 2e8f34f9..a6893d8d 100644
50e627
--- a/libmultipath/configure.c
50e627
+++ b/libmultipath/configure.c
50e627
@@ -511,6 +511,7 @@ static void trigger_partitions_udev_change(struct udev_device *dev,
50e627
 {
50e627
 	struct udev_enumerate *part_enum;
50e627
 	struct udev_list_entry *item;
50e627
+	const char *devtype;
50e627
 
50e627
 	part_enum = udev_enumerate_new(udev);
50e627
 	if (!part_enum)
50e627
@@ -531,7 +532,8 @@ static void trigger_partitions_udev_change(struct udev_device *dev,
50e627
 		if (!part)
50e627
 			continue;
50e627
 
50e627
-		if (!strcmp("partition", udev_device_get_devtype(part))) {
50e627
+		devtype = udev_device_get_devtype(part);
50e627
+		if (devtype && !strcmp("partition", devtype)) {
50e627
 			condlog(4, "%s: triggering %s event for %s", __func__,
50e627
 				action, syspath);
50e627
 			sysfs_attr_set_value(part, "uevent", action, len);
50e627
diff --git a/libmultipath/discovery.c b/libmultipath/discovery.c
50e627
index a328aafa..74abf34d 100644
50e627
--- a/libmultipath/discovery.c
50e627
+++ b/libmultipath/discovery.c
50e627
@@ -353,7 +353,7 @@ sysfs_get_tgt_nodename(struct path *pp, char *node)
50e627
 		tgtdev = udev_device_get_parent(parent);
50e627
 		while (tgtdev) {
50e627
 			tgtname = udev_device_get_sysname(tgtdev);
50e627
-			if (sscanf(tgtname, "end_device-%d:%d",
50e627
+			if (tgtname && sscanf(tgtname, "end_device-%d:%d",
50e627
 				   &host, &tgtid) == 2)
50e627
 				break;
50e627
 			tgtdev = udev_device_get_parent(tgtdev);
50e627
@@ -386,7 +386,7 @@ sysfs_get_tgt_nodename(struct path *pp, char *node)
50e627
 	/* Check for FibreChannel */
50e627
 	tgtdev = udev_device_get_parent(parent);
50e627
 	value = udev_device_get_sysname(tgtdev);
50e627
-	if (sscanf(value, "rport-%d:%d-%d",
50e627
+	if (value && sscanf(value, "rport-%d:%d-%d",
50e627
 		   &host, &channel, &tgtid) == 3) {
50e627
 		tgtdev = udev_device_new_from_subsystem_sysname(udev,
50e627
 				"fc_remote_ports", value);
50e627
@@ -516,6 +516,9 @@ int sysfs_get_host_pci_name(const struct path *pp, char *pci_name)
50e627
 		 */
50e627
 		value = udev_device_get_sysname(parent);
50e627
 
50e627
+		if (!value)
50e627
+			return 1;
50e627
+
50e627
 		strncpy(pci_name, value, SLOT_NAME_SIZE);
50e627
 		udev_device_unref(hostdev);
50e627
 		return 0;
50e627
@@ -1518,6 +1521,8 @@ ccw_sysfs_pathinfo (struct path * pp, vector hwtable)
50e627
 	 * host / bus / target / lun
50e627
 	 */
50e627
 	attr_path = udev_device_get_sysname(parent);
50e627
+	if (!attr_path)
50e627
+		return PATHINFO_FAILED;
50e627
 	pp->sg_id.lun = 0;
50e627
 	if (sscanf(attr_path, "%i.%i.%x",
50e627
 		   &pp->sg_id.host_no,
50e627
diff --git a/libmultipath/foreign/nvme.c b/libmultipath/foreign/nvme.c
50e627
index 09cdddf0..5feb1e95 100644
50e627
--- a/libmultipath/foreign/nvme.c
50e627
+++ b/libmultipath/foreign/nvme.c
50e627
@@ -482,6 +482,7 @@ _find_path_by_syspath(struct nvme_map *map, const char *syspath)
50e627
 	struct nvme_pathgroup *pg;
50e627
 	char real[PATH_MAX];
50e627
 	const char *ppath;
50e627
+	const char *psyspath;
50e627
 	int i;
50e627
 
50e627
 	ppath = realpath(syspath, real);
50e627
@@ -493,8 +494,8 @@ _find_path_by_syspath(struct nvme_map *map, const char *syspath)
50e627
 	vector_foreach_slot(&map->pgvec, pg, i) {
50e627
 		struct nvme_path *path = nvme_pg_to_path(pg);
50e627
 
50e627
-		if (!strcmp(ppath,
50e627
-			    udev_device_get_syspath(path->udev)))
50e627
+		psyspath = udev_device_get_syspath(path->udev);
50e627
+		if (psyspath && !strcmp(ppath, psyspath))
50e627
 			return path;
50e627
 	}
50e627
 	condlog(4, "%s: %s: %s not found", __func__, THIS, ppath);
50e627
@@ -538,6 +539,7 @@ struct udev_device *get_ctrl_blkdev(const struct context *ctx,
50e627
 	struct udev_list_entry *item;
50e627
 	struct udev_device *blkdev = NULL;
50e627
 	struct udev_enumerate *enm = udev_enumerate_new(ctx->udev);
50e627
+	const char *devtype;
50e627
 
50e627
 	if (enm == NULL)
50e627
 		return NULL;
50e627
@@ -562,7 +564,9 @@ struct udev_device *get_ctrl_blkdev(const struct context *ctx,
50e627
 					   udev_list_entry_get_name(item));
50e627
 		if (tmp == NULL)
50e627
 			continue;
50e627
-		if (!strcmp(udev_device_get_devtype(tmp), "disk")) {
50e627
+
50e627
+		devtype = udev_device_get_devtype(tmp);
50e627
+		if (devtype && !strcmp(devtype, "disk")) {
50e627
 			blkdev = tmp;
50e627
 			break;
50e627
 		} else
50e627
-- 
50e627
2.17.2
50e627