]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
leak-detective: Try to properly free allocations after deinitialization
authorTobias Brunner <tobias@strongswan.org>
Mon, 27 Jun 2016 16:04:39 +0000 (18:04 +0200)
committerTobias Brunner <tobias@strongswan.org>
Wed, 29 Jun 2016 09:09:38 +0000 (11:09 +0200)
If a function we whitelist allocates memory while leak detective is enabled
but only frees it after LD has already been disabled, free() will get called
with invalid pointers (not pointing to the actually allocated memory by LD),
which will cause checks in the C library to fail and the program to crash.
This tries to detect such cases and calling free with the correct pointer.

src/libstrongswan/utils/leak_detective.c

index aeadc0cb381c8d3ef6c4e78aedc48ff5cb4042f5..d0f646c31531bb6b08456ebf63dc6087469534ac 100644 (file)
@@ -844,6 +844,18 @@ HOOK(void, free, void *ptr)
 
        if (!enabled || thread_disabled->get(thread_disabled))
        {
+               /* after deinitialization we might have to free stuff we allocated
+                * while we were enabled */
+               if (!first_header.magic && ptr)
+               {
+                       hdr = ptr - sizeof(memory_header_t);
+                       tail = ptr + hdr->bytes;
+                       if (hdr->magic == MEMORY_HEADER_MAGIC &&
+                               tail->magic == MEMORY_TAIL_MAGIC)
+                       {
+                               ptr = hdr;
+                       }
+               }
                real_free(ptr);
                return;
        }
@@ -960,6 +972,7 @@ METHOD(leak_detective_t, destroy, void,
        lock->destroy(lock);
        thread_disabled->destroy(thread_disabled);
        free(this);
+       first_header.magic = 0;
        first_header.next = NULL;
 }