Skip to contentSkip to conversation

checkpolicy: drop unused token CLONE

PATCH In review Christian Göttsche 2 participants 6 messages May 12, 2023 – Jun 5, 2023 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 checkpolicy: drop unused token CLONE

The token CLONE is never used in the grammar; drop it.

As side effect clone and CLONE become available as identifier names.

---
 checkpolicy/policy_parse.y | 1 -
 checkpolicy/policy_scan.l  | 2 --
 2 files changed, 3 deletions(-)
checkpolicy/policy_parse.y +0 -1
index 45f973ff..da32a776 100644
@@ -85,7 +85,6 @@ typedef int (* require_func_t)(int pass);
 %token PATH
 %token QPATH
 %token FILENAME
-%token CLONE
 %token COMMON
 %token CLASS
 %token CONSTRAIN
checkpolicy/policy_scan.l +0 -2
index 9fefea7b..2c025b61 100644
@@ -77,8 +77,6 @@ hexval	[0-9A-Fa-f]
 				      source_lineno++;
 				  yyless(1);
 				}
-CLONE |
-clone				{ return(CLONE); }
 COMMON |
 common				{ return(COMMON); }
 CLASS |
Signature · 1 line
2.40.1
#1 lore raw

PATCH 2/4 checkpolicy: reject condition with bool and tunable in expression

If tunables are not preserved (the mode unconditionally used by checkpolicy) an expression must not consist of booleans and tunables, since such expressions are not supported during expansion (see expand.c:
discard_tunables()).

---
 checkpolicy/policy_define.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
checkpolicy/policy_define.c +16 -1
index d4e376ad..95cd5c85 100644
@@ -3964,8 +3964,9 @@ uintptr_t define_cexpr(uint32_t expr_type, uintptr_t arg1, uintptr_t arg2)
 int define_conditional(cond_expr_t * expr, avrule_t * t, avrule_t * f)
 {
 	cond_expr_t *e;
-	int depth;
+	int depth, booleans, tunables;
 	cond_node_t cn, *cn_old;
+	const cond_bool_datum_t *bool_var;
 
 	/* expression cannot be NULL */
 	if (!expr) {
@@ -3990,6 +3991,8 @@ int define_conditional(cond_expr_t * expr, avrule_t * t, avrule_t * f)
 
 	/* verify expression */
 	depth = -1;
+	booleans = 0;
+	tunables = 0;
 	for (e = expr; e; e = e->next) {
 		switch (e->expr_type) {
 		case COND_NOT:
@@ -4018,6 +4021,14 @@ int define_conditional(cond_expr_t * expr, avrule_t * t, avrule_t * f)
 				return -1;
 			}
 			depth++;
+
+			bool_var = policydbp->bool_val_to_struct[e->boolean - 1];
+			if (bool_var->flags & COND_BOOL_FLAGS_TUNABLE) {
+				tunables = 1;
+			} else {
+				booleans = 1;
+			}
+
 			break;
 		default:
 			yyerror("illegal conditional expression");
@@ -4028,6 +4039,10 @@ int define_conditional(cond_expr_t * expr, avrule_t * t, avrule_t * f)
 		yyerror("illegal conditional expression");
 		return -1;
 	}
+	if (booleans && tunables) {
+		yyerror("illegal conditional expression; Contains boolean and tunable");
+		return -1;
+	}
 
 	/*  use tmp conditional node to partially build new node */
 	memset(&cn, 0, sizeof(cn));
Signature · 1 line
2.40.1
#2 lore raw

PATCH 3/4 checkpolicy: only set declared permission bits for wildcards

In reply to Christian Göttsche

When setting permission bits from a wildcard or complement only set the bits for permissions actually declared for the associated class. This helps optimizing the policy later, since only rules are dropped with a complete empty permission bitset. Example policy:

    class CLASS1
    sid kernel
    class CLASS1 { PERM1 }
    type TYPE1;
    bool BOOL1 true;
    allow TYPE1 self : CLASS1 { PERM1 };
    role ROLE1;
    role ROLE1 types { TYPE1 };
    if ! BOOL1 { allow TYPE1 self: CLASS1 *; }
    user USER1 roles ROLE1;
    sid kernel USER1:ROLE1:TYPE1

Also emit a warning if a rule will have an empty permission bitset due to an exhausting complement.

---
 checkpolicy/policy_define.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
checkpolicy/policy_define.c +14 -5
index 95cd5c85..cef8f3c4 100644
@@ -2511,6 +2511,8 @@ int define_te_avtab_extended_perms(int which)
 	return rc;
 }
 
+#define PERMISSION_MASK(nprim) ((nprim) == PERM_SYMTAB_SIZE ? (~UINT32_C(0)) : ((UINT32_C(1) << (nprim)) - 1))
+
 static int define_te_avtab_helper(int which, avrule_t ** rule)
 {
 	char *id;
@@ -2616,8 +2618,8 @@ static int define_te_avtab_helper(int which, avrule_t ** rule)
 			cladatum = policydbp->class_val_to_struct[i];
 
 			if (strcmp(id, "*") == 0) {
-				/* set all permissions in the class */
-				cur_perms->data = ~0U;
+				/* set all declared permissions in the class */
+				cur_perms->data = PERMISSION_MASK(cladatum->permissions.nprim);
 				goto next;
 			}
 
@@ -2625,7 +2627,16 @@ static int define_te_avtab_helper(int which, avrule_t ** rule)
 				/* complement the set */
 				if (which == AVRULE_DONTAUDIT)
 					yywarn("dontaudit rule with a ~?");
-				cur_perms->data = ~cur_perms->data;
+				cur_perms->data = ~cur_perms->data & PERMISSION_MASK(cladatum->permissions.nprim);
+				if (cur_perms->data == 0) {
+					class_perm_node_t *tmp = cur_perms;
+					yywarn("omitting avrule with no permission set");
+					if (perms == cur_perms)
+						perms = cur_perms->next;
+					cur_perms = cur_perms->next;
+					free(tmp);
+					continue;
+				}
 				goto next;
 			}
 
@@ -3549,8 +3560,6 @@ static constraint_expr_t *constraint_expr_clone(const constraint_expr_t * expr)
 	return NULL;
 }
 
