Skip to contentSkip to conversation

libsepol: Copy a chain of type alias references

PATCH In review James Carter 2 participants 3 messages Aug 11 – Aug 12 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

---
v2: Move MAX_ALIAS_REPEATS to private.h

 libsepol/src/expand.c  | 45 +++++++++++++++++++++++++++---------------
 libsepol/src/private.h |  2 ++
 2 files changed, 31 insertions(+), 16 deletions(-)
libsepol/src/expand.c +29 -16
index 466d5bc1..3d5bae83 100644
@@ -49,6 +49,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 +772,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 +801,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 +3267,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 +3350,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;
libsepol/src/private.h +2 -0
index 50fe2b2c..6fc5fa2d 100644
@@ -43,6 +43,8 @@
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 
+#define MAX_ALIAS_REPEATS 32
+
 static inline int exceeds_available_bytes(const struct policy_file *fp,
 					  size_t x, size_t req_elem_size)
 {
Signature · 1 line
2.55.0
#1 lore raw

PATCH v2 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.

---
v2: Remove MAX_ALIAS_REPEATS (now in private.h) and add private.h

 libsepol/src/policydb_validate.c | 71 +++++++++++++++++++++-----------
 1 file changed, 46 insertions(+), 25 deletions(-)
libsepol/src/policydb_validate.c +46 -25
index 5687c82b..09d90a41 100644
@@ -7,6 +7,7 @@
 #include <sepol/policydb/services.h>
 
 #include "debug.h"
+#include "private.h"
 #include "kernel_to_common.h"
 #include "policydb_validate.h"
 
@@ -660,19 +661,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 +688,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
#2 lore raw
In reply to James Carter
Quoted 16 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.

Quoted 4 lines

---
v2: Move MAX_ALIAS_REPEATS to private.h

Thanks, both patches merged.