* [PATCH v2] selinux: validate primary symbol count when loading policy
@ 2026-12-03 19:51 Kyuwon Cho
2026-12-03 20:11 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Kyuwon Cho @ 2026-12-03 19:51 UTC (permalink / raw)
To: Paul Moore, Stephen Smalley
Cc: Ondrej Mosnáček, sashiko-reviews, selinux
policydb_index() sizes multiple arrays from each symbol table primary
count. A malformed policy can provide a count large enough to trigger an
allocator warning while containing almost no symbol data.
The primary count can legitimately exceed the serialized entry count,
particularly for type attributes in policy versions before 24. Bound the
count by the remaining policy image instead of assuming a dense symbol
table. This rejects the malformed input without excluding valid legacy
policies.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Kyuwon Cho <kyuwon.cho@skku.edu>
---
Changes in v2:
- Bound nprim by the remaining policy image instead of comparing it with
nel, as suggested by the Sashiko review.
- Loaded versions 15, 20, 23, 24, 30, and 35 generated by both checkpolicy
and secilc, as requested by Stephen Smalley.
v1: https://lore.kernel.org/all/179621430413.2669974.16944159499886043558@skku.edu/
The issue was found by an LLM-guided test generator that we are
developing as a research prototype.
A reproducer is available on request.
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..9e96d8e69474 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]);
+ rc = size_check(sizeof(u32), nprim, fp);
+ if (rc) {
+ pr_err("SELinux: symbol table %u has an invalid primary symbol count %u\n",
+ i, nprim);
+ goto bad;
+ }
/* every read_f() implementation reads at least 128 bytes
* for any valid entry
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH v2] selinux: validate primary symbol count when loading policy
2026-12-03 19:51 [PATCH v2] selinux: validate primary symbol count when loading policy Kyuwon Cho
@ 2026-12-03 20:11 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-03 20:11 UTC (permalink / raw)
Cc: selinux
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] selinux: nprim bound in policydb_read() still allows
allocator warnings
- [Medium] selinux: nprim size_check() in policydb_read() rejects valid
v15-19 policies
--
commit 73282705b94805b2c857b96c5fb8c6592be5a8da
Author: Kyuwon Cho <kyuwon.cho@skku.edu>
selinux: validate primary symbol count when loading policy
This commit adds a size_check() in policydb_read() to bound each symbol
table's primary symbol count against the remaining policy image size.
> diff --git a/security/selinux/ss/policydb.c b/security/selinux/ss/policydb.c
> index b75c977c6673d..27437fc1e4797 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]);
> + rc = size_check(sizeof(u32), nprim, fp);
> + if (rc) {
> + pr_err("SELinux: symbol table %u has an invalid primary symbol count %u\n",
> + i, nprim);
> + goto bad;
> + }
>
> /* every read_f() implementation reads at least 128 bytes
> * for any valid entry
[Severity: Medium]
Can a malformed policy with zero symbol entries still trigger allocator
warnings with this bound?
sel_write_load() accepts multi-megabyte images via vmalloc(), so a ~2.1 MiB
padded image with nel == 0 passes this check with nprim = 524289. Both
policydb_index() (class/role/user_val_to_struct) and
cond_init_bool_indexes() (bool_val_to_struct) use contiguous kzalloc_objs().
On 64-bit, 524289 pointers exceeds 4 MiB and hits the MAX_PAGE_ORDER warning
in alloc_order_allowed() because kzalloc_objs() omits __GFP_NOWARN. The
kvzalloc_objs()/kvcalloc() calls for type_val_to_struct, sym_val_to_name,
and type_attr_map_array also omit __GFP_NOWARN and still warn above INT_MAX.
Switching those kzalloc_objs() arrays to kvzalloc_objs()/kvfree() and adding
__GFP_NOWARN to the policy index allocations would prevent these warnings.
[Severity: Medium]
Will this check falsely reject valid version 15-19 policies when SYM_TYPES
has unserialized type attributes?
Below POLICYDB_VERSION_BOUNDARY (24), type attributes increment
p->p_types.nprim without being serialized in SYM_TYPES. For versions 20-23,
policydb_read() later reads one ebitmap per type into type_attr_map_array,
so the remaining image still holds at least 12 bytes per nprim.
For versions 15-19 (below POLICYDB_VERSION_AVTAB), policydb_read() skips
reading type_attr_map_array from fp, so those attributes occupy zero bytes
anywhere in the remaining image. A compact v15-19 policy where
sizeof(u32) * p->p_types.nprim exceeds fp->len will fail to load with -EINVAL.
--
Sashiko AI review · https://sashiko.dev/#/patchset/179632334562.3849443.7476539692307740906@skku.edu?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-03 20:11 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 19:51 [PATCH v2] selinux: validate primary symbol count when loading policy Kyuwon Cho
2026-12-03 20:11 ` sashiko-bot
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.