Skip to contentSkip to conversation

libsepol: Allow booleanif to have info nodes

PATCH Superseded Inseob Kim 2 participants 4 messages Apr 4, 2025 – Apr 9, 2025 SELinux security module

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Inseob Kim

author
#0 lore raw

PATCH 1/3 libsepol: Allow booleanif to have info nodes

Allowing more info nodes helps debuggability, especially neverallow failure reports.

---
 libsepol/cil/src/cil_binary.c      | 1 +
 libsepol/cil/src/cil_build_ast.c   | 1 +
 libsepol/cil/src/cil_resolve_ast.c | 1 +
 libsepol/cil/src/cil_verify.c      | 3 +++
 4 files changed, 6 insertions(+)
libsepol/cil/src/cil_binary.c +1 -0
index e84188a0..b0befda3 100644
@@ -2153,6 +2153,7 @@ static int __cil_cond_to_policydb_helper(struct cil_tree_node *node, __attribute
 
 	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 +1 -0
index 072d2622..fc11758d 100644
@@ -6164,6 +6164,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 &&
 			((args->db->policy_version < POLICYDB_VERSION_COND_XPERMS) ||
 			  (parse_current->data != CIL_KEY_ALLOWX &&
libsepol/cil/src/cil_resolve_ast.c +1 -0
index a8fa89df..392f03c7 100644
@@ -3849,6 +3849,7 @@ static int __cil_resolve_ast_node_helper(struct cil_tree_node *node, uint32_t *f
 			node->flavor != CIL_AVRULE &&
 			node->flavor != CIL_TYPE_RULE &&
 			node->flavor != CIL_NAMETYPETRANSITION &&
+			node->flavor != CIL_SRC_INFO &&
 			((args->db->policy_version < POLICYDB_VERSION_COND_XPERMS) ||
 			 (node->flavor != CIL_AVRULEX))) {
 			rc = SEPOL_ERR;
libsepol/cil/src/cil_verify.c +3 -0
index 550b4542..cde9dd41 100644
@@ -1176,6 +1176,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) {
Signature · 1 line
2.49.0.504.g3bcea36a83-goog
author
#1 lore raw

PATCH 2/3 libsepol: Make line markers of rules configurable

This commit introduces a new variable line_marker_avrules in the policydb structure. It controls which avrules will have their line markers upon sepol_module_policydb_to_cil.

The default value is AVRULE_NEVERALLOW|AVRULE_XPERMS_NEVERALLOW to keep existing behavior. One expected usage is that checkpolicy prints line markers for allow rules, providing better debuggability.

---
 libsepol/include/sepol/policydb/policydb.h | 4 ++++
 libsepol/src/module_to_cil.c               | 6 ++----
 libsepol/src/policydb.c                    | 2 ++
 3 files changed, 8 insertions(+), 4 deletions(-)
libsepol/include/sepol/policydb/policydb.h +4 -0
index f833354b..ab4b1a12 100644
@@ -615,6 +615,10 @@ typedef struct policydb {
 	sepol_security_class_t dir_class;
 	sepol_access_vector_t process_trans;
 	sepol_access_vector_t process_trans_dyntrans;
+
+	/* avrules whose line markes will be printed. Defaults to neverallow and
+	   neverallowxperm */
+	uint32_t line_marker_avrules;
 } policydb_t;
 
 struct sepol_policydb {
libsepol/src/module_to_cil.c +2 -4
index ae9a2b5d..63c800e9 100644
@@ -1196,8 +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)) &&
-		    avrule->source_filename) {
+		if ((avrule->specified & pdb->line_marker_avrules) && avrule->source_filename) {
 			cil_println(0, ";;* lmx %lu %s\n",avrule->source_line, avrule->source_filename);
 		}
 
@@ -1264,8 +1263,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)) &&
-		    avrule->source_filename) {
+		if ((avrule->specified & pdb->line_marker_avrules) && avrule->source_filename) {
 			cil_println(0, ";;* lme\n");
 		}
 	}
libsepol/src/policydb.c +2 -0
index 8443380b..53d57d81 100644
@@ -945,6 +945,8 @@ int policydb_init(policydb_t * p)
 	ebitmap_init(&p->policycaps);
 	ebitmap_init(&p->permissive_map);
 
+	p->line_marker_avrules = AVRULE_NEVERALLOW|AVRULE_XPERMS_NEVERALLOW;
+
 	return 0;
 err:
 	hashtab_destroy(p->filename_trans);
Signature · 1 line
2.49.0.504.g3bcea36a83-goog
author
#2 lore raw

PATCH 3/3 checkpolicy: Support line markers for allow rules

In reply to Inseob Kim

Using line markers for allow rules helps debugging, especially neverallow failure reports. But unconditionally printing them can bloat output cil files, so this commit adds an option to do that.

---
 checkpolicy/checkpolicy.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
checkpolicy/checkpolicy.c +15 -2
index ede2b6ad..b808e4d0 100644
@@ -107,7 +107,7 @@ static __attribute__((__noreturn__)) void usage(const char *progname)
 	printf
 	    ("usage:  %s [-b[F]] [-C] [-d] [-U handle_unknown (allow,deny,reject)] [-M] "
 	     "[-N] [-c policyvers (%d-%d)] [-o output_file|-] [-S] [-O] "
-	     "[-t target_platform (selinux,xen)] [-E] [-V] [input_file]\n",
+	     "[-t target_platform (selinux,xen)] [-E] [-V] [-L] [input_file]\n",
 	     progname, POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
 	exit(1);
 }
@@ -390,6 +390,7 @@ int main(int argc, char **argv)
 	unsigned int i;
 	unsigned int protocol, port;
 	unsigned int binary = 0, debug = 0, sort = 0, cil = 0, conf = 0, optimize = 0, disable_neverallow = 0;
