Просмотр исходного кода

coroparse: Be more strict in what is parsed

Corosync parser is not very clever, but it is able to detect more errors
without too much code.

1. Check if section name is not empty (just '{' character)
2. Check if there is no extra characters after opening bracket '{'
3. Check if there is no extra characters after or before closing bracket
   '}'
4. Check if line is opening section, closing section or key/value

So following examples are reported as error:

totem {
    version: 2
}}}}}}}}}}

Signed-off-by: Jan Friesse <jfriesse@redhat.com>
Reviewed-by: Christine Caulfield <ccaulfie@redhat.com>
Jan Friesse 7 лет назад
Родитель
Сommit
80701845ab
1 измененных файлов с 32 добавлено и 5 удалено
  1. 32 5
      exec/coroparse.c

+ 32 - 5
exec/coroparse.c

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2013 Red Hat, Inc.
+ * Copyright (c) 2006-2018 Red Hat, Inc.
  *
  * All rights reserved.
  *
@@ -347,11 +347,23 @@ static int parse_section(FILE *fp,
 
 		/* New section ? */
 		if ((loc = strchr_rs (line, '{'))) {
-			char *section = remove_whitespace(line, 1);
+			char *section;
+			char *after_section;
 			enum main_cp_cb_data_state newstate;
 
-			loc--;
-			*loc = '\0';
+			*(loc-1) = '\0';
+			section = remove_whitespace(line, 1);
+			after_section = remove_whitespace(loc, 0);
+
+			if (strcmp(section, "") == 0) {
+				*error_string = "parser error: Missing section name before opening bracket '{'";
+				return -1;
+			}
+
+			if (strcmp(after_section, "") != 0) {
+				*error_string = "parser error: Extra characters after opening bracket '{'";
+				return -1;
+			}
 
 			if (strlen(path) + strlen(section) + 1 >= ICMAP_KEYNAME_MAXLEN) {
 				*error_string = "parser error: Start of section makes total cmap path too long";
@@ -402,18 +414,33 @@ static int parse_section(FILE *fp,
 		}
 
 		if (strchr_rs (line, '}')) {
+			char *trimmed_line;
+			trimmed_line = remove_whitespace(line, 0);
+
+			if (strcmp(trimmed_line, "}") != 0) {
+				*error_string = "parser error: extra characters before or after closing bracket '}'";
+				return -1;
+			}
+
 			if (depth == 0) {
 				*error_string = "parser error: Unexpected closing brace";
 
 				return -1;
 			}
 
-			if (!parser_cb(path, NULL, NULL, &state, PARSER_CB_SECTION_END, error_string, config_map, user_data)) {
+			if (!parser_cb(path, NULL, NULL, &state, PARSER_CB_SECTION_END, error_string,
+			    config_map, user_data)) {
 				return -1;
 			}
 
 			return 0;
 		}
+
+		/*
+		 * Line is not opening section, ending section or value -> error
+		 */
+		*error_string = "parser error: Line is not opening or closing section or key value";
+		return -1;
 	}
 
 	if (strcmp(path, "") != 0) {