SELinux: standardize return code handling in policydb.c
PATCH 1/4 SELinux: standardize return code handling in policydb.c
policydb.c has lots of different standards on how to handle return paths on error. For the most part transition to
rc=errno if (failure) goto out; [...] out: cleanup() return rc;
Instead of doing cleanup mid function, or having multiple returns or other options. This doesn't do that for every function, but most of the complex functions which have cleanup routines on error.
---
security/selinux/ss/policydb.c | 555 +++++++++++++++++++--------------------- 1 files changed, 268 insertions(+), 287 deletions(-)
security/selinux/ss/policydb.c +268 -287
@@ -148,32 +148,30 @@ static int roles_init(struct policydb *p)int rc;struct role_datum *role; + rc = -ENOMEM ; role =kzalloc (sizeof (*role),GFP_KERNEL ); -if (!role) { - rc = -ENOMEM ; +if (!role) goto out; - } + + rc = -EINVAL ; role->value = ++p->p_roles.nprim; -if (role->value !=OBJECT_R_VAL ) { - rc = -EINVAL ; - goto out_free_role; - } +if (role->value !=OBJECT_R_VAL ) + goto out; + + rc = -ENOMEM ; key =kstrdup (OBJECT_R ,GFP_KERNEL ); -if (!key) { - rc = -ENOMEM ; - goto out_free_role; - } +if (!key) + goto out; + rc =hashtab_insert (p->p_roles.table, key, role);if (rc) - goto out_free_key; -out: -return rc; + goto out; -out_free_key: +return 0 ; +out:kfree (key); -out_free_role:kfree (role); - goto out; +return rc; }static u32 rangetr_hash (struct hashtab *h,const void *k)
@@ -203,35 +201,33 @@ static int policydb_init(struct policydb *p)for (i =0 ; i <SYM_NUM ; i++) { rc =symtab_init (&p->symtab[i], symtab_sizes[i]);if (rc) - goto out_free_symtab; + goto out; } rc =avtab_init (&p->te_avtab);if (rc) - goto out_free_symtab; + goto out; rc =roles_init (p);if (rc) - goto out_free_symtab; + goto out; rc =cond_policydb_init (p);if (rc) - goto out_free_symtab; + goto out; p->range_tr =hashtab_create (rangetr_hash, rangetr_cmp,256 );if (!p->range_tr) - goto out_free_symtab; + goto out;ebitmap_init (&p->policycaps);ebitmap_init (&p->permissive_map); +return 0 ; out: -return rc; - -out_free_symtab:for (i =0 ; i <SYM_NUM ; i++)hashtab_destroy (p->symtab[i].table); - goto out; +return rc; }/*
@@ -381,30 +377,27 @@ static int policydb_index_classes(struct policydb *p) {int rc; + rc = -ENOMEM ; p->p_common_val_to_name =kmalloc (p->p_commons.nprim *sizeof (char *),GFP_KERNEL ); -if (!p->p_common_val_to_name) { - rc = -ENOMEM ; +if (!p->p_common_val_to_name) 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) { - rc = -ENOMEM ; +if (!p->class_val_to_struct) goto out; - } + rc = -ENOMEM ; p->p_class_val_to_name =kmalloc (p->p_classes.nprim *sizeof (char *),GFP_KERNEL ); -if (!p->p_class_val_to_name) { - rc = -ENOMEM ; +if (!p->p_class_val_to_name) goto out; - } rc =hashtab_map (p->p_classes.table, class_index, p); out:
@@ -450,7 +443,7 @@ static inline void rangetr_hash_eval(struct hashtab *h)*/ static int policydb_index_others (struct policydb *p) { -int i, rc =0 ; +int i, rc;printk (KERN_DEBUG "SELinux: %d users, %d roles, %d types, %d bools" , p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
@@ -467,47 +460,42 @@ static int policydb_index_others(struct policydb *p)symtab_hash_eval (p->symtab);#endif + rc = -ENOMEM ; p->role_val_to_struct =kmalloc (p->p_roles.nprim *sizeof (*(p->role_val_to_struct)),GFP_KERNEL ); -if (!p->role_val_to_struct) { - rc = -ENOMEM ; +if (!p->role_val_to_struct) goto out; - } + rc = -ENOMEM ; p->user_val_to_struct =kmalloc (p->p_users.nprim *sizeof (*(p->user_val_to_struct)),GFP_KERNEL ); -if (!p->user_val_to_struct) { - rc = -ENOMEM ; +if (!p->user_val_to_struct) goto out; - } + 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) { - rc = -ENOMEM ; +if (!p->type_val_to_struct) goto out; - } -if (cond_init_bool_indexes (p)) { - rc = -ENOMEM ; + rc = -ENOMEM ; +if (cond_init_bool_indexes (p)) goto out; - }for (i =SYM_ROLES ; i <SYM_NUM ; i++) { + rc = -ENOMEM ; p->sym_val_to_name[i] =kmalloc (p->symtab[i].nprim *sizeof (char *),GFP_KERNEL ); -if (!p->sym_val_to_name[i]) { - rc = -ENOMEM ; +if (!p->sym_val_to_name[i]) goto out; - } rc =hashtab_map (p->symtab[i].table, index_f[i], p);if (rc) goto out; } - + rc =0 ; out:return rc; }
@@ -530,9 +518,11 @@ static int common_destroy(void *key, void *datum, void *p)struct common_datum *comdatum;kfree (key); - comdatum = datum; -hashtab_map (comdatum->permissions.table, perm_destroy,NULL ); -hashtab_destroy (comdatum->permissions.table); +if (datum) { + comdatum = datum; +hashtab_map (comdatum->permissions.table, perm_destroy,NULL ); +hashtab_destroy (comdatum->permissions.table); + }kfree (datum);return 0 ; }
@@ -544,38 +534,40 @@ static int cls_destroy(void *key, void *datum, void *p)struct constraint_expr *e, *etmp;kfree (key); - cladatum = datum; -hashtab_map (cladatum->permissions.table, perm_destroy,NULL ); -hashtab_destroy (cladatum->permissions.table); - constraint = cladatum->constraints; -while (constraint) { - e = constraint->expr; -while (e) { -ebitmap_destroy (&e->names); - etmp = e; - e = e->next; -kfree (etmp); +if (datum) { + cladatum = datum; +hashtab_map (cladatum->permissions.table, perm_destroy,NULL ); +hashtab_destroy (cladatum->permissions.table); + constraint = cladatum->constraints; +while (constraint) { + e = constraint->expr; +while (e) { +ebitmap_destroy (&e->names); + etmp = e; + e = e->next; +kfree (etmp); + } + ctemp = constraint; + constraint = constraint->next; +kfree (ctemp); } - ctemp = constraint; - constraint = constraint->next; -kfree (ctemp); - } - - constraint = cladatum->validatetrans; -while (constraint) { - e = constraint->expr; -while (e) { -ebitmap_destroy (&e->names); - etmp = e; - e = e->next; -kfree (etmp); + + constraint = cladatum->validatetrans; +while (constraint) { + e = constraint->expr; +while (e) { +ebitmap_destroy (&e->names); + etmp = e; + e = e->next; +kfree (etmp); + } + ctemp = constraint; + constraint = constraint->next; +kfree (ctemp); } - ctemp = constraint; - constraint = constraint->next; -kfree (ctemp); + +kfree (cladatum->comkey); } - -kfree (cladatum->comkey);kfree (datum);return 0 ; }
@@ -585,9 +577,11 @@ static int role_destroy(void *key, void *datum, void *p)struct role_datum *role;kfree (key); - role = datum; -ebitmap_destroy (&role->dominates); -ebitmap_destroy (&role->types); +if (datum) { + role = datum; +ebitmap_destroy (&role->dominates); +ebitmap_destroy (&role->types); + }kfree (datum);return 0 ; }
@@ -604,11 +598,13 @@ static int user_destroy(void *key, void *datum, void *p)struct user_datum *usrdatum;kfree (key); - usrdatum = datum; -ebitmap_destroy (&usrdatum->roles); -ebitmap_destroy (&usrdatum->range.level[0 ].cat); -ebitmap_destroy (&usrdatum->range.level[1 ].cat); -ebitmap_destroy (&usrdatum->dfltlevel.cat); +if (datum) { + usrdatum = datum; +ebitmap_destroy (&usrdatum->roles); +ebitmap_destroy (&usrdatum->range.level[0 ].cat); +ebitmap_destroy (&usrdatum->range.level[1 ].cat); +ebitmap_destroy (&usrdatum->dfltlevel.cat); + }kfree (datum);return 0 ; }
@@ -618,9 +614,11 @@ static int sens_destroy(void *key, void *datum, void *p)struct level_datum *levdatum;kfree (key); - levdatum = datum; -ebitmap_destroy (&levdatum->level->cat); -kfree (levdatum->level); +if (datum) { + levdatum = datum; +ebitmap_destroy (&levdatum->level->cat); +kfree (levdatum->level); + }kfree (datum);return 0 ; }
@@ -775,19 +773,21 @@ int policydb_load_isids(struct policydb *p, struct sidtab *s) head = p->ocontexts[OCON_ISID ];for (c = head; c; c = c->next) { + rc = -EINVAL ;if (!c->context[0 ].user) { -printk (KERN_ERR "SELinux: SID %s was never " -"defined.\n" , c->u.name); - rc = -EINVAL ; +printk (KERN_ERR "SELinux: SID %s was never defined.\n" , + c->u.name); goto out; } -if (sidtab_insert (s, c->sid[0 ], &c->context[0 ])) { -printk (KERN_ERR "SELinux: unable to load initial " -"SID %s.\n" , c->u.name); - rc = -EINVAL ; + + rc =sidtab_insert (s, c->sid[0 ], &c->context[0 ]); +if (rc) { +printk (KERN_ERR "SELinux: unable to load initial SID %s.\n" , + c->u.name); goto out; } } + rc =0 ; out:return rc; }
@@ -836,8 +836,7 @@ int policydb_context_isvalid(struct policydb *p, struct context *c)* Role must be authorized for the type. */ role = p->role_val_to_struct[c->role -1 ]; -if (!ebitmap_get_bit (&role->types, - c->type -1 )) +if (!ebitmap_get_bit (&role->types, c->type -1 ))/* role may not be associated with type */ return 0 ;
@@ -848,8 +847,7 @@ int policydb_context_isvalid(struct policydb *p, struct context *c)if (!usrdatum)return 0 ; -if (!ebitmap_get_bit (&usrdatum->roles, - c->role -1 )) +if (!ebitmap_get_bit (&usrdatum->roles, c->role -1 ))/* user may not be associated with role */ return 0 ; }
@@ -871,20 +869,22 @@ static int mls_read_range_helper(struct mls_range *r, void *fp)int rc; rc =next_entry (buf, fp,sizeof (u32)); -if (rc <0 ) +if (rc) goto out; + rc = -EINVAL ; items =le32_to_cpu (buf[0 ]);if (items >ARRAY_SIZE (buf)) {printk (KERN_ERR "SELinux: mls: range overflow\n" ); - rc = -EINVAL ; goto out; } + rc =next_entry (buf, fp,sizeof (u32) * items); -if (rc <0 ) { +if (rc) {printk (KERN_ERR "SELinux: mls: truncated range\n" ); goto out; } + r->level[0 ].sens =le32_to_cpu (buf[0 ]);if (items >1 ) r->level[1 ].sens =le32_to_cpu (buf[1 ]);
@@ -893,15 +893,13 @@ static int mls_read_range_helper(struct mls_range *r, void *fp) rc =ebitmap_read (&r->level[0 ].cat, fp);if (rc) { -printk (KERN_ERR "SELinux: mls: error reading low " -"categories\n" ); +printk (KERN_ERR "SELinux: mls: error reading low categories\n" ); goto out; }if (items >1 ) { rc =ebitmap_read (&r->level[1 ].cat, fp);if (rc) { -printk (KERN_ERR "SELinux: mls: error reading high " -"categories\n" ); +printk (KERN_ERR "SELinux: mls: error reading high categories\n" ); goto bad_high; } }else {
@@ -912,12 +910,11 @@ static int mls_read_range_helper(struct mls_range *r, void *fp) } } - rc =0 ; -out: -return rc; +return 0 ; bad_high:ebitmap_destroy (&r->level[0 ].cat); - goto out; +out: +return rc; }/*
@@ -932,7 +929,7 @@ static int context_read_and_validate(struct context *c,int rc; rc =next_entry (buf, fp,sizeof buf); -if (rc <0 ) { +if (rc) {printk (KERN_ERR "SELinux: context truncated\n" ); goto out; }
@@ -940,19 +937,20 @@ static int context_read_and_validate(struct context *c, c->role =le32_to_cpu (buf[1 ]); c->type =le32_to_cpu (buf[2 ]);if (p->policyvers >=POLICYDB_VERSION_MLS ) { -if (mls_read_range_helper (&c->range, fp)) { -printk (KERN_ERR "SELinux: error reading MLS range of " -"context\n" ); - rc = -EINVAL ; + rc =mls_read_range_helper (&c->range, fp); +if (rc) { +printk (KERN_ERR "SELinux: error reading MLS range of context\n" ); goto out; } } + rc = -EINVAL ;if (!policydb_context_isvalid (p, c)) {printk (KERN_ERR "SELinux: invalid security context\n" );context_destroy (c); - rc = -EINVAL ; + goto out; } + rc =0 ; out:return rc; }
@@ -971,37 +969,36 @@ static int perm_read(struct policydb *p, struct hashtab *h, void *fp)__le32 buf[2 ];u32 len; + rc = -ENOMEM ; perdatum =kzalloc (sizeof (*perdatum),GFP_KERNEL ); -if (!perdatum) { - rc = -ENOMEM ; - goto out; - } +if (!perdatum) + goto bad; rc =next_entry (buf, fp,sizeof buf); -if (rc <0 ) +if (rc) goto bad; len =le32_to_cpu (buf[0 ]); perdatum->value =le32_to_cpu (buf[1 ]); + rc = -ENOMEM ; key =kmalloc (len +1 ,GFP_KERNEL ); -if (!key) { - rc = -ENOMEM ; +if (!key) goto bad; - } + rc =next_entry (key, fp, len); -if (rc <0 ) +if (rc) goto bad; key[len] ='\0' ; rc =hashtab_insert (h, key, perdatum);if (rc) goto bad; -out: -return rc; + +return 0 ; bad:perm_destroy (key, perdatum,NULL ); - goto out; +return rc; }static int common_read (struct policydb *p,struct hashtab *h,void *fp)
@@ -1012,14 +1009,13 @@ static int common_read(struct policydb *p, struct hashtab *h, void *fp)u32 len, nel;int i, rc; + rc = -ENOMEM ; comdatum =kzalloc (sizeof (*comdatum),GFP_KERNEL ); -if (!comdatum) { - rc = -ENOMEM ; - goto out; - } +if (!comdatum) + goto bad; rc =next_entry (buf, fp,sizeof buf); -if (rc <0 ) +if (rc) goto bad; len =le32_to_cpu (buf[0 ]);
@@ -1031,13 +1027,13 @@ static int common_read(struct policydb *p, struct hashtab *h, void *fp) comdatum->permissions.nprim =le32_to_cpu (buf[2 ]); nel =le32_to_cpu (buf[3 ]); + rc = -ENOMEM ; key =kmalloc (len +1 ,GFP_KERNEL ); -if (!key) { - rc = -ENOMEM ; +if (!key) goto bad; - } + rc =next_entry (key, fp, len); -if (rc <0 ) +if (rc) goto bad; key[len] ='\0' ;
@@ -1050,11 +1046,10 @@ static int common_read(struct policydb *p, struct hashtab *h, void *fp) rc =hashtab_insert (h, key, comdatum);if (rc) goto bad; -out: -return rc; +return 0 ; bad:common_destroy (key, comdatum,NULL ); - goto out; +return rc; } static int read_cons_helper(struct constraint_node **nodep, int ncons,
@@ -1078,7 +1073,7 @@ static int read_cons_helper(struct constraint_node **nodep, int ncons, *nodep = c; rc =next_entry (buf, fp, (sizeof (u32) *2 )); -if (rc <0 ) +if (rc)return rc; c->permissions =le32_to_cpu (buf[0 ]); nexpr =le32_to_cpu (buf[1 ]);
@@ -1095,7 +1090,7 @@ static int read_cons_helper(struct constraint_node **nodep, int ncons, c->expr = e; rc =next_entry (buf, fp, (sizeof (u32) *3 )); -if (rc <0 ) +if (rc)return rc; e->expr_type =le32_to_cpu (buf[0 ]); e->attr =le32_to_cpu (buf[1 ]);
@@ -1123,8 +1118,9 @@ static int read_cons_helper(struct constraint_node **nodep, int ncons,if (depth == (CEXPR_MAXDEPTH -1 ))return -EINVAL ; depth++; -if (ebitmap_read (&e->names, fp)) -return -EINVAL ; + rc =ebitmap_read (&e->names, fp); +if (rc) +return rc;break ;default :return -EINVAL ;
@@ -1147,14 +1143,13 @@ static int class_read(struct policydb *p, struct hashtab *h, void *fp)u32 len, len2, ncons, nel;int i, rc; + rc = -ENOMEM ; cladatum =kzalloc (sizeof (*cladatum),GFP_KERNEL ); -if (!cladatum) { - rc = -ENOMEM ; - goto out; - } +if (!cladatum) + goto bad; rc =next_entry (buf, fp,sizeof (u32)*6 ); -if (rc <0 ) +if (rc) goto bad; len =le32_to_cpu (buf[0 ]);
@@ -1169,33 +1164,30 @@ static int class_read(struct policydb *p, struct hashtab *h, void *fp) ncons =le32_to_cpu (buf[5 ]); + rc = -ENOMEM ; key =kmalloc (len +1 ,GFP_KERNEL ); -if (!key) { - rc = -ENOMEM ; +if (!key) goto bad; - } + rc =next_entry (key, fp, len); -if (rc <0 ) +if (rc) goto bad; key[len] ='\0' ;if (len2) { + rc = -ENOMEM ; cladatum->comkey =kmalloc (len2 +1 ,GFP_KERNEL ); -if (!cladatum->comkey) { - rc = -ENOMEM ; +if (!cladatum->comkey) goto bad; - } rc =next_entry (cladatum->comkey, fp, len2); -if (rc <0 ) +if (rc) goto bad; cladatum->comkey[len2] ='\0' ; - cladatum->comdatum =hashtab_search (p->p_commons.table, - cladatum->comkey); + rc = -EINVAL ; + cladatum->comdatum =hashtab_search (p->p_commons.table, cladatum->comkey);if (!cladatum->comdatum) { -printk (KERN_ERR "SELinux: unknown common %s\n" , - cladatum->comkey); - rc = -EINVAL ; +printk (KERN_ERR "SELinux: unknown common %s\n" , cladatum->comkey); goto bad; } }
@@ -1212,7 +1204,7 @@ static int class_read(struct policydb *p, struct hashtab *h, void *fp)if (p->policyvers >=POLICYDB_VERSION_VALIDATETRANS ) {/* grab the validatetrans rules */ rc =next_entry (buf, fp,sizeof (u32)); -if (rc <0 ) +if (rc) goto bad; ncons =le32_to_cpu (buf[0 ]); rc =read_cons_helper (&cladatum->validatetrans, ncons,1 , fp);
@@ -1224,12 +1216,10 @@ static int class_read(struct policydb *p, struct hashtab *h, void *fp)if (rc) goto bad; - rc =0 ; -out: -return rc; +return 0 ; bad:cls_destroy (key, cladatum,NULL ); - goto out; +return rc; }static int role_read (struct policydb *p,struct hashtab *h,void *fp)
@@ -1240,17 +1230,16 @@ static int role_read(struct policydb *p, struct hashtab *h, void *fp)__le32 buf[3 ];u32 len; + rc = -ENOMEM ; role =kzalloc (sizeof (*role),GFP_KERNEL ); -if (!role) { - rc = -ENOMEM ; - goto out; - } +if (!role) + goto bad;if (p->policyvers >=POLICYDB_VERSION_BOUNDARY ) to_read =3 ; rc =next_entry (buf, fp,sizeof (buf[0 ]) * to_read); -if (rc <0 ) +if (rc) goto bad; len =le32_to_cpu (buf[0 ]);
@@ -1258,13 +1247,13 @@ static int role_read(struct policydb *p, struct hashtab *h, void *fp)if (p->policyvers >=POLICYDB_VERSION_BOUNDARY ) role->bounds =le32_to_cpu (buf[2 ]); + rc = -ENOMEM ; key =kmalloc (len +1 ,GFP_KERNEL ); -if (!key) { - rc = -ENOMEM ; +if (!key) goto bad; - } + rc =next_entry (key, fp, len); -if (rc <0 ) +if (rc) goto bad; key[len] ='\0' ;
@@ -1277,10 +1266,10 @@ static int role_read(struct policydb *p, struct hashtab *h, void *fp) goto bad;if (strcmp (key,OBJECT_R ) ==0 ) { + rc = -EINVAL ;if (role->value !=OBJECT_R_VAL ) {printk (KERN_ERR "SELinux: Role %s has wrong value %d\n" ,OBJECT_R , role->value); - rc = -EINVAL ; goto bad; } rc =0 ;
@@ -1290,11 +1279,10 @@ static int role_read(struct policydb *p, struct hashtab *h, void *fp) rc =hashtab_insert (h, key, role);if (rc) goto bad; -out: -return rc; +return 0 ; bad:role_destroy (key, role,NULL ); - goto out; +return rc; }static int type_read (struct policydb *p,struct hashtab *h,void *fp)
@@ -1305,17 +1293,16 @@ static int type_read(struct policydb *p, struct hashtab *h, void *fp)__le32 buf[4 ];u32 len; + rc = -ENOMEM ; typdatum =kzalloc (sizeof (*typdatum),GFP_KERNEL ); -if (!typdatum) { - rc = -ENOMEM ; -return rc; - } +if (!typdatum) + goto bad;if (p->policyvers >=POLICYDB_VERSION_BOUNDARY ) to_read =4 ; rc =next_entry (buf, fp,sizeof (buf[0 ]) * to_read); -if (rc <0 ) +if (rc) goto bad; len =le32_to_cpu (buf[0 ]);
@@ -1333,24 +1320,22 @@ static int type_read(struct policydb *p, struct hashtab *h, void *fp) typdatum->primary =le32_to_cpu (buf[2 ]); } + rc = -ENOMEM ; key =kmalloc (len +1 ,GFP_KERNEL ); -if (!key) { - rc = -ENOMEM ; +if (!key) goto bad; - } rc =next_entry (key, fp, len); -if (rc <0 ) +if (rc) goto bad; key[len] ='\0' ; rc =hashtab_insert (h, key, typdatum);if (rc) goto bad; -out: -return rc; +return 0 ; bad:type_destroy (key, typdatum,NULL ); - goto out; +return rc; }
@@ -1366,22 +1351,18 @@ static int mls_read_level(struct mls_level *lp, void *fp)memset (lp,0 ,sizeof (*lp)); rc =next_entry (buf, fp,sizeof buf); -if (rc <0 ) { +if (rc) {printk (KERN_ERR "SELinux: mls: truncated level\n" ); - goto bad; +return rc; } lp->sens =le32_to_cpu (buf[0 ]); -if (ebitmap_read (&lp->cat, fp)) { -printk (KERN_ERR "SELinux: mls: error reading level " -"categories\n" ); - goto bad; + rc =ebitmap_read (&lp->cat, fp); +if (rc) { +printk (KERN_ERR "SELinux: mls: error reading level categories\n" ); +return rc; } -return 0 ; - -bad: -return -EINVAL ; }static int user_read (struct policydb *p,struct hashtab *h,void *fp)
@@ -1392,17 +1373,16 @@ static int user_read(struct policydb *p, struct hashtab *h, void *fp)__le32 buf[3 ];u32 len; + rc = -ENOMEM ; usrdatum =kzalloc (sizeof (*usrdatum),GFP_KERNEL ); -if (!usrdatum) { - rc = -ENOMEM ; - goto out; - } +if (!usrdatum) + goto bad;if (p->policyvers >=POLICYDB_VERSION_BOUNDARY ) to_read =3 ; rc =next_entry (buf, fp,sizeof (buf[0 ]) * to_read); -if (rc <0 ) +if (rc) goto bad; len =le32_to_cpu (buf[0 ]);
@@ -1410,13 +1390,12 @@ static int user_read(struct policydb *p, struct hashtab *h, void *fp)if (p->policyvers >=POLICYDB_VERSION_BOUNDARY ) usrdatum->bounds =le32_to_cpu (buf[2 ]); + rc = -ENOMEM ; key =kmalloc (len +1 ,GFP_KERNEL ); -if (!key) { - rc = -ENOMEM ; +if (!key) goto bad; - } rc =next_entry (key, fp, len); -if (rc <0 ) +if (rc) goto bad; key[len] ='\0' ;
@@ -1436,11 +1415,10 @@ static int user_read(struct policydb *p, struct hashtab *h, void *fp) rc =hashtab_insert (h, key, usrdatum);if (rc) goto bad; -out: -return rc; +return 0 ; bad:user_destroy (key, usrdatum,NULL ); - goto out; +return rc; }static int sens_read (struct policydb *p,struct hashtab *h,void *fp)
@@ -1451,47 +1429,43 @@ static int sens_read(struct policydb *p, struct hashtab *h, void *fp)__le32 buf[2 ];u32 len; + rc = -ENOMEM ; levdatum =kzalloc (sizeof (*levdatum),GFP_ATOMIC ); -if (!levdatum) { - rc = -ENOMEM ; - goto out; - } +if (!levdatum) + goto bad; rc =next_entry (buf, fp,sizeof buf); -if (rc <0 ) +if (rc) goto bad; len =le32_to_cpu (buf[0 ]); levdatum->isalias =le32_to_cpu (buf[1 ]); + rc = -ENOMEM ; key =kmalloc (len +1 ,GFP_ATOMIC ); -if (!key) { - rc = -ENOMEM ; +if (!key) goto bad; - } rc =next_entry (key, fp, len); -if (rc <0 ) +if (rc) goto bad; key[len] ='\0' ; + rc = -ENOMEM ; levdatum->level =kmalloc (sizeof (struct mls_level ),GFP_ATOMIC ); -if (!levdatum->level) { - rc = -ENOMEM ; +if (!levdatum->level) goto bad; - } -if (mls_read_level (levdatum->level, fp)) { - rc = -EINVAL ; + + rc =mls_read_level (levdatum->level, fp); +if (rc) goto bad; - } rc =hashtab_insert (h, key, levdatum);if (rc) goto bad; -out: -return rc; +return 0 ; bad:sens_destroy (key, levdatum,NULL ); - goto out; +return rc; }static int cat_read (struct policydb *p,struct hashtab *h,void *fp)
@@ -1502,39 +1476,35 @@ static int cat_read(struct policydb *p, struct hashtab *h, void *fp)__le32 buf[3 ];u32 len; + rc = -ENOMEM ; catdatum =kzalloc (sizeof (*catdatum),GFP_ATOMIC ); -if (!catdatum) { - rc = -ENOMEM ; - goto out; - } +if (!catdatum) + goto bad; rc =next_entry (buf, fp,sizeof buf); -if (rc <0 ) +if (rc) goto bad; len =le32_to_cpu (buf[0 ]); catdatum->value =le32_to_cpu (buf[1 ]); catdatum->isalias =le32_to_cpu (buf[2 ]); + rc = -ENOMEM ; key =kmalloc (len +1 ,GFP_ATOMIC ); -if (!key) { - rc = -ENOMEM ; +if (!key) goto bad; - } rc =next_entry (key, fp, len); -if (rc <0 ) +if (rc) goto bad; key[len] ='\0' ; rc =hashtab_insert (h, key, catdatum);if (rc) goto bad; -out: -return rc; - +return 0 ; bad:cat_destroy (key, catdatum,NULL ); - goto out; +return rc; } static int (*read_f[SYM_NUM]) (struct policydb *p, struct hashtab *h, void *fp) =
@@ -2056,13 +2026,14 @@ int policydb_read(struct policydb *p, void *fp) rc =policydb_init (p);if (rc) - goto out; +return rc;/* Read the magic number and string length. */ rc =next_entry (buf, fp,sizeof (u32) *2 ); -if (rc <0 ) +if (rc) goto bad; + rc = -EINVAL ;if (le32_to_cpu (buf[0 ]) !=POLICYDB_MAGIC ) { printk(KERN_ERR "SELinux: policydb magic number 0x%x does " "not match expected magic number 0x%x\n",
@@ -2070,6 +2041,7 @@ int policydb_read(struct policydb *p, void *fp) goto bad; } + rc = -EINVAL ; len =le32_to_cpu (buf[1 ]);if (len !=strlen (POLICYDB_STRING )) { printk(KERN_ERR "SELinux: policydb string length %d does not "
@@ -2077,19 +2049,23 @@ int policydb_read(struct policydb *p, void *fp) len, strlen(POLICYDB_STRING)); goto bad; } + + rc = -ENOMEM ; policydb_str =kmalloc (len +1 ,GFP_KERNEL );if (!policydb_str) {printk (KERN_ERR "SELinux: unable to allocate memory for policydb " "string of length %d\n" , len); - rc = -ENOMEM ; goto bad; } + rc =next_entry (policydb_str, fp, len); -if (rc <0 ) { +if (rc) {printk (KERN_ERR "SELinux: truncated policydb string identifier\n" );kfree (policydb_str); goto bad; } + + rc = -EINVAL ; policydb_str[len] ='\0' ;if (strcmp (policydb_str,POLICYDB_STRING )) { printk(KERN_ERR "SELinux: policydb string %s does not match "
@@ -2103,9 +2079,10 @@ int policydb_read(struct policydb *p, void *fp)/* Read the version and table sizes. */ rc =next_entry (buf, fp,sizeof (u32)*4 ); -if (rc <0 ) +if (rc) goto bad; + rc = -EINVAL ; p->policyvers =le32_to_cpu (buf[0 ]);if (p->policyvers <POLICYDB_VERSION_MIN || p->policyvers >POLICYDB_VERSION_MAX ) {
@@ -2118,6 +2095,7 @@ int policydb_read(struct policydb *p, void *fp)if ((le32_to_cpu (buf[1 ]) &POLICYDB_CONFIG_MLS )) { p->mls_enabled =1 ; + rc = -EINVAL ;if (p->policyvers <POLICYDB_VERSION_MLS ) { printk(KERN_ERR "SELinux: security policydb version %d " "(MLS) not backwards compatible\n",
@@ -2128,14 +2106,19 @@ int policydb_read(struct policydb *p, void *fp) p->reject_unknown = !!(le32_to_cpu (buf[1 ]) &REJECT_UNKNOWN ); p->allow_unknown = !!(le32_to_cpu (buf[1 ]) &ALLOW_UNKNOWN ); -if (p->policyvers >=POLICYDB_VERSION_POLCAP && -ebitmap_read (&p->policycaps, fp) !=0 ) - goto bad; +if (p->policyvers >=POLICYDB_VERSION_POLCAP ) { + rc =ebitmap_read (&p->policycaps, fp); +if (rc) + goto bad; + } -if (p->policyvers >=POLICYDB_VERSION_PERMISSIVE && -ebitmap_read (&p->permissive_map, fp) !=0 ) - goto bad; +if (p->policyvers >=POLICYDB_VERSION_PERMISSIVE ) { + rc =ebitmap_read (&p->permissive_map, fp); +if (rc) + goto bad; + } + rc = -EINVAL ; info =policydb_lookup_compat (p->policyvers);if (!info) { printk(KERN_ERR "SELinux: unable to find policy compat info "
@@ -2143,6 +2126,7 @@ int policydb_read(struct policydb *p, void *fp) goto bad; } + rc = -EINVAL ;if (le32_to_cpu (buf[2 ]) != info->sym_num ||le32_to_cpu (buf[3 ]) != info->ocon_num) { printk(KERN_ERR "SELinux: policydb table sizes (%d,%d) do "
@@ -2154,7 +2138,7 @@ int policydb_read(struct policydb *p, void *fp)for (i =0 ; i < info->sym_num; i++) { rc =next_entry (buf, fp,sizeof (u32)*2 ); -if (rc <0 ) +if (rc) goto bad; nprim =le32_to_cpu (buf[0 ]); nel =le32_to_cpu (buf[1 ]);
@@ -2178,60 +2162,58 @@ int policydb_read(struct policydb *p, void *fp) } rc =next_entry (buf, fp,sizeof (u32)); -if (rc <0 ) +if (rc) goto bad; nel =le32_to_cpu (buf[0 ]); ltr =NULL ;for (i =0 ; i < nel; i++) { + rc = -ENOMEM ; tr =kzalloc (sizeof (*tr),GFP_KERNEL ); -if (!tr) { - rc = -ENOMEM ; +if (!tr) goto bad; - }if (ltr) ltr->next = tr;else p->role_tr = tr; rc =next_entry (buf, fp,sizeof (u32)*3 ); -if (rc <0 ) +if (rc) goto bad; + + rc = -EINVAL ; tr->role =le32_to_cpu (buf[0 ]); tr->type =le32_to_cpu (buf[1 ]); tr->new_role =le32_to_cpu (buf[2 ]);if (!policydb_role_isvalid (p, tr->role) || !policydb_type_isvalid (p, tr->type) || - !policydb_role_isvalid (p, tr->new_role)) { - rc = -EINVAL ; + !policydb_role_isvalid (p, tr->new_role)) goto bad; - } ltr = tr; } rc =next_entry (buf, fp,sizeof (u32)); -if (rc <0 ) +if (rc) goto bad; nel =le32_to_cpu (buf[0 ]); lra =NULL ;for (i =0 ; i < nel; i++) { + rc = -ENOMEM ; ra =kzalloc (sizeof (*ra),GFP_KERNEL ); -if (!ra) { - rc = -ENOMEM ; +if (!ra) goto bad; - }if (lra) lra->next = ra;else p->role_allow = ra; rc =next_entry (buf, fp,sizeof (u32)*2 ); -if (rc <0 ) +if (rc) goto bad; + + rc = -EINVAL ; ra->role =le32_to_cpu (buf[0 ]); ra->new_role =le32_to_cpu (buf[1 ]);if (!policydb_role_isvalid (p, ra->role) || - !policydb_role_isvalid (p, ra->new_role)) { - rc = -EINVAL ; + !policydb_role_isvalid (p, ra->new_role)) goto bad; - } lra = ra; }
@@ -2243,13 +2225,14 @@ int policydb_read(struct policydb *p, void *fp)if (rc) goto bad; + rc = -EINVAL ; p->process_class =string_to_security_class (p,"process" );if (!p->process_class) goto bad; - p->process_trans_perms =string_to_av_perm (p, p->process_class, -"transition" ); - p->process_trans_perms |=string_to_av_perm (p, p->process_class, -"dyntransition" ); + + rc = -EINVAL ; + p->process_trans_perms =string_to_av_perm (p, p->process_class,"transition" ); + p->process_trans_perms |=string_to_av_perm (p, p->process_class,"dyntransition" );if (!p->process_trans_perms) goto bad;
@@ -2302,8 +2285,6 @@ int policydb_read(struct policydb *p, void *fp) out:return rc; bad: -if (!rc) - rc = -EINVAL ;policydb_destroy (p); goto out; }
@@ -3066,7 +3047,7 @@ int policydb_write(struct policydb *p, void *fp)if (!info) {printk (KERN_ERR "SELinux: compatibility lookup failed for policy " "version %d" , p->policyvers); -return rc; +return -EINVAL ; } buf[0 ] =cpu_to_le32 (p->policyvers);
--
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: standardize return code handling in selinuxfs.c
selinuxfs.c has lots of different standards on how to handle return paths on error. For the most part transition to
rc=errno if (failure) goto out; [...] out: cleanup() return rc;
Instead of doing cleanup mid function, or having multiple returns or other options. This doesn't do that for every function, but most of the complex functions which have cleanup routines on error.
---
security/selinux/selinuxfs.c | 652 ++++++++++++++++++++---------------------- 1 files changed, 317 insertions(+), 335 deletions(-)
security/selinux/selinuxfs.c +317 -335
@@ -140,19 +140,24 @@ static ssize_t sel_write_enforce(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { - char *page; + char *page = NULL;ssize_t length;int new_value; + length = -ENOMEM ;if (count >=PAGE_SIZE ) -return -ENOMEM ; -if (*ppos !=0 ) { -/* No partial writes. */ -return -EINVAL ; - } + goto out; + +/* No partial writes. */ + length =EINVAL ; +if (*ppos !=0 ) + goto out; + + length = -ENOMEM ; page = (char *)get_zeroed_page (GFP_KERNEL );if (!page) -return -ENOMEM ; + goto out; + length = -EFAULT ;if (copy_from_user (page, buf, count)) goto out;
@@ -177,7 +182,8 @@ static ssize_t sel_write_enforce(struct file *file, const char __user *buf, } length = count; out: -free_page ((unsigned long ) page); +if (page) +free_page ((unsigned long ) page);return length; }#else
@@ -213,20 +219,25 @@ static ssize_t sel_write_disable(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { - char *page; + char *page = NULL;ssize_t length;int new_value;extern int selinux_disable (void ); + length = -ENOMEM ;if (count >=PAGE_SIZE ) -return -ENOMEM ; -if (*ppos !=0 ) { -/* No partial writes. */ -return -EINVAL ; - } + goto out;; + +/* No partial writes. */ + length = -EINVAL ; +if (*ppos !=0 ) + goto out; + + length = -ENOMEM ; page = (char *)get_zeroed_page (GFP_KERNEL );if (!page) -return -ENOMEM ; + goto out; + length = -EFAULT ;if (copy_from_user (page, buf, count)) goto out;
@@ -237,7 +248,7 @@ static ssize_t sel_write_disable(struct file *file, const char __user *buf,if (new_value) { length =selinux_disable (); -if (length <0 ) +if (length) goto out; audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS, "selinux=0 auid=%u ses=%u",
@@ -247,7 +258,8 @@ static ssize_t sel_write_disable(struct file *file, const char __user *buf, length = count; out: -free_page ((unsigned long ) page); +if (page) +free_page ((unsigned long ) page); return length; }#else
@@ -438,7 +450,6 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { - int ret; ssize_t length;void *data =NULL ;
@@ -448,17 +459,19 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,if (length) goto out; -if (*ppos !=0 ) { -/* No partial writes. */ - length = -EINVAL ; +/* No partial writes. */ + length = -EINVAL ; +if (*ppos !=0 ) goto out; - } -if ((count >64 *1024 *1024 ) - || (data =vmalloc (count)) ==NULL ) { - length = -ENOMEM ; + length = -EFBIG ; +if (count >64 *1024 *1024 ) + goto out; + + length = -ENOMEM ; + data =vmalloc (count); +if (!data) goto out; - } length = -EFAULT ;if (copy_from_user (data, buf, count) !=0 )
@@ -468,23 +481,19 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,if (length) goto out; - ret =sel_make_bools (); -if (ret) { - length = ret; + length =sel_make_bools (); +if (length) goto out1; - } - ret =sel_make_classes (); -if (ret) { - length = ret; + length =sel_make_classes (); +if (length) goto out1; - } - ret =sel_make_policycap (); -if (ret) - length = ret; -else - length = count; + length =sel_make_policycap (); +if (length) + goto out1; + + length = count; out1: audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
@@ -504,26 +513,26 @@ static const struct file_operations sel_load_ops = {static ssize_t sel_write_context (struct file *file,char *buf,size_t size) { -char *canon; +char *canon =NULL ;u32 sid, len;ssize_t length; length =task_has_security (current,SECURITY__CHECK_CONTEXT );if (length) -return length; + goto out; length =security_context_to_sid (buf, size, &sid); -if (length <0 ) -return length; +if (length) + goto out; length =security_sid_to_context (sid, &canon, &len); -if (length <0 ) -return length; +if (length) + goto out; + length = -ERANGE ;if (len >SIMPLE_TRANSACTION_LIMIT ) {printk (KERN_ERR "SELinux: %s: context size (%u) exceeds " "payload max\n" , __func__, len); - length = -ERANGE ; goto out; }
@@ -547,23 +556,28 @@ static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,static ssize_t sel_write_checkreqprot (struct file *file,const char __user *buf,size_t count,loff_t *ppos) { -char *page; +char *page =NULL ;ssize_t length;unsigned int new_value; length =task_has_security (current,SECURITY__SETCHECKREQPROT );if (length) -return length; + goto out; + length = -ENOMEM ;if (count >=PAGE_SIZE ) -return -ENOMEM ; -if (*ppos !=0 ) { -/* No partial writes. */ -return -EINVAL ; - } + goto out; + +/* No partial writes. */ + length = -EINVAL ; +if (*ppos !=0 ) + goto out; + + length = -ENOMEM ; page = (char *)get_zeroed_page (GFP_KERNEL );if (!page) -return -ENOMEM ; + goto out; + length = -EFAULT ;if (copy_from_user (page, buf, count)) goto out;
@@ -575,7 +589,8 @@ static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, selinux_checkreqprot = new_value ?1 :0 ; length = count; out: -free_page ((unsigned long ) page); +if (page) +free_page ((unsigned long ) page);return length; }static const struct file_operations sel_checkreqprot_ops = {
@@ -638,7 +653,7 @@ static const struct file_operations transaction_ops = {static ssize_t sel_write_access (struct file *file,char *buf,size_t size) { -char *scon, *tcon; +char *scon =NULL , *tcon =NULL ;u32 ssid, tsid;u16 tclass;struct av_decision avd;
@@ -646,27 +661,29 @@ static ssize_t sel_write_access(struct file *file, char *buf, size_t size) length =task_has_security (current,SECURITY__COMPUTE_AV );if (length) -return length; + goto out; length = -ENOMEM ; scon =kzalloc (size +1 ,GFP_KERNEL );if (!scon) -return length; + goto out; + length = -ENOMEM ; tcon =kzalloc (size +1 ,GFP_KERNEL );if (!tcon) goto out; length = -EINVAL ;if (sscanf (buf,"%s %s %hu" , scon, tcon, &tclass) !=3 ) - goto out2; + goto out; length =security_context_to_sid (scon,strlen (scon) +1 , &ssid); -if (length <0 ) - goto out2; +if (length) + goto out; + length =security_context_to_sid (tcon,strlen (tcon) +1 , &tsid); -if (length <0 ) - goto out2; +if (length) + goto out;security_compute_av_user (ssid, tsid, tclass, &avd);
@@ -675,133 +692,131 @@ static ssize_t sel_write_access(struct file *file, char *buf, size_t size) avd.allowed,0xffffffff , avd.auditallow, avd.auditdeny, avd.seqno, avd.flags); -out2: -kfree (tcon); out: +kfree (tcon);kfree (scon);return length; }static ssize_t sel_write_create (struct file *file,char *buf,size_t size) { -char *scon, *tcon; +char *scon =NULL , *tcon =NULL ;u32 ssid, tsid, newsid;u16 tclass;ssize_t length; -char *newcon; +char *newcon =NULL ;u32 len; length =task_has_security (current,SECURITY__COMPUTE_CREATE );if (length) -return length; + goto out; length = -ENOMEM ; scon =kzalloc (size +1 ,GFP_KERNEL );if (!scon) -return length; + goto out; + length = -ENOMEM ; tcon =kzalloc (size +1 ,GFP_KERNEL );if (!tcon) goto out; length = -EINVAL ;if (sscanf (buf,"%s %s %hu" , scon, tcon, &tclass) !=3 ) - goto out2; + goto out; length =security_context_to_sid (scon,strlen (scon) +1 , &ssid); -if (length <0 ) - goto out2; +if (length) + goto out; + length =security_context_to_sid (tcon,strlen (tcon) +1 , &tsid); -if (length <0 ) - goto out2; +if (length) + goto out; length =security_transition_sid_user (ssid, tsid, tclass, &newsid); -if (length <0 ) - goto out2; +if (length) + goto out; length =security_sid_to_context (newsid, &newcon, &len); -if (length <0 ) - goto out2; +if (length) + goto out; + length = -ERANGE ;if (len >SIMPLE_TRANSACTION_LIMIT ) {printk (KERN_ERR "SELinux: %s: context size (%u) exceeds " "payload max\n" , __func__, len); - length = -ERANGE ; - goto out3; + goto out; }memcpy (buf, newcon, len); length = len; -out3: +out:kfree (newcon); -out2:kfree (tcon); -out:kfree (scon);return length; }static ssize_t sel_write_relabel (struct file *file,char *buf,size_t size) { -char *scon, *tcon; +char *scon =NULL , *tcon =NULL ;u32 ssid, tsid, newsid;u16 tclass;ssize_t length; -char *newcon; +char *newcon =NULL ;u32 len; length =task_has_security (current,SECURITY__COMPUTE_RELABEL );if (length) -return length; + goto out; length = -ENOMEM ; scon =kzalloc (size +1 ,GFP_KERNEL );if (!scon) -return length; + goto out; + length = -ENOMEM ; tcon =kzalloc (size +1 ,GFP_KERNEL );if (!tcon) goto out; length = -EINVAL ;if (sscanf (buf,"%s %s %hu" , scon, tcon, &tclass) !=3 ) - goto out2; + goto out; length =security_context_to_sid (scon,strlen (scon) +1 , &ssid); -if (length <0 ) - goto out2; +if (length) + goto out; + length =security_context_to_sid (tcon,strlen (tcon) +1 , &tsid); -if (length <0 ) - goto out2; +if (length) + goto out; length =security_change_sid (ssid, tsid, tclass, &newsid); -if (length <0 ) - goto out2; +if (length) + goto out; length =security_sid_to_context (newsid, &newcon, &len); -if (length <0 ) - goto out2; +if (length) + goto out; -if (len >SIMPLE_TRANSACTION_LIMIT ) { - length = -ERANGE ; - goto out3; - } + length = -ERANGE ; +if (len >SIMPLE_TRANSACTION_LIMIT ) + goto out;memcpy (buf, newcon, len); length = len; -out3: +out:kfree (newcon); -out2:kfree (tcon); -out:kfree (scon);return length; }static ssize_t sel_write_user (struct file *file,char *buf,size_t size) { -char *con, *user, *ptr; -u32 sid, *sids; +char *con =NULL , *user =NULL , *ptr; +u32 sid, *sids =NULL ;ssize_t length;char *newcon;int i, rc;
@@ -809,28 +824,29 @@ static ssize_t sel_write_user(struct file *file, char *buf, size_t size) length =task_has_security (current,SECURITY__COMPUTE_USER );if (length) -return length; + goto out;; length = -ENOMEM ; con =kzalloc (size +1 ,GFP_KERNEL );if (!con) -return length; + goto out;; + length = -ENOMEM ; user =kzalloc (size +1 ,GFP_KERNEL );if (!user) goto out; length = -EINVAL ;if (sscanf (buf,"%s %s" , con, user) !=2 ) - goto out2; + goto out; length =security_context_to_sid (con,strlen (con) +1 , &sid); -if (length <0 ) - goto out2; +if (length) + goto out; length =security_get_user_sids (sid, user, &sids, &nsids); -if (length <0 ) - goto out2; +if (length) + goto out; length =sprintf (buf,"%u" , nsids) +1 ; ptr = buf + length;
@@ -838,82 +854,80 @@ static ssize_t sel_write_user(struct file *file, char *buf, size_t size) rc =security_sid_to_context (sids[i], &newcon, &len);if (rc) { length = rc; - goto out3; + goto out; }if ((length + len) >=SIMPLE_TRANSACTION_LIMIT ) {kfree (newcon); length = -ERANGE ; - goto out3; + goto out; }memcpy (ptr, newcon, len);kfree (newcon); ptr += len; length += len; } -out3: +out:kfree (sids); -out2:kfree (user); -out:kfree (con);return length; }static ssize_t sel_write_member (struct file *file,char *buf,size_t size) { -char *scon, *tcon; +char *scon =NULL , *tcon =NULL ;u32 ssid, tsid, newsid;u16 tclass;ssize_t length; -char *newcon; +char *newcon =NULL ;u32 len; length =task_has_security (current,SECURITY__COMPUTE_MEMBER );if (length) -return length; + goto out; length = -ENOMEM ; scon =kzalloc (size +1 ,GFP_KERNEL );if (!scon) -return length; + goto out;; + length = -ENOMEM ; tcon =kzalloc (size +1 ,GFP_KERNEL );if (!tcon) goto out; length = -EINVAL ;if (sscanf (buf,"%s %s %hu" , scon, tcon, &tclass) !=3 ) - goto out2; + goto out; length =security_context_to_sid (scon,strlen (scon) +1 , &ssid); -if (length <0 ) - goto out2; +if (length) + goto out; + length =security_context_to_sid (tcon,strlen (tcon) +1 , &tsid); -if (length <0 ) - goto out2; +if (length) + goto out; length =security_member_sid (ssid, tsid, tclass, &newsid); -if (length <0 ) - goto out2; +if (length) + goto out; length =security_sid_to_context (newsid, &newcon, &len); -if (length <0 ) - goto out2; +if (length) + goto out; + length = -ERANGE ;if (len >SIMPLE_TRANSACTION_LIMIT ) {printk (KERN_ERR "SELinux: %s: context size (%u) exceeds " "payload max\n" , __func__, len); - length = -ERANGE ; - goto out3; + goto out; }memcpy (buf, newcon, len); length = len; -out3: +out:kfree (newcon); -out2:kfree (tcon); -out:kfree (scon);return length; }
@@ -942,16 +956,14 @@ static ssize_t sel_read_bool(struct file *filep, char __user *buf,mutex_lock (&sel_mutex); -if (index >= bool_num ||strcmp (name, bool_pending_names[index])) { - ret = -EINVAL ; + ret = -EINVAL ; +if (index >= bool_num ||strcmp (name, bool_pending_names[index])) goto out; - } + ret = -ENOMEM ; page = (char *)get_zeroed_page (GFP_KERNEL ); -if (!page) { - ret = -ENOMEM ; +if (!page) goto out; - } cur_enforcing =security_get_bool_value (index);if (cur_enforcing <0 ) {
@@ -984,26 +996,23 @@ static ssize_t sel_write_bool(struct file *filep, const char __user *buf,if (length) goto out; -if (index >= bool_num ||strcmp (name, bool_pending_names[index])) { - length = -EINVAL ; + length = -EINVAL ; +if (index >= bool_num ||strcmp (name, bool_pending_names[index])) goto out; - } -if (count >=PAGE_SIZE ) { - length = -ENOMEM ; + length = -ENOMEM ; +if (count >=PAGE_SIZE ) goto out; - } -if (*ppos !=0 ) { -/* No partial writes. */ - length = -EINVAL ; +/* No partial writes. */ + length = -EINVAL ; +if (*ppos !=0 ) goto out; - } + + length = -ENOMEM ; page = (char *)get_zeroed_page (GFP_KERNEL ); -if (!page) { - length = -ENOMEM ; +if (!page) goto out; - } length = -EFAULT ;if (copy_from_user (page, buf, count))
@@ -1046,19 +1055,19 @@ static ssize_t sel_commit_bools_write(struct file *filep,if (length) goto out; -if (count >=PAGE_SIZE ) { - length = -ENOMEM ; + length = -ENOMEM ; +if (count >=PAGE_SIZE ) goto out; - } -if (*ppos !=0 ) { -/* No partial writes. */ + +/* No partial writes. */ + length = -EINVAL ; +if (*ppos !=0 ) goto out; - } + + length = -ENOMEM ; page = (char *)get_zeroed_page (GFP_KERNEL ); -if (!page) { - length = -ENOMEM ; +if (!page) goto out; - } length = -EFAULT ;if (copy_from_user (page, buf, count))
@@ -1068,10 +1077,12 @@ static ssize_t sel_commit_bools_write(struct file *filep,if (sscanf (page,"%d" , &new_value) !=1 ) goto out; + length =0 ;if (new_value && bool_pending_values) -security_set_bools (bool_num, bool_pending_values); + length =security_set_bools (bool_num, bool_pending_values); - length = count; +if (!length) + length = count; out:mutex_unlock (&sel_mutex);
@@ -1113,7 +1124,7 @@ static void sel_remove_entries(struct dentry *de)static int sel_make_bools (void ) { -int i, ret =0 ; +int i, ret;ssize_t len;struct dentry *dentry =NULL ;struct dentry *dir = bool_dir;
@@ -1134,38 +1145,40 @@ static int sel_make_bools(void)sel_remove_entries (dir); + ret = -ENOMEM ; page = (char *)get_zeroed_page (GFP_KERNEL );if (!page) -return -ENOMEM ; + goto out; ret =security_get_bools (&num, &names, &values); -if (ret !=0 ) +if (ret) goto out;for (i =0 ; i < num; i++) { + ret = -ENOMEM ; dentry =d_alloc_name (dir, names[i]); -if (!dentry) { - ret = -ENOMEM ; - goto err; - } +if (!dentry) + goto out; + + ret = -ENOMEM ; inode =sel_make_inode (dir->d_sb,S_IFREG |S_IRUGO |S_IWUSR ); -if (!inode) { - ret = -ENOMEM ; - goto err; - } +if (!inode) + goto out; + ret = -EINVAL ; len =snprintf (page,PAGE_SIZE ,"/%s/%s" ,BOOL_DIR_NAME , names[i]); -if (len <0 ) { - ret = -EINVAL ; - goto err; - }else if (len >=PAGE_SIZE ) { - ret = -ENAMETOOLONG ; - goto err; - } +if (len <0 ) + goto out; + + ret = -ENAMETOOLONG ; +if (len >=PAGE_SIZE ) + goto out; + isec = (struct inode_security_struct *)inode->i_security; ret =security_genfs_sid ("selinuxfs" , page,SECCLASS_FILE , &sid);if (ret) - goto err; + goto out; + isec->sid = sid; isec->initialized =1 ; inode->i_fop = &sel_bool_ops;
@@ -1175,10 +1188,13 @@ static int sel_make_bools(void) bool_num = num; bool_pending_names = names; bool_pending_values = values; -out: +free_page ((unsigned long )page); -return ret; -err: +return 0 ; +out: +if (page) +free_page ((unsigned long )page); +if (names) {for (i =0 ; i < num; i++)kfree (names[i]);
@@ -1186,8 +1202,8 @@ err: }kfree (values);sel_remove_entries (dir); - ret = -ENOMEM ; - goto out; + +return ret; }#define NULL_FILE_NAME "null"
@@ -1209,47 +1225,42 @@ static ssize_t sel_write_avc_cache_threshold(struct file *file, size_t count, loff_t *ppos) { - char *page; + char *page = NULL;ssize_t ret;int new_value; -if (count >=PAGE_SIZE ) { - ret = -ENOMEM ; + ret =task_has_security (current,SECURITY__SETSECPARAM ); +if (ret) goto out; - } -if (*ppos !=0 ) { -/* No partial writes. */ - ret = -EINVAL ; + ret = -ENOMEM ; +if (count >=PAGE_SIZE ) goto out; - } +/* No partial writes. */ + ret = -EINVAL ; +if (*ppos !=0 ) + goto out; + + ret = -ENOMEM ; page = (char *)get_zeroed_page (GFP_KERNEL ); -if (!page) { - ret = -ENOMEM ; +if (!page) goto out; - } -if (copy_from_user (page, buf, count)) { - ret = -EFAULT ; - goto out_free; - } + ret = -EFAULT ; +if (copy_from_user (page, buf, count)) + goto out; -if (sscanf (page,"%u" , &new_value) !=1 ) { - ret = -EINVAL ; + ret = -EINVAL ; +if (sscanf (page,"%u" , &new_value) !=1 ) goto out; - } -if (new_value != avc_cache_threshold) { - ret =task_has_security (current,SECURITY__SETSECPARAM ); -if (ret) - goto out_free; - avc_cache_threshold = new_value; - } + avc_cache_threshold = new_value; + ret = count; -out_free: -free_page ((unsigned long )page); out: +if (page) +free_page ((unsigned long )page);return ret; }
@@ -1257,19 +1268,18 @@ static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf, size_t count, loff_t *ppos) { char *page; -ssize_t ret =0 ; +ssize_t length; page = (char *)__get_free_page (GFP_KERNEL ); -if (!page) { - ret = -ENOMEM ; - goto out; - } - ret =avc_get_hash_stats (page); -if (ret >=0 ) - ret =simple_read_from_buffer (buf, count, ppos, page, ret); +if (!page) +return -ENOMEM ; + + length =avc_get_hash_stats (page); +if (length >=0 ) + length =simple_read_from_buffer (buf, count, ppos, page, length);free_page ((unsigned long )page); -out: -return ret; + +return length; } static const struct file_operations sel_avc_cache_threshold_ops = {
@@ -1351,7 +1361,7 @@ static const struct file_operations sel_avc_cache_stats_ops = {static int sel_make_avc_files (struct dentry *dir) { -int i, ret =0 ; +int i;static struct tree_descr files[] = { {"cache_threshold" , &sel_avc_cache_threshold_ops,S_IRUGO |S_IWUSR },
@@ -1366,22 +1376,19 @@ static int sel_make_avc_files(struct dentry *dir)struct dentry *dentry; dentry =d_alloc_name (dir, files[i].name); -if (!dentry) { - ret = -ENOMEM ; - goto out; - } +if (!dentry) +return -ENOMEM ; inode =sel_make_inode (dir->d_sb,S_IFREG |files[i].mode); -if (!inode) { - ret = -ENOMEM ; - goto out; - } +if (!inode) +return -ENOMEM ; + inode->i_fop = files[i].ops; inode->i_ino = ++sel_last_ino;d_add (dentry, inode); } -out: -return ret; + +return 0 ; } static ssize_t sel_read_initcon(struct file *file, char __user *buf,
@@ -1395,7 +1402,7 @@ static ssize_t sel_read_initcon(struct file *file, char __user *buf, inode = file->f_path.dentry->d_inode; sid = inode->i_ino&SEL_INO_MASK ; ret =security_sid_to_context (sid, &con, &len); -if (ret <0 ) +if (ret)return ret; ret =simple_read_from_buffer (buf, count, ppos, con, len);
@@ -1410,28 +1417,25 @@ static const struct file_operations sel_initcon_ops = {static int sel_make_initcon_files (struct dentry *dir) { -int i, ret =0 ; +int i;for (i =1 ; i <=SECINITSID_NUM ; i++) {struct inode *inode;struct dentry *dentry; dentry =d_alloc_name (dir,security_get_initial_sid_context (i)); -if (!dentry) { - ret = -ENOMEM ; - goto out; - } +if (!dentry) +return -ENOMEM ; inode =sel_make_inode (dir->d_sb,S_IFREG |S_IRUGO ); -if (!inode) { - ret = -ENOMEM ; - goto out; - } +if (!inode) +return -ENOMEM ; + inode->i_fop = &sel_initcon_ops; inode->i_ino = i|SEL_INITCON_INO_OFFSET ;d_add (dentry, inode); } -out: -return ret; + +return 0 ; }static inline unsigned int sel_div (unsigned long a,unsigned long b)
@@ -1467,15 +1471,13 @@ static ssize_t sel_read_class(struct file *file, char __user *buf,unsigned long ino = file->f_path.dentry->d_inode->i_ino; page = (char *)__get_free_page (GFP_KERNEL ); -if (!page) { - rc = -ENOMEM ; - goto out; - } +if (!page) +return -ENOMEM ; len =snprintf (page,PAGE_SIZE ,"%d" ,sel_ino_to_class (ino)); rc =simple_read_from_buffer (buf, count, ppos, page, len);free_page ((unsigned long )page); -out: +return rc; }
@@ -1492,15 +1494,13 @@ static ssize_t sel_read_perm(struct file *file, char __user *buf,unsigned long ino = file->f_path.dentry->d_inode->i_ino; page = (char *)__get_free_page (GFP_KERNEL ); -if (!page) { - rc = -ENOMEM ; - goto out; - } +if (!page) +return -ENOMEM ; len =snprintf (page,PAGE_SIZE ,"%d" ,sel_ino_to_perm (ino)); rc =simple_read_from_buffer (buf, count, ppos, page, len);free_page ((unsigned long )page); -out: +return rc; }
@@ -1531,39 +1531,37 @@ static const struct file_operations sel_policycap_ops = {static int sel_make_perm_files (char *objclass,int classvalue,struct dentry *dir) { -int i, rc =0 , nperms; +int i, rc, nperms;char **perms; rc =security_get_permissions (objclass, &perms, &nperms);if (rc) - goto out; +return rc;for (i =0 ; i < nperms; i++) {struct inode *inode;struct dentry *dentry; + rc = -ENOMEM ; dentry =d_alloc_name (dir, perms[i]); -if (!dentry) { - rc = -ENOMEM ; - goto out1; - } +if (!dentry) + goto out; + rc = -ENOMEM ; inode =sel_make_inode (dir->d_sb,S_IFREG |S_IRUGO ); -if (!inode) { - rc = -ENOMEM ; - goto out1; - } +if (!inode) + goto out; + inode->i_fop = &sel_perm_ops;/* i+1 since perm values are 1-indexed */ inode->i_ino =sel_perm_to_ino (classvalue, i +1 );d_add (dentry, inode); } - -out1: + rc =0 ; +out:for (i =0 ; i < nperms; i++)kfree (perms[i]);kfree (perms); -out:return rc; }
@@ -1575,34 +1573,27 @@ static int sel_make_class_dir_entries(char *classname, int index,int rc; dentry =d_alloc_name (dir,"index" ); -if (!dentry) { - rc = -ENOMEM ; - goto out; - } +if (!dentry) +return -ENOMEM ; inode =sel_make_inode (dir->d_sb,S_IFREG |S_IRUGO ); -if (!inode) { - rc = -ENOMEM ; - goto out; - } +if (!inode) +return -ENOMEM ; inode->i_fop = &sel_class_ops; inode->i_ino =sel_class_to_ino (index);d_add (dentry, inode); dentry =d_alloc_name (dir,"perms" ); -if (!dentry) { - rc = -ENOMEM ; - goto out; - } +if (!dentry) +return -ENOMEM ; rc =sel_make_dir (dir->d_inode, dentry, &last_class_ino);if (rc) - goto out; +return rc; rc =sel_make_perm_files (classname, index, dentry); -out:return rc; }
@@ -1632,15 +1623,15 @@ static void sel_remove_classes(void)static int sel_make_classes (void ) { -int rc =0 , nclasses, i; +int rc, nclasses, i;char **classes;/* delete any existing entries */ sel_remove_classes (); rc =security_get_classes (&classes, &nclasses); -if (rc <0 ) - goto out; +if (rc) +return rc;/* +2 since classes are 1-indexed */ last_class_ino =sel_class_to_ino (nclasses +2 );
@@ -1648,29 +1639,27 @@ static int sel_make_classes(void)for (i =0 ; i < nclasses; i++) {struct dentry *class_name_dir; + rc = -ENOMEM ; class_name_dir =d_alloc_name (class_dir, classes[i]); -if (!class_name_dir) { - rc = -ENOMEM ; - goto out1; - } +if (!class_name_dir) + goto out; rc =sel_make_dir (class_dir->d_inode, class_name_dir, &last_class_ino);if (rc) - goto out1; + goto out;/* i+1 since class values are 1-indexed */ rc =sel_make_class_dir_entries (classes[i], i +1 , class_name_dir);if (rc) - goto out1; + goto out; } - -out1: + rc =0 ; +out:for (i =0 ; i < nclasses; i++)kfree (classes[i]);kfree (classes); -out:return rc; }
@@ -1707,14 +1696,12 @@ static int sel_make_policycap(void)static int sel_make_dir (struct inode *dir,struct dentry *dentry,unsigned long *ino) { -int ret =0 ;struct inode *inode; inode =sel_make_inode (dir->i_sb,S_IFDIR |S_IRUGO |S_IXUGO ); -if (!inode) { - ret = -ENOMEM ; - goto out; - } +if (!inode) +return -ENOMEM ; + inode->i_op = &simple_dir_inode_operations; inode->i_fop = &simple_dir_operations; inode->i_ino = ++(*ino);
@@ -1723,8 +1710,8 @@ static int sel_make_dir(struct inode *dir, struct dentry *dentry,d_add (dentry, inode);/* bump link count on parent directory, too */ inc_nlink (dir); -out: -return ret; + +return 0 ; }static int sel_fill_super (struct super_block *sb,void *data,int silent)
@@ -1759,11 +1746,10 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent) root_inode = sb->s_root->d_inode; + ret = -ENOMEM ; dentry =d_alloc_name (sb->s_root,BOOL_DIR_NAME ); -if (!dentry) { - ret = -ENOMEM ; +if (!dentry) goto err; - } ret =sel_make_dir (root_inode, dentry, &sel_last_ino);if (ret)
@@ -1771,17 +1757,16 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent) bool_dir = dentry; + ret = -ENOMEM ; dentry =d_alloc_name (sb->s_root,NULL_FILE_NAME ); -if (!dentry) { - ret = -ENOMEM ; +if (!dentry) goto err; - } + ret = -ENOMEM ; inode =sel_make_inode (sb,S_IFCHR |S_IRUGO |S_IWUGO ); -if (!inode) { - ret = -ENOMEM ; +if (!inode) goto err; - } + inode->i_ino = ++sel_last_ino; isec = (struct inode_security_struct *)inode->i_security; isec->sid =SECINITSID_DEVNULL ;
@@ -1792,11 +1777,10 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent)d_add (dentry, inode); selinux_null = dentry; + ret = -ENOMEM ; dentry =d_alloc_name (sb->s_root,"avc" ); -if (!dentry) { - ret = -ENOMEM ; +if (!dentry) goto err; - } ret =sel_make_dir (root_inode, dentry, &sel_last_ino);if (ret)
@@ -1806,11 +1790,10 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent)if (ret) goto err; + ret = -ENOMEM ; dentry =d_alloc_name (sb->s_root,"initial_contexts" ); -if (!dentry) { - ret = -ENOMEM ; +if (!dentry) goto err; - } ret =sel_make_dir (root_inode, dentry, &sel_last_ino);if (ret)
@@ -1820,11 +1803,10 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent)if (ret) goto err; + ret = -ENOMEM ; dentry =d_alloc_name (sb->s_root,"class" ); -if (!dentry) { - ret = -ENOMEM ; +if (!dentry) goto err; - } ret =sel_make_dir (root_inode, dentry, &sel_last_ino);if (ret)
@@ -1832,11 +1814,10 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent) class_dir = dentry; + ret = -ENOMEM ; dentry =d_alloc_name (sb->s_root,"policy_capabilities" ); -if (!dentry) { - ret = -ENOMEM ; +if (!dentry) goto err; - } ret =sel_make_dir (root_inode, dentry, &sel_last_ino);if (ret)
@@ -1844,12 +1825,11 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent) policycap_dir = dentry; -out: -return ret; +return 0 ; err:printk (KERN_ERR "SELinux: %s: failed while creating inodes\n" , __func__); - goto out; +return ret; } static int sel_get_sb(struct file_system_type *fs_type,
@@ -1874,14 +1854,16 @@ static int __init init_sel_fs(void)if (!selinux_enabled)return 0 ; err =register_filesystem (&sel_fs_type); -if (!err) { - selinuxfs_mount =kern_mount (&sel_fs_type); -if (IS_ERR (selinuxfs_mount)) { -printk (KERN_ERR "selinuxfs: could not mount!\n" ); - err =PTR_ERR (selinuxfs_mount); - selinuxfs_mount =NULL ; - } +if (err) +return err; + + selinuxfs_mount =kern_mount (&sel_fs_type); +if (IS_ERR (selinuxfs_mount)) { +printk (KERN_ERR "selinuxfs: could not mount!\n" ); + err =PTR_ERR (selinuxfs_mount); + selinuxfs_mount =NULL ; } +return err; }
--
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: standardize return code handling in selinuxfs.c
In reply to Eric Parisselinuxfs.c has lots of different standards on how to handle return paths on error. For the most part transition to
rc=errno if (failure) goto out; [...] out: cleanup() return rc;
Instead of doing cleanup mid function, or having multiple returns or other options. This doesn't do that for every function, but most of the complex functions which have cleanup routines on error.
---
security/selinux/ss/services.c | 328 +++++++++++++++++++--------------------- 1 files changed, 157 insertions(+), 171 deletions(-)
security/selinux/ss/services.c +157 -171
@@ -700,11 +700,11 @@ static int security_validtrans_handle_fail(struct context *ocontext,char *o =NULL , *n =NULL , *t =NULL ;u32 olen, nlen, tlen; -if (context_struct_to_string (ocontext, &o, &olen) <0 ) +if (context_struct_to_string (ocontext, &o, &olen)) goto out; -if (context_struct_to_string (ncontext, &n, &nlen) <0 ) +if (context_struct_to_string (ncontext, &n, &nlen)) goto out; -if (context_struct_to_string (tcontext, &t, &tlen) <0 ) +if (context_struct_to_string (tcontext, &t, &tlen)) goto out;audit_log (current->audit_context,GFP_ATOMIC ,AUDIT_SELINUX_ERR , "security_validate_transition: denied for"
@@ -800,10 +800,11 @@ int security_bounded_transition(u32 old_sid, u32 new_sid)struct context *old_context, *new_context;struct type_datum *type;int index; -int rc = -EINVAL ; +int rc;read_lock (&policy_rwlock); + rc = -EINVAL ; old_context =sidtab_search (&sidtab, old_sid);if (!old_context) { printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
@@ -811,6 +812,7 @@ int security_bounded_transition(u32 old_sid, u32 new_sid) goto out; } + rc = -EINVAL ; new_context =sidtab_search (&sidtab, new_sid);if (!new_context) { printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
@@ -818,11 +820,10 @@ int security_bounded_transition(u32 old_sid, u32 new_sid) goto out; } + rc =0 ;/* type/domain unchanged */ -if (old_context->type == new_context->type) { - rc =0 ; +if (old_context->type == new_context->type) goto out; - } index = new_context->type;while (true) {
@@ -830,16 +831,15 @@ int security_bounded_transition(u32 old_sid, u32 new_sid)BUG_ON (!type);/* not bounded anymore */ -if (!type->bounds) { - rc = -EPERM ; + rc = -EPERM ; +if (!type->bounds)break ; - }/* @newsid is bounded by @oldsid */ -if (type->bounds == old_context->type) { - rc =0 ; + rc =0 ; +if (type->bounds == old_context->type)break ; - } + index = type->bounds; }
@@ -1179,16 +1179,13 @@ static int string_to_context_struct(struct policydb *pol,if (rc) goto out; -if ((p - scontext) < scontext_len) { - rc = -EINVAL ; + rc = -EINVAL ; +if ((p - scontext) < scontext_len) goto out; - }/* Check the validity of the new context. */ -if (!policydb_context_isvalid (pol, ctx)) { - rc = -EINVAL ; +if (!policydb_context_isvalid (pol, ctx)) goto out; - } rc =0 ; out:if (rc)
@@ -1227,27 +1224,26 @@ static int security_context_to_sid_core(const char *scontext, u32 scontext_len,if (force) {/* Save another copy for storing in uninterpreted form */ + rc = -ENOMEM ; str =kstrdup (scontext2, gfp_flags); -if (!str) { -kfree (scontext2); -return -ENOMEM ; - } +if (!str) + goto out; }read_lock (&policy_rwlock); - rc =string_to_context_struct (&policydb, &sidtab, - scontext2, scontext_len, - &context, def_sid); + rc =string_to_context_struct (&policydb, &sidtab, scontext2, + scontext_len, &context, def_sid);if (rc == -EINVAL && force) { context.str = str; context.len = scontext_len; str =NULL ; }else if (rc) - goto out; + goto out_unlock; rc =sidtab_context_to_sid (&sidtab, &context, sid);context_destroy (&context); -out: +out_unlock:read_unlock (&policy_rwlock); +out:kfree (scontext2);kfree (str);return rc;
@@ -1311,11 +1307,11 @@ static int compute_sid_handle_invalid_context(char *s =NULL , *t =NULL , *n =NULL ;u32 slen, tlen, nlen; -if (context_struct_to_string (scontext, &s, &slen) <0 ) +if (context_struct_to_string (scontext, &s, &slen)) goto out; -if (context_struct_to_string (tcontext, &t, &tlen) <0 ) +if (context_struct_to_string (tcontext, &t, &tlen)) goto out; -if (context_struct_to_string (newcontext, &n, &nlen) <0 ) +if (context_struct_to_string (newcontext, &n, &nlen)) goto out;audit_log (current->audit_context,GFP_ATOMIC ,AUDIT_SELINUX_ERR , "security_compute_sid: invalid context %s"
@@ -1561,22 +1557,17 @@ static int clone_sid(u32 sid,static inline int convert_context_handle_invalid_context (struct context *context) { -int rc =0 ; +char *s; +u32 len; -if (selinux_enforcing) { - rc = -EINVAL ; - }else { -char *s; -u32 len; - -if (!context_struct_to_string (context, &s, &len)) { -printk (KERN_WARNING -"SELinux: Context %s would be invalid if enforcing\n" , - s); -kfree (s); - } +if (selinux_enforcing) +return -EINVAL ; + +if (!context_struct_to_string (context, &s, &len)) { +printk (KERN_WARNING "SELinux: Context %s would be invalid if enforcing\n" , s); +kfree (s); } -return rc; +return 0 ; }struct convert_context_args {
@@ -1613,17 +1604,17 @@ static int convert_context(u32 key,if (c->str) {struct context ctx; + + rc = -ENOMEM ; s =kstrdup (c->str,GFP_KERNEL ); -if (!s) { - rc = -ENOMEM ; +if (!s) goto out; - } + rc =string_to_context_struct (args->newp,NULL , s, c->len, &ctx,SECSID_NULL );kfree (s);if (!rc) { -printk (KERN_INFO -"SELinux: Context %s became valid (mapped).\n" , +printk (KERN_INFO "SELinux: Context %s became valid (mapped).\n" , c->str);/* Replace string with mapped representation. */ kfree (c->str);
@@ -1635,8 +1626,7 @@ static int convert_context(u32 key, goto out; }else {/* Other error condition, e.g. ENOMEM. */ -printk (KERN_ERR -"SELinux: Unable to map context %s, rc = %d.\n" , +printk (KERN_ERR "SELinux: Unable to map context %s, rc = %d.\n" , c->str, -rc); goto out; }
@@ -1646,9 +1636,8 @@ static int convert_context(u32 key,if (rc) goto out; - rc = -EINVAL ; -/* Convert the user. */ + rc = -EINVAL ; usrdatum =hashtab_search (args->newp->p_users.table, args->oldp->p_user_val_to_name[c->user -1 ]);if (!usrdatum)
@@ -1656,6 +1645,7 @@ static int convert_context(u32 key, c->user = usrdatum->value;/* Convert the role. */ + rc = -EINVAL ; role =hashtab_search (args->newp->p_roles.table, args->oldp->p_role_val_to_name[c->role -1 ]);if (!role)
@@ -1663,6 +1653,7 @@ static int convert_context(u32 key, c->role = role->value;/* Convert the type. */ + rc = -EINVAL ; typdatum =hashtab_search (args->newp->p_types.table, args->oldp->p_type_val_to_name[c->type -1 ]);if (!typdatum)
@@ -1692,6 +1683,7 @@ static int convert_context(u32 key, oc = args->newp->ocontexts[OCON_ISID ];while (oc && oc->sid[0 ] !=SECINITSID_UNLABELED ) oc = oc->next; + rc = -EINVAL ;if (!oc) {printk (KERN_ERR "SELinux: unable to look up" " the initial SIDs list\n" );
@@ -1711,19 +1703,20 @@ static int convert_context(u32 key, }context_destroy (&oldc); + rc =0 ; out:return rc; bad:/* Map old representation to string and save it. */ -if (context_struct_to_string (&oldc, &s, &len)) -return -ENOMEM ; + rc =context_struct_to_string (&oldc, &s, &len); +if (rc) +return rc;context_destroy (&oldc);context_destroy (c); c->str = s; c->len = len; -printk (KERN_INFO -"SELinux: Context %s became invalid (unmapped).\n" , +printk (KERN_INFO "SELinux: Context %s became invalid (unmapped).\n" , c->str); rc =0 ; goto out;
@@ -1996,7 +1989,7 @@ int security_node_sid(u16 domain,u32 addrlen, u32 *out_sid) { - int rc = 0; + int rc;struct ocontext *c;read_lock (&policy_rwlock);
@@ -2005,10 +1998,9 @@ int security_node_sid(u16 domain,case AF_INET : {u32 addr; -if (addrlen !=sizeof (u32)) { - rc = -EINVAL ; + rc = -EINVAL ; +if (addrlen !=sizeof (u32)) goto out; - } addr = *((u32 *)addrp);
@@ -2022,10 +2014,9 @@ int security_node_sid(u16 domain, }case AF_INET6 : -if (addrlen !=sizeof (u64) *2 ) { - rc = -EINVAL ; + rc = -EINVAL ; +if (addrlen !=sizeof (u64) *2 ) goto out; - } c = policydb.ocontexts[OCON_NODE6 ];while (c) { if (match_ipv6_addrmask(addrp, c->u.node6.addr,
@@ -2036,6 +2027,7 @@ int security_node_sid(u16 domain,break ;default : + rc =0 ; *out_sid =SECINITSID_NODE ; goto out; }
@@ -2053,6 +2045,7 @@ int security_node_sid(u16 domain, *out_sid =SECINITSID_NODE ; } + rc =0 ; out:read_unlock (&policy_rwlock);return rc;
@@ -2097,24 +2090,22 @@ int security_get_user_sids(u32 fromsid,context_init (&usercon); + rc = -EINVAL ; fromcon =sidtab_search (&sidtab, fromsid); -if (!fromcon) { - rc = -EINVAL ; +if (!fromcon) goto out_unlock; - } + rc = -EINVAL ; user =hashtab_search (policydb.p_users.table, username); -if (!user) { - rc = -EINVAL ; +if (!user) goto out_unlock; - } + usercon.user = user->value; + rc = -ENOMEM ; mysids =kcalloc (maxnel,sizeof (*mysids),GFP_ATOMIC ); -if (!mysids) { - rc = -ENOMEM ; +if (!mysids) goto out_unlock; - }ebitmap_for_each_positive_bit (&user->roles, rnode, i) { role = policydb.role_val_to_struct[i];
@@ -2131,12 +2122,11 @@ int security_get_user_sids(u32 fromsid,if (mynel < maxnel) { mysids[mynel++] = sid; }else { + rc = -ENOMEM ; maxnel +=SIDS_NEL ; mysids2 =kcalloc (maxnel,sizeof (*mysids2),GFP_ATOMIC ); -if (!mysids2) { - rc = -ENOMEM ; +if (!mysids2) goto out_unlock; - }memcpy (mysids2, mysids, mynel *sizeof (*mysids2));kfree (mysids); mysids = mysids2;
@@ -2144,7 +2134,7 @@ int security_get_user_sids(u32 fromsid, } } } - + rc =0 ; out_unlock:read_unlock (&policy_rwlock);if (rc || !mynel) {
@@ -2152,9 +2142,9 @@ out_unlock: goto out; } + rc = -ENOMEM ; mysids2 =kcalloc (mynel,sizeof (*mysids2),GFP_KERNEL );if (!mysids2) { - rc = -ENOMEM ;kfree (mysids); goto out; }
@@ -2195,7 +2185,7 @@ int security_genfs_sid(const char *fstype,u16 sclass;struct genfs *genfs;struct ocontext *c; -int rc =0 , cmp =0 ; +int rc, cmp =0 ;while (path[0 ] =='/' && path[1 ] =='/' ) path++;
@@ -2203,6 +2193,7 @@ int security_genfs_sid(const char *fstype,read_lock (&policy_rwlock); sclass =unmap_class (orig_sclass); + *sid =SECINITSID_UNLABELED ;for (genfs = policydb.genfs; genfs; genfs = genfs->next) { cmp =strcmp (fstype, genfs->fstype);
@@ -2210,11 +2201,9 @@ int security_genfs_sid(const char *fstype,break ; } -if (!genfs || cmp) { - *sid =SECINITSID_UNLABELED ; - rc = -ENOENT ; + rc = -ENOENT ; +if (!genfs || cmp) goto out; - }for (c = genfs->head; c; c = c->next) { len =strlen (c->u.name);
@@ -2223,21 +2212,18 @@ int security_genfs_sid(const char *fstype,break ; } -if (!c) { - *sid =SECINITSID_UNLABELED ; - rc = -ENOENT ; + rc = -ENOENT ; +if (!c) goto out; - }if (!c->sid[0 ]) { - rc =sidtab_context_to_sid (&sidtab, - &c->context[0 ], - &c->sid[0 ]); + rc =sidtab_context_to_sid (&sidtab, &c->context[0 ], &c->sid[0 ]);if (rc) goto out; } *sid = c->sid[0 ]; + rc =0 ; out:read_unlock (&policy_rwlock);return rc;
@@ -2269,8 +2255,7 @@ int security_fs_use(if (c) { *behavior = c->v.behavior;if (!c->sid[0 ]) { - rc =sidtab_context_to_sid (&sidtab, - &c->context[0 ], + rc =sidtab_context_to_sid (&sidtab, &c->context[0 ], &c->sid[0 ]);if (rc) goto out;
@@ -2293,33 +2278,38 @@ out:int security_get_bools (int *len,char ***names,int **values) { -int i, rc = -ENOMEM ; +int i, rc;read_lock (&policy_rwlock); *names =NULL ; *values =NULL ; + rc =0 ; *len = policydb.p_bools.nprim; -if (!*len) { - rc =0 ; +if (!*len) goto out; - } - *names =kcalloc (*len,sizeof (char *),GFP_ATOMIC ); + rc = -ENOMEM ; + *names =kcalloc (*len,sizeof (char *),GFP_ATOMIC );if (!*names) goto err; - *values =kcalloc (*len,sizeof (int ),GFP_ATOMIC ); + rc = -ENOMEM ; + *values =kcalloc (*len,sizeof (int ),GFP_ATOMIC );if (!*values) goto err;for (i =0 ; i < *len; i++) {size_t name_len; + (*values)[i] = policydb.bool_val_to_struct[i]->state; name_len =strlen (policydb.p_bool_val_to_name[i]) +1 ; - (*names)[i] =kmalloc (sizeof (char ) * name_len,GFP_ATOMIC ); + + 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); (*names)[i][name_len -1 ] =0 ; }
@@ -2339,17 +2329,16 @@ err:int security_set_bools (int len,int *values) { -int i, rc =0 ; +int i, rc;int lenp, seqno =0 ;struct cond_node *cur;write_lock_irq (&policy_rwlock); + rc = -EFAULT ; lenp = policydb.p_bools.nprim; -if (len != lenp) { - rc = -EFAULT ; +if (len != lenp) goto out; - }for (i =0 ; i < len; i++) {if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
@@ -2375,7 +2364,7 @@ int security_set_bools(int len, int *values) } seqno = ++latest_granting; - + rc =0 ; out:write_unlock_irq (&policy_rwlock);if (!rc) {
@@ -2388,16 +2377,15 @@ out:int security_get_bool_value (int bool) { -int rc =0 ; +int rc;int len;read_lock (&policy_rwlock); + rc = -EFAULT ; len = policydb.p_bools.nprim; -if (bool >= len) { - rc = -EFAULT ; +if (bool >= len) goto out; - } rc = policydb.bool_val_to_struct[bool]->state; out:
@@ -2447,8 +2435,9 @@ int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)struct context newcon;char *s;u32 len; -int rc =0 ; +int rc; + rc =0 ;if (!ss_initialized || !policydb.mls_enabled) { *new_sid = sid; goto out;
@@ -2457,19 +2446,20 @@ int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)context_init (&newcon);read_lock (&policy_rwlock); + + rc = -EINVAL ; context1 =sidtab_search (&sidtab, sid);if (!context1) {printk (KERN_ERR "SELinux: %s: unrecognized SID %d\n" , __func__, sid); - rc = -EINVAL ; goto out_unlock; } + rc = -EINVAL ; context2 =sidtab_search (&sidtab, mls_sid);if (!context2) {printk (KERN_ERR "SELinux: %s: unrecognized SID %d\n" , __func__, mls_sid); - rc = -EINVAL ; goto out_unlock; }
@@ -2483,20 +2473,17 @@ int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)/* Check the validity of the new context. */ if (!policydb_context_isvalid (&policydb, &newcon)) { rc =convert_context_handle_invalid_context (&newcon); -if (rc) - goto bad; +if (rc) { +if (!context_struct_to_string (&newcon, &s, &len)) { +audit_log (current->audit_context,GFP_ATOMIC ,AUDIT_SELINUX_ERR , +"security_sid_mls_copy: invalid context %s" , s); +kfree (s); + } + goto out_unlock; + } } rc =sidtab_context_to_sid (&sidtab, &newcon, new_sid); - goto out_unlock; - -bad: -if (!context_struct_to_string (&newcon, &s, &len)) { -audit_log (current->audit_context,GFP_ATOMIC ,AUDIT_SELINUX_ERR , -"security_sid_mls_copy: invalid context %s" , s); -kfree (s); - } - out_unlock:read_unlock (&policy_rwlock);context_destroy (&newcon);
@@ -2532,6 +2519,8 @@ int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,struct context *nlbl_ctx;struct context *xfrm_ctx; + *peer_sid =SECSID_NULL ; +/* handle the common (which also happens to be the set of easy) cases * right away, these two if statements catch everything involving a * single or absent peer SID/label */
@@ -2550,40 +2539,37 @@ int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,/* we don't need to check ss_initialized here since the only way both * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the * security server was initialized and ss_initialized was true */ -if (!policydb.mls_enabled) { - *peer_sid =SECSID_NULL ; +if (!policydb.mls_enabled)return 0 ; - }read_lock (&policy_rwlock); + rc = -EINVAL ; nlbl_ctx =sidtab_search (&sidtab, nlbl_sid);if (!nlbl_ctx) {printk (KERN_ERR "SELinux: %s: unrecognized SID %d\n" , __func__, nlbl_sid); - rc = -EINVAL ; - goto out_slowpath; + goto out; } + rc = -EINVAL ; xfrm_ctx =sidtab_search (&sidtab, xfrm_sid);if (!xfrm_ctx) {printk (KERN_ERR "SELinux: %s: unrecognized SID %d\n" , __func__, xfrm_sid); - rc = -EINVAL ; - goto out_slowpath; + goto out; } rc = (mls_context_cmp (nlbl_ctx, xfrm_ctx) ?0 : -EACCES ); +if (rc) + goto out; -out_slowpath: +/* at present NetLabel SIDs/labels really only carry MLS +* information so if the MLS portion of the NetLabel SID +* matches the MLS portion of the labeled XFRM SID/label +* then pass along the XFRM SID as it is the most +* expressive */ + *peer_sid = xfrm_sid; +out:read_unlock (&policy_rwlock); -if (rc ==0 ) -/* at present NetLabel SIDs/labels really only carry MLS -* information so if the MLS portion of the NetLabel SID -* matches the MLS portion of the labeled XFRM SID/label -* then pass along the XFRM SID as it is the most -* expressive */ - *peer_sid = xfrm_sid; -else - *peer_sid =SECSID_NULL ;return rc; }
@@ -2602,10 +2588,11 @@ static int get_classes_callback(void *k, void *d, void *args)int security_get_classes (char ***classes,int *nclasses) { -int rc = -ENOMEM ; +int rc;read_lock (&policy_rwlock); + rc = -ENOMEM ; *nclasses = policydb.p_classes.nprim; *classes =kcalloc (*nclasses,sizeof (**classes),GFP_ATOMIC );if (!*classes)
@@ -2613,7 +2600,7 @@ int security_get_classes(char ***classes, int *nclasses) rc =hashtab_map (policydb.p_classes.table, get_classes_callback, *classes); -if (rc <0 ) { +if (rc) {int i;for (i =0 ; i < *nclasses; i++)kfree ((*classes)[i]);
@@ -2640,19 +2627,20 @@ static int get_permissions_callback(void *k, void *d, void *args)int security_get_permissions (char *class,char ***perms,int *nperms) { -int rc = -ENOMEM , i; +int rc, i;struct class_datum *match;read_lock (&policy_rwlock); + rc = -EINVAL ; match =hashtab_search (policydb.p_classes.table, class);if (!match) {printk (KERN_ERR "SELinux: %s: unrecognized class %s\n" , __func__, class); - rc = -EINVAL ; goto out; } + rc = -ENOMEM ; *nperms = match->permissions.nprim; *perms =kcalloc (*nperms,sizeof (**perms),GFP_ATOMIC );if (!*perms)
@@ -2661,13 +2649,13 @@ int security_get_permissions(char *class, char ***perms, int *nperms)if (match->comdatum) { rc =hashtab_map (match->comdatum->permissions.table, get_permissions_callback, *perms); -if (rc <0 ) +if (rc) goto err; } rc =hashtab_map (match->permissions.table, get_permissions_callback, *perms); -if (rc <0 ) +if (rc) goto err; out:
@@ -2779,36 +2767,39 @@ int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)switch (field) {case AUDIT_SUBJ_USER :case AUDIT_OBJ_USER : + rc = -EINVAL ; userdatum =hashtab_search (policydb.p_users.table, rulestr);if (!userdatum) - rc = -EINVAL ; -else - tmprule->au_ctxt.user = userdatum->value; + goto out; + tmprule->au_ctxt.user = userdatum->value;break ;case AUDIT_SUBJ_ROLE :case AUDIT_OBJ_ROLE : + rc = -EINVAL ; roledatum =hashtab_search (policydb.p_roles.table, rulestr);if (!roledatum) - rc = -EINVAL ; -else - tmprule->au_ctxt.role = roledatum->value; + goto out; + tmprule->au_ctxt.role = roledatum->value;break ;case AUDIT_SUBJ_TYPE :case AUDIT_OBJ_TYPE : + rc = -EINVAL ; typedatum =hashtab_search (policydb.p_types.table, rulestr);if (!typedatum) - rc = -EINVAL ; -else - tmprule->au_ctxt.type = typedatum->value; + goto out; + tmprule->au_ctxt.type = typedatum->value;break ;case AUDIT_SUBJ_SEN :case AUDIT_SUBJ_CLR :case AUDIT_OBJ_LEV_LOW :case AUDIT_OBJ_LEV_HIGH : rc =mls_from_string (rulestr, &tmprule->au_ctxt,GFP_ATOMIC ); +if (rc) + goto out;break ; } - + rc =0 ; +out:read_unlock (&policy_rwlock);if (rc) {
@@ -3110,28 +3101,23 @@ int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)return 0 ;read_lock (&policy_rwlock); + + rc = -ENOENT ; ctx =sidtab_search (&sidtab, sid); -if (ctx ==NULL ) { - rc = -ENOENT ; - goto netlbl_sid_to_secattr_failure; - } +if (ctx ==NULL ) + goto out; + + rc = -ENOMEM ; secattr->domain =kstrdup (policydb.p_type_val_to_name[ctx->type -1 ],GFP_ATOMIC ); -if (secattr->domain ==NULL ) { - rc = -ENOMEM ; - goto netlbl_sid_to_secattr_failure; - } +if (secattr->domain ==NULL ) + goto out; + secattr->attr.secid = sid; secattr->flags |=NETLBL_SECATTR_DOMAIN_CPY |NETLBL_SECATTR_SECID ;mls_export_netlbl_lvl (ctx, secattr); rc =mls_export_netlbl_cat (ctx, secattr); -if (rc !=0 ) - goto netlbl_sid_to_secattr_failure; -read_unlock (&policy_rwlock); - -return 0 ; - -netlbl_sid_to_secattr_failure: +out:read_unlock (&policy_rwlock);return rc; }
--
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: rework security_netlbl_secattr_to_sid
In reply to Eric Parissecurity_netlbl_secattr_to_sid is difficult to follow, especially the return codes. Try to make the function obvious.
---
security/selinux/ss/services.c | 33 +++++++++++++++++---------------- 1 files changed, 17 insertions(+), 16 deletions(-)
security/selinux/ss/services.c +17 -16
@@ -3024,7 +3024,7 @@ static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,int security_netlbl_secattr_to_sid (struct netlbl_lsm_secattr *secattr,u32 *sid) { -int rc = -EIDRM ; +int rc;struct context *ctx;struct context ctx_new;
@@ -3037,14 +3037,13 @@ int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,if (secattr->flags &NETLBL_SECATTR_CACHE ) { *sid = *(u32 *)secattr->cache->data; - rc =0 ; }else if (secattr->flags &NETLBL_SECATTR_SECID ) { *sid = secattr->attr.secid; - rc =0 ; }else if (secattr->flags &NETLBL_SECATTR_MLS_LVL ) { + rc = -EIDRM ; ctx =sidtab_search (&sidtab,SECINITSID_NETMSG );if (ctx ==NULL ) - goto netlbl_secattr_to_sid_return; + goto out;context_init (&ctx_new); ctx_new.user = ctx->user;
@@ -3052,34 +3051,36 @@ int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr, ctx_new.type = ctx->type;mls_import_netlbl_lvl (&ctx_new, secattr);if (secattr->flags &NETLBL_SECATTR_MLS_CAT ) { -if (ebitmap_netlbl_import (&ctx_new.range.level[0 ].cat, - secattr->attr.mls.cat) !=0 ) - goto netlbl_secattr_to_sid_return; + rc =ebitmap_netlbl_import (&ctx_new.range.level[0 ].cat, + secattr->attr.mls.cat); +if (rc) + goto out;memcpy (&ctx_new.range.level[1 ].cat, &ctx_new.range.level[0 ].cat,sizeof (ctx_new.range.level[0 ].cat)); } -if (mls_context_isvalid (&policydb, &ctx_new) !=1 ) - goto netlbl_secattr_to_sid_return_cleanup; + rc = -EIDRM ; +if (!mls_context_isvalid (&policydb, &ctx_new)) + goto out_free; rc =sidtab_context_to_sid (&sidtab, &ctx_new, sid); -if (rc !=0 ) - goto netlbl_secattr_to_sid_return_cleanup; +if (rc) + goto out_free;security_netlbl_cache_add (secattr, *sid);ebitmap_destroy (&ctx_new.range.level[0 ].cat); }else { *sid =SECSID_NULL ; - rc =0 ; } -netlbl_secattr_to_sid_return:read_unlock (&policy_rwlock); -return rc; -netlbl_secattr_to_sid_return_cleanup: +return 0 ; +out_free:ebitmap_destroy (&ctx_new.range.level[0 ].cat); - goto netlbl_secattr_to_sid_return; +out: +read_unlock (&policy_rwlock); +return rc; }/**
--
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.
Re: [PATCH 2/4] SELinux: standardize return code handling in selinuxfs.c
In reply to Eric ParisQuoted 26 lines · Eric Paris
selinuxfs.chas lots of different standards on how to handle return paths on error. For the most part transition torc=errno if (failure) goto out; [...] out: cleanup() return rc;Instead of doing cleanup mid function, or having multiple returns or other options. This doesn't do that for every function, but most of the complex functions which have cleanup routines on error.
Signed-off-by Eric Paris <[email protected]>---
security/selinux/selinuxfs.c | 652 ++++++++++++++++++++---------------------- 1 files changed, 317 insertions(+), 335 deletions(-)security/selinux/selinuxfs.c +0
-0
security/selinux/selinuxfs.c static ssize_t sel_write_enforce(struct file *file, const char __user *buf, · line 187
Quoted 7 lines
security/selinux/selinuxfs.c +2
-1@@ -177,7 +182,8 @@ static ssize_t sel_write_enforce(struct file *file, const char __user *buf, } length = count; out: -free_page ((unsigned long ) page); +if (page) +free_page ((unsigned long ) page);return length;
Isn't free_page(NULL) legal?
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.
Re: [PATCH 2/4] SELinux: standardize return code handling in selinuxfs.c
Quoted 22 lines · Stephen Smalley
Quoted 21 lines · Eric Paris
selinuxfs.chas lots of different standards on how to handle return paths on error. For the most part transition torc=errno if (failure) goto out; [...] out: cleanup() return rc;Instead of doing cleanup mid function, or having multiple returns or other options. This doesn't do that for every function, but most of the complex functions which have cleanup routines on error.
Signed-off-by Eric Paris <[email protected]>---
security/selinux/selinuxfs.c | 652 ++++++++++++++++++++---------------------- 1 files changed, 317 insertions(+), 335 deletions(-)
Quoted 14 lines
security/selinux/selinuxfs.c static ssize_t sel_write_enforce(struct file *file, const char __user *buf, · line 187
Quoted 12 lines
security/selinux/selinuxfs.c +2
-1@@ -177,7 +182,8 @@ static ssize_t sel_write_enforce(struct file *file, const char __user *buf, } length = count; out: -free_page ((unsigned long ) page); +if (page) +free_page ((unsigned long ) page);return length;Isn't
free_page(NULL)legal?
appears as though it is:
void free_pages(unsigned long addr, unsigned int order)
{
if (addr != 0) {
VM_BUG_ON(!virt_addr_valid((void *)addr));
__free_pages(virt_to_page((void *)addr), order);
}
}Will resend.
--
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.