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

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