Skip to content

Commit dc27f3c

Browse files
WOnder93pcmoore
authored andcommitted
selinux: fix NULL-pointer dereference when hashtab allocation fails
When the hash table slot array allocation fails in hashtab_init(), h->size is left initialized with a non-zero value, but the h->htable pointer is NULL. This may then cause a NULL pointer dereference, since the policydb code relies on the assumption that even after a failed hashtab_init(), hashtab_map() and hashtab_destroy() can be safely called on it. Yet, these detect an empty hashtab only by looking at the size. Fix this by making sure that hashtab_init() always leaves behind a valid empty hashtab when the allocation fails. Cc: [email protected] Fixes: 03414a4 ("selinux: do not allocate hashtabs dynamically") Signed-off-by: Ondrej Mosnacek <[email protected]> Signed-off-by: Paul Moore <[email protected]>
1 parent 32a370a commit dc27f3c

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

security/selinux/ss/hashtab.c

+12-5
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,20 @@ static u32 hashtab_compute_size(u32 nel)
3131

3232
int hashtab_init(struct hashtab *h, u32 nel_hint)
3333
{
34-
h->size = hashtab_compute_size(nel_hint);
34+
u32 size = hashtab_compute_size(nel_hint);
35+
36+
/* should already be zeroed, but better be safe */
3537
h->nel = 0;
36-
if (!h->size)
37-
return 0;
38+
h->size = 0;
39+
h->htable = NULL;
3840

39-
h->htable = kcalloc(h->size, sizeof(*h->htable), GFP_KERNEL);
40-
return h->htable ? 0 : -ENOMEM;
41+
if (size) {
42+
h->htable = kcalloc(size, sizeof(*h->htable), GFP_KERNEL);
43+
if (!h->htable)
44+
return -ENOMEM;
45+
h->size = size;
46+
}
47+
return 0;
4148
}
4249

4350
int __hashtab_insert(struct hashtab *h, struct hashtab_node **dst,

0 commit comments

Comments
 (0)