All of lore.kernel.org
 help / color / mirror / Atom feed
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 2/2] checkpolicy: warn on negative-only type sets
Date: Thu, 26 Nov 2026 14:29:23 +1100	[thread overview]
Message-ID: <20261126032924.1868720-3-tweek@google.com> (raw)
In-Reply-To: <20261126032924.1868720-1-tweek@google.com>

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


  parent 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 [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 [this message]
2026-11-30 15:46 ` [PATCH userspace 0/2] Fix and warn on negative-only type sets 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-3-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.