Blame SOURCES/0027-libmultipath-make-set_int-take-a-range-for-valid-val.patch

ebef98
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
ebef98
From: Benjamin Marzinski <bmarzins@redhat.com>
ebef98
Date: Tue, 28 Sep 2021 15:59:19 -0500
ebef98
Subject: [PATCH] libmultipath: make set_int take a range for valid values
ebef98
ebef98
If a value outside of the valid range is passed to set_int, it caps the
ebef98
value at appropriate limit, and issues a warning.
ebef98
ebef98
Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
ebef98
---
ebef98
 libmultipath/dict.c | 121 +++++++++++++++++++++++++++-----------------
ebef98
 1 file changed, 75 insertions(+), 46 deletions(-)
ebef98
ebef98
diff --git a/libmultipath/dict.c b/libmultipath/dict.c
ebef98
index eb2c44c0..57b6a7b6 100644
ebef98
--- a/libmultipath/dict.c
ebef98
+++ b/libmultipath/dict.c
ebef98
@@ -29,7 +29,8 @@
ebef98
 #include "strbuf.h"
ebef98
 
ebef98
 static int
ebef98
-set_int(vector strvec, void *ptr, const char *file, int line_nr)
ebef98
+set_int(vector strvec, void *ptr, int min, int max, const char *file,
ebef98
+	int line_nr)
ebef98
 {
ebef98
 	int *int_ptr = (int *)ptr;
ebef98
 	char *buff, *eptr;
ebef98
@@ -44,11 +45,17 @@ set_int(vector strvec, void *ptr, const char *file, int line_nr)
ebef98
 	if (eptr > buff)
ebef98
 		while (isspace(*eptr))
ebef98
 			eptr++;
ebef98
-	if (*buff == '\0' || *eptr != '\0' || res > INT_MAX || res < INT_MIN) {
ebef98
-		condlog(1, "%s: invalid value for %s: \"%s\"",
ebef98
-			__func__, (char*)VECTOR_SLOT(strvec, 0), buff);
ebef98
+	if (*buff == '\0' || *eptr != '\0') {
ebef98
+		condlog(1, "%s line %d, invalid value for %s: \"%s\"",
ebef98
+			file, line_nr, (char*)VECTOR_SLOT(strvec, 0), buff);
ebef98
 		rc = 1;
ebef98
 	} else {
ebef98
+		if (res > max || res < min) {
ebef98
+			res = (res > max) ? max : min;
ebef98
+			condlog(1, "%s line %d, value for %s too %s, capping at %ld",
ebef98
+			file, line_nr, (char*)VECTOR_SLOT(strvec, 0),
ebef98
+			(res == max)? "large" : "small", res);
ebef98
+		}
ebef98
 		rc = 0;
ebef98
 		*int_ptr = res;
ebef98
 	}
ebef98
@@ -77,8 +84,8 @@ set_uint(vector strvec, void *ptr, const char *file, int line_nr)
ebef98
 		while (isspace(*eptr))
ebef98
 			eptr++;
ebef98
 	if (*buff == '\0' || *eptr != '\0' || !isdigit(*p) || res > UINT_MAX) {
ebef98
-		condlog(1, "%s: invalid value for %s: \"%s\"",
ebef98
-			__func__, (char*)VECTOR_SLOT(strvec, 0), buff);
ebef98
+		condlog(1, "%s line %d, invalid value for %s: \"%s\"",
ebef98
+			file, line_nr, (char*)VECTOR_SLOT(strvec, 0), buff);
ebef98
 		rc = 1;
ebef98
 	} else {
ebef98
 		rc = 0;
ebef98
@@ -193,6 +200,14 @@ def_ ## option ## _handler (struct config *conf, vector strvec,		\
ebef98
 	return function (strvec, &conf->option, file, line_nr);		\
ebef98
 }
ebef98
 
ebef98
+#define declare_def_range_handler(option, minval, maxval)			\
ebef98
+static int								\
ebef98
+def_ ## option ## _handler (struct config *conf, vector strvec,         \
ebef98
+			    const char *file, int line_nr)		\
ebef98
+{									\
ebef98
+	return set_int(strvec, &conf->option, minval, maxval, file, line_nr); \
ebef98
+}
ebef98
+
ebef98
 #define declare_def_snprint(option, function)				\
ebef98
 static int								\
ebef98
 snprint_def_ ## option (struct config *conf, struct strbuf *buff,	\
ebef98
@@ -234,6 +249,18 @@ hw_ ## option ## _handler (struct config *conf, vector strvec,		\
ebef98
 	return function (strvec, &hwe->option, file, line_nr);		\
ebef98
 }
ebef98
 
ebef98
+#define declare_hw_range_handler(option, minval, maxval)		\
ebef98
+static int								\
ebef98
+hw_ ## option ## _handler (struct config *conf, vector strvec,		\
ebef98
+			   const char *file, int line_nr)		\
ebef98
+{									\
ebef98
+	struct hwentry * hwe = VECTOR_LAST_SLOT(conf->hwtable);		\
ebef98
+	if (!hwe)							\
ebef98
+		return 1;						\
ebef98
+	return set_int(strvec, &hwe->option, minval, maxval, file, line_nr); \
ebef98
+}
ebef98
+
ebef98
+
ebef98
 #define declare_hw_snprint(option, function)				\
