Skip to content

Commit

Permalink
libselinux: free data on selabel open failure
Browse files Browse the repository at this point in the history
In case the init function for a selabel backend fails, free the possible
already allocated data:

    Direct leak of 16 byte(s) in 1 object(s) allocated from:
        #0 0x5e7e2bf001e3 in malloc (/tmp/destdir/usr/sbin/selabel_digest+0xc71e3)
        #1 0x7233764baa65 in selabel_media_init /home/christian/Coding/workspaces/selinux/libselinux/src/label_media.c:226:30
        #2 0x7233764ac1fe in selabel_open /home/christian/Coding/workspaces/selinux/libselinux/src/label.c:227:6
        #3 0x5e7e2bf3ebfc in main /home/christian/Coding/workspaces/selinux/libselinux/utils/selabel_digest.c:125:8
        #4 0x7233761856c9 in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16

    SUMMARY: AddressSanitizer: 16 byte(s) leaked in 1 allocation(s).

Signed-off-by: Christian Göttsche <[email protected]>
Acked-by: James Carter <[email protected]>
  • Loading branch information
cgzones authored and jwcart2 committed Mar 20, 2024
1 parent 994b9b2 commit 5876aca
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 4 deletions.
5 changes: 1 addition & 4 deletions libselinux/src/label.c
Expand Up @@ -225,10 +225,7 @@ struct selabel_handle *selabel_open(unsigned int backend,
rec->digest = selabel_is_digest_set(opts, nopts);

if ((*initfuncs[backend])(rec, opts, nopts)) {
if (rec->digest)
selabel_digest_fini(rec->digest);
free(rec->spec_file);
free(rec);
selabel_close(rec);
rec = NULL;
}

Expand Down
3 changes: 3 additions & 0 deletions libselinux/src/label_backends_android.c
Expand Up @@ -246,6 +246,9 @@ static void closef(struct selabel_handle *rec)
struct spec *spec;
unsigned int i;

if (!data)
return;

for (i = 0; i < data->nspec; i++) {
spec = &data->spec_arr[i];
free(spec->property_key);
Expand Down
3 changes: 3 additions & 0 deletions libselinux/src/label_db.c
Expand Up @@ -178,6 +178,9 @@ db_close(struct selabel_handle *rec)
spec_t *spec;
unsigned int i;

if (!catalog)
return;

for (i = 0; i < catalog->nspec; i++) {
spec = &catalog->specs[i];
free(spec->key);
Expand Down
3 changes: 3 additions & 0 deletions libselinux/src/label_file.c
Expand Up @@ -904,6 +904,9 @@ static void closef(struct selabel_handle *rec)
struct stem *stem;
unsigned int i;

if (!data)
return;

selabel_subs_fini(data->subs);
selabel_subs_fini(data->dist_subs);

Expand Down
3 changes: 3 additions & 0 deletions libselinux/src/label_media.c
Expand Up @@ -167,6 +167,9 @@ static void close(struct selabel_handle *rec)
struct spec *spec, *spec_arr = data->spec_arr;
unsigned int i;

if (!data)
return;

for (i = 0; i < data->nspec; i++) {
spec = &spec_arr[i];
free(spec->key);
Expand Down
3 changes: 3 additions & 0 deletions libselinux/src/label_x.c
Expand Up @@ -194,6 +194,9 @@ static void close(struct selabel_handle *rec)
struct spec *spec, *spec_arr = data->spec_arr;
unsigned int i;

if (!data)
return;

for (i = 0; i < data->nspec; i++) {
spec = &spec_arr[i];
free(spec->key);
Expand Down

0 comments on commit 5876aca

Please sign in to comment.