Skip to contentSkip to conversation

libsepol: Allow multiple policycap statements

PATCH In review Inseob Kim 2 participants 4 messages Jun 13, 2025 – Jun 23, 2025 SELinux security module

lore
Share & tools
lore

Inseob Kim

author
#0 lore raw

PATCH v3 1/2 libsepol: Allow multiple policycap statements

Same policycap statements don't conflict each other, so it's fine to allow multiple policycap statements to exist.

Change since v2:
- Remove the parent patch adding NETIF_WILDCARD
- Follow the pattern of SEPOL_EEXIST check and destroy

---
 libsepol/cil/src/cil_build_ast.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
libsepol/cil/src/cil_build_ast.c +11 -2
index b1f151e9..53f825ad 100644
@@ -145,6 +145,9 @@ static int cil_allow_multiple_decls(struct cil_db *db, enum cil_flavor f_new, en
 	case CIL_OPTIONAL:
 		return CIL_TRUE;
 		break;
+	case CIL_POLICYCAP:
+		return CIL_TRUE;
+		break;
 	default:
 		break;
 	}
@@ -5559,8 +5562,14 @@ int cil_gen_policycap(struct cil_db *db, struct cil_tree_node *parse_current, st
 	key = parse_current->next->data;
 
 	rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)polcap, (hashtab_key_t)key, CIL_SYM_POLICYCAPS, CIL_POLICYCAP);
-	if (rc != SEPOL_OK)
-		goto exit;
+	if (rc != SEPOL_OK) {
+		if (rc == SEPOL_EEXIST) {
+			cil_destroy_policycap(polcap);
+			polcap = NULL;
+		} else {
+			goto exit;
+		}
+	}
 
 	return SEPOL_OK;
 
Signature · 1 line
2.50.0.rc2.692.g299adb8693-goog
author
#1 lore raw

PATCH v3 2/2 libsepol: Support genfs_seclabel_wildcard

This adds genfs_seclabel_wildcard support to libsepol, which makes genfs paths wildcard-matched. To support backward compatibility and to keep semantics of genfscon statements, an asterisk '*' is added to paths of genfscon statements when compiling CIL/conf files having policycap genfs_seclabel_wildcard.

Changes since v2:
- Use cil_malloc, not plain malloc

---
 checkpolicy/policy_define.c               | 14 ++++++++++++++
 libsepol/cil/src/cil_binary.c             | 11 ++++++++++-
 libsepol/include/sepol/policydb/polcaps.h |  1 +
 libsepol/src/kernel_to_cil.c              | 20 ++++++++++++++++----
 libsepol/src/kernel_to_conf.c             | 20 ++++++++++++++++----
 libsepol/src/polcaps.c                    |  1 +
 6 files changed, 58 insertions(+), 9 deletions(-)
checkpolicy/policy_define.c +14 -0
index 4e0ddcc6..7029b834 100644
@@ -6010,6 +6010,7 @@ static int define_genfs_context_helper(char *fstype, int has_type)
 	char *type = NULL;
 	const char *sclass;
 	size_t len, len2;
