Skip to contentSkip to conversation

secilc/cil: Fix name resolution for macro calls

PATCH In review James Carter 1 participant 8 messages May 11, 2021 – Jun 4, 2021 SELinux security module

lore
Share & tools
lore
#0 lore raw

v2 cover secilc/cil: Fix name resolution for macro calls

This patch series fixes name resolution within expanded macro calls and with call arguments.

The first patch is same as in v1.
Patches 2-4 replace patch 2 in v1. The original way of fixing name resolution of call arguments did not work if a disabled optional forced a reset.
Patch 5 is the same as patch 3 in v1.

James Carter (5):
libsepol/cil: Make name resolution in macros work as documented libsepol/cil: Do not add NULL node when inserting key into symtab libsepo/cil: Refactor macro call resolution libsepol/cil: Do not resolve arguments to declarations in the call secilc/docs: Relocate and reword macro call name resolution order

 libsepol/cil/src/cil_internal.h          |   1 -
 libsepol/cil/src/cil_resolve_ast.c       | 635 ++++++++++++-----------
 libsepol/cil/src/cil_symtab.c            |   8 +-
 secilc/docs/cil_call_macro_statements.md |  24 +-
 4 files changed, 354 insertions(+), 314 deletions(-)
Signature · 1 line
2.26.3
#1 lore raw

PATCH v2 1/5 libsepol/cil: Make name resolution in macros work as documented

The CIL Reference Guide specifies how name resolution is suppose to work within an expanded macro.
1. Items defined inside the macro
2. Items passed into the macro as arguments
3. Items defined in the same namespace of the macro
4. Items defined in the caller's namespace
5. Items defined in the global namespace

But Lorenzo Ceragioli <[email protected]> found that the first step is not done.

So the following policy:
(block A

    (type a)
    (macro m ()
      (type a)
      (allow a self (CLASS (PERM)))
    )
  )
  (block B
    (call A.m)

)
will result in:
(allow A.a self (CLASS (PERM)))
instead of the expected:
(allow B.a self (CLASS (PERM)))

Now when an expanded call is found, the macro's namespace is checked first. If the name is found, then the name was declared in the macro and it is declared in the expanded call, so only the namespace of the call up to and including the global namespace will be searched. If the name is not found in the macro's namespace then name resolution continues with steps 2-5 above.

---
 libsepol/cil/src/cil_resolve_ast.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
