Skip to contentSkip to conversation

libsepol: invalidate the pointer to the policydb if policydb_init fails

PATCH In review Nicolas Iooss 2 participants 3 messages Feb 28, 2021 – Mar 4, 2021 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsepol: invalidate the pointer to the policydb if policydb_init fails

Facebook's Infer static analyzer warns about a use-after-free issue in libsemanage:

    int semanage_direct_mls_enabled(semanage_handle_t * sh)
    {
            sepol_policydb_t *p = NULL;
            int retval;

            retval = sepol_policydb_create(&p);
            if (retval < 0)
                    goto cleanup;

            /* ... */
    cleanup:
            sepol_policydb_free(p);
            return retval;
    }

When sepol_policydb_create() is called, p is allocated and policydb_init() is called. If this second call fails, p is freed andsepol_policydb_create() returns -1, but p still stores a pointer to freed memory. This pointer is then freed again in the cleanup part of semanage_direct_mls_enabled().

Fix this by setting p to NULL in sepol_policydb_create() after freeing it.

---
 libsepol/src/policydb_public.c | 1 +
 1 file changed, 1 insertion(+)
libsepol/src/policydb_public.c +1 -0
index e5def7078eb0..0218c9403856 100644
@@ -68,6 +68,7 @@ int sepol_policydb_create(sepol_policydb_t ** sp)
 	p = &(*sp)->p;
 	if (policydb_init(p)) {
 		free(*sp);
+		*sp = NULL;
 		return -1;
 	}
 	return 0;
Signature · 1 line
2.30.0
Acked-by on v1 1/1
#1 lore raw
Quoted 25 lines · Nicolas Iooss

Facebook's Infer static analyzer warns about a use-after-free issue in libsemanage:

    int semanage_direct_mls_enabled(semanage_handle_t * sh)
    {
            sepol_policydb_t *p = NULL;
            int retval;

            retval = sepol_policydb_create(&p);
            if (retval < 0)
                    goto cleanup;

            /* ... */
    cleanup:
            sepol_policydb_free(p);
            return retval;
    }

When sepol_policydb_create() is called, p is allocated and policydb_init() is called. If this second call fails, p is freed andsepol_policydb_create() returns -1, but p still stores a pointer to freed memory. This pointer is then freed again in the cleanup part of semanage_direct_mls_enabled().

Quoted 4 lines

Fix this by setting p to NULL in sepol_policydb_create() after freeing it.

Quoted 19 lines
---
 libsepol/src/policydb_public.c | 1 +
 1 file changed, 1 insertion(+)
libsepol/src/policydb_public.c +1 -1
index e5def7078eb0..0218c9403856 100644
@@ -68,6 +68,7 @@ int sepol_policydb_create(sepol_policydb_t ** sp)
        p = &(*sp)->p;
        if (policydb_init(p)) {
                free(*sp);
+               *sp = NULL;
                return -1;
        }
        return 0;
--

2.30.0

#2 lore raw
Quoted 27 lines · James Carter
Quoted 25 lines · Nicolas Iooss

Facebook's Infer static analyzer warns about a use-after-free issue in libsemanage:

    int semanage_direct_mls_enabled(semanage_handle_t * sh)
    {
            sepol_policydb_t *p = NULL;
            int retval;

            retval = sepol_policydb_create(&p);
            if (retval < 0)
                    goto cleanup;

            /* ... */
    cleanup:
            sepol_policydb_free(p);
            return retval;
    }

When sepol_policydb_create() is called, p is allocated and policydb_init() is called. If this second call fails, p is freed andsepol_policydb_create() returns -1, but p still stores a pointer to freed memory. This pointer is then freed again in the cleanup part of semanage_direct_mls_enabled().

Quoted 6 lines
Quoted 4 lines

Fix this by setting p to NULL in sepol_policydb_create() after freeing it.

Merged.
Nicolas

Quoted 19 lines
Quoted 19 lines
---
 libsepol/src/policydb_public.c | 1 +
 1 file changed, 1 insertion(+)
libsepol/src/policydb_public.c +1 -1
index e5def7078eb0..0218c9403856 100644
@@ -68,6 +68,7 @@ int sepol_policydb_create(sepol_policydb_t ** sp)
        p = &(*sp)->p;
        if (policydb_init(p)) {
                free(*sp);
+               *sp = NULL;
                return -1;
        }
        return 0;
--

2.30.0