Skip to contentSkip to conversation

libsepol: Print info nodes at the first child

PATCH RESEND In review Inseob Kim 2 participants 3 messages Apr 2, 2025 – Apr 7, 2025 SELinux security module

lore
Share & tools
lore

Inseob Kim

author
#0 lore raw

PATCH RESEND 1/1 libsepol: Print info nodes at the first child

cil_write_src_info_node has been called with __write_cil_ast_node_helper, but that may break the result CIL file in case there are no children for the info node, because the "lme" marker is printed with __write_cil_ast_last_child_helper.

This change moves the cil_write_src_info_node call to __write_cil_ast_first_child_helper, so opening markers and closing markers always match.

---
 libsepol/cil/src/cil_write_ast.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
libsepol/cil/src/cil_write_ast.c +4 -1
index 15d8bbaf..7f41387f 100644
@@ -1623,7 +1623,6 @@ static int __write_cil_ast_node_helper(struct cil_tree_node *node, uint32_t *fin
 	struct cil_write_ast_args *args = extra_args;
 
 	if (node->flavor == CIL_SRC_INFO) {
-		cil_write_src_info_node(args->out, node);
 		return SEPOL_OK;
 	}
 
@@ -1643,6 +1642,10 @@ static int __write_cil_ast_first_child_helper(struct cil_tree_node *node, void *
 	struct cil_write_ast_args *args = extra_args;
 	struct cil_tree_node *parent = node->parent;
 
+	if (parent->flavor == CIL_SRC_INFO) {
+		cil_write_src_info_node(args->out, parent);
+	}
+
 	if (parent->flavor != CIL_SRC_INFO && parent->flavor != CIL_ROOT) {
 		args->depth++;
 	}
Signature · 1 line
2.49.0.rc1.451.g8f38331e32-goog
#1 lore raw
Quoted 28 lines · Inseob Kim

cil_write_src_info_node has been called with __write_cil_ast_node_helper, but that may break the result CIL file in case there are no children for the info node, because the "lme" marker is printed with __write_cil_ast_last_child_helper.

This change moves the cil_write_src_info_node call to __write_cil_ast_first_child_helper, so opening markers and closing markers always match.

---
 libsepol/cil/src/cil_write_ast.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
libsepol/cil/src/cil_write_ast.c +0 -1
index 15d8bbaf..7f41387f 100644
@@ -1623,7 +1623,6 @@ static int __write_cil_ast_node_helper(struct cil_tree_node *node, uint32_t *fin
        struct cil_write_ast_args *args = extra_args;
 
        if (node->flavor == CIL_SRC_INFO) {
-               cil_write_src_info_node(args->out, node);
                return SEPOL_OK;
        }
 

libsepol/cil/src/cil_write_ast.c static int __write_cil_ast_first_child_helper(struct cil_tree_node *node, void * · line 1648

Quoted 7 lines
libsepol/cil/src/cil_write_ast.c +4 -0
@@ -1643,6 +1642,10 @@ static int __write_cil_ast_first_child_helper(struct cil_tree_node *node, void *
        struct cil_write_ast_args *args = extra_args;
        struct cil_tree_node *parent = node->parent;
 
+       if (parent->flavor == CIL_SRC_INFO) {
+               cil_write_src_info_node(args->out, parent);
+       }
+

This is not going to work quite right.

if, for example, you have two cil policy files. One with a minimum policy and the other with just a comment, then it will currently not write the "lme" marker as you note, but with your patch, it will not write either the "lms" marker or the "lme" marker.

I think that what you need to do is to instead of removing the ci_write_src_info_node() call in __write_cil_ast_node_helper(), you should add a check to see if cl_head is NULL and write out the "lme" marker if it is.

Thanks,
Jim

Quoted 7 lines
libsepol/cil/src/cil_write_ast.c +0 -1
        if (parent->flavor != CIL_SRC_INFO && parent->flavor != CIL_ROOT) {
                args->depth++;
        }
--

2.49.0.rc1.451.g8f38331e32-goog

author
#2 lore raw
Quoted 40 lines · James Carter
Quoted 28 lines · Inseob Kim

cil_write_src_info_node has been called with __write_cil_ast_node_helper, but that may break the result CIL file in case there are no children for the info node, because the "lme" marker is printed with __write_cil_ast_last_child_helper.

This change moves the cil_write_src_info_node call to __write_cil_ast_first_child_helper, so opening markers and closing markers always match.

---
 libsepol/cil/src/cil_write_ast.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
libsepol/cil/src/cil_write_ast.c +0 -1
index 15d8bbaf..7f41387f 100644
@@ -1623,7 +1623,6 @@ static int __write_cil_ast_node_helper(struct cil_tree_node *node, uint32_t *fin
        struct cil_write_ast_args *args = extra_args;
 
        if (node->flavor == CIL_SRC_INFO) {
-               cil_write_src_info_node(args->out, node);
                return SEPOL_OK;
        }
 

libsepol/cil/src/cil_write_ast.c static int __write_cil_ast_first_child_helper(struct cil_tree_node *node, void * · line 1648

Quoted 7 lines
libsepol/cil/src/cil_write_ast.c +4 -0
@@ -1643,6 +1642,10 @@ static int __write_cil_ast_first_child_helper(struct cil_tree_node *node, void *
        struct cil_write_ast_args *args = extra_args;
        struct cil_tree_node *parent = node->parent;
 
+       if (parent->flavor == CIL_SRC_INFO) {
+               cil_write_src_info_node(args->out, parent);
+       }
+

This is not going to work quite right.

Quoted 4 lines

if, for example, you have two cil policy files. One with a minimum policy and the other with just a comment, then it will currently not write the "lme" marker as you note, but with your patch, it will not write either the "lms" marker or the "lme" marker.

Actually this is what I wanted, because I thought such "lms" markers are redundant. But thinking again, it's not necessarily redundant because it anyway has information about included source files, so...

Quoted 5 lines

I think that what you need to do is to instead of removing the ci_write_src_info_node() call in __write_cil_ast_node_helper(), you should add a check to see if cl_head is NULL and write out the "lme" marker if it is.

... this looks fair enough, let me send another patch.

Quoted 12 lines

Thanks,
Jim

Quoted 7 lines
libsepol/cil/src/cil_write_ast.c +0 -1
        if (parent->flavor != CIL_SRC_INFO && parent->flavor != CIL_ROOT) {
                args->depth++;
        }
--

2.49.0.rc1.451.g8f38331e32-goog

Signature · 1 line
Inseob Kim | Software Engineer | [email protected]