+	int wildcard = ebitmap_get_bit(&policydbp->policycaps, POLICYDB_CAP_GENFS_SECLABEL_WILDCARD);
 
 	if (policydbp->target_platform != SEPOL_TARGET_SELINUX) {
 		yyerror("genfs not supported for target");
@@ -6060,6 +6061,19 @@ static int define_genfs_context_helper(char *fstype, int has_type)
 	newc->u.name = (char *)queue_remove(id_queue);
 	if (!newc->u.name)
 		goto fail;
+
+	if (wildcard) {
+		size_t name_len = strlen(newc->u.name);
+		newc->u.name = realloc(newc->u.name, name_len + 2);
+		if (newc->u.name == NULL) {
+			yyerror("out of memory");
+			return -1;
+		}
+
+		newc->u.name[name_len] = '*';
+		newc->u.name[name_len + 1] = '\0';
+	}
+
 	if (has_type) {
 		type = (char *)queue_remove(id_queue);
 		if (!type)
libsepol/cil/src/cil_binary.c +10 -1
index 6b17e7f4..74ec3cd5 100644
@@ -3770,6 +3770,7 @@ int cil_genfscon_to_policydb(policydb_t *pdb, struct cil_sort *genfscons)
 	uint32_t i = 0;
 	genfs_t *genfs_tail = NULL;
 	ocontext_t *ocon_tail = NULL;
+	int wildcard = ebitmap_get_bit(&pdb->policycaps, POLICYDB_CAP_GENFS_SECLABEL_WILDCARD);
 
 	for (i = 0; i < genfscons->count; i++) {
 		struct cil_genfscon *cil_genfscon = genfscons->array[i];
@@ -3794,7 +3795,15 @@ int cil_genfscon_to_policydb(policydb_t *pdb, struct cil_sort *genfscons)
 
 		ocon_tail = new_ocon;
 
-		new_ocon->u.name = cil_strdup(cil_genfscon->path_str);
+		if (wildcard) {
+			size_t name_len = strlen(cil_genfscon->path_str);
+			new_ocon->u.name = cil_malloc(name_len + 2);
+			memcpy(new_ocon->u.name, cil_genfscon->path_str, name_len);
+			new_ocon->u.name[name_len] = '*';
+			new_ocon->u.name[name_len + 1] = '\0';
+		} else {
+			new_ocon->u.name = cil_strdup(cil_genfscon->path_str);
+		}
 
 		if (cil_genfscon->file_type != CIL_FILECON_ANY) {
 			class_datum_t *class_datum;
libsepol/include/sepol/policydb/polcaps.h +1 -0
index 1aa9b30a..72147665 100644
@@ -17,6 +17,7 @@ enum {
 	POLICYDB_CAP_IOCTL_SKIP_CLOEXEC,
 	POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT,
 	POLICYDB_CAP_NETLINK_XPERM,
+	POLICYDB_CAP_GENFS_SECLABEL_WILDCARD,
 	__POLICYDB_CAP_MAX
 };
 #define POLICYDB_CAP_MAX (__POLICYDB_CAP_MAX - 1)
libsepol/src/kernel_to_cil.c +16 -4
index 6fea2c6f..4da63ba5 100644
@@ -2750,6 +2750,8 @@ static int write_genfscon_rules_to_cil(FILE *out, struct policydb *pdb)
 	uint32_t sclass;
 	const char *file_type;
 	int rc;
+	int wildcard = ebitmap_get_bit(&pdb->policycaps, POLICYDB_CAP_GENFS_SECLABEL_WILDCARD);
+	size_t name_len;
 
 	rc = strs_init(&strs, 32);
 	if (rc != 0) {
@@ -2791,12 +2793,22 @@ static int write_genfscon_rules_to_cil(FILE *out, struct policydb *pdb)
 				goto exit;
 			}
 
+			name_len = strlen(name);
+			if (wildcard) {
+				if (name_len == 0 || name[name_len - 1] != '*') {
+					ERR(NULL, "genfscon path must end with '*' when genfs_seclabel_wildcard");
+					rc = -1;
+					goto exit;
+				}
+				--name_len;
+			}
+
 			if (file_type) {
-				rc = strs_create_and_add(strs, "(genfscon %s \"%s\" %s %s)",
-										 fstype, name, file_type, ctx);
+				rc = strs_create_and_add(strs, "(genfscon %s \"%.*s\" %s %s)",
+				                         fstype, (int)name_len, name, file_type, ctx);
 			} else {
-				rc = strs_create_and_add(strs, "(genfscon %s \"%s\" %s)",
-										 fstype, name, ctx);
+				rc = strs_create_and_add(strs, "(genfscon %s \"%.*s\" %s)",
+				                         fstype, (int)name_len, name, ctx);
 			}
 			free(ctx);
 			if (rc != 0) {
libsepol/src/kernel_to_conf.c +16 -4
index b3b0f224..6d608443 100644
@@ -2598,6 +2598,8 @@ static int write_genfscon_rules_to_conf(FILE *out, struct policydb *pdb)
 	uint32_t sclass;
 	const char *file_type;
 	int rc;
+	int wildcard = ebitmap_get_bit(&pdb->policycaps, POLICYDB_CAP_GENFS_SECLABEL_WILDCARD);
+	size_t name_len;
 
 	rc = strs_init(&strs, 32);
 	if (rc != 0) {
@@ -2639,12 +2641,22 @@ static int write_genfscon_rules_to_conf(FILE *out, struct policydb *pdb)
 				goto exit;
 			}
 
+			name_len = strlen(name);
+			if (wildcard) {
+				if (name_len == 0 || name[name_len - 1] != '*') {
+					ERR(NULL, "genfscon path must end with '*' when genfs_seclabel_wildcard");
+					rc = -1;
+					goto exit;
+				}
+				--name_len;
+			}
+
 			if (file_type) {
-				rc = strs_create_and_add(strs, "genfscon %s \"%s\" %s %s",
-										 fstype, name, file_type, ctx);
+				rc = strs_create_and_add(strs, "genfscon %s \"%.*s\" %s %s",
+				                         fstype, (int)name_len, name, file_type, ctx);
 			} else {
-				rc = strs_create_and_add(strs, "genfscon %s \"%s\" %s",
-										 fstype, name, ctx);
+				rc = strs_create_and_add(strs, "genfscon %s \"%.*s\" %s",
+				                         fstype, (int)name_len, name, ctx);
 			}
 			free(ctx);
 			if (rc != 0) {
libsepol/src/polcaps.c +1 -0
index 6b28c84e..0bd26df8 100644
@@ -16,6 +16,7 @@ static const char * const polcap_names[POLICYDB_CAP_MAX + 1] = {
 	[POLICYDB_CAP_IOCTL_SKIP_CLOEXEC]		= "ioctl_skip_cloexec",
 	[POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT]	= "userspace_initial_context",
 	[POLICYDB_CAP_NETLINK_XPERM]			= "netlink_xperm",
+	[POLICYDB_CAP_GENFS_SECLABEL_WILDCARD]		= "genfs_seclabel_wildcard",
 };
 
 int sepol_polcap_getnum(const char *name)
Signature · 1 line
2.50.0.rc2.692.g299adb8693-goog
Acked-by on v3 1/2
#2 lore raw
In reply to Inseob Kim
Quoted 6 lines · Inseob Kim

Same policycap statements don't conflict each other, so it's fine to allow multiple policycap statements to exist.

For these two patches:

Quoted 42 lines

Change since v2:
- Remove the parent patch adding NETIF_WILDCARD
- Follow the pattern of SEPOL_EEXIST check and destroy

---
 libsepol/cil/src/cil_build_ast.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
libsepol/cil/src/cil_build_ast.c +11 -3
index b1f151e9..53f825ad 100644
@@ -145,6 +145,9 @@ static int cil_allow_multiple_decls(struct cil_db *db, enum cil_flavor f_new, en
        case CIL_OPTIONAL:
                return CIL_TRUE;
                break;
+       case CIL_POLICYCAP:
+               return CIL_TRUE;
+               break;
        default:
                break;
        }
@@ -5559,8 +5562,14 @@ int cil_gen_policycap(struct cil_db *db, struct cil_tree_node *parse_current, st
        key = parse_current->next->data;
 
        rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)polcap, (hashtab_key_t)key, CIL_SYM_POLICYCAPS, CIL_POLICYCAP);
-       if (rc != SEPOL_OK)
-               goto exit;
+       if (rc != SEPOL_OK) {
+               if (rc == SEPOL_EEXIST) {
+                       cil_destroy_policycap(polcap);
+                       polcap = NULL;
+               } else {
+                       goto exit;
+               }
+       }
 
        return SEPOL_OK;
 
--

2.50.0.rc2.692.g299adb8693-goog

#3 lore raw
Quoted 12 lines · James Carter
Quoted 6 lines · Inseob Kim

Same policycap statements don't conflict each other, so it's fine to allow multiple policycap statements to exist.

For these two patches:

These two patches have been merged.
Thanks,
Jim

Quoted 42 lines
Quoted 42 lines

Change since v2:
- Remove the parent patch adding NETIF_WILDCARD
- Follow the pattern of SEPOL_EEXIST check and destroy

---
 libsepol/cil/src/cil_build_ast.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
libsepol/cil/src/cil_build_ast.c +11 -3
index b1f151e9..53f825ad 100644
@@ -145,6 +145,9 @@ static int cil_allow_multiple_decls(struct cil_db *db, enum cil_flavor f_new, en
        case CIL_OPTIONAL:
                return CIL_TRUE;
                break;
+       case CIL_POLICYCAP:
+               return CIL_TRUE;
+               break;
        default:
                break;
        }
@@ -5559,8 +5562,14 @@ int cil_gen_policycap(struct cil_db *db, struct cil_tree_node *parse_current, st
        key = parse_current->next->data;
 
        rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)polcap, (hashtab_key_t)key, CIL_SYM_POLICYCAPS, CIL_POLICYCAP);
-       if (rc != SEPOL_OK)
-               goto exit;
+       if (rc != SEPOL_OK) {
+               if (rc == SEPOL_EEXIST) {
+                       cil_destroy_policycap(polcap);
+                       polcap = NULL;
+               } else {
+                       goto exit;
+               }
+       }
 
        return SEPOL_OK;
 
--

2.50.0.rc2.692.g299adb8693-goog