Skip to contentSkip to conversation

libsepol: Validate more access vector permissions

PATCH Superseded James Carter 2 participants 3 messages Jul 22 – Jul 23 SELinux security module

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 libsepol: Validate more access vector permissions

The commit 8c64e5bb6fe7 ("libsepol: validate access vector permissions") added a check that at least one permission of an access vector is valid. Add a macro to simplify the check and add a check for av rules in conditional blocks.

---
 libsepol/src/policydb_validate.c | 36 +++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 12 deletions(-)
libsepol/src/policydb_validate.c +24 -12
index cde0b8d6..166e20a5 100644
@@ -11,9 +11,15 @@
 
 #define bool_xor(a, b) (!(a) != !(b))
 #define bool_xnor(a, b) (!bool_xor(a, b))
+
+/*
+ * Check that at least one permission bit is valid.
+ * Older compilers might set invalid bits for the wildcard permission.
+ */
 #define PERMISSION_MASK(nprim)                          \
 	((nprim) == PERM_SYMTAB_SIZE ? (~UINT32_C(0)) : \
 				       ((UINT32_C(1) << (nprim)) - 1))
+#define NO_VALID_PERMS(av, nprim) (!((av) & PERMISSION_MASK(nprim)))
 
 typedef struct validate {
 	uint32_t nprim;
@@ -265,10 +271,8 @@ static int validate_constraint_nodes(sepol_handle_t *handle, uint32_t nperms,
 	for (; cons; cons = cons->next) {
 		if (is_validatetrans && cons->permissions != 0)
 			goto bad;
-		if (!is_validatetrans && cons->permissions == 0)
-			goto bad;
-		if (!is_validatetrans && nperms != PERM_SYMTAB_SIZE &&
-		    cons->permissions >= (UINT32_C(1) << nperms))
+		if (!is_validatetrans &&
+		    NO_VALID_PERMS(cons->permissions, nperms))
 			goto bad;
 
 		if (!cons->expr)
@@ -1091,11 +1095,7 @@ static int validate_access_vector(sepol_handle_t *handle, const policydb_t *p,
 {
 	const class_datum_t *cladatum = p->class_val_to_struct[tclass - 1];
 
-	/*
-	 * Check that at least one permission bit is valid.
-	 * Older compilers might set invalid bits for the wildcard permission.
-	 */
-	if (!(av & PERMISSION_MASK(cladatum->permissions.nprim)))
+	if (NO_VALID_PERMS(av, cladatum->permissions.nprim))
 		goto bad;
 
 	return 0;
@@ -1214,12 +1214,24 @@ static int validate_avrules(sepol_handle_t *handle, const avrule_t *avrule,
 
 		for (classperm = avrule->perms; classperm;
 		     classperm = classperm->next) {
+			class_datum_t *cladatum;
 			if (validate_value(classperm->tclass,
 					   &flavors[SYM_CLASSES]))
 				goto bad;
-			if ((avrule->specified & AVRULE_TYPE) &&
-			    validate_simpletype(classperm->data, p, flavors))
-				goto bad;
+			cladatum =
+				p->class_val_to_struct[classperm->tclass - 1];
+			if (avrule->specified & AVRULE_AV) {
+				if (NO_VALID_PERMS(
+					    classperm->data,
+					    cladatum->permissions.nprim)) {
+					goto bad;
+				}
+			} else if (avrule->specified & AVRULE_TYPE) {
+				if (validate_simpletype(classperm->data, p,
+							flavors)) {
+					goto bad;
+				}
+			}
 		}
 
 		if (avrule->specified & AVRULE_XPERMS) {
Signature · 1 line
2.55.0
#1 lore raw

PATCH 2/2 libsepol: Validate scope index class_perm_map array

In addtion to the array of ebitmaps that shows which declarations are in scope for a given block, the scope index also contains an array of ebitmaps, class_perms_map, that shows which permissions are in scope for each class and it contains the length of that array.

An optional block has a required scope. For a required scope validate that the array is long enough to contain all of the classes that are in scope for the block and that for each class in the array either the permission ebitmap is empty (the class is not required in the block) or contains at least one valid permission. [Older compilers could set all the bits if "*" was used.]

For declared scopes, verify that the length of the class permissions ebitmap array is 0 and the array itself is NULL.

---
 libsepol/src/policydb_validate.c | 43 +++++++++++++++++++++++++-------
 1 file changed, 34 insertions(+), 9 deletions(-)
libsepol/src/policydb_validate.c +34 -9
index 166e20a5..1cf3868c 100644
@@ -1690,10 +1690,9 @@ bad:
 
 static int validate_scope_index(sepol_handle_t *handle,
 				const scope_index_t *scope_index,
-				validate_t flavors[])
+				const policydb_t *p, validate_t flavors[],
+				bool required)
 {
-	uint32_t i;
-
 	if (!ebitmap_is_empty(&scope_index->scope[SYM_COMMONS]))
 		goto bad;
 	if (validate_ebitmap(&scope_index->p_classes_scope,
@@ -1712,9 +1711,35 @@ static int validate_scope_index(sepol_handle_t *handle,
 	if (validate_ebitmap(&scope_index->p_cat_scope, &flavors[SYM_CATS]))
 		goto bad;
 
-	for (i = 0; i < scope_index->class_perms_len; i++)
-		if (validate_value(i + 1, &flavors[SYM_CLASSES]))
+	if (required) {
+		uint32_t i;
+		if (ebitmap_highest_set_bit(&scope_index->p_classes_scope) >=
+		    scope_index->class_perms_len)
+			goto bad;
+		for (i = 0; i < scope_index->class_perms_len; i++) {
+			const ebitmap_t *map = &scope_index->class_perms_map[i];
+			class_datum_t *class = p->class_val_to_struct[i];
+			ebitmap_node_t *node;
+			unsigned int bit = 0;
+			if (validate_value(i + 1, &flavors[SYM_CLASSES]))
+				goto bad;
+			/* Either there are no perms */
+			if (ebitmap_is_empty(map))
+				continue;
+			/* Or at least one valid perm */
+			ebitmap_for_each_positive_bit(map, node, bit) {
+				if (bit < class->permissions.nprim)
+					break;
+			}
+			if (bit >= class->permissions.nprim)
+				goto bad;
+		}
+	} else {
+		if (scope_index->class_perms_len != 0)
+			goto bad;
+		if (scope_index->class_perms_map != NULL)
 			goto bad;
+	}
 
 	return 0;
 
@@ -1786,11 +1811,11 @@ static int validate_avrule_blocks(sepol_handle_t *handle,
 			if (validate_range_trans_rules(
 				    handle, decl->range_tr_rules, flavors))
 				goto bad;
-			if (validate_scope_index(handle, &decl->required,
-						 flavors))
+			if (validate_scope_index(handle, &decl->required, p,
+						 flavors, true))
 				goto bad;
-			if (validate_scope_index(handle, &decl->declared,
-						 flavors))
+			if (validate_scope_index(handle, &decl->declared, p,
+						 flavors, false))
 				goto bad;
 			if (validate_filename_trans_rules(
 				    handle, decl->filename_trans_rules, p,
Signature · 1 line
2.55.0
maintainer Acked-by on v1 1/2
#2 lore raw
In reply to James Carter
Quoted 7 lines · James Carter

The commit 8c64e5bb6fe7 ("libsepol: validate access vector permissions") added a check that at least one permission of an access vector is valid. Add a macro to simplify the check and add a check for av rules in conditional blocks.

Quoted 81 lines
---
 libsepol/src/policydb_validate.c | 36 +++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 12 deletions(-)
libsepol/src/policydb_validate.c +24 -13
index cde0b8d6..166e20a5 100644
@@ -11,9 +11,15 @@
 
 #define bool_xor(a, b) (!(a) != !(b))
 #define bool_xnor(a, b) (!bool_xor(a, b))
+
+/*
+ * Check that at least one permission bit is valid.
+ * Older compilers might set invalid bits for the wildcard permission.
+ */
 #define PERMISSION_MASK(nprim)                          \
        ((nprim) == PERM_SYMTAB_SIZE ? (~UINT32_C(0)) : \
                                       ((UINT32_C(1) << (nprim)) - 1))
+#define NO_VALID_PERMS(av, nprim) (!((av) & PERMISSION_MASK(nprim)))
 
 typedef struct validate {
        uint32_t nprim;
@@ -265,10 +271,8 @@ static int validate_constraint_nodes(sepol_handle_t *handle, uint32_t nperms,
        for (; cons; cons = cons->next) {
                if (is_validatetrans && cons->permissions != 0)
                        goto bad;
-               if (!is_validatetrans && cons->permissions == 0)
-                       goto bad;
-               if (!is_validatetrans && nperms != PERM_SYMTAB_SIZE &&
-                   cons->permissions >= (UINT32_C(1) << nperms))
+               if (!is_validatetrans &&
+                   NO_VALID_PERMS(cons->permissions, nperms))
                        goto bad;
 
                if (!cons->expr)
@@ -1091,11 +1095,7 @@ static int validate_access_vector(sepol_handle_t *handle, const policydb_t *p,
 {
        const class_datum_t *cladatum = p->class_val_to_struct[tclass - 1];
 
-       /*
-        * Check that at least one permission bit is valid.
-        * Older compilers might set invalid bits for the wildcard permission.
-        */
-       if (!(av & PERMISSION_MASK(cladatum->permissions.nprim)))
+       if (NO_VALID_PERMS(av, cladatum->permissions.nprim))
                goto bad;
 
        return 0;
@@ -1214,12 +1214,24 @@ static int validate_avrules(sepol_handle_t *handle, const avrule_t *avrule,
 
                for (classperm = avrule->perms; classperm;
                     classperm = classperm->next) {
+                       class_datum_t *cladatum;
                        if (validate_value(classperm->tclass,
                                           &flavors[SYM_CLASSES]))
                                goto bad;
-                       if ((avrule->specified & AVRULE_TYPE) &&
-                           validate_simpletype(classperm->data, p, flavors))
-                               goto bad;
+                       cladatum =
+                               p->class_val_to_struct[classperm->tclass - 1];
+                       if (avrule->specified & AVRULE_AV) {
+                               if (NO_VALID_PERMS(
+                                           classperm->data,
+                                           cladatum->permissions.nprim)) {
+                                       goto bad;
+                               }
+                       } else if (avrule->specified & AVRULE_TYPE) {
+                               if (validate_simpletype(classperm->data, p,
+                                                       flavors)) {
+                                       goto bad;
+                               }
+                       }
                }
 
                if (avrule->specified & AVRULE_XPERMS) {
--

2.55.0