Blame SOURCES/0009-libsepol-cil-Reorder-checks-for-invalid-rules-when-b.patch

060220
From 6b6a787188804cad4f7f853e95eb0a58dea7ad62 Mon Sep 17 00:00:00 2001
060220
From: James Carter <jwcart2@gmail.com>
060220
Date: Tue, 30 Mar 2021 13:39:12 -0400
060220
Subject: [PATCH] libsepol/cil: Reorder checks for invalid rules when building
060220
 AST
060220
060220
Reorder checks for invalid rules in the blocks of tunableifs,
060220
in-statements, macros, and booleanifs when building the AST for
060220
consistency.
060220
060220
Order the checks in the same order the blocks will be resolved in,
060220
so tuanbleif, in-statement, macro, booleanif, and then non-block
060220
rules.
060220
060220
Signed-off-by: James Carter <jwcart2@gmail.com>
060220
---
060220
 libsepol/cil/src/cil_build_ast.c | 100 +++++++++++++++----------------
060220
 1 file changed, 50 insertions(+), 50 deletions(-)
060220
060220
diff --git a/libsepol/cil/src/cil_build_ast.c b/libsepol/cil/src/cil_build_ast.c
060220
index fe7b7777..6d5a57fa 100644
060220
--- a/libsepol/cil/src/cil_build_ast.c
060220
+++ b/libsepol/cil/src/cil_build_ast.c
060220
@@ -49,10 +49,10 @@
060220
 struct cil_args_build {
060220
 	struct cil_tree_node *ast;
060220
 	struct cil_db *db;
060220
-	struct cil_tree_node *macro;
060220
-	struct cil_tree_node *boolif;
060220
 	struct cil_tree_node *tunif;
060220
 	struct cil_tree_node *in;
060220
+	struct cil_tree_node *macro;
060220
+	struct cil_tree_node *boolif;
060220
 };
060220
 
060220
 int cil_fill_list(struct cil_tree_node *current, enum cil_flavor flavor, struct cil_list **list)
