Skip to contentSkip to conversation

Fix bugs identified by the secilc-fuzzer

PATCH In review James Carter 2 participants 9 messages Apr 28, 2021 – May 4, 2021 SELinux security module

lore
Share & tools
lore
#0 lore raw

cover Fix bugs identified by the secilc-fuzzer

These patches are all related by the fact that the secilc-fuzzer identified the bugs that led to them. Wherever possible I have tried to fix all of the issues related to the specific one found.

James Carter (5):
libsepol/cil: Fix instances where an error returns SEPOL_OK libsepol/cil: Detect degenerate inheritance and exit with an error libsepol/cil: Check datum in ordered list for expected flavor libsepol/cil: Check for self-referential loops in sets libsepol/cil: Return an error if a call argument fails to resolve

 libsepol/cil/src/cil_build_ast.c   |   3 +
 libsepol/cil/src/cil_internal.h    |   2 +
 libsepol/cil/src/cil_resolve_ast.c | 107 ++++++++++++++++++++---------
 libsepol/cil/src/cil_verify.c      |  97 ++++++++++++++++++--------
 libsepol/cil/src/cil_verify.h      |   1 -
 5 files changed, 151 insertions(+), 59 deletions(-)
Signature · 1 line
2.26.3
#1 lore raw

PATCH 1/5 libsepol/cil: Fix instances where an error returns SEPOL_OK

There are six instances when the CIL policy is being built or resolved where an error can be detected, but SEPOL_OK is returned instead of SEPOL_ERR. This causes the policy compiler to continue when it should exit with an error.

Return SEPOL_ERR in these cases, so the compiler exits with an error.

Two of the instances were found by the secilc-fuzzer.

---
 libsepol/cil/src/cil_build_ast.c   | 3 +++
 libsepol/cil/src/cil_resolve_ast.c | 3 +++
 2 files changed, 6 insertions(+)
libsepol/cil/src/cil_build_ast.c +3 -0
index 5b1e2824..87043a8f 100644
@@ -444,6 +444,7 @@ int cil_gen_class(struct cil_db *db, struct cil_tree_node *parse_current, struct
 		}
 		if (class->num_perms > CIL_PERMS_PER_CLASS) {
 			cil_tree_log(parse_current, CIL_ERR, "Too many permissions in class '%s'", class->datum.name);
+			rc = SEPOL_ERR;
 			goto exit;
 		}
 
@@ -1018,6 +1019,7 @@ int cil_gen_common(struct cil_db *db, struct cil_tree_node *parse_current, struc
 	}
 	if (common->num_perms > CIL_PERMS_PER_CLASS) {
 		cil_tree_log(parse_current, CIL_ERR, "Too many permissions in common '%s'", common->datum.name);
+		rc = SEPOL_ERR;
 		goto exit;
 	}
 
@@ -3209,6 +3211,7 @@ int cil_gen_expandtypeattribute(struct cil_db *db, struct cil_tree_node *parse_c
 		expandattr->expand = CIL_FALSE;
 	} else {
 		cil_log(CIL_ERR, "Value must be either \'true\' or \'false\'");
+		rc = SEPOL_ERR;
 		goto exit;
 	}
 
libsepol/cil/src/cil_resolve_ast.c +3 -0
index 872b6799..5389df43 100644
@@ -772,6 +772,7 @@ int cil_resolve_classcommon(struct cil_tree_node *current, void *extra_args)
 	class->num_perms += common->num_perms;
 	if (class->num_perms > CIL_PERMS_PER_CLASS) {
 		cil_tree_log(current, CIL_ERR, "Too many permissions in class '%s' when including common permissions", class->datum.name);
+		rc = SEPOL_ERR;
 		goto exit;
 	}
 
