Skip to contentSkip to conversation

libsepol: clear attribute member set after building type_attr_map

PATCH In review Stephen Smalley 1 participant 2 messages Oct 13 – Oct 14 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/1 libsepol: clear attribute member set after building type_attr_map

expand_module() populates each attribute's type_datum.type via attr_convert_callback() and then uses that set to build the kernel policydb's attr_type_map/type_attr_map in type_attr_map(). The ebitmap is left in place on the datum afterward.

type_write() does not emit type_datum.types for a kernel policy, so a policydb produced by policydb_read() always has it empty, and policydb_validate() rejects a kernel-policy attribute whose types set is non-empty. This makes a freshly expanded policydb fail validation while the same policy round-tripped through a policy file passes. Both in-tree fuzzers call policydb_validate() on the output of expand_module(): the binpolicy fuzzer bails out before exercising the write and decompile paths on the expanded policy, and the checkpolicy fuzzer would abort() on any source policy that associates a type with an attribute.

Destroy the ebitmap once the maps are built so the in-memory kernel policydb has the same shape as one read back from a binary. This covers both the kept and the expand-away (TYPE_FLAGS_EXPAND_ATTR_TRUE) cases.

Adjust the expander CUnit test helper to read attribute membership from attr_type_map on a kernel policydb, which is where it now lives (and what is actually written out).

---
 libsepol/src/expand.c        |  6 ++++++
 libsepol/tests/test-common.c | 12 +++++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)
libsepol/src/expand.c +6 -0
index f7ff61cd..153b609f 100644
@@ -2669,6 +2669,12 @@ static int type_attr_map(hashtab_key_t key __attribute__((unused)),
 				goto oom;
 			}
 		}
+		/*
+		 * The member set now lives in attr_type_map; drop it from the
+		 * datum so the kernel policydb matches what policydb_read()
+		 * would produce (type_write() does not emit it for KERN).
+		 */
+		ebitmap_destroy(&type->types);
 	} else {
 		if (ebitmap_set_bit(&p->attr_type_map[value - 1], value - 1,
 				    1)) {
libsepol/tests/test-common.c +11 -1
index 20449cf3..61a8fb6b 100644
@@ -246,6 +246,7 @@ void test_attr_types(policydb_t *p, const char *id, avrule_decl_t *decl,
 		     const char **types, int len)
 {
 	ebitmap_node_t *tnode;
+	const ebitmap_t *members;
 	int j, new, found = 0;
 	unsigned int i;
 	type_datum_t *attr;
@@ -265,7 +266,16 @@ void test_attr_types(policydb_t *p, const char *id, avrule_decl_t *decl,
 	CU_ASSERT(attr->flavor == TYPE_ATTRIB);
 	CU_ASSERT(attr->primary == 1);
 
-	ebitmap_for_each_positive_bit(&attr->types, tnode, i) {
+	/*
+	 * On a kernel policydb attribute membership lives in attr_type_map;
+	 * type_datum.types is only populated on BASE/MOD or per-decl.
+	 */
+	if (!decl && p->policy_type == POLICY_KERN)
+		members = &p->attr_type_map[attr->s.value - 1];
+	else
+		members = &attr->types;
+
+	ebitmap_for_each_positive_bit(members, tnode, i) {
 		new = 0;
 		for (j = 0; j < len; j++) {
 			if (strcmp(p->sym_val_to_name[SYM_TYPES][i],
Signature · 1 line
2.55.0
author maintainer
#1 lore raw
Quoted 22 lines · Stephen Smalley

expand_module() populates each attribute's type_datum.type via attr_convert_callback() and then uses that set to build the kernel policydb's attr_type_map/type_attr_map in type_attr_map(). The ebitmap is left in place on the datum afterward.

type_write() does not emit type_datum.types for a kernel policy, so a policydb produced by policydb_read() always has it empty, and policydb_validate() rejects a kernel-policy attribute whose types set is non-empty. This makes a freshly expanded policydb fail validation while the same policy round-tripped through a policy file passes. Both in-tree fuzzers call policydb_validate() on the output of expand_module(): the binpolicy fuzzer bails out before exercising the write and decompile paths on the expanded policy, and the checkpolicy fuzzer would abort() on any source policy that associates a type with an attribute.

Destroy the ebitmap once the maps are built so the in-memory kernel policydb has the same shape as one read back from a binary. This covers both the kept and the expand-away (TYPE_FLAGS_EXPAND_ATTR_TRUE) cases.

Quoted 5 lines

Adjust the expander CUnit test helper to read attribute membership from attr_type_map on a kernel policydb, which is where it now lives (and what is actually written out).

This patch has been merged.