Skip to content

Commit 03414a4

Browse files
WOnder93pcmoore
authored andcommitted
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 <[email protected]> [PM: manual merging required in policydb.c] Signed-off-by: Paul Moore <[email protected]>
1 parent 46619b4 commit 03414a4

File tree

8 files changed

+116
-146
lines changed

8 files changed

+116
-146
lines changed

security/selinux/ss/hashtab.c

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,21 @@ static u32 hashtab_compute_size(u32 nel)
2929
return nel == 0 ? 0 : roundup_pow_of_two(nel);
3030
}
3131

32-
struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
33-
int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
34-
u32 nel_hint)
32+
int hashtab_init(struct hashtab *h,
33+
u32 (*hash_value)(struct hashtab *h, const void *key),
34+
int (*keycmp)(struct hashtab *h, const void *key1,
35+
const void *key2),
36+
u32 nel_hint)
3537
{
36-
struct hashtab *p;
37-
u32 i, size = hashtab_compute_size(nel_hint);
38-
39-
p = kzalloc(sizeof(*p), GFP_KERNEL);
40-
if (!p)
41-
return p;
42-
43-
p->size = size;
44-
p->nel = 0;
45-
p->hash_value = hash_value;
46-
p->keycmp = keycmp;
47-
if (!size)
48-
return p;
49-
50-
p->htable = kmalloc_array(size, sizeof(*p->htable), GFP_KERNEL);
51-
if (!p->htable) {
52-
kfree(p);
53-
return NULL;
54-
}
55-
56-
for (i = 0; i < size; i++)
57-
p->htable[i] = NULL;
38+
h->size = hashtab_compute_size(nel_hint);
39+
h->nel = 0;
40+
h->hash_value = hash_value;
41+
h->keycmp = keycmp;
42+
if (!h->size)
43+
return 0;
5844

59-
return p;
45+
h->htable = kcalloc(h->size, sizeof(*h->htable), GFP_KERNEL);
46+
return h->htable ? 0 : -ENOMEM;
6047
}
6148

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

6754
cond_resched();
6855

69-
if (!h || !h->size || h->nel == HASHTAB_MAX_NODES)
56+
if (!h->size || h->nel == HASHTAB_MAX_NODES)
7057
return -EINVAL;
7158

7259
hvalue = h->hash_value(h, key);
@@ -102,7 +89,7 @@ void *hashtab_search(struct hashtab *h, const void *key)
10289
u32 hvalue;
10390
struct hashtab_node *cur;
10491

105-
if (!h || !h->size)
92+
if (!h->size)
10693
return NULL;
10794

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

124-
if (!h)
125-
return;
126-
127111
for (i = 0; i < h->size; i++) {
128112
cur = h->htable[i];
129113
while (cur) {
@@ -136,8 +120,6 @@ void hashtab_destroy(struct hashtab *h)
136120

137121
kfree(h->htable);
138122
h->htable = NULL;
139-
140-
kfree(h);
141123
}
142124

143125
int hashtab_map(struct hashtab *h,
@@ -148,9 +130,6 @@ int hashtab_map(struct hashtab *h,
148130
int ret;
149131
struct hashtab_node *cur;
150132

151-
if (!h)
152-
return 0;
153-
154133
for (i = 0; i < h->size; i++) {
155134
cur = h->htable[i];
156135
while (cur) {

security/selinux/ss/hashtab.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ struct hashtab_info {
3535
};
3636

3737
/*
38-
* Creates a new hash table with the specified characteristics.
38+
* Initializes a new hash table with the specified characteristics.
3939
*
40-
* Returns NULL if insufficent space is available or
41-
* the new hash table otherwise.
40+
* Returns -ENOMEM if insufficient space is available or 0 otherwise.
4241
*/
43-
struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
44-
int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
45-
u32 nel_hint);
42+
int hashtab_init(struct hashtab *h,
43+
u32 (*hash_value)(struct hashtab *h, const void *key),
44+
int (*keycmp)(struct hashtab *h, const void *key1,
45+
const void *key2),
46+
u32 nel_hint);
4647

4748
/*
4849
* Inserts the specified (key, datum) pair into the specified hash table.

security/selinux/ss/mls.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ int mls_level_isvalid(struct policydb *p, struct mls_level *l)
165165

166166
if (!l->sens || l->sens > p->p_levels.nprim)
167167
return 0;
168-
levdatum = hashtab_search(p->p_levels.table,
168+
levdatum = hashtab_search(&p->p_levels.table,
169169
sym_name(p, SYM_LEVELS, l->sens - 1));
170170
if (!levdatum)
171171
return 0;
@@ -293,7 +293,7 @@ int mls_context_to_sid(struct policydb *pol,
293293
*(next_cat++) = '\0';
294294

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

315-
catdatum = hashtab_search(pol->p_cats.table, cur_cat);
315+
catdatum = hashtab_search(&pol->p_cats.table, cur_cat);
316316
if (!catdatum)
317317
return -EINVAL;
318318

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

328-
rngdatum = hashtab_search(pol->p_cats.table, rngptr);
328+
rngdatum = hashtab_search(&pol->p_cats.table, rngptr);
329329
if (!rngdatum)
330330
return -EINVAL;
331331

@@ -458,7 +458,7 @@ int mls_convert_context(struct policydb *oldp,
458458
return 0;
459459

460460
for (l = 0; l < 2; l++) {
461-
levdatum = hashtab_search(newp->p_levels.table,
461+
levdatum = hashtab_search(&newp->p_levels.table,
462462
sym_name(oldp, SYM_LEVELS,
463463
oldc->range.level[l].sens - 1));
464464

@@ -470,7 +470,7 @@ int mls_convert_context(struct policydb *oldp,
470470
node, i) {
471471
int rc;
472472

473-
catdatum = hashtab_search(newp->p_cats.table,
473+
catdatum = hashtab_search(&newp->p_cats.table,
474474
sym_name(oldp, SYM_CATS, i));
475475
if (!catdatum)
476476
return -EINVAL;
@@ -506,7 +506,7 @@ int mls_compute_sid(struct policydb *p,
506506
rtr.source_type = scontext->type;
507507
rtr.target_type = tcontext->type;
508508
rtr.target_class = tclass;
509-
r = hashtab_search(p->range_tr, &rtr);
509+
r = hashtab_search(&p->range_tr, &rtr);
510510
if (r)
511511
return mls_range_set(newcontext, r);
512512

0 commit comments

Comments
 (0)