All of lore.kernel.org
 help / color / mirror / Atom feed
* [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

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.