<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>Advogato blog for argp</title>
    <link>http://www.advogato.org/person/argp/</link>
    <description>Advogato blog for argp</description>
    <language>en-us</language>
    <generator>mod_virgule</generator>
    <pubDate>Wed, 22 May 2013 11:12:18 GMT</pubDate>
    <item>
      <pubDate>Fri, 17 Aug 2012 11:09:05 GMT</pubDate>
      <title>Heap Exploitation Abstraction by Example - OWASP AppSec Research 2012</title>
      <link>http://www.advogato.org/person/argp/diary.html?start=45</link>
      <guid>http://www.census-labs.com/news/2012/08/17/heap-exploitation-abstraction-owasp/</guid>
      <description>&lt;p&gt;This year&#x2019;s &lt;a href="http://www.appsecresearch.org/" &gt;OWASP AppSec Research&lt;/a&gt; conference took place in Athens, Greece and we were planning to be there as participants. However, the day before the conference, Konstantinos Papapanagiotou (General Chair) asked if we could do a presentation to replace a cancelled talk. Myself and Chariton Karamitas agreed to help and spend around three hours preparing a talk on heap exploitation abstraction, a subject dear to us.&lt;/p&gt;

&lt;p&gt;
&lt;/p&gt;&lt;center&gt;
&lt;img src="http://www.census-labs.com/news/author/argp//media/heap_weird_machine.png" width="500"/&gt;&lt;/center&gt;


&lt;!--more--&gt;

&lt;p&gt;Our talk was titled &lt;a href="http://www.advogato.org/media/heap-owasp-appsec-2012.pdf" &gt;&#x201C;Heap Exploitation Abstraction by Example&#x201D;&lt;/a&gt; and was divided into two main parts. In the first part we focused on presenting examples of exploiting heap managers. Specifically, we talked about attacking the FreeBSD kernel allocator (UMA), the Linux kernel allocator (SLUB) and the jemalloc userland allocator.&lt;/p&gt;

&lt;p&gt;In the second part we started by finding the common elements of these three allocators and categorizing them into the following:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;End-user allocations&lt;/li&gt;
&lt;li&gt;Allocation containers&lt;/li&gt;
&lt;li&gt;Container groupings&lt;/li&gt;
&lt;li&gt;Execution-specific (thread, CPU core) metadata&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;We then proceeded to make an argument that the value of abstracting heap exploitation is in having a library of attack techniques, or &lt;i&gt;primitives&lt;/i&gt;, that can be reused on other allocators. We took the concept of &lt;i&gt;weird machines&lt;/i&gt; as defined by Sergey Bratus and Halvar Flake and applied it to heap managers. Briefly, we consider an allocator to be a deterministic automaton. Metadata corruption attacks can then be viewed as corruptions of the automaton&#x2019;s transition function. Application-specific attacks, like adjacent memory region corruptions, can be viewed as manipulations of the automaton&#x2019;s determinacy.&lt;/p&gt;

&lt;p&gt;Please consider this as our early attempt on abstracting heap exploitation (also that it was put together in three hours) and expect more work on the subject from us.&lt;/p&gt;

