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

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