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

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