Skip to contentSkip to conversation

selinux: clean up error path in policydb_init()

PATCH In review Ondrej Mosnacek 3 participants 5 messages Mar 3, 2020 – Mar 5, 2020 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selinux: clean up error path in policydb_init()

Commit e0ac568de1fa ("selinux: reduce the use of hard-coded hash sizes") moved symtab initialization out of policydb_init(), but left the cleanup of symtabs from the error path. This patch fixes the oversight.

---
 security/selinux/ss/policydb.c | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)
security/selinux/ss/policydb.c +5 -13
index 7739369f5d9a..00edcd216aaa 100644
@@ -463,36 +463,28 @@ static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
  */
 static int policydb_init(struct policydb *p)
 {
-	int i, rc;
+	int rc;
 
 	memset(p, 0, sizeof(*p));
 
 	rc = avtab_init(&p->te_avtab);
 	if (rc)
-		goto out;
+		return rc;
 
 	rc = cond_policydb_init(p);
 	if (rc)
-		goto out;
+		return rc;
 
 	p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp,
 					   (1 << 11));
-	if (!p->filename_trans) {
-		rc = -ENOMEM;
-		goto out;
-	}
+	if (!p->filename_trans)
+		return -ENOMEM;
 
 	ebitmap_init(&p->filename_trans_ttypes);
 	ebitmap_init(&p->policycaps);
 	ebitmap_init(&p->permissive_map);
 
 	return 0;
-out:
-	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.24.1
maintainer
#1 lore raw
Quoted 23 lines · Ondrej Mosnacek

Commit e0ac568de1fa ("selinux: reduce the use of hard-coded hash sizes") moved symtab initialization out of policydb_init(), but left the cleanup of symtabs from the error path. This patch fixes the oversight.

---
 security/selinux/ss/policydb.c | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)
security/selinux/ss/policydb.c +1 -1
index 7739369f5d9a..00edcd216aaa 100644
@@ -463,36 +463,28 @@ static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
  */
 static int policydb_init(struct policydb *p)
 {
-       int i, rc;
+       int rc;
 
        memset(p, 0, sizeof(*p));

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

Quoted 10 lines
security/selinux/ss/policydb.c +2 -2
 
        rc = avtab_init(&p->te_avtab);
        if (rc)
-               goto out;
+               return rc;
 
        rc = cond_policydb_init(p);
        if (rc)
-               goto out;
+               return rc;

Looks like avtab_init() and cond_policydb_init() can no longer return errors, merely initialize fields to 0/NULL,
which is already done via the memset above, and are not used anywhere else so those can go away entirely?

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

Quoted 13 lines
security/selinux/ss/policydb.c +2 -4
        p->filename_trans = hashtab_create(filenametr_hash, filenametr_cmp,
                                           (1 << 11));
-       if (!p->filename_trans) {
-               rc = -ENOMEM;
-               goto out;
-       }
+       if (!p->filename_trans)
+               return -ENOMEM;
 
        ebitmap_init(&p->filename_trans_ttypes);
        ebitmap_init(&p->policycaps);
        ebitmap_init(&p->permissive_map);

Technically these aren't needed either but I guess we can leave them in case ebitmap_init() does more than just memset
at some point.

#2 lore raw
Quoted 30 lines · Stephen Smalley
Quoted 29 lines · Ondrej Mosnacek

Commit e0ac568de1fa ("selinux: reduce the use of hard-coded hash sizes") moved symtab initialization out of policydb_init(), but left the cleanup of symtabs from the error path. This patch fixes the oversight.

---
 security/selinux/ss/policydb.c | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)
security/selinux/ss/policydb.c +2 -2
index 7739369f5d9a..00edcd216aaa 100644
@@ -463,36 +463,28 @@ static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
  */
 static int policydb_init(struct policydb *p)
 {
-       int i, rc;
+       int rc;
 
        memset(p, 0, sizeof(*p));
 
        rc = avtab_init(&p->te_avtab);
        if (rc)
-               goto out;
+               return rc;
 
Quoted 9 lines

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

Quoted 4 lines
security/selinux/ss/policydb.c +1 -1
        rc = cond_policydb_init(p);
        if (rc)
-               goto out;
+               return rc;

Looks like avtab_init() and cond_policydb_init() can no longer return errors, merely initialize fields to 0/NULL,
which is already done via the memset above, and are not used anywhere else so those can go away entirely?

OK, but that can be done in a separate patch, right? Do you plan to send it? Anyway, I'd prefer to keep the *_init() functions for the sake of abstraction - I'd suggest just changing the return type to void where possible.

Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com>
Software Engineer, Security Technologies
Red Hat, Inc.
maintainer Acked-by
#3 lore raw
Quoted 17 lines · Ondrej Mosnacek
Quoted 6 lines · Stephen Smalley
Quoted 5 lines · Ondrej Mosnacek

Commit e0ac568de1fa ("selinux: reduce the use of hard-coded hash sizes") moved symtab initialization out of policydb_init(), but left the cleanup of symtabs from the error path. This patch fixes the oversight.

Quoted 7 lines
Quoted 3 lines

---

Looks like avtab_init() and cond_policydb_init() can no longer return errors, merely initialize fields to 0/NULL,
which is already done via the memset above, and are not used anywhere else so those can go away entirely?

Quoted 4 lines

OK, but that can be done in a separate patch, right? Do you plan to send it? Anyway, I'd prefer to keep the *_init() functions for the sake of abstraction - I'd suggest just changing the return type to void where possible.

Fair enough.

maintainer
#4 lore raw
In reply to Ondrej Mosnacek
Quoted 42 lines · Ondrej Mosnacek
Quoted 30 lines · Stephen Smalley
Quoted 29 lines · Ondrej Mosnacek

Commit e0ac568de1fa ("selinux: reduce the use of hard-coded hash sizes") moved symtab initialization out of policydb_init(), but left the cleanup of symtabs from the error path. This patch fixes the oversight.

---
 security/selinux/ss/policydb.c | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)
security/selinux/ss/policydb.c +2 -2
index 7739369f5d9a..00edcd216aaa 100644
@@ -463,36 +463,28 @@ static int rangetr_cmp(struct hashtab *h, const void *k1, const void *k2)
  */
 static int policydb_init(struct policydb *p)
 {
-       int i, rc;
+       int rc;
 
        memset(p, 0, sizeof(*p));
 
        rc = avtab_init(&p->te_avtab);
        if (rc)
-               goto out;
+               return rc;
 
Quoted 9 lines

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

Quoted 4 lines
security/selinux/ss/policydb.c +1 -1
        rc = cond_policydb_init(p);
        if (rc)
-               goto out;
+               return rc;

Looks like avtab_init() and cond_policydb_init() can no longer return errors, merely initialize fields to 0/NULL,
which is already done via the memset above, and are not used anywhere else so those can go away entirely?

Quoted 4 lines

OK, but that can be done in a separate patch, right? Do you plan to send it? Anyway, I'd prefer to keep the *_init() functions for the sake of abstraction - I'd suggest just changing the return type to void where possible.

I tend to agree. Merged into selinux/next.

I'm also not seeing a patch from anyone to change the return type so I'll put one together now.

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