ebef98
 static int								\
ebef98
 snprint_hw_ ## option (struct config *conf, struct strbuf *buff,	\
ebef98
@@ -253,6 +280,17 @@ ovr_ ## option ## _handler (struct config *conf, vector strvec,		\
ebef98
 	return function (strvec, &conf->overrides->option, file, line_nr); \
ebef98
 }
ebef98
 
ebef98
+#define declare_ovr_range_handler(option, minval, maxval)		\
ebef98
+static int								\
ebef98
+ovr_ ## option ## _handler (struct config *conf, vector strvec,		\
ebef98
+			    const char *file, int line_nr)		\
ebef98
+{									\
ebef98
+	if (!conf->overrides)						\
ebef98
+		return 1;						\
ebef98
+	return set_int(strvec, &conf->overrides->option, minval, maxval, \
ebef98
+		       file, line_nr); \
ebef98
+}
ebef98
+
ebef98
 #define declare_ovr_snprint(option, function)				\
ebef98
 static int								\
ebef98
 snprint_ovr_ ## option (struct config *conf, struct strbuf *buff,	\
ebef98
@@ -272,6 +310,17 @@ mp_ ## option ## _handler (struct config *conf, vector strvec,		\
ebef98
 	return function (strvec, &mpe->option, file, line_nr);		\
ebef98
 }
ebef98
 
ebef98
+#define declare_mp_range_handler(option, minval, maxval)		\
ebef98
+static int								\
ebef98
+mp_ ## option ## _handler (struct config *conf, vector strvec,		\
ebef98
+			   const char *file, int line_nr)		\
ebef98
+{									\
ebef98
+	struct mpentry * mpe = VECTOR_LAST_SLOT(conf->mptable);		\
ebef98
+	if (!mpe)							\
ebef98
+		return 1;						\
ebef98
+	return set_int(strvec, &mpe->option, minval, maxval, file, line_nr); \
ebef98
+}
ebef98
+
ebef98
 #define declare_mp_snprint(option, function)				\
ebef98
 static int								\
ebef98
 snprint_mp_ ## option (struct config *conf, struct strbuf *buff,	\
ebef98
@@ -298,7 +347,7 @@ declare_def_snprint(checkint, print_int)
ebef98
 declare_def_handler(max_checkint, set_uint)
ebef98
 declare_def_snprint(max_checkint, print_int)
ebef98
 
ebef98
-declare_def_handler(verbosity, set_int)
ebef98
+declare_def_range_handler(verbosity, 0, MAX_VERBOSITY)
ebef98
 declare_def_snprint(verbosity, print_int)
ebef98
 
ebef98
 declare_def_handler(reassign_maps, set_yes_no)
ebef98
@@ -473,22 +522,22 @@ declare_ovr_snprint(checker_name, print_str)
ebef98
 declare_hw_handler(checker_name, set_str)
ebef98
 declare_hw_snprint(checker_name, print_str)
ebef98
 
ebef98
-declare_def_handler(minio, set_int)
ebef98
+declare_def_range_handler(minio, 0, INT_MAX)
ebef98
 declare_def_snprint_defint(minio, print_int, DEFAULT_MINIO)
ebef98
-declare_ovr_handler(minio, set_int)
ebef98
+declare_ovr_range_handler(minio, 0, INT_MAX)
ebef98
 declare_ovr_snprint(minio, print_nonzero)
ebef98
-declare_hw_handler(minio, set_int)
ebef98
+declare_hw_range_handler(minio, 0, INT_MAX)
ebef98
 declare_hw_snprint(minio, print_nonzero)
ebef98
-declare_mp_handler(minio, set_int)
ebef98
+declare_mp_range_handler(minio, 0, INT_MAX)
ebef98
 declare_mp_snprint(minio, print_nonzero)
ebef98
 
ebef98
-declare_def_handler(minio_rq, set_int)
ebef98
+declare_def_range_handler(minio_rq, 0, INT_MAX)
ebef98
 declare_def_snprint_defint(minio_rq, print_int, DEFAULT_MINIO_RQ)
ebef98
-declare_ovr_handler(minio_rq, set_int)
ebef98
+declare_ovr_range_handler(minio_rq, 0, INT_MAX)
ebef98
 declare_ovr_snprint(minio_rq, print_nonzero)
ebef98
-declare_hw_handler(minio_rq, set_int)
ebef98
+declare_hw_range_handler(minio_rq, 0, INT_MAX)
ebef98
 declare_hw_snprint(minio_rq, print_nonzero)
ebef98
-declare_mp_handler(minio_rq, set_int)
ebef98
+declare_mp_range_handler(minio_rq, 0, INT_MAX)
ebef98
 declare_mp_snprint(minio_rq, print_nonzero)
ebef98
 
ebef98
 declare_def_handler(queue_without_daemon, set_yes_no)
