Engineering Heap Overflow Exploits with Javascript

Engineering Heap Overflow Exploits with Javascript

Engineering Heap Overflow Exploits with JavaScript Mark Daniel Jake Honoroff Independent Security Evaluators Independent Security Evaluators Charlie Miller Independent Security Evaluators Abstract low for attacker control over the target heap. In this pa- per we describe a new technique, inspired by his Heap This paper presents a new technique for exploiting heap Feng Shui, that can be used to reliably position function overflows in JavaScript interpreters. Briefly, given a heap pointers for later smashing with a heap overflow. overflow, JavaScript commands can be used to insure that a function pointer is reliably present for smashing, just This paper contains a description of the technique fol- after the overflown buffer. A case study serves to high- lowed by an account of its application to a WebKit vul- light the technique: the Safari exploit that the authors nerability discovered by the authors and used to win the used to win the 2008 CanSecWest Pwn2Own contest. 2008 CanSecWest Pwn2Own contest. 1 Introduction 2 Technique Many buffer and integer overflow vulnerabilities allow for a somewhat arbitrary set of values to be written at a relative offset to a pointer on the heap. Unfortunately for 2.1 Context the attacker, often the data following the pointer is un- predictable, making exploitation difficult and unreliable. Broadly, this technique can be used to develop client side The most ideal heap overflow, in terms of full attacker exploits against web browsers that use JavaScript. In this control over the quantity and values of overflow bytes, context, the attacker crafts a web page containing(among can be virtually unexploitable if nothing interesting and other things) JavaScript commands, and induces the vic- predictable is waiting to be overwritten. tim to browse the page. Using particular JavaScript com- Thanks to safe unlinking protections, the heap meta- mands, the attacker influences the state of the heap in data structures are often no longer a viable target for the victim’s browser process to arrange for a successful overflows. Currently, application specific data is usually attack. needed as an overflowtarget, where normal programflow results in the calling of a function pointer that has been More specifically, the technique is applicable when overwritten with an attacker supplied shellcode address. one has a heap overflow in hand. In what follows, we However, such exploits are in no way guaranteed to be refer to the buffer that is overflown as the vulnerable reliable. It must be the case that pointers yet to be ac- buffer. This heap overflow must have the property that cessed are sitting on the heap after the overflown buffer, both allocation of the vulnerable buffer and the over- and no other critical data or unmapped memory lies in flow itself must be triggerable within the JavaScript in- between, the smashing of which would result in a pre- terpreter. In particular, this technique will not apply in mature crash. Such ideal circumstances can certainly be situations where the vulnerable buffer has already been rare for an arbitrary application vulnerability. allocated before the JavaScript interpreter has been in- However, given access to a client-side scripting lan- stantiated. guage such as JavaScript, an attacker may be able to cre- We also assume that shellcode is available and a mech- ate these ideal circumstances for vulnerabilities in appli- anism for loading it into memory has already been found. cations like web browsers. In [2], Sotirov describes how This is trivial with JavaScript – just load it into a big to use JavaScript allocations in Internet Explorer to al- string. Figure 1: Fragmented heap. Figure 2: Defragmented heap. Future allocations end up adjacent. 2.2 Overview browser) is unpredictable. Generally, such a heap is frag- mented in that it has many holes of free memory. The Keep in mind that the goal is to control a buffer in the presence of varying sized holes of free memory means heap immediately following the vulnerable buffer. We that the addresses of successive allocations of similarly accomplish this by arranging the heap so that all holes in sized buffers are unlikely to have any reliable relation- it that are big enough to hold the vulnerable buffer are ship. Figure 1 shows how an allocation in a fragmented surrounded by buffers that we control. heap might look. Since it is impossible to predict where The technique consists of five steps. these holes might occur in a heap that has been in use 1. Defragment the heap. for some time, it is impossible to predict where the next allocation will occur. 2. Make holes in the heap. However, if we have some control over the target ap- plication, we may be able to force the application to 3. Prepare the blocks around the holes. make many allocations of any given size. In particular, we can make many allocations that will essentially fill in 4. Trigger allocation and overflow. all the holes. Once the holes are filled up, i.e. the heap is defragmented, allocations of similar size will, in general, 5. Trigger the jump to shellcode. be predictably close to each other, as in Figure 2. These steps are described in more detail in the rest of this We emphasize that defragmentation is always with re- section. spect to a particular buffer size. In preparation for the exploit, we must defragment the heap with respect to the size of the vulnerable buffer. The size of this buffer will 2.3 Defragment vary, but should be known. As shown in [2], arranging The state of a process’s heap dependson the history of al- for defragmentation is simple in JavaScript. For exam- locations and deallocations that have occurred during the ple, process’s lifetime. Consequently, the state of the heap var bigdummy = new Array(1000); for a long running multithreaded process (e.g. a web for(i=0; i<1000; i++){ 2 Figure 3: Defragmented heap with many allocations. We see a long line of same-sized buffers that we control. Figure 4: Controlled heap with every other buffer freed. The allocation of the vulnerable buffer ends up in one of the holes. bigdummy[i] = new Array(size); we have reached the point where all subsequent alloca- } tions are occurring contiguously at the end of the heap. Unfortunately, at this point, we have a problem. Sim- In the code snippet above, each call to ply deleting an object in JavaScript does not imme- new Array(size) results in an allocation of diately result in the object’s space on the heap be- 4*size+8 bytes on the heap.1 This allocation corre- ing freed. Space on the heap is not relinquished until sponds to an ArrayStorage object consisting of an garbage collection occurs. Internet Explorer provides eight byte header followed by an array of size pointers. a CollectGarbage() method that immediately trig- Initially, the entire allocation is zeroed out. A value of gers garbage collection [2], but other implementations do size should be chosen so that the resulting allocations not. In particular, WebKit does not. Accordingly, we di- are as close as in size as possible, and no smaller, than gress with a discussion of garbage collection in WebKit. the size of the vulnerable buffer. The value of 1000 used above was determined empirically. Our inspection of the WebKit source code turned up three main events that can trigger garbage collection. To understand these events, one must be familiar with the 2.4 Make Holes way WebKit’s JavaScript manages objects. Remember, our goal is to arrange for control of the buffer The implementation maintains two structures, that follows the vulnerable buffer. Assuming defragmen- primaryHeap and numberHeap, each of which is tation worked, we should have a number of contiguous essentially an array of pointers to CollectorBlock buffers at the end of the heap, all roughly the same size objects. A CollectorBlock is a fixed–sized array of as the (yet to be allocated) vulnerable buffer (Figure 3). Cells and each Cell can hold a JSCell object (or The next step is to free every other of these contiguous derivative). Every JavaScript object occupies a Cell buffers, leaving alternating buffers and holes, all match- in one of these heaps. Large objects (such as arrays ing the size of the vulnerable buffer. The first step in and strings) occupy additional memory on the system achieving this is heap. We refer to this additional system heap memory as for(i=900; i<1000; i+=2){ associated storage. delete(bigdummy[i]); Each CollectorBlock maintains a linked list of } those Cells that are free. When an allocation is re- The lower limit in the for loop is based on the assump- quested, and no free Cells are available in any exist- tion that after 900 allocations in the defragment stage, ing CollectorBlocks, a new CollectorBlock is allocated. 1Clearly, details such as this depend on the particular implementa- tion of JavaScript. We used WebKit release 31114 for this investiga- All JavaScript objects deriving from JSObject are tion. allocated on the primaryHeap. The numberHeap 3 before overflow free block ArrayStorage 1 0 NumberInstance VFT pNI pVFT pF[0] _ZN3KJS14NumberInstanceD1EV pF[1] _ZN3KJS14NumberInstanceD0EV . pF[2] _ZNK3KJS8JSObject4typeEv . free block Figure 5: Details of an attacker controlled block just before the overflow is triggered. is reserved for NumberImp objects. Note that the lat- web browsing of pages with JavaScript. Since the ter do not correspond to JavaScript Number objects, but numberHeap CollectorBlock has 4062 Cells, instead are typically short lived number objects corre- JavaScript code that allocates at least this many sponding to intermediate arithmetic computations. NumberImps should trigger garbage collection. As When garbage collection is triggered, both heaps are an example, manipulation of doubles is one opera- tion that results in a NumberImp allocation from the examined for objects with zero reference counts, and numberHeap, so the following JavaScript code can be such objects (and their associated storage if any) are used for garbage collection triggering: freed.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    6 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