Blame SOURCES/0078-libmutipath-validate-the-argument-count-of-config-st.patch

e71b65
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
e71b65
From: Benjamin Marzinski <bmarzins@redhat.com>
e71b65
Date: Wed, 14 Dec 2022 15:38:20 -0600
e71b65
Subject: [PATCH] libmutipath: validate the argument count of config strings
e71b65
e71b65
The features, path_selector, and hardware_handler config options pass
e71b65
their strings directly into the kernel.  If users omit the argument
e71b65
counts from these strings, or use the wrong value, the kernel's table
e71b65
parsing gets completely messed up, and the error messages it prints
e71b65
don't reflect what actully went wrong. To avoid messing up the
e71b65
kernel table parsing, verify that these strings correctly set the
e71b65
argument count to the number of arguments they have.
e71b65
e71b65
Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
e71b65
Reviewed-by: Martin Wilck <mwilck@suse.com>
e71b65
---
e71b65
 libmultipath/dict.c | 110 ++++++++++++++++++++++++++++++++++++++++----
e71b65
 1 file changed, 101 insertions(+), 9 deletions(-)
e71b65
e71b65
diff --git a/libmultipath/dict.c b/libmultipath/dict.c
e71b65
index 861d1d17..eb2f33a2 100644
e71b65
--- a/libmultipath/dict.c
e71b65
+++ b/libmultipath/dict.c
e71b65
@@ -142,6 +142,58 @@ set_dir(vector strvec, void *ptr, const char *file, int line_nr)
e71b65
 	return 0;
e71b65
 }
e71b65
 
e71b65
+static int
e71b65
+set_arg_str(vector strvec, void *ptr, int count_idx, const char *file,
e71b65
+	    int line_nr)
e71b65
+{
e71b65
+	char **str_ptr = (char **)ptr;
e71b65
+	char *old_str = *str_ptr;
e71b65
+	const char * const spaces = " \f\r\t\v";
e71b65
+	char *p, *end;
e71b65
+	int idx = -1;
e71b65
+	long int count = -1;
e71b65
+
e71b65
+	*str_ptr = set_value(strvec);
e71b65
+	if (!*str_ptr) {
e71b65
+		free(old_str);
e71b65
+		return 1;
e71b65
+	}
e71b65
+	p = *str_ptr;
e71b65
+	while (*p != '\0') {
e71b65
+		p += strspn(p, spaces);
e71b65
+		if (*p == '\0')
e71b65
+			break;
e71b65
+		idx += 1;
e71b65
+		if (idx == count_idx) {
e71b65
+			errno = 0;
e71b65
+			count = strtol(p, &end, 10);
e71b65
+			if (errno == ERANGE || end == p ||
e71b65
+			    !(isspace(*end) || *end == '\0')) {
e71b65
+				count = -1;
e71b65
+				break;
e71b65
+			}
e71b65
+		}
e71b65
+		p += strcspn(p, spaces);
e71b65
+	}
e71b65
+	if (count < 0) {
e71b65
+		condlog(1, "%s line %d, missing argument count for %s",
e71b65
+			file, line_nr, (char*)VECTOR_SLOT(strvec, 0));
e71b65
+		goto fail;
e71b65
+	}
e71b65
+	if (count != idx - count_idx) {
e71b65
+		condlog(1, "%s line %d, invalid argument count for %s:, got '%ld' expected '%d'",
e71b65
+			file, line_nr, (char*)VECTOR_SLOT(strvec, 0), count,
e71b65
+			idx - count_idx);
e71b65
+		goto fail;
e71b65
+	}
e71b65
+	free(old_str);
e71b65
+	return 0;
e71b65
+fail:
e71b65
+	free(*str_ptr);
e71b65
+	*str_ptr = old_str;
e71b65
+	return 0;
e71b65
+}
e71b65
+
e71b65
 static int
e71b65
 set_path(vector strvec, void *ptr, const char *file, int line_nr)
e71b65
 {
e71b65
@@ -294,6 +346,14 @@ def_ ## option ## _handler (struct config *conf, vector strvec,         \
e71b65
 	return set_int(strvec, &conf->option, minval, maxval, file, line_nr); \
e71b65
 }
e71b65
 
e71b65
+#define declare_def_arg_str_handler(option, count_idx)			\
e71b65
+static int								\
e71b65
+def_ ## option ## _handler (struct config *conf, vector strvec,		\
e71b65
+			    const char *file, int line_nr)		\
e71b65
+{									\
e71b65
+	return set_arg_str(strvec, &conf->option, count_idx, file, line_nr); \
e71b65
+}
e71b65
+
e71b65
 #define declare_def_snprint(option, function)				\
e71b65
 static int								\
e71b65
 snprint_def_ ## option (struct config *conf, struct strbuf *buff,	\
e71b65
@@ -346,6 +406,17 @@ hw_ ## option ## _handler (struct config *conf, vector strvec,		\
e71b65
 	return set_int(strvec, &hwe->option, minval, maxval, file, line_nr); \
e71b65
 }
e71b65
 
