Home

Awesome

libdheap

A shared (dynamic) library that can be transparently injected into different processes to detect memory corruption in glibc heap.

It works by intercepting and wrapping over libc's malloc() and free() while maintaining information about various chunks in an intermediate data storage (also on the heap). Also, canaries are added before and after heap chunks to detect overflows.

+------------------+                               +---------------------+
|                  |       malloc(), free()        |                     |
|      User        | ----------------------------> |      libdheap       |
|     Process      | <---------------------------- | (injected library)  |
|                  |         intercepted           |                     |
+------------------+                               +---------------------+
       | ^                                                   | ^
       | |                               __libc_**           | |
       | |                          +------------------------+ |
       | |                          | +------------------------+
       | |                          | |
       | |                          v |
       | |                   +---------------+               +------------+-----------+
       | |  printf(), etc.   |               |    chunks     |            |           |
       | +------------------ |     glibc     | ------------> |  user      | libdheap  |
       +-------------------> |    library    | <------------ |  data      |   data    |
                             |               |               |            |           |
                             +---------------+               +------------+-----------+
                                    ^ |                             Heap Memory
                                    | |
                                    | | brk(), mmap()
                                    | |
     -------------------------------------------------------------------------
                                    | |                Operating System
                                    | v
                             +---------------+
                             |               |
                             |    kernel     |
                             |               |
                             +---------------+

Features

Installation

This library is not portable and works only with glibc.

To install, clone this repository and cd to it:

git clone https://github.com/DhavalKapil/libdheap

Run make:

make

The shared library will be generated: libdheap.so

Usage

To run any program with libdheap, load the library using LD_PRELOAD environment variable.

LD_PRELOAD=/path/to/libdheap.so gedit

libdheap will output any error/log to standard error by default. You might want to redirect the output to some external file.

[LIBDHEAP LOG] : Freeing non allocated chunk!
[LIBDHEAP LOG] : Printing Stack Trace ====>
[LIBDHEAP LOG] :    0x400604
[LIBDHEAP LOG] :    0x2b3b8016ff45
[LIBDHEAP LOG] : <==== End of Stack Trace

libdheap allows setting two configuration options (through environment variables) as follow:

  1. LIBDHEAP_DEBUG: If 1, will output debugging statements along with errors and logs.
  2. LIBDHEAP_EXIT_ON_ERROR: If 1, will exit the instant it detects any memory corruption error.

By default, both are set to 0. Use the following command to configure:

LD_PRELOAD=/path/to/libdheap.so LIBDHEAP_DEBUG=1 gedit

Note: If debugging is enabled, it is advised to redirect output to an external file (libdheap outputs a lot of things). Also, this library is not developed for using in production, since it slows the application by approximately 5 times.

Implementation details

Contribution

Feel free to file issues and submit pull requests – contributions are welcome.

License

libdheap is licensed under the MIT license.