Skip to contentSkip to conversation

libsepol/cil: unlink blockinherit->block link when destroying a block

PATCH In review Nicolas Iooss 3 participants 3 messages Feb 1, 2021 – Feb 3, 2021 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsepol/cil: unlink blockinherit->block link when destroying a block

The following CIL policy triggers a heap use-after-free in secilc because when the blockinherit node is destroyed, the block node was already destroyed:

    (block b2a)
    (blockinherit b2a)

Fix this by setting blockinherit->block to NULL when destroying block.

---
 libsepol/cil/src/cil_build_ast.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
libsepol/cil/src/cil_build_ast.c +18 -1
index 02481558ad11..c6edcde6bc5d 100644
@@ -231,13 +231,30 @@ exit:
 
 void cil_destroy_block(struct cil_block *block)
 {
+	struct cil_list_item *item;
+	struct cil_tree_node *bi_node;
+	struct cil_blockinherit *inherit;
+
 	if (block == NULL) {
 		return;
 	}
 
 	cil_symtab_datum_destroy(&block->datum);
 	cil_symtab_array_destroy(block->symtab);
-	cil_list_destroy(&block->bi_nodes, CIL_FALSE);
+	if (block->bi_nodes != NULL) {
+		/* unlink blockinherit->block */
+		cil_list_for_each(item, block->bi_nodes) {
+			bi_node = item->data;
+			/* the conditions should always be true, but better be sure */
+			if (bi_node->flavor == CIL_BLOCKINHERIT) {
+				inherit = bi_node->data;
+				if (inherit->block == block) {
+					inherit->block = NULL;
+				}
+			}
+		}
+		cil_list_destroy(&block->bi_nodes, CIL_FALSE);
+	}
 
 	free(block);
 }
Signature · 1 line
2.30.0
Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Nicolas Iooss

The following CIL policy triggers a heap use-after-free in secilc because when the blockinherit node is destroyed, the block node was already destroyed:

    (block b2a)
    (blockinherit b2a)

Fix this by setting blockinherit->block to NULL when destroying block.

Quoted 43 lines
---
 libsepol/cil/src/cil_build_ast.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
libsepol/cil/src/cil_build_ast.c +18 -2
index 02481558ad11..c6edcde6bc5d 100644
@@ -231,13 +231,30 @@ exit:
 
 void cil_destroy_block(struct cil_block *block)
 {
+       struct cil_list_item *item;
+       struct cil_tree_node *bi_node;
+       struct cil_blockinherit *inherit;
+
        if (block == NULL) {
                return;
        }
 
        cil_symtab_datum_destroy(&block->datum);
        cil_symtab_array_destroy(block->symtab);
-       cil_list_destroy(&block->bi_nodes, CIL_FALSE);
+       if (block->bi_nodes != NULL) {
+               /* unlink blockinherit->block */
+               cil_list_for_each(item, block->bi_nodes) {
+                       bi_node = item->data;
+                       /* the conditions should always be true, but better be sure */
+                       if (bi_node->flavor == CIL_BLOCKINHERIT) {
+                               inherit = bi_node->data;
+                               if (inherit->block == block) {
+                                       inherit->block = NULL;
+                               }
+                       }
+               }
+               cil_list_destroy(&block->bi_nodes, CIL_FALSE);
+       }
 
        free(block);
 }
--

2.30.0

#2 lore raw
Quoted 6 lines · James Carter
Quoted 5 lines · Nicolas Iooss

The following CIL policy triggers a heap use-after-free in secilc because when the blockinherit node is destroyed, the block node was already destroyed:

Quoted 8 lines
Quoted 6 lines
    (block b2a)
    (blockinherit b2a)

Fix this by setting blockinherit->block to NULL when destroying block.

Merged. Thanks!

Quoted 43 lines
Quoted 43 lines
---
 libsepol/cil/src/cil_build_ast.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
libsepol/cil/src/cil_build_ast.c +18 -2
index 02481558ad11..c6edcde6bc5d 100644
@@ -231,13 +231,30 @@ exit:
 
 void cil_destroy_block(struct cil_block *block)
 {
+       struct cil_list_item *item;
+       struct cil_tree_node *bi_node;
+       struct cil_blockinherit *inherit;
+
        if (block == NULL) {
                return;
        }
 
        cil_symtab_datum_destroy(&block->datum);
        cil_symtab_array_destroy(block->symtab);
-       cil_list_destroy(&block->bi_nodes, CIL_FALSE);
+       if (block->bi_nodes != NULL) {
+               /* unlink blockinherit->block */
+               cil_list_for_each(item, block->bi_nodes) {
+                       bi_node = item->data;
+                       /* the conditions should always be true, but better be sure */
+                       if (bi_node->flavor == CIL_BLOCKINHERIT) {
+                               inherit = bi_node->data;
+                               if (inherit->block == block) {
+                                       inherit->block = NULL;
+                               }
+                       }
+               }
+               cil_list_destroy(&block->bi_nodes, CIL_FALSE);
+       }
 
        free(block);
 }
--

2.30.0