Blame SOURCES/0087-RHBZ-1110013-config-error-checking.patch

38852f
---
38852f
 libmultipath/parser.c |  154 ++++++++++++++++++++++++++++++++++++++++----------
38852f
 1 file changed, 126 insertions(+), 28 deletions(-)
38852f
38852f
Index: multipath-tools-130222/libmultipath/parser.c
38852f
===================================================================
38852f
--- multipath-tools-130222.orig/libmultipath/parser.c
38852f
+++ multipath-tools-130222/libmultipath/parser.c
38852f
@@ -395,36 +395,57 @@ set_value(vector strvec)
38852f
 	char *alloc = NULL;
38852f
 	char *tmp;
38852f
 
38852f
-	if (!str)
38852f
+	if (!str) {
38852f
+		condlog(0, "option '%s' missing value",
38852f
+			(char *)VECTOR_SLOT(strvec, 0));
38852f
 		return NULL;
38852f
-
38852f
+	}
38852f
 	size = strlen(str);
38852f
-	if (size == 0)
38852f
+	if (size == 0) {
38852f
+		condlog(0, "option '%s' has empty value",
38852f
+			(char *)VECTOR_SLOT(strvec, 0));
38852f
 		return NULL;
38852f
-
38852f
-	if (*str == '"') {
38852f
-		for (i = 2; i < VECTOR_SIZE(strvec); i++) {
38852f
-			str = VECTOR_SLOT(strvec, i);
38852f
-			len += strlen(str);
38852f
-			if (!alloc)
38852f
-				alloc =
38852f
-				    (char *) MALLOC(sizeof (char *) *
38852f
-						    (len + 1));
38852f
-			else {
38852f
-				alloc =
38852f
-				    REALLOC(alloc, sizeof (char *) * (len + 1));
38852f
-				tmp = VECTOR_SLOT(strvec, i-1);
38852f
-				if (alloc && *str != '"' && *tmp != '"')
38852f
-					strncat(alloc, " ", 1);
38852f
-			}
38852f
-
38852f
-			if (alloc && i != VECTOR_SIZE(strvec)-1)
38852f
-				strncat(alloc, str, strlen(str));
38852f
-		}
38852f
-	} else {
38852f
-		alloc = MALLOC(sizeof (char *) * (size + 1));
38852f
+	}
38852f
+	if (*str != '"') {
38852f
+		alloc = MALLOC(sizeof (char) * (size + 1));
38852f
 		if (alloc)
38852f
 			memcpy(alloc, str, size);
38852f
+		else
38852f
+			condlog(0, "can't allocate memeory for option '%s'",
38852f
+				(char *)VECTOR_SLOT(strvec, 0));
38852f
+		return alloc;
38852f
+	}
38852f
+	/* Even empty quotes counts as a value (An empty string) */
38852f
+	alloc = (char *) MALLOC(sizeof (char));
38852f
+	if (!alloc) {
38852f
+		condlog(0, "can't allocate memeory for option '%s'",
38852f
+			(char *)VECTOR_SLOT(strvec, 0));
38852f
+		return NULL;
38852f
+	}
38852f
+	for (i = 2; i < VECTOR_SIZE(strvec); i++) {
38852f
+		str = VECTOR_SLOT(strvec, i);
38852f
+		if (!str) {
38852f
+			free(alloc);
38852f
+			condlog(0, "parse error for option '%s'",
38852f
+				(char *)VECTOR_SLOT(strvec, 0));
38852f
+			return NULL;
38852f
+		}
38852f
+		if (*str == '"')
38852f
+			break;
38852f
+		tmp = alloc;
38852f
+		/* The first +1 is for the NULL byte. The rest are for the
38852f
+		 * spaces between words */
38852f
+		len += strlen(str) + 1;
38852f
+		alloc = REALLOC(alloc, sizeof (char) * len);
38852f
+		if (!alloc) {
38852f
+			FREE(tmp);
38852f
+			condlog(0, "can't allocate memeory for option '%s'",
38852f
+				(char *)VECTOR_SLOT(strvec, 0));
38852f
+			return NULL;
38852f
+		}
38852f
+		if (*alloc != '\0')
38852f
+			strncat(alloc, " ", 1);
38852f
+		strncat(alloc, str, strlen(str));
38852f
 	}
38852f
 	return alloc;
38852f
 }
38852f
@@ -465,6 +486,74 @@ void free_uniques(vector uniques)
38852f
 }
38852f
 
38852f
 int
