Skip to content

Commit

Permalink
selinux: do not allocate hashtabs dynamically
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
WOnder93 authored and pcmoore committed May 1, 2020
1 parent 46619b4 commit 03414a4
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 146 deletions.
51 changes: 15 additions & 36 deletions security/selinux/ss/hashtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,21 @@ static u32 hashtab_compute_size(u32 nel)
return nel == 0 ? 0 : roundup_pow_of_two(nel);
}

struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
u32 nel_hint)
int hashtab_init(struct hashtab *h,
u32 (*hash_value)(struct hashtab *h, const void *key),
int (*keycmp)(struct hashtab *h, const void *key1,
const void *key2),
u32 nel_hint)
{
struct hashtab *p;
u32 i, size = hashtab_compute_size(nel_hint);

p = kzalloc(sizeof(*p), GFP_KERNEL);
if (!p)
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;
h->size = hashtab_compute_size(nel_hint);
h->nel = 0;
h->hash_value = hash_value;
h->keycmp = keycmp;
if (!h->size)
return 0;

return p;
h->htable = kcalloc(h->size, sizeof(*h->htable), GFP_KERNEL);
return h->htable ? 0 : -ENOMEM;
}

int hashtab_insert(struct hashtab *h, void *key, void *datum)
Expand All @@ -66,7 +53,7 @@ int hashtab_insert(struct hashtab *h, void *key, void *datum)

cond_resched();

if (!h || !h->size || h->nel == HASHTAB_MAX_NODES)
if (!h->size || h->nel == HASHTAB_MAX_NODES)
return -EINVAL;

hvalue = h->hash_value(h, key);
Expand Down Expand Up @@ -102,7 +89,7 @@ void *hashtab_search(struct hashtab *h, const void *key)
u32 hvalue;
struct hashtab_node *cur;

if (!h || !h->size)
if (!h->size)
return NULL;

hvalue = h->hash_value(h, key);
Expand All @@ -121,9 +108,6 @@ void hashtab_destroy(struct hashtab *h)
u32 i;
struct hashtab_node *cur, *temp;

if (!h)
return;

for (i = 0; i < h->size; i++) {
cur = h->htable[i];
while (cur) {
Expand All @@ -136,8 +120,6 @@ void hashtab_destroy(struct hashtab *h)

kfree(h->htable);
h->htable = NULL;

kfree(h);
}

int hashtab_map(struct hashtab *h,
Expand All @@ -148,9 +130,6 @@ int hashtab_map(struct hashtab *h,
int ret;
struct hashtab_node *cur;

if (!h)
return 0;

for (i = 0; i < h->size; i++) {
cur = h->htable[i];
while (cur) {
Expand Down
13 changes: 7 additions & 6 deletions security/selinux/ss/hashtab.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
* the new hash table otherwise.
* Returns -ENOMEM if insufficient space is available or 0 otherwise.
*/
struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
u32 nel_hint);
int hashtab_init(struct hashtab *h,
u32 (*hash_value)(struct hashtab *h, const void *key),
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.
Expand Down
14 changes: 7 additions & 7 deletions security/selinux/ss/mls.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ int mls_level_isvalid(struct policydb *p, struct mls_level *l)

if (!l->sens || l->sens > p->p_levels.nprim)
return 0;
levdatum = hashtab_search(p->p_levels.table,
levdatum = hashtab_search(&p->p_levels.table,
sym_name(p, SYM_LEVELS, l->sens - 1));
if (!levdatum)
return 0;
Expand Down Expand Up @@ -293,7 +293,7 @@ int mls_context_to_sid(struct policydb *pol,
*(next_cat++) = '\0';

/* Parse sensitivity. */
levdatum = hashtab_search(pol->p_levels.table, sensitivity);
levdatum = hashtab_search(&pol->p_levels.table, sensitivity);
if (!levdatum)
return -EINVAL;
context->range.level[l].sens = levdatum->level->sens;
Expand All @@ -312,7 +312,7 @@ int mls_context_to_sid(struct policydb *pol,
*rngptr++ = '\0';
}

catdatum = hashtab_search(pol->p_cats.table, cur_cat);
catdatum = hashtab_search(&pol->p_cats.table, cur_cat);
if (!catdatum)
return -EINVAL;

Expand All @@ -325,7 +325,7 @@ int mls_context_to_sid(struct policydb *pol,
if (rngptr == NULL)
continue;

rngdatum = hashtab_search(pol->p_cats.table, rngptr);
rngdatum = hashtab_search(&pol->p_cats.table, rngptr);
if (!rngdatum)
return -EINVAL;

Expand Down Expand Up @@ -458,7 +458,7 @@ int mls_convert_context(struct policydb *oldp,
return 0;

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,
oldc->range.level[l].sens - 1));

Expand All @@ -470,7 +470,7 @@ int mls_convert_context(struct policydb *oldp,
node, i) {
int rc;

catdatum = hashtab_search(newp->p_cats.table,
catdatum = hashtab_search(&newp->p_cats.table,
sym_name(oldp, SYM_CATS, i));
if (!catdatum)
return -EINVAL;
Expand Down Expand Up @@ -506,7 +506,7 @@ int mls_compute_sid(struct policydb *p,
rtr.source_type = scontext->type;
rtr.target_type = tcontext->type;
rtr.target_class = tclass;
r = hashtab_search(p->range_tr, &rtr);
r = hashtab_search(&p->range_tr, &rtr);
if (r)
return mls_range_set(newcontext, r);

Expand Down
Loading

0 comments on commit 03414a4

Please sign in to comment.