Skip to contentSkip to conversation

Allow type attributes to be associated with other type attributes

PATCH Superseded James Carter 1 participant 1 message Dec 11, 2025 SELinux security module

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Allow type attributes to be associated with other type attributes

Allow the following:
attribute at1;
attribute at2;
typeattribute at2 at1;

This associates the attribute at2 with the attribute at1. Any types associated with at2 will be associated with at1 as well when the policy is expanded. A loop in the associations between attributes will produce an error.

The kernel binary policy is not effected by this change because all attribute associations are expanded when the policy is expanded. The modular policy is effected because compiled modules and the linked modular policy can now have type attributes associated with other type attributes, so a new module version is needed to prevent an older policy toolchain from creating a kernel binary policy that has type attributes associated with other attributes.

Also validate that the types field of a type datum for an attribute is empty for kernel policies and does not contain an attribute for modular policies before this new version.

This was suggested by Vit Mojzis <[email protected]> who also provided an initial patch for checkpolicy.

---
 checkpolicy/policy_define.c                |  9 +++-
 libsepol/include/sepol/policydb/policydb.h |  3 +-
 libsepol/src/expand.c                      | 54 ++++++++++++++++++++++
 libsepol/src/policydb.c                    | 25 ++++++++--
 libsepol/src/policydb_validate.c           | 30 +++++++++++-
 5 files changed, 113 insertions(+), 8 deletions(-)
checkpolicy/policy_define.c +8 -1
index 7029b834..a79b944c 100644
@@ -1440,11 +1440,18 @@ int define_typeattribute(void)
 		return -1;
 	}
 	t = hashtab_search(policydbp->p_types.table, id);
-	if (!t || t->flavor == TYPE_ATTRIB) {
+	if (!t) {
 		yyerror2("unknown type %s", id);
 		free(id);
 		return -1;
+	} else if (t->flavor == TYPE_ATTRIB) {
+		if (policydbp->policy_type != POLICY_KERN && policydbp->policyvers < MOD_POLICYDB_VERSION_TYPE_ATTR_ATTRS) {
+			yyerror2("Type attributes cannot be used in a typeattribute definition in policy version %d", policydbp->policyvers);
+			free(id);
+			return -1;
+		}
 	}
+
 	free(id);
 
 	while ((id = queue_remove(id_queue))) {
libsepol/include/sepol/policydb/policydb.h +2 -1
index 9875255c..4107b662 100644
@@ -802,9 +802,10 @@ extern int policydb_set_target_platform(policydb_t *p, int platform);
 #define MOD_POLICYDB_VERSION_SELF_TYPETRANS	21
 #define MOD_POLICYDB_VERSION_COND_XPERMS	22
 #define MOD_POLICYDB_VERSION_NEVERAUDIT		23
+#define MOD_POLICYDB_VERSION_TYPE_ATTR_ATTRS 24
 
 #define MOD_POLICYDB_VERSION_MIN MOD_POLICYDB_VERSION_BASE
-#define MOD_POLICYDB_VERSION_MAX MOD_POLICYDB_VERSION_NEVERAUDIT
+#define MOD_POLICYDB_VERSION_MAX MOD_POLICYDB_VERSION_TYPE_ATTR_ATTRS
 
 #define POLICYDB_CONFIG_MLS    1
 
libsepol/src/expand.c +54 -0

66 lines not on this page · raw message

libsepol/src/policydb.c +21 -4
index 8a81687d..6aaa18f2 100644
@@ -362,6 +362,13 @@ static const struct policydb_compat_info policydb_compat[] = {
 	 .ocon_num = OCON_IBENDPORT + 1,
 	 .target_platform = SEPOL_TARGET_SELINUX,
 	},
+	{
+	 .type = POLICY_BASE,
+	 .version = MOD_POLICYDB_VERSION_TYPE_ATTR_ATTRS,
+	 .sym_num = SYM_NUM,
+	 .ocon_num = OCON_IBENDPORT + 1,
+	 .target_platform = SEPOL_TARGET_SELINUX,
+	},
 	{
 	 .type = POLICY_MOD,
 	 .version = MOD_POLICYDB_VERSION_BASE,
@@ -502,6 +509,13 @@ static const struct policydb_compat_info policydb_compat[] = {
 	 .ocon_num = 0,
 	 .target_platform = SEPOL_TARGET_SELINUX,
 	},
+	{
+	 .type = POLICY_MOD,
+	 .version = MOD_POLICYDB_VERSION_TYPE_ATTR_ATTRS,
+	 .sym_num = SYM_NUM,
+	 .ocon_num = 0,
+	 .target_platform = SEPOL_TARGET_SELINUX,
+	},
 };
 
 #if 0
@@ -4492,16 +4506,19 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose)
 			if (r_policyvers >= POLICYDB_VERSION_AVTAB) {
 				if (ebitmap_read(&p->type_attr_map[i], fp))
 					goto bad;
-				ebitmap_for_each_positive_bit(&p->type_attr_map[i],
-							 tnode, j) {
+				ebitmap_for_each_positive_bit(&p->type_attr_map[i], tnode, j) {
 					if (i == j)
 						continue;
 
 					if (j >= p->p_types.nprim)
 						goto bad;
 
-					if (ebitmap_set_bit
-					    (&p->attr_type_map[j], i, 1))
+					if (p->type_val_to_struct[i]->flavor == TYPE_ATTRIB) {
+						ERR(fp->handle, "Invalid to have type attributes associated with an attribute for a kernel policy");
+						goto bad;
+					}
+
+					if (ebitmap_set_bit(&p->attr_type_map[j], i, 1))
 						goto bad;
 				}
 			}
libsepol/src/policydb_validate.c +28 -2
index d441526e..38b89495 100644
@@ -566,6 +566,27 @@ bad:
 	return -1;
 }
 
+static int validate_types_in_attribute(const ebitmap_t *map, const policydb_t *p, const validate_t flavors[SYM_NUM])
+{
+	ebitmap_node_t *node;
+	uint32_t i;
+
+	if (validate_ebitmap(map, &flavors[SYM_TYPES]))
+		goto bad;
+
+	if (p->policy_type != POLICY_KERN && p->policyvers < MOD_POLICYDB_VERSION_TYPE_ATTR_ATTRS) {
+		ebitmap_for_each_positive_bit(map, node, i) {
+			if (validate_simpletype(i+1, p, flavors))
+				goto bad;
+		}
+	}
+
+	return 0;
+
+bad:
+	return -1;
+}
+
 static int validate_type_datum(sepol_handle_t *handle, const type_datum_t *type, const policydb_t *p, validate_t flavors[])
 {
 	if (validate_value(type->s.value, &flavors[SYM_TYPES]))
@@ -582,8 +603,13 @@ static int validate_type_datum(sepol_handle_t *handle, const type_datum_t *type,
 			goto bad;
 		break;
 	case TYPE_ATTRIB:
-		if (validate_ebitmap(&type->types, &flavors[SYM_TYPES]))
-			goto bad;
+		if (p->policy_type == POLICY_KERN) {
+			if (!ebitmap_is_empty(&type->types))
+				goto bad;
+		} else {
+			if (validate_types_in_attribute(&type->types, p, flavors))
+				goto bad;
+		}
 		if (type->bounds)
 			goto bad;
 		break;
Signature · 1 line
2.50.0