From: James Carter <jwcart2@gmail.com>
To: selinux@vger.kernel.org
Cc: stephen.smalley.work@gmail.com, James Carter <jwcart2@gmail.com>
Subject: [PATCH 2/2] libsepol: Remove junk permissions before optimzing the policy
Date: Tue, 25 Aug 2026 15:41:11 -0400 [thread overview]
Message-ID: <20260825194111.75602-2-jwcart2@gmail.com> (raw)
In-Reply-To: <20260825194111.75602-1-jwcart2@gmail.com>
In older policy compilers, a wildcard or complement would cause
all the bits higher than those corresponding to the class's declared
permissions to be set. These junk bits must be removed before
optimization is done to avoid a situation where the only bits left
for an access vector are one or more of these junk bits. If a policy
does not have at least one actual permission bit set, it will fail
validation.
Signed-off-by: James Carter <jwcart2@gmail.com>
---
libsepol/src/optimize.c | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/libsepol/src/optimize.c b/libsepol/src/optimize.c
index eeffee7d..1efd3832 100644
--- a/libsepol/src/optimize.c
+++ b/libsepol/src/optimize.c
@@ -446,6 +446,32 @@ static void optimize_cond_avtab(policydb_t *p, const struct type_vec *type_map)
}
}
+/*
+ * Older policy compilers could set bits higher than any of the permissions
+ * in the class. These junk bits must be removed before optimization to keep
+ * redundant rules from being kept because of junk bits and to prevent the
+ * failure of policy validation because no actual permission bit is set.
+ */
+static int remove_junk_permissions(avtab_key_t *k, avtab_datum_t *d, void *args)
+{
+ const policydb_t *p = args;
+ const class_datum_t *tclass;
+ uint32_t mask;
+
+ if (!(k->specified & AVTAB_AV))
+ return 0;
+
+ tclass = p->class_val_to_struct[k->target_class - 1];
+ mask = PERMISSION_MASK(tclass->permissions.nprim);
+
+ if ((k->specified & ~AVTAB_ENABLED) == AVTAB_AUDITDENY)
+ d->data |= ~mask;
+ else
+ d->data &= mask;
+
+ return 0;
+}
+
int policydb_optimize(policydb_t *p)
{
struct type_vec *type_map;
@@ -453,6 +479,9 @@ int policydb_optimize(policydb_t *p)
if (p->policy_type != POLICY_KERN)
return -1;
+ avtab_map(&p->te_avtab, remove_junk_permissions, p);
+ avtab_map(&p->te_cond_avtab, remove_junk_permissions, p);
+
type_map = build_type_map(p);
if (!type_map)
return -1;
--
2.55.0
next prev parent reply other threads:[~2026-08-25 19:41 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-25 19:41 [PATCH 1/2] libsepol: Move the PERMISSION_MASK macro to private.h James Carter
2026-08-25 19:41 ` James Carter [this message]
2026-08-25 20:14 ` [PATCH 2/2] libsepol: Remove junk permissions before optimzing the policy Stephen Smalley
2026-08-25 20:13 ` [PATCH 1/2] libsepol: Move the PERMISSION_MASK macro to private.h Stephen Smalley
2026-08-26 12:25 ` Stephen Smalley
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=20260825194111.75602-2-jwcart2@gmail.com \
--to=jwcart2@gmail.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.