Skip to content

Commit 6a1afff

Browse files
WOnder93pcmoore
authored andcommitted
selinux: fix NULL dereference in policydb_destroy()
The conversion to kvmalloc() forgot to account for the possibility that p->type_attr_map_array might be null in policydb_destroy(). Fix this by destroying its contents only if it is not NULL. Also make sure ebitmap_init() is called on all entries before policydb_destroy() can be called. Right now this is a no-op, because both kvcalloc() and ebitmap_init() just zero out the whole struct, but let's rather not rely on a specific implementation. Reported-by: syzbot+a57b2aff60832666fc28@syzkaller.appspotmail.com Fixes: acdf52d ("selinux: convert to kvmalloc") Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com> Acked-by: Stephen Smalley <sds@tycho.nsa.gov> Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent 9e98c67 commit 6a1afff

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

security/selinux/ss/policydb.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -828,9 +828,11 @@ void policydb_destroy(struct policydb *p)
828828
hashtab_map(p->range_tr, range_tr_destroy, NULL);
829829
hashtab_destroy(p->range_tr);
830830

831-
for (i = 0; i < p->p_types.nprim; i++)
832-
ebitmap_destroy(&p->type_attr_map_array[i]);
833-
kvfree(p->type_attr_map_array);
831+
if (p->type_attr_map_array) {
832+
for (i = 0; i < p->p_types.nprim; i++)
833+
ebitmap_destroy(&p->type_attr_map_array[i]);
834+
kvfree(p->type_attr_map_array);
835+
}
834836

835837
ebitmap_destroy(&p->filename_trans_ttypes);
836838
ebitmap_destroy(&p->policycaps);
@@ -2496,10 +2498,13 @@ int policydb_read(struct policydb *p, void *fp)
24962498
if (!p->type_attr_map_array)
24972499
goto bad;
24982500

2501+
/* just in case ebitmap_init() becomes more than just a memset(0): */
2502+
for (i = 0; i < p->p_types.nprim; i++)
2503+
ebitmap_init(&p->type_attr_map_array[i]);
2504+
24992505
for (i = 0; i < p->p_types.nprim; i++) {
25002506
struct ebitmap *e = &p->type_attr_map_array[i];
25012507

2502-
ebitmap_init(e);
25032508
if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
25042509
rc = ebitmap_read(e, fp);
25052510
if (rc)

0 commit comments

Comments
 (0)