Skip to contentSkip to conversation

libsepol/cil: Properly check parse tree when printing error messages

PATCH In review James Carter 2 participants 5 messages Aug 27, 2021 – Sep 2, 2021 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 libsepol/cil: Properly check parse tree when printing error messages

The function cil_tree_get_next_path() does not check whether the parse tree node that stores the high-level language file path of a src_info rule actually exists before trying to read the path. This can result in a NULL dereference.

Check that all of the parse tree nodes of a src_info rule exist before reading the data from them.

This bug was found by the secilc-fuzzer.

---
 libsepol/cil/src/cil_tree.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
libsepol/cil/src/cil_tree.c +4 -1
index 75293005..e70722ec 100644
@@ -62,7 +62,10 @@ struct cil_tree_node *cil_tree_get_next_path(struct cil_tree_node *node, char **
 
 	while (node) {
 		if (node->flavor == CIL_NODE && node->data == NULL) {
-			if (node->cl_head->data == CIL_KEY_SRC_INFO && node->cl_head->next != NULL && node->cl_head->next->next != NULL) {
+			if (node->cl_head && node->cl_head->data == CIL_KEY_SRC_INFO) {
+				if (!node->cl_head->next || !node->cl_head->next->next || !node->cl_head->next->next->next) {
+					goto exit;
+				}
 				/* Parse Tree */
 				*info_kind = node->cl_head->next->data;
 				rc = cil_string_to_uint32(node->cl_head->next->next->data, hll_line, 10);
Signature · 1 line
2.31.1
#1 lore raw

PATCH 2/3 libsepol/cil: Reset expandtypeattribute rules when resetting AST

A list is created to store type attribute datums when resolving an expandtypeattribute rule and that list needs to be destroyed if the AST is reset or a memory leak will occur.

Destroy the list storing type attributes datums when resetting expandtypeattribute rules.

This bug was found by the secilc-fuzzer.

---
 libsepol/cil/src/cil_reset_ast.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
libsepol/cil/src/cil_reset_ast.c +8 -1
index 6d1d2da7..0ba075c8 100644
@@ -208,6 +208,11 @@ static void cil_reset_typeattributeset(struct cil_typeattributeset *tas)
 	cil_list_destroy(&tas->datum_expr, CIL_FALSE);
 }
 
+static void cil_reset_expandtypeattribute(struct cil_expandtypeattribute *expandattr)
+{
+	cil_list_destroy(&expandattr->attr_datums, CIL_FALSE);
+}
+
 static void cil_reset_avrule(struct cil_avrule *rule)
 {
 	cil_reset_classperms_list(rule->perms.classperms);
@@ -531,6 +536,9 @@ int __cil_reset_node(struct cil_tree_node *node,  __attribute__((unused)) uint32
 	case CIL_TYPEATTRIBUTESET:
 		cil_reset_typeattributeset(node->data);
 		break;
+	case CIL_EXPANDTYPEATTRIBUTE:
+		cil_reset_expandtypeattribute(node->data);
+		break;
 	case CIL_RANGETRANSITION:
 		cil_reset_rangetransition(node->data);
 		break;
@@ -630,7 +638,6 @@ int __cil_reset_node(struct cil_tree_node *node,  __attribute__((unused)) uint32
 	case CIL_CLASSORDER:
 	case CIL_CATORDER:
 	case CIL_SENSITIVITYORDER:
-	case CIL_EXPANDTYPEATTRIBUTE:
 		break; /* Nothing to reset */
 	default:
 		break;
Signature · 1 line
2.31.1
#2 lore raw

PATCH 3/3 libsepol/cil: Properly check for parameter when inserting name

In reply to James Carter

File names for typetransition rules are stored in their own datums. This allows them to be passed as a parameter, but there needs to be a check in __cil_insert_name() so that parameter names are not mistaken for file name strings. This check did not verify that a matching parameter name had the flavor of CIL_NAME.

Check that the parameter flavor is CIL_NAME and that the paramter name matches the file name to be stored in the datum.

This bug 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 18007324..a4de1c75 100644
@@ -87,7 +87,8 @@ static struct cil_name * __cil_insert_name(struct cil_db *db, hashtab_key_t key,
 	if (macro != NULL && macro->params != NULL) {
 		struct cil_list_item *item;
 		cil_list_for_each(item, macro->params) {
-			if (((struct cil_param*)item->data)->str == key) {
+			struct cil_param *param = item->data;
+			if (param->flavor == CIL_NAME && param->str == key) {
 				return NULL;
 			}
 		}
Signature · 1 line
2.31.1
Acked-by on v1 1/3
#3 lore raw
In reply to James Carter
Quoted 6 lines · James Carter

The function cil_tree_get_next_path() does not check whether the parse tree node that stores the high-level language file path of a src_info rule actually exists before trying to read the path. This can result in a NULL dereference.

Quoted 6 lines

Check that all of the parse tree nodes of a src_info rule exist before reading the data from them.

This bug was found by the secilc-fuzzer.

For the 3 patches:

Thanks,
Nicolas

Quoted 23 lines
---
 libsepol/cil/src/cil_tree.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
libsepol/cil/src/cil_tree.c +4 -2
index 75293005..e70722ec 100644
@@ -62,7 +62,10 @@ struct cil_tree_node *cil_tree_get_next_path(struct cil_tree_node *node, char **
 
        while (node) {
                if (node->flavor == CIL_NODE && node->data == NULL) {
-                       if (node->cl_head->data == CIL_KEY_SRC_INFO && node->cl_head->next != NULL && node->cl_head->next->next != NULL) {
+                       if (node->cl_head && node->cl_head->data == CIL_KEY_SRC_INFO) {
+                               if (!node->cl_head->next || !node->cl_head->next->next || !node->cl_head->next->next->next) {
+                                       goto exit;
+                               }
                                /* Parse Tree */
                                *info_kind = node->cl_head->next->data;
                                rc = cil_string_to_uint32(node->cl_head->next->next->data, hll_line, 10);
--

2.31.1

#4 lore raw
Quoted 11 lines · Nicolas Iooss
Quoted 9 lines · James Carter

The function cil_tree_get_next_path() does not check whether the parse tree node that stores the high-level language file path of a src_info rule actually exists before trying to read the path. This can result in a NULL dereference.

Check that all of the parse tree nodes of a src_info rule exist before reading the data from them.

Quoted 8 lines
Quoted 3 lines

This bug was found by the secilc-fuzzer.

For the 3 patches:

This series has been merged.
Thanks,
Jim

Quoted 27 lines

Thanks,
Nicolas

Quoted 23 lines
---
 libsepol/cil/src/cil_tree.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
libsepol/cil/src/cil_tree.c +4 -2
index 75293005..e70722ec 100644
@@ -62,7 +62,10 @@ struct cil_tree_node *cil_tree_get_next_path(struct cil_tree_node *node, char **
 
        while (node) {
                if (node->flavor == CIL_NODE && node->data == NULL) {
-                       if (node->cl_head->data == CIL_KEY_SRC_INFO && node->cl_head->next != NULL && node->cl_head->next->next != NULL) {
+                       if (node->cl_head && node->cl_head->data == CIL_KEY_SRC_INFO) {
+                               if (!node->cl_head->next || !node->cl_head->next->next || !node->cl_head->next->next->next) {
+                                       goto exit;
+                               }
                                /* Parse Tree */
                                *info_kind = node->cl_head->next->data;
                                rc = cil_string_to_uint32(node->cl_head->next->next->data, hll_line, 10);
--

2.31.1