-#define PERMISSION_MASK(nprim) ((nprim) == PERM_SYMTAB_SIZE ? (~UINT32_C(0)) : ((UINT32_C(1) << (nprim)) - 1))
-
 int define_constraint(constraint_expr_t * expr)
 {
 	struct constraint_node *node;
Signature · 1 line
2.40.1
#3 lore raw

PATCH 4/4 libsepol: dump non-mls validatetrans rules as such

In reply to Christian Göttsche

The functions constraint_expr_to_str() prepare a string representation for validatetrans and mlsvalidatetrans rules. To decide what keyword to use the type of expression is consulted. Currently the extra target type (CEXPR_XTARGET) is considered to be an MLS statement while its not, e.g.:

    validatetrans CLASS1 t3 == ATTR1;

Actually check for MLS expression types only.

---
 libsepol/src/kernel_to_cil.c  | 2 +-
 libsepol/src/kernel_to_conf.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
libsepol/src/kernel_to_cil.c +1 -1
index e9cd89c2..7e279e3f 100644
@@ -172,7 +172,7 @@ static char *constraint_expr_to_str(struct policydb *pdb, struct constraint_expr
 				goto exit;
 			}
 
-			if (curr->attr >= CEXPR_XTARGET) {
+			if (curr->attr >= CEXPR_L1L2) {
 				*use_mls = 1;
 			}
 
libsepol/src/kernel_to_conf.c +1 -1
index c48a7114..4c93cc10 100644
@@ -169,7 +169,7 @@ static char *constraint_expr_to_str(struct policydb *pdb, struct constraint_expr
 				goto exit;
 			}
 
-			if (curr->attr >= CEXPR_XTARGET) {
+			if (curr->attr >= CEXPR_L1L2) {
 				*use_mls = 1;
 			}
 
Signature · 1 line
2.40.1
Acked-by on v1 1/4
#4 lore raw
In reply to Christian Göttsche
Quoted 6 lines · Christian Göttsche

The token CLONE is never used in the grammar; drop it.

As side effect clone and CLONE become available as identifier names.

For these four patches:

Quoted 33 lines
---
 checkpolicy/policy_parse.y | 1 -
 checkpolicy/policy_scan.l  | 2 --
 2 files changed, 3 deletions(-)
checkpolicy/policy_parse.y +0 -1
index 45f973ff..da32a776 100644
@@ -85,7 +85,6 @@ typedef int (* require_func_t)(int pass);
 %token PATH
 %token QPATH
 %token FILENAME
-%token CLONE
 %token COMMON
 %token CLASS
 %token CONSTRAIN
checkpolicy/policy_scan.l +0 -3
index 9fefea7b..2c025b61 100644
@@ -77,8 +77,6 @@ hexval        [0-9A-Fa-f]
                                      source_lineno++;
                                  yyless(1);
                                }
-CLONE |
-clone                          { return(CLONE); }
 COMMON |
 common                         { return(COMMON); }
 CLASS |
--

2.40.1

#5 lore raw
Quoted 4 lines · James Carter
Quoted 1 line · Christian Göttsche
Quoted 9 lines
Quoted 5 lines

The token CLONE is never used in the grammar; drop it.

As side effect clone and CLONE become available as identifier names.

For these four patches:

These four patches have been merged.
Thanks,
Jim

Quoted 33 lines
Quoted 33 lines
---
 checkpolicy/policy_parse.y | 1 -
 checkpolicy/policy_scan.l  | 2 --
 2 files changed, 3 deletions(-)
checkpolicy/policy_parse.y +0 -1
index 45f973ff..da32a776 100644
@@ -85,7 +85,6 @@ typedef int (* require_func_t)(int pass);
 %token PATH
 %token QPATH
 %token FILENAME
-%token CLONE
 %token COMMON
 %token CLASS
 %token CONSTRAIN
checkpolicy/policy_scan.l +0 -3
index 9fefea7b..2c025b61 100644
@@ -77,8 +77,6 @@ hexval        [0-9A-Fa-f]
                                      source_lineno++;
                                  yyless(1);
                                }
-CLONE |
-clone                          { return(CLONE); }
 COMMON |
 common                         { return(COMMON); }
 CLASS |
--

2.40.1