Blame SOURCES/0032-libsepol-cil-Reorder-checks-for-invalid-rules-when-r.patch

71cd55
From ef533c8fd941bfb0c9a729b757d8a5b68fe3d080 Mon Sep 17 00:00:00 2001
71cd55
From: James Carter <jwcart2@gmail.com>
71cd55
Date: Tue, 30 Mar 2021 13:39:16 -0400
71cd55
Subject: [PATCH] libsepol/cil: Reorder checks for invalid rules when resolving
71cd55
 AST
71cd55
71cd55
Reorder checks for invalid rules in the blocks of tunableifs,
71cd55
in-statements, macros, and booleanifs when resolving the AST for
71cd55
consistency.
71cd55
71cd55
Order the checks in the same order the blocks will be resolved in,
71cd55
so tuanbleif, in-statement, macro, booleanif, and then non-block
71cd55
rules.
71cd55
71cd55
Signed-off-by: James Carter <jwcart2@gmail.com>
71cd55
---
71cd55
 libsepol/cil/src/cil_resolve_ast.c | 76 +++++++++++++++---------------
71cd55
 1 file changed, 39 insertions(+), 37 deletions(-)
71cd55
71cd55
diff --git a/libsepol/cil/src/cil_resolve_ast.c b/libsepol/cil/src/cil_resolve_ast.c
71cd55
index a61462d0eb31..93fc0d633cc7 100644
71cd55
--- a/libsepol/cil/src/cil_resolve_ast.c
71cd55
+++ b/libsepol/cil/src/cil_resolve_ast.c
71cd55
@@ -52,10 +52,10 @@ struct cil_args_resolve {
71cd55
 	enum cil_pass pass;
71cd55
 	uint32_t *changed;
71cd55
 	struct cil_list *disabled_optionals;
71cd55
+	struct cil_tree_node *block;
71cd55
+	struct cil_tree_node *macro;
71cd55
 	struct cil_tree_node *optional;
71cd55
 	struct cil_tree_node *boolif;
71cd55
-	struct cil_tree_node *macro;
71cd55
-	struct cil_tree_node *block;
71cd55
 	struct cil_list *sidorder_lists;
71cd55
 	struct cil_list *classorder_lists;
71cd55
 	struct cil_list *unordered_classorder_lists;
71cd55
@@ -3777,50 +3777,52 @@ int __cil_resolve_ast_node_helper(struct cil_tree_node *node, uint32_t *finished
71cd55
 	int rc = SEPOL_ERR;
71cd55
 	struct cil_args_resolve *args = extra_args;
71cd55
 	enum cil_pass pass = args->pass;
71cd55
-	struct cil_tree_node *optional = args->optional;
71cd55
-	struct cil_tree_node *boolif = args->boolif;
71cd55
 	struct cil_tree_node *block = args->block;
71cd55
 	struct cil_tree_node *macro = args->macro;
71cd55
+	struct cil_tree_node *optional = args->optional;
71cd55
+	struct cil_tree_node *boolif = args->boolif;
71cd55
 
71cd55
 	if (node == NULL) {
71cd55
 		goto exit;
71cd55
 	}
71cd55
 
71cd55
-	if (optional != NULL) {
71cd55
-		if (node->flavor == CIL_TUNABLE || node->flavor == CIL_MACRO) {
71cd55
-			/* tuanbles and macros are not allowed in optionals*/
71cd55
-			cil_tree_log(node, CIL_ERR, "%s statement is not allowed in optionals", cil_node_to_string(node));
71cd55
+	if (block != NULL) {
71cd55
+		if (node->flavor == CIL_CAT ||
71cd55
+		    node->flavor == CIL_SENS) {
71cd55
+			cil_tree_log(node, CIL_ERR, "%s statement is not allowed in blocks", cil_node_to_string(node));
71cd55
 			rc = SEPOL_ERR;
71cd55
 			goto exit;
71cd55
 		}
71cd55
 	}
71cd55
 
71cd55
-	if (block != NULL) {
71cd55
-		if (node->flavor == CIL_CAT || node->flavor == CIL_SENS) {
71cd55
-			cil_tree_log(node, CIL_ERR, "%s statement is not allowed in blocks", cil_node_to_string(node));
71cd55
+	if (macro != NULL) {
71cd55
+		if (node->flavor == CIL_BLOCK ||
71cd55
+		    node->flavor == CIL_BLOCKINHERIT ||
71cd55
+		    node->flavor == CIL_BLOCKABSTRACT ||
71cd55
+		    node->flavor == CIL_MACRO) {
71cd55
+			cil_tree_log(node, CIL_ERR, "%s statement is not allowed in macros", cil_node_to_string(node));
71cd55
 			rc = SEPOL_ERR;
71cd55
 			goto exit;
71cd55
 		}
71cd55
 	}
71cd55
 
71cd55
-	if (macro != NULL) {
71cd55
-		if (node->flavor == CIL_BLOCKINHERIT ||
71cd55
-			node->flavor == CIL_BLOCK ||
71cd55
-			node->flavor == CIL_BLOCKABSTRACT ||
71cd55
-			node->flavor == CIL_MACRO) {
71cd55
-			cil_tree_log(node, CIL_ERR, "%s statement is not allowed in macros", cil_node_to_string(node));
71cd55
+	if (optional != NULL) {
71cd55
+		if (node->flavor == CIL_TUNABLE ||
71cd55
+		    node->flavor == CIL_MACRO) {
71cd55
+			/* tuanbles and macros are not allowed in optionals*/
71cd55
+			cil_tree_log(node, CIL_ERR, "%s statement is not allowed in optionals", cil_node_to_string(node));
71cd55
 			rc = SEPOL_ERR;
71cd55
 			goto exit;
71cd55
 		}
71cd55
 	}
71cd55
 
71cd55
 	if (boolif != NULL) {
71cd55
-		if (!(node->flavor == CIL_CONDBLOCK ||
71cd55
-			node->flavor == CIL_AVRULE ||
71cd55
-			node->flavor == CIL_TYPE_RULE ||
71cd55
-			node->flavor == CIL_CALL ||
71cd55
-			node->flavor == CIL_TUNABLEIF ||
71cd55
-			node->flavor == CIL_NAMETYPETRANSITION)) {
71cd55
+		if (!(node->flavor == CIL_TUNABLEIF ||
71cd55
+		      node->flavor == CIL_CALL ||
71cd55
+		      node->flavor == CIL_CONDBLOCK ||
71cd55
+		      node->flavor == CIL_AVRULE ||
71cd55
+		      node->flavor == CIL_TYPE_RULE ||
71cd55
+		      node->flavor == CIL_NAMETYPETRANSITION)) {
71cd55
 			if (((struct cil_booleanif*)boolif->data)->preserved_tunable) {
71cd55
 				cil_tree_log(node, CIL_ERR, "%s statement is not allowed in booleanifs (tunableif treated as a booleanif)", cil_node_to_string(node));
71cd55
 			} else {
71cd55
@@ -3886,12 +3888,12 @@ int __cil_resolve_ast_first_child_helper(struct cil_tree_node *current, void *ex
71cd55
 
71cd55
 	if (parent->flavor == CIL_BLOCK) {
71cd55
 		args->block = parent;
71cd55
+	} else if (parent->flavor == CIL_MACRO) {
71cd55
+		args->macro = parent;
71cd55
 	} else if (parent->flavor == CIL_OPTIONAL) {
71cd55
 		args->optional = parent;
71cd55
 	} else if (parent->flavor == CIL_BOOLEANIF) {
71cd55
 		args->boolif = parent;
71cd55
-	} else if (parent->flavor == CIL_MACRO) {
71cd55
-		args->macro = parent;
71cd55
 	}
71cd55
 
71cd55
 	return SEPOL_OK;
71cd55
@@ -3913,7 +3915,17 @@ int __cil_resolve_ast_last_child_helper(struct cil_tree_node *current, void *ext
71cd55
 
71cd55
 	parent = current->parent;
71cd55
 
71cd55
-	if (parent->flavor == CIL_MACRO) {
71cd55
+	if (parent->flavor == CIL_BLOCK) {
71cd55
+		struct cil_tree_node *n = parent->parent;
71cd55
+		args->block = NULL;
71cd55
+		while (n && n->flavor != CIL_ROOT) {
71cd55
+			if (n->flavor == CIL_BLOCK) {
71cd55
+				args->block = n;
71cd55
+				break;
71cd55
+			}
71cd55
+			n = n->parent;
71cd55
+		}
71cd55
+	} else if (parent->flavor == CIL_MACRO) {
71cd55
 		args->macro = NULL;
71cd55
 	} else if (parent->flavor == CIL_OPTIONAL) {
71cd55
 		struct cil_tree_node *n = parent->parent;
71cd55
@@ -3931,16 +3943,6 @@ int __cil_resolve_ast_last_child_helper(struct cil_tree_node *current, void *ext
71cd55
 		}
71cd55
 	} else if (parent->flavor == CIL_BOOLEANIF) {
71cd55
 		args->boolif = NULL;
71cd55
-	} else if (parent->flavor == CIL_BLOCK) {
71cd55
-		struct cil_tree_node *n = parent->parent;
71cd55
-		args->block = NULL;
71cd55
-		while (n && n->flavor != CIL_ROOT) {
71cd55
-			if (n->flavor == CIL_BLOCK) {
71cd55
-				args->block = n;
71cd55
-				break;
71cd55
-			}
71cd55
-			n = n->parent;
71cd55
-		}
71cd55
 	}
71cd55
 
71cd55
 	return SEPOL_OK;
71cd55
@@ -3964,9 +3966,9 @@ int cil_resolve_ast(struct cil_db *db, struct cil_tree_node *current)
71cd55
 	extra_args.pass = pass;
71cd55
 	extra_args.changed = &changed;
71cd55
 	extra_args.block = NULL;
71cd55
+	extra_args.macro = NULL;
71cd55
 	extra_args.optional = NULL;
71cd55
 	extra_args.boolif= NULL;
71cd55
-	extra_args.macro = NULL;
71cd55
 	extra_args.sidorder_lists = NULL;
71cd55
 	extra_args.classorder_lists = NULL;
71cd55
 	extra_args.unordered_classorder_lists = NULL;
71cd55
-- 
71cd55
2.32.0
71cd55