@@ -1484,6 +1485,7 @@ int cil_resolve_classorder(struct cil_tree_node *current, void *extra_args)
 		rc = cil_resolve_name(current, (char *)curr->data, CIL_SYM_CLASSES, extra_args, &datum);
 		if (rc != SEPOL_OK) {
 			cil_log(CIL_ERR, "Failed to resolve class %s in classorder\n", (char *)curr->data);
+			rc = SEPOL_ERR;
 			goto exit;
 		}
 		cil_list_append(new, CIL_CLASS, datum);
@@ -2464,6 +2466,7 @@ int cil_resolve_blockabstract(struct cil_tree_node *current, void *extra_args)
 	block_node = NODE(block_datum);
 	if (block_node->flavor != CIL_BLOCK) {
 		cil_log(CIL_ERR, "Failed to resolve blockabstract to a block, rc: %d\n", rc);
+		rc = SEPOL_ERR;
 		goto exit;
 	}
 
Signature · 1 line
2.26.3
#2 lore raw

PATCH 2/5 libsepol/cil: Detect degenerate inheritance and exit with an error

In reply to James Carter

A CIL policy with inheritance of the form
...
(blockinherit ba)
(block ba
(block b1
(blockinherit bb)
)
(block bb

    (block b2
      (blockinherit bc)
    )
    (block bc
      (block b3
        (blockinherit bd)
      )
      (block bd
        (block b4
          (blockinherit be)
        )
        (block be
        ...

will require creating 2^depth copies of the block at the bottom of the inheritance chain. This pattern can quickly consume all the memory of the system compiling this policy.

The depth of the inheritance chain can be found be walking the tree up through the parents and noting how many of the parent blocks have been inherited. The number of times a block will be copied is found by counting the list of nodes in the "bi_nodes" list of the block. To minimize legitimate policies from being falsely detected as being degenerate, both the depth and breadth (number of copies) are checked and an error is given only if both exceed the limits (depth >= 12 and breadth >= 4096).

This problem was found by the secilc-fuzzer.

---
 libsepol/cil/src/cil_internal.h    |  2 ++
 libsepol/cil/src/cil_resolve_ast.c | 54 ++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+)
libsepol/cil/src/cil_internal.h +2 -0
index 9bdcbdd0..74e0b34d 100644
@@ -48,6 +48,8 @@
 
 #define CIL_MAX_NAME_LENGTH 2048
 
+#define CIL_DEGENERATE_INHERITANCE_DEPTH 12
+#define CIL_DEGENERATE_INHERITANCE_BREADTH (0x1 << CIL_DEGENERATE_INHERITANCE_DEPTH)
 
 enum cil_pass {
 	CIL_PASS_INIT = 0,
libsepol/cil/src/cil_resolve_ast.c +54 -0

66 lines not on this page · raw message

Signature · 1 line
2.26.3
#3 lore raw

PATCH 3/5 libsepol/cil: Check datum in ordered list for expected flavor

In reply to James Carter

The secilc-fuzzer found an out of bounds memory access occurs when building the binary policy if a map class is included in a classorder statement.

The order statements in CIL (sidorder, classorder, categoryorder, and sensitivityorder) are used to specify an ordering for sids, classes, categories, and sensitivities. When the order statments are resolved and merged, only in the case of the category order list is the datum resolved checked to see if it is the expected flavor.

When resolving the sid, class, and sensitivity order statements, check that each name resolved to a datum of the expected flavor and return an error if it does not.

---
 libsepol/cil/src/cil_resolve_ast.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
libsepol/cil/src/cil_resolve_ast.c +16 -0
index 68909647..b081d45d 100644
@@ -1488,6 +1488,11 @@ int cil_resolve_classorder(struct cil_tree_node *current, void *extra_args)
 			rc = SEPOL_ERR;
 			goto exit;
 		}
+		if (FLAVOR(datum) != CIL_CLASS) {
+			cil_log(CIL_ERR, "%s is not a class. Only classes are allowed in classorder statements\n", datum->name);
+			rc = SEPOL_ERR;
+			goto exit;
+		}
 		cil_list_append(new, CIL_CLASS, datum);
 	}
 
@@ -1526,6 +1531,12 @@ int cil_resolve_sidorder(struct cil_tree_node *current, void *extra_args)
 			cil_log(CIL_ERR, "Failed to resolve sid %s in sidorder\n", (char *)curr->data);
 			goto exit;
 		}
+		if (FLAVOR(datum) != CIL_SID) {
+			cil_log(CIL_ERR, "%s is not a sid. Only sids are allowed in sidorder statements\n", datum->name);
+			rc = SEPOL_ERR;
+			goto exit;
+		}
+
 		cil_list_append(new, CIL_SID, datum);
 	}
 
@@ -1617,6 +1628,11 @@ int cil_resolve_sensitivityorder(struct cil_tree_node *current, void *extra_args
 			cil_log(CIL_ERR, "Failed to resolve sensitivty %s in sensitivityorder\n", (char *)curr->data);
 			goto exit;
 		}
+		if (FLAVOR(datum) != CIL_SENS) {
+			cil_log(CIL_ERR, "%s is not a sensitivity. Only sensitivities are allowed in sensitivityorder statements\n", datum->name);
+			rc = SEPOL_ERR;
+			goto exit;
+		}
 		cil_list_append(new, CIL_SENS, datum);
 	}
 
