A general purpose memory allocator that implements an isolation security strategy to mitigate memory safety issues while maintaining good performance
A general purpose memory allocator that implements an isolation security strategy to mitigate memory safety issues while maintaining good performance
Isolation Alloc (or IsoAlloc) is a secure and fast(ish) memory allocator written in C11. It is a drop in replacement for malloc on Linux / Mac OS using LD_PRELOAD or DYLD_INSERT_LIBRARIES respectively. Its security strategy is originally inspired by Chrome's PartitionAlloc. A memory allocation isolation security strategy is best summed up as maintaining spatial separation, or isolation between objects of different sizes or types. While IsoAlloc wraps malloc and enforces naive isolation by default very strict isolation of allocations can be achieved using the APIs directly.
IsoAlloc is designed and tested for 64 bit Linux and MacOS. The space afforded by a 64 bit process makes this possible, therefore Isolation Alloc does not support 32 bit targets. The number of bits of entropy provided to mmap based page allocations is far too low in a 32 bit process to provide much security value. It may work on operating systems other than Linux/MacOS but that is also untested at this time. There is partial FreeBSD support but CI is often flakey. A minimal Solaris/Illumos support is available, LTO not supported by the compilers backends.
At a high level IsoAlloc creates zones which are used to manage regions of memory that hold individual allocations of a specific size. If you are familiar with the implementation of arenas in other heap allocators then the concepts here will be familiar to you.
There is one iso_alloc_root structure which contains a pointer to a fixed number of iso_alloc_zone structures. These iso_alloc_zone structures are referred to as zones. Zones point to user chunks and a bitmap that is used to manage those chunks. The translation between bitmap and user chunks is referred to as bit slots. The pages that back both the user chunks and the bitmap are allocated separately. The pointers that reference these in the zone meta data are masked in between allocation and free operations. The bitmap contains 2 bits of state per user chunk. The current bit value specification is as follows:
00 free chunk10 currently in use01 was used but is now free11 canary chunkAll user chunk pages and bitmap pages are surrounded by guard page allocations with the PROT_NONE permission. Zones are created for specific sizes, or manually created through the exposed API for a particular size or object type. Internally managed zones will live for the entire lifetime of the process, but zones created via the API can be destroyed at any time.
If DEBUG, LEAK_DETECTOR, or MEM_USAGE are specified during compilation a memory leak and memory usage routine will be called from the destructor which will print useful information about the state of the heap at that time. These can also be invoked via the API, which is documented further below.
iso_alloc_root structure is thread safe and guarded by a mutex or spinlock when THREAD_SUPPORT is enabled.SMALL_SIZE_MAX live in big zones which have a size limitation of 4 GB. See PERFORMANCE.There is support for Address Sanitizer, Memory Sanitizer, and Undefined Behavior Sanitizer. If you want to enable it just uncomment the ENABLE_ASAN, ENABLE_MSAN, or ENABLE_UBSAN flags in the Makefile. Like any other usage of Address Sanitizer these are mutually exclusive. IsoAlloc will use Address Sanitizer macros to poison and unpoison user chunks appropriately. IsoAlloc still catches a number of issues Address Sanitizer does not, including double/unaligned/wild free's.
A feature similar to GWP-ASAN can be enabled with ALLOC_SANITY in the Makefile. It samples calls to iso_alloc/malloc and allocates a page of memory surrounded by guard pages in order to detect Use-After-Free and linear heap overflows. All sampled sanity allocations are verified with canaries to detect over/underflows into the surrounding bytes of the page. A percentage of sanity allocations are allocated at end of the page to detect linear overflows. This feature works on all supported platforms.
You can also enable UNINIT_READ_SANITY for detecting uninitialized read vulnerabilities using the userfaultfd syscall. You can read more about that feature here. This feature is only available on Linux and requires ALLOC_SANITY and THREAD_SUPPORT to be enabled.
See the PERFORMANCE documentation for more information.
IsoAlloc is thread safe by way of protecting the root structure with a global lock built with either a pthread mutex, or a C11 atomic_flag when USE_SPINLOCK is enabled. This means every thread that wants to allocate or free a chunk needs to wait until it can take ownership of the lock. This design choice has some tradeoffs. It can negatively impact performance of multi threaded programs that perform a lot of allocations. This is because every thread shares the same set of global zones. The benefit of this is that you can allocate and free any chunk from any thread with no additional complexity required. In order to help alleviate contention on this lock each thread has a zone cache built using thread local storage (TLS). This is implemented as a simple FILO cache of the most recently used zones by that thread. It's size is 8 by default but can be increased modifying the ZONE_CACHE_SZ define in the internal header file. Making this cache too large can lead to negative performance implications for certain allocation patterns. For example, if a thread allocates multiple 32 byte chunks in a row then the cache may be populated entirely by the same zone that holds 32 byte chunks. Now when the thread goes to allocate a 64 byte chunk it iterates through the entire cache, does not find a usable zone, and then has to take the slow path which iterates through all zones again. This cache is also used when thread support is disabled but it does not live in TLS and is instead allocated on its own set of pages. See the PERFORMANCE documentation for more information on the various caches in use in IsoAlloc.
When enabled, the CPU_PIN feature will restrict allocations from a given zone to the CPU core that created that zone. Free operations are not restricted in this way. This mode is compatible with and without thread support, but is only available on Linux, and will introduce a negative performance hit to the hot path and may increase memory usage. The benefit of this mode is that it introduces an isolation mechanism based on CPU core with no configuration beyond enabling the CPU_PIN define in the Makefile.
iso_free.iso_verify_zones or iso_verify_zone(zone).iso_alloc.0x00 to prevent unbounded C string reads from leaking it.iso_free_permanently.SANITIZE_CHUNKS is set all user chunks are cleared when passed to iso_free with the constant 0xde.MASK_PTRS is enabled (default) the user_pages_start and bitmap_start pointers stored in zone metadata are XOR'd with a per-zone random secret between alloc and free operations, making them useless to an attacker who reads or corrupts zone metadata.iso_free that was not allocated with iso_alloc will abort.iso_free must be 8 byte aligned, and a multiple of the zone chunk size.PROT_NONE to prevent use-after-free.realloc will always return a new chunk. Use PERM_FREE_REALLOC to make these free's permanent.FUZZ_MODE in the Makefile to verify all zones upon alloc/free, and never reuse private zones.CPU_PIN is enabled allocation from a zone will be restricted to the CPU core that created it.UAF_PTR_PAGE is enabled calls to iso_free will be sampled to search for dangling references.VERIFY_FREE_BIT_SLOTS to verify there are no duplicates in the bit slot cache upon free.ALLOC_SANITY is enabled a percentage of allocations will be sampled to detect UAF/overflows, see above.mmap to ensure contiguous page ranges are not allocated.ABORT_ON_NULL is enabled IsoAlloc will abort instead of returning NULL.NO_ZERO_ALLOCATIONS will return a pointer to a page marked PROT_NONE for all 0 sized allocations.ABORT_ON_UNOWNED_PTR is enabled (default) IsoAlloc will abort whenever it is passed a pointer it does not own.ABORT_NO_ENTROPY is enabled IsoAlloc will abort when it can't gather enough entropy.RANDOMIZE_FREELIST is enabled IsoAlloc will randomize the free list upon creation. May have a perf hit.ZONE_ALLOC_RETIRE * max_chunk_count_for_zone.BIG_ZONE_ALLOC_RETIRE times.MEMORY_TAGGING When enabled IsoAlloc will create a 1 byte tag for each chunk in private zones. See the MEMORY_TAGGING.md documentation, or this test for an example of how to use it.MEMCPY_SANITY and MEMSET_SANITY Configures the allocator will hook all calls to memcpy/memset and check for out of bounds r/w operations when either src or dst points to a chunk allocated by IsoAllocSTRONG_SIZE_ISOLATION Enables a policy that enforces stronger memory isolation by sizePROTECT_FREE_BIG_ZONES Marks big zones on the free list as PROT_NONE when not in useBIG_ZONE_META_DATA_GUARD Enables guard pages for big zone meta dataBIG_ZONE_GUARD Enables guard pages for big zone user pagesARM_MTE Enables support for the ARM v8.5a Memory Tagging ExtensionThe Makefile targets are very simple:
make library - Builds a release version of the library without C++ support
make library_less_strict - Builds a release library with ABORT_ON_UNOWNED_PTR=0. Recommended when using IsoAlloc via LD_PRELOAD.
make library_debug - Builds a debug version of the library
make library_debug_no_output - Builds a debug version of the library with no logging output
make analyze_library_debug - Builds the
No open issues yet, or sync has not completed.