flex_array: fix flex_array_put_ptr macro to be valid C
PATCH 1/4 flex_array: fix flex_array_put_ptr macro to be valid C
Using flex_array_put_ptr() results in a compile error "error: lvalue required as unary ‘&’ operand" fix the casting order to fix this.
---
include/linux/flex_array.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
include/linux/flex_array.h +1 -1
@@ -71,7 +71,7 @@ void *flex_array_get(struct flex_array *fa, unsigned int element_nr);int flex_array_shrink (struct flex_array *fa);#define flex_array_put_ptr (fa, nr, src, gfp) \ - flex_array_put(fa, nr, &(void *)(src), gfp) + flex_array_put(fa, nr, (void *)&(src), gfp)void *flex_array_get_ptr (struct flex_array *fa,unsigned int element_nr);
--
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.
PATCH 2/4 selinux: convert type_val_to_struct to flex_array
In rawhide type_val_to_struct will allocate 26848 bytes, an order 3 allocations. While this hasn't been seen to fail it isn't outside the realm of possibiliy on systems with severe memory fragmentation. Convert to flex_array so no allocation will ever be bigger than PAGE_SIZE.
---
security/selinux/ss/policydb.c | 28 +++++++++++++++++++++------- security/selinux/ss/policydb.h | 2 +- security/selinux/ss/services.c | 17 ++++++++++++----- 3 files changed, 34 insertions(+), 13 deletions(-)
security/selinux/ss/policydb.c +21 -7
@@ -307,7 +307,11 @@ static int type_index(void *key, void *datum, void *datap) || typdatum->bounds > p->p_types.nprim)return -EINVAL ; p->p_type_val_to_name[typdatum->value -1 ] = key; - p->type_val_to_struct[typdatum->value -1 ] = typdatum; +/* this flex array was all preallocated, this cannot fail */ +if (flex_array_put_ptr (p->type_val_to_struct_array, + typdatum->value -1 , typdatum, +GFP_KERNEL | __GFP_ZERO)) +BUG (); }return 0 ;
@@ -484,11 +488,17 @@ static int policydb_index_others(struct policydb *p)if (!p->user_val_to_struct) goto out; +/* Yes, I want the sizeof the pointer, not the structure */ rc = -ENOMEM ; - p->type_val_to_struct = -kmalloc (p->p_types.nprim *sizeof (*(p->type_val_to_struct)), -GFP_KERNEL ); -if (!p->type_val_to_struct) + p->type_val_to_struct_array =flex_array_alloc (sizeof (struct type_datum *), + p->p_types.nprim, +GFP_KERNEL | __GFP_ZERO); +if (!p->type_val_to_struct_array) + goto out; + + rc =flex_array_prealloc (p->type_val_to_struct_array,0 , + p->p_types.nprim -1 ,GFP_KERNEL | __GFP_ZERO); +if (rc) goto out; rc = -ENOMEM ;
@@ -699,7 +709,8 @@ void policydb_destroy(struct policydb *p)kfree (p->class_val_to_struct);kfree (p->role_val_to_struct);kfree (p->user_val_to_struct); -kfree (p->type_val_to_struct); +if (p->type_val_to_struct_array) +flex_array_free (p->type_val_to_struct_array);avtab_destroy (&p->te_avtab);
@@ -1618,7 +1629,10 @@ static int type_bounds_sanity_check(void *key, void *datum, void *datap)return -EINVAL ; } - upper = p->type_val_to_struct[upper->bounds -1 ]; + upper =flex_array_get_ptr (p->type_val_to_struct_array, + upper->bounds -1 ); +BUG_ON (!upper); +if (upper->attribute) { printk(KERN_ERR "SELinux: type %s: " "bounded by attribute %s",
security/selinux/ss/policydb.h +1 -1
@@ -217,7 +217,7 @@ struct policydb {struct class_datum **class_val_to_struct;struct role_datum **role_val_to_struct;struct user_datum **user_val_to_struct; -struct type_datum **type_val_to_struct; +struct flex_array *type_val_to_struct_array;/* type enforcement access vectors and transitions */ struct avtab te_avtab;
security/selinux/ss/services.c +12 -5
@@ -530,12 +530,18 @@ static void type_attribute_bounds_av(struct context *scontext,struct context lo_scontext;struct context lo_tcontext;struct av_decision lo_avd; -struct type_datum *source - = policydb.type_val_to_struct[scontext->type -1 ]; -struct type_datum *target - = policydb.type_val_to_struct[tcontext->type -1 ]; +struct type_datum *source; +struct type_datum *target;u32 masked =0 ; + source =flex_array_get_ptr (policydb.type_val_to_struct_array, + scontext->type -1 ); +BUG_ON (!source); + + target =flex_array_get_ptr (policydb.type_val_to_struct_array, + tcontext->type -1 ); +BUG_ON (!target); +if (source->bounds) {memset (&lo_avd,0 ,sizeof (lo_avd));
@@ -828,7 +834,8 @@ int security_bounded_transition(u32 old_sid, u32 new_sid) index = new_context->type;while (true) { - type = policydb.type_val_to_struct[index -1 ]; + type =flex_array_get_ptr (policydb.type_val_to_struct_array, + index -1 );BUG_ON (!type);/* not bounded anymore */
--
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.
PATCH 3/4 selinux: convert part of the sym_val_to_name array to use flex_array
In reply to Eric ParisThe sym_val_to_name type array can be quite large as it grows linearly with the number of types. With known policies having over 5k types these allocations are growing large enough that they are likely to fail. Convert those to flex_array so no allocation is larger than PAGE_SIZE
---
security/selinux/ss/conditional.c | 6 ++ security/selinux/ss/mls.c | 25 ++++---- security/selinux/ss/policydb.c | 109 ++++++++++++++++++++++++++++--------- security/selinux/ss/policydb.h | 17 +++--- security/selinux/ss/services.c | 38 ++++++------- 5 files changed, 127 insertions(+), 68 deletions(-)
security/selinux/ss/conditional.c +5 -1
@@ -193,6 +193,7 @@ int cond_index_bool(void *key, void *datum, void *datap) {struct policydb *p;struct cond_bool_datum *booldatum; +struct flex_array *fa; booldatum = datum; p = datap;
@@ -200,7 +201,10 @@ int cond_index_bool(void *key, void *datum, void *datap)if (!booldatum->value || booldatum->value > p->p_bools.nprim)return -EINVAL ; - p->p_bool_val_to_name[booldatum->value -1 ] = key; + fa = p->sym_val_to_name[SYM_BOOLS ]; +if (flex_array_put_ptr (fa, booldatum->value -1 , key, +GFP_KERNEL | __GFP_ZERO)) +BUG (); p->bool_val_to_struct[booldatum->value -1 ] = booldatum;return 0 ;
security/selinux/ss/mls.c +13 -12
@@ -45,7 +45,7 @@ int mls_compute_context_len(struct context *context) len =1 ;/* for the beginning ":" */ for (l =0 ; l <2 ; l++) {int index_sens = context->range.level[l].sens; - len +=strlen (policydb.p_sens_val_to_name[index_sens -1 ]); + len +=strlen (sym_name (&policydb,SYM_LEVELS , index_sens -1 ));/* categories */ head =-2 ;
@@ -55,17 +55,17 @@ int mls_compute_context_len(struct context *context)if (i - prev >1 ) {/* one or more negative bits are skipped */ if (head != prev) { - nm = policydb.p_cat_val_to_name[prev]; + nm =sym_name (&policydb,SYM_CATS , prev); len +=strlen (nm) +1 ; } - nm = policydb.p_cat_val_to_name[i]; + nm =sym_name (&policydb,SYM_CATS , i); len +=strlen (nm) +1 ; head = i; } prev = i; }if (prev != head) { - nm = policydb.p_cat_val_to_name[prev]; + nm =sym_name (&policydb,SYM_CATS , prev); len +=strlen (nm) +1 ; }if (l ==0 ) {
@@ -102,8 +102,8 @@ void mls_sid_to_context(struct context *context, scontextp++;for (l =0 ; l <2 ; l++) { -strcpy (scontextp, - policydb.p_sens_val_to_name[context->range.level[l].sens -1 ]); +strcpy (scontextp,sym_name (&policydb,SYM_LEVELS , + context->range.level[l].sens -1 )); scontextp +=strlen (scontextp);/* categories */
@@ -118,7 +118,7 @@ void mls_sid_to_context(struct context *context, *scontextp++ ='.' ; else *scontextp++ = ','; - nm = policydb.p_cat_val_to_name[prev]; + nm =sym_name (&policydb,SYM_CATS , prev);strcpy (scontextp, nm); scontextp +=strlen (nm); }
@@ -126,7 +126,7 @@ void mls_sid_to_context(struct context *context, *scontextp++ =':' ; else *scontextp++ = ','; - nm = policydb.p_cat_val_to_name[i]; + nm =sym_name (&policydb,SYM_CATS , i);strcpy (scontextp, nm); scontextp +=strlen (nm); head = i;
@@ -139,7 +139,7 @@ void mls_sid_to_context(struct context *context, *scontextp++ ='.' ; else *scontextp++ = ','; - nm = policydb.p_cat_val_to_name[prev]; + nm =sym_name (&policydb,SYM_CATS , prev);strcpy (scontextp, nm); scontextp +=strlen (nm); }
@@ -166,7 +166,7 @@ int mls_level_isvalid(struct policydb *p, struct mls_level *l)if (!l->sens || l->sens > p->p_levels.nprim)return 0 ; levdatum =hashtab_search (p->p_levels.table, - p->p_sens_val_to_name[l->sens -1 ]); +sym_name (p,SYM_LEVELS , l->sens -1 ));if (!levdatum)return 0 ;
@@ -482,7 +482,8 @@ int mls_convert_context(struct policydb *oldp,for (l =0 ; l <2 ; l++) { levdatum =hashtab_search (newp->p_levels.table, - oldp->p_sens_val_to_name[c->range.level[l].sens -1 ]); +sym_name (oldp,SYM_LEVELS , + c->range.level[l].sens -1 ));if (!levdatum)return -EINVAL ;
@@ -493,7 +494,7 @@ int mls_convert_context(struct policydb *oldp,int rc; catdatum =hashtab_search (newp->p_cats.table, - oldp->p_cat_val_to_name[i]); +sym_name (oldp,SYM_CATS , i));if (!catdatum)return -EINVAL ; rc =ebitmap_set_bit (&bitmap, catdatum->value -1 ,1 );
security/selinux/ss/policydb.c +82 -27
235 lines not on this page · raw message
security/selinux/ss/policydb.h +8 -9
@@ -203,15 +203,7 @@ struct policydb {#define p_cats symtab[SYM_CATS]/* symbol names indexed by (value - 1) */ -char **sym_val_to_name[SYM_NUM ]; -#define p_common_val_to_name sym_val_to_name[SYM_COMMONS] -#define p_class_val_to_name sym_val_to_name[SYM_CLASSES] -#define p_role_val_to_name sym_val_to_name[SYM_ROLES] -#define p_type_val_to_name sym_val_to_name[SYM_TYPES] -#define p_user_val_to_name sym_val_to_name[SYM_USERS] -#define p_bool_val_to_name sym_val_to_name[SYM_BOOLS] -#define p_sens_val_to_name sym_val_to_name[SYM_LEVELS] -#define p_cat_val_to_name sym_val_to_name[SYM_CATS] +struct flex_array *sym_val_to_name[SYM_NUM ];/* class, role, and user attributes indexed by (value - 1) */ struct class_datum **class_val_to_struct;
@@ -321,6 +313,13 @@ static inline int put_entry(void *buf, size_t bytes, int num, struct policy_filereturn 0 ; } +static inline char *sym_name (struct policydb *p,unsigned int sym_num,unsigned int element_nr) +{ +struct flex_array *fa = p->sym_val_to_name[sym_num]; + +return flex_array_get_ptr (fa, element_nr); +} +extern u16 string_to_security_class (struct policydb *p,const char *name);extern u32 string_to_av_perm (struct policydb *p,u16 tclass,const char *name);
security/selinux/ss/services.c +19 -19
@@ -464,7 +464,7 @@ static void security_dump_masked_av(struct context *scontext,if (!permissions)return ; - tclass_name = policydb.p_class_val_to_name[tclass -1 ]; + tclass_name =sym_name (&policydb,SYM_CLASSES , tclass -1 ); tclass_dat = policydb.class_val_to_struct[tclass -1 ]; common_dat = tclass_dat->comdatum;
@@ -716,7 +716,7 @@ static int security_validtrans_handle_fail(struct context *ocontext,audit_log (current->audit_context,GFP_ATOMIC ,AUDIT_SELINUX_ERR ,"security_validate_transition: denied for" " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s" , - o, n, t, policydb.p_class_val_to_name[tclass-1 ]); + o, n, t,sym_name (&policydb,SYM_CLASSES , tclass-1 )); out:kfree (o);kfree (n);
@@ -1012,9 +1012,9 @@ static int context_struct_to_string(struct context *context, char **scontext, u3 }/* Compute the size of the context. */ - *scontext_len +=strlen (policydb.p_user_val_to_name[context->user -1 ]) +1 ; - *scontext_len +=strlen (policydb.p_role_val_to_name[context->role -1 ]) +1 ; - *scontext_len +=strlen (policydb.p_type_val_to_name[context->type -1 ]) +1 ; + *scontext_len +=strlen (sym_name (&policydb,SYM_USERS , context->user -1 )) +1 ; + *scontext_len +=strlen (sym_name (&policydb,SYM_ROLES , context->role -1 )) +1 ; + *scontext_len +=strlen (sym_name (&policydb,SYM_TYPES , context->type -1 )) +1 ; *scontext_len +=mls_compute_context_len (context);if (!scontext)
@@ -1030,12 +1030,12 @@ static int context_struct_to_string(struct context *context, char **scontext, u3* Copy the user name, role name and type name into the context. */ sprintf (scontextp,"%s:%s:%s" , - policydb.p_user_val_to_name[context->user -1 ], - policydb.p_role_val_to_name[context->role -1 ], - policydb.p_type_val_to_name[context->type -1 ]); - scontextp +=strlen (policydb.p_user_val_to_name[context->user -1 ]) + -1 +strlen (policydb.p_role_val_to_name[context->role -1 ]) + -1 +strlen (policydb.p_type_val_to_name[context->type -1 ]); +sym_name (&policydb,SYM_USERS , context->user -1 ), +sym_name (&policydb,SYM_ROLES , context->role -1 ), +sym_name (&policydb,SYM_TYPES , context->type -1 )); + scontextp +=strlen (sym_name (&policydb,SYM_USERS , context->user -1 )) + +1 +strlen (sym_name (&policydb,SYM_ROLES , context->role -1 )) + +1 +strlen (sym_name (&policydb,SYM_TYPES , context->type -1 ));mls_sid_to_context (context, &scontextp);
@@ -1333,7 +1333,7 @@ static int compute_sid_handle_invalid_context(" for scontext=%s" " tcontext=%s" " tclass=%s" , - n, s, t, policydb.p_class_val_to_name[tclass-1]); + n, s, t, sym_name(&policydb, SYM_CLASSES, tclass-1)); out:kfree (s);kfree (t);
@@ -1654,7 +1654,7 @@ static int convert_context(u32 key,/* Convert the user. */ rc = -EINVAL ; usrdatum =hashtab_search (args->newp->p_users.table, - args->oldp->p_user_val_to_name[c->user -1 ]); +sym_name (args->oldp,SYM_USERS , c->user -1 ));if (!usrdatum) goto bad; c->user = usrdatum->value;
@@ -1662,7 +1662,7 @@ static int convert_context(u32 key,/* Convert the role. */ rc = -EINVAL ; role =hashtab_search (args->newp->p_roles.table, - args->oldp->p_role_val_to_name[c->role -1 ]); +sym_name (args->oldp,SYM_ROLES , c->role -1 ));if (!role) goto bad; c->role = role->value;
@@ -1670,7 +1670,7 @@ static int convert_context(u32 key,/* Convert the type. */ rc = -EINVAL ; typdatum =hashtab_search (args->newp->p_types.table, - args->oldp->p_type_val_to_name[c->type -1 ]); +sym_name (args->oldp,SYM_TYPES , c->type -1 ));if (!typdatum) goto bad; c->type = typdatum->value;
@@ -2326,14 +2326,14 @@ int security_get_bools(int *len, char ***names, int **values)size_t name_len; (*values)[i] = policydb.bool_val_to_struct[i]->state; - name_len =strlen (policydb.p_bool_val_to_name[i]) +1 ; + name_len =strlen (sym_name (&policydb,SYM_BOOLS , i)) +1 ; rc = -ENOMEM ; (*names)[i] =kmalloc (sizeof (char ) * name_len,GFP_ATOMIC );if (!(*names)[i]) goto err; -strncpy ((*names)[i], policydb.p_bool_val_to_name[i], name_len); +strncpy ((*names)[i],sym_name (&policydb,SYM_BOOLS , i), name_len); (*names)[i][name_len -1 ] =0 ; } rc =0 ;
@@ -2368,7 +2368,7 @@ int security_set_bools(int len, int *values) audit_log(current->audit_context, GFP_ATOMIC, AUDIT_MAC_CONFIG_CHANGE, "bool=%s val=%d old_val=%d auid=%u ses=%u", - policydb.p_bool_val_to_name[i], + sym_name(&policydb, SYM_BOOLS, i), !!values[i], policydb.bool_val_to_struct[i]->state, audit_get_loginuid(current),
@@ -3132,7 +3132,7 @@ int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr) goto out; rc = -ENOMEM ; - secattr->domain =kstrdup (policydb.p_type_val_to_name[ctx->type -1 ], + secattr->domain =kstrdup (sym_name (&policydb,SYM_TYPES , ctx->type -1 ),GFP_ATOMIC );if (secattr->domain ==NULL ) goto out;
--
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.
PATCH 4/4 SELinux: merge policydb_index_classes and policydb_index_others
In reply to Eric ParisWe duplicate functionality in policydb_index_classes() and policydb_index_others(). This patch merges those functions just to make it clear there is nothing special happening here.
---
security/selinux/ss/policydb.c | 69 ++++++---------------------------------- 1 files changed, 10 insertions(+), 59 deletions(-)
security/selinux/ss/policydb.c +10 -59
@@ -411,58 +411,6 @@ static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) = cat_index, }; -/* -* Define the common val_to_name array and the class -* val_to_name and val_to_struct arrays in a policy -* database structure. -* -* Caller must clean up upon failure. -*/ -static int policydb_index_classes (struct policydb *p) -{ -int rc; - - rc = -ENOMEM ; - p->sym_val_to_name[SYM_COMMONS ] =flex_array_alloc (sizeof (char *), - p->p_commons.nprim, -GFP_KERNEL | __GFP_ZERO); -if (!p->sym_val_to_name[SYM_COMMONS ]) - goto out; - - rc =flex_array_prealloc (p->sym_val_to_name[SYM_COMMONS ], -0 , p->p_commons.nprim -1 , -GFP_KERNEL | __GFP_ZERO); -if (rc) - goto out; - - rc =hashtab_map (p->p_commons.table, common_index, p); -if (rc) - goto out; - - rc = -ENOMEM ; - p->class_val_to_struct = -kmalloc (p->p_classes.nprim *sizeof (*(p->class_val_to_struct)),GFP_KERNEL ); -if (!p->class_val_to_struct) - goto out; - - rc = -ENOMEM ; - p->sym_val_to_name[SYM_CLASSES ] =flex_array_alloc (sizeof (char *), - p->p_classes.nprim, -GFP_KERNEL | __GFP_ZERO); -if (!p->sym_val_to_name[SYM_CLASSES ]) - goto out; - - rc =flex_array_prealloc (p->sym_val_to_name[SYM_CLASSES ], -0 , p->p_classes.nprim -1 , -GFP_KERNEL | __GFP_ZERO); -if (rc) - goto out; - - rc =hashtab_map (p->p_classes.table, class_index, p); -out: -return rc; -} -#ifdef DEBUG_HASHES static void symtab_hash_eval (struct symtab *s) {
@@ -500,7 +448,7 @@ static inline void rangetr_hash_eval(struct hashtab *h)* * Caller must clean up on failure. */ -static int policydb_index_others (struct policydb *p) +static int policydb_index (struct policydb *p) {int i, rc;
@@ -520,6 +468,13 @@ static int policydb_index_others(struct policydb *p)#endif rc = -ENOMEM ; + p->class_val_to_struct = +kmalloc (p->p_classes.nprim *sizeof (*(p->class_val_to_struct)), +GFP_KERNEL ); +if (!p->class_val_to_struct) + goto out; + + rc = -ENOMEM ; p->role_val_to_struct =kmalloc (p->p_roles.nprim *sizeof (*(p->role_val_to_struct)),GFP_KERNEL );
@@ -550,7 +505,7 @@ static int policydb_index_others(struct policydb *p)if (cond_init_bool_indexes (p)) goto out; -for (i =SYM_ROLES ; i <SYM_NUM ; i++) { +for (i =0 ; i <SYM_NUM ; i++) { rc = -ENOMEM ; p->sym_val_to_name[i] = flex_array_alloc(sizeof(char *), p->symtab[i].nprim,
@@ -2296,11 +2251,7 @@ int policydb_read(struct policydb *p, void *fp) lra = ra; } - rc =policydb_index_classes (p); -if (rc) - goto bad; - - rc =policydb_index_others (p); + rc =policydb_index (p);if (rc) goto bad;
--
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 4 lines · Eric Paris
Using
flex_array_put_ptr()results in a compile error "error: lvalue required as unary ?&? operand" fix the casting order to fix this.Signed-off-by Eric Paris <[email protected]>
These all look good to me. Consider cc'ing the LSM list with SELinux kernel changes.
Quoted 19 lines
---
include/linux/flex_array.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)include/linux/flex_array.h +1
-1@@ -71,7 +71,7 @@ void *flex_array_get(struct flex_array *fa, unsigned int element_nr);int flex_array_shrink (struct flex_array *fa);#define flex_array_put_ptr (fa, nr, src, gfp) \ - flex_array_put(fa, nr, &(void *)(src), gfp) + flex_array_put(fa, nr, (void *)&(src), gfp)void *flex_array_get_ptr (struct flex_array *fa,unsigned int element_nr);
Signature · 7 lines
James Morris <[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.