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

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