ebef98
@@ -512,7 +561,7 @@ snprint_def_queue_without_daemon(struct config *conf, struct strbuf *buff,
ebef98
 	return append_strbuf_quoted(buff, qwd);
ebef98
 }
ebef98
 
ebef98
-declare_def_handler(checker_timeout, set_int)
ebef98
+declare_def_range_handler(checker_timeout, 0, INT_MAX)
ebef98
 declare_def_snprint(checker_timeout, print_nonzero)
ebef98
 
ebef98
 declare_def_handler(allow_usb_devices, set_yes_no)
ebef98
@@ -583,13 +632,13 @@ declare_hw_snprint(deferred_remove, print_yes_no_undef)
ebef98
 declare_mp_handler(deferred_remove, set_yes_no_undef)
ebef98
 declare_mp_snprint(deferred_remove, print_yes_no_undef)
ebef98
 
ebef98
-declare_def_handler(retrigger_tries, set_int)
ebef98
+declare_def_range_handler(retrigger_tries, 0, INT_MAX)
ebef98
 declare_def_snprint(retrigger_tries, print_int)
ebef98
 
ebef98
-declare_def_handler(retrigger_delay, set_int)
ebef98
+declare_def_range_handler(retrigger_delay, 0, INT_MAX)
ebef98
 declare_def_snprint(retrigger_delay, print_int)
ebef98
 
ebef98
-declare_def_handler(uev_wait_timeout, set_int)
ebef98
+declare_def_range_handler(uev_wait_timeout, 0, INT_MAX)
ebef98
 declare_def_snprint(uev_wait_timeout, print_int)
ebef98
 
ebef98
 declare_def_handler(strict_timing, set_yes_no)
ebef98
@@ -616,19 +665,19 @@ static int snprint_def_disable_changed_wwids(struct config *conf,
ebef98
 	return print_ignored(buff);
ebef98
 }
ebef98
 
ebef98
-declare_def_handler(remove_retries, set_int)
ebef98
+declare_def_range_handler(remove_retries, 0, INT_MAX)
ebef98
 declare_def_snprint(remove_retries, print_int)
ebef98
 
ebef98
-declare_def_handler(max_sectors_kb, set_int)
ebef98
+declare_def_range_handler(max_sectors_kb, 0, INT_MAX)
ebef98
 declare_def_snprint(max_sectors_kb, print_nonzero)
ebef98
-declare_ovr_handler(max_sectors_kb, set_int)
ebef98
+declare_ovr_range_handler(max_sectors_kb, 0, INT_MAX)
ebef98
 declare_ovr_snprint(max_sectors_kb, print_nonzero)
ebef98
-declare_hw_handler(max_sectors_kb, set_int)
ebef98
+declare_hw_range_handler(max_sectors_kb, 0, INT_MAX)
ebef98
 declare_hw_snprint(max_sectors_kb, print_nonzero)
ebef98
-declare_mp_handler(max_sectors_kb, set_int)
ebef98
+declare_mp_range_handler(max_sectors_kb, 0, INT_MAX)
ebef98
 declare_mp_snprint(max_sectors_kb, print_nonzero)
ebef98
 
ebef98
-declare_def_handler(find_multipaths_timeout, set_int)
ebef98
+declare_def_range_handler(find_multipaths_timeout, INT_MIN, INT_MAX)
ebef98
 declare_def_snprint_defint(find_multipaths_timeout, print_int,
ebef98
 			   DEFAULT_FIND_MULTIPATHS_TIMEOUT)
ebef98
 
ebef98
@@ -1385,27 +1434,7 @@ declare_ovr_snprint(recheck_wwid, print_yes_no_undef)
ebef98
 declare_hw_handler(recheck_wwid, set_yes_no_undef)
ebef98
 declare_hw_snprint(recheck_wwid, print_yes_no_undef)
ebef98
 
ebef98
-
ebef98
-static int
ebef98
-def_uxsock_timeout_handler(struct config *conf, vector strvec, const char *file,
ebef98
-			   int line_nr)
ebef98
-{
ebef98
-	unsigned int uxsock_timeout;
ebef98
-	char *buff;
ebef98
-
ebef98
-	buff = set_value(strvec);
ebef98
-	if (!buff)
ebef98
-		return 1;
ebef98
-
ebef98
-	if (sscanf(buff, "%u", &uxsock_timeout) == 1 &&
ebef98
-	    uxsock_timeout > DEFAULT_REPLY_TIMEOUT)
ebef98
-		conf->uxsock_timeout = uxsock_timeout;
ebef98
-	else
ebef98
-		conf->uxsock_timeout = DEFAULT_REPLY_TIMEOUT;
ebef98
-
ebef98
-	free(buff);
ebef98
-	return 0;
ebef98
-}
ebef98
+declare_def_range_handler(uxsock_timeout, DEFAULT_REPLY_TIMEOUT, INT_MAX)
ebef98
 
ebef98
 static int
ebef98
 hw_vpd_vendor_handler(struct config *conf, vector strvec, const char *file,