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

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