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

080173
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
080173
From: Benjamin Marzinski <bmarzins@redhat.com>
080173
Date: Tue, 28 Sep 2021 15:59:19 -0500
080173
Subject: [PATCH] libmultipath: make set_int take a range for valid values
080173
080173
If a value outside of the valid range is passed to set_int, it caps the
080173
value at appropriate limit, and issues a warning.
080173
080173
Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
080173
---
080173
 libmultipath/dict.c | 121 +++++++++++++++++++++++++++-----------------
080173
 1 file changed, 75 insertions(+), 46 deletions(-)
080173
080173
diff --git a/libmultipath/dict.c b/libmultipath/dict.c
080173
index a8872da7..686f4d5c 100644
080173
--- a/libmultipath/dict.c
080173
+++ b/libmultipath/dict.c
080173
@@ -28,7 +28,8 @@
080173
 #include "dict.h"
080173
 
080173
 static int
080173
-set_int(vector strvec, void *ptr, const char *file, int line_nr)
080173
+set_int(vector strvec, void *ptr, int min, int max, const char *file,
080173
+	int line_nr)
080173
 {
080173
 	int *int_ptr = (int *)ptr;
080173
 	char *buff, *eptr;
080173
@@ -43,11 +44,17 @@ set_int(vector strvec, void *ptr, const char *file, int line_nr)
080173
 	if (eptr > buff)
080173
 		while (isspace(*eptr))
080173
 			eptr++;
080173
-	if (*buff == '\0' || *eptr != '\0' || res > INT_MAX || res < INT_MIN) {
080173
-		condlog(1, "%s: invalid value for %s: \"%s\"",
080173
-			__func__, (char*)VECTOR_SLOT(strvec, 0), buff);
080173
+	if (*buff == '\0' || *eptr != '\0') {
080173
+		condlog(1, "%s line %d, invalid value for %s: \"%s\"",
080173
+			file, line_nr, (char*)VECTOR_SLOT(strvec, 0), buff);
080173
 		rc = 1;
080173
 	} else {
080173
+		if (res > max || res < min) {
080173
+			res = (res > max) ? max : min;
080173
+			condlog(1, "%s line %d, value for %s too %s, capping at %ld",
080173
+			file, line_nr, (char*)VECTOR_SLOT(strvec, 0),
080173
+			(res == max)? "large" : "small", res);
080173
+		}
080173
 		rc = 0;
080173
 		*int_ptr = res;
080173
 	}
080173
@@ -76,8 +83,8 @@ set_uint(vector strvec, void *ptr, const char *file, int line_nr)
080173
 		while (isspace(*eptr))
080173
 			eptr++;
080173
 	if (*buff == '\0' || *eptr != '\0' || !isdigit(*p) || res > UINT_MAX) {
080173
-		condlog(1, "%s: invalid value for %s: \"%s\"",
080173
-			__func__, (char*)VECTOR_SLOT(strvec, 0), buff);
080173
+		condlog(1, "%s line %d, invalid value for %s: \"%s\"",
080173
+			file, line_nr, (char*)VECTOR_SLOT(strvec, 0), buff);
080173
 		rc = 1;
080173
 	} else {
080173
 		rc = 0;
080173
@@ -246,6 +253,14 @@ def_ ## option ## _handler (struct config *conf, vector strvec,		\
080173
 	return function (strvec, &conf->option, file, line_nr);		\
080173
 }
080173
 
080173
+#define declare_def_range_handler(option, minval, maxval)			\
080173
+static int								\
080173
+def_ ## option ## _handler (struct config *conf, vector strvec,         \
080173
+			    const char *file, int line_nr)		\
080173
+{									\
080173
+	return set_int(strvec, &conf->option, minval, maxval, file, line_nr); \
080173
+}
080173
+
080173
 #define declare_def_snprint(option, function)				\
080173
 static int								\
080173
 snprint_def_ ## option (struct config *conf, char * buff, int len,	\
080173
@@ -287,6 +302,18 @@ hw_ ## option ## _handler (struct config *conf, vector strvec,		\
080173
 	return function (strvec, &hwe->option, file, line_nr);		\
080173
 }
080173
 
080173
+#define declare_hw_range_handler(option, minval, maxval)		\
080173
+static int								\
080173
+hw_ ## option ## _handler (struct config *conf, vector strvec,		\
080173
+			   const char *file, int line_nr)		\
080173
+{									\
080173
+	struct hwentry * hwe = VECTOR_LAST_SLOT(conf->hwtable);		\
080173
+	if (!hwe)							\
080173
+		return 1;						\
080173
+	return set_int(strvec, &hwe->option, minval, maxval, file, line_nr); \
080173
+}
080173
+
080173
+
080173
 #define declare_hw_snprint(option, function)				\