Signature · 1 line
2.26.3
#4 lore raw

PATCH 4/5 libsepol/cil: Check for self-referential loops in sets

In reply to James Carter

The secilc-fuzzer found a self-referential loop using category sets. Any set declaration in CIL that allows sets in it is susceptible to the creation of a self-referential loop. There is a check, but only for the name of the set being declared being used in the set declaration.

Check for self-refential loops in user, role, and type attributes and in category sets. Since all of the sets need to be declared, this check has to be done when verifying the CIL db before doing the post phase.

---
 libsepol/cil/src/cil_resolve_ast.c | 31 +---------
 libsepol/cil/src/cil_verify.c      | 97 +++++++++++++++++++++---------
 libsepol/cil/src/cil_verify.h      |  1 -
 3 files changed, 71 insertions(+), 58 deletions(-)
libsepol/cil/src/cil_resolve_ast.c +1 -30
index b081d45d..34c28fc6 100644
@@ -438,11 +438,6 @@ int cil_resolve_typeattributeset(struct cil_tree_node *current, void *extra_args
 		goto exit;
 	}
 
-	rc = cil_verify_no_self_reference(attr_datum, attrtypes->datum_expr);
-	if (rc != SEPOL_OK) {
-		goto exit;
-	}
-
 	if (attr->expr_list == NULL) {
 		cil_list_init(&attr->expr_list, CIL_TYPEATTRIBUTE);
 	}
@@ -1151,11 +1146,6 @@ int cil_resolve_roleattributeset(struct cil_tree_node *current, void *extra_args
 		goto exit;
 	}
 
-	rc = cil_verify_no_self_reference(attr_datum, attrroles->datum_expr);
-	if (rc != SEPOL_OK) {
-		goto exit;
-	}
-
 	if (attr->expr_list == NULL) {
 		cil_list_init(&attr->expr_list, CIL_ROLEATTRIBUTE);
 	}
@@ -1666,21 +1656,7 @@ exit:
 
 int cil_resolve_catset(struct cil_tree_node *current, struct cil_catset *catset, void *extra_args)
 {
-	int rc = SEPOL_ERR;
-
-	rc = cil_resolve_cats(current, catset->cats, extra_args);
-	if (rc != SEPOL_OK) {
-		goto exit;
-	}
-
-	rc = cil_verify_no_self_reference((struct cil_symtab_datum *)catset, catset->cats->datum_expr);
-	if (rc != SEPOL_OK) {
-		cil_list_destroy(&catset->cats->datum_expr, CIL_FALSE);
-		goto exit;
-	}
-
-exit:
-	return rc;
+	return cil_resolve_cats(current, catset->cats, extra_args);
 }
 
 int cil_resolve_senscat(struct cil_tree_node *current, void *extra_args)
