Skip to contentSkip to conversation

libsepol: Allow booleanif to have info nodes

PATCH In review Inseob Kim 2 participants 6 messages Apr 11, 2025 – Apr 25, 2025 SELinux security module

lore
Share & tools
lore

Inseob Kim

author
#0 lore raw

PATCH v3 1/4 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.604.gff1f9ca942-goog
author
#1 lore raw

PATCH v3 2/4 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.604.gff1f9ca942-goog
author
#2 lore raw

PATCH v3 3/4 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.

Changes since v1:
- Updated the checkpolicy.8 man page for the new flag

Changes sinve v2:
- "writing a CIL" instead of "writing CIL"
- Updated the man page's SYNOPSIS

---
 checkpolicy/checkpolicy.8 |  7 ++++++-
 checkpolicy/checkpolicy.c | 17 +++++++++++++++--
 2 files changed, 21 insertions(+), 3 deletions(-)
checkpolicy/checkpolicy.8 +6 -1
index 81a3647d..caaaf675 100644
@@ -3,7 +3,7 @@
 checkpolicy \- SELinux policy compiler
 .SH SYNOPSIS
 .B checkpolicy
-.I "[\-b[F]] [\-C] [\-d] [\-U handle_unknown (allow,deny,reject)] [\-M] [\-N] [\-c policyvers] [\-o output_file|\-] [\-S] [\-t target_platform (selinux,xen)] [\-O] [\-E] [\-V] [input_file]"
+.I "[\-b[F]] [\-C] [\-d] [\-U handle_unknown (allow,deny,reject)] [\-M] [\-N] [\-L] [\-c policyvers] [\-o output_file|\-] [\-S] [\-t target_platform (selinux,xen)] [\-O] [\-E] [\-V] [input_file]"
 .br
 .SH "DESCRIPTION"
 This manual page describes the
@@ -41,6 +41,11 @@ Enable the MLS policy when checking and compiling the policy.
 .B \-N,\-\-disable-neverallow
 Do not check neverallow rules.
 .TP
+.B \-L,\-\-line-marker-for-allow
+Output line markers for allow rules, in addition to neverallow rules. This option increases the size
+of the output CIL policy file, but the additional line markers helps debugging, especially
+neverallow failure reports. Can only be used when writing a CIL policy file.
+.TP
 .B \-c policyvers
 Specify the policy version, defaults to the latest.
 .TP
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.604.gff1f9ca942-goog
author
#3 lore raw

PATCH v3 4/4 checkmodule: 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.

Changes sinve v2:
- "writing a CIL" instead of "writing CIL"
- Updated the man page's SYNOPSIS

---
 checkpolicy/checkmodule.8 |  7 ++++++-
 checkpolicy/checkmodule.c | 18 ++++++++++++++++--
 2 files changed, 22 insertions(+), 3 deletions(-)
checkpolicy/checkmodule.8 +6 -1
index 93c9b537..519d6c34 100644
@@ -3,7 +3,7 @@
 checkmodule \- SELinux policy module compiler
 .SH SYNOPSIS
 .B checkmodule
-.I "[\-h] [\-b] [\-c policy_version] [\-C] [\-E] [\-m] [\-M] [\-N] [\-U handle_unknown] [\-V] [\-o output_file] [input_file]"
+.I "[\-h] [\-b] [\-c policy_version] [\-C] [\-E] [\-m] [\-M] [\-N] [\-L] [\-U handle_unknown] [\-V] [\-o output_file] [input_file]"
 .SH "DESCRIPTION"
 This manual page describes the
 .BR checkmodule
@@ -46,6 +46,11 @@ Enable the MLS/MCS support when checking and compiling the policy module.
 .B \-N,\-\-disable-neverallow
 Do not check neverallow rules.
 .TP
+.B \-L,\-\-line-marker-for-allow
+Output line markers for allow rules, in addition to neverallow rules. This option increases the size
+of the output CIL policy file, but the additional line markers helps debugging, especially
+neverallow failure reports. Can only be used when writing a CIL policy file.
+.TP
 .B \-V,\-\-version
 Show policy versions created by this program.
 .TP
