Blame SOURCES/0051-libsepo-cil-Refactor-macro-call-resolution.patch

71cd55
From bccec36a7694e8eee03ab0d592c3b0d8ddfff36e Mon Sep 17 00:00:00 2001
71cd55
From: James Carter <jwcart2@gmail.com>
71cd55
Date: Tue, 11 May 2021 12:34:27 -0400
71cd55
Subject: [PATCH] libsepo/cil: Refactor macro call resolution
71cd55
71cd55
Rename cil_resolve_call1() as cil resolve_call() and rename
71cd55
cil_resolve_call2() as cil_resolve_call_args() to make it clearer
71cd55
what is being done in each function.
71cd55
71cd55
Move code to build call arguments out of cil_resolve_call() and into
71cd55
the new function called cil_build_call_args() so that the logic of
71cd55
cil_resolve_call() can be seen.
71cd55
71cd55
Exit cil_resolve_call() immediately if the call has already been
71cd55
copied.
71cd55
71cd55
In __cil_resolve_ast_node(), only resolve calls outside of macros.
71cd55
This results in more calls to cil_copy_ast(), but slightly less
71cd55
rules copied overall (since no rules are copied into a macro). This
71cd55
also means that the CIL_PASS_MACRO pass is not needed and can be
71cd55
eliminated.
71cd55
71cd55
Signed-off-by: James Carter <jwcart2@gmail.com>
71cd55
---
71cd55
 libsepol/cil/src/cil_internal.h    |   1 -
71cd55
 libsepol/cil/src/cil_resolve_ast.c | 599 +++++++++++++++--------------
71cd55
 2 files changed, 303 insertions(+), 297 deletions(-)
71cd55
71cd55
diff --git a/libsepol/cil/src/cil_internal.h b/libsepol/cil/src/cil_internal.h
71cd55
index 74e0b34d6cbd..a77c95201fb7 100644
71cd55
--- a/libsepol/cil/src/cil_internal.h
71cd55
+++ b/libsepol/cil/src/cil_internal.h
71cd55
@@ -59,7 +59,6 @@ enum cil_pass {
71cd55
 	CIL_PASS_BLKIN_LINK,
71cd55
 	CIL_PASS_BLKIN_COPY,
71cd55
 	CIL_PASS_BLKABS,
71cd55
-	CIL_PASS_MACRO,
71cd55
 	CIL_PASS_CALL1,
71cd55
 	CIL_PASS_CALL2,
71cd55
 	CIL_PASS_ALIAS1,
71cd55
diff --git a/libsepol/cil/src/cil_resolve_ast.c b/libsepol/cil/src/cil_resolve_ast.c
71cd55
index ae6743f92f4c..258fdb1bb69f 100644
71cd55
--- a/libsepol/cil/src/cil_resolve_ast.c
71cd55
+++ b/libsepol/cil/src/cil_resolve_ast.c
71cd55
@@ -2816,359 +2816,371 @@ exit:
71cd55
 	return rc;
71cd55
 }
71cd55
 
