Blame SOURCES/0019-libsepol-cil-Sync-checks-for-invalid-rules-in-boolea.patch

212ad1
From dadf1e9ad66318fdd814cf06af2b83741467a3d8 Mon Sep 17 00:00:00 2001
212ad1
From: James Carter <jwcart2@gmail.com>
212ad1
Date: Tue, 30 Mar 2021 13:39:17 -0400
212ad1
Subject: [PATCH] libsepol/cil: Sync checks for invalid rules in booleanifs
212ad1
212ad1
When building the AST, typemember rules in a booleanif block will
212ad1
be incorrectly called invalid. They are allowed in the kernel
212ad1
policy and should be allowed in CIL.
212ad1
212ad1
When resolving the AST, if a neverallow rule is copied into a
212ad1
booleanif block, it will not be considered an invalid rule, even
212ad1
though this is not allowed in the kernel policy.
212ad1
212ad1
Update the booleanif checks to allow typemember rules and to not
212ad1
allow neverallow rules in booleanifs. Also use the same form of
212ad1
conditional for the checks when building and resolving the AST.
212ad1
212ad1
Signed-off-by: James Carter <jwcart2@gmail.com>
212ad1
---
212ad1
 libsepol/cil/src/cil_build_ast.c   |  3 ++-
212ad1
 libsepol/cil/src/cil_resolve_ast.c | 23 +++++++++++++++--------
212ad1
 2 files changed, 17 insertions(+), 9 deletions(-)
212ad1
212ad1
diff --git a/libsepol/cil/src/cil_build_ast.c b/libsepol/cil/src/cil_build_ast.c
212ad1
index ceb55324..3a91be03 100644
212ad1
--- a/libsepol/cil/src/cil_build_ast.c
212ad1
+++ b/libsepol/cil/src/cil_build_ast.c
212ad1
@@ -6136,7 +6136,8 @@ int __cil_build_ast_node_helper(struct cil_tree_node *parse_current, uint32_t *f
212ad1
 			parse_current->data != CIL_KEY_DONTAUDIT &&
212ad1
 			parse_current->data != CIL_KEY_AUDITALLOW &&
212ad1
 			parse_current->data != CIL_KEY_TYPETRANSITION &&
212ad1
-			parse_current->data != CIL_KEY_TYPECHANGE) {
212ad1
+			parse_current->data != CIL_KEY_TYPECHANGE &&
212ad1
+			parse_current->data != CIL_KEY_TYPEMEMBER) {
212ad1
 			rc = SEPOL_ERR;
212ad1
 			cil_tree_log(parse_current, CIL_ERR, "Found %s", (char*)parse_current->data);
212ad1
 			if (((struct cil_booleanif*)boolif->data)->preserved_tunable) {
212ad1
diff --git a/libsepol/cil/src/cil_resolve_ast.c b/libsepol/cil/src/cil_resolve_ast.c
212ad1
index c520c44a..06b6ab48 100644
212ad1
--- a/libsepol/cil/src/cil_resolve_ast.c
212ad1
+++ b/libsepol/cil/src/cil_resolve_ast.c
212ad1
@@ -3689,7 +3689,7 @@ exit:
212ad1
 
212ad1
 int __cil_resolve_ast_node_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
212ad1
 {
212ad1
-	int rc = SEPOL_ERR;
212ad1
+	int rc = SEPOL_OK;
212ad1
 	struct cil_args_resolve *args = extra_args;
212ad1
 	enum cil_pass pass = args->pass;
212ad1
 	struct cil_tree_node *block = args->block;
212ad1
@@ -3732,18 +3732,25 @@ int __cil_resolve_ast_node_helper(struct cil_tree_node *node, uint32_t *finished
212ad1
 	}
212ad1
 
212ad1
 	if (boolif != NULL) {
212ad1
-		if (!(node->flavor == CIL_TUNABLEIF ||
212ad1
-		      node->flavor == CIL_CALL ||
212ad1
-		      node->flavor == CIL_CONDBLOCK ||
212ad1
-		      node->flavor == CIL_AVRULE ||
212ad1
-		      node->flavor == CIL_TYPE_RULE ||
212ad1
-		      node->flavor == CIL_NAMETYPETRANSITION)) {
212ad1
+		if (node->flavor != CIL_TUNABLEIF &&
212ad1
+			node->flavor != CIL_CALL &&
212ad1
+			node->flavor != CIL_CONDBLOCK &&
212ad1
+			node->flavor != CIL_AVRULE &&
212ad1
+			node->flavor != CIL_TYPE_RULE &&
212ad1
+			node->flavor != CIL_NAMETYPETRANSITION) {
212ad1
+			rc = SEPOL_ERR;
212ad1
+		} else if (node->flavor == CIL_AVRULE) {
212ad1
+			struct cil_avrule *rule = node->data;
212ad1
+			if (rule->rule_kind == CIL_AVRULE_NEVERALLOW) {
212ad1
+				rc = SEPOL_ERR;
212ad1
+			}
212ad1
+		}
212ad1
+		if (rc == SEPOL_ERR) {
212ad1
 			if (((struct cil_booleanif*)boolif->data)->preserved_tunable) {
212ad1
 				cil_tree_log(node, CIL_ERR, "%s statement is not allowed in booleanifs (tunableif treated as a booleanif)", cil_node_to_string(node));
212ad1
 			} else {
212ad1
 				cil_tree_log(node, CIL_ERR, "%s statement is not allowed in booleanifs", cil_node_to_string(node));
212ad1
 			}
212ad1
-			rc = SEPOL_ERR;
212ad1
 			goto exit;
212ad1
 		}
212ad1
 	}
212ad1
-- 
212ad1
2.30.2
212ad1