Blame SOURCES/0083-libmultipath-improve-checks-for-set_str.patch

c4b4b8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
c4b4b8
From: Benjamin Marzinski <bmarzins@redhat.com>
c4b4b8
Date: Wed, 29 Sep 2021 12:56:04 -0500
c4b4b8
Subject: [PATCH] libmultipath: improve checks for set_str
c4b4b8
c4b4b8
multipath always requires absolute pathnames, so make sure all file and
c4b4b8
directory names start with a slash.  Also check that the directories
c4b4b8
exist.  Finally, some strings, like the alias, will be used in paths.
c4b4b8
These must not contain the slash character '/', since it is a forbidden
c4b4b8
character in file/directory names. This patch adds seperate handlers for
c4b4b8
these three cases. If a config line is invalid, these handlers retain
c4b4b8
the existing config string, if any.
c4b4b8
c4b4b8
Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
c4b4b8
---
c4b4b8
 libmultipath/dict.c | 89 +++++++++++++++++++++++++++++++++++++++------
c4b4b8
 1 file changed, 78 insertions(+), 11 deletions(-)
c4b4b8
c4b4b8
diff --git a/libmultipath/dict.c b/libmultipath/dict.c
c4b4b8
index 686f4d5c..d547d898 100644
c4b4b8
--- a/libmultipath/dict.c
c4b4b8
+++ b/libmultipath/dict.c
c4b4b8
@@ -5,6 +5,8 @@
c4b4b8
  * Copyright (c) 2005 Kiyoshi Ueda, NEC
c4b4b8
  */
c4b4b8
 #include <sys/types.h>
c4b4b8
+#include <sys/stat.h>
c4b4b8
+#include <unistd.h>
c4b4b8
 #include <pwd.h>
c4b4b8
 #include <string.h>
c4b4b8
 #include "checkers.h"
c4b4b8
@@ -121,7 +123,72 @@ set_regex(vector strvec, void *ptr, const char *file, int line_nr)
c4b4b8
 
c4b4b8
 	if (!*str_ptr)
c4b4b8
 		return 1;
c4b4b8
+	return 0;
c4b4b8
+}
c4b4b8
 
c4b4b8
+static int
c4b4b8
+set_dir(vector strvec, void *ptr, const char *file, int line_nr)
c4b4b8
+{
c4b4b8
+	char **str_ptr = (char **)ptr;
c4b4b8
+	char *old_str = *str_ptr;
c4b4b8
+	struct stat sb;
c4b4b8
+
c4b4b8
+	*str_ptr = set_value(strvec);
c4b4b8
+	if (!*str_ptr) {
c4b4b8
+		free(old_str);
c4b4b8
+		return 1;
c4b4b8
+	}
c4b4b8
+	if ((*str_ptr)[0] != '/'){
c4b4b8
+		condlog(1, "%s line %d, %s is not an absolute directory path. Ignoring", file, line_nr, *str_ptr);
c4b4b8
+		*str_ptr = old_str;
c4b4b8
+	} else {
c4b4b8
+		if (stat(*str_ptr, &sb) == 0 && S_ISDIR(sb.st_mode))
c4b4b8
+			free(old_str);
c4b4b8
+		else {
c4b4b8
+			condlog(1, "%s line %d, %s is not an existing directory. Ignoring", file, line_nr, *str_ptr);
c4b4b8
+			*str_ptr = old_str;
c4b4b8
+		}
c4b4b8
+	}
c4b4b8
+	return 0;
c4b4b8
+}
c4b4b8
+
c4b4b8
+static int
c4b4b8
+set_path(vector strvec, void *ptr, const char *file, int line_nr)
c4b4b8
+{
c4b4b8
+	char **str_ptr = (char **)ptr;
c4b4b8
+	char *old_str = *str_ptr;
c4b4b8
+
c4b4b8
+	*str_ptr = set_value(strvec);
c4b4b8
+	if (!*str_ptr) {
c4b4b8
+		free(old_str);
c4b4b8
+		return 1;
c4b4b8
+	}
c4b4b8
+	if ((*str_ptr)[0] != '/'){
c4b4b8
+		condlog(1, "%s line %d, %s is not an absolute path. Ignoring",
c4b4b8
+			file, line_nr, *str_ptr);
c4b4b8
+		*str_ptr = old_str;
c4b4b8
+	} else
c4b4b8
+		free(old_str);
c4b4b8
+	return 0;
c4b4b8
+}
c4b4b8
+
c4b4b8
+static int
c4b4b8
+set_str_noslash(vector strvec, void *ptr, const char *file, int line_nr)
c4b4b8
+{
c4b4b8
+	char **str_ptr = (char **)ptr;
c4b4b8
+	char *old_str = *str_ptr;
c4b4b8
+
c4b4b8
+	*str_ptr = set_value(strvec);
c4b4b8
+	if (!*str_ptr) {
c4b4b8
+		free(old_str);
c4b4b8
+		return 1;
c4b4b8
+	}
c4b4b8
+	if (strchr(*str_ptr, '/')) {
c4b4b8
+		condlog(1, "%s line %d, %s cannot contain a slash. Ignoring",
c4b4b8
+			file, line_nr, *str_ptr);
c4b4b8
+		*str_ptr = old_str;
c4b4b8
+	} else
c4b4b8
+		free(old_str);
c4b4b8
 	return 0;
c4b4b8
 }