@@ -3544,11 +3520,6 @@ int cil_resolve_userattributeset(struct cil_tree_node *current, void *extra_args
 		goto exit;
 	}
 
-	rc = cil_verify_no_self_reference(attr_datum, attrusers->datum_expr);
-	if (rc != SEPOL_OK) {
-		goto exit;
-	}
-
 	if (attr->expr_list == NULL) {
 		cil_list_init(&attr->expr_list, CIL_USERATTRIBUTE);
 	}
libsepol/cil/src/cil_verify.c +70 -27

136 lines not on this page · raw message

libsepol/cil/src/cil_verify.h +0 -1
index c497018f..4ea14f5b 100644
@@ -63,7 +63,6 @@ int cil_verify_constraint_leaf_expr_syntax(enum cil_flavor l_flavor, enum cil_fl
 int cil_verify_constraint_expr_syntax(struct cil_tree_node *current, enum cil_flavor op);
 int cil_verify_conditional_blocks(struct cil_tree_node *current);
 int cil_verify_decl_does_not_shadow_macro_parameter(struct cil_macro *macro, struct cil_tree_node *node, const char *name);
-int cil_verify_no_self_reference(struct cil_symtab_datum *datum, struct cil_list *datum_list);
 int __cil_verify_ranges(struct cil_list *list);
 int __cil_verify_ordered_node_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args);
 int __cil_verify_ordered(struct cil_tree_node *current, enum cil_flavor flavor);
Signature · 1 line
2.26.3
Acked-by on v1 4/5
#5 lore raw

Re: [PATCH 4/5] libsepol/cil: Check for self-referential loops in sets

Quoted 19 lines · James Carter

The secilc-fuzzer found a self-referential loop using category sets. Any set declaration in CIL that allows sets in it is susceptible to the creation of a self-referential loop. There is a check, but only for the name of the set being declared being used in the set declaration.

Check for self-refential loops in user, role, and type attributes and in category sets. Since all of the sets need to be declared, this check has to be done when verifying the CIL db before doing the post phase.

---
 libsepol/cil/src/cil_resolve_ast.c | 31 +---------
 libsepol/cil/src/cil_verify.c      | 97 +++++++++++++++++++++---------
 libsepol/cil/src/cil_verify.h      |  1 -
 3 files changed, 71 insertions(+), 58 deletions(-)

[...]

