Skip to contentSkip to conversation

libsepol/cil: Optionally allow duplicate role declarations

PATCH In review James Carter 1 participant 5 messages Nov 4, 2024 – Nov 18, 2024 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 libsepol/cil: Optionally allow duplicate role declarations

Allow duplicate role declarations (along with duplicate type and type attribute declarations and context rules) if the multiple_decls field in the CIL db has been set. This field can be set by a call to cil_set_multiple_decls().

---
 libsepol/cil/src/cil_build_ast.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
libsepol/cil/src/cil_build_ast.c +7 -1
index 19fbb04e..80e9c679 100644
@@ -137,6 +137,7 @@ static int cil_allow_multiple_decls(struct cil_db *db, enum cil_flavor f_new, en
 	switch (f_new) {
 	case CIL_TYPE:
 	case CIL_TYPEATTRIBUTE:
+	case CIL_ROLE:
 		if (db->multiple_decls) {
 			return CIL_TRUE;
 		}
@@ -1744,7 +1745,12 @@ int cil_gen_role(struct cil_db *db, struct cil_tree_node *parse_current, struct
 
 	rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)role, (hashtab_key_t)key, CIL_SYM_ROLES, CIL_ROLE);
 	if (rc != SEPOL_OK) {
-		goto exit;
+		if (rc == SEPOL_EEXIST) {
+			cil_destroy_role(role);
+			role = NULL;
+		} else {
+			goto exit;
+		}
 	}
 
 	return SEPOL_OK;
Signature · 1 line
2.47.0
#1 lore raw

PATCH 2/3 libsemanage: Optionally allow duplicate declarations

Add a configuration option that when set to "true" allows duplicate type, type attribute, and role declarations and duplicate context rules.

The default is set to "true" to support the removal of the special handling of certain roles when converting a policy module to CIL without causing problems for existing policies.

---
 libsemanage/man/man5/semanage.conf.5 |  5 +++++
 libsemanage/src/conf-parse.y         | 15 ++++++++++++++-
 libsemanage/src/conf-scan.l          |  1 +
 libsemanage/src/direct_api.c         |  1 +
 libsemanage/src/semanage_conf.h      |  1 +
 5 files changed, 22 insertions(+), 1 deletion(-)
libsemanage/man/man5/semanage.conf.5 +5 -0
index 380b58be..71712562 100644
@@ -126,6 +126,11 @@ In order to compile the original HLL file into CIL, the same HLL file will need
 When set to "true", the kernel policy will be optimized upon rebuilds.
 It can be set to either "true" or "false" and by default it is set to "false".
 
+.TP
+.B multiple-decls
+When set to "true", duplicate type, type attribute, and role declarations will be allowed.
+It can be set to either "true" or "false" and by default it is set to "true".
+
 .SH "SEE ALSO"
 .TP
 semanage(8)
libsemanage/src/conf-parse.y +14 -1
index eac91344..beab85a2 100644
@@ -59,7 +59,7 @@ static int parse_errors;
         char *s;
 }
 
-%token MODULE_STORE VERSION EXPAND_CHECK FILE_MODE SAVE_PREVIOUS SAVE_LINKED TARGET_PLATFORM COMPILER_DIR IGNORE_MODULE_CACHE STORE_ROOT OPTIMIZE_POLICY
+%token MODULE_STORE VERSION EXPAND_CHECK FILE_MODE SAVE_PREVIOUS SAVE_LINKED TARGET_PLATFORM COMPILER_DIR IGNORE_MODULE_CACHE STORE_ROOT OPTIMIZE_POLICY MULTIPLE_DECLS
 %token LOAD_POLICY_START SETFILES_START SEFCONTEXT_COMPILE_START DISABLE_GENHOMEDIRCON HANDLE_UNKNOWN USEPASSWD IGNOREDIRS
 %token BZIP_BLOCKSIZE BZIP_SMALL REMOVE_HLL
 %token VERIFY_MOD_START VERIFY_LINKED_START VERIFY_KERNEL_START BLOCK_END
@@ -96,6 +96,7 @@ single_opt:     module_store
 	|	bzip_small
 	|	remove_hll
 	|	optimize_policy