c4b4b8
 
c4b4b8
@@ -400,19 +467,19 @@ 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_range_handler(verbosity, 0, MAX_VERBOSITY)
c4b4b8
+declare_def_range_handler(verbosity, 0, 4)
c4b4b8
 declare_def_snprint(verbosity, print_int)
c4b4b8
 
c4b4b8
 declare_def_handler(reassign_maps, set_yes_no)
c4b4b8
 declare_def_snprint(reassign_maps, print_yes_no)
c4b4b8
 
c4b4b8
-declare_def_handler(multipath_dir, set_str)
c4b4b8
+declare_def_handler(multipath_dir, set_dir)
c4b4b8
 declare_def_snprint(multipath_dir, print_str)
c4b4b8
 
c4b4b8
 static int def_partition_delim_handler(struct config *conf, vector strvec,
c4b4b8
 				       const char *file, int line_nr)
c4b4b8
 {
c4b4b8
-	int rc = set_str(strvec, &conf->partition_delim, file, line_nr);
c4b4b8
+	int rc = set_str_noslash(strvec, &conf->partition_delim, file, line_nr);
c4b4b8
 
c4b4b8
 	if (rc != 0)
c4b4b8
 		return rc;
c4b4b8
@@ -545,11 +612,11 @@ declare_hw_snprint(prio_name, print_str)
c4b4b8
 declare_mp_handler(prio_name, set_str)
c4b4b8
 declare_mp_snprint(prio_name, print_str)
c4b4b8
 
c4b4b8
-declare_def_handler(alias_prefix, set_str)
c4b4b8
+declare_def_handler(alias_prefix, set_str_noslash)
c4b4b8
 declare_def_snprint_defstr(alias_prefix, print_str, DEFAULT_ALIAS_PREFIX)
c4b4b8
-declare_ovr_handler(alias_prefix, set_str)
c4b4b8
+declare_ovr_handler(alias_prefix, set_str_noslash)
c4b4b8
 declare_ovr_snprint(alias_prefix, print_str)
c4b4b8
-declare_hw_handler(alias_prefix, set_str)
c4b4b8
+declare_hw_handler(alias_prefix, set_str_noslash)
c4b4b8
 declare_hw_snprint(alias_prefix, print_str)
c4b4b8
 
c4b4b8
 declare_def_handler(prio_args, set_str)
c4b4b8
@@ -633,13 +700,13 @@ declare_hw_snprint(user_friendly_names, print_yes_no_undef)
c4b4b8
 declare_mp_handler(user_friendly_names, set_yes_no_undef)
c4b4b8
 declare_mp_snprint(user_friendly_names, print_yes_no_undef)
c4b4b8
 
c4b4b8
-declare_def_handler(bindings_file, set_str)
c4b4b8
+declare_def_handler(bindings_file, set_path)
c4b4b8
 declare_def_snprint(bindings_file, print_str)
c4b4b8
 
c4b4b8
-declare_def_handler(wwids_file, set_str)
c4b4b8
+declare_def_handler(wwids_file, set_path)
c4b4b8
 declare_def_snprint(wwids_file, print_str)
c4b4b8
 
c4b4b8
-declare_def_handler(prkeys_file, set_str)
c4b4b8
+declare_def_handler(prkeys_file, set_path)
c4b4b8
 declare_def_snprint(prkeys_file, print_str)
c4b4b8
 
c4b4b8
 declare_def_handler(retain_hwhandler, set_yes_no_undef)
c4b4b8
@@ -738,7 +805,7 @@ def_config_dir_handler(struct config *conf, vector strvec, const char *file,
c4b4b8
 	/* this is only valid in the main config file */
c4b4b8
 	if (conf->processed_main_config)
c4b4b8
 		return 0;
c4b4b8
-	return set_str(strvec, &conf->config_dir, file, line_nr);
c4b4b8
+	return set_path(strvec, &conf->config_dir, file, line_nr);
c4b4b8
 }
c4b4b8
 declare_def_snprint(config_dir, print_str)
c4b4b8
 
c4b4b8
@@ -1781,7 +1848,7 @@ multipath_handler(struct config *conf, vector strvec, const char *file,
c4b4b8
 declare_mp_handler(wwid, set_str)
c4b4b8
 declare_mp_snprint(wwid, print_str)
c4b4b8
 
c4b4b8
-declare_mp_handler(alias, set_str)
c4b4b8
+declare_mp_handler(alias, set_str_noslash)
c4b4b8
 declare_mp_snprint(alias, print_str)
c4b4b8
 
c4b4b8
 /*