38852f
+is_sublevel_keyword(char *str)
38852f
+{
38852f
+	return (strcmp(str, "defaults") == 0 || strcmp(str, "blacklist") == 0 ||
38852f
+		strcmp(str, "blacklist_exceptions") == 0 ||
38852f
+		strcmp(str, "devices") == 0 || strcmp(str, "devices") == 0 ||
38852f
+		strcmp(str, "device") == 0 || strcmp(str, "multipaths") == 0 ||
38852f
+		strcmp(str, "multipath") == 0);
38852f
+}
38852f
+
38852f
+int
38852f
+validate_config_strvec(vector strvec)
38852f
+{
38852f
+	char *str;
38852f
+	int i;
38852f
+
38852f
+	str = VECTOR_SLOT(strvec, 0);
38852f
+	if (str == NULL) {
38852f
+		condlog(0, "can't parse option on line %d of config file",
38852f
+			line_nr);
38852f
+	return -1;
38852f
+	}
38852f
+	if (*str == '}') {
38852f
+		if (VECTOR_SIZE(strvec) > 1)
38852f
+			condlog(0, "ignoring extra data starting with '%s' on line %d of config file", (char *)VECTOR_SLOT(strvec, 1), line_nr);
38852f
+		return 0;
38852f
+	}
38852f
+	if (*str == '{') {
38852f
+		condlog(0, "invalid keyword '%s' on line %d of config file", str, line_nr);
38852f
+		return -1;
38852f
+	}
38852f
+	if (is_sublevel_keyword(str)) {
38852f
+		str = VECTOR_SLOT(strvec, 1);
38852f
+		if (str == NULL)
38852f
+			condlog(0, "missing '{' on line %d of config file", line_nr);
38852f
+		else if (*str != '{')
38852f
+			condlog(0, "expecting '{' on line %d of config file. found '%s'", line_nr, str);
38852f
+		else if (VECTOR_SIZE(strvec) > 2)
38852f
+			condlog(0, "ignoring extra data starting with '%s' on line %d of config file", (char *)VECTOR_SLOT(strvec, 2), line_nr);
38852f
+		return 0;
38852f
+	}
38852f
+	str = VECTOR_SLOT(strvec, 1);
38852f
+	if (str == NULL) {
38852f
+		condlog(0, "missing value for option '%s' on line %d of config file", (char *)VECTOR_SLOT(strvec, 0), line_nr);
38852f
+		return -1;
38852f
+	}
38852f
+	if (*str != '"') {
38852f
+		if (VECTOR_SIZE(strvec) > 2)
38852f
+			condlog(0, "ignoring extra data starting with '%s' on line %d of config file", (char *)VECTOR_SLOT(strvec, 2), line_nr);
38852f
+		return 0;
38852f
+	}
38852f
+	for (i = 2; i < VECTOR_SIZE(strvec); i++) {
38852f
+		str = VECTOR_SLOT(strvec, i);
38852f
+		if (str == NULL) {
38852f
+			condlog(0, "can't parse value on line %d of config file", line_nr);
38852f
+			return -1;
38852f
+		}
38852f
+		if (*str == '"') {
38852f
+			if (VECTOR_SIZE(strvec) > i + 1)
38852f
+				condlog(0, "ignoring extra data starting with '%s' on line %d of config file", (char *)VECTOR_SLOT(strvec, (i + 1)), line_nr);
38852f
+			return 0;
38852f
+		}
38852f
+	}
38852f
+	condlog(0, "missing closing quotes on line %d of config file",
38852f
+		line_nr);
38852f
+	return 0;
38852f
+}
38852f
+
38852f
+int
38852f
 process_stream(vector keywords)
38852f
 {
38852f
 	int i;
38852f
@@ -494,11 +583,20 @@ process_stream(vector keywords)
38852f
 		if (!strvec)
38852f
 			continue;
38852f
 
38852f
+		if (validate_config_strvec(strvec) != 0) {
38852f
+			free_strvec(strvec);
38852f
+			continue;
38852f
+		}
38852f
+
38852f
 		str = VECTOR_SLOT(strvec, 0);
38852f
 
38852f
-		if (!strcmp(str, EOB) && kw_level > 0) {
38852f
-			free_strvec(strvec);
38852f
-			break;
38852f
+		if (!strcmp(str, EOB)) {
38852f
+			if (kw_level > 0) {
38852f
+				free_strvec(strvec);
38852f
+				break;
38852f
+			}
38852f
+			condlog(0, "unmatched '%s' at line %d of config file",
38852f
+				EOB, line_nr);
38852f
 		}
38852f
 
38852f
 		for (i = 0; i < VECTOR_SIZE(keywords); i++) {