Phrack Heap Hacking

Phrack Heap Hacking

PHRACK HEAP HACKING Contents 1. Pseudomonarchia jemallocum – argp, huku ....................................................................................... 3 2. The House Of Lore: Reloaded - blackngel ......................................................................................... 53 3. Malloc des-maleficarum - blackngel .................................................................................................. 99 4. Yet another free() exploitation technique - huku ........................................................................... 145 5. The use of set_head to defeat the wilderness – g463 .................................................................... 169 6. OS X heap exploitation techniques - nemo ..................................................................................... 209 7. Advanced Doug lea's malloc exploits - jp ........................................................................................ 227 8. The Malloc Maleficarum - Phantasmal Phantasmagoria ................................................................ 269 9. Exploiting The Wilderness - Phantasmal Phantasmagoria .............................................................. 288 1. Pseudomonarchia jemallocum – argp, huku ==Phrack Inc.== Volume 0x0e, Issue 0x44, Phile #0x0a of 0x13 |=-----------------------------------------------------------------------=| |=-------------------=[ Pseudomonarchia jemallocum ]=--------------------=| |=-----------------------------------------------------------------------=| |=---------------=[ The false kingdom of jemalloc, or ]=------------------| |=-----------=[ On exploiting the jemalloc memory manager ]=-------------=| |=-----------------------------------------------------------------------=| |=------------------------=[ argp | huku ]=------------------------=| |=--------------------=[ {argp,huku}@grhack.net ]=---------------------=| |=-----------------------------------------------------------------------=| --[ Table of contents 1 - Introduction 1.1 - Thousand-faced jemalloc 2 - jemalloc memory allocator overview 2.1 - Basic structures 2.1.1 - Chunks (arena_chunk_t) 2.1.2 - Arenas (arena_t) 2.1.3 - Runs (arena_run_t) 2.1.4 - Regions/Allocations 2.1.5 - Bins (arena_bin_t) 2.1.6 - Huge allocations 2.1.7 - Thread caches (tcache_t) 2.1.8 - Unmask jemalloc 2.2 - Algorithms 3 - Exploitation tactics 3.1 - Adjacent region corruption 3.2 - Heap manipulation 3.3 - Metadata corruption 3.3.1 - Run (arena_run_t) 3.3.2 - Chunk (arena_chunk_t) 3.3.3 - Thread caches (tcache_t) 4 - A real vulnerability 5 - Future work 6 - Conclusion 7 - References 8 - Code --[ 1 - Introduction In this paper we investigate the security of the jemalloc allocator in both theory and practice. We are particularly interested in the exploitation of memory corruption bugs, so our security analysis will be biased towards that end. jemalloc is a userland memory allocator. It provides an implementation for the standard malloc(3) interface for dynamic memory management. It was written by Jason Evans (hence the 'je') for FreeBSD since there was a need for a high performance, SMP-enabled memory allocator for libc. After that, jemalloc was also used by the Mozilla Firefox browser as its internal dedicated custom memory allocator. All the above have led to a few versions of jemalloc that are very [ 1. Pseudomonarchia jemallocum – argp, huku] similar but not exactly the same. To summarize, there are three different widely used versions of jemalloc: 1) the standalone version [JESA], 2) the version in the Mozilla Firefox web browser [JEMF], and 3) the FreeBSD libc [JEFB] version. The exploitation vectors we investigate in this paper have been tested on the jemalloc versions presented in subsection 1.1, all on the x86 platform. We assume basic knowledge of x86 and a general familiarity with userland malloc() implementations, however these are not strictly required. ----[ 1.1 - Thousand-faced jemalloc There are so many different jemalloc versions that we almost went crazy double checking everything in all possible platforms. Specifically, we tested the latest standalone jemalloc version (2.2.3 at the time of this writing), the version included in the latest FreeBSD libc (8.2-RELEASE), and the Mozilla Firefox web browser version 11.0. Furthermore, we also tested the Linux port of the FreeBSD malloc(3) implementation (jemalloc_linux_20080828a in the accompanying code archive) [JELX]. --[ 2 - jemalloc memory allocator overview The goal of this section is to provide a technical overview of the jemalloc memory allocator. However, it is not all-inclusive. We will only focus on the details that are useful for understanding the exploitation attacks against jemalloc analyzed in the next section. The interested reader can look in [JE06] for a more academic treatment of jemalloc (including benchmarks, comparisons with other allocators, etc). Before we start our analysis we would like to point out that jemalloc (as well as other malloc implementations) does not implement concepts like 'unlinking' or 'frontlinking' which have proven to be catalytic for the exploitation of dlmalloc and Microsoft Windows allocators. That said, we would like to stress the fact that the attacks we are going to present do not directly achieve a write-4-anywhere primitive. We, instead, focus on how to force malloc() (and possibly realloc()) to return a chunk that will most likely point to an already initialized memory region, in hope that the region in question may hold objects important for the functionality of the target application (C++ VPTRs, function pointers, buffer sizes and so on). Considering the various anti-exploitation countermeasures present in modern operating systems (ASLR, DEP and so on), we believe that such an outcome is far more useful for an attacker than a 4 byte overwrite. jemalloc, as a modern memory allocator should, recognizes that minimal page utilization is no longer the most critical feature. Instead it focuses on enhanced performance in retrieving data from the RAM. Based on the principle of locality which states that items that are allocated together are also used together, jemalloc tries to situate allocations contiguously in memory. Another fundamental design choice of jemalloc is its support for SMP systems and multi-threaded applications by trying to avoid lock contention problems between many simultaneously running threads. This is achieved by using many 'arenas' and the first time a thread calls into the memory allocator (for example by calling malloc(3)) it is associated with a specific arena. The assignment of threads to arenas happens with three possible algorithms: 1) with a simple hashing on the thread's ID if TLS is available 2) with a simple builtin linear congruential pseudo random number generator in case MALLOC_BALANCE is defined and TLS is not available 3) or with the traditional round-robin Page 4 [ 1. Pseudomonarchia jemallocum – argp, huku] algorithm. For the later two cases, the association between a thread and an arena doesn't stay the same for the whole life of the thread. Continuing our high-level overview of the main jemalloc structures before we dive into the details in subsection 2.1, we have the concept of 'chunks'. jemalloc divides memory into chunks, always of the same size, and uses these chunks to store all of its other data structures (and user-requested memory as well). Chunks are further divided into 'runs' that are responsible for requests/allocations up to certain sizes. A run keeps track of free and used 'regions' of these sizes. Regions are the heap items returned on user allocations (e.g. malloc(3) calls). Finally, each run is associated with a 'bin'. Bins are responsible for storing structures (trees) of free regions. The following diagram illustrates in an abstract manner the relationships between the basic building blocks of jemalloc. Chunk #0 Chunk #1 .--------------------------------. .--------------------------------. | | | | | Run #0 Run #1 | | Run #0 Run #1 | | .-------------..-------------. | | .-------------..-------------. | | | || | | | | || | | | | Page || Page | | | | Page || Page | | | | .---------. || .---------. | | | | .---------. || .---------. | | | | | | || | | | | | | | | || | | | | ... | | | Regions | || | Regions | | | | | | Regions | || | Regions | | | | | |[] [] [] | || |[] [] [] | | | | | |[] [] [] | || |[] [] [] | | | | | | ^ ^ | || | | | | | | | ^ ^ | || | | | | | | `-|-----|-' || `---------' | | | | `-|-----|-' || `---------' | | | `---|-----|---'`-------------' | | `---|-----|---'`-------------' | `-----|-----|--------------------' `-----|-----|--------------------' | | | | | | | | .---|-----|----------. .---|-----|----------. | | | | | | | | | free regions' tree | ... | free regions' tree | ... | | | | `--------------------' `--------------------' bin[Chunk #0][Run #0] bin[Chunk #1][Run #0] ----[ 2.1 - Basic structures In the following paragraphs we analyze in detail the basic jemalloc structures. Familiarity with these structures is essential in order to begin our understanding of the jemalloc internals and proceed to the exploitation step. ------[ 2.1.1 - Chunks (arena_chunk_t) If you are familiar with Linux heap exploitation (and more precisely with dlmalloc internals) you have probably heard of the term 'chunk' before. In dlmalloc, the term 'chunk' is used to denote the memory regions returned by malloc(3) to the end user. We hope you get over it soon because when it comes to jemalloc the term 'chunk' is used to describe big virtual memory regions

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    301 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us