Merge tag 'selinux-pr-20190917' of git://git.kernel.org/pub/scm/linux/kernel/git...
[platform/kernel/linux-rpi.git] / security / selinux / ss / policydb.c
index f8efaa9..1260f5f 100644 (file)
@@ -177,6 +177,195 @@ static struct policydb_compat_info *policydb_lookup_compat(int version)
 }
 
 /*
+ * The following *_destroy functions are used to
+ * free any memory allocated for each kind of
+ * symbol data in the policy database.
+ */
+
+static int perm_destroy(void *key, void *datum, void *p)
+{
+       kfree(key);
+       kfree(datum);
+       return 0;
+}
+
+static int common_destroy(void *key, void *datum, void *p)
+{
+       struct common_datum *comdatum;
+
+       kfree(key);
+       if (datum) {
+               comdatum = datum;
+               hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
+               hashtab_destroy(comdatum->permissions.table);
+       }
+       kfree(datum);
+       return 0;
+}
+
+static void constraint_expr_destroy(struct constraint_expr *expr)
+{
+       if (expr) {
+               ebitmap_destroy(&expr->names);
+               if (expr->type_names) {
+                       ebitmap_destroy(&expr->type_names->types);
+                       ebitmap_destroy(&expr->type_names->negset);
+                       kfree(expr->type_names);
+               }
+               kfree(expr);
+       }
+}
+
+static int cls_destroy(void *key, void *datum, void *p)
+{
+       struct class_datum *cladatum;
+       struct constraint_node *constraint, *ctemp;
+       struct constraint_expr *e, *etmp;
+
+       kfree(key);
+       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) {
+                               etmp = e;
+                               e = e->next;
+                               constraint_expr_destroy(etmp);
+                       }
+                       ctemp = constraint;
+                       constraint = constraint->next;
+                       kfree(ctemp);
+               }
+
+               constraint = cladatum->validatetrans;
+               while (constraint) {
+                       e = constraint->expr;
+                       while (e) {
+                               etmp = e;
+                               e = e->next;
+                               constraint_expr_destroy(etmp);
+                       }
+                       ctemp = constraint;
+                       constraint = constraint->next;
+                       kfree(ctemp);
+               }
+               kfree(cladatum->comkey);
+       }
+       kfree(datum);
+       return 0;
+}
+
+static int role_destroy(void *key, void *datum, void *p)
+{
+       struct role_datum *role;
+
+       kfree(key);
+       if (datum) {
+               role = datum;
+               ebitmap_destroy(&role->dominates);
+               ebitmap_destroy(&role->types);
+       }
+       kfree(datum);
+       return 0;
+}
+
+static int type_destroy(void *key, void *datum, void *p)
+{
+       kfree(key);
+       kfree(datum);
+       return 0;
+}
+
+static int user_destroy(void *key, void *datum, void *p)
+{
+       struct user_datum *usrdatum;
+
+       kfree(key);
+       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;
+}
+
+static int sens_destroy(void *key, void *datum, void *p)
+{
+       struct level_datum *levdatum;
+
+       kfree(key);
+       if (datum) {
+               levdatum = datum;
+               if (levdatum->level)
+                       ebitmap_destroy(&levdatum->level->cat);
+               kfree(levdatum->level);
+       }
+       kfree(datum);
+       return 0;
+}
+
+static int cat_destroy(void *key, void *datum, void *p)
+{
+       kfree(key);
+       kfree(datum);
+       return 0;
+}
+
+static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
+{
+       common_destroy,
+       cls_destroy,
+       role_destroy,
+       type_destroy,
+       user_destroy,
+       cond_destroy_bool,
+       sens_destroy,
+       cat_destroy,
+};
+
+static int filenametr_destroy(void *key, void *datum, void *p)
+{
+       struct filename_trans *ft = key;
+
+       kfree(ft->name);
+       kfree(key);
+       kfree(datum);
+       cond_resched();
+       return 0;
+}
+
+static int range_tr_destroy(void *key, void *datum, void *p)
+{
+       struct mls_range *rt = datum;
+
+       kfree(key);
+       ebitmap_destroy(&rt->level[0].cat);
+       ebitmap_destroy(&rt->level[1].cat);
+       kfree(datum);
+       cond_resched();
+       return 0;
+}
+
+static void ocontext_destroy(struct ocontext *c, int i)
+{
+       if (!c)
+               return;
+
+       context_destroy(&c->context[0]);
+       context_destroy(&c->context[1]);
+       if (i == OCON_ISID || i == OCON_FS ||
+           i == OCON_NETIF || i == OCON_FSUSE)
+               kfree(c->u.name);
+       kfree(c);
+}
+
+/*
  * Initialize the role table.
  */
 static int roles_init(struct policydb *p)