+	unsigned int line_marker_for_allow = 0;
 	struct val_to_name v;
 	int ret, ch, fd, target = SEPOL_TARGET_SELINUX;
 	unsigned int policyvers = 0;
@@ -418,11 +419,12 @@ int main(int argc, char **argv)
 		{"sort", no_argument, NULL, 'S'},
 		{"optimize", no_argument, NULL, 'O'},
 		{"werror", no_argument, NULL, 'E'},
+		{"line-marker-for-allow", no_argument, NULL, 'L'},
 		{"help", no_argument, NULL, 'h'},
 		{NULL, 0, NULL, 0}
 	};
 
-	while ((ch = getopt_long(argc, argv, "o:t:dbU:MNCFSVc:OEh", long_options, NULL)) != -1) {
+	while ((ch = getopt_long(argc, argv, "o:t:dbU:MNCFSVc:OELh", long_options, NULL)) != -1) {
 		switch (ch) {
 		case 'o':
 			outfile = optarg;
@@ -506,6 +508,9 @@ int main(int argc, char **argv)
 		case 'E':
 			 werror = 1;
 			 break;
+		case 'L':
+			line_marker_for_allow = 1;
+			break;
 		case 'h':
 		default:
 			usage(argv[0]);
@@ -535,6 +540,11 @@ int main(int argc, char **argv)
 		exit(1);
 	}
 
+	if (line_marker_for_allow && !cil) {
+		fprintf(stderr, "Must convert to CIL for line markers to be printed\n");
+		exit(1);
+	}
+
 	if (binary) {
 		fd = open(file, O_RDONLY);
 		if (fd < 0) {
@@ -690,6 +700,9 @@ int main(int argc, char **argv)
 				exit(1);
 			}
 		} else {
+			if (line_marker_for_allow) {
+				policydbp->line_marker_avrules |= AVRULE_ALLOWED | AVRULE_XPERMS_ALLOWED;
+			}
 			if (binary) {
 				ret = sepol_kernel_policydb_to_cil(outfp, policydbp);
 			} else {
Signature · 1 line
2.49.0.504.g3bcea36a83-goog
#3 lore raw

Re: [PATCH 3/3] checkpolicy: Support line markers for allow rules

Quoted 78 lines · Inseob Kim

Using line markers for allow rules helps debugging, especially neverallow failure reports. But unconditionally printing them can bloat output cil files, so this commit adds an option to do that.

---
 checkpolicy/checkpolicy.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
checkpolicy/checkpolicy.c +15 -3
index ede2b6ad..b808e4d0 100644
@@ -107,7 +107,7 @@ static __attribute__((__noreturn__)) void usage(const char *progname)
        printf
            ("usage:  %s [-b[F]] [-C] [-d] [-U handle_unknown (allow,deny,reject)] [-M] "
             "[-N] [-c policyvers (%d-%d)] [-o output_file|-] [-S] [-O] "
-            "[-t target_platform (selinux,xen)] [-E] [-V] [input_file]\n",
+            "[-t target_platform (selinux,xen)] [-E] [-V] [-L] [input_file]\n",
             progname, POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
        exit(1);
 }
@@ -390,6 +390,7 @@ int main(int argc, char **argv)
        unsigned int i;
        unsigned int protocol, port;
        unsigned int binary = 0, debug = 0, sort = 0, cil = 0, conf = 0, optimize = 0, disable_neverallow = 0;
+       unsigned int line_marker_for_allow = 0;
        struct val_to_name v;
        int ret, ch, fd, target = SEPOL_TARGET_SELINUX;
        unsigned int policyvers = 0;
@@ -418,11 +419,12 @@ int main(int argc, char **argv)
                {"sort", no_argument, NULL, 'S'},
                {"optimize", no_argument, NULL, 'O'},
                {"werror", no_argument, NULL, 'E'},
+               {"line-marker-for-allow", no_argument, NULL, 'L'},
                {"help", no_argument, NULL, 'h'},
                {NULL, 0, NULL, 0}
        };
 
-       while ((ch = getopt_long(argc, argv, "o:t:dbU:MNCFSVc:OEh", long_options, NULL)) != -1) {
+       while ((ch = getopt_long(argc, argv, "o:t:dbU:MNCFSVc:OELh", long_options, NULL)) != -1) {
                switch (ch) {
                case 'o':
                        outfile = optarg;
@@ -506,6 +508,9 @@ int main(int argc, char **argv)
                case 'E':
                         werror = 1;
                         break;
+               case 'L':
+                       line_marker_for_allow = 1;
+                       break;
                case 'h':
                default:
                        usage(argv[0]);
@@ -535,6 +540,11 @@ int main(int argc, char **argv)
                exit(1);
        }
 
+       if (line_marker_for_allow && !cil) {
+               fprintf(stderr, "Must convert to CIL for line markers to be printed\n");
+               exit(1);
+       }
+
        if (binary) {
                fd = open(file, O_RDONLY);
                if (fd < 0) {
@@ -690,6 +700,9 @@ int main(int argc, char **argv)
                                exit(1);
                        }
                } else {
+                       if (line_marker_for_allow) {
+                               policydbp->line_marker_avrules |= AVRULE_ALLOWED | AVRULE_XPERMS_ALLOWED;
+                       }
                        if (binary) {
                                ret = sepol_kernel_policydb_to_cil(outfp, policydbp);
                        } else {
--
Quoted 3 lines

2.49.0.504.g3bcea36a83-goog

The first two patches look good to me. The only thing here is I would like to see the same option for checkmodule and please update the man pages.

Thanks,
Jim