selinux: do not allocate hashtabs dynamically

It is simpler to allocate them statically in the corresponding
structure, avoiding unnecessary kmalloc() calls and pointer
dereferencing.

Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
[PM: manual merging required in policydb.c]
Signed-off-by: Paul Moore <paul@paul-moore.com>
This commit is contained in:
Ondrej Mosnacek 2020-04-28 14:55:12 +02:00 committed by Paul Moore
parent 46619b44e4
commit 03414a49ad
8 changed files with 116 additions and 146 deletions

View File

@ -29,34 +29,21 @@ static u32 hashtab_compute_size(u32 nel)
return nel == 0 ? 0 : roundup_pow_of_two(nel); return nel == 0 ? 0 : roundup_pow_of_two(nel);
} }
struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key), int hashtab_init(struct hashtab *h,
int (*keycmp)(struct hashtab *h, const void *key1, const void *key2), u32 (*hash_value)(struct hashtab *h, const void *key),
u32 nel_hint) int (*keycmp)(struct hashtab *h, const void *key1,
const void *key2),
u32 nel_hint)
{ {
struct hashtab *p; h->size = hashtab_compute_size(nel_hint);
u32 i, size = hashtab_compute_size(nel_hint); h->nel = 0;
h->hash_value = hash_value;
h->keycmp = keycmp;
if (!h->size)
return 0;
p = kzalloc(sizeof(*p), GFP_KERNEL); h->htable = kcalloc(h->size, sizeof(*h->htable), GFP_KERNEL);
if (!p) return h->htable ? 0 : -ENOMEM;
return p;
p->size = size;
p->nel = 0;
p->hash_value = hash_value;
p->keycmp = keycmp;
if (!size)
return p;
p->htable = kmalloc_array(size, sizeof(*p->htable), GFP_KERNEL);
if (!p->htable) {
kfree(p);
return NULL;
}
for (i = 0; i < size; i++)
p->htable[i] = NULL;
return p;
} }
int hashtab_insert(struct hashtab *h, void *key, void *datum) int hashtab_insert(struct hashtab *h, void *key, void *datum)
@ -66,7 +53,7 @@ int hashtab_insert(struct hashtab *h, void *key, void *datum)
cond_resched(); cond_resched();
if (!h || !h->size || h->nel == HASHTAB_MAX_NODES) if (!h->size || h->nel == HASHTAB_MAX_NODES)
return -EINVAL; return -EINVAL;
hvalue = h->hash_value(h, key); hvalue = h->hash_value(h, key);
@ -102,7 +89,7 @@ void *hashtab_search(struct hashtab *h, const void *key)
u32 hvalue; u32 hvalue;
struct hashtab_node *cur; struct hashtab_node *cur;
if (!h || !h->size) if (!h->size)
return NULL; return NULL;
hvalue = h->hash_value(h, key); hvalue = h->hash_value(h, key);
@ -121,9 +108,6 @@ void hashtab_destroy(struct hashtab *h)
u32 i; u32 i;
struct hashtab_node *cur, *temp; struct hashtab_node *cur, *temp;
if (!h)
return;
for (i = 0; i < h->size; i++) { for (i = 0; i < h->size; i++) {
cur = h->htable[i]; cur = h->htable[i];
while (cur) { while (cur) {
@ -136,8 +120,6 @@ void hashtab_destroy(struct hashtab *h)
kfree(h->htable); kfree(h->htable);
h->htable = NULL; h->htable = NULL;
kfree(h);
} }
int hashtab_map(struct hashtab *h, int hashtab_map(struct hashtab *h,
@ -148,9 +130,6 @@ int hashtab_map(struct hashtab *h,
int ret; int ret;
struct hashtab_node *cur; struct hashtab_node *cur;
if (!h)
return 0;
for (i = 0; i < h->size; i++) { for (i = 0; i < h->size; i++) {
cur = h->htable[i]; cur = h->htable[i];
while (cur) { while (cur) {

View File

@ -35,14 +35,15 @@ struct hashtab_info {
}; };
/* /*
* Creates a new hash table with the specified characteristics. * Initializes a new hash table with the specified characteristics.
* *
* Returns NULL if insufficent space is available or * Returns -ENOMEM if insufficient space is available or 0 otherwise.
* the new hash table otherwise.
*/ */
struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key), int hashtab_init(struct hashtab *h,
int (*keycmp)(struct hashtab *h, const void *key1, const void *key2), u32 (*hash_value)(struct hashtab *h, const void *key),
u32 nel_hint); int (*keycmp)(struct hashtab *h, const void *key1,
const void *key2),
u32 nel_hint);
/* /*
* Inserts the specified (key, datum) pair into the specified hash table. * Inserts the specified (key, datum) pair into the specified hash table.

View File

@ -165,7 +165,7 @@ int mls_level_isvalid(struct policydb *p, struct mls_level *l)
if (!l->sens || l->sens > p->p_levels.nprim) if (!l->sens || l->sens > p->p_levels.nprim)
return 0; return 0;
levdatum = hashtab_search(p->p_levels.table, levdatum = hashtab_search(&p->p_levels.table,
sym_name(p, SYM_LEVELS, l->sens - 1)); sym_name(p, SYM_LEVELS, l->sens - 1));
if (!levdatum) if (!levdatum)
return 0; return 0;
@ -293,7 +293,7 @@ int mls_context_to_sid(struct policydb *pol,
*(next_cat++) = '\0'; *(next_cat++) = '\0';
/* Parse sensitivity. */ /* Parse sensitivity. */
levdatum = hashtab_search(pol->p_levels.table, sensitivity); levdatum = hashtab_search(&pol->p_levels.table, sensitivity);
if (!levdatum) if (!levdatum)
return -EINVAL; return -EINVAL;
context->range.level[l].sens = levdatum->level->sens; context->range.level[l].sens = levdatum->level->sens;
@ -312,7 +312,7 @@ int mls_context_to_sid(struct policydb *pol,
*rngptr++ = '\0'; *rngptr++ = '\0';
} }
catdatum = hashtab_search(pol->p_cats.table, cur_cat); catdatum = hashtab_search(&pol->p_cats.table, cur_cat);
if (!catdatum) if (!catdatum)
return -EINVAL; return -EINVAL;
@ -325,7 +325,7 @@ int mls_context_to_sid(struct policydb *pol,
if (rngptr == NULL) if (rngptr == NULL)
continue; continue;
rngdatum = hashtab_search(pol->p_cats.table, rngptr); rngdatum = hashtab_search(&pol->p_cats.table, rngptr);
if (!rngdatum) if (!rngdatum)
return -EINVAL; return -EINVAL;
@ -458,7 +458,7 @@ int mls_convert_context(struct policydb *oldp,
return 0; return 0;
for (l = 0; l < 2; l++) { for (l = 0; l < 2; l++) {
levdatum = hashtab_search(newp->p_levels.table, levdatum = hashtab_search(&newp->p_levels.table,
sym_name(oldp, SYM_LEVELS, sym_name(oldp, SYM_LEVELS,
oldc->range.level[l].sens - 1)); oldc->range.level[l].sens - 1));
@ -470,7 +470,7 @@ int mls_convert_context(struct policydb *oldp,
node, i) { node, i) {
int rc; int rc;
catdatum = hashtab_search(newp->p_cats.table, catdatum = hashtab_search(&newp->p_cats.table,
sym_name(oldp, SYM_CATS, i)); sym_name(oldp, SYM_CATS, i));
if (!catdatum) if (!catdatum)
return -EINVAL; return -EINVAL;
@ -506,7 +506,7 @@ int mls_compute_sid(struct policydb *p,
rtr.source_type = scontext->type; rtr.source_type = scontext->type;
rtr.target_type = tcontext->type; rtr.target_type = tcontext->type;
rtr.target_class = tclass; rtr.target_class = tclass;
r = hashtab_search(p->range_tr, &rtr); r = hashtab_search(&p->range_tr, &rtr);
if (r) if (r)
return mls_range_set(newcontext, r); return mls_range_set(newcontext, r);

View File

@ -195,8 +195,8 @@ static int common_destroy(void *key, void *datum, void *p)
kfree(key); kfree(key);
if (datum) { if (datum) {
comdatum = datum; comdatum = datum;
hashtab_map(comdatum->permissions.table, perm_destroy, NULL); hashtab_map(&comdatum->permissions.table, perm_destroy, NULL);
hashtab_destroy(comdatum->permissions.table); hashtab_destroy(&comdatum->permissions.table);
} }
kfree(datum); kfree(datum);
return 0; return 0;
@ -224,8 +224,8 @@ static int cls_destroy(void *key, void *datum, void *p)
kfree(key); kfree(key);
if (datum) { if (datum) {
cladatum = datum; cladatum = datum;
hashtab_map(cladatum->permissions.table, perm_destroy, NULL); hashtab_map(&cladatum->permissions.table, perm_destroy, NULL);
hashtab_destroy(cladatum->permissions.table); hashtab_destroy(&cladatum->permissions.table);
constraint = cladatum->constraints; constraint = cladatum->constraints;
while (constraint) { while (constraint) {
e = constraint->expr; e = constraint->expr;
@ -400,7 +400,7 @@ static int roles_init(struct policydb *p)
if (!key) if (!key)
goto out; goto out;
rc = hashtab_insert(p->p_roles.table, key, role); rc = hashtab_insert(&p->p_roles.table, key, role);
if (rc) if (rc)
goto out; goto out;
@ -668,7 +668,7 @@ static void symtab_hash_eval(struct symtab *s)
int i; int i;
for (i = 0; i < SYM_NUM; i++) for (i = 0; i < SYM_NUM; i++)
hash_eval(s[i].table, symtab_name[i]); hash_eval(&s[i].table, symtab_name[i]);
} }
#else #else
@ -739,7 +739,7 @@ static int policydb_index(struct policydb *p)
if (!p->sym_val_to_name[i]) if (!p->sym_val_to_name[i])
return -ENOMEM; return -ENOMEM;
rc = hashtab_map(p->symtab[i].table, index_f[i], p); rc = hashtab_map(&p->symtab[i].table, index_f[i], p);
if (rc) if (rc)
goto out; goto out;
} }
@ -760,8 +760,8 @@ void policydb_destroy(struct policydb *p)
for (i = 0; i < SYM_NUM; i++) { for (i = 0; i < SYM_NUM; i++) {
cond_resched(); cond_resched();
hashtab_map(p->symtab[i].table, destroy_f[i], NULL); hashtab_map(&p->symtab[i].table, destroy_f[i], NULL);
hashtab_destroy(p->symtab[i].table); hashtab_destroy(&p->symtab[i].table);
} }
for (i = 0; i < SYM_NUM; i++) for (i = 0; i < SYM_NUM; i++)
@ -803,8 +803,8 @@ void policydb_destroy(struct policydb *p)
cond_policydb_destroy(p); cond_policydb_destroy(p);
hashtab_map(p->role_tr, role_tr_destroy, NULL); hashtab_map(&p->role_tr, role_tr_destroy, NULL);
hashtab_destroy(p->role_tr); hashtab_destroy(&p->role_tr);
for (ra = p->role_allow; ra; ra = ra->next) { for (ra = p->role_allow; ra; ra = ra->next) {
cond_resched(); cond_resched();
@ -813,11 +813,11 @@ void policydb_destroy(struct policydb *p)
} }
kfree(lra); kfree(lra);
hashtab_map(p->filename_trans, filenametr_destroy, NULL); hashtab_map(&p->filename_trans, filenametr_destroy, NULL);
hashtab_destroy(p->filename_trans); hashtab_destroy(&p->filename_trans);
hashtab_map(p->range_tr, range_tr_destroy, NULL); hashtab_map(&p->range_tr, range_tr_destroy, NULL);
hashtab_destroy(p->range_tr); hashtab_destroy(&p->range_tr);
if (p->type_attr_map_array) { if (p->type_attr_map_array) {
for (i = 0; i < p->p_types.nprim; i++) for (i = 0; i < p->p_types.nprim; i++)
@ -1128,7 +1128,7 @@ static int common_read(struct policydb *p, struct hashtab *h, void *fp)
goto bad; goto bad;
for (i = 0; i < nel; i++) { for (i = 0; i < nel; i++) {
rc = perm_read(p, comdatum->permissions.table, fp); rc = perm_read(p, &comdatum->permissions.table, fp);
if (rc) if (rc)
goto bad; goto bad;
} }
@ -1300,7 +1300,8 @@ static int class_read(struct policydb *p, struct hashtab *h, void *fp)
goto bad; goto bad;
rc = -EINVAL; rc = -EINVAL;
cladatum->comdatum = hashtab_search(p->p_commons.table, cladatum->comkey); cladatum->comdatum = hashtab_search(&p->p_commons.table,
cladatum->comkey);
if (!cladatum->comdatum) { if (!cladatum->comdatum) {
pr_err("SELinux: unknown common %s\n", pr_err("SELinux: unknown common %s\n",
cladatum->comkey); cladatum->comkey);
@ -1308,7 +1309,7 @@ static int class_read(struct policydb *p, struct hashtab *h, void *fp)
} }
} }
for (i = 0; i < nel; i++) { for (i = 0; i < nel; i++) {
rc = perm_read(p, cladatum->permissions.table, fp); rc = perm_read(p, &cladatum->permissions.table, fp);
if (rc) if (rc)
goto bad; goto bad;
} }
@ -1731,18 +1732,15 @@ static int policydb_bounds_sanity_check(struct policydb *p)
if (p->policyvers < POLICYDB_VERSION_BOUNDARY) if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
return 0; return 0;
rc = hashtab_map(p->p_users.table, rc = hashtab_map(&p->p_users.table, user_bounds_sanity_check, p);
user_bounds_sanity_check, p);
if (rc) if (rc)
return rc; return rc;
rc = hashtab_map(p->p_roles.table, rc = hashtab_map(&p->p_roles.table, role_bounds_sanity_check, p);
role_bounds_sanity_check, p);
if (rc) if (rc)
return rc; return rc;
rc = hashtab_map(p->p_types.table, rc = hashtab_map(&p->p_types.table, type_bounds_sanity_check, p);
type_bounds_sanity_check, p);
if (rc) if (rc)
return rc; return rc;
@ -1753,7 +1751,7 @@ u16 string_to_security_class(struct policydb *p, const char *name)
{ {
struct class_datum *cladatum; struct class_datum *cladatum;
cladatum = hashtab_search(p->p_classes.table, name); cladatum = hashtab_search(&p->p_classes.table, name);
if (!cladatum) if (!cladatum)
return 0; return 0;
@ -1772,11 +1770,9 @@ u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
cladatum = p->class_val_to_struct[tclass-1]; cladatum = p->class_val_to_struct[tclass-1];
comdatum = cladatum->comdatum; comdatum = cladatum->comdatum;
if (comdatum) if (comdatum)
perdatum = hashtab_search(comdatum->permissions.table, perdatum = hashtab_search(&comdatum->permissions.table, name);
name);
if (!perdatum) if (!perdatum)
perdatum = hashtab_search(cladatum->permissions.table, perdatum = hashtab_search(&cladatum->permissions.table, name);
name);
if (!perdatum) if (!perdatum)
return 0; return 0;
@ -1800,9 +1796,9 @@ static int range_read(struct policydb *p, void *fp)
nel = le32_to_cpu(buf[0]); nel = le32_to_cpu(buf[0]);
p->range_tr = hashtab_create(rangetr_hash, rangetr_cmp, nel); rc = hashtab_init(&p->range_tr, rangetr_hash, rangetr_cmp, nel);
if (!p->range_tr) if (rc)
return -ENOMEM; return rc;
for (i = 0; i < nel; i++) { for (i = 0; i < nel; i++) {
rc = -ENOMEM; rc = -ENOMEM;
@ -1845,14 +1841,14 @@ static int range_read(struct policydb *p, void *fp)
goto out; goto out;
} }
rc = hashtab_insert(p->range_tr, rt, r); rc = hashtab_insert(&p->range_tr, rt, r);
if (rc) if (rc)
goto out; goto out;
rt = NULL; rt = NULL;
r = NULL; r = NULL;
} }
hash_eval(p->range_tr, "rangetr"); hash_eval(&p->range_tr, "rangetr");
rc = 0; rc = 0;
out: out:
kfree(rt); kfree(rt);
@ -1892,7 +1888,7 @@ static int filename_trans_read_helper_compat(struct policydb *p, void *fp)
otype = le32_to_cpu(buf[3]); otype = le32_to_cpu(buf[3]);
last = NULL; last = NULL;
datum = hashtab_search(p->filename_trans, &key); datum = hashtab_search(&p->filename_trans, &key);
while (datum) { while (datum) {
if (unlikely(ebitmap_get_bit(&datum->stypes, stype - 1))) { if (unlikely(ebitmap_get_bit(&datum->stypes, stype - 1))) {
/* conflicting/duplicate rules are ignored */ /* conflicting/duplicate rules are ignored */
@ -1922,7 +1918,7 @@ static int filename_trans_read_helper_compat(struct policydb *p, void *fp)
if (!ft) if (!ft)
goto out; goto out;
rc = hashtab_insert(p->filename_trans, ft, datum); rc = hashtab_insert(&p->filename_trans, ft, datum);
if (rc) if (rc)
goto out; goto out;
name = NULL; name = NULL;
@ -2010,7 +2006,7 @@ static int filename_trans_read_helper(struct policydb *p, void *fp)
ft->tclass = tclass; ft->tclass = tclass;
ft->name = name; ft->name = name;
rc = hashtab_insert(p->filename_trans, ft, first); rc = hashtab_insert(&p->filename_trans, ft, first);
if (rc == -EEXIST) if (rc == -EEXIST)
pr_err("SELinux: Duplicate filename transition key\n"); pr_err("SELinux: Duplicate filename transition key\n");
if (rc) if (rc)
@ -2047,10 +2043,11 @@ static int filename_trans_read(struct policydb *p, void *fp)
if (p->policyvers < POLICYDB_VERSION_COMP_FTRANS) { if (p->policyvers < POLICYDB_VERSION_COMP_FTRANS) {
p->compat_filename_trans_count = nel; p->compat_filename_trans_count = nel;
p->filename_trans = hashtab_create(filenametr_hash,
filenametr_cmp, (1 << 11)); rc = hashtab_init(&p->filename_trans, filenametr_hash,
if (!p->filename_trans) filenametr_cmp, (1 << 11));
return -ENOMEM; if (rc)
return rc;
for (i = 0; i < nel; i++) { for (i = 0; i < nel; i++) {
rc = filename_trans_read_helper_compat(p, fp); rc = filename_trans_read_helper_compat(p, fp);
@ -2058,10 +2055,10 @@ static int filename_trans_read(struct policydb *p, void *fp)
return rc; return rc;
} }
} else { } else {
p->filename_trans = hashtab_create(filenametr_hash, rc = hashtab_init(&p->filename_trans, filenametr_hash,
filenametr_cmp, nel); filenametr_cmp, nel);
if (!p->filename_trans) if (rc)
return -ENOMEM; return rc;
for (i = 0; i < nel; i++) { for (i = 0; i < nel; i++) {
rc = filename_trans_read_helper(p, fp); rc = filename_trans_read_helper(p, fp);
@ -2069,7 +2066,7 @@ static int filename_trans_read(struct policydb *p, void *fp)
return rc; return rc;
} }
} }
hash_eval(p->filename_trans, "filenametr"); hash_eval(&p->filename_trans, "filenametr");
return 0; return 0;
} }
@ -2512,7 +2509,7 @@ int policydb_read(struct policydb *p, void *fp)
} }
for (j = 0; j < nel; j++) { for (j = 0; j < nel; j++) {
rc = read_f[i](p, p->symtab[i].table, fp); rc = read_f[i](p, &p->symtab[i].table, fp);
if (rc) if (rc)
goto bad; goto bad;
} }
@ -2540,9 +2537,8 @@ int policydb_read(struct policydb *p, void *fp)
goto bad; goto bad;
nel = le32_to_cpu(buf[0]); nel = le32_to_cpu(buf[0]);
rc = -ENOMEM; rc = hashtab_init(&p->role_tr, role_trans_hash, role_trans_cmp, nel);
p->role_tr = hashtab_create(role_trans_hash, role_trans_cmp, nel); if (rc)
if (!p->role_tr)
goto bad; goto bad;
for (i = 0; i < nel; i++) { for (i = 0; i < nel; i++) {
rc = -ENOMEM; rc = -ENOMEM;
@ -2578,7 +2574,7 @@ int policydb_read(struct policydb *p, void *fp)
!policydb_role_isvalid(p, rtd->new_role)) !policydb_role_isvalid(p, rtd->new_role))
goto bad; goto bad;
rc = hashtab_insert(p->role_tr, rtk, rtd); rc = hashtab_insert(&p->role_tr, rtk, rtd);
if (rc) if (rc)
goto bad; goto bad;
@ -2822,12 +2818,12 @@ static int role_trans_write(struct policydb *p, void *fp)
__le32 buf[1]; __le32 buf[1];
int rc; int rc;
buf[0] = cpu_to_le32(p->role_tr->nel); buf[0] = cpu_to_le32(p->role_tr.nel);
rc = put_entry(buf, sizeof(u32), 1, fp); rc = put_entry(buf, sizeof(u32), 1, fp);
if (rc) if (rc)
return rc; return rc;
return hashtab_map(p->role_tr, role_trans_write_one, &pd); return hashtab_map(&p->role_tr, role_trans_write_one, &pd);
} }
static int role_allow_write(struct role_allow *r, void *fp) static int role_allow_write(struct role_allow *r, void *fp)
@ -2921,7 +2917,7 @@ static int common_write(void *vkey, void *datum, void *ptr)
buf[0] = cpu_to_le32(len); buf[0] = cpu_to_le32(len);
buf[1] = cpu_to_le32(comdatum->value); buf[1] = cpu_to_le32(comdatum->value);
buf[2] = cpu_to_le32(comdatum->permissions.nprim); buf[2] = cpu_to_le32(comdatum->permissions.nprim);
buf[3] = cpu_to_le32(comdatum->permissions.table->nel); buf[3] = cpu_to_le32(comdatum->permissions.table.nel);
rc = put_entry(buf, sizeof(u32), 4, fp); rc = put_entry(buf, sizeof(u32), 4, fp);
if (rc) if (rc)
return rc; return rc;
@ -2930,7 +2926,7 @@ static int common_write(void *vkey, void *datum, void *ptr)
if (rc) if (rc)
return rc; return rc;
rc = hashtab_map(comdatum->permissions.table, perm_write, fp); rc = hashtab_map(&comdatum->permissions.table, perm_write, fp);
if (rc) if (rc)
return rc; return rc;
@ -3029,10 +3025,7 @@ static int class_write(void *vkey, void *datum, void *ptr)
buf[1] = cpu_to_le32(len2); buf[1] = cpu_to_le32(len2);
buf[2] = cpu_to_le32(cladatum->value); buf[2] = cpu_to_le32(cladatum->value);
buf[3] = cpu_to_le32(cladatum->permissions.nprim); buf[3] = cpu_to_le32(cladatum->permissions.nprim);
if (cladatum->permissions.table) buf[4] = cpu_to_le32(cladatum->permissions.table.nel);
buf[4] = cpu_to_le32(cladatum->permissions.table->nel);
else
buf[4] = 0;
buf[5] = cpu_to_le32(ncons); buf[5] = cpu_to_le32(ncons);
rc = put_entry(buf, sizeof(u32), 6, fp); rc = put_entry(buf, sizeof(u32), 6, fp);
if (rc) if (rc)
@ -3048,7 +3041,7 @@ static int class_write(void *vkey, void *datum, void *ptr)
return rc; return rc;
} }
rc = hashtab_map(cladatum->permissions.table, perm_write, fp); rc = hashtab_map(&cladatum->permissions.table, perm_write, fp);
if (rc) if (rc)
return rc; return rc;
@ -3443,13 +3436,13 @@ static int range_write(struct policydb *p, void *fp)
pd.p = p; pd.p = p;
pd.fp = fp; pd.fp = fp;
buf[0] = cpu_to_le32(p->range_tr->nel); buf[0] = cpu_to_le32(p->range_tr.nel);
rc = put_entry(buf, sizeof(u32), 1, fp); rc = put_entry(buf, sizeof(u32), 1, fp);
if (rc) if (rc)
return rc; return rc;
/* actually write all of the entries */ /* actually write all of the entries */
rc = hashtab_map(p->range_tr, range_write_helper, &pd); rc = hashtab_map(&p->range_tr, range_write_helper, &pd);
if (rc) if (rc)
return rc; return rc;
@ -3556,15 +3549,15 @@ static int filename_trans_write(struct policydb *p, void *fp)
if (rc) if (rc)
return rc; return rc;
rc = hashtab_map(p->filename_trans, rc = hashtab_map(&p->filename_trans,
filename_write_helper_compat, fp); filename_write_helper_compat, fp);
} else { } else {
buf[0] = cpu_to_le32(p->filename_trans->nel); buf[0] = cpu_to_le32(p->filename_trans.nel);
rc = put_entry(buf, sizeof(u32), 1, fp); rc = put_entry(buf, sizeof(u32), 1, fp);
if (rc) if (rc)
return rc; return rc;
rc = hashtab_map(p->filename_trans, filename_write_helper, fp); rc = hashtab_map(&p->filename_trans, filename_write_helper, fp);
} }
return rc; return rc;
} }
@ -3653,12 +3646,12 @@ int policydb_write(struct policydb *p, void *fp)
pd.p = p; pd.p = p;
buf[0] = cpu_to_le32(p->symtab[i].nprim); buf[0] = cpu_to_le32(p->symtab[i].nprim);
buf[1] = cpu_to_le32(p->symtab[i].table->nel); buf[1] = cpu_to_le32(p->symtab[i].table.nel);
rc = put_entry(buf, sizeof(u32), 2, fp); rc = put_entry(buf, sizeof(u32), 2, fp);
if (rc) if (rc)
return rc; return rc;
rc = hashtab_map(p->symtab[i].table, write_f[i], &pd); rc = hashtab_map(&p->symtab[i].table, write_f[i], &pd);
if (rc) if (rc)
return rc; return rc;
} }

View File

@ -263,13 +263,13 @@ struct policydb {
struct avtab te_avtab; struct avtab te_avtab;
/* role transitions */ /* role transitions */
struct hashtab *role_tr; struct hashtab role_tr;
/* file transitions with the last path component */ /* file transitions with the last path component */
/* quickly exclude lookups when parent ttype has no rules */ /* quickly exclude lookups when parent ttype has no rules */
struct ebitmap filename_trans_ttypes; struct ebitmap filename_trans_ttypes;
/* actual set of filename_trans rules */ /* actual set of filename_trans rules */
struct hashtab *filename_trans; struct hashtab filename_trans;
/* only used if policyvers < POLICYDB_VERSION_COMP_FTRANS */ /* only used if policyvers < POLICYDB_VERSION_COMP_FTRANS */
u32 compat_filename_trans_count; u32 compat_filename_trans_count;
@ -294,7 +294,7 @@ struct policydb {
struct genfs *genfs; struct genfs *genfs;
/* range transitions table (range_trans_key -> mls_range) */ /* range transitions table (range_trans_key -> mls_range) */
struct hashtab *range_tr; struct hashtab range_tr;
/* type -> attribute reverse mapping */ /* type -> attribute reverse mapping */
struct ebitmap *type_attr_map_array; struct ebitmap *type_attr_map_array;

View File

@ -482,11 +482,11 @@ static void security_dump_masked_av(struct policydb *policydb,
/* init permission_names */ /* init permission_names */
if (common_dat && if (common_dat &&
hashtab_map(common_dat->permissions.table, hashtab_map(&common_dat->permissions.table,
dump_masked_av_helper, permission_names) < 0) dump_masked_av_helper, permission_names) < 0)
goto out; goto out;
if (hashtab_map(tclass_dat->permissions.table, if (hashtab_map(&tclass_dat->permissions.table,
dump_masked_av_helper, permission_names) < 0) dump_masked_av_helper, permission_names) < 0)
goto out; goto out;
@ -1441,7 +1441,7 @@ static int string_to_context_struct(struct policydb *pol,
*p++ = 0; *p++ = 0;
usrdatum = hashtab_search(pol->p_users.table, scontextp); usrdatum = hashtab_search(&pol->p_users.table, scontextp);
if (!usrdatum) if (!usrdatum)
goto out; goto out;
@ -1457,7 +1457,7 @@ static int string_to_context_struct(struct policydb *pol,
*p++ = 0; *p++ = 0;
role = hashtab_search(pol->p_roles.table, scontextp); role = hashtab_search(&pol->p_roles.table, scontextp);
if (!role) if (!role)
goto out; goto out;
ctx->role = role->value; ctx->role = role->value;
@ -1469,7 +1469,7 @@ static int string_to_context_struct(struct policydb *pol,
oldc = *p; oldc = *p;
*p++ = 0; *p++ = 0;
typdatum = hashtab_search(pol->p_types.table, scontextp); typdatum = hashtab_search(&pol->p_types.table, scontextp);
if (!typdatum || typdatum->attribute) if (!typdatum || typdatum->attribute)
goto out; goto out;
@ -1671,7 +1671,7 @@ static void filename_compute_type(struct policydb *policydb,
ft.tclass = tclass; ft.tclass = tclass;
ft.name = objname; ft.name = objname;
datum = hashtab_search(policydb->filename_trans, &ft); datum = hashtab_search(&policydb->filename_trans, &ft);
while (datum) { while (datum) {
if (ebitmap_get_bit(&datum->stypes, stype - 1)) { if (ebitmap_get_bit(&datum->stypes, stype - 1)) {
newcontext->type = datum->otype; newcontext->type = datum->otype;
@ -1834,7 +1834,7 @@ static int security_compute_sid(struct selinux_state *state,
.tclass = tclass, .tclass = tclass,
}; };
rtd = hashtab_search(policydb->role_tr, &rtk); rtd = hashtab_search(&policydb->role_tr, &rtk);
if (rtd) if (rtd)
newcontext.role = rtd->new_role; newcontext.role = rtd->new_role;
} }
@ -2024,7 +2024,7 @@ static int convert_context(struct context *oldc, struct context *newc, void *p)
/* Convert the user. */ /* Convert the user. */
rc = -EINVAL; rc = -EINVAL;
usrdatum = hashtab_search(args->newp->p_users.table, usrdatum = hashtab_search(&args->newp->p_users.table,
sym_name(args->oldp, sym_name(args->oldp,
SYM_USERS, oldc->user - 1)); SYM_USERS, oldc->user - 1));
if (!usrdatum) if (!usrdatum)
@ -2033,7 +2033,7 @@ static int convert_context(struct context *oldc, struct context *newc, void *p)
/* Convert the role. */ /* Convert the role. */
rc = -EINVAL; rc = -EINVAL;
role = hashtab_search(args->newp->p_roles.table, role = hashtab_search(&args->newp->p_roles.table,
sym_name(args->oldp, SYM_ROLES, oldc->role - 1)); sym_name(args->oldp, SYM_ROLES, oldc->role - 1));
if (!role) if (!role)
goto bad; goto bad;
@ -2041,7 +2041,7 @@ static int convert_context(struct context *oldc, struct context *newc, void *p)
/* Convert the type. */ /* Convert the type. */
rc = -EINVAL; rc = -EINVAL;
typdatum = hashtab_search(args->newp->p_types.table, typdatum = hashtab_search(&args->newp->p_types.table,
sym_name(args->oldp, sym_name(args->oldp,
SYM_TYPES, oldc->type - 1)); SYM_TYPES, oldc->type - 1));
if (!typdatum) if (!typdatum)
@ -2623,7 +2623,7 @@ int security_get_user_sids(struct selinux_state *state,
goto out_unlock; goto out_unlock;
rc = -EINVAL; rc = -EINVAL;
user = hashtab_search(policydb->p_users.table, username); user = hashtab_search(&policydb->p_users.table, username);
if (!user) if (!user)
goto out_unlock; goto out_unlock;
@ -2975,7 +2975,7 @@ static int security_preserve_bools(struct selinux_state *state,
if (rc) if (rc)
goto out; goto out;
for (i = 0; i < nbools; i++) { for (i = 0; i < nbools; i++) {
booldatum = hashtab_search(policydb->p_bools.table, bnames[i]); booldatum = hashtab_search(&policydb->p_bools.table, bnames[i]);
if (booldatum) if (booldatum)
booldatum->state = bvalues[i]; booldatum->state = bvalues[i];
} }
@ -3189,8 +3189,8 @@ int security_get_classes(struct selinux_state *state,
if (!*classes) if (!*classes)
goto out; goto out;
rc = hashtab_map(policydb->p_classes.table, get_classes_callback, rc = hashtab_map(&policydb->p_classes.table, get_classes_callback,
*classes); *classes);
if (rc) { if (rc) {
int i; int i;
for (i = 0; i < *nclasses; i++) for (i = 0; i < *nclasses; i++)
@ -3226,7 +3226,7 @@ int security_get_permissions(struct selinux_state *state,
read_lock(&state->ss->policy_rwlock); read_lock(&state->ss->policy_rwlock);
rc = -EINVAL; rc = -EINVAL;
match = hashtab_search(policydb->p_classes.table, class); match = hashtab_search(&policydb->p_classes.table, class);
if (!match) { if (!match) {
pr_err("SELinux: %s: unrecognized class %s\n", pr_err("SELinux: %s: unrecognized class %s\n",
__func__, class); __func__, class);
@ -3240,14 +3240,14 @@ int security_get_permissions(struct selinux_state *state,
goto out; goto out;
if (match->comdatum) { if (match->comdatum) {
rc = hashtab_map(match->comdatum->permissions.table, rc = hashtab_map(&match->comdatum->permissions.table,
get_permissions_callback, *perms); get_permissions_callback, *perms);
if (rc) if (rc)
goto err; goto err;
} }
rc = hashtab_map(match->permissions.table, get_permissions_callback, rc = hashtab_map(&match->permissions.table, get_permissions_callback,
*perms); *perms);
if (rc) if (rc)
goto err; goto err;
@ -3365,7 +3365,7 @@ int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
case AUDIT_SUBJ_USER: case AUDIT_SUBJ_USER:
case AUDIT_OBJ_USER: case AUDIT_OBJ_USER:
rc = -EINVAL; rc = -EINVAL;
userdatum = hashtab_search(policydb->p_users.table, rulestr); userdatum = hashtab_search(&policydb->p_users.table, rulestr);
if (!userdatum) if (!userdatum)
goto out; goto out;
tmprule->au_ctxt.user = userdatum->value; tmprule->au_ctxt.user = userdatum->value;
@ -3373,7 +3373,7 @@ int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
case AUDIT_SUBJ_ROLE: case AUDIT_SUBJ_ROLE:
case AUDIT_OBJ_ROLE: case AUDIT_OBJ_ROLE:
rc = -EINVAL; rc = -EINVAL;
roledatum = hashtab_search(policydb->p_roles.table, rulestr); roledatum = hashtab_search(&policydb->p_roles.table, rulestr);
if (!roledatum) if (!roledatum)
goto out; goto out;
tmprule->au_ctxt.role = roledatum->value; tmprule->au_ctxt.role = roledatum->value;
@ -3381,7 +3381,7 @@ int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
case AUDIT_SUBJ_TYPE: case AUDIT_SUBJ_TYPE:
case AUDIT_OBJ_TYPE: case AUDIT_OBJ_TYPE:
rc = -EINVAL; rc = -EINVAL;
typedatum = hashtab_search(policydb->p_types.table, rulestr); typedatum = hashtab_search(&policydb->p_types.table, rulestr);
if (!typedatum) if (!typedatum)
goto out; goto out;
tmprule->au_ctxt.type = typedatum->value; tmprule->au_ctxt.type = typedatum->value;

View File

@ -35,10 +35,7 @@ static int symcmp(struct hashtab *h, const void *key1, const void *key2)
int symtab_init(struct symtab *s, unsigned int size) int symtab_init(struct symtab *s, unsigned int size)
{ {
s->table = hashtab_create(symhash, symcmp, size);
if (!s->table)
return -ENOMEM;
s->nprim = 0; s->nprim = 0;
return 0; return hashtab_init(&s->table, symhash, symcmp, size);
} }

View File

@ -13,7 +13,7 @@
#include "hashtab.h" #include "hashtab.h"
struct symtab { struct symtab {
struct hashtab *table; /* hash table (keyed on a string) */ struct hashtab table; /* hash table (keyed on a string) */
u32 nprim; /* number of primary names in table */ u32 nprim; /* number of primary names in table */
}; };