Blame SOURCES/0017-libsepol-cil-Use-AST-to-track-blocks-and-optionals-w.patch

212ad1
From 599c1422479ae9dd9501c43680bf4a1667e7c951 Mon Sep 17 00:00:00 2001
212ad1
From: James Carter <jwcart2@gmail.com>
212ad1
Date: Tue, 30 Mar 2021 13:39:15 -0400
212ad1
Subject: [PATCH] libsepol/cil: Use AST to track blocks and optionals when
212ad1
 resolving
212ad1
212ad1
When resolving the AST, block and optional stacks are used to
212ad1
determine if the current rule being resolved is in a block or
212ad1
an optional. There is no need to do this since the parent node
212ad1
pointers can be used when exiting a block or an optional to
212ad1
determine if resolution is still within a block or an optional.
212ad1
212ad1
When entering either a block or an optional, update the appropriate
212ad1
tree node pointer. When finished with the last child of a block or
212ad1
optional, set the appropriate pointer to NULL. If a parent of the
212ad1
same kind is found when the parent node pointers are followed back
212ad1
to the root node, then set the pointer to that tree node.
212ad1
212ad1
Signed-off-by: James Carter <jwcart2@gmail.com>
212ad1
---
212ad1
 libsepol/cil/src/cil_resolve_ast.c | 107 +++++++++--------------------
212ad1
 1 file changed, 32 insertions(+), 75 deletions(-)