libsepol/cil/src/cil_verify.c int cil_verify_decl_does_not_shadow_macro_parameter(struct cil_macro *macro, str · line 433

Quoted 10 lines
libsepol/cil/src/cil_verify.c +1 -1
index 5a37dd2f..4c860a21 100644
@@ -430,28 +430,71 @@ int cil_verify_decl_does_not_shadow_macro_parameter(struct cil_macro *macro, str
        return SEPOL_OK;
 }
 
-int cil_verify_no_self_reference(struct cil_symtab_datum *datum, struct cil_list *datum_list)
+int cil_verify_no_self_reference(enum cil_flavor flavor, struct cil_symtab_datum *datum, struct cil_symtab_datum *orig);

Hello,
Your patches look fine. Nevertheless it would be cleaner if this function was declared "static", as it is not used outside of cil_verify.c. This is a suggestion which is not blocking any merge, so if you prefer to merge the patches directly, feel free to do so.

And thanks for working on fixing issues identified by OSS-Fuzz! Nicolas

#6 lore raw

Re: [PATCH 4/5] libsepol/cil: Check for self-referential loops in sets

Quoted 39 lines · Nicolas Iooss
Quoted 19 lines · James Carter

The secilc-fuzzer found a self-referential loop using category sets. Any set declaration in CIL that allows sets in it is susceptible to the creation of a self-referential loop. There is a check, but only for the name of the set being declared being used in the set declaration.

Check for self-refential loops in user, role, and type attributes and in category sets. Since all of the sets need to be declared, this check has to be done when verifying the CIL db before doing the post phase.

---
 libsepol/cil/src/cil_resolve_ast.c | 31 +---------
 libsepol/cil/src/cil_verify.c      | 97 +++++++++++++++++++++---------
 libsepol/cil/src/cil_verify.h      |  1 -
 3 files changed, 71 insertions(+), 58 deletions(-)

[...]

libsepol/cil/src/cil_verify.c int cil_verify_decl_does_not_shadow_macro_parameter(struct cil_macro *macro, str · line 433

Quoted 10 lines
libsepol/cil/src/cil_verify.c +1 -1
index 5a37dd2f..4c860a21 100644
@@ -430,28 +430,71 @@ int cil_verify_decl_does_not_shadow_macro_parameter(struct cil_macro *macro, str
        return SEPOL_OK;
 }
 
-int cil_verify_no_self_reference(struct cil_symtab_datum *datum, struct cil_list *datum_list)
+int cil_verify_no_self_reference(enum cil_flavor flavor, struct cil_symtab_datum *datum, struct cil_symtab_datum *orig);

Hello,
Your patches look fine. Nevertheless it would be cleaner if this function was declared "static", as it is not used outside of cil_verify.c. This is a suggestion which is not blocking any merge, so if you prefer to merge the patches directly, feel free to do so.

Quoted 2 lines

I like your suggestion better.
Thanks,
Jim

Quoted 3 lines

And thanks for working on fixing issues identified by OSS-Fuzz! Nicolas

#7 lore raw

PATCH 5/5 libsepol/cil: Return an error if a call argument fails to resolve

In reply to James Carter

Return an error if a call argument fails to resolve so that the resolution phase stops and returns an error.

This problem was found by the secilc-fuzzer.

---
 libsepol/cil/src/cil_resolve_ast.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
libsepol/cil/src/cil_resolve_ast.c +2 -1
index 34c28fc6..5368ae80 100644
@@ -3253,6 +3253,7 @@ int cil_resolve_call2(struct cil_tree_node *current, void *extra_args)
 		if (sym_index != CIL_SYM_UNKNOWN) {
 			rc = cil_resolve_name(current, arg->arg_str, sym_index, extra_args, &(arg->arg));
 			if (rc != SEPOL_OK) {
+				cil_tree_log(current, CIL_ERR, "Failed to resolve %s in call argument list", arg->arg_str);
 				goto exit;
 			}
 		}
@@ -3284,7 +3285,7 @@ int cil_resolve_name_call_args(struct cil_call *call, char *name, enum cil_sym_i
 		if (param_index == sym_index) {
 			if (name == arg->param_str) {
 				*datum = arg->arg;
-				rc = SEPOL_OK;
+				rc = *datum ? SEPOL_OK : SEPOL_ERR;
 				goto exit;
 			}
 		}
Signature · 1 line
2.26.3
#8 lore raw
In reply to James Carter
Quoted 19 lines · James Carter

These patches are all related by the fact that the secilc-fuzzer identified the bugs that led to them. Wherever possible I have tried to fix all of the issues related to the specific one found.

James Carter (5):
libsepol/cil: Fix instances where an error returns SEPOL_OK libsepol/cil: Detect degenerate inheritance and exit with an error libsepol/cil: Check datum in ordered list for expected flavor libsepol/cil: Check for self-referential loops in sets libsepol/cil: Return an error if a call argument fails to resolve

 libsepol/cil/src/cil_build_ast.c   |   3 +
 libsepol/cil/src/cil_internal.h    |   2 +
 libsepol/cil/src/cil_resolve_ast.c | 107 ++++++++++++++++++++---------
 libsepol/cil/src/cil_verify.c      |  97 ++++++++++++++++++--------
 libsepol/cil/src/cil_verify.h      |   1 -
 5 files changed, 151 insertions(+), 59 deletions(-)
Quoted 3 lines

--
2.26.3

All but patch 4 has been applied. I will send a v2 for patch 4 based on the suggested by Nicolas.
Jim