checkpolicy/checkmodule.c +16 -2
index 2d6f2399..c9ff80cc 100644
@@ -119,7 +119,7 @@ static int write_binary_policy(policydb_t * p, FILE *outfp, unsigned int policy_
 
 static __attribute__((__noreturn__)) void usage(const char *progname)
 {
-	printf("usage:  %s [-h] [-V] [-b] [-C] [-E] [-U handle_unknown] [-m] [-M] [-N] [-o FILE] [-c VERSION] [INPUT]\n", progname);
+	printf("usage:  %s [-h] [-V] [-b] [-C] [-E] [-U handle_unknown] [-m] [-M] [-N] [-L] [-o FILE] [-c VERSION] [INPUT]\n", progname);
 	printf("Build base and policy modules.\n");
 	printf("Options:\n");
 	printf("  INPUT      build module from INPUT (else read from \"%s\")\n",
@@ -136,6 +136,7 @@ static __attribute__((__noreturn__)) void usage(const char *progname)
 	printf("  -m         build a policy module instead of a base module\n");
 	printf("  -M         enable MLS policy\n");
 	printf("  -N         do not check neverallow rules\n");
+	printf("  -L         output line markers for allow rules\n");
 	printf("  -o FILE    write module to FILE (else just check syntax)\n");
 	printf("  -c VERSION build a policy module targeting a modular policy version (%d-%d)\n",
 	       MOD_POLICYDB_VERSION_MIN, MOD_POLICYDB_VERSION_MAX);
@@ -146,6 +147,7 @@ int main(int argc, char **argv)
 {
 	const char *file = txtfile, *outfile = NULL;
 	unsigned int binary = 0, cil = 0, disable_neverallow = 0;
+	unsigned int line_marker_for_allow = 0;
 	unsigned int policy_type = POLICY_BASE;
 	unsigned int policyvers = MOD_POLICYDB_VERSION_MAX;
 	int ch;
@@ -159,12 +161,13 @@ int main(int argc, char **argv)
 		{"handle-unknown", required_argument, NULL, 'U'},
 		{"mls", no_argument, NULL, 'M'},
 		{"disable-neverallow", no_argument, NULL, 'N'},
+		{"line-marker-for-allow", no_argument, NULL, 'L'},
 		{"cil", no_argument, NULL, 'C'},
 		{"werror", no_argument, NULL, 'E'},
 		{NULL, 0, NULL, 0}
 	};
 
-	while ((ch = getopt_long(argc, argv, "ho:bVEU:mMNCc:", long_options, NULL)) != -1) {
+	while ((ch = getopt_long(argc, argv, "ho:bVEU:mMNCc:L", long_options, NULL)) != -1) {
 		switch (ch) {
 		case 'h':
 			usage(argv[0]);
@@ -231,6 +234,9 @@ int main(int argc, char **argv)
 			policyvers = n;
 			break;
 		}
+		case 'L':
+			line_marker_for_allow = 1;
+			break;
 		default:
 			usage(argv[0]);
 		}
@@ -252,6 +258,11 @@ int main(int argc, char **argv)
 		exit(1);
 	}
 
+	if (line_marker_for_allow && !cil) {
+		fprintf(stderr, "%s:  -L must be used along with -C.\n", argv[0]);
+		exit(1);
+	}
+
 	if (optind != argc) {
 		file = argv[optind++];
 		if (optind != argc)
@@ -347,6 +358,9 @@ int main(int argc, char **argv)
 				exit(1);
 			}
 		} else {
+			if (line_marker_for_allow) {
+				modpolicydb.line_marker_avrules |= AVRULE_ALLOWED | AVRULE_XPERMS_ALLOWED;
+			}
 			if (sepol_module_policydb_to_cil(outfp, &modpolicydb, 0) != 0) {
 				fprintf(stderr, "%s:  error writing %s\n", argv[0], outfile);
 				exit(1);
Signature · 1 line
2.49.0.604.gff1f9ca942-goog
Acked-by on v3 1/4
#4 lore raw
In reply to Inseob Kim
Quoted 5 lines · Inseob Kim

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

For these four patches:

Quoted 61 lines
---
 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 -1
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) {
--

2.49.0.604.gff1f9ca942-goog

#5 lore raw
Quoted 11 lines · James Carter
Quoted 5 lines · Inseob Kim

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

For these four patches:

These four patches have been merged.
Thanks,
Jim

Quoted 61 lines
Quoted 61 lines
---
 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 -1
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) {
--

2.49.0.604.gff1f9ca942-goog