All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH userspace 0/2] Fix and warn on negative-only type sets
@ 2026-11-26  3:29 Thiébaud Weksteen
  2026-11-26  3:29 ` [PATCH userspace 1/2] libsepol: align module_to_cil type/role set handling with expand.c Thiébaud Weksteen
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Thiébaud Weksteen @ 2026-11-26  3:29 UTC (permalink / raw)
  To: selinux
  Cc: Thiébaud Weksteen, Stephen Smalley, James Carter,
	Petr Lautrbach, Christian Göttsche, Inseob Kim,
	Jeff Vander Stoep

This series addresses two related issues with negative-only type sets
such as "{ -TYPE1 }" (and "~{ -TYPE1 }"): a semantic divergence between
libsepol's expansion and CIL conversion paths, and a silent pitfall for
policy authors.

1. Divergence between expand.c and module_to_cil.c
--------------------------------------------------

In SELinux policy language, "-" performs set subtraction from the
positive types listed in the same set, rather than set complement ("~").
Currently, libsepol's monolithic expansion path (expand.c) and CIL
conversion path (module_to_cil.c) disagree on how negative-only type
sets are evaluated:

- libsepol/src/expand.c:type_set_expand() computes (empty \ {TYPE1}) =
  empty, so the rule is a no-op.
- libsepol/src/module_to_cil.c:cil_print_attr_strs() omits the
  "(and ...)" wrapper when the positive set is empty and emits
  "(typeattributeset base_typeattr_1 (not (TYPE1 )))", treating
  "{ -TYPE1 }" as "~TYPE1" (all types except TYPE1).

Beyond causing monolithic checkpolicy and checkpolicy -C / semodule to
disagree on the same policy, this also undermines checkpolicy's ban on
complemented allow rules: checkpolicy rejects "allow ~TYPE1 ...", yet
accepts "allow { -TYPE1 } ...", which grants nothing under expand.c but
grants access to every type except TYPE1 when converted to CIL.

Two related edge cases in cil_print_attr_strs() also diverge from
expand.c: "~{ -TYPE1 }" emits "(not (not (TYPE1 )))" instead of "(all)",
and TYPE_STAR with a non-empty negset emits invalid "(all)(not (...))"
instead of "(not (...))".

2. Silent pitfall for policy authors
------------------------------------

Because "{ attr -foo }" is a common idiom in SELinux policy, authors
occasionally omit the positive set and write "{ -foo }" when they intend
the set complement "~foo" (all types other than foo).  This mistake is
easy to miss in code review and has surprising, silent consequences:

- Checkpolicy accepts "{ -foo }" without any warning or error.
- Under expand.c (and under CIL once patch 1 aligns the two paths),
  "{ -foo }" evaluates to the empty set.  In a neverallow rule (e.g.
  "neverallow { -domain } ..."), the assertion silently becomes a no-op
  and provides no protection, while still looking like a valid invariant
  to readers.
- Previously, the bug in module_to_cil.c masked the mistake whenever
  policies were checked through CIL, while monolithic checkpolicy (and
  tools built on expand.c) silently ignored the rule.
- Similarly, "~{ -foo }" silently expands to all types rather than foo.

Warning when a parsed type set has a non-empty negative set without any
positive types (and without "*") catches this mistake at compile time,
while leaving standard cases that legitimately expand to an empty set
(such as "{ empty_attr -foo }", "{ foo -foo }", or "{ -self }")
unaffected.

Series overview
---------------

1. libsepol: align module_to_cil type/role set handling with expand.c
   Updates cil_print_attr_strs() to match type_set_expand() and
   role_set_expand() for empty/negative-only, complemented empty, and
   star-with-negset sets, and adds CUnit regression tests.

2. checkpolicy: warn on negative-only type sets
   Emits a warning via yywarn() when a type set has no positive types
   (and no "*") alongside a non-empty negative set, with regression
   tests.  yywarn() is used rather than yyerror() to avoid breaking
   existing/frozen policies on upgrade while still failing builds that
   pass -E / --werror (happy to switch to yyerror() if preferred).

Tested with `make test` across libsepol, checkpolicy, and secilc under
gcc, clang, and gcc with ASan/UBSan, as well as `make check-format`.

Minimal reproducer (expand.c vs module_to_cil.c divergence)
-----------------------------------------------------------

Given the following 11-line policy (poc.conf):

    class CLASS1
    sid kernel
    class CLASS1 { PERM1 }
    type TYPE1;
    type TYPE2;
    allow TYPE2 self:CLASS1 PERM1;
    neverallow { -TYPE1 } self:CLASS1 PERM1;
    role ROLE1;
    role ROLE1 types { TYPE1 TYPE2 };
    user USER1 roles ROLE1;
    sid kernel USER1:ROLE1:TYPE1

Before this series, compiling directly with checkpolicy (expand.c)
succeeds silently because the neverallow expands to the empty set:

    $ checkpolicy -o policy_mono.bin poc.conf
    $ echo $?
    0

Converting to CIL and compiling with secilc (module_to_cil.c) fails the
neverallow assertion because module_to_cil emitted "(not (TYPE1 ))":

    $ checkpolicy -C -o poc.cil poc.conf && \
      secilc -M false -f /dev/null -o policy_cil.bin poc.cil
    neverallow check failed at poc.cil:30 from poc.conf:7
      (neverallow base_typeattr_1 self (CLASS1 (PERM1)))
        <root>
        allow at poc.cil:27
          (allow TYPE2 self (CLASS1 (PERM1)))

    Failed to generate binary
    Failed to build policydb
    $ echo $?
    255

Thiébaud Weksteen (2):
  libsepol: align module_to_cil type/role set handling with expand.c
  checkpolicy: warn on negative-only type sets

 checkpolicy/Makefile                          |   1 +
 checkpolicy/policy_define.c                   |  24 ++
 checkpolicy/tests/test_negative_typeset.sh    | 245 ++++++++++++++++
 libsepol/src/module_to_cil.c                  |  69 +++--
 libsepol/tests/libsepol-tests.c               |   2 +
 .../test-module-to-cil/typeset-comp.conf      |  24 ++
 .../policies/test-module-to-cil/typeset.conf  |  34 +++
 libsepol/tests/test-module-to-cil.c           | 268 ++++++++++++++++++
 libsepol/tests/test-module-to-cil.h           |  10 +
 9 files changed, 653 insertions(+), 24 deletions(-)
 create mode 100644 checkpolicy/tests/test_negative_typeset.sh
 create mode 100644 libsepol/tests/policies/test-module-to-cil/typeset-comp.conf
 create mode 100644 libsepol/tests/policies/test-module-to-cil/typeset.conf
 create mode 100644 libsepol/tests/test-module-to-cil.c
 create mode 100644 libsepol/tests/test-module-to-cil.h

-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH userspace 1/2] libsepol: align module_to_cil type/role set handling with expand.c
  2026-11-26  3:29 [PATCH userspace 0/2] Fix and warn on negative-only type sets Thiébaud Weksteen
@ 2026-11-26  3:29 ` Thiébaud Weksteen
  2026-11-26  3:29 ` [PATCH userspace 2/2] checkpolicy: warn on negative-only type sets Thiébaud Weksteen
  2026-11-30 15:46 ` [PATCH userspace 0/2] Fix and " Stephen Smalley
  2 siblings, 0 replies; 5+ messages in thread
