Skip to contentSkip to conversation

libsepol: Copy a chain of type alias references

PATCH Superseded James Carter 2 participants 4 messages Aug 11 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 libsepol: Copy a chain of type alias references

For a policy with multiple modules it is possible for there to be a chain of type alias references where an alias in one module refers to an alias in another module which refers an alias in a third module and so on. When expanding a policy with this sort of chain, a situation can arise where an alias being copied references an alias that has yet to be copied. Since the value of an alias is set to the typemap of its primary and the typemap of its primary has not been set yet, this alias will end up with an invalid value of 0 and return an error.

When copying aliases, skip over aliases that refer to an alias that has yet to be copied and keep repeating the copying of aliases until none have been skipped or the maximum number of repeats has been exceeded.

---
 libsepol/src/expand.c | 47 ++++++++++++++++++++++++++++---------------
 1 file changed, 31 insertions(+), 16 deletions(-)
libsepol/src/expand.c +31 -16
index 466d5bc1..96d7b332 100644
@@ -39,6 +39,8 @@
 #include "debug.h"
 #include "private.h"
 
+#define MAX_ALIAS_REPEATS 32
+
 typedef struct expand_state {
 	int verbose;
 	uint32_t *typemap;
@@ -49,6 +51,7 @@ typedef struct expand_state {
 	policydb_t *out;
 	sepol_handle_t *handle;
 	int expand_neverallow;
+	int aliases_skipped;
 } expand_state_t;
 
 static void expand_state_init(expand_state_t *state)
@@ -771,6 +774,19 @@ static int alias_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
 		return 0;
 	}
 
+	if (state->typemap[prival - 1] == 0) {
+		/* The primary is another alias that has yet to be copied.
+		 * Skip this alias for now and come back to it later. */
+		state->aliases_skipped++;
+		return 0;
+	}
+
+	new_alias = hashtab_search(state->out->p_types.table, key);
+	if (new_alias) {
+		/* This alias has already been copied */
+		return 0;
+	}
+
 	if (state->verbose)
 		INFO(state->handle, "copying alias %s", id);
 
@@ -787,20 +803,7 @@ static int alias_copy_callback(hashtab_key_t key, hashtab_datum_t datum,
 		return SEPOL_ENOMEM;
 	}
 	memset(new_alias, 0, sizeof(type_datum_t));