71cd55
-int cil_resolve_call1(struct cil_tree_node *current, void *extra_args)
71cd55
+static int cil_build_call_args(struct cil_tree_node *call_node, struct cil_call *call, struct cil_macro *macro, void *extra_args)
71cd55
 {
71cd55
-	struct cil_call *new_call = current->data;
71cd55
 	struct cil_args_resolve *args = extra_args;
71cd55
-	struct cil_db *db = NULL;
71cd55
-	struct cil_tree_node *macro_node = NULL;
71cd55
-	struct cil_symtab_datum *macro_datum = NULL;
71cd55
+	struct cil_list_item *item;
71cd55
+	struct cil_args *arg = NULL;
71cd55
+	struct cil_tree_node *arg_node = NULL;
71cd55
 	int rc = SEPOL_ERR;
71cd55
 
71cd55
-	if (args != NULL) {
71cd55
-		db = args->db;
71cd55
+	if (macro->params == NULL) {
71cd55
+		if (call->args_tree == NULL) {
71cd55
+			return SEPOL_OK;
71cd55
+		} else {
71cd55
+			cil_tree_log(call_node, CIL_ERR, "Unexpected arguments");
71cd55
+			return SEPOL_ERR;;
71cd55
+		}
71cd55
 	}
71cd55
-
71cd55
-	rc = cil_resolve_name(current, new_call->macro_str, CIL_SYM_BLOCKS, extra_args, &macro_datum);
71cd55
-	if (rc != SEPOL_OK) {
71cd55
-		goto exit;
71cd55
+	if (call->args_tree == NULL) {
71cd55
+		cil_tree_log(call_node, CIL_ERR, "Missing arguments");
71cd55
+		return SEPOL_ERR;
71cd55
 	}
71cd55
 
71cd55
-	macro_node = NODE(macro_datum);
71cd55
+	arg_node = call->args_tree->root->cl_head;
71cd55
 
71cd55
-	if (macro_node->flavor != CIL_MACRO) {
71cd55
-		cil_tree_log(current, CIL_ERR, "Failed to resolve %s to a macro", new_call->macro_str);
71cd55
-		rc = SEPOL_ERR;
71cd55
-		goto exit;
71cd55
-	}
71cd55
-	new_call->macro = (struct cil_macro*)macro_datum;
71cd55
+	cil_list_init(&call->args, CIL_LIST_ITEM);
71cd55
 
71cd55
-	if (new_call->macro->params != NULL ) {
71cd55
+	cil_list_for_each(item, macro->params) {
71cd55
+		enum cil_flavor flavor = ((struct cil_param*)item->data)->flavor;
71cd55
 
71cd55
-		struct cil_list_item *item;
71cd55
-		struct cil_args *new_arg = NULL;
71cd55
-		struct cil_tree_node *pc = NULL;
71cd55
-
71cd55
-		if (new_call->args_tree == NULL) {
71cd55
-			cil_tree_log(current, CIL_ERR, "Missing arguments");
71cd55
+		if (arg_node == NULL) {
71cd55
+			cil_tree_log(call_node, CIL_ERR, "Missing arguments");
71cd55
+			rc = SEPOL_ERR;
71cd55
+			goto exit;
71cd55
+		}
71cd55
+		if (item->flavor != CIL_PARAM) {
71cd55
 			rc = SEPOL_ERR;
71cd55
 			goto exit;
71cd55
 		}
71cd55
 
71cd55
-		pc = new_call->args_tree->root->cl_head;
71cd55
-
71cd55
-		cil_list_init(&new_call->args, CIL_LIST_ITEM);
71cd55
-
71cd55
-		cil_list_for_each(item, new_call->macro->params) {
71cd55
-			enum cil_flavor flavor = ((struct cil_param*)item->data)->flavor;
71cd55
+		cil_args_init(&arg;;
71cd55
 
71cd55
-			if (pc == NULL) {
71cd55
-				cil_tree_log(current, CIL_ERR, "Missing arguments");
71cd55
+		switch (flavor) {
71cd55
+		case CIL_NAME: {
71cd55
+			struct cil_name *name;
71cd55
+			if (arg_node->data == NULL) {
71cd55
+				cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
71cd55
+				cil_destroy_args(arg);
71cd55
 				rc = SEPOL_ERR;
71cd55
 				goto exit;
71cd55
 			}
71cd55
-			if (item->flavor != CIL_PARAM) {
71cd55
+			name = __cil_insert_name(args->db, arg_node->data, call_node);
71cd55
+			if (name != NULL) {
71cd55
+				arg->arg = (struct cil_symtab_datum *)name;
71cd55
+			} else {
71cd55
+				arg->arg_str = arg_node->data;
71cd55
+			}
71cd55
+		}
71cd55
+			break;
71cd55
+		case CIL_TYPE:
71cd55
+			if (arg_node->data == NULL) {
71cd55
+				cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
71cd55
+				cil_destroy_args(arg);
71cd55
 				rc = SEPOL_ERR;
71cd55
 				goto exit;
71cd55
 			}
71cd55
-
71cd55
-			cil_args_init(&new_arg);
71cd55
-
71cd55
-			switch (flavor) {
71cd55
-			case CIL_NAME: {
71cd55
-				struct cil_name *name;
71cd55
-				if (pc->data == NULL) {
71cd55
-					cil_tree_log(current, CIL_ERR, "Invalid macro parameter");
71cd55
-					cil_destroy_args(new_arg);
71cd55
-					rc = SEPOL_ERR;
71cd55
-					goto exit;
71cd55
-				}
71cd55
-				name = __cil_insert_name(args->db, pc->data, current);
71cd55
-				if (name != NULL) {
71cd55
-					new_arg->arg = (struct cil_symtab_datum *)name;
71cd55
-				} else {
71cd55
-					new_arg->arg_str = pc->data;
71cd55
-				}
71cd55
+			arg->arg_str = arg_node->data;
71cd55
+			break;
71cd55
+		case CIL_ROLE:
71cd55
+			if (arg_node->data == NULL) {
71cd55
+				cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
71cd55
+				cil_destroy_args(arg);
71cd55
+				rc = SEPOL_ERR;
71cd55
+				goto exit;
71cd55
 			}
71cd55
-				break;
71cd55
-			case CIL_TYPE:
71cd55
-				if (pc->data == NULL) {
71cd55
-					cil_tree_log(current, CIL_ERR, "Invalid macro parameter");
71cd55
-					cil_destroy_args(new_arg);
71cd55
-					rc = SEPOL_ERR;
71cd55
-					goto exit;
71cd55
-				}
71cd55
-				new_arg->arg_str = pc->data;
71cd55
-				break;
71cd55
-			case CIL_ROLE:
71cd55
-				if (pc->data == NULL) {
71cd55
-					cil_tree_log(current, CIL_ERR, "Invalid macro parameter");
71cd55
-					cil_destroy_args(new_arg);
71cd55
-					rc = SEPOL_ERR;
71cd55
-					goto exit;
71cd55
-				}
71cd55
-				new_arg->arg_str = pc->data;
71cd55
-				break;
71cd55
-			case CIL_USER:
71cd55
-				if (pc->data == NULL) {
71cd55
-					cil_tree_log(current, CIL_ERR, "Invalid macro parameter");
71cd55
-					cil_destroy_args(new_arg);
71cd55
-					rc = SEPOL_ERR;
71cd55
-					goto exit;
71cd55
-				}
71cd55
-				new_arg->arg_str = pc->data;
71cd55
-				break;
71cd55
-			case CIL_SENS:
71cd55
-				if (pc->data == NULL) {
71cd55
-					cil_tree_log(current, CIL_ERR, "Invalid macro parameter");
71cd55
-					cil_destroy_args(new_arg);
71cd55
-					rc = SEPOL_ERR;
71cd55
-					goto exit;
71cd55
-				}
71cd55
-				new_arg->arg_str = pc->data;
71cd55
-				break;
71cd55
-			case CIL_CAT:
71cd55
-				if (pc->data == NULL) {
71cd55
-					cil_tree_log(current, CIL_ERR, "Invalid macro parameter");
71cd55
-					cil_destroy_args(new_arg);
71cd55
-					rc = SEPOL_ERR;
71cd55
-					goto exit;
71cd55
-				}
71cd55
-				new_arg->arg_str = pc->data;
71cd55
-				break;
71cd55
-			case CIL_BOOL:
71cd55
-				if (pc->data == NULL) {
71cd55
-					cil_tree_log(current, CIL_ERR, "Invalid macro parameter");
71cd55
-					cil_destroy_args(new_arg);
71cd55
-					rc = SEPOL_ERR;
71cd55
-					goto exit;
71cd55
-				}
71cd55
-				new_arg->arg_str = pc->data;
71cd55
-				break;
71cd55
-			case CIL_CATSET: {
71cd55
-				if (pc->cl_head != NULL) {
71cd55
-					struct cil_catset *catset = NULL;
71cd55
-					struct cil_tree_node *cat_node = NULL;
71cd55
-					cil_catset_init(&catset);
71cd55
-					rc = cil_fill_cats(pc, &catset->cats);
71cd55
-					if (rc != SEPOL_OK) {
71cd55
-						cil_destroy_catset(catset);
71cd55
-						cil_destroy_args(new_arg);
71cd55
-						goto exit;
71cd55
-					}
71cd55
-					cil_tree_node_init(&cat_node);
71cd55
-					cat_node->flavor = CIL_CATSET;
71cd55
-					cat_node->data = catset;
71cd55
-					cil_list_append(((struct cil_symtab_datum*)catset)->nodes,
71cd55
-									CIL_LIST_ITEM, cat_node);
71cd55
-					new_arg->arg = (struct cil_symtab_datum*)catset;
71cd55
-				} else if (pc->data == NULL) {
71cd55
-					cil_tree_log(current, CIL_ERR, "Invalid macro parameter");
71cd55
-					cil_destroy_args(new_arg);
71cd55
-					rc = SEPOL_ERR;
71cd55
+			arg->arg_str = arg_node->data;
71cd55
+			break;
71cd55
+		case CIL_USER:
71cd55
+			if (arg_node->data == NULL) {
71cd55
+				cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
71cd55
+				cil_destroy_args(arg);
71cd55
+				rc = SEPOL_ERR;
71cd55
+				goto exit;
71cd55
+			}
71cd55
+			arg->arg_str = arg_node->data;
71cd55
+			break;
71cd55
+		case CIL_SENS:
71cd55
+			if (arg_node->data == NULL) {
71cd55
+				cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
71cd55
+				cil_destroy_args(arg);
71cd55
+				rc = SEPOL_ERR;
71cd55
+				goto exit;
71cd55
+			}
71cd55
+			arg->arg_str = arg_node->data;
71cd55
+			break;
71cd55
+		case CIL_CAT:
71cd55
+			if (arg_node->data == NULL) {
71cd55
+				cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
71cd55
+				cil_destroy_args(arg);
71cd55
+				rc = SEPOL_ERR;
71cd55
+				goto exit;
71cd55
+			}
71cd55
+			arg->arg_str = arg_node->data;
71cd55
+			break;
71cd55
+		case CIL_BOOL:
71cd55
+			if (arg_node->data == NULL) {
71cd55
+				cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
71cd55
+				cil_destroy_args(arg);
71cd55
+				rc = SEPOL_ERR;
71cd55
+				goto exit;
71cd55
+			}
71cd55
+			arg->arg_str = arg_node->data;
71cd55
+			break;
71cd55
+		case CIL_CATSET: {
71cd55
+			if (arg_node->cl_head != NULL) {
71cd55
+				struct cil_catset *catset = NULL;
71cd55
+				struct cil_tree_node *cat_node = NULL;
71cd55
+				cil_catset_init(&catset);
71cd55
+				rc = cil_fill_cats(arg_node, &catset->cats);
71cd55
+				if (rc != SEPOL_OK) {
71cd55
+					cil_destroy_catset(catset);
71cd55
+					cil_destroy_args(arg);
71cd55
 					goto exit;
71cd55
-				} else {
71cd55
-					new_arg->arg_str = pc->data;
71cd55
 				}
71cd55
-
71cd55
-				break;
71cd55
+				cil_tree_node_init(&cat_node);
71cd55
+				cat_node->flavor = CIL_CATSET;
71cd55
+				cat_node->data = catset;
71cd55
+				cil_list_append(((struct cil_symtab_datum*)catset)->nodes,
71cd55
+								CIL_LIST_ITEM, cat_node);
71cd55
+				arg->arg = (struct cil_symtab_datum*)catset;
71cd55
+			} else if (arg_node->data == NULL) {
71cd55
+				cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
71cd55
+				cil_destroy_args(arg);
71cd55
+				rc = SEPOL_ERR;
71cd55
+				goto exit;
71cd55
+			} else {
71cd55
+				arg->arg_str = arg_node->data;
71cd55
 			}
71cd55
-			case CIL_LEVEL: {
71cd55
-				if (pc->cl_head != NULL) {
71cd55
-					struct cil_level *level = NULL;
71cd55
-					struct cil_tree_node *lvl_node = NULL;
71cd55
-					cil_level_init(&level);
71cd55
-
71cd55
-					rc = cil_fill_level(pc->cl_head, level);
71cd55
-					if (rc != SEPOL_OK) {
71cd55
-						cil_log(CIL_ERR, "Failed to create anonymous level, rc: %d\n", rc);
71cd55
-						cil_destroy_level(level);
71cd55
-						cil_destroy_args(new_arg);
71cd55
-						goto exit;
71cd55
-					}
71cd55
-					cil_tree_node_init(&lvl_node);
71cd55
-					lvl_node->flavor = CIL_LEVEL;
71cd55
-					lvl_node->data = level;
71cd55
-					cil_list_append(((struct cil_symtab_datum*)level)->nodes, 
71cd55
-									CIL_LIST_ITEM, lvl_node);
71cd55
-					new_arg->arg = (struct cil_symtab_datum*)level;
71cd55
-				} else if (pc->data == NULL) {
71cd55
-					cil_tree_log(current, CIL_ERR, "Invalid macro parameter");
71cd55
-					cil_destroy_args(new_arg);
71cd55
-					rc = SEPOL_ERR;
71cd55
+
71cd55
+			break;
71cd55
+		}
71cd55
+		case CIL_LEVEL: {
71cd55
+			if (arg_node->cl_head != NULL) {
71cd55
+				struct cil_level *level = NULL;
71cd55
+				struct cil_tree_node *lvl_node = NULL;
71cd55
+				cil_level_init(&level);
71cd55
+
71cd55
+				rc = cil_fill_level(arg_node->cl_head, level);
71cd55
+				if (rc != SEPOL_OK) {
71cd55
+					cil_log(CIL_ERR, "Failed to create anonymous level, rc: %d\n", rc);
71cd55
+					cil_destroy_level(level);
71cd55
+					cil_destroy_args(arg);
71cd55
 					goto exit;
71cd55
-				} else {
71cd55
-					new_arg->arg_str = pc->data;
71cd55
 				}
71cd55
-
71cd55
-				break;
71cd55
+				cil_tree_node_init(&lvl_node);
71cd55
+				lvl_node->flavor = CIL_LEVEL;
71cd55
+				lvl_node->data = level;
71cd55
+				cil_list_append(((struct cil_symtab_datum*)level)->nodes,
71cd55
+								CIL_LIST_ITEM, lvl_node);
71cd55
+				arg->arg = (struct cil_symtab_datum*)level;
71cd55
+			} else if (arg_node->data == NULL) {
71cd55
+				cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
71cd55
+				cil_destroy_args(arg);
71cd55
+				rc = SEPOL_ERR;
71cd55
+				goto exit;
71cd55
+			} else {
71cd55
+				arg->arg_str = arg_node->data;
71cd55
 			}
71cd55
-			case CIL_LEVELRANGE: {
71cd55
-				if (pc->cl_head != NULL) {
71cd55
-					struct cil_levelrange *range = NULL;
71cd55
-					struct cil_tree_node *range_node = NULL;
71cd55
-					cil_levelrange_init(&range);
71cd55
-
71cd55
-					rc = cil_fill_levelrange(pc->cl_head, range);
71cd55
-					if (rc != SEPOL_OK) {
71cd55
-						cil_log(CIL_ERR, "Failed to create anonymous levelrange, rc: %d\n", rc);
71cd55
-						cil_destroy_levelrange(range);
71cd55
-						cil_destroy_args(new_arg);
71cd55
-						goto exit;
71cd55
-					}
71cd55
-					cil_tree_node_init(&range_node);
71cd55
-					range_node->flavor = CIL_LEVELRANGE;
71cd55
-					range_node->data = range;
71cd55
-					cil_list_append(((struct cil_symtab_datum*)range)->nodes, 
71cd55
-									CIL_LIST_ITEM, range_node);
71cd55
-					new_arg->arg = (struct cil_symtab_datum*)range;
71cd55
-				} else if (pc->data == NULL) {
71cd55
-					cil_tree_log(current, CIL_ERR, "Invalid macro parameter");
71cd55
-					cil_destroy_args(new_arg);
71cd55
-					rc = SEPOL_ERR;
71cd55
+
71cd55
+			break;
71cd55
+		}
71cd55
+		case CIL_LEVELRANGE: {
71cd55
+			if (arg_node->cl_head != NULL) {
71cd55
+				struct cil_levelrange *range = NULL;
71cd55
+				struct cil_tree_node *range_node = NULL;
71cd55
+				cil_levelrange_init(&range);
71cd55
+
71cd55
+				rc = cil_fill_levelrange(arg_node->cl_head, range);
71cd55
+				if (rc != SEPOL_OK) {
71cd55
+					cil_log(CIL_ERR, "Failed to create anonymous levelrange, rc: %d\n", rc);
71cd55
+					cil_destroy_levelrange(range);
71cd55
+					cil_destroy_args(arg);
71cd55
 					goto exit;
71cd55
-				} else {
71cd55
-					new_arg->arg_str = pc->data;
71cd55
 				}
71cd55
-
71cd55
-				break;
71cd55
+				cil_tree_node_init(&range_node);
71cd55
+				range_node->flavor = CIL_LEVELRANGE;
71cd55
+				range_node->data = range;
71cd55
+				cil_list_append(((struct cil_symtab_datum*)range)->nodes,
71cd55
+								CIL_LIST_ITEM, range_node);
71cd55
+				arg->arg = (struct cil_symtab_datum*)range;
71cd55
+			} else if (arg_node->data == NULL) {
71cd55
+				cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
71cd55
+				cil_destroy_args(arg);
71cd55
+				rc = SEPOL_ERR;
71cd55
+				goto exit;
71cd55
+			} else {
71cd55
+				arg->arg_str = arg_node->data;
71cd55
 			}
71cd55
-			case CIL_IPADDR: {
71cd55
-				if (pc->cl_head != NULL) {
71cd55
-					struct cil_ipaddr *ipaddr = NULL;
71cd55
-					struct cil_tree_node *addr_node = NULL;
71cd55
-					cil_ipaddr_init(&ipaddr);
71cd55
-
71cd55
-					rc = cil_fill_ipaddr(pc->cl_head, ipaddr);
71cd55
-					if (rc != SEPOL_OK) {
71cd55
-						cil_log(CIL_ERR, "Failed to create anonymous ip address, rc: %d\n", rc);
71cd55
-						cil_destroy_ipaddr(ipaddr);
71cd55
-						cil_destroy_args(new_arg);
71cd55
-						goto exit;
71cd55
-					}
71cd55
-					cil_tree_node_init(&addr_node);
71cd55
-					addr_node->flavor = CIL_IPADDR;
71cd55
-					addr_node->data = ipaddr;
71cd55
-					cil_list_append(((struct cil_symtab_datum*)ipaddr)->nodes,
71cd55
-									CIL_LIST_ITEM, addr_node);
71cd55
-					new_arg->arg = (struct cil_symtab_datum*)ipaddr;
71cd55
-				} else if (pc->data == NULL) {
71cd55
-					cil_tree_log(current, CIL_ERR, "Invalid macro parameter");
71cd55
-					cil_destroy_args(new_arg);
71cd55
-					rc = SEPOL_ERR;
71cd55
+
71cd55
+			break;
71cd55
+		}
71cd55
+		case CIL_IPADDR: {
71cd55
+			if (arg_node->cl_head != NULL) {
71cd55
+				struct cil_ipaddr *ipaddr = NULL;
71cd55
+				struct cil_tree_node *addr_node = NULL;
71cd55
+				cil_ipaddr_init(&ipaddr);
71cd55
+
71cd55
+				rc = cil_fill_ipaddr(arg_node->cl_head, ipaddr);
71cd55
+				if (rc != SEPOL_OK) {
71cd55
+					cil_log(CIL_ERR, "Failed to create anonymous ip address, rc: %d\n", rc);
71cd55
+					cil_destroy_ipaddr(ipaddr);
71cd55
+					cil_destroy_args(arg);
71cd55
 					goto exit;
71cd55
-				} else {
71cd55
-					new_arg->arg_str = pc->data;
71cd55
 				}
71cd55
+				cil_tree_node_init(&addr_node);
71cd55
+				addr_node->flavor = CIL_IPADDR;
71cd55
+				addr_node->data = ipaddr;
71cd55
+				cil_list_append(((struct cil_symtab_datum*)ipaddr)->nodes,
71cd55
+								CIL_LIST_ITEM, addr_node);
71cd55
+				arg->arg = (struct cil_symtab_datum*)ipaddr;
71cd55
+			} else if (arg_node->data == NULL) {
71cd55
+				cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
71cd55
+				cil_destroy_args(arg);
71cd55
+				rc = SEPOL_ERR;
71cd55
+				goto exit;
71cd55
+			} else {
71cd55
+				arg->arg_str = arg_node->data;
71cd55
+			}
71cd55
 
71cd55
-				break;
71cd55
+			break;
71cd55
+		}
71cd55
+		case CIL_CLASS:
71cd55
+			if (arg_node->data == NULL) {
71cd55
+				cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
71cd55
+				cil_destroy_args(arg);
71cd55
+				rc = SEPOL_ERR;
71cd55
+				goto exit;
71cd55
 			}
71cd55
-			case CIL_CLASS:
71cd55
-				if (pc->data == NULL) {
71cd55
-					cil_tree_log(current, CIL_ERR, "Invalid macro parameter");
71cd55
-					cil_destroy_args(new_arg);
71cd55
-					rc = SEPOL_ERR;
71cd55
-					goto exit;
71cd55
-				}
71cd55
-				new_arg->arg_str = pc->data;
71cd55
-				break;
71cd55
-			case CIL_MAP_CLASS:
71cd55
-				if (pc->data == NULL) {
71cd55
-					cil_tree_log(current, CIL_ERR, "Invalid macro parameter");
71cd55
-					cil_destroy_args(new_arg);
71cd55
-					rc = SEPOL_ERR;
71cd55
-					goto exit;
71cd55
-				}
71cd55
-				new_arg->arg_str = pc->data;
71cd55
-				break;
71cd55
-			case CIL_CLASSPERMISSION: {
71cd55
-				if (pc->cl_head != NULL) {
71cd55
-					struct cil_classpermission *cp = NULL;
71cd55
-					struct cil_tree_node *cp_node = NULL;
71cd55
-
71cd55
-					cil_classpermission_init(&cp;;
71cd55
-					rc = cil_fill_classperms_list(pc, &cp->classperms);
71cd55
-					if (rc != SEPOL_OK) {
71cd55
-						cil_log(CIL_ERR, "Failed to create anonymous classpermission\n");
71cd55
-						cil_destroy_classpermission(cp);
71cd55
-						cil_destroy_args(new_arg);
71cd55
-						goto exit;
71cd55
-					}
71cd55
-					cil_tree_node_init(&cp_node);
71cd55
-					cp_node->flavor = CIL_CLASSPERMISSION;
71cd55
-					cp_node->data = cp;
71cd55
-					cil_list_append(cp->datum.nodes, CIL_LIST_ITEM, cp_node);
71cd55
-					new_arg->arg = (struct cil_symtab_datum*)cp;
71cd55
-				} else if (pc->data == NULL) {
71cd55
-					cil_tree_log(current, CIL_ERR, "Invalid macro parameter");
71cd55
-					cil_destroy_args(new_arg);
71cd55
-					rc = SEPOL_ERR;
71cd55
+			arg->arg_str = arg_node->data;
71cd55
+			break;
71cd55
+		case CIL_MAP_CLASS:
71cd55
+			if (arg_node->data == NULL) {
71cd55
+				cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
71cd55
+				cil_destroy_args(arg);
71cd55
+				rc = SEPOL_ERR;
71cd55
+				goto exit;
71cd55
+			}
71cd55
+			arg->arg_str = arg_node->data;
71cd55
+			break;
71cd55
+		case CIL_CLASSPERMISSION: {
71cd55
+			if (arg_node->cl_head != NULL) {
71cd55
+				struct cil_classpermission *cp = NULL;
71cd55
+				struct cil_tree_node *cp_node = NULL;
71cd55
+
71cd55
+				cil_classpermission_init(&cp;;
71cd55
+				rc = cil_fill_classperms_list(arg_node, &cp->classperms);
71cd55
+				if (rc != SEPOL_OK) {
71cd55
+					cil_log(CIL_ERR, "Failed to create anonymous classpermission\n");
71cd55
+					cil_destroy_classpermission(cp);
71cd55
+					cil_destroy_args(arg);
71cd55
 					goto exit;
71cd55
-				} else {
71cd55
-					new_arg->arg_str = pc->data;
71cd55
 				}
71cd55
-				break;
71cd55
-			}
71cd55
-			default:
71cd55
-				cil_log(CIL_ERR, "Unexpected flavor: %d\n", 
71cd55
-						(((struct cil_param*)item->data)->flavor));
71cd55
-				cil_destroy_args(new_arg);
71cd55
+				cil_tree_node_init(&cp_node);
71cd55
+				cp_node->flavor = CIL_CLASSPERMISSION;
71cd55
+				cp_node->data = cp;
71cd55
+				cil_list_append(cp->datum.nodes, CIL_LIST_ITEM, cp_node);
71cd55
+				arg->arg = (struct cil_symtab_datum*)cp;
71cd55
+			} else if (arg_node->data == NULL) {
71cd55
+				cil_tree_log(call_node, CIL_ERR, "Invalid macro parameter");
71cd55
+				cil_destroy_args(arg);
71cd55
 				rc = SEPOL_ERR;
71cd55
 				goto exit;
71cd55
+			} else {
71cd55
+				arg->arg_str = arg_node->data;
71cd55
 			}
71cd55
-			new_arg->param_str = ((struct cil_param*)item->data)->str;
71cd55
-			new_arg->flavor = flavor;
71cd55
-
71cd55
-			cil_list_append(new_call->args, CIL_ARGS, new_arg);
71cd55
-
71cd55
-			pc = pc->next;
71cd55
+			break;
71cd55
 		}
71cd55
-
71cd55
-		if (pc != NULL) {
71cd55
-			cil_tree_log(current, CIL_ERR, "Unexpected arguments");
71cd55
+		default:
71cd55
+			cil_log(CIL_ERR, "Unexpected flavor: %d\n",
71cd55
+					(((struct cil_param*)item->data)->flavor));
71cd55
+			cil_destroy_args(arg);
71cd55
 			rc = SEPOL_ERR;
71cd55
 			goto exit;
71cd55
 		}
71cd55
-	} else if (new_call->args_tree != NULL) {
71cd55
-		cil_tree_log(current, CIL_ERR, "Unexpected arguments");
71cd55
+		arg->param_str = ((struct cil_param*)item->data)->str;
71cd55
+		arg->flavor = flavor;
71cd55
+
71cd55
+		cil_list_append(call->args, CIL_ARGS, arg);
71cd55
+
71cd55
+		arg_node = arg_node->next;
71cd55
+	}
71cd55
+
71cd55
+	if (arg_node != NULL) {
71cd55
+		cil_tree_log(call_node, CIL_ERR, "Unexpected arguments");
71cd55
 		rc = SEPOL_ERR;
71cd55
 		goto exit;
71cd55
 	}
71cd55
 
71cd55
-	if (new_call->copied == 0) {
71cd55
-		new_call->copied = 1;
71cd55
+	return SEPOL_OK;
71cd55
 
71cd55
-		rc = cil_check_recursive_call(current, macro_node);
71cd55
-		if (rc != SEPOL_OK) {
71cd55
-			goto exit;
71cd55
-		}
71cd55
+exit:
71cd55
+	return rc;
71cd55
+}
71cd55
 
71cd55
-		rc = cil_copy_ast(db, macro_node, current);
71cd55
-		if (rc != SEPOL_OK) {
71cd55
-			cil_log(CIL_ERR, "Failed to copy macro, rc: %d\n", rc);
71cd55
-			goto exit;
71cd55
-		}
71cd55
+int cil_resolve_call(struct cil_tree_node *current, void *extra_args)
71cd55
+{
71cd55
+	struct cil_call *call = current->data;
71cd55
+	struct cil_args_resolve *args = extra_args;
71cd55
+	struct cil_tree_node *macro_node = NULL;
71cd55
+	struct cil_symtab_datum *macro_datum = NULL;
71cd55
+	int rc = SEPOL_ERR;
71cd55
+
71cd55
+	if (call->copied) {
71cd55
+		return SEPOL_OK;
71cd55
+	}
71cd55
+
71cd55
+	rc = cil_resolve_name(current, call->macro_str, CIL_SYM_BLOCKS, extra_args, &macro_datum);
71cd55
+	if (rc != SEPOL_OK) {
71cd55
+		goto exit;
71cd55
+	}
71cd55
+
71cd55
+	macro_node = NODE(macro_datum);
71cd55
+
71cd55
+	if (macro_node->flavor != CIL_MACRO) {
71cd55
+		cil_tree_log(current, CIL_ERR, "Failed to resolve %s to a macro", call->macro_str);
71cd55
+		rc = SEPOL_ERR;
71cd55
+		goto exit;
71cd55
+	}
71cd55
+	call->macro = (struct cil_macro*)macro_datum;
71cd55
+
71cd55
+	rc = cil_build_call_args(current, call, call->macro, extra_args);
71cd55
+	if (rc != SEPOL_OK) {
71cd55
+		goto exit;
71cd55
 	}
71cd55
 
71cd55
+	rc = cil_check_recursive_call(current, macro_node);
71cd55
+	if (rc != SEPOL_OK) {
71cd55
+		goto exit;
71cd55
+	}
71cd55
+
71cd55
+	rc = cil_copy_ast(args->db, macro_node, current);
71cd55
+	if (rc != SEPOL_OK) {
71cd55
+		cil_tree_log(current, CIL_ERR, "Failed to copy macro %s to call", macro_datum->name);
71cd55
+		goto exit;
71cd55
+	}
71cd55
+
71cd55
+	call->copied = 1;
71cd55
+
71cd55
 	return SEPOL_OK;
71cd55
 
71cd55
 exit:
71cd55
 	return rc;
71cd55
 }
71cd55
 
71cd55
-int cil_resolve_call2(struct cil_tree_node *current, void *extra_args)
71cd55
+int cil_resolve_call_args(struct cil_tree_node *current, void *extra_args)
71cd55
 {
71cd55
-	struct cil_call *new_call = current->data;
71cd55
+	struct cil_call *call = current->data;
71cd55
 	int rc = SEPOL_ERR;
71cd55
 	enum cil_sym_index sym_index = CIL_SYM_UNKNOWN;
71cd55
 	struct cil_list_item *item;
71cd55
 
71cd55
-	if (new_call->args == NULL) {
71cd55
+	if (call->args == NULL) {
71cd55
 		rc = SEPOL_OK;
71cd55
 		goto exit;
71cd55
 	}
71cd55
 
71cd55
-	cil_list_for_each(item, new_call->args) {
71cd55
+	cil_list_for_each(item, call->args) {
71cd55
 		struct cil_args *arg = item->data;
71cd55
 		if (arg->arg == NULL && arg->arg_str == NULL) {
71cd55
 			cil_log(CIL_ERR, "Arguments not created correctly\n");
71cd55
@@ -3574,19 +3586,14 @@ int __cil_resolve_ast_node(struct cil_tree_node *node, void *extra_args)
71cd55
 			rc = cil_resolve_blockabstract(node, args);
71cd55
 		}
71cd55
 		break;
71cd55
-	case CIL_PASS_MACRO:
71cd55
-		if (node->flavor == CIL_CALL && args->macro != NULL) {
71cd55
-			rc = cil_resolve_call1(node, args);
71cd55
-		}
71cd55
-		break;
71cd55
 	case CIL_PASS_CALL1:
71cd55
-		if (node->flavor == CIL_CALL) {
71cd55
-			rc = cil_resolve_call1(node, args);
71cd55
+		if (node->flavor == CIL_CALL && args->macro == NULL) {
71cd55
+			rc = cil_resolve_call(node, args);
71cd55
 		}
71cd55
 		break;
71cd55
 	case CIL_PASS_CALL2:
71cd55
-		if (node->flavor == CIL_CALL) {
71cd55
-			rc = cil_resolve_call2(node, args);
71cd55
+		if (node->flavor == CIL_CALL && args->macro == NULL) {
71cd55
+			rc = cil_resolve_call_args(node, args);
71cd55
 		}
71cd55
 		break;
71cd55
 	case CIL_PASS_ALIAS1:
71cd55
@@ -3890,7 +3897,7 @@ int __cil_resolve_ast_node_helper(struct cil_tree_node *node, uint32_t *finished
71cd55
 	}
71cd55
 
71cd55
 	if (node->flavor == CIL_MACRO) {
71cd55
-		if (pass != CIL_PASS_TIF && pass != CIL_PASS_MACRO) {
71cd55
+		if (pass != CIL_PASS_TIF) {
71cd55
 			*finished = CIL_TREE_SKIP_HEAD;
71cd55
 			rc = SEPOL_OK;
71cd55
 			goto exit;
71cd55
-- 
71cd55
2.32.0
71cd55