Fix and warn on negative-only type sets
cover Fix and warn on negative-only type sets
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:TYPE1Before 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 $?
0Converting 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
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH 1/2 libsepol: align module_to_cil type/role set handling with expand.c
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.
--- 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
libsepol/src/module_to_cil.c +45 -24
@@ -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 (")" ); }
libsepol/tests/libsepol-tests.c +2 -0
@@ -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)
libsepol/tests/policies/test-module-to-cil/typeset-comp.conf new file +24 -0
@@ -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)
libsepol/tests/policies/test-module-to-cil/typeset.conf new file +34 -0
@@ -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)
libsepol/tests/test-module-to-cil.c new file +268 -0
268 lines not on this page · raw message
libsepol/tests/test-module-to-cil.h new file +10 -0
@@ -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
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH 2/2 checkpolicy: warn on negative-only type sets
In reply to Thiébaud WeksteenPolicy 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).
--- 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
checkpolicy/Makefile +1 -0
@@ -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)
checkpolicy/policy_define.c +24 -0
@@ -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))
checkpolicy/tests/test_negative_typeset.sh new file +245 -0
245 lines not on this page · raw message
Signature · 1 line
2.56.1.839.g22a8290db9-goog