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

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