@@ -250,6 +439,7 @@ static int filenametr_cmp(struct hashtab *h, const void *k1, const void *k2)
 static u32 rangetr_hash(struct hashtab *h, const void *k)
 {
        const struct range_trans *key = k;
+
        return (key->source_type + (key->target_type << 3) +
                (key->target_class << 5)) & (h->size - 1);
 }
@@ -272,8 +462,6 @@ static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
        return v;
 }
 
-static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap);
-
 /*
  * Initialize a policy database structure.
  */
@@ -301,7 +489,8 @@ static int policydb_init(struct policydb *p)
        if (rc)
                goto out;
 
-       p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp, (1 << 10));
+       p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp,
+                                          (1 << 10));
        if (!p->filename_trans) {
                rc = -ENOMEM;
                goto out;
@@ -399,7 +588,7 @@ static int type_index(void *key, void *datum, void *datap)
                    || typdatum->bounds > p->p_types.nprim)
                        return -EINVAL;
                p->sym_val_to_name[SYM_TYPES][typdatum->value - 1] = key;
-               p->type_val_to_struct_array[typdatum->value - 1] = typdatum;
+               p->type_val_to_struct[typdatum->value - 1] = typdatum;
        }
 
        return 0;
@@ -477,9 +666,9 @@ static void hash_eval(struct hashtab *h, const char *hash_name)
        struct hashtab_info info;
 
        hashtab_stat(h, &info);
-       pr_debug("SELinux: %s:  %d entries and %d/%d buckets used, "
-              "longest chain length %d\n", hash_name, h->nel,
-              info.slots_used, h->size, info.max_chain_len);
+       pr_debug("SELinux: %s:  %d entries and %d/%d buckets used, longest chain length %d\n",
+                hash_name, h->nel, info.slots_used, h->size,
+                info.max_chain_len);
 }
 
 static void symtab_hash_eval(struct symtab *s)
@@ -541,10 +730,10 @@ static int policydb_index(struct policydb *p)
        if (!p->user_val_to_struct)
                return -ENOMEM;
 
-       p->type_val_to_struct_array = kvcalloc(p->p_types.nprim,
-                                              sizeof(*p->type_val_to_struct_array),
-                                              GFP_KERNEL);
-       if (!p->type_val_to_struct_array)
+       p->type_val_to_struct = kvcalloc(p->p_types.nprim,
+                                        sizeof(*p->type_val_to_struct),
+                                        GFP_KERNEL);
+       if (!p->type_val_to_struct)
                return -ENOMEM;
 
        rc = cond_init_bool_indexes(p);