libsepol/cil/src/cil_resolve_ast.c +12 -4
index f251ed15..bbe86e22 100644
@@ -4224,10 +4224,18 @@ static int __cil_resolve_name_with_parents(struct cil_tree_node *node, char *nam
 			break;
 		case CIL_CALL: {
 			struct cil_call *call = node->data;
-			rc = cil_resolve_name_call_args(call, name, sym_index, datum);
-			if (rc != SEPOL_OK) {
-				/* Continue search in macro's parent */
-				rc = __cil_resolve_name_with_parents(NODE(call->macro)->parent, name, sym_index, datum);
+			struct cil_macro *macro = call->macro;
+			symtab = &macro->symtab[sym_index];
+			rc = cil_symtab_get_datum(symtab, name, datum);
+			if (rc == SEPOL_OK) {
+				/* If the name was declared in the macro, just look on the call side */
+				rc = SEPOL_ERR;
+			} else {
+				rc = cil_resolve_name_call_args(call, name, sym_index, datum);
+				if (rc != SEPOL_OK) {
+					/* Continue search in macro's parent */
+					rc = __cil_resolve_name_with_parents(NODE(call->macro)->parent, name, sym_index, datum);
+				}
 			}
 		}
 			break;
Signature · 1 line
2.26.3
#2 lore raw

PATCH v2 2/5 libsepol/cil: Do not add NULL node when inserting key into symtab

In reply to James Carter

Allow inserting a key without providing a node.

This will make it easier to properly resolve call arguments where a key might need to be temporarily removed to search for a datum that is not declared within the call. Since the node is already in the node list, re-inserting the key without this option would add another link to the node and cause problems.

Also, do not add the node to the datum's node list if the result of the call to hashtab_insert() is SEPOL_EEXIST because the datum is a duplicate and will be destroyed.

---
 libsepol/cil/src/cil_symtab.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
libsepol/cil/src/cil_symtab.c +4 -4
index 579a888e..c1951560 100644
@@ -93,10 +93,10 @@ int cil_symtab_insert(symtab_t *symtab, hashtab_key_t key, struct cil_symtab_dat
 		datum->fqn = key;
 		datum->symtab = symtab;
 		symtab->nprim++;
-		cil_list_append(datum->nodes, CIL_NODE, node);
-	} else if (rc == SEPOL_EEXIST) {
-		cil_list_append(datum->nodes, CIL_NODE, node);
-	} else {
+		if (node) {
+			cil_list_append(datum->nodes, CIL_NODE, node);
+		}
+	} else if (rc != SEPOL_EEXIST) {
 		cil_symtab_error("Failed to insert datum into hashtab\n");
 	}
 
Signature · 1 line
2.26.3
#3 lore raw

PATCH v2 3/5 libsepo/cil: Refactor macro call resolution

In reply to James Carter

Rename cil_resolve_call1() as cil resolve_call() and rename cil_resolve_call2() as cil_resolve_call_args() to make it clearer what is being done in each function.

Move code to build call arguments out of cil_resolve_call() and into the new function called cil_build_call_args() so that the logic of cil_resolve_call() can be seen.

Exit cil_resolve_call() immediately if the call has already been copied.

In __cil_resolve_ast_node(), only resolve calls outside of macros. This results in more calls to cil_copy_ast(), but slightly less rules copied overall (since no rules are copied into a macro). This also means that the CIL_PASS_MACRO pass is not needed and can be eliminated.

---
 libsepol/cil/src/cil_internal.h    |   1 -
 libsepol/cil/src/cil_resolve_ast.c | 599 +++++++++++++++--------------
 2 files changed, 303 insertions(+), 297 deletions(-)
libsepol/cil/src/cil_internal.h +0 -1
index 74e0b34d..a77c9520 100644
@@ -59,7 +59,6 @@ enum cil_pass {
 	CIL_PASS_BLKIN_LINK,
 	CIL_PASS_BLKIN_COPY,
 	CIL_PASS_BLKABS,
-	CIL_PASS_MACRO,
 	CIL_PASS_CALL1,
 	CIL_PASS_CALL2,
 	CIL_PASS_ALIAS1,
libsepol/cil/src/cil_resolve_ast.c +303 -296

688 lines not on this page · raw message

Signature · 1 line
2.26.3
#4 lore raw

PATCH v2 4/5 libsepol/cil: Do not resolve arguments to declarations in the call

In reply to James Carter

Lorenzo Ceragioli <[email protected]> noted that the following policy:
(type a)
(block A

    (macro m ((type x))
      (type a)
      (allow x x (file (read))))
  )
  (block B
    (call A.m(a))

)
results in the allow rule (allow B.a B.a (file(read))). This makes no sense because the "a" being passed as an argument has to be the global "a" and not the "a" defined in the macro.

This behavior occurs because the call arguments are resolved AFTER the macro body has been copied and the declaration of "a" in the macro has been added to block B's namespace, so this is the "a" that the call argument resolves to, rather than the one in the global namespace.

When resolving call arguments, check if the datum found belongs to a declaration in the call. If it does, then remove the datum from the symbol table, re-resolve the argument, and add the datum back into the symbol table.

---
 libsepol/cil/src/cil_resolve_ast.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
libsepol/cil/src/cil_resolve_ast.c +27 -1
index b96118ce..d416ce75 100644
@@ -3287,11 +3287,37 @@ int cil_resolve_call_args(struct cil_tree_node *current, void *extra_args)
 		}
 
 		if (sym_index != CIL_SYM_UNKNOWN) {
-			rc = cil_resolve_name(current, arg->arg_str, sym_index, extra_args, &(arg->arg));
+			struct cil_symtab_datum *datum;
+			struct cil_tree_node *n;
+			rc = cil_resolve_name(current, arg->arg_str, sym_index, extra_args, &datum);
 			if (rc != SEPOL_OK) {
 				cil_tree_log(current, CIL_ERR, "Failed to resolve %s in call argument list", arg->arg_str);
 				goto exit;
 			}
+			arg->arg = datum;
+			n = NODE(datum);
+			while (n && n->flavor != CIL_ROOT) {
+				if (n == current) {
+					symtab_t *s = datum->symtab;
+					/* Call arg should not resolve to declaration in the call
+					 * Need to remove datum temporarily to resolve to a datum outside
+					 * the call.
+					 */
+					cil_symtab_remove_datum(datum);
+					rc = cil_resolve_name(current, arg->arg_str, sym_index, extra_args, &(arg->arg));
+					if (rc != SEPOL_OK) {
+						cil_tree_log(current, CIL_ERR, "Failed to resolve %s in call argument list", arg->arg_str);
+						goto exit;
+					}
+					rc = cil_symtab_insert(s, datum->name, datum, NULL);
+					if (rc != SEPOL_OK) {
+						cil_tree_log(current, CIL_ERR, "Failed to re-insert datum while resolving %s in call argument list", arg->arg_str);
+						goto exit;
+					}
+					break;
+				}
+				n = n->parent;
+			}
 		}
 	}
 
Signature · 1 line
2.26.3
#5 lore raw

PATCH v2 5/5 secilc/docs: Relocate and reword macro call name resolution order

In reply to James Carter

The listing of the order was in the macro section, but it belongs in the call section.

Move the listing of the order to the call section and provide a better explanation.

---
 secilc/docs/cil_call_macro_statements.md | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
secilc/docs/cil_call_macro_statements.md +12 -12
index 352a9fb0..3cc14bf8 100644
@@ -8,6 +8,18 @@ Instantiate a [macro](#macro) within the current namespace. There may be zero or
 
 Each parameter passed contains an argument to be resolved by the [macro](#macro), these can be named or anonymous but must conform to the parameter types defined in the [`macro`](cil_call_macro_statements.md#macro) statement.
 
+Macro rules are resolved by searching in the following order:
+
+-   The macro namespace (If found this means that the name was declared in the macro and is now declared in the namespace of one of the parents of the call.)
+
+-   The call arguments
+
+-   The parent namespaces of the macro being called (if any) with the exception of the global namespace.
+
+-   The parent namespaces of the call (if any) with the exception of the global namespace.
+
+-   The global namespace
+
 **Statement definition:**
 
 ```secil
@@ -46,18 +58,6 @@ macro
 
 Declare a macro in the current namespace with its associated parameters. The macro identifier is used by the [`call`](cil_call_macro_statements.md#call) statement to instantiate the macro and resolve any parameters. The call statement may be within the body of a macro.
 
-When resolving macros the following places are checked in this order:
-
--   Items defined inside the macro
-
--   Items passed into the macro as arguments
-
--   Items defined in the same namespace of the macro
-
--   Items defined in the callers namespace
-
--   Items defined in the global namespace
-
 [`tunable`](cil_conditional_statements.md#tunable), [`in`](cil_container_statements.md#in), [`block`](cil_container_statements.md#block), [`blockinherit`](cil_container_statements.md#blockinherit), [`blockabstract`](cil_container_statements.md#blockabstract), and other [`macro`](cil_call_macro_statements.md#macro) statements are not allowed in [`macro`](cil_call_macro_statements.md#macro) blocks.
 
 **Statement definition:**
Signature · 1 line
2.26.3
#6 lore raw
In reply to James Carter
Quoted 17 lines · James Carter

This patch series fixes name resolution within expanded macro calls and with call arguments.

The first patch is same as in v1.
Patches 2-4 replace patch 2 in v1. The original way of fixing name resolution of call arguments did not work if a disabled optional forced a reset.
Patch 5 is the same as patch 3 in v1.

James Carter (5):
libsepol/cil: Make name resolution in macros work as documented libsepol/cil: Do not add NULL node when inserting key into symtab libsepo/cil: Refactor macro call resolution libsepol/cil: Do not resolve arguments to declarations in the call secilc/docs: Relocate and reword macro call name resolution order

Quoted 9 lines
 libsepol/cil/src/cil_internal.h          |   1 -
 libsepol/cil/src/cil_resolve_ast.c       | 635 ++++++++++++-----------
 libsepol/cil/src/cil_symtab.c            |   8 +-
 secilc/docs/cil_call_macro_statements.md |  24 +-
 4 files changed, 354 insertions(+), 314 deletions(-)

--
2.26.3

There hasn't been any comments on this series, and it has been more than three weeks since I sent it, so I plan on merging it tomorrow. Jim

#7 lore raw
Quoted 25 lines · James Carter
Quoted 23 lines · James Carter

This patch series fixes name resolution within expanded macro calls and with call arguments.

The first patch is same as in v1.
Patches 2-4 replace patch 2 in v1. The original way of fixing name resolution of call arguments did not work if a disabled optional forced a reset.
Patch 5 is the same as patch 3 in v1.

James Carter (5):
libsepol/cil: Make name resolution in macros work as documented libsepol/cil: Do not add NULL node when inserting key into symtab libsepo/cil: Refactor macro call resolution libsepol/cil: Do not resolve arguments to declarations in the call secilc/docs: Relocate and reword macro call name resolution order

 libsepol/cil/src/cil_internal.h          |   1 -
 libsepol/cil/src/cil_resolve_ast.c       | 635 ++++++++++++-----------
 libsepol/cil/src/cil_symtab.c            |   8 +-
 secilc/docs/cil_call_macro_statements.md |  24 +-
 4 files changed, 354 insertions(+), 314 deletions(-)
Quoted 7 lines
Quoted 3 lines

--
2.26.3

There hasn't been any comments on this series, and it has been more than three weeks since I sent it, so I plan on merging it tomorrow. Jim

This series has been merged.
Jim