&lt;p&gt;Presentation Material&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;Slides (&lt;a href="http://www.advogato.org/media/heap-owasp-appsec-2012.pdf" &gt;pdf&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Thanks to Sergey Bratus and Halvar Flake for the weird machine concept. Chris Valasek, Dimitris Glynos and Yiorgos Adamopoulos for interesting discussions on exploitation abstraction. Konstantinos Papapanagiotou for inviting us to replace the cancelled talk.&lt;/p&gt;</description>
    </item>
    <item>
      <pubDate>Fri, 3 Aug 2012 10:12:58 GMT</pubDate>
      <title>Black Hat USA 2012 update</title>
      <link>http://www.advogato.org/person/argp/diary.html?start=44</link>
      <guid>http://www.census-labs.com/news/2012/08/03/blackhat-usa-2012-update/</guid>
      <description>&lt;p&gt;
This year we have presented our research work at &lt;a href="http://www.blackhat.com/html/bh-us-12/" &gt;Black Hat USA 2012&lt;/a&gt;, the leading information security conference. Our researchers Patroklos Argyroudis and Chariton Karamitas visited Caesar&#x2019;s Palace at Las Vegas, Nevada and &lt;a href="https://www.blackhat.com/html/bh-us-12/bh-us-12-archives.html#Argyroudis" &gt;delivered the talk&lt;/a&gt;.

&lt;/p&gt;&lt;p&gt;
&lt;/p&gt;&lt;center&gt;
&lt;a href="http://www.blackhat.com/html/bh-us-12/" &gt;
&lt;img src="http://www.census-labs.com/news/author/argp//media/black-hat-usa-2012.png" width="500"/&gt;&lt;/a&gt;
&lt;/center&gt;


&lt;p&gt;Our presentation was titled &lt;a href="http://www.advogato.org/media/bhusa-2012-slides.pdf" &gt;&#x201C;Exploiting the jemalloc Memory Allocator: Owning Firefox&#x2019;s Heap&#x201D;&lt;/a&gt; and described in detail attack primitives against &lt;a href="http://www.canonware.com/jemalloc/" &gt;jemalloc&lt;/a&gt; and how these can be used to exploit heap overflow and use-after-free vulnerabilities that affect the &lt;a href="http://www.mozilla.org/en-US/firefox/new/" &gt;Mozilla Firefox&lt;/a&gt; browser.&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;Specifically, the presentation was divided into three parts. The first part comprised of an introduction to the jemalloc memory allocator and presented its architecture, its fundamental concepts and its management data structures.&lt;/p&gt;

&lt;center&gt;
&lt;a href="http://www.advogato.org/media/jemalloc.png" &gt;
&lt;img src="http://www.census-labs.com/news/author/argp//media/jemalloc.png" width="471" height="342"/&gt;&lt;/a&gt;
&lt;/center&gt;


&lt;p&gt;The second part of the presentation focused on identifying attack vectors against the allocator and on the development of novel exploitation approaches and primitives that can be used to attack jemalloc heap corruption vulnerabilities.&lt;/p&gt;

&lt;p&gt;In the third and final part, our researchers applied these primitives to &lt;a href="https://github.com/argp/jemalloc_feng_shui" &gt;manipulate the heap&lt;/a&gt; of the Mozilla Firefox browser and to develop a PoC exploit for vulnerability &lt;a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2011-3026" &gt;CVE-2011-3026&lt;/a&gt; (&lt;a href="https://github.com/argp/cve-2011-3026-firefox" &gt;demonstrated on Firefox&lt;/a&gt;). Furthermore, a utility (&lt;a href="https://github.com/argp/unmask_jemalloc" &gt;unmask_jemalloc&lt;/a&gt;) was released that can be used to aid the process of exploit development for jemalloc/Firefox vulnerabilities.&lt;/p&gt;

&lt;p&gt;Our presentation was 1 of 5 selected to be part of the &#x201C;Breaking Things&#x201D; track of the Black Hat conference. After the talk, our researchers participated in the press conference for the &#x201C;Breaking Things&#x201D; track and answered questions related to the impact of the presented work and its consequences.&lt;/p&gt;

&lt;h3&gt;Updated Presentation Material&lt;/h3&gt;

&lt;p/&gt;&lt;p&gt;All material from our presentation are provided below in the hope that they will be useful to security researchers willing to continue our work.&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;Slides: &lt;a href="http://www.advogato.org/media/bhusa-2012-slides.pdf" &gt;bhusa-2012-slides.pdf&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Code archive: &lt;a href="http://www.advogato.org/media/bhusa-2012-src.tar.gz" &gt;bhusa-2012-src.tar.gz&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p/&gt;&lt;p&gt;The development of the released code and utilities will continue on &lt;a href="https://github.com/" &gt;GitHub&lt;/a&gt; at the following repositories:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;a href="https://github.com/argp/unmask_jemalloc" &gt;unmask_jemalloc&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/argp/jemalloc_feng_shui" &gt;jemalloc_feng_shui&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/argp/cve-2011-3026-firefox" &gt;cve-2011-3026-firefox&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/argp/lipodebugwalk" &gt;lipodebugwalk&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p/&gt;&lt;p&gt;See you at the next security conference!&lt;/p&gt;</description>
    </item>
    <item>
      <pubDate>Mon, 14 May 2012 20:16:39 GMT</pubDate>
      <title>Black Hat USA 2012: Owning Firefox's Heap</title>
      <link>http://www.advogato.org/person/argp/diary.html?start=43</link>
      <guid>http://www.census-labs.com/news/2012/05/14/blackhat-usa-2012/</guid>
      <description>&lt;p&gt;Continuing our work on jemalloc exploitation, myself and Chariton Karamitas (intern at &lt;a href="http://census-labs.com/" &gt;Census, Inc&lt;/a&gt;) are presenting &lt;a href="https://www.blackhat.com/html/bh-us-12/bh-us-12-briefings.html#Argyroudis" &gt;&#x201C;Owning Firefox&#x2019;s Heap&#x201D;&lt;/a&gt; at &lt;a href="https://www.blackhat.com/html/bh-us-12/bh-us-12-briefings.html" &gt;Black Hat USA 2012&lt;/a&gt;. This presentation extends our recently published &lt;a href="http://phrack.org/issues.html?issue=68&amp;amp;id=10#article" &gt;Phrack paper&lt;/a&gt; by focusing specifically on the most widely used jemalloc application, namely the &lt;a href="http://www.mozilla.org/en-US/firefox/new/" &gt;Mozilla Firefox&lt;/a&gt; web browser.&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;The abstract of our talk will give you a good preview of the content: &lt;/p&gt;

&lt;blockquote&gt;
jemalloc is a userland memory allocator that is being increasingly adopted by software projects as a high performance heap manager. It is used in Mozilla Firefox for the Windows, Mac OS X and Linux platforms, and as the default system allocator on the FreeBSD and NetBSD operating systems. Facebook also uses jemalloc in various components to handle the load of its web services. However, despite such widespread use, there is no work on the exploitation of jemalloc.
&lt;p&gt;
Our research addresses this. We will begin by examining the architecture of the jemalloc heap manager and its internal concepts, while focusing on identifying possible attack vectors. jemalloc does not utilize concepts such as &#x2018;unlinking&#x2019; or &#x2018;frontlinking&#x2019; that have been used extensively in the past to undermine the security of other allocators. Therefore, we will develop novel exploitation approaches and primitives that can be used to attack jemalloc heap corruption vulnerabilities. As a case study, we will investigate Mozilla Firefox and demonstrate the impact of our developed exploitation primitives on the browser&#x2019;s heap. In order to aid the researchers willing to continue our work, we will also release our jemalloc debugging tool belt.
&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;For updates on this talk, information on my research and my work at &lt;a href="http://census-labs.com/" &gt;Census, Inc&lt;/a&gt; in general you can &lt;a href="http://twitter.com/_argp" &gt;follow me on Twitter&lt;/a&gt;.&lt;/p&gt;</description>
    </item>
    <item>
      <pubDate>Tue, 3 Jan 2012 10:35:29 GMT</pubDate>
      <title>The Linux kernel memory allocators from an exploitation perspective</title>
      <link>http://www.advogato.org/person/argp/diary.html?start=42</link>
      <guid>http://www.census-labs.com/news/2012/01/03/linux-kernel-heap-exploitation/</guid>
      <description>&lt;p&gt;In anticipation of &lt;a href="https://twitter.com/djrbliss" &gt;Dan Rosenberg&#x2019;s&lt;/a&gt; talk on exploiting the Linux kernel&#x2019;s SLOB memory allocator at the &lt;a href="http://immunityinc.com/infiltrate/" &gt;Infiltrate security conference&lt;/a&gt; and because I recently had a discussion with some friends about the different kernel memory allocators in Linux, I decided to write this quick introduction. I will present some of the allocators&#x2019; characteristics and also provide references to public work on exploitation techniques.&lt;/p&gt;

&lt;p/&gt;&lt;center&gt;
&lt;img src="http://www.census-labs.com/news/author/argp//media/slabs.jpg" width="300" height="225"/&gt;&lt;/center&gt;

&lt;!--more--&gt;

&lt;p&gt;At the time of this writing, the Linux kernel has three different memory allocators in the official code tree, namely SLAB, SLUB and SLOB. These allocators are on a memory management layer that is logically on top of the system&#x2019;s low level page allocator and are mutually exclusive (i.e. you can only have one of them enabled/compiled in your kernel). They are used when a kernel developer calls &lt;code&gt;kmalloc()&lt;/code&gt; or a similar function. Unsurprisingly, they can all be found in the &lt;a href="http://lxr.linux.no/linux+v3.1.6/mm/" &gt;mm directory&lt;/a&gt;. All of them follow, to various extends and by extending or simplifying, the &lt;a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.29.4759" &gt;traditional slab allocator design&lt;/a&gt; (notice the lowercase &#x201C;slab&#x201D;; that&#x2019;s the term for the general allocator design approach, while &#x201C;SLAB&#x201D; is a slab implementation in the Linux kernel). Slab allocators allocate prior to any request, for example at kernel boot time, large areas of virtual memory (called &#x201C;slabs&#x201D;, hence the name). Each one of these slabs is then associated to a kernel structure of a specific type and size. Furthermore, each slab is divided into the appropriate number of slots for the size of the kernel structure it is associated with. As an example consider that a slab for the structure &lt;code&gt;task_struct&lt;/code&gt; has 31 slots. The size of a &lt;code&gt;task_struct&lt;/code&gt; is 1040 bytes, so assuming that a page is 4096 bytes (the default) then a &lt;code&gt;task_struct&lt;/code&gt; slab is 8 pages long. Apart from the structure-specific slabs, like the one above for &lt;code&gt;task_struct&lt;/code&gt;, there are also the so called general purpose slabs which are used to serve arbitrary-sized &lt;code&gt;kmalloc()&lt;/code&gt; requests. These requests are adjusted by the allocator for alignment and assigned to a suitable slab.&lt;/p&gt;

&lt;p&gt;Let&#x2019;s take a look at the slabs of a recent Linux kernel:&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;$ cat /proc/slabinfo
slabinfo &#x2014; version: 2.1
...
fat_inode_cache       57     57    416   19    2 : tunables    0    0    0 : slabdata
3      3      0
fat_cache            170    170     24  170    1 : tunables    0    0    0 : slabdata
1      1      0
VMBlockInodeCache      7      7   4480    7    8 : tunables    0    0    0 : slabdata
1      1      0
blockInfoCache         0      0   4160    7    8 : tunables    0    0    0 : slabdata
0      0      0
AF_VMCI                0      0    704   23    4 : tunables    0    0    0 : slabdata
0      0      0
fuse_request          80     80    400   20    2 : tunables    0    0    0 : slabdata
4      4      0
fuse_inode         21299  21690    448   18    2 : tunables    0    0    0 : slabdata
1205   1205      0
...
kmalloc-8192          94     96   8192    4    8 : tunables    0    0    0 : slabdata
24     24      0
kmalloc-4096         118    128   4096    8    8 : tunables    0    0    0 : slabdata
16     16      0
kmalloc-2048         173    208   2048   16    8 : tunables    0    0    0 : slabdata
13     13      0
kmalloc-1024         576    640   1024   16    4 : tunables    0    0    0 : slabdata
40     40      0
kmalloc-512          904    992    512   16    2 : tunables    0    0    0 : slabdata
62     62      0
kmalloc-256          540    976    256   16    1 : tunables    0    0    0 : slabdata
61     61      0
kmalloc-128          946   1408    128   32    1 : tunables    0    0    0 : slabdata
44     44      0
kmalloc-64         13013  13248     64   64    1 : tunables    0    0    0 : slabdata
207    207      0
kmalloc-32         23624  27264     32  128    1 : tunables    0    0    0 : slabdata
213    213      0
kmalloc-16          3546   3584     16  256    1 : tunables    0    0    0 : slabdata
14     14      0
kmalloc-8           4601   4608      8  512    1 : tunables    0    0    0 : slabdata
9      9      0
kmalloc-192         3659   4620    192   21    1 : tunables    0    0    0 : slabdata
220    220      0
kmalloc-96         10137  11340     96   42    1 : tunables    0    0    0 : slabdata
270    270      0
kmem_cache            32     32    128   32    1 : tunables    0    0    0 : slabdata
1      1      0
kmem_cache_node      256    256     32  128    1 : tunables    0    0    0 : slabdata
2      2      0
&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;Here you can see structure-specific slabs, for example &lt;code&gt;fuse_inode&lt;/code&gt;, and general purpose slabs, for example &lt;code&gt;kmalloc-96&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When it comes to the exploitation of overflow bugs in the context of slab allocators, there are three general approaches to corrupt kernel memory:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;Corruption of the adjacent objects/structures of the same slab.
&lt;/li&gt;
  &lt;li&gt;Corruption of the slab allocator&#x2019;s management structures (referred to as &lt;i&gt;metadata&lt;/i&gt;).
&lt;/li&gt;
  &lt;li&gt;Corruption of the adjacent &lt;i&gt;physical&lt;/i&gt; page of the slab your vulnerable structure is allocated on. 
&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;The ultimate goal of the above approaches is of course to gain control of the kernel&#x2019;s execution flow and divert/hijack it to your own code. In order to be able to manipulate the allocator and the state of its slabs, arranging structures on them to your favor (i.e. next to each other on the same slab, or at the end of a slab), it is nice (but not strictly required ;) to have some information on the allocator&#x2019;s state. The proc filesystem provides us with a way to get this information. Unprivileged local users can simply &lt;code&gt;cat /proc/slabinfo&lt;/code&gt; (as shown above) and see the allocator&#x2019;s slabs, the number of used/free structures on them, etc. &lt;a href="http://thread.gmane.org/gmane.linux.kernel/1108378/" &gt;Is your distribution still allowing this?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For each one of the Linux kernel&#x2019;s allocators I will provide references to papers describing practical attack techniques and examples of public exploits.&lt;/p&gt;

&lt;h3&gt;SLAB&lt;/h3&gt;

&lt;p&gt;Starting with the oldest of the three allocators, &lt;a href="http://lxr.linux.no/linux+v3.1.6/mm/slab.c" &gt;SLAB&lt;/a&gt; organizes physical memory frames in caches. Each cache is responsible for a specific kernel structure. Also, each cache holds slabs that consist of contiguous pages and these slabs are responsible for the actual storing of the kernel structures of the cache&#x2019;s type. A SLAB&#x2019;s slab can have both allocated (in use) and deallocated (free) slots. Based on this and with the goal of reducing fragmentation of the system&#x2019;s virtual memory, a cache&#x2019;s slabs are divided into three lists; a list with full slabs (i.e slabs with no free slots), a list with empty slabs (slabs on which all slots are free), and a list with partial slabs (slabs that have slots both in use and free).&lt;/p&gt;

&lt;p&gt;A SLAB&#x2019;s slab is described by the following structure:&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;struct slab
{
    union
    {
        struct
        {
            struct list_head list;
            unsigned long colouroff;
            void *s_mem;            /* including colour offset */
            unsigned int inuse;     /* num of objs active in slab */
            kmem_bufctl_t free;
            unsigned short nodeid;
        };

        struct slab_rcu __slab_cover_slab_rcu;
    };
};
&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;list&lt;/code&gt; variable is used to place the slab in one of the lists I described above. Coloring and the variable &lt;code&gt;colouroff&lt;/code&gt; require some explanation in case you haven&#x2019;t seen them before. Coloring or cache coloring is a performance trick to reduce processor L1 cache hits. This is accomplished by making sure that the first &#x201C;slot&#x201D; of a slab (which is used to store the slab&#x2019;s &lt;code&gt;slab&lt;/code&gt; structure, i.e. the slab&#x2019;s metadata) is not placed at the beginning of the slab (which is also at the start of a page) but an offset &lt;code&gt;colouroff&lt;/code&gt; from it. &lt;code&gt;s_mem&lt;/code&gt; is a pointer to the first slot of the slab that stores an actual kernel structure/object. &lt;code&gt;free&lt;/code&gt; is an index to the next free object of the slab.&lt;/p&gt;

&lt;p&gt;As I mentioned in the previous paragraph, a SLAB&#x2019;s slab begins with a &lt;code&gt;slab&lt;/code&gt; structure (the slab&#x2019;s metadata) and is followed by the slab&#x2019;s objects. The stored objects on a slab are contiguous, with no metadata in between them, making easier the exploitation approach of corrupting adjacent objects. Easier means that when we overflow from one object to its adjacent we don&#x2019;t corrupt management data that could lead to making the system crash.&lt;/p&gt;

&lt;p&gt;By manipulating SLAB through controlled allocations and deallocations from userland that affect the kernel (for example via system calls) we can arrange that the overflow from a vulnerable structure will corrupt an adjacent structure of our own choosing. The fact that SLAB&#x2019;s allocations and deallocations work in a LIFO manner is of course to our advantage in arranging structures/objects on the slabs. As qobaiashi has presented in his paper &lt;a href="http://home.bn-paf.de/sebastian.haase/kmalloc_exploitation.pdf" &gt;&#x201C;The story of exploiting kmalloc() overflows&#x201D;&lt;/a&gt;, the system calls &lt;code&gt;semget()&lt;/code&gt; and &lt;code&gt;semctl(..., ..., IPC_RMID)&lt;/code&gt; is one way to make controlled allocations and deallocations respectively. The term &#x201C;controlled&#x201D; here refers to both the size of the allocation/deallocation and the fact that we can use them directly from userland. Another requirement that these system calls satisfy is that the structure they allocate can help us in our quest for code execution when used as a victim object and corrupted from a vulnerable object. Other ways/system calls that satisfy all the above requirements do exist.&lt;/p&gt;

&lt;p&gt;Another resource on attacking SLAB is &#x201C;Exploiting kmalloc overflows to 0wn j00&#x201D; by amnesia and clflush. In that presentation the authors explained the development process for a reliable exploit for vulnerability &lt;a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2004-0424" &gt;CVE-2004-0424&lt;/a&gt; (which was an integer overflow leading to a kernel heap buffer overflow found by ihaquer and cliph). Both the presentation and the exploit are not public as far as I know. However, a full exploit was published by twiz and sgrakkyu in &lt;a href="http://phrack.org/issues.html?issue=64&amp;amp;id=6#article" &gt;Phrack #64&lt;/a&gt; (castity.c).&lt;/p&gt;

&lt;h3&gt;SLUB&lt;/h3&gt;

&lt;p&gt;&lt;a href="http://lxr.linux.no/linux+v3.1.6/mm/slub.c" &gt;SLUB&lt;/a&gt; is currently the default allocator of the Linux kernel. It follows the SLAB allocator I have already described in its general design (caches, slabs, full/empty/partial lists of slabs, etc.), however it has introduced simplifications in respect to management overhead to achieve better performance. One of the main differences is that SLUB has no metadata at the beginning of each slab like SLAB, but instead it has added it&#x2019;s metadata variables in the Linux kernel&#x2019;s &lt;code&gt;page&lt;/code&gt; structure to track the allocator&#x2019;s data on the physical pages.&lt;/p&gt;

&lt;p&gt;The following excerpt includes only the relevant parts of the &lt;code&gt;page&lt;/code&gt; structure, see &lt;a href="http://lxr.linux.no/linux+v3.1.6/include/linux/mm_types.h#L27" &gt;here&lt;/a&gt; for the complete version.&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;struct page
{
    ...

    struct
    {
        union
        {
            pgoff_t index;          /* Our offset within mapping. */
            void *freelist;         /* slub first free object */
        };

        ...

        struct
        {
            unsigned inuse:16;
            unsigned objects:15;
            unsigned frozen:1;
        };

        ...
    };

    ...

    union
    {
        ...
        struct kmem_cache *slab;        /* SLUB: Pointer to slab */  
        ...
    };

    ...
};
&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;Since there are no metadata on the slab itself, a &lt;code&gt;page&lt;/code&gt;&#x2018;s &lt;code&gt;freelist&lt;/code&gt; pointer is used to point to the first free object of the slab. A free object of a slab has a small header with metadata that contain a pointer to the next free object of the slab. The &lt;code&gt;index&lt;/code&gt; variable holds the offset to these metadata within a free object. &lt;code&gt;inuse&lt;/code&gt; and &lt;code&gt;objects&lt;/code&gt; hold respectively the allocated and total number of objects of the slab. &lt;code&gt;frozen&lt;/code&gt; is a flag that specifies whether the page can be used by SLUB&#x2019;s list management functions. Specifically, if a page has been frozen by a CPU core, only this core can retrieve free objects from the page, while the other available CPU cores can only add free objects to it. The last interesting for our discussion variable is &lt;code&gt;slab&lt;/code&gt; which is of type &lt;code&gt;struct kmem_cache&lt;/code&gt; and is a pointer to the slab on the page. &lt;/p&gt;

&lt;p&gt;The function &lt;code&gt;on_freelist()&lt;/code&gt; is used by SLUB to determine if a given object is on a given page&#x2019;s &lt;code&gt;freelist&lt;/code&gt; and provides a nice introduction to the use of the above elements. The following snippet is an example invocation of &lt;code&gt;on_freelist()&lt;/code&gt; (taken from &lt;a href="http://lxr.linux.no/linux+v3.1.6/mm/slub.c#L3269" &gt;here&lt;/a&gt;):&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
slab_lock(page);

if(on_freelist(page-&amp;gt;slab, page, object))
{
    object_err(page-&amp;gt;slab, page, object, "Object is on free-list");
    rv = false;
}
else
{
    rv = true;
}

slab_unlock(page);
&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;Locking is required to avoid inconsistencies since &lt;code&gt;on_freelist()&lt;/code&gt; makes some modifications and it could be interrupted. Let&#x2019;s take a look at an excerpt from &lt;code&gt;on_freelist()&lt;/code&gt; (the full version is &lt;a href="http://lxr.linux.no/linux+v3.1.6/mm/slub.c#L929" &gt;here&lt;/a&gt;):&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
{
    int nr = 0;
    void *fp;
    void *object = NULL;
    unsigned long max_objects;

    fp = page-&amp;gt;freelist;
    while(fp &amp;amp;&amp;amp; nr objects)
    {
        if(fp == search)
            return 1;
        if(!check_valid_pointer(s, page, fp))
        {
            if(object)
            {
                object_err(s, page, object, "Freechain corrupt");
                set_freepointer(s, object, NULL);
                break;
            }
            else
            {
                slab_err(s, page, "Freepointer corrupt");
                page-&amp;gt;freelist = NULL;
                page-&amp;gt;inuse = page-&amp;gt;objects;
                slab_fix(s, "Freelist cleared");
                return 0;
            }

            break;
        }

        object = fp;
        fp = get_freepointer(s, object);
        nr++;
    }

    ...
}
&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;The function starts with a simple piece of code that walks the &lt;code&gt;freelist&lt;/code&gt; and demonstrates the use of SLUB internal variables. Of particular interest is the call of the &lt;code&gt;check_valid_pointer()&lt;/code&gt; function which verifies that a &lt;code&gt;freelist&lt;/code&gt;&#x2018;s object&#x2019;s address (variable &lt;code&gt;fp&lt;/code&gt;) is within a slab page. This is a check that safeguards against corruptions of the &lt;code&gt;freelist&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This brings us to attacks against the SLUB memory allocator. The attack vector of corrupting adjacent objects on the same slab is fully applicable to SLUB and largely works like in the case of the SLAB allocator. However, in the case of SLUB there is an added attack vector: exploiting the allocator&#x2019;s metadata (the ones responsible for finding the next free object on the slab). As twiz and sgrakkyu have demonstrated in &lt;a href="http://www.amazon.com/gp/product/1597494860/ref=as_li_ss_tl?ie=UTF8&amp;amp;tag=oss042-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=1597494860" &gt;their book on kernel exploitation&lt;/a&gt;, the slab can be misaligned by corrupting the least significant byte of the metadata of a free object that hold the pointer to the next free object. This misalignment of the slab allows us to create an in-slab fake object and by doing so to a) satisfy safeguard checks as the one I explained in the previous paragraph when they are used, and b) to hijack the kernel&#x2019;s execution flow to our own code.&lt;/p&gt;

&lt;p&gt;An example of SLUB metadata corruption and slab misalignment is the exploit for vulnerability &lt;a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-1046" &gt;CVE-2009-1046&lt;/a&gt; which was an off-by-two kernel heap overflow. In &lt;a href="http://kernelbof.blogspot.com/2009/07/even-when-one-byte-matters.html" &gt;this blog post&lt;/a&gt;, sgrakkyu explained how by using only an one byte overflow turned this vulnerability into a reliable exploit (tiocl_houdini.c). If you&#x2019;re wondering why an one byte overflow is more reliable than a two byte overflow think about little-endian representation.&lt;/p&gt;

&lt;p&gt;A public example of corrupting adjacent SLUB objects is the exploit i-can-haz-modharden.c by Jon Oberheide for vulnerability &lt;a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-2959" &gt;CVE-2010-2959&lt;/a&gt; discovered by Ben Hawkes. In &lt;a href="http://jon.oberheide.org/blog/2010/09/10/linux-kernel-can-slub-overflow/" &gt;this blog post&lt;/a&gt; you can find an overview of the exploit and the technique.&lt;/p&gt;

&lt;h3&gt;SLOB&lt;/h3&gt;

&lt;p&gt;Finally, &lt;a href="http://lxr.linux.no/linux+v3.1.6/mm/slob.c" &gt;SLOB&lt;/a&gt; is a stripped down kernel allocator implementation designed for systems with limited amounts of memory, for example embedded versions/distributions of Linux. In fact its design is closer to traditional userland memory allocators rather than the slab allocators SLAB and SLUB. SLOB places all objects/structures on pages arranged in three linked lists, for small, medium and large allocations. Small are the allocations of size less than &lt;code&gt;SLOB_BREAK1&lt;/code&gt; (256 bytes), medium those less than &lt;code&gt;SLOB_BREAK2&lt;/code&gt; (1024 bytes), and large are all the other allocations:&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
#define SLOB_BREAK1 256
#define SLOB_BREAK2 1024
static LIST_HEAD(free_slob_small);
static LIST_HEAD(free_slob_medium);
static LIST_HEAD(free_slob_large);
&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;Of course this means that in SLOB we can have objects/structures of different types and sizes on the same page. This is the main difference between SLOB and SLAB/SLUB. A SLOB page is defined as follows:&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
struct slob_page
{
    union
    {
        struct
        {
            unsigned long flags;    /* mandatory */
            atomic_t _count;        /* mandatory */
            slobidx_t units;        /* free units left in page */
            unsigned long pad[2];
            slob_t *free;           /* first free slob_t in page */
            struct list_head list;  /* linked list of free pages */
        };

        struct page page;
    };
};
&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;The function &lt;code&gt;slob_alloc()&lt;/code&gt; is SLOB&#x2019;s main allocation routine and based on the requested size it walks the appropriate list trying to find if a page of the list has enough room to accommodate the new object/structure (the full function is &lt;a href="http://lxr.linux.no/linux+v3.1.6/mm/slob.c#L321" &gt;here&lt;/a&gt;):&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
static void *slob_alloc(size_t size, gfp_t gfp, int align, int node)
{
    struct slob_page *sp;
    struct list_head *prev;
    struct list_head *slob_list;
    slob_t *b = NULL;
    unsigned long flags;

    if (size &lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;I think this is a good place to stop since I don't want to go into too many details and because I really look forward to &lt;a href="http://immunityinc.com/infiltrate/speakers.html" &gt;Dan Rosenberg's talk&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Edit: Dan has published a whitepaper to accompany his talk with all the details on SLOB exploitation; you can find it &lt;a href="http://vsecurity.com/download/papers/slob-exploitation.pdf" &gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Notes&lt;/h3&gt;

&lt;p&gt;Wrapping this post up, I would like to mention that there are other slab allocators proposed and implemented for Linux apart from the above three. &lt;a href="http://thread.gmane.org/gmane.linux.kernel/780475" &gt;SLQB&lt;/a&gt; and &lt;a href="http://thread.gmane.org/gmane.linux.kernel.mm/48394/" &gt;SLEB&lt;/a&gt; come to mind, however &lt;a href="http://thread.gmane.org/gmane.linux.kernel.mm/48515" &gt;as the benevolent dictator has ruled&lt;/a&gt; they are not going to be included in the mainline Linux kernel tree until one of the existing three has been removed.&lt;/p&gt;

&lt;p&gt;Exploitation techniques and methodologies like the ones I mentioned in this post can be very helpful when you have a vulnerability you're trying to develop a reliable exploit for. However, you should keep in mind that every vulnerability has its own set of requirements and conditions and therefore every exploit is a different story/learning experience. Understanding a bug and actually developing an exploit for it are two very different things.&lt;/p&gt;

&lt;p&gt;Thanks to Dan and Dimitris for their comments.&lt;/p&gt;

&lt;h3&gt;References&lt;/h3&gt;

&lt;p&gt;The following resources were not linked directly in the discussion, but would be helpful in case you want to look more into the subject.&lt;/p&gt;

&lt;p&gt;
  &lt;a href="http://lxr.linux.no/linux+v3.1.6/" &gt;http://lxr.linux.no/linux+v3.1.6/&lt;/a&gt;
  &lt;br/&gt;&lt;a href="http://lwn.net/Articles/229984/" &gt;http://lwn.net/Articles/229984/&lt;/a&gt;
  &lt;br/&gt;&lt;a href="http://lwn.net/Articles/311502/" &gt;http://lwn.net/Articles/311502/&lt;/a&gt;
  &lt;br/&gt;&lt;a href="http://lwn.net/Articles/229096/" &gt;http://lwn.net/Articles/229096/&lt;/a&gt;
  &lt;br/&gt;&lt;a href="http://phrack.org/issues.html?issue=66&amp;amp;id=15#article" &gt;http://phrack.org/issues.html?issue=66&amp;amp;id=15#article&lt;/a&gt;
  &lt;br/&gt;&lt;a href="http://phrack.org/issues.html?issue=66&amp;amp;id=8#article" &gt;http://phrack.org/issues.html?issue=66&amp;amp;id=8#article&lt;/a&gt;
&lt;/p&gt;</description>
    </item>
    <item>
      <pubDate>Tue, 26 Apr 2011 12:09:26 GMT</pubDate>
      <title>Black Hat Europe 2011 update</title>
      <link>http://www.advogato.org/person/argp/diary.html?start=41</link>
      <guid>http://www.census-labs.com/news/2011/03/22/blackhat-eu-2011-update/</guid>
      <description>&lt;p&gt;
&lt;a href="http://www.blackhat.com/html/bh-eu-11/bh-eu-11-home.html" &gt;Black Hat Europe 2011&lt;/a&gt; is now over and we are very happy to have &lt;a href="http://www.blackhat.com/html/bh-eu-11/bh-eu-11-briefings.html#Argyroudis" &gt;participated&lt;/a&gt; once again in the best European IT security conference!&lt;/p&gt;

&lt;p&gt;
Continuing from our last year&amp;#8217;s &lt;a href="https://www.blackhat.com/html/bh-eu-10/bh-eu-10-briefings.html#Argyroudis" &gt;presentation&lt;/a&gt;, our talk this year focused on operating system kernel protections. Specifically, our researchers Patroklos Argyroudis and Dimitris Glynos collected their experiences from kernel exploit development and presented the ways in which modern operating systems protect their kernels from memory corruption attacks.
&lt;/p&gt;

&lt;p&gt;&lt;center&gt;
&lt;img src="/media/6os.png" width="250" height="187"&gt;
&lt;/center&gt;&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
The first part of our talk touched on the importance of kernel security. Operating system kernel security has become very important lately, mainly for two reasons. The first one is that in userland most generic exploitation approaches have been defeated by countermeasure technologies. This means that it is becoming increasingly harder to successfully penetrate target systems through memory corruption bugs in applications and therefore penetration testers and security researchers have started looking more at kernel vulnerabilities. The second reason for the importance of kernel security is that operating system kernels are large and complex code bases, increasing the possibility of security-related bugs. This has led to the wide adoption of proactive kernel defense mechanisms. 
&lt;/p&gt;

&lt;p&gt;
The second part of our talk gave an overview of kernel vulnerability classes and programming bugs that lead to such vulnerabilities.
&lt;/p&gt;

&lt;p&gt;
In order to allow the attendants to compare userland mitigations to kernel ones, the third part of the talk briefly presented the currently available userland exploitation defenses.
&lt;/p&gt;

&lt;p&gt;
The fourth and main part of our talk presented a detailed technical analysis of the kernel exploitation mitigations found on Linux (version 2.6.37), Windows 7, Mac OS X Snow Leopard (version 10.6.6) and FreeBSD (version 8.1). Our talk also covered the kernel defenses available on popular mobile phone platforms, such as Apple&amp;#8217;s iOS (the iPhone operating system) and Google&amp;#8217;s Android.
&lt;/p&gt;

&lt;p&gt;
As an example success story of a kernel exploitation mitigation one may consider the &lt;a href="http://www.microsoft.com/technet/security/bulletin/ms10-009.mspx" &gt;MS10-009/CVE-2010-0239&lt;/a&gt; ICMPv6 router advertisement vulnerability. This is a Windows 7 remote code execution vulnerability due to unbounded memory copying when processing ICMPv6 router advertisement packets. When the vulnerability is triggered, the Windows 7 kernel detects the corrupted kernel stack and the operating system halts. This is indeed preferable to the alternative which is remote code execution and subsequent full compromise of the system.

&lt;center&gt;
&lt;a href="http://www.advogato.org/media/ipv6phandlerouteradvertisement-windbg.png" &gt;
&lt;img src="/media/ipv6phandlerouteradvertisement-windbg.png" width="500" height="242"&gt;
&lt;/a&gt;
&lt;/center&gt;
&lt;/p&gt;

&lt;p&gt;
Where possible, the fourth part of our talk also presented the available techniques for bypassing some of the examined mitigation technologies, along with the appropriate references to enable all interested parties to get a head start in this area of research.
&lt;/p&gt;

&lt;p&gt;
The fifth and final part of our presentation provided generic hints and tips on bypassing proactive
kernel defense mechanisms.
&lt;/p&gt;

&lt;h3&gt;Presentation Material&lt;/h3&gt;

&lt;p&gt;&lt;p&gt;
All material from our presentation are provided below in the hope that they will be useful to security researchers, developers and users of the above operating systems.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Slides: &lt;a href="http://www.advogato.org/media/bheu-2011-slides.pdf" &gt;bheu-2011-slides.pdf&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;White paper: &lt;a href="http://www.advogato.org/media/bheu-2011-wp.pdf" &gt;bheu-2011-wp.pdf&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Acknowledgements&lt;/h3&gt;

&lt;p&gt;We would like to thank Matt Miller and Maarten Van Horenbeeck of &lt;a href="http://www.microsoft.com/" &gt;Microsoft&lt;/a&gt; for providing us with helpful comments regarding Windows 7.&lt;/p&gt;

&lt;h3&gt;Legal Notice&lt;/h3&gt;

&lt;p&gt;Linux is a registered trademark of Linus Torvalds. Microsoft, Windows 7 and the Windows logo are
registered trademarks of Microsoft Corporation. Mac OS X Snow Leopard, iPhone and the Apple logo are registered trademarks of Apple Inc. FreeBSD and the FreeBSD logo are registered trademarks of The FreeBSD Foundation. Android is a trademark of Google Inc. Finally, the Android Robot logo is reproduced from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.&lt;/p&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 27 May 2010 10:25:10 GMT</pubDate>
      <title>FreeBSD kernel NFS client local vulnerabilities</title>
      <link>http://www.advogato.org/person/argp/diary.html?start=40</link>
      <guid>http://www.census-labs.com/news/2010/05/23/freebsd-kernel-nfsclient/</guid>
      <description>&lt;table&gt;
&lt;tr&gt;&lt;td&gt;census ID:&lt;/td&gt;&lt;td&gt;census-2010-0001&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;CVE ID:&lt;/td&gt;&lt;td&gt;&lt;a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-2020" &gt;CVE-2010-2020&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Affected Products:&lt;/td&gt;&lt;td&gt;FreeBSD 8.0-RELEASE, 7.3-RELEASE, 7.2-RELEASE&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Class:&lt;/td&gt;&lt;td&gt;Improper Input Validation (&lt;a href="http://cwe.mitre.org/data/definitions/20.html" &gt;CWE-20&lt;/a&gt;)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Remote:&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Discovered by:&lt;/td&gt;&lt;td&gt;Patroklos Argyroudis&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;We have discovered two improper input validation vulnerabilities in the FreeBSD kernel&amp;#8217;s NFS client-side implementation (FreeBSD 8.0-RELEASE, 7.3-RELEASE and 7.2-RELEASE) that allow local unprivileged users to escalate their privileges, or to crash the system by performing a denial of service attack.&lt;/p&gt;

&lt;!--more--&gt;

&lt;h3&gt;Details&lt;/h3&gt;

&lt;p&gt;&lt;a href="http://www.freebsd.org/" &gt;FreeBSD&lt;/a&gt; is an advanced operating system which focuses on reliability and performance.  More information about its features can be found &lt;a href="http://www.freebsd.org/about.html" &gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;FreeBSD 8.0-RELEASE, 7.3-RELEASE and 7.2-RELEASE employ an improper input validation method in the kernel&amp;#8217;s NFS client-side implementation.  Specifically, the first vulnerability is in function &lt;code&gt;nfs_mount()&lt;/code&gt; (file &lt;code&gt;src/sys/nfsclient/nfs_vfsops.c&lt;/code&gt;) which is reachable from the &lt;code&gt;mount(2)&lt;/code&gt; and &lt;code&gt;nmount(2)&lt;/code&gt; system calls.  In order for them to be enabled for unprivileged users the &lt;code&gt;sysctl(8)&lt;/code&gt; variable &lt;code&gt;vfs.usermount&lt;/code&gt; must be set to a non-zero value.&lt;/p&gt;

&lt;p&gt;The function &lt;code&gt;nfs_mount()&lt;/code&gt; employs an insufficient input validation method for copying data passed in a structure of type &lt;code&gt;nfs_args&lt;/code&gt; from userspace to kernel.  Specifically, the file handle buffer to be mounted (&lt;code&gt;args.fh&lt;/code&gt;) and its size (&lt;code&gt;args.fhsize&lt;/code&gt;) are completely user-controllable.  The unbounded copy operation is in file &lt;code&gt;src/sys/nfsclient/nfs_vfsops.c&lt;/code&gt; (the excerpts are from 8.0-RELEASE):&lt;/p&gt;

&lt;pre&gt;&lt;code class="cpp"&gt;1094:      if (!has_fh_opt) {
1095:            error = copyin((caddr_t)args.fh, (caddr_t)nfh,
1096:                 args.fhsize);
1097:          if (error) {
1098:               goto out;
1099:            }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The declaration of the variables &lt;code&gt;args&lt;/code&gt; and &lt;code&gt;nfh&lt;/code&gt; is at:&lt;/p&gt;

&lt;pre&gt;&lt;code class="cpp"&gt;786: static int
787: nfs_mount(struct mount *mp)
788: {
789:         struct nfs_args args = {
790:             .version = NFS_ARGSVERSION,
791:             .addr = NULL,
792:             .addrlen = sizeof (struct sockaddr_in),
793:             .sotype = SOCK_STREAM,
794:             .proto = 0,
795:             .fh = NULL,
796:             .fhsize = 0,
797:             .flags = NFSMNT_RESVPORT,
798:             .wsize = NFS_WSIZE,
799:             .rsize = NFS_RSIZE,
800:             .readdirsize = NFS_READDIRSIZE,
801:             .timeo = 10,
802:             .retrans = NFS_RETRANS,
803:             .maxgrouplist = NFS_MAXGRPS,
804:             .readahead = NFS_DEFRAHEAD,
805:             .wcommitsize = 0,                   /* was: NQ_DEFLEASE */
806:             .deadthresh = NFS_MAXDEADTHRESH,    /* was: NQ_DEADTHRESH */
807:             .hostname = NULL,
808:             /* args version 4 */
809:             .acregmin = NFS_MINATTRTIMO,
810:             .acregmax = NFS_MAXATTRTIMO,
811:             .acdirmin = NFS_MINDIRATTRTIMO,
812:             .acdirmax = NFS_MAXDIRATTRTIMO,
813:         };
814:         int error, ret, has_nfs_args_opt;
815:         int has_addr_opt, has_fh_opt, has_hostname_opt;
816:         struct sockaddr *nam;
817:         struct vnode *vp;
818:         char hst[MNAMELEN];
819:         size_t len;
820:         u_char nfh[NFSX_V3FHMAX];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This vulnerability can cause a kernel stack overflow which leads to privilege escalation on FreeBSD 7.3-RELEASE and 7.2-RELEASE.  On FreeBSD 8.0-RELEASE the result is a kernel crash/denial of service due to the SSP/ProPolice kernel stack-smashing protection which is enabled by default.  Versions 7.1-RELEASE and earlier do not appear to be vulnerable since the bug was introduced in 7.2-RELEASE. In order to demonstrate the impact of the vulnerability we have developed a &lt;a href="http://census-labs.com/media/nfs_mount_ex.c" &gt;proof-of-concept privilege escalation exploit&lt;/a&gt;.  A sample run of the exploit follows:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[argp@julius ~]$ uname -rsi
FreeBSD 7.3-RELEASE GENERIC
[argp@julius ~]$ sysctl vfs.usermount
vfs.usermount: 1
[argp@julius ~]$ id
uid=1001(argp) gid=1001(argp) groups=1001(argp)
[argp@julius ~]$ gcc -Wall nfs_mount_ex.c -o nfs_mount_ex
[argp@julius ~]$ ./nfs_mount_ex
[*] calling nmount()
[!] nmount error: -1030740736
nmount: Unknown error: -1030740736
[argp@julius ~]$ id
uid=0(root) gid=0(wheel) egid=1001(argp) groups=1001(argp)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The second vulnerability exists in the function &lt;code&gt;mountnfs()&lt;/code&gt; that is called from function &lt;code&gt;nfs_mount()&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class="cpp"&gt;1119: error = mountnfs(&amp;args, mp, nam, args.hostname, &amp;vp,
1120:     curthread-&gt;td_ucred);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The function &lt;code&gt;mountnfs()&lt;/code&gt; is reachable from the &lt;code&gt;mount(2)&lt;/code&gt; and &lt;code&gt;nmount(2)&lt;/code&gt; system calls by unprivileged users.  As with the &lt;code&gt;nfs_mount()&lt;/code&gt; case above, this requires the &lt;code&gt;sysctl(8)&lt;/code&gt; variable &lt;code&gt;vfs.usermount&lt;/code&gt; to be set to a non-zero value.&lt;/p&gt;

&lt;p&gt;The file handle to be mounted (&lt;code&gt;argp-&gt;fh&lt;/code&gt;) and its size (&lt;code&gt;argp-&gt;fhsize&lt;/code&gt;) are passed to function &lt;code&gt;mountnfs()&lt;/code&gt; from function &lt;code&gt;nfs_mount()&lt;/code&gt; and are user-controllable.  These are subsequently used in an unbounded &lt;code&gt;bcopy()&lt;/code&gt; call (file &lt;code&gt;src/sys/nfsclient/nfs_vfsops.c&lt;/code&gt;):&lt;/p&gt;

&lt;pre&gt;&lt;code class="cpp"&gt;1219: bcopy((caddr_t)argp-&gt;fh, (caddr_t)nmp-&gt;nm_fh, argp-&gt;fhsize);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The above can cause a kernel heap overflow when &lt;code&gt;argp-&gt;fh&lt;/code&gt; is bigger than 128 bytes (the size of &lt;code&gt;nmp-&gt;nm_fh&lt;/code&gt;) since &lt;code&gt;nmp&lt;/code&gt; is an allocated item on the Universal Memory Allocator (UMA, the FreeBSD kernel&amp;#8217;s heap allocator) zone &lt;code&gt;nfsmount_zone&lt;/code&gt; (again from &lt;code&gt;src/sys/nfsclient/nfs_vfsops.c&lt;/code&gt;):&lt;/p&gt;

&lt;pre&gt;&lt;code class="cpp"&gt;1160: static int
1161: mountnfs(struct nfs_args *argp, struct mount *mp, struct sockaddr *nam,
1162:     char *hst, struct vnode **vpp, struct ucred *cred)
1163: {
1164:         struct nfsmount *nmp;
1165:         struct nfsnode *np;
1166:         int error;
1167:         struct vattr attrs;
1168:
1169:         if (mp-&gt;mnt_flag &amp;MNT_UPDATE) {
1170:                 nmp = VFSTONFS(mp);
1171:                 printf("%s: MNT_UPDATE is no longer handled here\n", 
                             __func__);
1172:                 free(nam, M_SONAME);
1173:                 return (0);
1174:         } else {
1175:                 nmp = uma_zalloc(nfsmount_zone, M_WAITOK);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This kernel heap overflow can lead on FreeBSD 8.0-RELEASE, 7.3-RELEASE and 7.2-RELEASE to privilege escalation and/or a kernel crash/denial of service attack.  Similarly to the first vulnerability, FreeBSD 7.1-RELEASE and earlier versions do not appear to be vulnerable. We have developed a &lt;a href="http://census-labs.com/media/mountnfsex.c" &gt;proof-of-concept DoS exploit&lt;/a&gt; to demonstrate the vulnerability.  Furthermore, we have also developed a privilege escalation exploit for this second vulnerability which will not be released at this point.&lt;/p&gt;

&lt;p&gt;FreeBSD has released an &lt;a href="http://security.freebsd.org/advisories/FreeBSD-SA-10:06.nfsclient.asc" &gt;official advisory&lt;/a&gt; and a &lt;a href="http://security.FreeBSD.org/patches/SA-10:06/nfsclient.patch" &gt;patch&lt;/a&gt; to address both vulnerabilities. All affected parties are advised to follow the upgrade instructions included in the advisory and patch their systems.&lt;/p&gt;
</description>
    </item>
    <item>
      <pubDate>Mon, 26 Apr 2010 08:12:11 GMT</pubDate>
      <title>FreeBSD kernel exploitation mitigations</title>
      <link>http://www.advogato.org/person/argp/diary.html?start=39</link>
      <guid>http://www.census-labs.com/news/2010/04/26/kernel-exploitation-mitigations/</guid>
      <description>&lt;p&gt;In my recent &lt;a href="http://www.blackhat.com/html/bh-eu-10/bh-eu-10-archives.html#Argyroudis" &gt;Black Hat Europe 2010 talk&lt;/a&gt; I gave an overview of the kernel exploitation prevention mechanisms that exist on FreeBSD. A few people at the conference have subsequently asked me to elaborate on the subject. In this post I will collect all the information from my talk and the various discussions I had in the Black Hat conference hallways.&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;Userland memory corruption protections (also known as exploitation mitigations) have made most of the generic exploitation approaches obsolete. This is true both on Windows and Unix-like operating systems. In order to successfully achieve arbitrary code execution from a vulnerable application nowadays a researcher needs to look to the memory layout and the code structure of the particular application.&lt;/p&gt;

&lt;p&gt;On the other hand,  exploitation mitigation mechanisms for kernel code have not seen the same level of adoption mostly due to the performance penalty they introduce. This has increased the interest in viewing the operating system kernel as part of the attack surface targeted in a penetration test. Therefore, many operating systems have started to introduce kernel exploitation mitigations. The &lt;a href="http://www.cr0.org/paper/to-jt-party-at-ring0.pdf" &gt;recent CanSecWest talk&lt;/a&gt; by Tavis Ormandy and Julien Tinnes titled &amp;#8220;There&amp;#8217;s a party at Ring0, and you&amp;#8217;re invited&amp;#8221; presented an overview of such mitigations on Windows and Linux.&lt;/p&gt;

&lt;p&gt;FreeBSD also has a number of memory corruption protections for kernel code. Not all of these were developed with the goal of undermining attacks, but primarily as debugging mechanisms. Some are enabled by default in the latest stable version (8.0-RELEASE) and some are not.&lt;/p&gt;

&lt;h3&gt;Stack-smashing&lt;/h3&gt;

&lt;p&gt;Kernel stack-smashing protection for FreeBSD was introduced in version 8.0 via &lt;a href="http://www.trl.ibm.com/projects/security/ssp/" &gt;ProPolice/SSP&lt;/a&gt;. Specifically, the file &lt;code&gt;src/sys/kern/stack_protector.c&lt;/code&gt; is compiled with gcc&amp;#8217;s &lt;code&gt;-fstack-protector&lt;/code&gt; option and registers an event handler called &lt;code&gt;__stack_chk_init&lt;/code&gt; that generates a random canary value (the &amp;#8220;guard&amp;#8221; variable in SSP terminology) placed between the local variables and the saved frame pointer of a kernel process&#x2019;s stack during a function&#x2019;s prologue. Below is the relevant part of the &lt;code&gt;stack_protector.c&lt;/code&gt; file:&lt;/p&gt;

&lt;pre&gt;&lt;code class="cpp"&gt;10: __stack_chk_guard[8] = {};
    ...
20: #define __arraycount(__x)       (sizeof(__x) / sizeof(__x[0]))
21: static void
22: __stack_chk_init(void *dummy __unused)
23: {
24:         size_t i;
25:         long guard[__arraycount(__stack_chk_guard)];
26: 
27:         arc4rand(guard, sizeof(guard), 0);
28:         for (i = 0; i &lt; __arraycount(guard); i++)
29:                 __stack_chk_guard[i] = guard[i];
30: }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;During the protected function&#x2019;s epilogue the canary is checked against its original value. If it has been altered the kernel calls &lt;a href="http://www.freebsd.org/cgi/man.cgi?query=panic&amp;amp;apropos=0&amp;amp;sektion=9&amp;amp;manpath=FreeBSD+8.0-RELEASE&amp;amp;format=html" &gt;&lt;code&gt;panic(9)&lt;/code&gt;&lt;/a&gt; bringing down the whole system, but also stopping any execution flow redirection caused by manipulation of the function&#x2019;s saved frame pointer or saved return address (again from the &lt;code&gt;stack_protector.c&lt;/code&gt; file):&lt;/p&gt;

&lt;pre&gt;&lt;code class="cpp"&gt;13: void
14: __stack_chk_fail(void)
15: {
16: 
17:         panic("stack overflow detected; backtrace may be corrupted");
18: }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;ProPolice/SSP also performs local variable and pointer reordering in order to protect against the corruption of variables and pointers due to stack buffer overflow vulnerabilities. &lt;/p&gt;

&lt;h3&gt;NULL page mappings&lt;/h3&gt;

&lt;p&gt;Also in version 8.0, FreeBSD has &lt;a href="http://security.freebsd.org/advisories/FreeBSD-EN-09:05.null.asc" &gt;introduced a protection&lt;/a&gt; against user mappings at address 0 (NULL). This exploitation mitigation mechanism is exposed through the &lt;a href="http://www.freebsd.org/cgi/man.cgi?query=sysctl&amp;amp;apropos=0&amp;amp;sektion=8&amp;amp;manpath=FreeBSD+8.0-RELEASE&amp;amp;format=html" &gt;&lt;code&gt;sysctl(8)&lt;/code&gt;&lt;/a&gt; variable &lt;code&gt;security.bsd.map_at_zero&lt;/code&gt; and is enabled by default (i.e. the variable has the value 0). When a user request is made for the NULL page and the feature is enabled an error occurs and the mapping fails. Obviously this protection is ineffective in vulnerabilities which the attacker can (directly or indirectly) control the kernel dereference offset. For an applicable example see the exploit for vulnerability CVE-2008-3531 I have previously &lt;a href="http://census-labs.com/news/2009/07/02/cve-2008-3531-exploit/" &gt;published&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Heap-smashing&lt;/h3&gt;

&lt;p&gt;FreeBSD has introduced kernel heap-smashing detection in 8.0-RELEASE via an implementation
called &lt;a href="http://fxr.watson.org/fxr/source/vm/redzone.c" &gt;RedZone&lt;/a&gt;. RedZone is oriented more towards debugging the kernel memory allocator rather than detecting and stopping deliberate attacks against it. If enabled (it is disabled by default) RedZone places a static canary value of 16 bytes above and below each buffer allocated on the heap. The canary value consists of the hexadecimal value 0x42 repeated in these 16 bytes.&lt;/p&gt;

&lt;p&gt;During a heap buffer's deallocation the canary value is checked and if it has been corrupted the details of the corruption (address of the offending buffer and stack traces of the buffer's allocation and deallocation) are logged. The code that performs the check for a heap overflow is the following (from file &lt;code&gt;src/sys/vm/redzone.c&lt;/code&gt;):&lt;/p&gt;

&lt;pre&gt;&lt;code class="cpp"&gt;166: ncorruptions = 0;
167: for (i = 0; i &lt; REDZONE_CFSIZE; i++, faddr++) {
168:       if (*(u_char *)faddr != 0x42)
169:               ncorruptions++;
170: }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This protection mechanism can obviously be easily bypassed. &lt;/p&gt;

&lt;h3&gt;Use-after-free&lt;/h3&gt;

&lt;p&gt;&lt;a href="http://www.freebsd.org/cgi/man.cgi?query=memguard&amp;amp;apropos=0&amp;amp;sektion=9&amp;amp;manpath=FreeBSD+8.0-RELEASE&amp;amp;format=html" &gt;MemGuard&lt;/a&gt; is a replacement kernel memory allocator introduced in FreeBSD version 6.0 and is designed to detect use-after-free bugs in kernel code. Similarly to RedZone, MemGuard mainly targets debugging scenarios and does not constitute a mechanism to mitigate deliberate attacks. However, MemGuard is not compatible and cannot replace the Universal Memory Allocator's (UMA &amp;mdash; which is the default kernel allocator in FreeBSD) calls. Therefore (and also due to the overhead it introduced even before UMA was developed), it is not enabled by default.&lt;/p&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 22 Apr 2010 07:09:48 GMT</pubDate>
      <title>Black Hat Europe 2010 update</title>
      <link>http://www.advogato.org/person/argp/diary.html?start=38</link>
      <guid>http://www.census-labs.com/news/2010/04/22/blackhat-eu-2010-update/</guid>
      <description>&lt;p&gt;
&lt;a href="http://blackhat.com/html/bh-eu-10/bh-eu-10-home.html" &gt;Black Hat Europe 2010&lt;/a&gt; is now over and after a brief ash cloud caused delay I am back in Greece. It has been a great conference, flawlessly organised and with many outstanding presentations. I would like to thank everyone that attended my &lt;a href="http://www.blackhat.com/html/bh-eu-10/bh-eu-10-archives.html#Argyroudis" &gt;presentation&lt;/a&gt; but also all the kind people that spoke to me before and afterwards. I hope to meet all of you again at a future event.
&lt;/p&gt;

&lt;p&gt;
&lt;center&gt;
&lt;a href="http://blackhat.com/html/bh-eu-10/bh-eu-10-home.html" &gt;
&lt;img src="/media/bheu10_banner.png"&gt;
&lt;/a&gt;
&lt;/center&gt;
&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;
My presentation, titled &amp;#8220;Binding the Daemon: FreeBSD Kernel Stack and Heap Exploitation&amp;#8221;, was divided into four parts. In the first part I gave an overview of the published work on the subject of kernel exploitation for Unix-like operating systems. The second and third parts were the main body of the presentation. Specifically, in the second part I explained how a kernel stack overflow vulnerability on FreeBSD can be leveraged to achieve arbitrary code execution. The third part focused on a detailed security analysis of the Universal Memory Allocator (UMA), the FreeBSD kernel&amp;#8217;s memory allocator. I explored how UMA overflows can lead to arbitrary code execution in the context of the latest stable FreeBSD kernel (8.0-RELEASE), and I developed an exploitation methodology for privilege escalation and kernel continuation.
&lt;/p&gt;

&lt;p&gt;
In the fourth and final part I gave a demo of a FreeBSD kernel local 0day vulnerability that I have discovered. However, I have not released the details of the vulnerability in my Black Hat presentation. The details of this vulnerability (plus the proof-of-concept exploit) will be released shortly, once the relevant code is patched and the official advisory is out.
&lt;/p&gt;

&lt;p&gt;
Below you may find all the material of my presentation, updated with some extra information and minor corrections:
&lt;ul&gt;
&lt;li&gt;Slides: &lt;a href="http://www.advogato.org/media/bheu-2010-slides.pdf" &gt;bheu-2010-slides.pdf&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;White paper: &lt;a href="http://www.advogato.org/media/bheu-2010-wp.pdf" &gt;bheu-2010-wp.pdf&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Source code: &lt;a href="http://www.advogato.org/media/bheu-2010-src.tar.gz" &gt;bheu-2010-src.tar.gz&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt; 
&lt;/p&gt;
</description>
    </item>
    <item>
      <pubDate>Fri, 19 Mar 2010 08:11:45 GMT</pubDate>
      <title>Binding the Daemon - Black Hat Europe 2010</title>
      <link>http://www.advogato.org/person/argp/diary.html?start=37</link>
      <guid>http://www.census-labs.com/news/2010/03/19/blackhat-eu-2010/</guid>
      <description>&lt;p&gt;&lt;p&gt;
&lt;a href="http://www.advogato.org/" &gt;census&lt;/a&gt; will be presenting &lt;a href="http://www.blackhat.com/html/bh-eu-10/bh-eu-10-briefings.html#Argyroudis" &gt;&amp;#8220;Binding the Daemon&amp;#8221;&lt;/a&gt;, an in-depth analysis of FreeBSD kernel stack and kernel heap exploitation methodologies at &lt;a href="http://www.blackhat.com/html/bh-eu-10/bh-eu-10-home.html" &gt;Black Hat Europe 2010&lt;/a&gt;. This year the European Black Hat Briefings conference will be held in Barcelona, Spain. We hope to see you there!&lt;/p&gt;
</description>
    </item>
    <item>
      <pubDate>Sun, 21 Feb 2010 17:53:22 GMT</pubDate>
      <title>21 Feb 2010</title>
      <link>http://www.advogato.org/person/argp/diary.html?start=36</link>
      <guid>http://www.advogato.org/person/argp/diary.html?start=36</guid>
      <description>&lt;p&gt;
&lt;b&gt;&lt;a href="http://argp.gr/blog/" &gt;argp.gr/blog&lt;/a&gt;&lt;/b&gt;
&lt;p&gt;
I have moved my blog to &lt;a href="http://argp.gr/blog/" &gt;http://argp.gr/blog/&lt;/a&gt;.
Please update your RSS reader's entry since I will no longer be directly
updating my Advogato diary.  The new feed is &lt;a href="http://argp.gr/blog/feed/" &gt;here&lt;/a&gt;.
</description>
    </item>
  </channel>
</rss>
