invalid memory allocation in policydb_index_others()
PATCH 1/1 invalid memory allocation in policydb_index_others()
There is an error in libsepol's policydb_index_others(). If a policy is lacking symbols of some kind (e.g., a policy with no booleans), then the function will attempt a calloc() of 0, which is undefined. The following patch checks for this condition and explicitly sets the field upon zero symbols.
src/policydb.c +10 -8
@@ -820,14 +820,16 @@cond_init_bool_indexes (p);for (i =SYM_ROLES ; i <SYM_NUM ; i++) { -if (p->sym_val_to_name[i]) -free (p->sym_val_to_name[i]); - p->sym_val_to_name[i] = (char **) -calloc (p->symtab[i].nprim,sizeof (char *)); -if (!p->sym_val_to_name[i]) -return -1 ; -if (hashtab_map (p->symtab[i].table, index_f[i], p)) -return -1 ; +free (p->sym_val_to_name[i]); + p->sym_val_to_name[i] =NULL ; +if (p->symtab[i].nprim) { + p->sym_val_to_name[i] = (char **) +calloc (p->symtab[i].nprim,sizeof (char *)); +if (!p->sym_val_to_name[i]) +return -1 ; +if (hashtab_map (p->symtab[i].table, index_f[i], p)) +return -1 ; + } }/* This pre-expands the roles and users for context validity checking */
Signature · 6 lines
Jason Tang / [email protected] -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 5 lines · J. Tang
There is an error in libsepol's
policydb_index_others(). If a policy is lacking symbols of some kind (e.g., a policy with no booleans), then the function will attempt acalloc()of 0, which is undefined. The following patch checks for this condition and explicitly sets the field upon zero symbols.
IIUC, this doesn't manifest under glibc since it returns a non-NULL pointer that can be later free'd without error, but could create a problem for other C libraries (e.g. uclibc) that may instead return NULL, producing an error return here.
Quoted 31 lines
Index:
libsepol/src/policydb.c
===================================================================src/policydb.c +10
-8@@ -820,14 +820,16 @@cond_init_bool_indexes (p);for (i =SYM_ROLES ; i <SYM_NUM ; i++) { -if (p->sym_val_to_name[i]) -free (p->sym_val_to_name[i]); - p->sym_val_to_name[i] = (char **) -calloc (p->symtab[i].nprim,sizeof (char *)); -if (!p->sym_val_to_name[i]) -return -1 ; -if (hashtab_map (p->symtab[i].table, index_f[i], p)) -return -1 ; +free (p->sym_val_to_name[i]); + p->sym_val_to_name[i] =NULL ; +if (p->symtab[i].nprim) { + p->sym_val_to_name[i] = (char **) +calloc (p->symtab[i].nprim,sizeof (char *)); +if (!p->sym_val_to_name[i]) +return -1 ; +if (hashtab_map (p->symtab[i].table, index_f[i], p)) +return -1 ; + } }/* This pre-expands the roles and users for context validity checking */
Signature · 8 lines
Stephen Smalley National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.