From: Thiébaud Weksteen @ 2026-11-26  3:29 UTC (permalink / raw)
  To: selinux
  Cc: Thiébaud Weksteen, Stephen Smalley, James Carter,
	Petr Lautrbach, Christian Göttsche, Inseob Kim,
	Jeff Vander Stoep

cil_print_attr_strs() converts module type_set and role_set structures
into CIL attributesets, but diverges from type_set_expand() and
role_set_expand() in libsepol/src/expand.c in several edge cases:

1. Negative-only type set ({ -t1 }, flags == 0, empty positive set,
   non-empty negset): type_set_expand() evaluates (empty \ negset) as
   the empty set, whereas cil_print_attr_strs() omitted the "(and ...)"
   wrapper and emitted "(not (t1 ))", matching all types except t1.
   Notably, checkpolicy rejects "allow ~t1 ..." yet accepts
   "allow { -t1 } ...", causing monolithic checkpolicy to grant nothing
   while checkpolicy -C / semodule grants access to every type except
   t1.

2. Complemented negative-only or empty set (~{ -t1 } or flags ==
   TYPE_COMP / ROLE_COMP with an empty positive set): expand.c evaluates
   ~empty as all types/roles, whereas cil_print_attr_strs() emitted
   "(not (not (t1 )))" (matching only t1) or invalid "(not )".

3. TYPE_STAR with a non-empty negset (or extra flags/positive set):
   type_set_expand() selects all types not in negset and ignores the
   positive set and TYPE_COMP, whereas cil_print_attr_strs() emitted
   invalid CIL such as "(all)(not (t1 ))".

Update cil_print_attr_strs() to match expand.c semantics for all
combinations of STAR, COMP, positive, and negative sets, leaving the
attribute empty (no attributeset statement) when the resulting set is
empty so rules referencing it remain valid CIL (including inside
booleanif blocks) while expanding to no entries.

Add unit tests covering negative-only, complemented negative-only, and
star-with-negset conversion in libsepol/tests/test-module-to-cil.c.

Signed-off-by: Thiébaud Weksteen <tweek@google.com>
---
 libsepol/src/module_to_cil.c                  |  69 +++--
 libsepol/tests/libsepol-tests.c               |   2 +
 .../test-module-to-cil/typeset-comp.conf      |  24 ++
 .../policies/test-module-to-cil/typeset.conf  |  34 +++
 libsepol/tests/test-module-to-cil.c           | 268 ++++++++++++++++++
 libsepol/tests/test-module-to-cil.h           |  10 +
 6 files changed, 383 insertions(+), 24 deletions(-)
 create mode 100644 libsepol/tests/policies/test-module-to-cil/typeset-comp.conf
 create mode 100644 libsepol/tests/policies/test-module-to-cil/typeset.conf
 create mode 100644 libsepol/tests/test-module-to-cil.c
 create mode 100644 libsepol/tests/test-module-to-cil.h