-	if (alias->flavor == TYPE_TYPE)
-		new_alias->s.value = state->typemap[alias->s.value - 1];
-	else if (alias->flavor == TYPE_ALIAS)
-		new_alias->s.value = state->typemap[alias->primary - 1];
-	else
-		assert(0); /* unreachable */
-
-	if (!new_alias->s.value) {
-		ERR(state->handle, "No type mapping for %s", new_id);
-		free(new_alias);
-		free(new_id);
-		return -1;
-	}
-
+	new_alias->s.value = state->typemap[prival - 1];
 	new_alias->flags = alias->flags;
 
 	ret = hashtab_insert(state->out->p_types.table, (hashtab_key_t)new_id,
@@ -3266,6 +3269,7 @@ int expand_module(sepol_handle_t *handle, policydb_t *base, policydb_t *out,
 	unsigned int i;
 	expand_state_t state;
 	avrule_block_t *curblock;
+	int repeats = 0;
 
 	/* Append tunable's avtrue_list or avfalse_list to the avrules list
 	 * of its home decl depending on its state value, so that the effect
@@ -3348,8 +3352,19 @@ int expand_module(sepol_handle_t *handle, policydb_t *base, policydb_t *out,
 	/* Needed for processing aliases */
 	if (policydb_index_others(handle, out, verbose))
 		goto cleanup;
-	if (hashtab_map(state.base->p_types.table, alias_copy_callback, &state))
-		goto cleanup;
+
+	do {
+		if (repeats >= MAX_ALIAS_REPEATS) {
+			ERR(handle, "Failed to resolve alias references");
+			goto cleanup;
+		}
+		state.aliases_skipped = 0;
+		if (hashtab_map(state.base->p_types.table, alias_copy_callback,
+				&state))
+			goto cleanup;
+		repeats++;
+	} while (state.aliases_skipped > 0);
+
 	if (hashtab_map(state.base->p_types.table, type_bounds_copy_callback,
 			&state))
 		goto cleanup;
Signature · 1 line
2.55.0
#1 lore raw

PATCH 2/2 libsepol: Improve the validation of type aliases

A maliciously crafted binary policy could have a type alias that refers to an attribute or that ultimately refers back to itself in a loop.

Validate that type aliases ultimately refer to a primary type and not to an attribute or themselves.

---
 libsepol/src/policydb_validate.c | 72 +++++++++++++++++++++-----------
 1 file changed, 47 insertions(+), 25 deletions(-)
libsepol/src/policydb_validate.c +47 -25
index 5687c82b..5178a2b4 100644
@@ -13,6 +13,8 @@
 #define bool_xor(a, b) (!(a) != !(b))
 #define bool_xnor(a, b) (!bool_xor(a, b))
 
+#define MAX_ALIAS_REPEATS 32
+
 /*
  * Check that at least one permission bit is valid.
  * Older compilers might set invalid bits for the wildcard permission.
@@ -660,19 +662,23 @@ static int validate_type_datum(sepol_handle_t *handle, const type_datum_t *type,
 {
 	if (validate_value(type->s.value, &flavors[SYM_TYPES]))
 		goto bad;
-	if (type->primary && validate_value(type->primary, &flavors[SYM_TYPES]))
+
+	switch (type->flags) {
+	case 0:
+	case TYPE_FLAGS_NEVERAUDIT:
+	case TYPE_FLAGS_PERMISSIVE:
+	case TYPE_FLAGS_NEVERAUDIT | TYPE_FLAGS_PERMISSIVE:
+	case TYPE_FLAGS_EXPAND_ATTR_TRUE:
+	case TYPE_FLAGS_EXPAND_ATTR_FALSE:
+	case TYPE_FLAGS_EXPAND_ATTR:
+		break;
+	default:
 		goto bad;
+	}
 
-	switch (type->flavor) {
-	case TYPE_TYPE:
-	case TYPE_ALIAS:
-		if (!ebitmap_is_empty(&type->types))
-			goto bad;
-		if (type->bounds &&
-		    validate_simpletype(type->bounds, p, flavors))
+	if (type->flavor == TYPE_ATTRIB) {
+		if (type->primary != 1)
 			goto bad;
-		break;
-	case TYPE_ATTRIB:
 		if (p->policy_type == POLICY_KERN) {
 			if (!ebitmap_is_empty(&type->types))
 				goto bad;
@@ -683,21 +689,37 @@ static int validate_type_datum(sepol_handle_t *handle, const type_datum_t *type,
 		}
 		if (type->bounds)
 			goto bad;
-		break;
-	default:
-		goto bad;
-	}
-
-	switch (type->flags) {
-	case 0:
-	case TYPE_FLAGS_NEVERAUDIT:
-	case TYPE_FLAGS_PERMISSIVE:
-	case TYPE_FLAGS_NEVERAUDIT | TYPE_FLAGS_PERMISSIVE:
-	case TYPE_FLAGS_EXPAND_ATTR_TRUE:
-	case TYPE_FLAGS_EXPAND_ATTR_FALSE:
-	case TYPE_FLAGS_EXPAND_ATTR:
-		break;
-	default:
+	} else if ((type->flavor == TYPE_TYPE) ||
+		   (type->flavor == TYPE_ALIAS)) {
+		if (!ebitmap_is_empty(&type->types))
+			goto bad;
+		if (type->bounds &&
+		    validate_simpletype(type->bounds, p, flavors))
+			goto bad;
+		if ((type->flavor == TYPE_TYPE) && type->primary) {
+			if (type->primary > 1)
+				goto bad;
+		} else {
+			const type_datum_t *t = type;
+			uint32_t v;
+			int repeats = 0;
+			while ((t->flavor == TYPE_ALIAS) || (t->primary == 0)) {
+				if (repeats >= MAX_ALIAS_REPEATS)
+					break;
+				v = (t->primary) ? t->primary : t->s.value;
+				if (validate_value(v, &flavors[SYM_TYPES]))
+					break;
+				t = p->type_val_to_struct[v - 1];
+				if (t == type)
+					break;
+				repeats++;
+			}
+			if ((t->flavor != TYPE_TYPE) || (t->primary != 1)) {
+				ERR(handle, "Alias validation failed");
+				goto bad;
+			}
+		}
+	} else {
 		goto bad;
 	}
 
Signature · 1 line
2.55.0
maintainer Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] libsepol: Improve the validation of type aliases

Quoted 18 lines · James Carter

A maliciously crafted binary policy could have a type alias that refers to an attribute or that ultimately refers back to itself in a loop.

Validate that type aliases ultimately refer to a primary type and not to an attribute or themselves.

---
 libsepol/src/policydb_validate.c | 72 +++++++++++++++++++++-----------
 1 file changed, 47 insertions(+), 25 deletions(-)
libsepol/src/policydb_validate.c +0 -0
index 5687c82b..5178a2b4 100644

libsepol/src/policydb_validate.c line 17

Quoted 5 lines
libsepol/src/policydb_validate.c +2 -0
@@ -13,6 +13,8 @@
 #define bool_xor(a, b) (!(a) != !(b))
 #define bool_xnor(a, b) (!bool_xor(a, b))
 
+#define MAX_ALIAS_REPEATS 32
+

Ideally this would be lifted to a header file like private.h shared by both files.

Otherwise,

maintainer Acked-by on v1 1/2
#3 lore raw
In reply to James Carter
Quoted 26 lines · James Carter

For a policy with multiple modules it is possible for there to be a chain of type alias references where an alias in one module refers to an alias in another module which refers an alias in a third module and so on. When expanding a policy with this sort of chain, a situation can arise where an alias being copied references an alias that has yet to be copied. Since the value of an alias is set to the typemap of its primary and the typemap of its primary has not been set yet, this alias will end up with an invalid value of 0 and return an error.

When copying aliases, skip over aliases that refer to an alias that has yet to be copied and keep repeating the copying of aliases until none have been skipped or the maximum number of repeats has been exceeded.

---
 libsepol/src/expand.c | 47 ++++++++++++++++++++++++++++---------------
 1 file changed, 31 insertions(+), 16 deletions(-)
libsepol/src/expand.c +0 -0
index 466d5bc1..96d7b332 100644

libsepol/src/expand.c line 43

Quoted 5 lines
libsepol/src/expand.c +2 -0
@@ -39,6 +39,8 @@
 #include "debug.h"
 #include "private.h"
 
+#define MAX_ALIAS_REPEATS 32
+

Ideally this would be lifted to a header file like private.h shared by both files.

Otherwise,