060220
@@ -6075,10 +6075,10 @@ int __cil_build_ast_node_helper(struct cil_tree_node *parse_current, uint32_t *f
060220
 	struct cil_tree_node *ast_current = NULL;
060220
 	struct cil_db *db = NULL;
060220
 	struct cil_tree_node *ast_node = NULL;
060220
-	struct cil_tree_node *macro = NULL;
060220
-	struct cil_tree_node *boolif = NULL;
060220
 	struct cil_tree_node *tunif = NULL;
060220
 	struct cil_tree_node *in = NULL;
060220
+	struct cil_tree_node *macro = NULL;
060220
+	struct cil_tree_node *boolif = NULL;
060220
 	int rc = SEPOL_ERR;
060220
 
060220
 	if (parse_current == NULL || finished == NULL || extra_args == NULL) {
060220
@@ -6088,10 +6088,10 @@ int __cil_build_ast_node_helper(struct cil_tree_node *parse_current, uint32_t *f
060220
 	args = extra_args;
060220
 	ast_current = args->ast;
060220
 	db = args->db;
060220
-	macro = args->macro;
060220
-	boolif = args->boolif;
060220
 	tunif = args->tunif;
060220
 	in = args->in;
060220
+	macro = args->macro;
060220
+	boolif = args->boolif;
060220
 
060220
 	if (parse_current->parent->cl_head != parse_current) {
060220
 		/* ignore anything that isn't following a parenthesis */
060220
@@ -6108,13 +6108,31 @@ int __cil_build_ast_node_helper(struct cil_tree_node *parse_current, uint32_t *f
060220
 		goto exit;
060220
 	}
060220
 
060220
+	if (tunif != NULL) {
060220
+		if (parse_current->data == CIL_KEY_TUNABLE) {
060220
+			rc = SEPOL_ERR;
060220
+			cil_tree_log(parse_current, CIL_ERR, "Found tunable");
060220
+			cil_log(CIL_ERR, "Tunables cannot be defined within tunableif statement\n");
060220
+			goto exit;
060220
+		}
060220
+	}
060220
+
060220
+	if (in != NULL) {
060220
+		if (parse_current->data == CIL_KEY_IN) {
060220
+			rc = SEPOL_ERR;
060220
+			cil_tree_log(parse_current, CIL_ERR, "Found in-statement");
060220
+			cil_log(CIL_ERR, "in-statements cannot be defined within in-statements\n");
060220
+			goto exit;
060220
+		}
060220
+	}
060220
+
060220
 	if (macro != NULL) {
060220
-		if (parse_current->data == CIL_KEY_MACRO ||
060220
-			parse_current->data == CIL_KEY_TUNABLE ||
060220
+		if (parse_current->data == CIL_KEY_TUNABLE ||
060220
 			parse_current->data == CIL_KEY_IN ||
060220
 			parse_current->data == CIL_KEY_BLOCK ||
060220
 			parse_current->data == CIL_KEY_BLOCKINHERIT ||
060220
-			parse_current->data == CIL_KEY_BLOCKABSTRACT) {
060220
+			parse_current->data == CIL_KEY_BLOCKABSTRACT ||
060220
+			parse_current->data == CIL_KEY_MACRO) {
060220
 			rc = SEPOL_ERR;
060220
 			cil_tree_log(parse_current, CIL_ERR, "%s is not allowed in macros", (char *)parse_current->data);
060220
 			goto exit;
060220
@@ -6122,15 +6140,15 @@ int __cil_build_ast_node_helper(struct cil_tree_node *parse_current, uint32_t *f
060220
 	}
060220
 
060220
 	if (boolif != NULL) {
060220
-		if (parse_current->data != CIL_KEY_CONDTRUE &&
060220
+		if (parse_current->data != CIL_KEY_TUNABLEIF &&
060220
+			parse_current->data != CIL_KEY_CALL &&
060220
+			parse_current->data != CIL_KEY_CONDTRUE &&
060220
 			parse_current->data != CIL_KEY_CONDFALSE &&
060220
-			parse_current->data != CIL_KEY_AUDITALLOW &&
060220
-			parse_current->data != CIL_KEY_TUNABLEIF &&
060220
 			parse_current->data != CIL_KEY_ALLOW &&
060220
 			parse_current->data != CIL_KEY_DONTAUDIT &&
060220
+			parse_current->data != CIL_KEY_AUDITALLOW &&
060220
 			parse_current->data != CIL_KEY_TYPETRANSITION &&
060220
-			parse_current->data != CIL_KEY_TYPECHANGE &&
060220
-			parse_current->data != CIL_KEY_CALL) {
060220
+			parse_current->data != CIL_KEY_TYPECHANGE) {
060220
 			rc = SEPOL_ERR;
060220
 			cil_tree_log(parse_current, CIL_ERR, "Found %s", (char*)parse_current->data);
060220
 			if (((struct cil_booleanif*)boolif->data)->preserved_tunable) {
060220
@@ -6144,24 +6162,6 @@ int __cil_build_ast_node_helper(struct cil_tree_node *parse_current, uint32_t *f
060220
 		}
060220
 	}
060220
 
060220
-	if (tunif != NULL) {
060220
-		if (parse_current->data == CIL_KEY_TUNABLE) {
060220
-			rc = SEPOL_ERR;
060220
-			cil_tree_log(parse_current, CIL_ERR, "Found tunable");
060220
-			cil_log(CIL_ERR, "Tunables cannot be defined within tunableif statement\n");
060220
-			goto exit;
060220
-		}
060220
-	}
060220
-
060220
-	if (in != NULL) {
060220
-		if (parse_current->data == CIL_KEY_IN) {
060220
-			rc = SEPOL_ERR;
060220
-			cil_tree_log(parse_current, CIL_ERR, "Found in-statement");
060220
-			cil_log(CIL_ERR, "in-statements cannot be defined within in-statements\n");
060220
-			goto exit;
060220
-		}
060220
-	}
060220
-
060220
 	cil_tree_node_init(&ast_node);
060220
 
060220
 	ast_node->parent = ast_current;
060220
@@ -6447,14 +6447,6 @@ int __cil_build_ast_node_helper(struct cil_tree_node *parse_current, uint32_t *f
060220
 
060220
 	if (rc == SEPOL_OK) {
060220
 		if (ast_current->cl_head == NULL) {
060220
-			if (ast_current->flavor == CIL_MACRO) {
060220
-				args->macro = ast_current;
060220
-			}
060220
-
060220
-			if (ast_current->flavor == CIL_BOOLEANIF) {
060220
-				args->boolif = ast_current;
060220
-			}
060220
-
060220
 			if (ast_current->flavor == CIL_TUNABLEIF) {
060220
 				args->tunif = ast_current;
060220
 			}
060220
@@ -6463,6 +6455,14 @@ int __cil_build_ast_node_helper(struct cil_tree_node *parse_current, uint32_t *f
060220
 				args->in = ast_current;
060220
 			}
060220
 
060220
+			if (ast_current->flavor == CIL_MACRO) {
060220
+				args->macro = ast_current;
060220
+			}
060220
+
060220
+			if (ast_current->flavor == CIL_BOOLEANIF) {
060220
+				args->boolif = ast_current;
060220
+			}
060220
+
060220
 			ast_current->cl_head = ast_node;
060220
 		} else {
060220
 			ast_current->cl_tail->next = ast_node;
060220
@@ -6498,14 +6498,6 @@ int __cil_build_ast_last_child_helper(struct cil_tree_node *parse_current, void
060220
 
060220
 	args->ast = ast->parent;
060220
 
060220
-	if (ast->flavor == CIL_MACRO) {
060220
-		args->macro = NULL;
060220
-	}
060220
-
060220
-	if (ast->flavor == CIL_BOOLEANIF) {
060220
-		args->boolif = NULL;
060220
-	}
060220
-
060220
 	if (ast->flavor == CIL_TUNABLEIF) {
060220
 		args->tunif = NULL;
060220
 	}
060220
@@ -6514,6 +6506,14 @@ int __cil_build_ast_last_child_helper(struct cil_tree_node *parse_current, void
060220
 		args->in = NULL;
060220
 	}
060220
 
060220
+	if (ast->flavor == CIL_MACRO) {
060220
+		args->macro = NULL;
060220
+	}
060220
+
060220
+	if (ast->flavor == CIL_BOOLEANIF) {
060220
+		args->boolif = NULL;
060220
+	}
060220
+
060220
 	// At this point we no longer have any need for parse_current or any of its
060220
 	// siblings; they have all been converted to the appropriate AST node. The
060220
 	// full parse tree will get deleted elsewhere, but in an attempt to
060220
@@ -6538,10 +6538,10 @@ int cil_build_ast(struct cil_db *db, struct cil_tree_node *parse_tree, struct ci
060220
 
060220
 	extra_args.ast = ast;
060220
 	extra_args.db = db;
060220
-	extra_args.macro = NULL;
060220
-	extra_args.boolif = NULL;
060220
 	extra_args.tunif = NULL;
060220
 	extra_args.in = NULL;
060220
+	extra_args.macro = NULL;
060220
+	extra_args.boolif = NULL;
060220
 
060220
 	rc = cil_tree_walk(parse_tree, __cil_build_ast_node_helper, NULL, __cil_build_ast_last_child_helper, &extra_args);
060220
 	if (rc != SEPOL_OK) {
060220
-- 
060220
2.30.2
060220