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.

Signed-off-by: Jie Lu <lujie54@huawei.com>
Acked-by: James Carter <jwcart2@gmail.com>
This commit is contained in:
Jie Lu 2022-11-22 13:21:10 +08:00 committed by James Carter
parent fb7f35495f
commit 001af27a6d

View File

@ -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];