Skip to contentSkip to conversation

selinux: fix memory leak in policydb_init()

PATCH Superseded Ondrej Mosnacek 2 participants 4 messages Jul 25, 2019 – Jul 31, 2019 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selinux: fix memory leak in policydb_init()

Since roles_init() adds some entries to the role hash table, we need to destroy also its keys/values on error, otherwise we get a memory leak in the error path.

Reported-by [email protected]
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 security/selinux/ss/policydb.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
security/selinux/ss/policydb.c +5 -1
index daecdfb15a9c..38d0083204f1 100644
@@ -274,6 +274,8 @@ static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
 	return v;
 }
 
+static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap);
+
 /*
  * Initialize a policy database structure.
  */
@@ -321,8 +323,10 @@ static int policydb_init(struct policydb *p)
 out:
 	hashtab_destroy(p->filename_trans);
 	hashtab_destroy(p->range_tr);
-	for (i = 0; i < SYM_NUM; i++)
+	for (i = 0; i < SYM_NUM; i++) {
+		hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
 		hashtab_destroy(p->symtab[i].table);
+	}
 	return rc;
 }
 
Signature · 1 line
2.21.0
maintainer
#1 lore raw
Quoted 16 lines · Ondrej Mosnacek

Since roles_init() adds some entries to the role hash table, we need to destroy also its keys/values on error, otherwise we get a memory leak in the error path.

Reported-by [email protected]
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 security/selinux/ss/policydb.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
security/selinux/ss/policydb.c +0 -0
index daecdfb15a9c..38d0083204f1 100644

security/selinux/ss/policydb.c static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2) · line 277

Quoted 4 lines
security/selinux/ss/policydb.c +1 -0
@@ -274,6 +274,8 @@ static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
        return v;
 }
 
+static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap);

I'm prefer not to use forward declarations if they can be avoided, and in this particular case it looks like we can move the *_destroy functions up just below policydb_lookup_compat() and avoid the forward declaration while keeping some sanity to the layout of the file.

Yes, the patch does become much larger (378 lines changed in the test patch I just did), but I think the end result is cleaner.

Quoted 18 lines
security/selinux/ss/policydb.c +3 -2
 /*
  * Initialize a policy database structure.
  */
@@ -321,8 +323,10 @@ static int policydb_init(struct policydb *p)
 out:
        hashtab_destroy(p->filename_trans);
        hashtab_destroy(p->range_tr);
-       for (i = 0; i < SYM_NUM; i++)
+       for (i = 0; i < SYM_NUM; i++) {
+               hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
                hashtab_destroy(p->symtab[i].table);
+       }
        return rc;
 }
 
--

2.21.0

Signature · 2 lines
paul moore
www.paul-moore.com
#2 lore raw
Quoted 27 lines · Paul Moore
Quoted 16 lines · Ondrej Mosnacek

Since roles_init() adds some entries to the role hash table, we need to destroy also its keys/values on error, otherwise we get a memory leak in the error path.

Reported-by [email protected]
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 security/selinux/ss/policydb.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
security/selinux/ss/policydb.c +0 -0
index daecdfb15a9c..38d0083204f1 100644

security/selinux/ss/policydb.c static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2) · line 277

Quoted 4 lines
security/selinux/ss/policydb.c +1 -0
@@ -274,6 +274,8 @@ static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
        return v;
 }
 
+static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap);

I'm prefer not to use forward declarations if they can be avoided, and in this particular case it looks like we can move the *_destroy functions up just below policydb_lookup_compat() and avoid the forward declaration while keeping some sanity to the layout of the file.

Quoted 2 lines

Yes, the patch does become much larger (378 lines changed in the test patch I just did), but I think the end result is cleaner.

OK, that'ts reasonable. I'll post an updated v2.

Quoted 24 lines
Quoted 17 lines
security/selinux/ss/policydb.c +3 -2
 /*
  * Initialize a policy database structure.
  */
@@ -321,8 +323,10 @@ static int policydb_init(struct policydb *p)
 out:
        hashtab_destroy(p->filename_trans);
        hashtab_destroy(p->range_tr);
-       for (i = 0; i < SYM_NUM; i++)
+       for (i = 0; i < SYM_NUM; i++) {
+               hashtab_map(p->symtab[i].table, destroy_f[i], NULL);
                hashtab_destroy(p->symtab[i].table);
+       }
        return rc;
 }
 
--
Quoted 2 lines

2.21.0

--
paul moore
www.paul-moore.com

Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com>
Software Engineer, Security Technologies
Red Hat, Inc.
maintainer
#3 lore raw
In reply to Ondrej Mosnacek
Quoted 10 lines · Ondrej Mosnacek

Since roles_init() adds some entries to the role hash table, we need to destroy also its keys/values on error, otherwise we get a memory leak in the error path.

Reported-by [email protected]
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 security/selinux/ss/policydb.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

As discussed in the v2 thread, I've gone ahead and merge this patch into selinux/stable-5.3. Assuming all is well I'll send this to Linus later this week.

Signature · 2 lines
paul moore
www.paul-moore.com