Blame SOURCES/0039-Fix-a-case-clang-analyzer-found-where-we-may-try-to-.patch

a43681
From 9819ec7325089d325ff13af3c3d615209f3fb2c9 Mon Sep 17 00:00:00 2001
a43681
From: Peter Jones <pjones@redhat.com>
a43681
Date: Tue, 18 Jun 2019 15:54:58 -0400
a43681
Subject: [PATCH 39/63] Fix a case clang-analyzer found where we may try to
a43681
 parse a NULL
a43681
a43681
I don't think this is something that can *actually* happen - it didn't
a43681
trigger before save_variable() was added, and the save_variable() path
a43681
that calls this calls validate_name() immediately prior to this call.
a43681
validate_name() calls exit() if it's NULL.  But that's weird as well,
a43681
because that's the same pattern all the other users of parse_name() use.
a43681
a43681
Anyway, this patch expands validate_name() and moves it into
a43681
parse_name() so we don't need to call it from everywhere when we're just
a43681
calling the two in a row anyway.
a43681
a43681
Signed-off-by: Peter Jones <pjones@redhat.com>
a43681
---
a43681
 src/efivar.c | 45 ++++++++++++++++++++++++++++++---------------
a43681
 1 file changed, 30 insertions(+), 15 deletions(-)
a43681
a43681
diff --git a/src/efivar.c b/src/efivar.c
a43681
index 885a9af864b..8b1da8888f6 100644
a43681
--- a/src/efivar.c
a43681
+++ b/src/efivar.c
a43681
@@ -95,6 +95,34 @@ show_errors(void)
a43681
 	}
a43681
 }
a43681
 
a43681
+static inline void
a43681
+validate_name(const char *name)
a43681
+{
a43681
+	if (name == NULL) {
a43681
+err:
a43681
+		warnx("Invalid variable name \"%s\"",
a43681
+		      (name == NULL) ? "(null)" : name);
a43681
+		show_errors();
a43681
+		exit(1);
a43681
+	}
a43681
+	if (name[0] == '{') {
a43681
+		const char *next = strchr(name+1, '}');
a43681
+		if (!next)
a43681
+			goto err;
a43681
+		if (next[1] != '-')
a43681
+			goto err;
a43681
+		if (next[2] == '\000')
a43681
+			goto err;
a43681
+	} else {
a43681
+		if (strlen(name) < 38)
a43681
+			goto err;
a43681
+		if (name[8] != '-' || name[13] != '-' ||
a43681
+		    name[18] != '-' || name[23] != '-' ||
a43681
+		    name[36] != '-')
a43681
+			goto err;
a43681
+	}
a43681
+}
a43681
+
a43681
 static void
a43681
 list_all_variables(void)
a43681
 {
a43681
@@ -124,6 +152,8 @@ parse_name(const char *guid_name, char **name, efi_guid_t *guid)
a43681
 
a43681
 	const char *left, *right;
a43681
 
a43681
+	validate_name(guid_name);
a43681
+
a43681
 	left = strchr(guid_name, '{');
a43681
 	right = strchr(guid_name, '}');
a43681
 	if (left && right) {
a43681
@@ -408,16 +438,6 @@ edit_variable(const char *guid_name, void *data, size_t data_size,
a43681
 	}
a43681
 }
a43681
 
a43681
-static void
a43681
-validate_name(const char *name)
a43681
-{
a43681
-	if (name == NULL) {
a43681
-		fprintf(stderr, "Invalid variable name\n");
a43681
-		show_errors();
a43681
-		exit(1);
a43681
-	}
a43681
-}
a43681
-
a43681
 static void
a43681
 prepare_data(const char *filename, uint8_t **data, size_t *data_size)
a43681
 {
a43681
@@ -588,21 +608,17 @@ int main(int argc, char *argv[])
a43681
 			list_all_variables();
a43681
 			break;
a43681
 		case ACTION_PRINT:
a43681
-			validate_name(guid_name);
a43681
 			show_variable(guid_name, SHOW_VERBOSE);
a43681
 			break;
a43681
 		case ACTION_PRINT_DEC | ACTION_PRINT:
a43681
-			validate_name(guid_name);
a43681
 			show_variable(guid_name, SHOW_DECIMAL);
a43681
 			break;
a43681
 		case ACTION_APPEND | ACTION_PRINT:
a43681
-			validate_name(guid_name);
a43681
 			prepare_data(infile, &data, &data_size);
a43681
 			edit_variable(guid_name, data, data_size, attributes,
a43681
 				      EDIT_APPEND);
a43681
 			break;
a43681
 		case ACTION_WRITE | ACTION_PRINT:
a43681
-			validate_name(guid_name);
a43681
 			prepare_data(infile, &data, &data_size);
a43681
 			edit_variable(guid_name, data, data_size, attributes,
a43681
 				      EDIT_WRITE);
a43681
@@ -653,7 +669,6 @@ int main(int argc, char *argv[])
a43681
 
a43681
 				efi_variable_free(var, false);
a43681
 			} else {
a43681
-				validate_name(guid_name);
a43681
 				save_variable(guid_name, outfile, dmpstore);
a43681
 			}
a43681
 			break;
a43681
-- 
a43681
2.26.2
a43681