diff --git a/libsepol/src/module_to_cil.c b/libsepol/src/module_to_cil.c
index 50b29b2e..7d9f02de 100644
--- a/libsepol/src/module_to_cil.c
+++ b/libsepol/src/module_to_cil.c
@@ -852,15 +852,17 @@ static int cil_print_attr_strs(int indent, struct policydb *pdb, int is_type,
 			       void *set, char *attr_name)
 {
 	// CIL doesn't support anonymous positive/negative/complemented sets.  So
-	// instead we create a CIL type/roleattributeset that matches the set. If
-	// the set has a negative set, then convert it to is (P & !N), where P is
-	// the list of members in the positive set and N is the list of members
-	// in the negative set. Additionally, if the set is complemented, then wrap
-	// the whole thing with a negation.
+	// instead we create a CIL type/roleattributeset that matches the set.
+	// Match expand.c semantics (type_set_expand() / role_set_expand()):
+	// - STAR selects all members except any in N (ignoring P and COMP).
+	// - Otherwise, if P is empty, (P & !N) is empty: emit (all) if COMP is
+	//   set, or leave the attribute empty otherwise.
+	// - Otherwise, emit P (or (P & !N) if N is non-empty), wrapped in
+	//   (not ...) if COMP is set.
 
 	struct ebitmap_node *node;
 	struct ebitmap *pos, *neg;
-	uint32_t flags;
+	uint32_t flags, star_flag, comp_flag;
 	unsigned i;
 	struct type_set *ts;
 	struct role_set *rs;
@@ -876,6 +878,8 @@ static int cil_print_attr_strs(int indent, struct policydb *pdb, int is_type,
 		pos = &ts->types;
 		neg = &ts->negset;
 		flags = ts->flags;
+		star_flag = TYPE_STAR;
+		comp_flag = TYPE_COMP;
 		has_positive = pos && !ebitmap_is_empty(pos);
 		has_negative = neg && !ebitmap_is_empty(neg);
 	} else {
@@ -885,33 +889,54 @@ static int cil_print_attr_strs(int indent, struct policydb *pdb, int is_type,
 		pos = &rs->roles;
 		neg = NULL;
 		flags = rs->flags;
+		star_flag = ROLE_STAR;
+		comp_flag = ROLE_COMP;
 		has_positive = pos && !ebitmap_is_empty(pos);
 		has_negative = 0;
 	}
 
 	cil_println(indent, "(%sattribute %s)", kind, attr_name);
-	cil_indent(indent);
-	cil_printf("(%sattributeset %s ", kind, attr_name);
 
-	if (flags & TYPE_STAR) {
-		cil_printf("(all)");
+	if (flags & star_flag) {
+		cil_indent(indent);
+		if (has_negative) {
+			cil_printf("(%sattributeset %s (not (", kind,
+				   attr_name);
+			ebitmap_for_each_positive_bit(neg, node, i) {
+				cil_printf("%s ", val_to_name[i]);
+			}
+			cil_printf(")))\n");
+		} else {
+			cil_printf("(%sattributeset %s (all))\n", kind,
+				   attr_name);
+		}
+		return rc;
+	}
+
+	if (!has_positive) {
+		if (flags & comp_flag) {
+			cil_println(indent, "(%sattributeset %s (all))", kind,
+				    attr_name);
+		}
+		return rc;
 	}
 
-	if (flags & TYPE_COMP) {
+	cil_indent(indent);
+	cil_printf("(%sattributeset %s ", kind, attr_name);
+
+	if (flags & comp_flag) {
 		cil_printf("(not ");
 	}
 
-	if (has_positive && has_negative) {
+	if (has_negative) {
 		cil_printf("(and ");
 	}
 
-	if (has_positive) {
-		cil_printf("(");
-		ebitmap_for_each_positive_bit(pos, node, i) {
-			cil_printf("%s ", val_to_name[i]);
-		}
-		cil_printf(") ");
+	cil_printf("(");
+	ebitmap_for_each_positive_bit(pos, node, i) {
+		cil_printf("%s ", val_to_name[i]);
 	}
+	cil_printf(") ");
 
 	if (has_negative) {
 		cil_printf("(not (");
@@ -920,14 +945,10 @@ static int cil_print_attr_strs(int indent, struct policydb *pdb, int is_type,
 			cil_printf("%s ", val_to_name[i]);
 		}
 
-		cil_printf("))");
-	}
-
-	if (has_positive && has_negative) {
-		cil_printf(")");
+		cil_printf(")))");
 	}
 
-	if (flags & TYPE_COMP) {
+	if (flags & comp_flag) {
 		cil_printf(")");
 	}
 
diff --git a/libsepol/tests/libsepol-tests.c b/libsepol/tests/libsepol-tests.c
index 1b70be01..c2746b8e 100644
--- a/libsepol/tests/libsepol-tests.c
+++ b/libsepol/tests/libsepol-tests.c
@@ -26,6 +26,7 @@
 #include "test-downgrade.h"
 #include "test-neverallow.h"
 #include "test-kernel-to-cil.h"
+#include "test-module-to-cil.h"
 #include "test-ocontext.h"
 
 #include <CUnit/Basic.h>
@@ -77,6 +78,7 @@ static bool do_tests(int interactive, int verbose)
 	DECLARE_SUITE(downgrade);
 	DECLARE_SUITE(neverallow);
 	DECLARE_SUITE(kernel_to_cil);
+	DECLARE_SUITE(module_to_cil);
 	DECLARE_SUITE(ocontext);
 
 	if (verbose)
diff --git a/libsepol/tests/policies/test-module-to-cil/typeset-comp.conf b/libsepol/tests/policies/test-module-to-cil/typeset-comp.conf
new file mode 100644
index 00000000..968ec7ac
--- /dev/null
+++ b/libsepol/tests/policies/test-module-to-cil/typeset-comp.conf
@@ -0,0 +1,24 @@
+class process
+
+sid kernel
+
+class process { transition fork }
+
+ifdef(`enable_mls',`
+sensitivity s0;
+dominance { s0 }
+category c0;
+level s0:c0;
+mlsconstrain process { transition } ( h1 dom h2 );
+')
+
+type type1;
+type type2;
+
+allow type1 self : process transition;
+neverallow ~{ -type1 } self : process fork;
+
+role sys_role;
+role sys_role types { type1 type2 };
+gen_user(sys_user,, sys_role, s0, s0)
+sid kernel gen_context(sys_user:sys_role:type1, s0)
diff --git a/libsepol/tests/policies/test-module-to-cil/typeset.conf b/libsepol/tests/policies/test-module-to-cil/typeset.conf
new file mode 100644
index 00000000..be48c632
--- /dev/null
+++ b/libsepol/tests/policies/test-module-to-cil/typeset.conf
@@ -0,0 +1,34 @@
+class process
+
+sid kernel
+
+class process { transition fork }
+
+ifdef(`enable_mls',`
+sensitivity s0;
+dominance { s0 }
+category c0;
+level s0:c0;
+mlsconstrain process { transition } ( h1 dom h2 );
+')
+
+type type1;
+type type2;
+
+bool test_bool false;
+
+allow type1 self : process transition;
+allow { -type1 } self : process transition;
+neverallow type2 self : process transition;
+
+allow type2 self : process fork;
+neverallow { -type1 } self : process fork;
+
+if (test_bool) {
+	allow { -type1 } self : process transition;
+}
+
+role sys_role;
+role sys_role types { type1 type2 };
+gen_user(sys_user,, sys_role, s0, s0)
+sid kernel gen_context(sys_user:sys_role:type1, s0)
diff --git a/libsepol/tests/test-module-to-cil.c b/libsepol/tests/test-module-to-cil.c
new file mode 100644
index 00000000..1d767058
--- /dev/null
+++ b/libsepol/tests/test-module-to-cil.c
@@ -0,0 +1,268 @@
+#include "test-module-to-cil.h"
+
+#include "helpers.h"
+
+#include <sepol/cil/cil.h>
+#include <sepol/module_to_cil.h>
+#include <sepol/policydb/expand.h>
+#include <sepol/policydb/link.h>
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+extern int mls;
+
+int module_to_cil_test_init(void)
+{
+	return 0;
+}
+
+int module_to_cil_test_cleanup(void)
+{
+	return 0;
+}
+
+static int quiet_cil_logs;
+
+static void test_cil_log_handler(int lvl, const char *msg)
+{
+	(void)lvl;
+	if (!quiet_cil_logs)
+		fprintf(stderr, "%s", msg);
+}
+
+static int compile_cil_buf(const char *buf, size_t len, const char *extra_cil,
+			   int quiet)
+{
+	cil_db_t *db = NULL;
+	sepol_policydb_t *pdb = NULL;
+	int rc;
+
+	quiet_cil_logs = quiet;
+	cil_set_log_handler(test_cil_log_handler);
+
+	cil_db_init(&db);
+	cil_set_mls(db, mls);
+
+	rc = cil_add_file(db, "test.cil", buf, len);
+	if (rc == SEPOL_OK && extra_cil)
+		rc = cil_add_file(db, "extra.cil", extra_cil,
+				  strlen(extra_cil));
+	if (rc == SEPOL_OK)
+		rc = cil_compile(db);
+	if (rc == SEPOL_OK)
+		rc = cil_build_policydb(db, &pdb);
+
+	sepol_policydb_free(pdb);
+	cil_db_destroy(&db);
+	quiet_cil_logs = 0;
+
+	return rc;
+}
+
+static char *module_to_cil_buf(policydb_t *pdb, size_t *len)
+{
+	char *buf = NULL;
+	FILE *out;
+
+	/*
+	 * sepol_module_policydb_to_cil() overwrites pdb->name for POLICY_BASE
+	 * without freeing any existing name set by read_source_policy().
+	 */
+	free(pdb->name);
+	pdb->name = NULL;
+
+	out = open_memstream(&buf, len);
+	CU_ASSERT_PTR_NOT_NULL_FATAL(out);
+	CU_ASSERT(sepol_module_policydb_to_cil(out, pdb, 1) == 0);
+	CU_ASSERT_FATAL(fclose(out) == 0);
+	CU_ASSERT_PTR_NOT_NULL_FATAL(buf);
+
+	return buf;
+}
+
+static int has_cil_line_with(const char *buf, const char *prefix,
+			     const char *suffix)
+{
+	const char *line = buf;
+
+	while (line && *line) {
+		const char *nl = strchr(line, '\n');
+		size_t line_len = nl ? (size_t)(nl - line) : strlen(line);
+		size_t pre_len = strlen(prefix);
+		size_t suf_len = strlen(suffix);
+
+		if (line_len >= pre_len + suf_len &&
+		    strncmp(line, prefix, pre_len) == 0 &&
+		    strncmp(line + line_len - suf_len, suffix, suf_len) == 0)
+			return 1;
+
+		if (!nl)
+			break;
+		line = nl + 1;
+	}
+
+	return 0;
+}
+
+/*
+ * Silence expected parser warnings when loading test policies that
+ * intentionally use negative-only type sets.
+ */
+static int load_policy_quiet(policydb_t *p, const char *policy_name)
+{
+	int saved_stderr, devnull, rc;
+
+	fflush(stderr);
+	saved_stderr = dup(STDERR_FILENO);
+	devnull = open("/dev/null", O_WRONLY);
+	if (saved_stderr >= 0 && devnull >= 0) {
+		if (dup2(devnull, STDERR_FILENO) < 0) {
+			close(saved_stderr);
+			saved_stderr = -1;
+		}
+	}
+	if (devnull >= 0)
+		close(devnull);
+
+	rc = test_load_policy(p, POLICY_BASE, mls, "test-module-to-cil",
+			      policy_name);
+
+	fflush(stderr);
+	if (saved_stderr >= 0) {
+		(void)dup2(saved_stderr, STDERR_FILENO);
+		close(saved_stderr);
+	}
+
+	return rc;
+}
+
+/*
+ * A type set with only negative types ({ -type1 }) and no flags expands to
+ * the empty set in expand.c. module_to_cil must emit an empty attribute
+ * rather than (not (type1 )), matching expand.c for allow, neverallow, and
+ * conditional rules.
+ */
+static void test_module_to_cil_negset_without_positive(void)
+{
+	policydb_t basemod, expanded;
+	char *buf = NULL;
+	size_t len = 0;
+
+	if (load_policy_quiet(&basemod, "typeset.conf"))
+		CU_FAIL_FATAL("Failed to load policy");
+
+	if (link_modules(NULL, &basemod, NULL, 0, 0))
+		CU_FAIL_FATAL("Failed to link base module");
+
+	if (policydb_init(&expanded))
+		CU_FAIL_FATAL("Failed to initialize policy");
+
+	CU_ASSERT(expand_module(NULL, &basemod, &expanded, 0, 1) == 0);
+
+	buf = module_to_cil_buf(&basemod, &len);
+	CU_ASSERT_PTR_NULL(strstr(buf, "(typeattributeset "));
+	CU_ASSERT_PTR_NULL(strstr(buf, "(not (type1"));
+	CU_ASSERT(compile_cil_buf(buf, len, NULL, 0) == SEPOL_OK);
+
+	free(buf);
+	policydb_destroy(&basemod);
+	policydb_destroy(&expanded);
+}
+
+/*
+ * A complemented negative-only type set (~{ -type1 }) expands to ~empty = all
+ * in expand.c. module_to_cil must emit (all) rather than (not (not (type1 ))).
+ */
+static void test_module_to_cil_comp_negset_without_positive(void)
+{
+	policydb_t basemod;
+	char *buf = NULL;
+	size_t len = 0;
+
+	if (load_policy_quiet(&basemod, "typeset-comp.conf"))
+		CU_FAIL_FATAL("Failed to load policy");
+
+	if (link_modules(NULL, &basemod, NULL, 0, 0))
+		CU_FAIL_FATAL("Failed to link base module");
+
+	buf = module_to_cil_buf(&basemod, &len);
+	CU_ASSERT(has_cil_line_with(buf, "(typeattributeset ", " (all))"));
+	CU_ASSERT_PTR_NULL(strstr(buf, "(not (not"));
+	CU_ASSERT(compile_cil_buf(buf, len, NULL, 0) == SEPOL_OK);
+
+	/* ~{ -type1 } matches all types, including type2. */
+	CU_ASSERT(compile_cil_buf(buf, len,
+				  "(allow type2 self (process (fork)))",
+				  1) != SEPOL_OK);
+
+	free(buf);
+	policydb_destroy(&basemod);
+}
+
+/*
+ * A type set with TYPE_STAR and a non-empty negset expands to all types
+ * except negset in expand.c. module_to_cil must emit (not (<negset>)) rather
+ * than invalid "(all)(not (<negset>))".
+ */
+static void test_module_to_cil_star_with_negset(void)
+{
+	policydb_t basemod;
+	char *buf = NULL;
+	size_t len = 0;
+	avrule_t *na_rule;
+
+	if (load_policy_quiet(&basemod, "typeset-comp.conf"))
+		CU_FAIL_FATAL("Failed to load policy");
+
+	if (link_modules(NULL, &basemod, NULL, 0, 0))
+		CU_FAIL_FATAL("Failed to link base module");
+
+	na_rule = basemod.global->branch_list->avrules;
+	while (na_rule && na_rule->specified != AVRULE_NEVERALLOW)
+		na_rule = na_rule->next;
+	CU_ASSERT_PTR_NOT_NULL_FATAL(na_rule);
+
+	/* Switch the neverallow's stypes from TYPE_COMP to TYPE_STAR + negset={type1}. */
+	na_rule->stypes.flags = TYPE_STAR;
+
+	buf = module_to_cil_buf(&basemod, &len);
+	CU_ASSERT(has_cil_line_with(buf, "(typeattributeset ",
+				    " (not (type1 )))"));
+	CU_ASSERT_PTR_NULL(strstr(buf, "(all)(not"));
+
+	/* type1 is excluded by negset; type2 is matched by TYPE_STAR \ {type1}. */
+	CU_ASSERT(compile_cil_buf(buf, len,
+				  "(allow type1 self (process (fork)))",
+				  0) == SEPOL_OK);
+	CU_ASSERT(compile_cil_buf(buf, len,
+				  "(allow type2 self (process (fork)))",
+				  1) != SEPOL_OK);
+
+	free(buf);
+	policydb_destroy(&basemod);
+}
+
+int module_to_cil_add_tests(CU_pSuite suite)
+{
+	if (NULL == CU_add_test(suite, "module_to_cil_negset_without_positive",
+				test_module_to_cil_negset_without_positive)) {
+		CU_cleanup_registry();
+		return CU_get_error();
+	}
+	if (NULL ==
+	    CU_add_test(suite, "module_to_cil_comp_negset_without_positive",
+			test_module_to_cil_comp_negset_without_positive)) {
+		CU_cleanup_registry();
+		return CU_get_error();
+	}
+	if (NULL == CU_add_test(suite, "module_to_cil_star_with_negset",
+				test_module_to_cil_star_with_negset)) {
+		CU_cleanup_registry();
+		return CU_get_error();
+	}
+	return 0;
+}
diff --git a/libsepol/tests/test-module-to-cil.h b/libsepol/tests/test-module-to-cil.h
new file mode 100644
index 00000000..0d20fd48
--- /dev/null
+++ b/libsepol/tests/test-module-to-cil.h
@@ -0,0 +1,10 @@
+#ifndef TEST_MODULE_TO_CIL_H__
+#define TEST_MODULE_TO_CIL_H__
+
+#include <CUnit/Basic.h>
+
+int module_to_cil_test_init(void);
+int module_to_cil_test_cleanup(void);
+int module_to_cil_add_tests(CU_pSuite suite);
+
+#endif
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH userspace 2/2] checkpolicy: warn on negative-only type sets
  2026-11-26  3:29 [PATCH userspace 0/2] Fix and warn on negative-only type sets Thiébaud Weksteen
  2026-11-26  3:29 ` [PATCH userspace 1/2] libsepol: align module_to_cil type/role set handling with expand.c Thiébaud Weksteen
@ 2026-11-26  3:29 ` Thiébaud Weksteen
  2026-11-30 15:46 ` [PATCH userspace 0/2] Fix and " Stephen Smalley
  2 siblings, 0 replies; 5+ messages in thread
From: Thiébaud Weksteen @ 2026-11-26  3:29 UTC (permalink / raw)
  To: selinux
  Cc: Thiébaud Weksteen, Stephen Smalley, James Carter,
	Petr Lautrbach, Christian Göttsche, Inseob Kim,
	Jeff Vander Stoep

Policy authors sometimes write "{ -foo }" expecting set complement
("~foo"), when in fact "-" is set subtraction from the positive set in
the same expression.  Without any positive types (and without "*"),
"{ -foo }" evaluates to the empty set and "~{ -foo }" evaluates to all
types, silently turning rules such as "neverallow { -foo } ..." into
no-ops.

Emit a warning via yywarn() when a parsed type set has an empty positive
set, a non-empty negative set, and does not have TYPE_STAR set.  Using
yywarn() avoids breaking existing policies on upgrade while still
failing strict builds that pass -E / --werror.

Because the check inspects the parsed type_set_t bitmaps before
expansion, standard cases that legitimately expand to an empty set are
unaffected:
- Empty attributes (e.g. "{ empty_attr -foo }"), where the positive set
  contains the attribute symbol and types may be associated in other
  files or modules.
- Self-canceling sets (e.g. "{ foo -foo }" or "{ attr -foo }" when attr
  only contains foo), which can occur via macro expansion and already
  have identical empty semantics in expand.c and module_to_cil.c.
- "{ -self }" and "~self", which record RULE_NOTSELF without populating
  negset.

Add a regression test script covering AV rules, extended permission
rules, transition rules, role-types statements, checkmodule -m, and
valid set-subtraction/complement controls (including empty attributes
and self-canceling sets).

Signed-off-by: Thiébaud Weksteen <tweek@google.com>
---
 checkpolicy/Makefile                       |   1 +
 checkpolicy/policy_define.c                |  24 ++
 checkpolicy/tests/test_negative_typeset.sh | 245 +++++++++++++++++++++
 3 files changed, 270 insertions(+)
 create mode 100644 checkpolicy/tests/test_negative_typeset.sh

diff --git a/checkpolicy/Makefile b/checkpolicy/Makefile
index 9d5b7123..9d0c69d6 100644
--- a/checkpolicy/Makefile
+++ b/checkpolicy/Makefile
@@ -57,6 +57,7 @@ test: checkpolicy checkmodule tests/test-type-enabled
 	sh ./tests/test_conditional_count.sh
 	./tests/test-type-enabled
 	sh ./tests/test_multiple_decls.sh
+	sh ./tests/test_negative_typeset.sh
 
 tests/test-type-enabled: tests/test_type_enabled.c $(LIBSEPOLA)
 	$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LDLIBS_LIBSEPOLA)
diff --git a/checkpolicy/policy_define.c b/checkpolicy/policy_define.c
index 349cd355..5e9715ea 100644
--- a/checkpolicy/policy_define.c
+++ b/checkpolicy/policy_define.c
@@ -1729,6 +1729,18 @@ oom:
 	return -1;
 }
 
+static void check_type_set(const type_set_t *set)
+{
+	if (ebitmap_is_empty(&set->types) && !ebitmap_is_empty(&set->negset) &&
+	    !(set->flags & TYPE_STAR)) {
+		if (set->flags & TYPE_COMP) {
+			yywarn("complemented type set has no positive types and matches all types");
+		} else {
+			yywarn("type set has no positive types and evaluates to the empty set");
+		}
+	}
+}
+
 static int define_compute_type_helper(int which, avrule_t **rule)
 {
 	char *id;
@@ -1761,6 +1773,7 @@ static int define_compute_type_helper(int which, avrule_t **rule)
 		if (set_types(&avrule->stypes, id, &add, 0))
 			goto bad;
 	}
+	check_type_set(&avrule->stypes);
 	add = 1;
 	while ((id = queue_remove(id_queue))) {
 		if (strcmp(id, "self") == 0) {
@@ -1775,6 +1788,7 @@ static int define_compute_type_helper(int which, avrule_t **rule)
 		if (set_types(&avrule->ttypes, id, &add, 0))
 			goto bad;
 	}
+	check_type_set(&avrule->ttypes);
 
 	if (read_classes(&tclasses))
 		goto bad;
@@ -2236,6 +2250,7 @@ static int define_te_avtab_xperms_helper(int which, avrule_t **rule)
 			goto out;
 		}
 	}
+	check_type_set(&avrule->stypes);
 	add = 1;
 	while ((id = queue_remove(id_queue))) {
 		if (strcmp(id, "self") == 0) {
@@ -2268,6 +2283,7 @@ static int define_te_avtab_xperms_helper(int which, avrule_t **rule)
 			avrule->flags |= RULE_NOTSELF;
 		}
 	}
+	check_type_set(&avrule->ttypes);
 
 	ebitmap_init(&tclasses);
 	ret = read_classes(&tclasses);
@@ -2867,6 +2883,7 @@ static int define_te_avtab_helper(int which, avrule_t **rule)
 			goto out;
 		}
 	}
+	check_type_set(&avrule->stypes);
 	add = 1;
 	while ((id = queue_remove(id_queue))) {
 		if (strcmp(id, "self") == 0) {
@@ -2903,6 +2920,7 @@ static int define_te_avtab_helper(int which, avrule_t **rule)
 			avrule->flags |= RULE_NOTSELF;
 		}
 	}
+	check_type_set(&avrule->ttypes);
 
 	ret = read_classes(&tclasses);
 	if (ret)
@@ -3092,6 +3110,7 @@ int define_role_types(void)
 		if (set_types(&role->types, id, &add, 0))
 			return -1;
 	}
+	check_type_set(&role->types);
 
 	return 0;
 }
@@ -3330,6 +3349,7 @@ int define_role_trans(int class_specified)
 		if (set_types(&types, id, &add, 0))
 			goto bad;
 	}
+	check_type_set(&types);
 
 	if (class_specified) {
 		if (read_classes(&e_classes))
@@ -3532,6 +3552,7 @@ int define_filename_trans(void)
 		if (set_types(&stypes, id, &add, 0))
 			goto bad;
 	}
+	check_type_set(&stypes);
 
 	self = 0;
 	add = 1;
@@ -3548,6 +3569,7 @@ int define_filename_trans(void)
 		if (set_types(&ttypes, id, &add, 0))
 			goto bad;
 	}
+	check_type_set(&ttypes);
 
 	if (read_classes(&e_tclasses))
 		goto bad;
@@ -6257,11 +6279,13 @@ int define_range_trans(int class_specified)
 		if (set_types(&rule->stypes, id, &add, 0))
 			goto out;
 	}
+	check_type_set(&rule->stypes);
 	add = 1;
 	while ((id = queue_remove(id_queue))) {
 		if (set_types(&rule->ttypes, id, &add, 0))
 			goto out;
 	}
+	check_type_set(&rule->ttypes);
 
 	if (class_specified) {
 		if (read_classes(&rule->tclasses))
diff --git a/checkpolicy/tests/test_negative_typeset.sh b/checkpolicy/tests/test_negative_typeset.sh
new file mode 100644
index 00000000..23204c05
--- /dev/null
+++ b/checkpolicy/tests/test_negative_typeset.sh
@@ -0,0 +1,245 @@
+#!/bin/sh
+set -eu
+
+BASEDIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
+CHECKPOLICY=${CHECKPOLICY:-"${BASEDIR}/../checkpolicy"}
+CHECKMODULE=${CHECKMODULE:-"${BASEDIR}/../checkmodule"}
+OUTDIR=$(mktemp -d "${TMPDIR:-/tmp}/negative-typeset.XXXXXX")
+trap 'rm -rf "$OUTDIR"' EXIT HUP INT TERM
+
+WARN_EMPTY="type set has no positive types and evaluates to the empty set"
+WARN_COMP="complemented type set has no positive types and matches all types"
+PASS=0
+
+write_conf() {
+	out=$1
+	mls=$2
+	extra=$3
+
+	{
+		printf '%s\n' \
+			'class CLASS1' \
+			'sid kernel' \
+			'class CLASS1 { PERM1 ioctl }'
+		if [ "$mls" = "1" ]; then
+			printf '%s\n' \
+				'sensitivity s0;' \
+				'dominance { s0 }' \
+				'category c0;' \
+				'level s0:c0;' \
+				'mlsconstrain CLASS1 { PERM1 } l1 == l2;'
+		fi
+		printf '%s\n' \
+			'attribute ATTR1;' \
+			'type TYPE1, ATTR1;' \
+			'type TYPE2, ATTR1;' \
+			'allow TYPE1 self:CLASS1 PERM1;' \
+			'role ROLE1;' \
+			'role ROLE1 types { TYPE1 TYPE2 };'
+		printf '%s\n' "$extra"
+		if [ "$mls" = "1" ]; then
+			printf '%s\n' \
+				'user USER1 roles ROLE1 level s0 range s0 - s0;' \
+				'sid kernel USER1:ROLE1:TYPE1:s0 - s0'
+		else
+			printf '%s\n' \
+				'user USER1 roles ROLE1;' \
+				'sid kernel USER1:ROLE1:TYPE1'
+		fi
+	} > "$out"
+}
+
+expect_conf_warn() {
+	desc=$1
+	mls=$2
+	rule=$3
+	pattern=$4
+
+	conf="$OUTDIR/policy.conf"
+	bin="$OUTDIR/policy.bin"
+	err="$OUTDIR/stderr.txt"
+	opts=""
+	if [ "$mls" = "1" ]; then
+		opts="-M"
+	fi
+
+	write_conf "$conf" "$mls" "$rule"
+
+	rm -f "$bin"
+	# Without -E: succeeds with a warning on stderr.
+	"$CHECKPOLICY" $opts -o "$bin" "$conf" 2> "$err"
+	if ! grep -Fq "$pattern" "$err"; then
+		echo "FAIL ($desc): expected warning '$pattern'" >&2
+		cat "$err" >&2
+		exit 1
+	fi
+
+	rm -f "$bin"
+	# With -E: fails and produces no output file.
+	if "$CHECKPOLICY" $opts -E -o "$bin" "$conf" 2> "$err"; then
+		echo "FAIL ($desc): expected failure with -E" >&2
+		exit 1
+	fi
+	if [ -f "$bin" ]; then
+		echo "FAIL ($desc): unexpected output file on -E failure" >&2
+		exit 1
+	fi
+	if ! grep -Fq "$pattern" "$err"; then
+		echo "FAIL ($desc): expected error '$pattern' with -E" >&2
+		cat "$err" >&2
+		exit 1
+	fi
+
+	PASS=$((PASS + 1))
+}
+
+expect_conf_clean() {
+	desc=$1
+	mls=$2
+	rule=$3
+
+	conf="$OUTDIR/policy.conf"
+	bin="$OUTDIR/policy.bin"
+	err="$OUTDIR/stderr.txt"
+	opts=""
+	if [ "$mls" = "1" ]; then
+		opts="-M"
+	fi
+
+	write_conf "$conf" "$mls" "$rule"
+	rm -f "$bin"
+	if ! "$CHECKPOLICY" $opts -E -o "$bin" "$conf" 2> "$err"; then
+		echo "FAIL ($desc): unexpected failure with -E" >&2
+		cat "$err" >&2
+		exit 1
+	fi
+	if [ ! -s "$bin" ]; then
+		echo "FAIL ($desc): expected non-empty output" >&2
+		exit 1
+	fi
+
+	PASS=$((PASS + 1))
+}
+
+expect_mod_warn() {
+	desc=$1
+	rule=$2
+	pattern=$3
+
+	te="$OUTDIR/testmod.te"
+	mod="$OUTDIR/testmod.mod"
+	err="$OUTDIR/stderr.txt"
+
+	cat > "$te" <<EOF
+module testmod 1.0;
+require {
+	attribute ATTR1;
+	type TYPE1, TYPE2;
+	role ROLE1;
+	class CLASS1 { PERM1 };
+}
+$rule
+EOF
+
+	rm -f "$mod"
+	"$CHECKMODULE" -m -o "$mod" "$te" 2> "$err"
+	if ! grep -Fq "$pattern" "$err"; then
+		echo "FAIL ($desc): expected warning '$pattern'" >&2
+		cat "$err" >&2
+		exit 1
+	fi
+
+	rm -f "$mod"
+	if "$CHECKMODULE" -m -E -o "$mod" "$te" 2> "$err"; then
+		echo "FAIL ($desc): expected failure with -E" >&2
+		exit 1
+	fi
+	if [ -f "$mod" ]; then
+		echo "FAIL ($desc): unexpected output module on -E failure" >&2
+		exit 1
+	fi
+
+	PASS=$((PASS + 1))
+}
+
+expect_mod_clean() {
+	desc=$1
+	rule=$2
+
+	te="$OUTDIR/testmod.te"
+	mod="$OUTDIR/testmod.mod"
+	err="$OUTDIR/stderr.txt"
+
+	cat > "$te" <<EOF
+module testmod 1.0;
+require {
+	attribute ATTR1;
+	type TYPE1, TYPE2;
+	role ROLE1;
+	class CLASS1 { PERM1 };
+}
+$rule
+EOF
+
+	rm -f "$mod"
+	if ! "$CHECKMODULE" -m -E -o "$mod" "$te" 2> "$err"; then
+		echo "FAIL ($desc): unexpected failure with -E" >&2
+		cat "$err" >&2
+		exit 1
+	fi
+
+	PASS=$((PASS + 1))
+}
+
+# Negative-only type sets must warn (and fail with -E).
+expect_conf_warn "neverallow source" 0 \
+	"neverallow { -TYPE1 } self:CLASS1 PERM1;" "$WARN_EMPTY"
+expect_conf_warn "allow source" 0 \
+	"allow { -TYPE1 } self:CLASS1 PERM1;" "$WARN_EMPTY"
+expect_conf_warn "allow target" 0 \
+	"allow TYPE1 { -TYPE2 }:CLASS1 PERM1;" "$WARN_EMPTY"
+expect_conf_warn "neverallow complemented" 0 \
+	"neverallow ~{ -TYPE1 } self:CLASS1 ioctl;" "$WARN_COMP"
+expect_conf_warn "neverallowxperm source" 0 \
+	"neverallowxperm { -TYPE1 } self:CLASS1 ioctl 0x1;" "$WARN_EMPTY"
+expect_conf_warn "type_transition source" 0 \
+	"type_transition { -TYPE1 } TYPE2:CLASS1 TYPE1;" "$WARN_EMPTY"
+expect_conf_warn "filename type_transition source" 0 \
+	'type_transition { -TYPE1 } TYPE2:CLASS1 TYPE1 "foo";' "$WARN_EMPTY"
+expect_conf_warn "role_transition target" 0 \
+	"role_transition ROLE1 { -TYPE1 }:CLASS1 ROLE1;" "$WARN_EMPTY"
+expect_conf_warn "range_transition source" 1 \
+	"range_transition { -TYPE1 } TYPE2:CLASS1 s0;" "$WARN_EMPTY"
+expect_conf_warn "role types negative-only" 0 \
+	"role ROLE2; role ROLE2 types { -TYPE1 };" "$WARN_EMPTY"
+
+expect_mod_warn "module allow negative-only" \
+	"allow { -TYPE1 } self:CLASS1 PERM1;" "$WARN_EMPTY"
+expect_mod_warn "module neverallow complemented negative-only" \
+	"neverallow ~{ -TYPE1 } self:CLASS1 PERM1;" "$WARN_COMP"
+expect_mod_warn "module role types negative-only" \
+	"role ROLE1 types { -TYPE1 };" "$WARN_EMPTY"
+
+# Valid type sets must compile cleanly with -E.
+expect_conf_clean "set subtraction with positive attr" 0 \
+	"allow { ATTR1 -TYPE1 } self:CLASS1 PERM1;"
+expect_conf_clean "set subtraction with empty attr" 0 \
+	"attribute EMPTY_ATTR; allow { EMPTY_ATTR -TYPE1 } self:CLASS1 PERM1;"
+expect_conf_clean "self-canceling set" 0 \
+	"allow { TYPE1 -TYPE1 } self:CLASS1 PERM1;"
+expect_conf_clean "set complement" 0 \
+	"neverallow ~TYPE1 self:CLASS1 ioctl;"
+expect_conf_clean "complemented subtraction with positive attr" 0 \
+	"neverallow ~{ ATTR1 -TYPE1 } self:CLASS1 ioctl;"
+expect_conf_clean "star wildcard" 0 \
+	"neverallow * self:CLASS1 ioctl;"
+expect_conf_clean "neverallow -self" 0 \
+	"neverallow TYPE1 { -self }:CLASS1 ioctl;"
+expect_conf_clean "neverallow ~self" 0 \
+	"neverallow TYPE1 ~self:CLASS1 ioctl;"
+expect_conf_clean "role types subtraction after positive types" 0 \
+	"role ROLE1 types { -TYPE2 };"
+expect_mod_clean "module set subtraction with positive attr" \
+	"allow { ATTR1 -TYPE1 } self:CLASS1 PERM1;"
+
+printf 'PASS %s negative-only type set tests\n' "$PASS"
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH userspace 0/2] Fix and warn on negative-only type sets
  2026-11-26  3:29 [PATCH userspace 0/2] Fix and warn on negative-only type sets Thiébaud Weksteen
  2026-11-26  3:29 ` [PATCH userspace 1/2] libsepol: align module_to_cil type/role set handling with expand.c Thiébaud Weksteen
  2026-11-26  3:29 ` [PATCH userspace 2/2] checkpolicy: warn on negative-only type sets Thiébaud Weksteen
@ 2026-11-30 15:46 ` Stephen Smalley
  2026-11-30 22:29   ` Thiébaud Weksteen
  2 siblings, 1 reply; 5+ messages in thread
From: Stephen Smalley @ 2026-11-30 15:46 UTC (permalink / raw)
  To: Thiébaud Weksteen
  Cc: selinux, James Carter, Petr Lautrbach, Christian Göttsche,
	Inseob Kim, Jeff Vander Stoep

On Wed, Nov 25, 2026 at 10:29 PM Thiébaud Weksteen <tweek@google.com> wrote:
>
> This series addresses two related issues with negative-only type sets
> such as "{ -TYPE1 }" (and "~{ -TYPE1 }"): a semantic divergence between
> libsepol's expansion and CIL conversion paths, and a silent pitfall for
> policy authors.
>
> 1. Divergence between expand.c and module_to_cil.c
> --------------------------------------------------
>
> In SELinux policy language, "-" performs set subtraction from the
> positive types listed in the same set, rather than set complement ("~").
> Currently, libsepol's monolithic expansion path (expand.c) and CIL
> conversion path (module_to_cil.c) disagree on how negative-only type
> sets are evaluated:
>
> - libsepol/src/expand.c:type_set_expand() computes (empty \ {TYPE1}) =
>   empty, so the rule is a no-op.
> - libsepol/src/module_to_cil.c:cil_print_attr_strs() omits the
>   "(and ...)" wrapper when the positive set is empty and emits
>   "(typeattributeset base_typeattr_1 (not (TYPE1 )))", treating
>   "{ -TYPE1 }" as "~TYPE1" (all types except TYPE1).
>
> Beyond causing monolithic checkpolicy and checkpolicy -C / semodule to
> disagree on the same policy, this also undermines checkpolicy's ban on
> complemented allow rules: checkpolicy rejects "allow ~TYPE1 ...", yet
> accepts "allow { -TYPE1 } ...", which grants nothing under expand.c but
> grants access to every type except TYPE1 when converted to CIL.
>
> Two related edge cases in cil_print_attr_strs() also diverge from
> expand.c: "~{ -TYPE1 }" emits "(not (not (TYPE1 )))" instead of "(all)",
> and TYPE_STAR with a non-empty negset emits invalid "(all)(not (...))"
> instead of "(not (...))".
>
> 2. Silent pitfall for policy authors
> ------------------------------------
>
> Because "{ attr -foo }" is a common idiom in SELinux policy, authors
> occasionally omit the positive set and write "{ -foo }" when they intend
> the set complement "~foo" (all types other than foo).  This mistake is
> easy to miss in code review and has surprising, silent consequences:
>
> - Checkpolicy accepts "{ -foo }" without any warning or error.
> - Under expand.c (and under CIL once patch 1 aligns the two paths),
>   "{ -foo }" evaluates to the empty set.  In a neverallow rule (e.g.
>   "neverallow { -domain } ..."), the assertion silently becomes a no-op
>   and provides no protection, while still looking like a valid invariant
>   to readers.
> - Previously, the bug in module_to_cil.c masked the mistake whenever
>   policies were checked through CIL, while monolithic checkpolicy (and
>   tools built on expand.c) silently ignored the rule.
> - Similarly, "~{ -foo }" silently expands to all types rather than foo.
>
> Warning when a parsed type set has a non-empty negative set without any
> positive types (and without "*") catches this mistake at compile time,
> while leaving standard cases that legitimately expand to an empty set
> (such as "{ empty_attr -foo }", "{ foo -foo }", or "{ -self }")
> unaffected.
>
> Series overview
> ---------------
>
> 1. libsepol: align module_to_cil type/role set handling with expand.c
>    Updates cil_print_attr_strs() to match type_set_expand() and
>    role_set_expand() for empty/negative-only, complemented empty, and
>    star-with-negset sets, and adds CUnit regression tests.
>
> 2. checkpolicy: warn on negative-only type sets
>    Emits a warning via yywarn() when a type set has no positive types
>    (and no "*") alongside a non-empty negative set, with regression
>    tests.  yywarn() is used rather than yyerror() to avoid breaking
>    existing/frozen policies on upgrade while still failing builds that
>    pass -E / --werror (happy to switch to yyerror() if preferred).
>
> Tested with `make test` across libsepol, checkpolicy, and secilc under
> gcc, clang, and gcc with ASan/UBSan, as well as `make check-format`.
>
> Minimal reproducer (expand.c vs module_to_cil.c divergence)
> -----------------------------------------------------------
>
> Given the following 11-line policy (poc.conf):
>
>     class CLASS1
>     sid kernel
>     class CLASS1 { PERM1 }
>     type TYPE1;
>     type TYPE2;
>     allow TYPE2 self:CLASS1 PERM1;
>     neverallow { -TYPE1 } self:CLASS1 PERM1;
>     role ROLE1;
>     role ROLE1 types { TYPE1 TYPE2 };
>     user USER1 roles ROLE1;
>     sid kernel USER1:ROLE1:TYPE1
>
> Before this series, compiling directly with checkpolicy (expand.c)
> succeeds silently because the neverallow expands to the empty set:
>
>     $ checkpolicy -o policy_mono.bin poc.conf
>     $ echo $?
>     0
>
> Converting to CIL and compiling with secilc (module_to_cil.c) fails the
> neverallow assertion because module_to_cil emitted "(not (TYPE1 ))":
>
>     $ checkpolicy -C -o poc.cil poc.conf && \
>       secilc -M false -f /dev/null -o policy_cil.bin poc.cil
>     neverallow check failed at poc.cil:30 from poc.conf:7
>       (neverallow base_typeattr_1 self (CLASS1 (PERM1)))
>         <root>
>         allow at poc.cil:27
>           (allow TYPE2 self (CLASS1 (PERM1)))
>
>     Failed to generate binary
>     Failed to build policydb
>     $ echo $?
>     255
>
> Thiébaud Weksteen (2):
>   libsepol: align module_to_cil type/role set handling with expand.c
>   checkpolicy: warn on negative-only type sets
>
>  checkpolicy/Makefile                          |   1 +
>  checkpolicy/policy_define.c                   |  24 ++
>  checkpolicy/tests/test_negative_typeset.sh    | 245 ++++++++++++++++
>  libsepol/src/module_to_cil.c                  |  69 +++--
>  libsepol/tests/libsepol-tests.c               |   2 +
>  .../test-module-to-cil/typeset-comp.conf      |  24 ++
>  .../policies/test-module-to-cil/typeset.conf  |  34 +++
>  libsepol/tests/test-module-to-cil.c           | 268 ++++++++++++++++++
>  libsepol/tests/test-module-to-cil.h           |  10 +
>  9 files changed, 653 insertions(+), 24 deletions(-)
>  create mode 100644 checkpolicy/tests/test_negative_typeset.sh
>  create mode 100644 libsepol/tests/policies/test-module-to-cil/typeset-comp.conf
>  create mode 100644 libsepol/tests/policies/test-module-to-cil/typeset.conf
>  create mode 100644 libsepol/tests/test-module-to-cil.c
>  create mode 100644 libsepol/tests/test-module-to-cil.h

Thanks for the report and patches, but selinux/main is currently
frozen for the 3.12 release and only taking medium or higher severity
security fixes and medium or higher bug fixes for bugs introduced
since 3.10.
I don't believe this qualifies; if you feel differently, let me know.
Otherwise, I will hold off on merging these until after 3.12 is
released later this week.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH userspace 0/2] Fix and warn on negative-only type sets
  2026-11-30 15:46 ` [PATCH userspace 0/2] Fix and " Stephen Smalley
@ 2026-11-30 22:29   ` Thiébaud Weksteen
  0 siblings, 0 replies; 5+ messages in thread
From: Thiébaud Weksteen @ 2026-11-30 22:29 UTC (permalink / raw)
  To: Stephen Smalley
  Cc: selinux, James Carter, Petr Lautrbach, Christian Göttsche,
	Inseob Kim, Jeff Vander Stoep

On Tue, Dec 1, 2026 at 2:47 AM Stephen Smalley
<stephen.smalley.work@gmail.com> wrote:
>
> On Wed, Nov 25, 2026 at 10:29 PM Thiébaud Weksteen <tweek@google.com> wrote:
> >
> > This series addresses two related issues with negative-only type sets
> > such as "{ -TYPE1 }" (and "~{ -TYPE1 }"): a semantic divergence between
> > libsepol's expansion and CIL conversion paths, and a silent pitfall for
> > policy authors.
> >
>
> Thanks for the report and patches, but selinux/main is currently
> frozen for the 3.12 release and only taking medium or higher severity
> security fixes and medium or higher bug fixes for bugs introduced
> since 3.10.
> I don't believe this qualifies; if you feel differently, let me know.
> Otherwise, I will hold off on merging these until after 3.12 is
> released later this week.

That sounds reasonable to me. Thanks Stephen.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-30 22:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  3:29 [PATCH userspace 0/2] Fix and warn on negative-only type sets Thiébaud Weksteen
2026-11-26  3:29 ` [PATCH userspace 1/2] libsepol: align module_to_cil type/role set handling with expand.c Thiébaud Weksteen
2026-11-26  3:29 ` [PATCH userspace 2/2] checkpolicy: warn on negative-only type sets Thiébaud Weksteen
2026-11-30 15:46 ` [PATCH userspace 0/2] Fix and " Stephen Smalley
2026-11-30 22:29   ` Thiébaud Weksteen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.