Skip to contentSkip to conversation

mdmon: fix segfault

PATCH Applied Mateusz Kusiak 2 participants 4 messages Jan 2, 2023 – Jan 4, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 mdmon: fix segfault

Mdmon crashes if stat2devnm returns null.
Use open_mddev to check if device is mddevice and get name using fd2devnm.
Refactor container name handling.

---
 Makefile |  2 +-
 mdmon.c  | 26 ++++++++++++--------------
 2 files changed, 13 insertions(+), 15 deletions(-)
Makefile +1 -1
index ec1f99ed..5eac1a4e 100644
@@ -160,7 +160,7 @@ SRCS =  $(patsubst %.o,%.c,$(OBJS))
 
 INCL = mdadm.h part.h bitmap.h
 
-MON_OBJS = mdmon.o monitor.o managemon.o uuid.o util.o maps.o mdstat.o sysfs.o \
+MON_OBJS = mdmon.o monitor.o managemon.o uuid.o util.o maps.o mdstat.o sysfs.o config.o mapfile.o mdopen.o\
 	policy.o lib.o \
 	Kill.o sg_io.o dlink.o ReadMe.o super-intel.o \
 	super-mbr.o super-gpt.o \
mdmon.c +12 -14
index e9d035eb..ecf52dc8 100644
@@ -363,14 +363,14 @@ int main(int argc, char *argv[])
 	}
 
 	if (all == 0 && container_name == NULL) {
-		if (argv[optind])
-			container_name = argv[optind];
+		if (argv[optind]) {
+			container_name = get_md_name(argv[optind]);
+			if (!container_name)
+				container_name = argv[optind];
+		}
 	}
 
-	if (container_name == NULL)
-		usage();
-
-	if (argc - optind > 1)
+	if (container_name == NULL || argc - optind > 1)
 		usage();
 
 	if (strcmp(container_name, "/proc/mdstat") == 0)
@@ -402,21 +402,19 @@ int main(int argc, char *argv[])
 		free_mdstat(mdstat);
 
 		return status;
-	} else if (strncmp(container_name, "md", 2) == 0) {
-		int id = devnm2devid(container_name);
-		if (id)
-			devnm = container_name;
 	} else {
-		struct stat st;
+		int mdfd = open_mddev(container_name, 1);
 
-		if (stat(container_name, &st) == 0)
-			devnm = xstrdup(stat2devnm(&st));
+		if (mdfd < 0)
+			return 1;
+		devnm = fd2devnm(mdfd);
+		close(mdfd);
 	}
 
 	if (!devnm) {
 		pr_err("%s is not a valid md device name\n",
 			container_name);
-		exit(1);
+		return 1;
 	}
 	return mdmon(devnm, dofork && do_fork(), takeover);
 }
Signature · 1 line
2.26.2
#1 lore raw

PATCH 2/2 util: remove obsolete code from get_md_name

get_md_name() is used only with mdstat entries.
Remove dead code and simplyfy function.

Remove redundadnt checks from mdmon.c

---
 mdmon.c |  8 +++-----
 util.c  | 51 +++++++++++++++++----------------------------------
 2 files changed, 20 insertions(+), 39 deletions(-)
mdmon.c +3 -5
index ecf52dc8..60ba3182 100644
@@ -366,7 +366,7 @@ int main(int argc, char *argv[])
 		if (argv[optind]) {
 			container_name = get_md_name(argv[optind]);
 			if (!container_name)
-				container_name = argv[optind];
+				return 1;
 		}
 	}
 
@@ -403,11 +403,9 @@ int main(int argc, char *argv[])
 
 		return status;
 	} else {
-		int mdfd = open_mddev(container_name, 1);
-
-		if (mdfd < 0)
-			return 1;
+		int mdfd = open_mddev(container_name, 0);
 		devnm = fd2devnm(mdfd);
+
 		close(mdfd);
 	}
 
util.c +17 -34

64 lines not on this page · raw message

Signature · 1 line
2.26.2
Applied
#2 lore raw

Re: [PATCH 2/2] util: remove obsolete code from get_md_name

Quoted 10 lines · Mateusz Kusiak

get_md_name() is used only with mdstat entries.
Remove dead code and simplyfy function.

Remove redundadnt checks from mdmon.c

---
 mdmon.c |  8 +++-----
 util.c  | 51 +++++++++++++++++----------------------------------
 2 files changed, 20 insertions(+), 39 deletions(-)

Applied!

Thanks,
Jes

Applied
#3 lore raw
In reply to Mateusz Kusiak
Quoted 11 lines · Mateusz Kusiak

Mdmon crashes if stat2devnm returns null.
Use open_mddev to check if device is mddevice and get name using fd2devnm.
Refactor container name handling.

---
 Makefile |  2 +-
 mdmon.c  | 26 ++++++++++++--------------
 2 files changed, 13 insertions(+), 15 deletions(-)

Applied!

Thanks,
Jes