]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
Mdmonitor: Fix segfault
authorKinga Tanska <kinga.tanska@intel.com>
Mon, 6 Jun 2022 10:32:12 +0000 (12:32 +0200)
committerJes Sorensen <jsorensen@fb.com>
Tue, 14 Jun 2022 14:36:03 +0000 (10:36 -0400)
Mdadm with "--monitor" parameter requires md device
as an argument to be monitored. If given argument is
not a md device, error shall be returned. Previously
it was not checked and invalid argument caused
segmentation fault. This commit adds checking
that devices passed to mdmonitor are md devices.

Signed-off-by: Kinga Tanska <kinga.tanska@intel.com>
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
Monitor.c
mdadm.h
mdopen.c

index c0ab5412261dbff743cfaf4fc6eeb8e9aad1a56f..4e5802b5d30e40b8e23c17de0ffa5c0d93baf510 100644 (file)
--- a/Monitor.c
+++ b/Monitor.c
@@ -182,6 +182,7 @@ int Monitor(struct mddev_dev *devlist,
                                continue;
                        if (strcasecmp(mdlist->devname, "<ignore>") == 0)
                                continue;
+
                        st = xcalloc(1, sizeof *st);
                        if (mdlist->devname[0] == '/')
                                st->devname = xstrdup(mdlist->devname);
@@ -190,6 +191,8 @@ int Monitor(struct mddev_dev *devlist,
                                strcpy(strcpy(st->devname, "/dev/md/"),
                                       mdlist->devname);
                        }
+                       if (!is_mddev(mdlist->devname))
+                               return 1;
                        st->next = statelist;
                        st->devnm[0] = 0;
                        st->percent = RESYNC_UNKNOWN;
@@ -203,7 +206,12 @@ int Monitor(struct mddev_dev *devlist,
                struct mddev_dev *dv;
 
                for (dv = devlist; dv; dv = dv->next) {
-                       struct state *st = xcalloc(1, sizeof *st);
+                       struct state *st;
+
+                       if (!is_mddev(dv->devname))
+                               return 1;
+
+                       st = xcalloc(1, sizeof *st);
                        mdlist = conf_get_ident(dv->devname);
                        st->devname = xstrdup(dv->devname);
                        st->next = statelist;
diff --git a/mdadm.h b/mdadm.h
index 09915a0009d96d77e91e0fb9e99992a4d1c96c37..d53df1697f889e878b59e5966e2a48c7bf803331 100644 (file)
--- a/mdadm.h
+++ b/mdadm.h
@@ -1636,6 +1636,7 @@ extern int create_mddev(char *dev, char *name, int autof, int trustworthy,
 #define        FOREIGN 2
 #define        METADATA 3
 extern int open_mddev(char *dev, int report_errors);
+extern int is_mddev(char *dev);
 extern int open_container(int fd);
 extern int metadata_container_matches(char *metadata, char *devnm);
 extern int metadata_subdev_matches(char *metadata, char *devnm);
index 245be537823016c33325599303d108845cb09aaa..d18c931996d28f52948e7a3871f86daa2cd13d39 100644 (file)
--- a/mdopen.c
+++ b/mdopen.c
@@ -475,6 +475,23 @@ int open_mddev(char *dev, int report_errors)
        return mdfd;
 }
 
+/**
+ * is_mddev() - check that file name passed is an md device.
+ * @dev: file name that has to be checked.
+ * Return: 1 if file passed is an md device, 0 if not.
+ */
+int is_mddev(char *dev)
+{
+       int fd = open_mddev(dev, 1);
+
+       if (fd >= 0) {
+               close(fd);
+               return 1;
+       }
+
+       return 0;
+}
+
 char *find_free_devnm(int use_partitions)
 {
        static char devnm[32];