@@ -568,193 +757,6 @@ out:
 }
 
 /*
- * The following *_destroy functions are used to
- * free any memory allocated for each kind of
- * symbol data in the policy database.
- */
-
-static int perm_destroy(void *key, void *datum, void *p)
-{
-       kfree(key);
-       kfree(datum);
-       return 0;
-}
-
-static int common_destroy(void *key, void *datum, void *p)
-{
-       struct common_datum *comdatum;
-
-       kfree(key);
-       if (datum) {
-               comdatum = datum;
-               hashtab_map(comdatum->permissions.table, perm_destroy, NULL);
-               hashtab_destroy(comdatum->permissions.table);
-       }
-       kfree(datum);
-       return 0;
-}
-
-static void constraint_expr_destroy(struct constraint_expr *expr)
-{
-       if (expr) {
-               ebitmap_destroy(&expr->names);
-               if (expr->type_names) {
-                       ebitmap_destroy(&expr->type_names->types);
-                       ebitmap_destroy(&expr->type_names->negset);
-                       kfree(expr->type_names);
-               }
-               kfree(expr);
-       }
-}
-
-static int cls_destroy(void *key, void *datum, void *p)
-{
-       struct class_datum *cladatum;
-       struct constraint_node *constraint, *ctemp;
-       struct constraint_expr *e, *etmp;
-
-       kfree(key);
-       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) {
-                               etmp = e;
-                               e = e->next;
-                               constraint_expr_destroy(etmp);
-                       }
-                       ctemp = constraint;
-                       constraint = constraint->next;
-                       kfree(ctemp);
-               }
-
-               constraint = cladatum->validatetrans;
-               while (constraint) {
-                       e = constraint->expr;
-                       while (e) {
-                               etmp = e;
-                               e = e->next;
-                               constraint_expr_destroy(etmp);
-                       }
-                       ctemp = constraint;
-                       constraint = constraint->next;
-                       kfree(ctemp);
-               }
-               kfree(cladatum->comkey);
-       }
-       kfree(datum);
-       return 0;
-}
-
-static int role_destroy(void *key, void *datum, void *p)
-{
-       struct role_datum *role;
-
-       kfree(key);
-       if (datum) {
-               role = datum;
-               ebitmap_destroy(&role->dominates);
-               ebitmap_destroy(&role->types);
-       }
-       kfree(datum);
-       return 0;
-}
-
-static int type_destroy(void *key, void *datum, void *p)
-{
-       kfree(key);
-       kfree(datum);
-       return 0;
-}
-
-static int user_destroy(void *key, void *datum, void *p)
-{
-       struct user_datum *usrdatum;
-
-       kfree(key);
-       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;
-}
-
-static int sens_destroy(void *key, void *datum, void *p)
-{
-       struct level_datum *levdatum;
-
-       kfree(key);
-       if (datum) {
-               levdatum = datum;
-               if (levdatum->level)
-                       ebitmap_destroy(&levdatum->level->cat);
-               kfree(levdatum->level);
-       }
-       kfree(datum);
-       return 0;
-}
-
-static int cat_destroy(void *key, void *datum, void *p)
-{
-       kfree(key);
-       kfree(datum);
-       return 0;
-}
-
-static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
-{
-       common_destroy,
-       cls_destroy,
-       role_destroy,
-       type_destroy,
-       user_destroy,
-       cond_destroy_bool,
-       sens_destroy,
-       cat_destroy,
-};
-
-static int filenametr_destroy(void *key, void *datum, void *p)
-{
-       struct filename_trans *ft = key;
-       kfree(ft->name);
-       kfree(key);
-       kfree(datum);
-       cond_resched();
-       return 0;
-}
-
-static int range_tr_destroy(void *key, void *datum, void *p)
-{
-       struct mls_range *rt = datum;
-       kfree(key);
-       ebitmap_destroy(&rt->level[0].cat);
-       ebitmap_destroy(&rt->level[1].cat);
-       kfree(datum);
-       cond_resched();
-       return 0;
-}
-
-static void ocontext_destroy(struct ocontext *c, int i)
-{
-       if (!c)
-               return;
-
-       context_destroy(&c->context[0]);
-       context_destroy(&c->context[1]);
-       if (i == OCON_ISID || i == OCON_FS ||
-           i == OCON_NETIF || i == OCON_FSUSE)
-               kfree(c->u.name);
-       kfree(c);
-}
-
-/*
  * Free any memory allocated by a policy database structure.
  */
 void policydb_destroy(struct policydb *p)
@@ -777,7 +779,7 @@ void policydb_destroy(struct policydb *p)
        kfree(p->class_val_to_struct);
        kfree(p->role_val_to_struct);
        kfree(p->user_val_to_struct);
-       kvfree(p->type_val_to_struct_array);
+       kvfree(p->type_val_to_struct);
 
        avtab_destroy(&p->te_avtab);
 
@@ -1722,7 +1724,7 @@ static int type_bounds_sanity_check(void *key, void *datum, void *datap)
                        return -EINVAL;
                }
 
-               upper = p->type_val_to_struct_array[upper->bounds - 1];
+               upper = p->type_val_to_struct[upper->bounds - 1];
                BUG_ON(!upper);
 
                if (upper->attribute) {