* [PATCH] selinux: validate primary symbol count when loading policy
@ 2026-12-03 16:41 Kyuwon Cho
2026-12-03 17:28 ` sashiko-bot
2026-12-03 17:37 ` Stephen Smalley
0 siblings, 2 replies; 3+ messages in thread
From: Kyuwon Cho @ 2026-12-03 16:41 UTC (permalink / raw)
To: Paul Moore, Stephen Smalley; +Cc: Ondrej Mosnáček, selinux
A policy symbol table can claim more primary names than its total number
of entries. policydb_read() accepts the inconsistent counts and
policydb_index() then allocates its value arrays from the unbounded primary
count, triggering the allocator warning for a malformed policy.
Every primary name is a symbol table entry, while aliases only increase
the entry count. Reject a primary count larger than the entry count.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Kyuwon Cho <kyuwon.cho@skku.edu>
---
The issue was found by an LLM-guided test generator that we are
developing as a research prototype.
The reproducer loads a malformed binary policy whose first symbol table
claims more primary names than total entries. Loading a policy through
selinuxfs requires root in the initial namespace in the tested setup.
The decoded trace is:
WARNING in __kvmalloc_node_noprof (mm/slub.c:7151)
policydb_read (security/selinux/ss/policydb.c:855
security/selinux/ss/policydb.c:3045)
security_load_policy (security/selinux/ss/services.c:2328)
sel_write_load (security/selinux/selinuxfs.c:557)
vfs_write (fs/read_write.c:665)
With this patch, the malformed policy is rejected with EINVAL and the
reproducer completes without the allocator warning on fe6ff8d5d675. The
patch also builds on the current SELinux tree at 85cb16a3dcc7.
security/selinux/ss/policydb.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/security/selinux/ss/policydb.c b/security/selinux/ss/policydb.c
index fe0c0792583f..40b0f7a1f7aa 100644
--- a/security/selinux/ss/policydb.c
+++ b/security/selinux/ss/policydb.c
@@ -3015,6 +3015,12 @@ int policydb_read(struct policydb *p, struct policy_file *fp)
goto bad;
nprim = le32_to_cpu(buf[0]);
nel = le32_to_cpu(buf[1]);
+ if (nprim > nel) {
+ pr_err("SELinux: symbol table %u has %u primary names but only %u entries\n",
+ i, nprim, nel);
+ rc = -EINVAL;
+ goto bad;
+ }
/* every read_f() implementation reads at least 128 bytes
* for any valid entry
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] selinux: validate primary symbol count when loading policy
2026-12-03 16:41 [PATCH] selinux: validate primary symbol count when loading policy Kyuwon Cho
@ 2026-12-03 17:28 ` sashiko-bot
2026-12-03 17:37 ` Stephen Smalley
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-12-03 17:28 UTC (permalink / raw)
Cc: selinux
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] selinux: policydb_read() rejects valid pre-v24 policies where
nprim > nel
--
commit 7c587f0526cf279c4d9f66db4544173a329147fa
Author: Kyuwon Cho <kyuwon.cho@skku.edu>
selinux: validate primary symbol count when loading policy
This commit adds a check in policydb_read() to reject symbol tables whose
primary symbol count (nprim) exceeds the entry count (nel) with -EINVAL.
> diff --git a/security/selinux/ss/policydb.c b/security/selinux/ss/policydb.c
> index b75c977c6673d..5eb17378483b6 100644
> --- a/security/selinux/ss/policydb.c
> +++ b/security/selinux/ss/policydb.c
> @@ -2904,6 +2904,12 @@ int policydb_read(struct policydb *p, struct policy_file *fp)
> goto bad;
> nprim = le32_to_cpu(buf[0]);
> nel = le32_to_cpu(buf[1]);
> + if (nprim > nel) {
> + pr_err("SELinux: symbol table %u has %u primary names but only %u entries\n",
> + i, nprim, nel);
> + rc = -EINVAL;
> + goto bad;
> + }
[Severity: Medium]
Does rejecting nprim > nel break loading valid policies older than
POLICYDB_VERSION_BOUNDARY (versions 15 through 23)?
Before POLICYDB_VERSION_BOUNDARY, type_read() has no attribute flag, so
valid policies omit type attributes from serialized SYM_TYPES entries
(nel) while still counting their IDs in p->p_types.nprim.
Any pre-v24 policy with more type attributes than type aliases therefore
has nprim > nel and will now fail to load with -EINVAL.
Top-level symbol tables can also be sparse, and roles_init()
pre-populates OBJECT_R into SYM_ROLES, so a policy omitting object_r
would hit the same rejection.
Guarding this check for pre-POLICYDB_VERSION_BOUNDARY SYM_TYPES (or
bounding nprim against the remaining policy image size) would avoid
rejecting valid legacy policies.
--
Sashiko AI review · https://sashiko.dev/#/patchset/179621430413.2669974.16944159499886043558@skku.edu?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] selinux: validate primary symbol count when loading policy
2026-12-03 16:41 [PATCH] selinux: validate primary symbol count when loading policy Kyuwon Cho
2026-12-03 17:28 ` sashiko-bot
@ 2026-12-03 17:37 ` Stephen Smalley
1 sibling, 0 replies; 3+ messages in thread
From: Stephen Smalley @ 2026-12-03 17:37 UTC (permalink / raw)
To: Kyuwon Cho; +Cc: Paul Moore, Ondrej Mosnáček, selinux
On Thu, Dec 3, 2026 at 11:41 AM Kyuwon Cho <kyuwon.cho@skku.edu> wrote:
>
> A policy symbol table can claim more primary names than its total number
> of entries. policydb_read() accepts the inconsistent counts and
> policydb_index() then allocates its value arrays from the unbounded primary
> count, triggering the allocator warning for a malformed policy.
>
> Every primary name is a symbol table entry, while aliases only increase
> the entry count. Reject a primary count larger than the entry count.
>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Signed-off-by: Kyuwon Cho <kyuwon.cho@skku.edu>
> ---
> The issue was found by an LLM-guided test generator that we are
> developing as a research prototype.
>
> The reproducer loads a malformed binary policy whose first symbol table
> claims more primary names than total entries. Loading a policy through
> selinuxfs requires root in the initial namespace in the tested setup.
>
> The decoded trace is:
>
> WARNING in __kvmalloc_node_noprof (mm/slub.c:7151)
> policydb_read (security/selinux/ss/policydb.c:855
> security/selinux/ss/policydb.c:3045)
> security_load_policy (security/selinux/ss/services.c:2328)
> sel_write_load (security/selinux/selinuxfs.c:557)
> vfs_write (fs/read_write.c:665)
>
> With this patch, the malformed policy is rejected with EINVAL and the
> reproducer completes without the allocator warning on fe6ff8d5d675. The
> patch also builds on the current SELinux tree at 85cb16a3dcc7.
See the issues noted by sashiko in
https://lore.kernel.org/selinux/sashiko-outbox-248262@kernel.org/T/#m18b9ad5163dde72fb65995319bedffec0f301059
Also be sure that you can successfully load valid policies after your change.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-03 17:37 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 16:41 [PATCH] selinux: validate primary symbol count when loading policy Kyuwon Cho
2026-12-03 17:28 ` sashiko-bot
2026-12-03 17:37 ` Stephen Smalley
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.