* [PATCH v3] selinux: validate primary symbol count when loading policy
@ 2026-12-04 5:06 Kyuwon Cho
2026-12-04 5:21 ` sashiko-bot
2026-12-04 13:48 ` Stephen Smalley
0 siblings, 2 replies; 5+ messages in thread
From: Kyuwon Cho @ 2026-12-04 5:06 UTC (permalink / raw)
To: Paul Moore, Stephen Smalley
Cc: Ondrej Mosnáček, sashiko-reviews, selinux
policydb_read() trusts each symbol table's primary count independently
of its serialized entries. A malformed policy can therefore request
oversized policy index arrays and trigger allocator warnings.
Bound each count by the remaining policy size where every primary value
requires serialized data. Exclude the type table on policy versions
before AVTAB because type attributes contribute to its primary count but
are not serialized in those versions.
The input-size bound still permits large arrays when the policy contains
padding. Allocate all policy index arrays with kvzalloc and
__GFP_NOWARN, allowing an unreasonable request to fail quietly without
requiring physically contiguous memory.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Kyuwon Cho <kyuwon.cho@skku.edu>
---
Changes in v3:
- Preserve valid v15-v19 policies whose type attributes are counted but not
serialized (Sashiko review).
- Use non-contiguous, warning-suppressed allocations for policy index arrays
that a padded image can still make large (Sashiko review).
- Rebase onto selinux next.
Link to v2: https://lore.kernel.org/all/179632334562.3849443.7476539692307740906@skku.edu/
Link to 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/conditional.c | 16 +++++++------
security/selinux/ss/policydb.c | 38 +++++++++++++++++++++----------
2 files changed, 35 insertions(+), 19 deletions(-)
diff --git a/security/selinux/ss/conditional.c b/security/selinux/ss/conditional.c
index 2956dd7edea7..23928eb63120 100644
--- a/security/selinux/ss/conditional.c
+++ b/security/selinux/ss/conditional.c
@@ -158,16 +158,17 @@ static void cond_list_destroy(struct policydb *p)
void cond_policydb_destroy(struct policydb *p)
{
- kfree(p->bool_val_to_struct);
+ kvfree(p->bool_val_to_struct);
avtab_destroy(&p->te_cond_avtab);
cond_list_destroy(p);
}
int cond_init_bool_indexes(struct policydb *p)
{
- kfree(p->bool_val_to_struct);
- p->bool_val_to_struct = kzalloc_objs(*p->bool_val_to_struct,
- p->p_bools.nprim);
+ kvfree(p->bool_val_to_struct);
+ p->bool_val_to_struct = kvzalloc_objs(*p->bool_val_to_struct,
+ p->p_bools.nprim,
+ GFP_KERNEL | __GFP_NOWARN);
if (!p->bool_val_to_struct)
return -ENOMEM;
@@ -722,15 +723,16 @@ static int duplicate_policydb_bools(struct policydb *newdb,
struct cond_bool_datum **cond_bool_array;
int rc;
- cond_bool_array = kzalloc_objs(*orig->bool_val_to_struct,
- orig->p_bools.nprim);
+ cond_bool_array = kvzalloc_objs(*orig->bool_val_to_struct,
+ orig->p_bools.nprim,
+ GFP_KERNEL | __GFP_NOWARN);
if (!cond_bool_array)
return -ENOMEM;
rc = hashtab_duplicate(&newdb->p_bools.table, &orig->p_bools.table,
cond_bools_copy, cond_bools_destroy, NULL);
if (rc) {
- kfree(cond_bool_array);
+ kvfree(cond_bool_array);
return -ENOMEM;
}
diff --git a/security/selinux/ss/policydb.c b/security/selinux/ss/policydb.c
index fe0c0792583f..54d2a227aa7a 100644
--- a/security/selinux/ss/policydb.c
+++ b/security/selinux/ss/policydb.c
@@ -837,23 +837,27 @@ static int policydb_index(struct policydb *p)
symtab_hash_eval(p->symtab);
- p->class_val_to_struct = kzalloc_objs(*p->class_val_to_struct,
- p->p_classes.nprim);
+ p->class_val_to_struct = kvzalloc_objs(*p->class_val_to_struct,
+ p->p_classes.nprim,
+ GFP_KERNEL | __GFP_NOWARN);
if (!p->class_val_to_struct)
return -ENOMEM;
- p->role_val_to_struct = kzalloc_objs(*p->role_val_to_struct,
- p->p_roles.nprim);
+ p->role_val_to_struct = kvzalloc_objs(*p->role_val_to_struct,
+ p->p_roles.nprim,
+ GFP_KERNEL | __GFP_NOWARN);
if (!p->role_val_to_struct)
return -ENOMEM;
- p->user_val_to_struct = kzalloc_objs(*p->user_val_to_struct,
- p->p_users.nprim);
+ p->user_val_to_struct = kvzalloc_objs(*p->user_val_to_struct,
+ p->p_users.nprim,
+ GFP_KERNEL | __GFP_NOWARN);
if (!p->user_val_to_struct)
return -ENOMEM;
p->type_val_to_struct = kvzalloc_objs(*p->type_val_to_struct,
- p->p_types.nprim);
+ p->p_types.nprim,
+ GFP_KERNEL | __GFP_NOWARN);
if (!p->type_val_to_struct)
return -ENOMEM;
@@ -863,7 +867,8 @@ static int policydb_index(struct policydb *p)
for (i = 0; i < SYM_NUM; i++) {
p->sym_val_to_name[i] = kvcalloc(p->symtab[i].nprim,
- sizeof(char *), GFP_KERNEL);
+ sizeof(char *),
+ GFP_KERNEL | __GFP_NOWARN);
if (!p->sym_val_to_name[i])
return -ENOMEM;
@@ -944,9 +949,9 @@ void policydb_destroy(struct policydb *p)
for (i = 0; i < SYM_NUM; i++)
kvfree(p->sym_val_to_name[i]);
- kfree(p->class_val_to_struct);
- kfree(p->role_val_to_struct);
- kfree(p->user_val_to_struct);
+ kvfree(p->class_val_to_struct);
+ kvfree(p->role_val_to_struct);
+ kvfree(p->user_val_to_struct);
kvfree(p->type_val_to_struct);
avtab_destroy(&p->te_avtab);
@@ -3015,6 +3020,14 @@ 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 (i != SYM_TYPES || p->policyvers >= POLICYDB_VERSION_AVTAB) {
+ 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
@@ -3184,7 +3197,8 @@ int policydb_read(struct policydb *p, struct policy_file *fp)
rc = -ENOMEM;
p->type_attr_map_array = kvzalloc_objs(*p->type_attr_map_array,
- p->p_types.nprim);
+ p->p_types.nprim,
+ GFP_KERNEL | __GFP_NOWARN);
if (!p->type_attr_map_array)
goto bad;
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v3] selinux: validate primary symbol count when loading policy
2026-12-04 5:06 [PATCH v3] selinux: validate primary symbol count when loading policy Kyuwon Cho
@ 2026-12-04 5:21 ` sashiko-bot
2026-12-04 13:48 ` Stephen Smalley
1 sibling, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-04 5:21 UTC (permalink / raw)
Cc: selinux
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] selinux: oversized p_classes.nprim warns in
security_get_classes()
--
commit d4902f40a6a7734f5f4e7db2f7f69f69e560e87e
Author: Kyuwon Cho <kyuwon.cho@skku.edu>
selinux: validate primary symbol count when loading policy
This commit bounds each symbol table's primary count by the remaining
policy size in policydb_read() and switches policy index array
allocations to kvzalloc/kvcalloc with __GFP_NOWARN.
> diff --git a/security/selinux/ss/policydb.c b/security/selinux/ss/policydb.c
> index b75c977c6673d..92dbb43fafbae 100644
> --- a/security/selinux/ss/policydb.c
> +++ b/security/selinux/ss/policydb.c
[ ... ]
> @@ -2904,6 +2909,14 @@ 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 (i != SYM_TYPES || p->policyvers >= POLICYDB_VERSION_AVTAB) {
> + 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;
> + }
> + }
[Severity: Medium]
Should SYM_CLASSES also be capped at U16_MAX here, or does an oversized
p_classes.nprim still trigger an allocator warning in
security_get_classes()?
Now that policydb_index() uses kvzalloc_objs() for class_val_to_struct,
a ~2.1 MiB padded policy with p_classes.nprim > 524288 passes
size_check() and succeeds in security_load_policy().
sel_write_load() then calls sel_make_policy_nodes() ->
sel_make_classes() -> security_get_classes():
security/selinux/ss/services.c:security_get_classes() {
...
*nclasses = policydb->p_classes.nprim;
*classes = kcalloc(*nclasses, sizeof(**classes), GFP_ATOMIC);
if (!*classes)
goto out;
...
}
On 64-bit 4 KiB-page kernels, that kcalloc() exceeds KMALLOC_MAX_SIZE
without __GFP_NOWARN and triggers the order > MAX_PAGE_ORDER warning in
alloc_order_allowed(). Since class_read() already rejects class values
above U16_MAX, capping SYM_CLASSES nprim at U16_MAX would avoid this.
--
Sashiko AI review · https://sashiko.dev/#/patchset/179633283854.20472.2166041983398323812@skku.edu?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v3] selinux: validate primary symbol count when loading policy
2026-12-04 5:06 [PATCH v3] selinux: validate primary symbol count when loading policy Kyuwon Cho
2026-12-04 5:21 ` sashiko-bot
@ 2026-12-04 13:48 ` Stephen Smalley
2026-12-04 13:53 ` Stephen Smalley
2026-12-04 18:19 ` Stephen Smalley
1 sibling, 2 replies; 5+ messages in thread
From: Stephen Smalley @ 2026-12-04 13:48 UTC (permalink / raw)
To: Kyuwon Cho
Cc: Paul Moore, Ondrej Mosnáček, sashiko-reviews, selinux
On Fri, Dec 4, 2026 at 12:06 AM Kyuwon Cho <kyuwon.cho@skku.edu> wrote:
>
> policydb_read() trusts each symbol table's primary count independently
> of its serialized entries. A malformed policy can therefore request
> oversized policy index arrays and trigger allocator warnings.
>
> Bound each count by the remaining policy size where every primary value
> requires serialized data. Exclude the type table on policy versions
> before AVTAB because type attributes contribute to its primary count but
> are not serialized in those versions.
>
> The input-size bound still permits large arrays when the policy contains
> padding. Allocate all policy index arrays with kvzalloc and
> __GFP_NOWARN, allowing an unreasonable request to fail quietly without
> requiring physically contiguous memory.
>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Signed-off-by: Kyuwon Cho <kyuwon.cho@skku.edu>
> ---
> diff --git a/security/selinux/ss/policydb.c b/security/selinux/ss/policydb.c
> index fe0c0792583f..54d2a227aa7a 100644
> --- a/security/selinux/ss/policydb.c
> +++ b/security/selinux/ss/policydb.c
> @@ -3015,6 +3020,14 @@ 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 (i != SYM_TYPES || p->policyvers >= POLICYDB_VERSION_AVTAB) {
This is gated on the wrong policyvers; kernel policy formats do not
include attributes in the type table until POLICYDB_VERSION_BOUNDARY
(24).
Please fix here and in the commit message.
Also note that role attributes are counted in nprim but never
serialized in kernel policies at any version; the check nonetheless
passes because the bound is deliberately loose.
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v3] selinux: validate primary symbol count when loading policy
2026-12-04 13:48 ` Stephen Smalley
@ 2026-12-04 13:53 ` Stephen Smalley
2026-12-04 18:19 ` Stephen Smalley
1 sibling, 0 replies; 5+ messages in thread
From: Stephen Smalley @ 2026-12-04 13:53 UTC (permalink / raw)
To: Kyuwon Cho
Cc: Paul Moore, Ondrej Mosnáček, sashiko-reviews, selinux
On Fri, Dec 4, 2026 at 8:48 AM Stephen Smalley
<stephen.smalley.work@gmail.com> wrote:
>
> On Fri, Dec 4, 2026 at 12:06 AM Kyuwon Cho <kyuwon.cho@skku.edu> wrote:
> >
> > policydb_read() trusts each symbol table's primary count independently
> > of its serialized entries. A malformed policy can therefore request
> > oversized policy index arrays and trigger allocator warnings.
> >
> > Bound each count by the remaining policy size where every primary value
> > requires serialized data. Exclude the type table on policy versions
> > before AVTAB because type attributes contribute to its primary count but
> > are not serialized in those versions.
> >
> > The input-size bound still permits large arrays when the policy contains
> > padding. Allocate all policy index arrays with kvzalloc and
> > __GFP_NOWARN, allowing an unreasonable request to fail quietly without
> > requiring physically contiguous memory.
> >
> > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> > Signed-off-by: Kyuwon Cho <kyuwon.cho@skku.edu>
> > ---
>
> > diff --git a/security/selinux/ss/policydb.c b/security/selinux/ss/policydb.c
> > index fe0c0792583f..54d2a227aa7a 100644
> > --- a/security/selinux/ss/policydb.c
> > +++ b/security/selinux/ss/policydb.c
> > @@ -3015,6 +3020,14 @@ 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 (i != SYM_TYPES || p->policyvers >= POLICYDB_VERSION_AVTAB) {
>
> This is gated on the wrong policyvers; kernel policy formats do not
> include attributes in the type table until POLICYDB_VERSION_BOUNDARY
> (24).
> Please fix here and in the commit message.
> Also note that role attributes are counted in nprim but never
> serialized in kernel policies at any version; the check nonetheless
> passes because the bound is deliberately loose.
See also sashiko's review at
https://lore.kernel.org/selinux/CAEjxPJ6SKyQOdVFPsDYE83aV3w=zEJnyfOxpXZ4OttAwS66Phw@mail.gmail.com/T/#m9a17bcf2eee55695f44fc4b2cea7fc02e993be25
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v3] selinux: validate primary symbol count when loading policy
2026-12-04 13:48 ` Stephen Smalley
2026-12-04 13:53 ` Stephen Smalley
@ 2026-12-04 18:19 ` Stephen Smalley
1 sibling, 0 replies; 5+ messages in thread
From: Stephen Smalley @ 2026-12-04 18:19 UTC (permalink / raw)
To: Kyuwon Cho
Cc: Paul Moore, Ondrej Mosnáček, sashiko-reviews, selinux
On Fri, Dec 4, 2026 at 8:48 AM Stephen Smalley
<stephen.smalley.work@gmail.com> wrote:
>
> On Fri, Dec 4, 2026 at 12:06 AM Kyuwon Cho <kyuwon.cho@skku.edu> wrote:
> >
> > policydb_read() trusts each symbol table's primary count independently
> > of its serialized entries. A malformed policy can therefore request
> > oversized policy index arrays and trigger allocator warnings.
> >
> > Bound each count by the remaining policy size where every primary value
> > requires serialized data. Exclude the type table on policy versions
> > before AVTAB because type attributes contribute to its primary count but
> > are not serialized in those versions.
> >
> > The input-size bound still permits large arrays when the policy contains
> > padding. Allocate all policy index arrays with kvzalloc and
> > __GFP_NOWARN, allowing an unreasonable request to fail quietly without
> > requiring physically contiguous memory.
> >
> > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> > Signed-off-by: Kyuwon Cho <kyuwon.cho@skku.edu>
> > ---
>
> > diff --git a/security/selinux/ss/policydb.c b/security/selinux/ss/policydb.c
> > index fe0c0792583f..54d2a227aa7a 100644
> > --- a/security/selinux/ss/policydb.c
> > +++ b/security/selinux/ss/policydb.c
> > @@ -3015,6 +3020,14 @@ 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 (i != SYM_TYPES || p->policyvers >= POLICYDB_VERSION_AVTAB) {
>
> This is gated on the wrong policyvers; kernel policy formats do not
> include attributes in the type table until POLICYDB_VERSION_BOUNDARY
> (24).
> Please fix here and in the commit message.
> Also note that role attributes are counted in nprim but never
> serialized in kernel policies at any version; the check nonetheless
> passes because the bound is deliberately loose.
Correction to my earlier reply: please keep POLICYDB_VERSION_AVTAB as
is; my error.
Also, for roles, you need to modify the check: consider a small v35
policy with two concrete roles and 1024 attribute_role declarations as
a test case.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-04 18:19 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 5:06 [PATCH v3] selinux: validate primary symbol count when loading policy Kyuwon Cho
2026-12-04 5:21 ` sashiko-bot
2026-12-04 13:48 ` Stephen Smalley
2026-12-04 13:53 ` Stephen Smalley
2026-12-04 18:19 ` 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.