Skip to contentSkip to conversation

libsepol: Print line markers also for allow rules

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

lore
Share & tools
lore

Inseob Kim

author
#0 lore raw

PATCH RESEND 1/1 libsepol: Print line markers also for allow rules

Currently, only line markers for neverallow rules are printed. This makes people difficult to debug a neverallow failure with cil files generated by checkpolicy.

This change additionally prints line markers for allow and allowxperm statements to make debugging easier.

---
 libsepol/src/module_to_cil.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
libsepol/src/module_to_cil.c +2 -2
index ae9a2b5d..76fe4739 100644
@@ -1196,7 +1196,7 @@ static int avrule_list_to_cil(int indent, struct policydb *pdb, struct avrule *a
 	struct type_set *ts;
 
 	for (avrule = avrule_list; avrule != NULL; avrule = avrule->next) {
-		if ((avrule->specified & (AVRULE_NEVERALLOW|AVRULE_XPERMS_NEVERALLOW)) &&
+		if ((avrule->specified & (AVRULE_ALLOWED|AVRULE_XPERMS_ALLOWED|AVRULE_NEVERALLOW|AVRULE_XPERMS_NEVERALLOW)) &&
 		    avrule->source_filename) {
 			cil_println(0, ";;* lmx %lu %s\n",avrule->source_line, avrule->source_filename);
 		}
@@ -1264,7 +1264,7 @@ static int avrule_list_to_cil(int indent, struct policydb *pdb, struct avrule *a
 		names_destroy(&snames, &num_snames);
 		names_destroy(&tnames, &num_tnames);
 
-		if ((avrule->specified & (AVRULE_NEVERALLOW|AVRULE_XPERMS_NEVERALLOW)) &&
+		if ((avrule->specified & (AVRULE_ALLOWED|AVRULE_XPERMS_ALLOWED|AVRULE_NEVERALLOW|AVRULE_XPERMS_NEVERALLOW)) &&
 		    avrule->source_filename) {
 			cil_println(0, ";;* lme\n");
 		}
Signature · 1 line
2.49.0.rc1.451.g8f38331e32-goog
#1 lore raw
Quoted 18 lines · Inseob Kim

Currently, only line markers for neverallow rules are printed. This makes people difficult to debug a neverallow failure with cil files generated by checkpolicy.

This change additionally prints line markers for allow and allowxperm statements to make debugging easier.

---
 libsepol/src/module_to_cil.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
libsepol/src/module_to_cil.c +0 -0
index ae9a2b5d..76fe4739 100644

libsepol/src/module_to_cil.c static int avrule_list_to_cil(int indent, struct policydb *pdb, struct avrule *a · line 1202

Quoted 8 lines
libsepol/src/module_to_cil.c +1 -1
@@ -1196,7 +1196,7 @@ static int avrule_list_to_cil(int indent, struct policydb *pdb, struct avrule *a
        struct type_set *ts;
 
        for (avrule = avrule_list; avrule != NULL; avrule = avrule->next) {
-               if ((avrule->specified & (AVRULE_NEVERALLOW|AVRULE_XPERMS_NEVERALLOW)) &&
+               if ((avrule->specified & (AVRULE_ALLOWED|AVRULE_XPERMS_ALLOWED|AVRULE_NEVERALLOW|AVRULE_XPERMS_NEVERALLOW)) &&
                    avrule->source_filename) {
                        cil_println(0, ";;* lmx %lu %s\n",avrule->source_line, avrule->source_filename);
                }

The problem is that currently line marks (which are converted to <src_info> rules when parsed) cannot be in booleanif statements, but allow rules can be, so this can create cil files that will not compile.
I suspect that that restriction was not intentional and can be relaxed, but I don't remember.

The other issue is that this will produce a lot of line marks for any real policy. A lot of line marks. It would be nice if this behavior could be made optional.

Jim

Quoted 13 lines
libsepol/src/module_to_cil.c +1 -2
@@ -1264,7 +1264,7 @@ static int avrule_list_to_cil(int indent, struct policydb *pdb, struct avrule *a
                names_destroy(&snames, &num_snames);
                names_destroy(&tnames, &num_tnames);
 
-               if ((avrule->specified & (AVRULE_NEVERALLOW|AVRULE_XPERMS_NEVERALLOW)) &&
+               if ((avrule->specified & (AVRULE_ALLOWED|AVRULE_XPERMS_ALLOWED|AVRULE_NEVERALLOW|AVRULE_XPERMS_NEVERALLOW)) &&
                    avrule->source_filename) {
                        cil_println(0, ";;* lme\n");
                }
--

2.49.0.rc1.451.g8f38331e32-goog

author
#2 lore raw
Quoted 22 lines · James Carter
Quoted 20 lines · Inseob Kim

Currently, only line markers for neverallow rules are printed. This makes people difficult to debug a neverallow failure with cil files generated by checkpolicy.

This change additionally prints line markers for allow and allowxperm statements to make debugging easier.

---
 libsepol/src/module_to_cil.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
libsepol/src/module_to_cil.c +0 -0
index ae9a2b5d..76fe4739 100644
@@ -1196,7 +1196,7 @@ static int avrule_list_to_cil(int indent, struct policydb *pdb, struct avrule *a
        struct type_set *ts;
 
Quoted 13 lines

libsepol/src/module_to_cil.c static int avrule_list_to_cil(int indent, struct policydb *pdb, struct avrule *a · line 1202

Quoted 6 lines
libsepol/src/module_to_cil.c +1 -1
        for (avrule = avrule_list; avrule != NULL; avrule = avrule->next) {
-               if ((avrule->specified & (AVRULE_NEVERALLOW|AVRULE_XPERMS_NEVERALLOW)) &&
+               if ((avrule->specified & (AVRULE_ALLOWED|AVRULE_XPERMS_ALLOWED|AVRULE_NEVERALLOW|AVRULE_XPERMS_NEVERALLOW)) &&
                    avrule->source_filename) {
                        cil_println(0, ";;* lmx %lu %s\n",avrule->source_line, avrule->source_filename);
                }

The problem is that currently line marks (which are converted to <src_info> rules when parsed) cannot be in booleanif statements, but allow rules can be, so this can create cil files that will not compile.
I suspect that that restriction was not intentional and can be relaxed, but I don't remember.

I tried supporting markers within booleanif statements and it seems to work with my tiny test.

PoC patch:

libsepol/cil/src/cil_binary.c +0 -0
index 3d920182..90745110 100644
@@ -2121,6 +2121,7 @@ static int __cil_cond_to_policydb_helper(struct

cil_tree_node *node, __attribute

                break;
        case CIL_CALL:
        case CIL_TUNABLEIF:

+ case CIL_SRC_INFO:
break;
default:
cil_tree_log(node, CIL_ERR, "Invalid statement within
booleanif");

libsepol/cil/src/cil_build_ast.c +0 -0
index 19fbb04e..619cd894 100644
@@ -6158,6 +6158,7 @@ static int check_for_illegal_statement(struct

cil_tree_node *parse_current, stru

                        parse_current->data != CIL_KEY_AUDITALLOW &&
                        parse_current->data != CIL_KEY_TYPETRANSITION &&
                        parse_current->data != CIL_KEY_TYPECHANGE &&
+                       parse_current->data != CIL_KEY_SRC_INFO &&
                        parse_current->data != CIL_KEY_TYPEMEMBER) {
                        if (((struct

cil_booleanif*)args->boolif->data)->preserved_tunable) {
cil_tree_log(parse_current, CIL_ERR,
"%s is not allowed in tunableif being treated as a booleanif", (char *)parse_current->data);

(quoted diff) +0 -0

b/libsepol/cil/src/cil_resolve_ast.c
index da8863c4..d0d53b1d 100644

libsepol/cil/src/cil_resolve_ast.c +0 -0
@@ -3848,7 +3848,8 @@ static int __cil_resolve_ast_node_helper(struct

cil_tree_node *node, uint32_t *f

                        node->flavor != CIL_CONDBLOCK &&
                        node->flavor != CIL_AVRULE &&
                        node->flavor != CIL_TYPE_RULE &&
-                       node->flavor != CIL_NAMETYPETRANSITION) {
+                       node->flavor != CIL_NAMETYPETRANSITION &&
+                       node->flavor != CIL_SRC_INFO) {
                        rc = SEPOL_ERR;
                } else if (node->flavor == CIL_AVRULE) {
                        struct cil_avrule *rule = node->data;
libsepol/cil/src/cil_verify.c +0 -0
index 9621a247..0b740c85 100644
@@ -1175,6 +1175,9 @@ static int __cil_verify_booleanif_helper(struct

cil_tree_node *node, __attribute
booleanif statements if they don't have "*" as the file. We can't check that here. Or at least we won't right now. */
break;
+ case CIL_SRC_INFO:
+ //Fall through

+               break;
        default: {
                const char * flavor = cil_node_to_string(node);
                if (bif->preserved_tunable) {

Snippet of test cil:

(boolean foobarbaz true)
(booleanif foobarbaz (true

;;* lmx 999 system/sepolicy/private/booleanif.te
(allow domain system_file (file (open)))
;;* lme

))

secilc neverallow check output:

neverallow check failed at plat_sepolicy.cil:15155 from system/sepolicy/private/domain.te:1237
(neverallow base_typeattr_430 base_typeattr_438 (...))
<root>
booleanif at plat_sepolicy.cil:9105
true at plat_sepolicy.cil:9105
allow at plat_sepolicy.cil:9108 from
system/sepolicy/private/booleanif.te:999
(allow domain system_file (file (open)))

I'll apply this in my next patchset. What do you think?

Quoted 4 lines

The other issue is that this will produce a lot of line marks for any real policy. A lot of line marks. It would be nice if this behavior could be made optional.

Ack, working on it.

Quoted 8 lines

Jim

Quoted 4 lines
libsepol/src/module_to_cil.c +0 -0
@@ -1264,7 +1264,7 @@ static int avrule_list_to_cil(int indent, struct policydb *pdb, struct avrule *a
                names_destroy(&snames, &num_snames);
                names_destroy(&tnames, &num_tnames);
 
Quoted 9 lines
Quoted 9 lines
libsepol/src/module_to_cil.c +1 -2
-               if ((avrule->specified & (AVRULE_NEVERALLOW|AVRULE_XPERMS_NEVERALLOW)) &&
+               if ((avrule->specified & (AVRULE_ALLOWED|AVRULE_XPERMS_ALLOWED|AVRULE_NEVERALLOW|AVRULE_XPERMS_NEVERALLOW)) &&
                    avrule->source_filename) {
                        cil_println(0, ";;* lme\n");
                }
--

2.49.0.rc1.451.g8f38331e32-goog

--
Inseob Kim | Software Engineer | [email protected]

author
#3 lore raw

While working on this change, I also found that
checkpolicy/policy_scan.l is a little broken.

#line[ ]1[ ]\"[^\n]*\" { set_source_file(yytext+9); }
#line[ ]{digit}+ {
errno = 0;
source_lineno = strtoul(yytext+6, NULL, 10) - 1; if (errno) {
yywarn("source line number too big");
}
}

It assumes that the line marker with a filename always starts with "#line 1 ...", but it isn't always the case if the file is preprocessed and the very first line is skipped. We may want to fix the rule like:

#line[ ]{digit}+[ ]\"[^\n]*\" { set_source_file(yytext+6); }
...
void set_source_file(const char *name)
{
// name points to "(number) (filename)".
// assign both source_lineno and source_file appropriately. }

Let me apply that to my next patchset too.

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