080173
 static int								\
080173
 snprint_hw_ ## option (struct config *conf, char * buff, int len,	\
080173
@@ -306,6 +333,17 @@ ovr_ ## option ## _handler (struct config *conf, vector strvec,		\
080173
 	return function (strvec, &conf->overrides->option, file, line_nr); \
080173
 }
080173
 
080173
+#define declare_ovr_range_handler(option, minval, maxval)		\
080173
+static int								\
080173
+ovr_ ## option ## _handler (struct config *conf, vector strvec,		\
080173
+			    const char *file, int line_nr)		\
080173
+{									\
080173
+	if (!conf->overrides)						\
080173
+		return 1;						\
080173
+	return set_int(strvec, &conf->overrides->option, minval, maxval, \
080173
+		       file, line_nr); \
080173
+}
080173
+
080173
 #define declare_ovr_snprint(option, function)				\
080173
 static int								\
080173
 snprint_ovr_ ## option (struct config *conf, char * buff, int len,	\
080173
@@ -325,6 +363,17 @@ mp_ ## option ## _handler (struct config *conf, vector strvec,		\
080173
 	return function (strvec, &mpe->option, file, line_nr);		\
080173
 }
080173
 
080173
+#define declare_mp_range_handler(option, minval, maxval)		\
080173
+static int								\
080173
+mp_ ## option ## _handler (struct config *conf, vector strvec,		\
080173
+			   const char *file, int line_nr)		\
080173
+{									\
080173
+	struct mpentry * mpe = VECTOR_LAST_SLOT(conf->mptable);		\
080173
+	if (!mpe)							\
080173
+		return 1;						\
080173
+	return set_int(strvec, &mpe->option, minval, maxval, file, line_nr); \
080173
+}
080173
+
080173
 #define declare_mp_snprint(option, function)				\
080173
 static int								\
080173
 snprint_mp_ ## option (struct config *conf, char * buff, int len,	\
080173
@@ -351,7 +400,7 @@ declare_def_snprint(checkint, print_int)
080173
 declare_def_handler(max_checkint, set_uint)
080173
 declare_def_snprint(max_checkint, print_int)
080173
 
080173
-declare_def_handler(verbosity, set_int)
080173
+declare_def_range_handler(verbosity, 0, MAX_VERBOSITY)
080173
 declare_def_snprint(verbosity, print_int)
080173
 
080173
 declare_def_handler(reassign_maps, set_yes_no)
080173
@@ -528,22 +577,22 @@ declare_ovr_snprint(checker_name, print_str)
080173
 declare_hw_handler(checker_name, set_str)
080173
 declare_hw_snprint(checker_name, print_str)
080173
 
080173
-declare_def_handler(minio, set_int)
080173
+declare_def_range_handler(minio, 0, INT_MAX)
080173
 declare_def_snprint_defint(minio, print_int, DEFAULT_MINIO)
080173
-declare_ovr_handler(minio, set_int)
080173
+declare_ovr_range_handler(minio, 0, INT_MAX)
080173
 declare_ovr_snprint(minio, print_nonzero)
080173
-declare_hw_handler(minio, set_int)
080173
+declare_hw_range_handler(minio, 0, INT_MAX)
080173
 declare_hw_snprint(minio, print_nonzero)
080173
-declare_mp_handler(minio, set_int)
080173
+declare_mp_range_handler(minio, 0, INT_MAX)
080173
 declare_mp_snprint(minio, print_nonzero)
080173
 
080173
-declare_def_handler(minio_rq, set_int)
080173
+declare_def_range_handler(minio_rq, 0, INT_MAX)
080173
 declare_def_snprint_defint(minio_rq, print_int, DEFAULT_MINIO_RQ)
080173
-declare_ovr_handler(minio_rq, set_int)
080173
+declare_ovr_range_handler(minio_rq, 0, INT_MAX)
080173
 declare_ovr_snprint(minio_rq, print_nonzero)
080173
-declare_hw_handler(minio_rq, set_int)
080173
+declare_hw_range_handler(minio_rq, 0, INT_MAX)
080173
 declare_hw_snprint(minio_rq, print_nonzero)
080173
-declare_mp_handler(minio_rq, set_int)
080173
+declare_mp_range_handler(minio_rq, 0, INT_MAX)
080173
 declare_mp_snprint(minio_rq, print_nonzero)
080173
 
080173
 declare_def_handler(queue_without_daemon, set_yes_no)