212ad1
212ad1
diff --git a/libsepol/cil/src/cil_resolve_ast.c b/libsepol/cil/src/cil_resolve_ast.c
212ad1
index dab8b276..e0379782 100644
212ad1
--- a/libsepol/cil/src/cil_resolve_ast.c
212ad1
+++ b/libsepol/cil/src/cil_resolve_ast.c
212ad1
@@ -52,10 +52,10 @@ struct cil_args_resolve {
212ad1
 	enum cil_pass pass;
212ad1
 	uint32_t *changed;
212ad1
 	struct cil_list *disabled_optionals;
212ad1
-	struct cil_tree_node *optstack;
212ad1
+	struct cil_tree_node *optional;
212ad1
 	struct cil_tree_node *boolif;
212ad1
 	struct cil_tree_node *macro;
212ad1
-	struct cil_tree_node *blockstack;
212ad1
+	struct cil_tree_node *block;
212ad1
 	struct cil_list *sidorder_lists;
212ad1
 	struct cil_list *classorder_lists;
212ad1
 	struct cil_list *unordered_classorder_lists;
212ad1
@@ -3692,16 +3692,16 @@ int __cil_resolve_ast_node_helper(struct cil_tree_node *node, uint32_t *finished
212ad1
 	int rc = SEPOL_ERR;
212ad1
 	struct cil_args_resolve *args = extra_args;
212ad1
 	enum cil_pass pass = args->pass;
212ad1
-	struct cil_tree_node *optstack = args->optstack;
212ad1
+	struct cil_tree_node *optional = args->optional;
212ad1
 	struct cil_tree_node *boolif = args->boolif;
212ad1
-	struct cil_tree_node *blockstack = args->blockstack;
212ad1
+	struct cil_tree_node *block = args->block;
212ad1
 	struct cil_tree_node *macro = args->macro;
212ad1
 
212ad1
 	if (node == NULL) {
212ad1
 		goto exit;
212ad1
 	}
212ad1
 
212ad1
-	if (optstack != NULL) {
212ad1
+	if (optional != NULL) {
212ad1
 		if (node->flavor == CIL_TUNABLE || node->flavor == CIL_MACRO) {
212ad1
 			/* tuanbles and macros are not allowed in optionals*/
212ad1
 			cil_tree_log(node, CIL_ERR, "%s statement is not allowed in optionals", cil_node_to_string(node));
212ad1
@@ -3710,7 +3710,7 @@ int __cil_resolve_ast_node_helper(struct cil_tree_node *node, uint32_t *finished
212ad1
 		}
212ad1
 	}
212ad1
 
212ad1
-	if (blockstack != NULL) {
212ad1
+	if (block != NULL) {
212ad1
 		if (node->flavor == CIL_CAT || node->flavor == CIL_SENS) {
212ad1
 			cil_tree_log(node, CIL_ERR, "%s statement is not allowed in blocks", cil_node_to_string(node));
212ad1
 			rc = SEPOL_ERR;
212ad1
@@ -3764,11 +3764,11 @@ int __cil_resolve_ast_node_helper(struct cil_tree_node *node, uint32_t *finished
212ad1
 	if (rc == SEPOL_ENOENT) {
212ad1
 		enum cil_log_level lvl = CIL_ERR;
212ad1
 
212ad1
-		if (optstack != NULL) {
212ad1
+		if (optional != NULL) {
212ad1
 			lvl = CIL_INFO;
212ad1
 
212ad1
-			struct cil_optional *opt = (struct cil_optional *)optstack->data;
212ad1
-			struct cil_tree_node *opt_node = opt->datum.nodes->head->data;
212ad1
+			struct cil_optional *opt = (struct cil_optional *)optional->data;
212ad1
+			struct cil_tree_node *opt_node = NODE(opt);;
212ad1
 			/* disable an optional if something failed to resolve */
212ad1
 			opt->enabled = CIL_FALSE;
212ad1
 			cil_tree_log(node, lvl, "Failed to resolve %s statement", cil_node_to_string(node));
212ad1
@@ -3791,39 +3791,18 @@ int __cil_resolve_ast_first_child_helper(struct cil_tree_node *current, void *ex
212ad1
 {
212ad1
 	int rc = SEPOL_ERR;
212ad1
 	struct cil_args_resolve *args = extra_args;
212ad1
-	struct cil_tree_node *optstack = NULL;
212ad1
 	struct cil_tree_node *parent = NULL;
212ad1
-	struct cil_tree_node *blockstack = NULL;
212ad1
-	struct cil_tree_node *new = NULL;
212ad1
 
212ad1
 	if (current == NULL || extra_args == NULL) {
212ad1
 		goto exit;
212ad1
 	}
212ad1
 
212ad1
-	optstack = args->optstack;
212ad1
 	parent = current->parent;
212ad1
-	blockstack = args->blockstack;
212ad1
 
212ad1
-	if (parent->flavor == CIL_OPTIONAL || parent->flavor == CIL_BLOCK) {
212ad1
-		/* push this node onto a stack */
212ad1
-		cil_tree_node_init(&new;;
212ad1
-
212ad1
-		new->data = parent->data;
212ad1
-		new->flavor = parent->flavor;
212ad1
-
212ad1
-		if (parent->flavor == CIL_OPTIONAL) {
212ad1
-			if (optstack != NULL) {
212ad1
-				optstack->parent = new;
212ad1
-				new->cl_head = optstack;
212ad1
-			}
212ad1
-			args->optstack = new;
212ad1
-		} else if (parent->flavor == CIL_BLOCK) {
212ad1
-			if (blockstack != NULL) {
212ad1
-				blockstack->parent = new;
212ad1
-				new->cl_head = blockstack;
212ad1
-			}
212ad1
-			args->blockstack = new;
212ad1
-		}
212ad1
+	if (parent->flavor == CIL_BLOCK) {
212ad1
+		args->block = parent;
212ad1
+	} else if (parent->flavor == CIL_OPTIONAL) {
212ad1
+		args->optional = parent;
212ad1
 	} else if (parent->flavor == CIL_BOOLEANIF) {
212ad1
 		args->boolif = parent;
212ad1
 	} else if (parent->flavor == CIL_MACRO) {
212ad1
@@ -3842,7 +3821,6 @@ int __cil_resolve_ast_last_child_helper(struct cil_tree_node *current, void *ext
212ad1
 	int rc = SEPOL_ERR;
212ad1
 	struct cil_args_resolve *args = extra_args;
212ad1
 	struct cil_tree_node *parent = NULL;
212ad1
-	struct cil_tree_node *blockstack = NULL;
212ad1
 
212ad1
 	if (current == NULL ||  extra_args == NULL) {
212ad1
 		goto exit;
212ad1
@@ -3853,30 +3831,31 @@ int __cil_resolve_ast_last_child_helper(struct cil_tree_node *current, void *ext
212ad1
 	if (parent->flavor == CIL_MACRO) {
212ad1
 		args->macro = NULL;
212ad1
 	} else if (parent->flavor == CIL_OPTIONAL) {
212ad1
-		struct cil_tree_node *optstack;
212ad1
-
212ad1
+		struct cil_tree_node *n = parent->parent;
212ad1
 		if (((struct cil_optional *)parent->data)->enabled == CIL_FALSE) {
212ad1
 			*(args->changed) = CIL_TRUE;
212ad1
 			cil_list_append(args->disabled_optionals, CIL_NODE, parent);
212ad1
 		}
212ad1
-
212ad1
-		/* pop off the stack */
212ad1
-		optstack = args->optstack;
212ad1
-		args->optstack = optstack->cl_head;
212ad1
-		if (optstack->cl_head) {
212ad1
-			optstack->cl_head->parent = NULL;
212ad1
+		args->optional = NULL;
212ad1
+		while (n && n->flavor != CIL_ROOT) {
212ad1
+			if (n->flavor == CIL_OPTIONAL) {
212ad1
+				args->optional = n;
212ad1
+				break;
212ad1
+			}
212ad1
+			n = n->parent;
212ad1
 		}
212ad1
-		free(optstack);
212ad1
 	} else if (parent->flavor == CIL_BOOLEANIF) {
212ad1
 		args->boolif = NULL;
212ad1
 	} else if (parent->flavor == CIL_BLOCK) {
212ad1
-		/* pop off the stack */
212ad1
-		blockstack = args->blockstack;
212ad1
-		args->blockstack = blockstack->cl_head;
212ad1
-		if (blockstack->cl_head) {
212ad1
-			blockstack->cl_head->parent = NULL;
212ad1
+		struct cil_tree_node *n = parent->parent;
212ad1
+		args->block = NULL;
212ad1
+		while (n && n->flavor != CIL_ROOT) {
212ad1
+			if (n->flavor == CIL_BLOCK) {
212ad1
+				args->block = n;
212ad1
+				break;
212ad1
+			}
212ad1
+			n = n->parent;
212ad1
 		}
212ad1
-		free(blockstack);
212ad1
 	}
212ad1
 
212ad1
 	return SEPOL_OK;
212ad1
@@ -3885,16 +3864,6 @@ exit:
212ad1
 	return rc;
212ad1
 }
212ad1
 
212ad1
-static void cil_destroy_tree_node_stack(struct cil_tree_node *curr)
212ad1
-{
212ad1
-	struct cil_tree_node *next;
212ad1
-	while (curr != NULL) {
212ad1
-		next = curr->cl_head;
212ad1
-		free(curr);
212ad1
-		curr = next;
212ad1
-	}
212ad1
-}
212ad1
-
212ad1
 int cil_resolve_ast(struct cil_db *db, struct cil_tree_node *current)
212ad1
 {
212ad1
 	int rc = SEPOL_ERR;
212ad1
@@ -3909,7 +3878,8 @@ int cil_resolve_ast(struct cil_db *db, struct cil_tree_node *current)
212ad1
 	extra_args.db = db;
212ad1
 	extra_args.pass = pass;
212ad1
 	extra_args.changed = &changed;
212ad1
-	extra_args.optstack = NULL;
212ad1
+	extra_args.block = NULL;
212ad1
+	extra_args.optional = NULL;
212ad1
 	extra_args.boolif= NULL;
212ad1
 	extra_args.macro = NULL;
212ad1
 	extra_args.sidorder_lists = NULL;
212ad1
@@ -3918,7 +3888,6 @@ int cil_resolve_ast(struct cil_db *db, struct cil_tree_node *current)
212ad1
 	extra_args.catorder_lists = NULL;
212ad1
 	extra_args.sensitivityorder_lists = NULL;
212ad1
 	extra_args.in_list = NULL;
212ad1
-	extra_args.blockstack = NULL;
212ad1
 
212ad1
 	cil_list_init(&extra_args.disabled_optionals, CIL_NODE);
212ad1
 	cil_list_init(&extra_args.sidorder_lists, CIL_LIST_ITEM);
212ad1
@@ -4022,17 +3991,7 @@ int cil_resolve_ast(struct cil_db *db, struct cil_tree_node *current)
212ad1
 			}
212ad1
 			cil_list_destroy(&extra_args.disabled_optionals, CIL_FALSE);
212ad1
 			cil_list_init(&extra_args.disabled_optionals, CIL_NODE);
212ad1
-		}
212ad1
-
212ad1
-		/* reset the arguments */
212ad1
-		changed = 0;
212ad1
-		while (extra_args.optstack != NULL) {
212ad1
-			cil_destroy_tree_node_stack(extra_args.optstack);
212ad1
-			extra_args.optstack = NULL;
212ad1
-		}
212ad1
-		while (extra_args.blockstack!= NULL) {
212ad1
-			cil_destroy_tree_node_stack(extra_args.blockstack);
212ad1
-			extra_args.blockstack = NULL;
212ad1
+			changed = 0;
212ad1
 		}
212ad1
 	}
212ad1
 
212ad1
@@ -4043,8 +4002,6 @@ int cil_resolve_ast(struct cil_db *db, struct cil_tree_node *current)
212ad1
 
212ad1
 	rc = SEPOL_OK;
212ad1
 exit:
212ad1
-	cil_destroy_tree_node_stack(extra_args.optstack);
212ad1
-	cil_destroy_tree_node_stack(extra_args.blockstack);
212ad1
 	__cil_ordered_lists_destroy(&extra_args.sidorder_lists);
212ad1
 	__cil_ordered_lists_destroy(&extra_args.classorder_lists);
212ad1
 	__cil_ordered_lists_destroy(&extra_args.catorder_lists);
212ad1
-- 
212ad1
2.30.2
212ad1