+	|	multiple_decls
         ;
 
 module_store:   MODULE_STORE '=' ARG {
@@ -280,6 +281,17 @@ optimize_policy:  OPTIMIZE_POLICY '=' ARG {
 	free($3);
 }
 
+multiple_decls:  MULTIPLE_DECLS '=' ARG {
+	if (strcasecmp($3, "false") == 0) {
+		current_conf->multiple_decls = 0;
+	} else if (strcasecmp($3, "true") == 0) {
+		current_conf->multiple_decls = 1;
+	} else {
+		yyerror("multiple-decls can only be 'true' or 'false'");
+	}
+	free($3);
+}
+
 command_block: 
                 command_start external_opts BLOCK_END  {
                         if (new_external->path == NULL) {
@@ -365,6 +377,7 @@ static int semanage_conf_init(semanage_conf_t * conf)
 	conf->ignore_module_cache = 0;
 	conf->remove_hll = 0;
 	conf->optimize_policy = 0;
+	conf->multiple_decls = 1;
 
 	conf->save_previous = 0;
 	conf->save_linked = 0;
libsemanage/src/conf-scan.l +1 -0
index b06a896c..6438516b 100644
@@ -55,6 +55,7 @@ bzip-blocksize	return BZIP_BLOCKSIZE;
 bzip-small	return BZIP_SMALL;
 remove-hll	return REMOVE_HLL;
 optimize-policy return OPTIMIZE_POLICY;
+multiple-decls return MULTIPLE_DECLS;
 "[load_policy]"   return LOAD_POLICY_START;
 "[setfiles]"      return SETFILES_START;
 "[sefcontext_compile]"      return SEFCONTEXT_COMPILE_START;
libsemanage/src/direct_api.c +1 -0
index 7631c7bf..43ab2f4c 100644
@@ -1346,6 +1346,7 @@ static int semanage_direct_commit(semanage_handle_t * sh)
 		cil_set_preserve_tunables(cildb, preserve_tunables);
 		cil_set_target_platform(cildb, sh->conf->target_platform);
 		cil_set_policy_version(cildb, sh->conf->policyvers);
+		cil_set_multiple_decls(cildb, sh->conf->multiple_decls);
 
 		if (sh->conf->handle_unknown != -1) {
 			cil_set_handle_unknown(cildb, sh->conf->handle_unknown);
libsemanage/src/semanage_conf.h +1 -0
index 23c4b8b4..5db08f0c 100644
@@ -48,6 +48,7 @@ typedef struct semanage_conf {
 	int remove_hll;
 	int ignore_module_cache;
 	int optimize_policy;
+	int multiple_decls;
 	char *ignoredirs;	/* ";" separated of list for genhomedircon to ignore */
 	struct external_prog *load_policy;
 	struct external_prog *setfiles;
Signature · 1 line
2.47.0
#2 lore raw

PATCH 3/3 libsepol: Remove special handling of roles in module_to_cil.c

In reply to James Carter

Certain roles (user_r, staff_r, sysadm_r, system_r, unconfined_r, auditadm_r, and secadm_r) have always been handled in a special way when converting a policy module to CIL to avoid having duplicate role declarations. By optionally allowing duplicate role declarations in CIL and by creating an option in libsemanage to make use of duplicate declaration support, the special handling of these roles can be removed.

Remove the special handling of certain roles in module_to_cil.c.

---
 libsepol/src/module_to_cil.c | 50 +-----------------------------------
 1 file changed, 1 insertion(+), 49 deletions(-)
libsepol/src/module_to_cil.c +1 -49
index 79636897..0ede0c9b 100644
@@ -2174,39 +2174,7 @@ static int role_to_cil(int indent, struct policydb *pdb, struct avrule_block *UN
 	switch (role->flavor) {
 	case ROLE_ROLE:
 		if (scope == SCOPE_DECL) {
-			// Only declare certain roles if we are reading a base module.
-			// These roles are defined in the base module and sometimes in
-			// other non-base modules. If we generated the roles regardless of
-			// the policy type, it would result in duplicate declarations,
-			// which isn't allowed in CIL. Patches have been made to refpolicy
-			// to remove these duplicate role declarations, but we need to be
-			// backwards compatible and support older policies. Since we know
-			// these roles are always declared in base, only print them when we
-			// see them in the base module. If the declarations appear in a
-			// non-base module, ignore their declarations.
-			//
-			// Note that this is a hack, and if a policy author does not define
-			// one of these roles in base, the declaration will not appear in
-			// the resulting policy, likely resulting in a compilation error in
-			// CIL.
-			//
-			// To make things more complicated, the auditadm_r and secadm_r
-			// roles could actually be in either the base module or a non-base
-			// module, or both. So we can't rely on this same behavior. So for
-			// these roles, don't declare them here, even if they are in a base
-			// or non-base module. Instead we will just declare them in the
-			// base module elsewhere.
-			int is_base_role = (!strcmp(key, "user_r") ||
-			                    !strcmp(key, "staff_r") ||
-			                    !strcmp(key, "sysadm_r") ||
-			                    !strcmp(key, "system_r") ||
-			                    !strcmp(key, "unconfined_r"));
-			int is_builtin_role = (!strcmp(key, "auditadm_r") ||
-			                       !strcmp(key, "secadm_r"));
-			if ((is_base_role && pdb->policy_type == SEPOL_POLICY_BASE) ||
-			    (!is_base_role && !is_builtin_role)) {
-				cil_println(indent, "(role %s)", key);
-			}
+			cil_println(indent, "(role %s)", key);
 		}
 
 		if (ebitmap_cardinality(&role->dominates) > 1) {
@@ -3992,17 +3960,6 @@ static int generate_default_object(void)
 	return 0;
 }
 
-static int generate_builtin_roles(void)
-{
-	// due to inconsistentencies between policies and CIL not allowing
-	// duplicate roles, some roles are always created, regardless of if they
-	// are declared in modules or not
-	cil_println(0, "(role auditadm_r)");
-	cil_println(0, "(role secadm_r)");
-
-	return 0;
-}
-
 static int generate_gen_require_attribute(void)
 {
 	cil_println(0, "(typeattribute " GEN_REQUIRE_ATTR ")");
@@ -4087,11 +4044,6 @@ int sepol_module_policydb_to_cil(FILE *fp, struct policydb *pdb, int linked)
 			goto exit;
 		}
 
-		rc = generate_builtin_roles();
-		if (rc != 0) {
-			goto exit;
-		}
-
 		// default attribute to be used to mimic gen_require in CIL
 		rc = generate_gen_require_attribute();
 		if (rc != 0) {
Signature · 1 line
2.47.0
#3 lore raw
In reply to James Carter
Quoted 7 lines · James Carter

Allow duplicate role declarations (along with duplicate type and type attribute declarations and context rules) if the multiple_decls field in the CIL db has been set. This field can be set by a call to cil_set_multiple_decls().

I plan on merging these three patches soon.
Jim

Quoted 33 lines
---
 libsepol/cil/src/cil_build_ast.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
libsepol/cil/src/cil_build_ast.c +7 -2
index 19fbb04e..80e9c679 100644
@@ -137,6 +137,7 @@ static int cil_allow_multiple_decls(struct cil_db *db, enum cil_flavor f_new, en
        switch (f_new) {
        case CIL_TYPE:
        case CIL_TYPEATTRIBUTE:
+       case CIL_ROLE:
                if (db->multiple_decls) {
                        return CIL_TRUE;
                }
@@ -1744,7 +1745,12 @@ int cil_gen_role(struct cil_db *db, struct cil_tree_node *parse_current, struct
 
        rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)role, (hashtab_key_t)key, CIL_SYM_ROLES, CIL_ROLE);
        if (rc != SEPOL_OK) {
-               goto exit;
+               if (rc == SEPOL_EEXIST) {
+                       cil_destroy_role(role);
+                       role = NULL;
+               } else {
+                       goto exit;
+               }
        }
 
        return SEPOL_OK;
--

2.47.0

#4 lore raw
Quoted 8 lines · James Carter
Quoted 6 lines · James Carter

Allow duplicate role declarations (along with duplicate type and type attribute declarations and context rules) if the multiple_decls field in the CIL db has been set. This field can be set by a call to cil_set_multiple_decls().

Quoted 5 lines
Quoted 1 line

I plan on merging these three patches soon.
Jim

These three patches have been merged.
Jim

Quoted 33 lines
Quoted 33 lines
---
 libsepol/cil/src/cil_build_ast.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
libsepol/cil/src/cil_build_ast.c +7 -2
index 19fbb04e..80e9c679 100644
@@ -137,6 +137,7 @@ static int cil_allow_multiple_decls(struct cil_db *db, enum cil_flavor f_new, en
        switch (f_new) {
        case CIL_TYPE:
        case CIL_TYPEATTRIBUTE:
+       case CIL_ROLE:
                if (db->multiple_decls) {
                        return CIL_TRUE;
                }
@@ -1744,7 +1745,12 @@ int cil_gen_role(struct cil_db *db, struct cil_tree_node *parse_current, struct
 
        rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)role, (hashtab_key_t)key, CIL_SYM_ROLES, CIL_ROLE);
        if (rc != SEPOL_OK) {
-               goto exit;
+               if (rc == SEPOL_EEXIST) {
+                       cil_destroy_role(role);
+                       role = NULL;
+               } else {
+                       goto exit;
+               }
        }
 
        return SEPOL_OK;
--

2.47.0