* [PATCH v4] selinux: validate primary symbol count when loading policy
@ 2026-12-04 19:50 Kyuwon Cho
2026-12-04 20:07 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Kyuwon Cho @ 2026-12-04 19:50 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 the format guarantees
at least a u32 of serialized data for every primary value. Exempt the type
table before POLICYDB_VERSION_AVTAB because type attributes contribute to
nprim but only version 20 and later serialize a type-attribute bitmap for
every value. Exempt the role table at every version because role attributes
contribute to nprim but are never serialized in kernel policies.
Since class values are represented by u16, also reject a class primary
count above U16_MAX before it reaches the later GFP_ATOMIC class-array
allocation.
The input-size bound can still permit 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 v4:
- Keep the type-table gate at POLICYDB_VERSION_AVTAB per Stephen
Smalley's correction:
https://lore.kernel.org/all/CAEjxPJ7m4bFz3rFzixntHU2ZXMLApU3rYrUbPebFu9jQc5sR7w@mail.gmail.com/
- Exclude role counts from the size bound after Stephen pointed out a
valid small v35 policy with two concrete roles and 1024 role attributes.
- Cap the primary class count at U16_MAX. This addresses the Sashiko AI
review Stephen pointed to:
https://lore.kernel.org/all/sashiko-outbox-249131@kernel.org/
v3: https://lore.kernel.org/all/179633283854.20472.2166041983398323812@skku.edu/
v2: https://lore.kernel.org/all/179632334562.3849443.7476539692307740906@skku.edu/
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.
The patch was tested in KVM with valid v15, v23, and v35 policies, the
v35 role-attribute case, malformed type-count policies, and an oversized
class-count policy.
A reproducer is available on request.
security/selinux/ss/conditional.c | 16 ++++++-----
security/selinux/ss/policydb.c | 45 ++++++++++++++++++++++---------
2 files changed, 42 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..ecbd85e58521 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,21 @@ 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_CLASSES && nprim > U16_MAX) {
+ pr_err("SELinux: class table has an invalid primary symbol count %u\n",
+ nprim);
+ goto bad;
+ }
+ if (i != SYM_ROLES &&
+ (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 +3204,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] 2+ messages in thread
* Re: [PATCH v4] selinux: validate primary symbol count when loading policy
2026-12-04 19:50 [PATCH v4] selinux: validate primary symbol count when loading policy Kyuwon Cho
@ 2026-12-04 20:07 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-04 20:07 UTC (permalink / raw)
Cc: selinux
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] selinux: policydb_read() returns 0 on oversized SYM_CLASSES
nprim
--
commit f5b341ba25ff924a6cef1b9b61666631c03a9a2d
Author: Kyuwon Cho <kyuwon.cho@skku.edu>
selinux: validate primary symbol count when loading policy
This commit validates primary symbol counts against the remaining policy
size (and U16_MAX for classes) in policydb_read(), and switches policy
index array allocations to kvzalloc with __GFP_NOWARN.
> diff --git a/security/selinux/ss/policydb.c b/security/selinux/ss/policydb.c
> index b75c977c6673d..633508855e165 100644
> --- a/security/selinux/ss/policydb.c
> +++ b/security/selinux/ss/policydb.c
[ ... ]
> @@ -2904,6 +2909,21 @@ 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_CLASSES && nprim > U16_MAX) {
> + pr_err("SELinux: class table has an invalid primary symbol count %u\n",
> + nprim);
> + goto bad;
> + }
[Severity: High]
Does this jump to bad return 0 because rc is still 0 from next_entry()?
At the bad label, policydb_read() calls policydb_destroy(p) and returns rc.
Seeing rc == 0, security_load_policy() continues using the destroyed
policydb instead of aborting the load.
If selinux_set_mapping() then fails (for example with REJECT_UNKNOWN set),
security_load_policy() calls policydb_destroy() a second time. Because
hashtab_destroy() clears h->htable without resetting h->size, hashtab_map()
triggers a NULL pointer dereference on SYM_COMMONS.
Setting rc = -EINVAL before goto bad fixes this.
--
Sashiko AI review · https://sashiko.dev/#/patchset/179641334208.1696188.13639506462697032325@skku.edu?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-04 20:07 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 19:50 [PATCH v4] selinux: validate primary symbol count when loading policy Kyuwon Cho
2026-12-04 20:07 ` 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.