All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v5] selinux: validate primary symbol count when loading policy
@ 2026-12-06  4:47 Kyuwon Cho
  2026-12-06  5:01 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Kyuwon Cho @ 2026-12-06  4:47 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 v5:
- Set rc to -EINVAL before rejecting an oversized class count. This
  addresses the Sashiko AI review:
  https://lore.kernel.org/all/sashiko-outbox-250600@kernel.org/

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/

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.

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.

v4: https://lore.kernel.org/all/179641334208.1696188.13639506462697032325@skku.edu/
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. The oversized class count returns EINVAL, and a valid
policy can be loaded afterward.

A reproducer is available on request.

 security/selinux/ss/conditional.c | 16 ++++++-----
 security/selinux/ss/policydb.c    | 46 +++++++++++++++++++++++--------
 2 files changed, 43 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..f3a975e71468 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,22 @@ 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);
+			rc = -EINVAL;
+			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 +3205,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 v5] selinux: validate primary symbol count when loading policy
  2026-12-06  4:47 [PATCH v5] selinux: validate primary symbol count when loading policy Kyuwon Cho
@ 2026-12-06  5:01 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-06  5:01 UTC (permalink / raw)
  Cc: selinux

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] selinux: unbounded SYM_ROLES nprim allows 4 GiB vmalloc in
  policydb_index()
--

commit 6e9bd0ca3cd7432aa4d0db65b40d675a7a002abe
Author: Kyuwon Cho <kyuwon.cho@skku.edu>

selinux: validate primary symbol count when loading policy

This commit bounds primary symbol table counts against the remaining
policy size (with exemptions for roles and pre-v20 types), caps the
class count at U16_MAX, 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..db302805ca7b9 100644
> --- a/security/selinux/ss/policydb.c
> +++ b/security/selinux/ss/policydb.c
> @@ -758,23 +758,27 @@ static int policydb_index(struct policydb *p)

[ ... ]

> -	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;

[ ... ]

> @@ -2904,6 +2909,22 @@ 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);
> +			rc = -EINVAL;
> +			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;
> +			}
> +		}

[Severity: Medium]
Can switching p->role_val_to_struct to kvzalloc_objs() while leaving
SYM_ROLES unbounded let a tiny policy allocate up to ~4 GiB of vmalloc
memory in policydb_index()?

Before this patch, kzalloc_objs() rejected p->p_roles.nprim once the
array exceeded KMALLOC_MAX_SIZE (4 MiB on x86_64), failing fast before
reaching kvcalloc() for p->sym_val_to_name[SYM_ROLES].

With kvzalloc_objs(), __kvmalloc_node_noprof() falls back to vmalloc up
to INT_MAX (2 GiB).  A malformed policy with SYM_ROLES nprim set to
0x0fffffff now allocates and zeroes nearly 2 GiB for role_val_to_struct
and another 2 GiB for sym_val_to_name[SYM_ROLES].

Because sel_write_load() runs policydb_read() under
selinux_state.policy_mutex, this stalls concurrent policy and boolean
operations and can pin the memory since sparse role tables are accepted.
Would capping SYM_ROLES nprim avoid this regression?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/179643369147.1996725.5532974391711154788@skku.edu?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-06  5:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06  4:47 [PATCH v5] selinux: validate primary symbol count when loading policy Kyuwon Cho
2026-12-06  5:01 ` 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.