Blame SOURCES/0016-Mdmonitor-Fix-segfault.patch

cd8c44
From e702f392959d1c2ad2089e595b52235ed97b4e18 Mon Sep 17 00:00:00 2001
cd8c44
From: Kinga Tanska <kinga.tanska@intel.com>
cd8c44
Date: Mon, 6 Jun 2022 12:32:12 +0200
2b63fb
Subject: [PATCH 16/83] Mdmonitor: Fix segfault
cd8c44
cd8c44
Mdadm with "--monitor" parameter requires md device
cd8c44
as an argument to be monitored. If given argument is
cd8c44
not a md device, error shall be returned. Previously
cd8c44
it was not checked and invalid argument caused
cd8c44
segmentation fault. This commit adds checking
cd8c44
that devices passed to mdmonitor are md devices.
cd8c44
cd8c44
Signed-off-by: Kinga Tanska <kinga.tanska@intel.com>
cd8c44
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
cd8c44
---
cd8c44
 Monitor.c | 10 +++++++++-
cd8c44
 mdadm.h   |  1 +
cd8c44
 mdopen.c  | 17 +++++++++++++++++
cd8c44
 3 files changed, 27 insertions(+), 1 deletion(-)
cd8c44
cd8c44
diff --git a/Monitor.c b/Monitor.c
cd8c44
index c0ab5412..4e5802b5 100644
cd8c44
--- a/Monitor.c
cd8c44
+++ b/Monitor.c
cd8c44
@@ -182,6 +182,7 @@ int Monitor(struct mddev_dev *devlist,
cd8c44
 				continue;
cd8c44
 			if (strcasecmp(mdlist->devname, "<ignore>") == 0)
cd8c44
 				continue;
cd8c44
+
cd8c44
 			st = xcalloc(1, sizeof *st);
cd8c44
 			if (mdlist->devname[0] == '/')
cd8c44
 				st->devname = xstrdup(mdlist->devname);
cd8c44
@@ -190,6 +191,8 @@ int Monitor(struct mddev_dev *devlist,
cd8c44
 				strcpy(strcpy(st->devname, "/dev/md/"),
cd8c44
 				       mdlist->devname);
cd8c44
 			}
cd8c44
+			if (!is_mddev(mdlist->devname))
cd8c44
+				return 1;
cd8c44
 			st->next = statelist;
cd8c44
 			st->devnm[0] = 0;
cd8c44
 			st->percent = RESYNC_UNKNOWN;
cd8c44
@@ -203,7 +206,12 @@ int Monitor(struct mddev_dev *devlist,
cd8c44
 		struct mddev_dev *dv;
cd8c44
 
cd8c44
 		for (dv = devlist; dv; dv = dv->next) {
cd8c44
-			struct state *st = xcalloc(1, sizeof *st);
cd8c44
+			struct state *st;
cd8c44
+
cd8c44
+			if (!is_mddev(dv->devname))
cd8c44
+				return 1;
cd8c44
+
cd8c44
+			st = xcalloc(1, sizeof *st);
cd8c44
 			mdlist = conf_get_ident(dv->devname);
cd8c44
 			st->devname = xstrdup(dv->devname);
cd8c44
 			st->next = statelist;
cd8c44
diff --git a/mdadm.h b/mdadm.h
cd8c44
index 09915a00..d53df169 100644
cd8c44
--- a/mdadm.h
cd8c44
+++ b/mdadm.h
cd8c44
@@ -1636,6 +1636,7 @@ extern int create_mddev(char *dev, char *name, int autof, int trustworthy,
cd8c44
 #define	FOREIGN	2
cd8c44
 #define	METADATA 3
cd8c44
 extern int open_mddev(char *dev, int report_errors);
cd8c44
+extern int is_mddev(char *dev);
cd8c44
 extern int open_container(int fd);
cd8c44
 extern int metadata_container_matches(char *metadata, char *devnm);
cd8c44
 extern int metadata_subdev_matches(char *metadata, char *devnm);
cd8c44
diff --git a/mdopen.c b/mdopen.c
cd8c44
index 245be537..d18c9319 100644
cd8c44
--- a/mdopen.c
cd8c44
+++ b/mdopen.c
cd8c44
@@ -475,6 +475,23 @@ int open_mddev(char *dev, int report_errors)
cd8c44
 	return mdfd;
cd8c44
 }
cd8c44
 
cd8c44
+/**
cd8c44
+ * is_mddev() - check that file name passed is an md device.
cd8c44
+ * @dev: file name that has to be checked.
cd8c44
+ * Return: 1 if file passed is an md device, 0 if not.
cd8c44
+ */
cd8c44
+int is_mddev(char *dev)
cd8c44
+{
cd8c44
+	int fd = open_mddev(dev, 1);
cd8c44
+
cd8c44
+	if (fd >= 0) {
cd8c44
+		close(fd);
cd8c44
+		return 1;
cd8c44
+	}
cd8c44
+
cd8c44
+	return 0;
cd8c44
+}
cd8c44
+
cd8c44
 char *find_free_devnm(int use_partitions)
cd8c44
 {
cd8c44
 	static char devnm[32];
cd8c44
-- 
2b63fb
2.38.1
cd8c44