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

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