Skip to contentSkip to conversation

libsepol: Validate more access vector permissions

PATCH New James Carter 2 participants 3 messages Jul 24 SELinux security module

lore
Share & tools
lore
#0 lore raw

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

---
v3: No changes

 libsepol/src/policydb_validate.c | 36 +++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 12 deletions(-)
libsepol/src/policydb_validate.c +24 -12
index e7d79871..1d8e0870 100644
@@ -12,9 +12,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;
@@ -283,10 +289,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)
@@ -1109,11 +1113,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;
@@ -1232,12 +1232,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.

If the class_perms_map is NULL, then verify that class_perms_len is 0. If the class_perms_map is not NULL, then verify that the class_perms_len is not 0 and 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 or it contains at least one valid permission. [Older compilers could set all the bits if "*" was used.]

---
v3: - Stop differentiating between required and declared blocks and
instead differentiate between NULL and non-NULL class_perms_map
- Remove the added bool argument "required"

 libsepol/src/policydb_validate.c | 45 +++++++++++++++++++++++++++-----
 1 file changed, 38 insertions(+), 7 deletions(-)
libsepol/src/policydb_validate.c +38 -7
index 1d8e0870..fc66a4c7 100644
@@ -1713,10 +1713,8 @@ 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[])
 {
-	uint32_t i;
-
 	if (!ebitmap_is_empty(&scope_index->scope[SYM_COMMONS]))
 		goto bad;
 	if (validate_ebitmap(&scope_index->p_classes_scope,
@@ -1735,9 +1733,42 @@ 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 (scope_index->class_perms_map != NULL) {
+		uint32_t i;
+		if (scope_index->class_perms_len == 0)
+			goto bad;
+		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;
+			class_datum_t *class;
+			ebitmap_node_t *node;
+			unsigned int bit = 0;
+			if (validate_value(i + 1, &flavors[SYM_CLASSES]))
+				goto bad;
+			map = &scope_index->class_perms_map[i];
+			class = p->class_val_to_struct[i];
+			/* 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 {
+		/* This is the normal branch for declared blocks.
+		 * Base module required blocks should never be in this branch.
+		 * Module required blocks can be if neither its nor the module's
+		 * require block has a class in it.
+		 */
+		if (scope_index->class_perms_len != 0)
+			goto bad;
+	}
 
 	return 0;
 
@@ -1811,10 +1842,10 @@ 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,
+			if (validate_scope_index(handle, &decl->required, p,
 						 flavors))
 				goto bad;
-			if (validate_scope_index(handle, &decl->declared,
+			if (validate_scope_index(handle, &decl->declared, p,
 						 flavors))
 				goto bad;
 			if (validate_filename_trans_rules(
Signature · 1 line
2.55.0
maintainer Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] libsepol: Validate scope index class_perm_map array

Quoted 15 lines · James Carter

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.

If the class_perms_map is NULL, then verify that class_perms_len is 0. If the class_perms_map is not NULL, then verify that the class_perms_len is not 0 and 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 or it contains at least one valid permission. [Older compilers could set all the bits if "*" was used.]

Quoted 1 line

Was missing the v3 in the subject line but regardless:

Quoted 85 lines

---
v3: - Stop differentiating between required and declared blocks and
instead differentiate between NULL and non-NULL class_perms_map
- Remove the added bool argument "required"

 libsepol/src/policydb_validate.c | 45 +++++++++++++++++++++++++++-----
 1 file changed, 38 insertions(+), 7 deletions(-)
libsepol/src/policydb_validate.c +38 -8
index 1d8e0870..fc66a4c7 100644
@@ -1713,10 +1713,8 @@ 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[])
 {
-       uint32_t i;
-
        if (!ebitmap_is_empty(&scope_index->scope[SYM_COMMONS]))
                goto bad;
        if (validate_ebitmap(&scope_index->p_classes_scope,
@@ -1735,9 +1733,42 @@ 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 (scope_index->class_perms_map != NULL) {
+               uint32_t i;
+               if (scope_index->class_perms_len == 0)
+                       goto bad;
+               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;
+                       class_datum_t *class;
+                       ebitmap_node_t *node;
+                       unsigned int bit = 0;
+                       if (validate_value(i + 1, &flavors[SYM_CLASSES]))
+                               goto bad;
+                       map = &scope_index->class_perms_map[i];
+                       class = p->class_val_to_struct[i];
+                       /* 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 {
+               /* This is the normal branch for declared blocks.
+                * Base module required blocks should never be in this branch.
+                * Module required blocks can be if neither its nor the module's
+                * require block has a class in it.
+                */
+               if (scope_index->class_perms_len != 0)
+                       goto bad;
+       }
 
        return 0;
 
@@ -1811,10 +1842,10 @@ 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,
+                       if (validate_scope_index(handle, &decl->required, p,
                                                 flavors))
                                goto bad;
-                       if (validate_scope_index(handle, &decl->declared,
+                       if (validate_scope_index(handle, &decl->declared, p,
                                                 flavors))
                                goto bad;
                        if (validate_filename_trans_rules(
--

2.55.0