Skip to content

Fix and warn on negative-only type sets

Thiébaud Weksteen selinux 1 version New

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest2 patches 0 repliesNew

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/2libsepol: align module_to_cil type/role set handling with expand.clibsepol/src/module_to_cil.c libsepol/tests/libsepol-tests.c libsepol/tests/policies/test-module-to-cil/typeset-comp.conf +3 more+383 -240 replies
2/2checkpolicy: warn on negative-only type setscheckpolicy/Makefile checkpolicy/policy_define.c checkpolicy/tests/test_negative_typeset.sh+270 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH userspace 0/2] 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: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
Signature · 1 line
2.56.1.839.g22a8290db9-goog