From: "Thiébaud Weksteen" <tweek@google.com>
To: selinux@vger.kernel.org
Cc: "Thiébaud Weksteen" <tweek@google.com>,
"Stephen Smalley" <stephen.smalley.work@gmail.com>,
"James Carter" <jwcart2@gmail.com>,
"Petr Lautrbach" <lautrbach@redhat.com>,
"Christian Göttsche" <cgzones@googlemail.com>,
"Inseob Kim" <inseob@google.com>,
"Jeff Vander Stoep" <jeffv@google.com>
Subject: [PATCH userspace 0/2] Fix and warn on negative-only type sets
Date: Thu, 26 Nov 2026 14:29:21 +1100 [thread overview]
Message-ID: <20261126032924.1868720-1-tweek@google.com> (raw)
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
next reply other threads:[~2026-11-26 3:29 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 3:29 Thiébaud Weksteen [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261126032924.1868720-1-tweek@google.com \
--to=tweek@google.com \
--cc=cgzones@googlemail.com \
--cc=inseob@google.com \
--cc=jeffv@google.com \
--cc=jwcart2@gmail.com \
--cc=lautrbach@redhat.com \
--cc=selinux@vger.kernel.org \
--cc=stephen.smalley.work@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.