<?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>0xCAFEBABE</title>
	<atom:link href="http://www.0xcafebabe.it/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.0xcafebabe.it</link>
	<description>Software engineering, reverse engineering, OS internals and stuff...</description>
	<lastBuildDate>Tue, 18 Oct 2011 09:25:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>On MacOS 10.7 dyld randomization</title>
		<link>http://www.0xcafebabe.it/2011/10/15/on-macos-10-7-dyld-randomization/</link>
		<comments>http://www.0xcafebabe.it/2011/10/15/on-macos-10-7-dyld-randomization/#comments</comments>
		<pubDate>Sat, 15 Oct 2011 09:54:58 +0000</pubDate>
		<dc:creator>rev</dc:creator>
				<category><![CDATA[Mach-O]]></category>
		<category><![CDATA[MacOS]]></category>
		<category><![CDATA[MacOS X Lion]]></category>
		<category><![CDATA[MacOS X Snow Leopard]]></category>
		<category><![CDATA[OS Internals]]></category>

		<guid isPermaLink="false">http://www.0xcafebabe.it/?p=11</guid>
		<description><![CDATA[First article for the blog, let&#8217;s talk about something I had in mind for a while. There has been a lot of talk about the introduced full ASLR on MacOS X Lion, so as soon as I had my hands on the OS I wanted to check which were the changes introduced. Let&#8217;s start from [...]]]></description>
			<content:encoded><![CDATA[<p>First article for the blog, let&#8217;s talk about something I had in mind for a while.<br />
There has been a lot of talk about the introduced full ASLR on MacOS X Lion, so as soon as I had my hands on the OS I wanted to check which were the changes introduced.</p>
<p>Let&#8217;s start from the very beginning, Mach-O. In order to understand what are the differences introduced in Lion, we need to first give a look at a Mach-O built on two different OSes, we will take as a reference Snow Leopard. Let&#8217;s build this simple code for test:</p>
<div class="codecolorer-container c railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>If we compile that code on Lion, no specific option passed to gcc, we will notice a difference from the very same code compiled on Snow Leopard. The difference is the presence of the flag MH_PIE (Position Independent):<br />
<span id="more-11"></span><br />
<em>/usr/include/mach-o/loader.h</em></p>
<div class="codecolorer-container c railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* When this bit is set, the OS will<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;load the main executable at a<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;random address. &nbsp;Only used in<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MH_EXECUTE filetypes. */</span><br />
<span style="color: #339933;">#define MH_PIE 0x200000</span></div></td></tr></tbody></table></div>
<p>The MH_PIE flag has been sitting there since MacOS 10.5 and the linker on Lion is now defaulting to it.<br />
The funny thing is that with MH_PIE enabled, the image base will always be at a fixed offset from dyld, 32bit or 64bit, no difference (just a bigger displacement).</p>
<p>Let&#8217;s give a look at the base addresses for the main executable and dyld on both cases [Lion, SL]:<br />
<strong>NOTE</strong>: If you execute the binary through gdb you will have to <code class="codecolorer text railscasts"><span class="text">set disable-aslr off</span></code> (in gdb, before run) in order to enable dyld randomization, but even then you won&#8217;t have PIE enabled. That&#8217;s why I have added that otherwise inexplicable <code class="codecolorer text railscasts"><span class="text">while (1) {}</span></code></p>
<p>Compiled on Lion (64bit), executed on Lion:</p>
<div class="codecolorer-container text Slush & Poppies" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">0x000000010b397f44 in main ()<br />
(gdb) info shared<br />
The DYLD shared library state has not yet been initialized.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Requested State Current State<br />
Num Basename &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Type Address &nbsp; &nbsp; &nbsp; &nbsp; Reason | | Source &nbsp; &nbsp; <br />
&nbsp; | | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| | | | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; 1 lion &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- 0x10b397000 &nbsp; &nbsp; &nbsp; &nbsp;exec Y Y /tmp/lion at 0x10b397000 (offset 0xb397000)<br />
&nbsp; 2 dyld &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- 0x7fff6af97000 &nbsp; &nbsp; &nbsp; &nbsp;dyld Y Y /usr/lib/dyld at 0x7fff6af97000 (offset 0xb397000) with prefix &quot;__dyld_&quot;</div></div>
<p>Compiled on SL (64bit), executed on Lion:</p>
<div class="codecolorer-container text Slush & Poppies" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">0x0000000100000f3c in main ()<br />
(gdb) info shared<br />
The DYLD shared library state has not yet been initialized.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Requested State Current State<br />
Num Basename &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Type Address &nbsp; &nbsp; &nbsp; &nbsp; Reason | | Source &nbsp; &nbsp; <br />
&nbsp; | | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| | | | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; 1 snow &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- 0x100000000 &nbsp; &nbsp; &nbsp; &nbsp;exec Y Y /tmp/snow (offset 0x0)<br />
&nbsp; 2 dyld &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- 0x7fff66058000 &nbsp; &nbsp; &nbsp; &nbsp;dyld Y Y /usr/lib/dyld at 0x7fff66058000 (offset 0x6458000) with prefix &quot;__dyld_&quot;</div></div>
<p>After hundreds of executions you can notice that dyld will <strong>always</strong> be at 0&#215;1000 from the image base on 32bit, and 0&#215;400000 on 64bit:</p>
<div class="codecolorer-container text Slush & Poppies" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">image base: 0x10b397000<br />
dyld &nbsp; &nbsp; &nbsp;: 0x7fff6af97000<br />
<br />
image base &amp; 0xFFFFFFF = 0xb397000<br />
dyld_base &nbsp;&amp; 0xFFFFFFF = 0xaf97000<br />
<br />
0xb397000 - 0xaf97000 &nbsp;= 0x400000</div></div>
<p>Since image bases are page aligned we can remove 1 byte and a nibble from the address, this leaves us with 2 bytes randomization on dyld (in this case 0xaf97). We&#8217;re actually working on 64bit! On 32bit we have, uhm, the incredible amount of 1 byte.</p>
<div class="codecolorer-container text Slush & Poppies" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">(gdb) info shared<br />
The DYLD shared library state has not yet been initialized.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Requested State Current State<br />
Num Basename &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Type Address &nbsp; &nbsp; &nbsp; &nbsp; Reason | | Source &nbsp; &nbsp; <br />
&nbsp; | | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| | | | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; 1 lion32 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- 0x65000 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exec Y Y /tmp/lion32 at 0x65000 (offset 0x64000)<br />
&nbsp; 2 dyld &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- 0x8fe64000 &nbsp; &nbsp; &nbsp; &nbsp;dyld Y Y /usr/lib/dyld at 0x8fe64000 (offset 0x64000) with prefix &quot;__dyld_&quot;</div></div>
<p>On Lion the userspace has been changed quite a bit, now there&#8217;s no big fat libSystem anymore (there still is but it&#8217;s not that fat <img src='http://www.0xcafebabe.it/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ), instead there are several different libsystem_something located at /usr/lib/system. This ensures logical separation and randomization (libraries were already randomized in 10.5). Now there is also a libdyld.dylib which has a lot of symbols that were once in /usr/lib/dyld.</p>
<p>The problem with dyld was that it was loaded at a fixed address providing loads of go-go-gadgets for ROP exploitation. Now they say it&#8217;s randomized. And several different symbols have been moved to libdyld which <strong>is</strong> randomized.</p>
<div class="codecolorer-container text Slush & Poppies" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">(gdb) info symbol _dyld_image_count<br />
_dyld_image_count in section LC_SEGMENT.__TEXT.__text of /usr/lib/system/libdyld.dylib<br />
(gdb) info symbol _dyld_get_image_header<br />
_dyld_get_image_header in section LC_SEGMENT.__TEXT.__text of /usr/lib/system/libdyld.dylib</div></div>
<p>Funny thing is that /usr/lib/dyld is still working as it was. dlsym() will resolve symbols in libdyld, but if you resolve symbols on the mapped dyld image (e.g. with your own implementation of dlsym()) you&#8217;ll see that everything works as it was in the past.</p>
<p>In conclusion, it looks like the big changes introduced in Lion (regarding dyld randomization) were actually a 2 bytes randomization on 64bit and 1 byte randomization on 32bit, always at a fixed offset from the main executable image base (with MH_PIE flag set).</p>
<p>Well, that&#8217;s it <img src='http://www.0xcafebabe.it/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Hope you guys enjoyed and stay tuned!</p>
<g:plusone size="tall" annotation="inline"></g:plusone><div id="tweetbutton11" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.0xcafebabe.it%2F2011%2F10%2F15%2Fon-macos-10-7-dyld-randomization%2F&amp;text=On%20MacOS%2010.7%20dyld%20randomization%20-%200xCAFEBABE&amp;related=&amp;lang=en&amp;count=none&amp;counturl=http%3A%2F%2Fwww.0xcafebabe.it%2F2011%2F10%2F15%2Fon-macos-10-7-dyld-randomization%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.0xcafebabe.it/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.0xcafebabe.it/2011/10/15/on-macos-10-7-dyld-randomization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hello world!</title>
		<link>http://www.0xcafebabe.it/2011/10/08/hello-world/</link>
		<comments>http://www.0xcafebabe.it/2011/10/08/hello-world/#comments</comments>
		<pubDate>Sat, 08 Oct 2011 13:49:52 +0000</pubDate>
		<dc:creator>rev</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.0xcafebabe.it/?p=1</guid>
		<description><![CDATA[Welcome to my new blog! I decided it was time to give real life to this website since I never really cared too much But hey, things change! I will make this the first place where I&#8217;ll post all the stuff I usually do &#8211; software engineering, reverse engineering, OS internals and everything else worth publishing [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to my new blog! I decided it was time to give <strong>real</strong> life to this website since I never really cared too much <img src='http://www.0xcafebabe.it/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  But hey, things change!</p>
<p>I will make this the first place where I&#8217;ll post all the stuff I usually do &#8211; software engineering, reverse engineering, OS internals and everything else worth publishing here.</p>
<p>I think that&#8217;s all for now <img src='http://www.0xcafebabe.it/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<g:plusone size="tall" annotation="inline"></g:plusone><div id="tweetbutton1" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.0xcafebabe.it%2F2011%2F10%2F08%2Fhello-world%2F&amp;text=Hello%20world%21%20-%200xCAFEBABE&amp;related=&amp;lang=en&amp;count=none&amp;counturl=http%3A%2F%2Fwww.0xcafebabe.it%2F2011%2F10%2F08%2Fhello-world%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.0xcafebabe.it/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.0xcafebabe.it/2011/10/08/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