080173
@@ -562,7 +611,7 @@ snprint_def_queue_without_daemon (struct config *conf,
080173
 	return 0;
080173
 }
080173
 
080173
-declare_def_handler(checker_timeout, set_int)
080173
+declare_def_range_handler(checker_timeout, 0, INT_MAX)
080173
 declare_def_snprint(checker_timeout, print_nonzero)
080173
 
080173
 declare_def_handler(flush_on_last_del, set_yes_no_undef)
080173
@@ -630,13 +679,13 @@ declare_hw_snprint(deferred_remove, print_yes_no_undef)
080173
 declare_mp_handler(deferred_remove, set_yes_no_undef)
080173
 declare_mp_snprint(deferred_remove, print_yes_no_undef)
080173
 
080173
-declare_def_handler(retrigger_tries, set_int)
080173
+declare_def_range_handler(retrigger_tries, 0, INT_MAX)
080173
 declare_def_snprint(retrigger_tries, print_int)
080173
 
080173
-declare_def_handler(retrigger_delay, set_int)
080173
+declare_def_range_handler(retrigger_delay, 0, INT_MAX)
080173
 declare_def_snprint(retrigger_delay, print_int)
080173
 
080173
-declare_def_handler(uev_wait_timeout, set_int)
080173
+declare_def_range_handler(uev_wait_timeout, 0, INT_MAX)
080173
 declare_def_snprint(uev_wait_timeout, print_int)
080173
 
080173
 declare_def_handler(strict_timing, set_yes_no)
080173
@@ -662,19 +711,19 @@ static int snprint_def_disable_changed_wwids(struct config *conf, char *buff,
080173
 	return print_ignored(buff, len);
080173
 }
080173
 
080173
-declare_def_handler(remove_retries, set_int)
080173
+declare_def_range_handler(remove_retries, 0, INT_MAX)
080173
 declare_def_snprint(remove_retries, print_int)
080173
 
080173
-declare_def_handler(max_sectors_kb, set_int)
080173
+declare_def_range_handler(max_sectors_kb, 0, INT_MAX)
080173
 declare_def_snprint(max_sectors_kb, print_nonzero)
080173
-declare_ovr_handler(max_sectors_kb, set_int)
080173
+declare_ovr_range_handler(max_sectors_kb, 0, INT_MAX)
080173
 declare_ovr_snprint(max_sectors_kb, print_nonzero)
080173
-declare_hw_handler(max_sectors_kb, set_int)
080173
+declare_hw_range_handler(max_sectors_kb, 0, INT_MAX)
080173
 declare_hw_snprint(max_sectors_kb, print_nonzero)
080173
-declare_mp_handler(max_sectors_kb, set_int)
080173
+declare_mp_range_handler(max_sectors_kb, 0, INT_MAX)
080173
 declare_mp_snprint(max_sectors_kb, print_nonzero)
080173
 
080173
-declare_def_handler(find_multipaths_timeout, set_int)
080173
+declare_def_range_handler(find_multipaths_timeout, INT_MIN, INT_MAX)
080173
 declare_def_snprint_defint(find_multipaths_timeout, print_int,
080173
 			   DEFAULT_FIND_MULTIPATHS_TIMEOUT)
080173
 
080173
@@ -1437,27 +1486,7 @@ declare_ovr_snprint(recheck_wwid, print_yes_no_undef)
080173
 declare_hw_handler(recheck_wwid, set_yes_no_undef)
080173
 declare_hw_snprint(recheck_wwid, print_yes_no_undef)
080173
 
080173
-
080173
-static int
080173
-def_uxsock_timeout_handler(struct config *conf, vector strvec, const char *file,
080173
-			   int line_nr)
080173
-{
080173
-	unsigned int uxsock_timeout;
080173
-	char *buff;
080173
-
080173
-	buff = set_value(strvec);
080173
-	if (!buff)
080173
-		return 1;
080173
-
080173
-	if (sscanf(buff, "%u", &uxsock_timeout) == 1 &&
080173
-	    uxsock_timeout > DEFAULT_REPLY_TIMEOUT)
080173
-		conf->uxsock_timeout = uxsock_timeout;
080173
-	else
080173
-		conf->uxsock_timeout = DEFAULT_REPLY_TIMEOUT;
080173
-
080173
-	free(buff);
080173
-	return 0;
080173
-}
080173
+declare_def_range_handler(uxsock_timeout, DEFAULT_REPLY_TIMEOUT, INT_MAX)
080173
 
080173
 static int
080173
 hw_vpd_vendor_handler(struct config *conf, vector strvec, const char *file,