e71b65
+#define declare_hw_arg_str_handler(option, count_idx)			\
e71b65
+static int								\
e71b65
+hw_ ## option ## _handler (struct config *conf, vector strvec,		\
e71b65
+			    const char *file, int line_nr)		\
e71b65
+{									\
e71b65
+	struct hwentry * hwe = VECTOR_LAST_SLOT(conf->hwtable);		\
e71b65
+	if (!hwe)							\
e71b65
+		return 1;						\
e71b65
+	return set_arg_str(strvec, &hwe->option, count_idx, file, line_nr); \
e71b65
+}
e71b65
+
e71b65
 
e71b65
 #define declare_hw_snprint(option, function)				\
e71b65
 static int								\
e71b65
@@ -377,6 +448,16 @@ ovr_ ## option ## _handler (struct config *conf, vector strvec,		\
e71b65
 		       file, line_nr); \
e71b65
 }
e71b65
 
e71b65
+#define declare_ovr_arg_str_handler(option, count_idx)			\
e71b65
+static int								\
e71b65
+ovr_ ## option ## _handler (struct config *conf, vector strvec,		\
e71b65
+			    const char *file, int line_nr)		\
e71b65
+{									\
e71b65
+	if (!conf->overrides)						\
e71b65
+		return 1;						\
e71b65
+	return set_arg_str(strvec, &conf->overrides->option, count_idx, file, line_nr); \
e71b65
+}
e71b65
+
e71b65
 #define declare_ovr_snprint(option, function)				\
e71b65
 static int								\
e71b65
 snprint_ovr_ ## option (struct config *conf, struct strbuf *buff,	\
e71b65
@@ -407,6 +488,17 @@ mp_ ## option ## _handler (struct config *conf, vector strvec,		\
e71b65
 	return set_int(strvec, &mpe->option, minval, maxval, file, line_nr); \
e71b65
 }
e71b65
 
e71b65
+#define declare_mp_arg_str_handler(option, count_idx)			\
e71b65
+static int								\
e71b65
+mp_ ## option ## _handler (struct config *conf, vector strvec,		\
e71b65
+			    const char *file, int line_nr)		\
e71b65
+{									\
e71b65
+	struct mpentry * mpe = VECTOR_LAST_SLOT(conf->mptable);		\
e71b65
+	if (!mpe)							\
e71b65
+		return 1;						\
e71b65
+	return set_arg_str(strvec, &mpe->option, count_idx, file, line_nr); \
e71b65
+}
e71b65
+
e71b65
 #define declare_mp_snprint(option, function)				\
e71b65
 static int								\
e71b65
 snprint_mp_ ## option (struct config *conf, struct strbuf *buff,	\
e71b65
@@ -591,13 +683,13 @@ snprint_def_marginal_pathgroups(struct config *conf, struct strbuf *buff,
e71b65
 }
e71b65
 
e71b65
 
e71b65
-declare_def_handler(selector, set_str)
e71b65
+declare_def_arg_str_handler(selector, 1)
e71b65
 declare_def_snprint_defstr(selector, print_str, DEFAULT_SELECTOR)
e71b65
-declare_hw_handler(selector, set_str)
e71b65
+declare_hw_arg_str_handler(selector, 1)
e71b65
 declare_hw_snprint(selector, print_str)
e71b65
-declare_ovr_handler(selector, set_str)
e71b65
+declare_ovr_arg_str_handler(selector, 1)
e71b65
 declare_ovr_snprint(selector, print_str)
e71b65
-declare_mp_handler(selector, set_str)
e71b65
+declare_mp_arg_str_handler(selector, 1)
e71b65
 declare_mp_snprint(selector, print_str)
e71b65
 
e71b65
 static int snprint_uid_attrs(struct config *conf, struct strbuf *buff,
e71b65
@@ -672,13 +764,13 @@ declare_hw_snprint(prio_args, print_str)
e71b65
 declare_mp_handler(prio_args, set_str)
e71b65
 declare_mp_snprint(prio_args, print_str)
e71b65
 
e71b65
-declare_def_handler(features, set_str)
e71b65
+declare_def_arg_str_handler(features, 0)
e71b65
 declare_def_snprint_defstr(features, print_str, DEFAULT_FEATURES)
e71b65
-declare_ovr_handler(features, set_str)
e71b65
+declare_ovr_arg_str_handler(features, 0)
e71b65
 declare_ovr_snprint(features, print_str)
e71b65
-declare_hw_handler(features, set_str)
e71b65
+declare_hw_arg_str_handler(features, 0)
e71b65
 declare_hw_snprint(features, print_str)
e71b65
-declare_mp_handler(features, set_str)
e71b65
+declare_mp_arg_str_handler(features, 0)
e71b65
 declare_mp_snprint(features, print_str)
e71b65
 
e71b65
 declare_def_handler(checker_name, set_str)
e71b65
@@ -1857,7 +1949,7 @@ declare_hw_snprint(revision, print_str)
e71b65
 declare_hw_handler(bl_product, set_str)
e71b65
 declare_hw_snprint(bl_product, print_str)
e71b65
 
e71b65
-declare_hw_handler(hwhandler, set_str)
e71b65
+declare_hw_arg_str_handler(hwhandler, 0)
e71b65
 declare_hw_snprint(hwhandler, print_str)
e71b65
 
e71b65
 /*