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

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