Skip to contentSkip to conversation

libselinux: fix some memory issues in db_init

PATCH In review Jie Lu 2 participants 3 messages Nov 22, 2022 – Dec 16, 2022 SELinux security module

lore
Share & tools
lore

Jie Lu

author
#0 lore raw

PATCH v2 1/1 libselinux: fix some memory issues in db_init

1. check the return of strdup to avoid a potential NULL reference.
2. make sure line_buf is freed.

---
 libselinux/src/label_db.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
libselinux/src/label_db.c +7 -1
index 94c05c6d..bd73201c 100644
@@ -293,6 +293,11 @@ db_init(const struct selinux_opt *opts, unsigned nopts,
 		return NULL;
 	}
 	rec->spec_file = strdup(path);
+	if (!rec->spec_file) {
+                free(catalog);
+                fclose(filp);
+                return NULL;
+	}
 
 	/*
 	 * Parse for each lines
@@ -322,18 +327,19 @@ db_init(const struct selinux_opt *opts, unsigned nopts,
 		if (process_line(path, line_buf, ++line_num, catalog) < 0)
 			goto out_error;
 	}
-	free(line_buf);
 
 	if (digest_add_specfile(rec->digest, filp, NULL, sb.st_size, path) < 0)
 		goto out_error;
 
 	digest_gen_hash(rec->digest);
 
+	free(line_buf);
 	fclose(filp);
 
 	return catalog;
 
 out_error:
+	free(line_buf);
 	for (i = 0; i < catalog->nspec; i++) {
 		spec_t	       *spec = &catalog->specs[i];
 
Signature · 1 line
2.27.0
Acked-by on v2 1/1
#1 lore raw
Quoted 5 lines · Jie Lu

1. check the return of strdup to avoid a potential NULL reference.
2. make sure line_buf is freed.

Quoted 44 lines
---
 libselinux/src/label_db.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
libselinux/src/label_db.c +7 -2
index 94c05c6d..bd73201c 100644
@@ -293,6 +293,11 @@ db_init(const struct selinux_opt *opts, unsigned nopts,
                return NULL;
        }
        rec->spec_file = strdup(path);
+       if (!rec->spec_file) {
+                free(catalog);
+                fclose(filp);
+                return NULL;
+       }
 
        /*
         * Parse for each lines
@@ -322,18 +327,19 @@ db_init(const struct selinux_opt *opts, unsigned nopts,
                if (process_line(path, line_buf, ++line_num, catalog) < 0)
                        goto out_error;
        }
-       free(line_buf);
 
        if (digest_add_specfile(rec->digest, filp, NULL, sb.st_size, path) < 0)
                goto out_error;
 
        digest_gen_hash(rec->digest);
 
+       free(line_buf);
        fclose(filp);
 
        return catalog;
 
 out_error:
+       free(line_buf);
        for (i = 0; i < catalog->nspec; i++) {
                spec_t         *spec = &catalog->specs[i];
 
--

2.27.0

#2 lore raw
Quoted 9 lines · James Carter
Quoted 5 lines · Jie Lu

1. check the return of strdup to avoid a potential NULL reference.
2. make sure line_buf is freed.

Merged.
Thanks,
Jim

Quoted 45 lines
Quoted 45 lines
---
 libselinux/src/label_db.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
libselinux/src/label_db.c +7 -2
index 94c05c6d..bd73201c 100644
@@ -293,6 +293,11 @@ db_init(const struct selinux_opt *opts, unsigned nopts,
                return NULL;
        }
        rec->spec_file = strdup(path);
+       if (!rec->spec_file) {
+                free(catalog);
+                fclose(filp);
+                return NULL;
+       }
 
        /*
         * Parse for each lines
@@ -322,18 +327,19 @@ db_init(const struct selinux_opt *opts, unsigned nopts,
                if (process_line(path, line_buf, ++line_num, catalog) < 0)
                        goto out_error;
        }
-       free(line_buf);
 
        if (digest_add_specfile(rec->digest, filp, NULL, sb.st_size, path) < 0)
                goto out_error;
 
        digest_gen_hash(rec->digest);
 
+       free(line_buf);
        fclose(filp);
 
        return catalog;
 
 out_error:
+       free(line_buf);
        for (i = 0; i < catalog->nspec; i++) {
                spec_t         *spec = &catalog->specs[i];
 
--

2.27.0