<?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>Fotis&#039; Blog</title>
	<atom:link href="http://fotis.loukos.me/blog/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://fotis.loukos.me/blog</link>
	<description></description>
	<lastBuildDate>Mon, 09 Aug 2010 10:30:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>PreZ: my new injector!</title>
		<link>http://fotis.loukos.me/blog/?p=149</link>
		<comments>http://fotis.loukos.me/blog/?p=149#comments</comments>
		<pubDate>Thu, 25 Mar 2010 00:07:30 +0000</pubDate>
		<dc:creator>Fotis</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://fotis.loukos.me/blog/?p=149</guid>
		<description><![CDATA[Together with my presentation at 0&#215;375 0&#215;2 (check previous post), I wrote a proof of concept program, PreZ! What it does is create a new thread at a running program. I will give a small description of the way it works. The images below are taken from the presentation and refer to the linux version [...]]]></description>
			<content:encoded><![CDATA[<p>Together with my presentation at 0&#215;375 0&#215;2 (check <a href="http://fotis.loukos.me/blog/?p=145">previous post</a>), I wrote a proof of concept program, PreZ! What it does is create a new thread at a running program. I will give a small description of the way it works. The images below are taken from the presentation and refer to the linux version but the same concept is used in all versions, i.e. linux, freebsd and opensolaris.</p>
<p>PreZ consists of three parts, the injector, the thread creation code and the code that will be executed at the new thread (the shell code). The following steps take place each time you run PreZ.</p>
<p style="text-align: center;"><a href="http://fotis.loukos.me/blog/wp-content/uploads/2010/03/fotis-1.png"><img class="aligncenter size-full wp-image-151" title="The injector" src="http://fotis.loukos.me/blog/wp-content/uploads/2010/03/fotis-1.png" alt="" width="374" height="259" /></a></p>
<p style="text-align: left;">At the beginning the injector stops the traced process, the &#8216;victim&#8217;. It&#8217;s state is saved (registers and some other stuff at the opensolaris version) and part of the code at the place where the EIP register points to. This code is then overwritten with the other two parts so we have the modified address space at the image above. The execution then continues and the new code we just injected runs.</p>
<p style="text-align: left;">
<p style="text-align: left;"><a href="http://fotis.loukos.me/blog/wp-content/uploads/2010/03/fotis-3.png"><img class="aligncenter size-full wp-image-153" title="The thread creator" src="http://fotis.loukos.me/blog/wp-content/uploads/2010/03/fotis-3.png" alt="" width="460" height="380" /></a></p>
<p style="text-align: left;">
<p style="text-align: left;">What the new code does is mmap a new space with read, write and execute permissions (the orange space at the image above). Then, the shellcode at the end is copied to this new location. All this runs in a single thread (the purple thread of execution at the image). A new thread is spawned and now we have two different threads of execution, the green and the pink at the image above.</p>
<p style="text-align: left;">
<p style="text-align: center;"><a href="http://fotis.loukos.me/blog/wp-content/uploads/2010/03/fotis-4.png"><img class="aligncenter size-full wp-image-154" title="Return to the injector" src="http://fotis.loukos.me/blog/wp-content/uploads/2010/03/fotis-4.png" alt="" width="470" height="367" /></a></p>
<p style="text-align: left;">
<p style="text-align: left;">Finally, the new thread jumps to the place where we copied the new code and the original thread executes an int3 instruction. When this instruction is executed, the injector catches the trap and knows that the thread has been created successfully. The original code read at the beginning is restored, the state is restored, and finally execution continues. All these are transparent to the original process which can&#8217;t understand that the injected code has been executed.</p>
<p style="text-align: left;">PreZ v1.0 can be downloaded from <a href="/security/programs/prezv1.tgz">this link</a>. The sample code for the new thread listens for a connection to port 65226 and when it accepts one it spawns a shell. You can do much more, this is just a simple code to demonstrate the injection process.</p>
]]></content:encoded>
			<wfw:commentRss>http://fotis.loukos.me/blog/?feed=rss2&amp;p=149</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>My 0&#215;375 presentation &#8211; injecting code at a running process!</title>
		<link>http://fotis.loukos.me/blog/?p=145</link>
		<comments>http://fotis.loukos.me/blog/?p=145#comments</comments>
		<pubDate>Mon, 22 Mar 2010 20:22:12 +0000</pubDate>
		<dc:creator>Fotis</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://fotis.loukos.me/blog/?p=145</guid>
		<description><![CDATA[It&#8217;s been a LOT of time since I last posted to my blog. Unfortunately, I&#8217;ve been too busy to write something even if I had some ideas. So, here is my first post after more than a month of absence! I recently made a presentation at the 0&#215;375 (Thessaloniki Tech Talk Sessions). You can find more [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a LOT of time since I last posted to my blog. Unfortunately, I&#8217;ve been too busy to write something even if I had some ideas. So, here is my first post after more than a month of absence!</p>
<p>I recently made a presentation at the 0&#215;375 (Thessaloniki Tech Talk Sessions). You can find more info about 0&#215;375 at the <a href="http://www.grhack.net">grhack site</a>. In short, it is a series of some tech talk session where anyone can present his work on a subject. Submissions are open for everyone. There are no regular dates but if you watch the site you can find info about when and where the next event will take place (always at Thessaloniki and till now at the Aristotle University).</p>
<p>My presentation was about injecting code at a running process and running it as a separate thread. You can download it <a href="/security/pdf/0x375-0x02-Injections.pdf">here</a>. Since 0&#215;375 takes place in Greece the presentation is written in Greek, sorry if you can&#8217;t ready it! In short what the technique I presented and the accompanying program does is create a new thread at a running process. The presentation talks about Linux, however a freebsd and an opensolaris version are ready. I will put them online soon so you can check it out. Wait for the next post!</p>
]]></content:encoded>
			<wfw:commentRss>http://fotis.loukos.me/blog/?feed=rss2&amp;p=145</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Timesharing at 1964</title>
		<link>http://fotis.loukos.me/blog/?p=139</link>
		<comments>http://fotis.loukos.me/blog/?p=139#comments</comments>
		<pubDate>Thu, 14 Jan 2010 10:13:44 +0000</pubDate>
		<dc:creator>Fotis</dc:creator>
				<category><![CDATA[Operating Systems]]></category>

		<guid isPermaLink="false">http://fotis.loukos.me/blog/?p=139</guid>
		<description><![CDATA[I recently found a youtube video (thanks stateless!) with professor Fernando Corbato. It is about timesharing, something revolutionary at 1964. MIT professor Corbato is the founder of Multics which later lead to the creation of UNIX. He has also received the Turing award for his work on resource sharing. The video lasts 27 minutes but [...]]]></description>
			<content:encoded><![CDATA[<p>I recently found a youtube video (thanks stateless!) with professor Fernando Corbato. It is about timesharing, something revolutionary at 1964. MIT professor Corbato is the founder of Multics which later lead to the creation of UNIX. He has also received the Turing award for his work on resource sharing. The video lasts 27 minutes but it&#8217;s worth seeing, trust me! And you also get to see a REAL geek!</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/Q07PhW5sCEk&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/Q07PhW5sCEk&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://fotis.loukos.me/blog/?feed=rss2&amp;p=139</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using non-standard bases</title>
		<link>http://fotis.loukos.me/blog/?p=112</link>
		<comments>http://fotis.loukos.me/blog/?p=112#comments</comments>
		<pubDate>Sun, 03 Jan 2010 22:32:32 +0000</pubDate>
		<dc:creator>Fotis</dc:creator>
				<category><![CDATA[Mathematics]]></category>

		<guid isPermaLink="false">http://fotis.loukos.me/blog/?p=112</guid>
		<description><![CDATA[There are a number of different bases, or radices. Most of us use the decimal positional numeral system, i.e. base 10 for our everyday jobs. When it comes to computers most people use the binary, the hexadecimal or even the octal numeral system. However, there are a number of different &#8220;unusual&#8221; bases. For example, there [...]]]></description>
			<content:encoded><![CDATA[<p>There are a number of different bases, or radices. Most of us use the decimal positional numeral system, i.e. base 10 for our everyday jobs. When it comes to computers most people use the binary, the hexadecimal or even the octal numeral system. However, there are a number of different &#8220;unusual&#8221; bases.</p>
<p>For example, there are negative bases. An example is the negadecimal positional numeral system, that is using the base -10. Converting a number from base -10 to base 10 is as simple as:</p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_0314f5f58f62ab22c3529b956cbde735.png" align="absmiddle" class="tex" alt="d_1d_2d_3d_4\ _{(-10)} = d_1(-10)^3 + d_2(-10)^2 +d_3(-10)^1 + d_4(-10)^0" /></p>
<p>But why use such a base? It&#8217;s very simple, you can represent any number you want, positive or negative, without using a sign. For example:</p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_cf539ceb5155a16208b27c85a097c47b.png" align="absmiddle" class="tex" alt="-1_{(10)} = 1(-10)^1 + 9(-10)^0 = 19_{(-10)}" /></p>
<p>The conversion from decimal to negadecimal is pretty simple. You continuously divide by -10 and keep the remainder as you would do with any other positional numeral system. For example:</p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_3595f5c1789d93e826fc20458d0fd60d.png" align="absmiddle" class="tex" alt="-256 = 26 * (-10) + 4" /></p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_ffab368d8827b1c82d6d72a363e97b8c.png" align="absmiddle" class="tex" alt="26 = -2 * (-10) + 6" /></p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_3afded0648b48b239b93e30a2bc48035.png" align="absmiddle" class="tex" alt="-2 = 1 * (-10) + 8" /></p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_9dffe2f6bf9834950cb49200901d6ddf.png" align="absmiddle" class="tex" alt="1 = 0 * (-10) + 1" /></p>
<p>So <img src="http://fotis.loukos.me/blog/wp-content/cache/tex_d4735770e143b3555c24815ca9693fb7.png" align="absmiddle" class="tex" alt="-256_{10} = 1864_{-10}" />. Converting a positive number is done the same way too.</p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_7c55ab1022d4e8642ace70a5aeb73608.png" align="absmiddle" class="tex" alt="256 = -25 * (-10) + 6" /></p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_f2925d040079bd77334cff529fc7e7fe.png" align="absmiddle" class="tex" alt="-25 = 3 * (-10) + 5" /></p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_48582d49bcb9ced78626b7b561bcd47d.png" align="absmiddle" class="tex" alt="3 = 0 * (-10) + 3" /></p>
<p>So <img src="http://fotis.loukos.me/blog/wp-content/cache/tex_e38bda5d171cd53969f23a9d30d9d70c.png" align="absmiddle" class="tex" alt="256_{(10)} = 356_{(-10)}" />. As you can see, there is no need for a sign symbol. And when using the negabinary numeral system there is no problem with signed and unsigned integers since there is no need for a sign bit!</p>
<p>But a negative base isn&#8217;t the only non-standard base. You can use complex numbers as bases too. This way there is no need to use a real and an imaginary part to represent a complex number. An example of such a base is <img src="http://fotis.loukos.me/blog/wp-content/cache/tex_c311b16184d7d96285eb199c65a489ea.png" align="absmiddle" class="tex" alt="-1 + i" /> where of course <img src="http://fotis.loukos.me/blog/wp-content/cache/tex_3fac904df7095c889e89db3415fbf4fd.png" align="absmiddle" class="tex" alt="i^2 = -1" />. A number can then have the form</p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_a1d67262c9c5ffb96cbd0ec23d9a13ce.png" align="absmiddle" class="tex" alt="d_1d_2d_3d_4 = d_1(-1 + i)^3 + d_2(-1 + i)^2 + d_3(-1 + i)^1 + d_4(-1 + i)^0, d_i \in {0,1}" /></p>
<p>Using this base you can represent any complex you want without using the <img src="http://fotis.loukos.me/blog/wp-content/cache/tex_865c0c0b4ab0e063e5caa3387c1a8741.png" align="absmiddle" class="tex" alt="i" /> symbol.</p>
<p>Converting from this base to decimal is pretty simple, however the reverse is a little bit difficult. What you do for the convertion is divide continuously with <img src="http://fotis.loukos.me/blog/wp-content/cache/tex_c311b16184d7d96285eb199c65a489ea.png" align="absmiddle" class="tex" alt="-1 + i" /> as usual. The remainder will always be <img src="http://fotis.loukos.me/blog/wp-content/cache/tex_cfcd208495d565ef66e7dff9f98764da.png" align="absmiddle" class="tex" alt="0" /> or <img src="http://fotis.loukos.me/blog/wp-content/cache/tex_c4ca4238a0b923820dcc509a6f75849b.png" align="absmiddle" class="tex" alt="1" />. So, if the quotient is <img src="http://fotis.loukos.me/blog/wp-content/cache/tex_c770191d5adbeb7f113e07d980395ed0.png" align="absmiddle" class="tex" alt="q = q_1 + q_2i" /> then:</p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_71fc02edc922c25b573fb81544917e24.png" align="absmiddle" class="tex" alt="a + bi = (q_1 + q_2i)(-1 + i) + r" /></p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_9ff5b68c04d9155333609c5282e66f53.png" align="absmiddle" class="tex" alt="a + bi = -q_1 + q_1i - q_2i - q_2 + r" /></p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_40d887529a134d92421e7ed4dd0193c5.png" align="absmiddle" class="tex" alt="a + bi = (-q_1 - q_2 + r) + (q_1 - q_2)i" /></p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_429a9ab6cd4dc3366a99ba0e55651853.png" align="absmiddle" class="tex" alt="-q_1 - q_2 + r = a" /></p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_775664df57c7c4df42c05c74c460e6f1.png" align="absmiddle" class="tex" alt="q_1 - q_2 = b" /></p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_9400401245ab3a5c6fe3250e7a265d57.png" align="absmiddle" class="tex" alt="q_1 = \frac{b - a + r}{2}" /></p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_e6a330cf2f92303bcdb708fbaa26a60e.png" align="absmiddle" class="tex" alt="q_2 = \frac{-b - a + r}{2}" /></p>
<p>That means that if <img src="http://fotis.loukos.me/blog/wp-content/cache/tex_0cc175b9c0f1b6a831c399e269772661.png" align="absmiddle" class="tex" alt="a" /> and <img src="http://fotis.loukos.me/blog/wp-content/cache/tex_92eb5ffee6ae2fec3ad71c777531578f.png" align="absmiddle" class="tex" alt="b" /> are both odd or even, then <img src="http://fotis.loukos.me/blog/wp-content/cache/tex_6252ab7a75ac12de93c41861a9d69a8c.png" align="absmiddle" class="tex" alt="r = 0" />, otherwise <img src="http://fotis.loukos.me/blog/wp-content/cache/tex_448e1a0554e7a44653db21090441fea3.png" align="absmiddle" class="tex" alt="r = 1" />. Then we continue the division of the quotient as usual.</p>
<p>Now let&#8217;s calculate the value of 2.</p>
<p>2 has both the real and imaginary part even, so <img src="http://fotis.loukos.me/blog/wp-content/cache/tex_6252ab7a75ac12de93c41861a9d69a8c.png" align="absmiddle" class="tex" alt="r = 0" />.</p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_862d22f1eb299436f5044c6ecb7ca126.png" align="absmiddle" class="tex" alt="\frac{2}{-1 + i} = \frac{2(-1 - i)}{(-1 + i)(-1 - i)} = -1 - i" /></p>
<p>The real and imaginary part are both odd, so <img src="http://fotis.loukos.me/blog/wp-content/cache/tex_6252ab7a75ac12de93c41861a9d69a8c.png" align="absmiddle" class="tex" alt="r = 0" /> again.</p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_ee1a3ef9216874cd9019a289c6725ff2.png" align="absmiddle" class="tex" alt="\frac{-1 - i}{-1 + i} = \frac{(-1 - i)(-1 - i)}{(-1 + i)(-1 - i)} = i" /></p>
<p>Since the real part is even and the imaginary is odd, <img src="http://fotis.loukos.me/blog/wp-content/cache/tex_448e1a0554e7a44653db21090441fea3.png" align="absmiddle" class="tex" alt="r = 1" />. So, we can divide by the number minus 1 and the remainder will be 0.</p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_dc6a3aa2eaeef8f440f449667bb47678.png" align="absmiddle" class="tex" alt="\frac{i - 1}{-1 + i} = 1" /></p>
<p>Now, the real part is odd and the imaginary is even. So again <img src="http://fotis.loukos.me/blog/wp-content/cache/tex_448e1a0554e7a44653db21090441fea3.png" align="absmiddle" class="tex" alt="r = 1" />. We divide by <img src="http://fotis.loukos.me/blog/wp-content/cache/tex_293c60575acdbbda46077b6797f07ff3.png" align="absmiddle" class="tex" alt="1 - 1" /> so,</p>
<p style="text-align: center;"><img src="http://fotis.loukos.me/blog/wp-content/cache/tex_dd6f7fefe7f317bf532b3e0cdac7384f.png" align="absmiddle" class="tex" alt="\frac{1 - 1}{-1 + i} = 0" /></p>
<p>We now stop since <img src="http://fotis.loukos.me/blog/wp-content/cache/tex_8b9d73b5ea3587b5c890171dc59366c2.png" align="absmiddle" class="tex" alt="q = 0" />. So we have <img src="http://fotis.loukos.me/blog/wp-content/cache/tex_8bc9e6408ac630ed272edbe33e673ec7.png" align="absmiddle" class="tex" alt="2_{(10)} = 1100_{(-1 + i)}" />. Pretty cool!</p>
]]></content:encoded>
			<wfw:commentRss>http://fotis.loukos.me/blog/?feed=rss2&amp;p=112</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ext4 online defrag and how you can mess up everything when implementing it</title>
		<link>http://fotis.loukos.me/blog/?p=100</link>
		<comments>http://fotis.loukos.me/blog/?p=100#comments</comments>
		<pubDate>Sat, 19 Dec 2009 23:57:05 +0000</pubDate>
		<dc:creator>Fotis</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://fotis.loukos.me/blog/?p=100</guid>
		<description><![CDATA[It&#8217;s been a long time since I last posted something at my blog. Unfortunatelly, I&#8217;m too busy so I have almost no time to write something! This post is about the online defrag the ext4 filesystem supports. It is a very cool feature and allows you to defrag any file without even unmounting a filesystem! [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a long time since I last posted something at my blog. Unfortunatelly, I&#8217;m too busy so I have almost no time to write something!</p>
<p>This post is about the online defrag the ext4 filesystem supports. It is a very cool feature and allows you to defrag any file without even unmounting a filesystem! This is done using a special ioctl, EXT4_IOC_MOVE_EXT defined as follows:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p100code4'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1004"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p100code4"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define EXT4_IOC_MOVE_EXT   _IOWR('f', 15, struct move_extent)</span></pre></td></tr></table></div>

<p>As you can see the ioctl&#8217;s last parameter is a special structure, struct move_extent, defined as:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p100code5'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1005"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p100code5"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">struct</span> move_extent <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> orig_fd<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> donor_fd<span style="color: #339933;">;</span>
    uint64_t orig_start<span style="color: #339933;">;</span>
    uint64_t donor_start<span style="color: #339933;">;</span>
    uint64_t len<span style="color: #339933;">;</span>
    uint64_t moved_len<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>What the ioctl does is copy len extents from the file with descriptor orig_fd starting with orig_start to the one with descriptor donor_fd starting at donor_start. Finally it returns the total number of extents moved at the member moved_len. Using this syscall it is pretty easy to defrag files. You must first open a new file, which will be the donor, and allocate some space using fallocate. Hopefully the extents of the new file will be less than the ones of the original file so you just swap the extents of donor using the ones from the original file. The following code is a simple call to this ioctl which can help you understand how it works:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p100code6'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1006"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code" id="p100code6"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> origfd<span style="color: #339933;">,</span> donorfd<span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> origsize<span style="color: #339933;">;</span>
<span style="color: #993333;">struct</span> move_extent me<span style="color: #339933;">;</span>
<span style="color: #993333;">char</span> <span style="color: #339933;">*</span>origfile<span style="color: #339933;">,</span> <span style="color: #339933;">*</span>donorfile<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* origfile, donorfile and origsize should be set here */</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>origfd <span style="color: #339933;">=</span> open<span style="color: #009900;">&#40;</span>origfile<span style="color: #339933;">,</span> O_RDONLY <span style="color: #339933;">|</span> O_EXCL<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    perror<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Cannot open original file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>donorfd <span style="color: #339933;">=</span> open<span style="color: #009900;">&#40;</span>donorfile<span style="color: #339933;">,</span> O_WRONLY <span style="color: #339933;">|</span> O_CREAT <span style="color: #339933;">|</span> O_EXCL<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    perror<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Cannot create donor file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>fallocate<span style="color: #009900;">&#40;</span>donorfd<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> origsize<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    perror<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Cannot allocate space for donor&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
memset<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>me<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>me<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
me.<span style="color: #202020;">orig_fd</span> <span style="color: #339933;">=</span> origfd<span style="color: #339933;">;</span>
me.<span style="color: #202020;">donor_fd</span> <span style="color: #339933;">=</span> donorfd<span style="color: #339933;">;</span>
me.<span style="color: #202020;">orig_start</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
me.<span style="color: #202020;">donor_start</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
me.<span style="color: #202020;">len</span> <span style="color: #339933;">=</span> extentcnt<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>ioctl<span style="color: #009900;">&#40;</span>origfd<span style="color: #339933;">,</span> EXT4_IOC_MOVE_EXT<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>me<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    perror<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Cannot move extents&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Moved extents: %i<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> me.<span style="color: #202020;">moved_len</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
close<span style="color: #009900;">&#40;</span>origfd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
close<span style="color: #009900;">&#40;</span>donorfd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You should note that there is no restriction on the doner file, it can be any file on the disk.</p>
<p>And here it is! CVE-2009-4131! Until commit 910123ba363623f15ffb5d05dd87bdf06d08c609 the only check was if the user could read the donor file, not write it! Furthermore, there were no checks on the file&#8217;s mode. You can simply open your program which executes /bin/sh as the original file, /bin/ping as the donor and kaboom! /bin/ping is an suid root executable and the code that will be executed will be your code!</p>
<p>I have written a small program that you can use to demonstrate this vulnerability. It simply moves extents. You can download it <a href="/programs/ext4movext/ext4movext.c">here</a>. Just use a program that spawns a shell as the original file, a suid root file as a donor, zero as the offset and ceil(filesize/1024) as len. However, it is not very usable yet since for some strange reason you need to unmount the fs and then remount it in order for the changes to take effect. If you find a solution just leave a comment!</p>
<p>UPDATE: Try reading a LOT of data from the partition after running the program! Damn cache! I shouldn&#8217;t have been working with a filesystem that&#8217;s 10mbs and store only the executable there!</p>
]]></content:encoded>
			<wfw:commentRss>http://fotis.loukos.me/blog/?feed=rss2&amp;p=100</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Concurrent programming the fast and dirty way!</title>
		<link>http://fotis.loukos.me/blog/?p=86</link>
		<comments>http://fotis.loukos.me/blog/?p=86#comments</comments>
		<pubDate>Fri, 20 Nov 2009 11:45:57 +0000</pubDate>
		<dc:creator>Fotis</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://fotis.loukos.me/blog/?p=86</guid>
		<description><![CDATA[Creating threads, mutexes, setting attributes and joining can be very easy using the pthreads library. However, there are times when you don&#8217;t want to link your program with another library or you need something fast. As an example you can see the pipe exploit where I need only two threads to trigger the race condition. [...]]]></description>
			<content:encoded><![CDATA[<p>Creating threads, mutexes, setting attributes and joining can be very easy using the pthreads library. However, there are times when you don&#8217;t want to link your program with another library or you need something fast. As an example you can see the pipe exploit where I need only two threads to trigger the race condition.</p>
<p>Here&#8217;s a simple way to create these threads. First, we need to allocate the stack for the thread using <tt>malloc(3)</tt> and then we can start it using <tt>clone(2)</tt>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p86code9'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p869"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code" id="p86code9"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> koko<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span>arg<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/* do something concurrently with main */</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> start_thread<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>func<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>arg<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> thread_id<span style="color: #339933;">;</span>
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>stack<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>stack <span style="color: #339933;">=</span> malloc<span style="color: #009900;">&#40;</span><span style="color: #208080;">0x4000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        perror<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;malloc&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>thread_id <span style="color: #339933;">=</span> clone<span style="color: #009900;">&#40;</span>func<span style="color: #339933;">,</span> stack <span style="color: #339933;">+</span> <span style="color: #208080;">0x4000</span> <span style="color: #339933;">-</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> CLONE_FS <span style="color: #339933;">|</span> CLONE_FILES <span style="color: #339933;">|</span> CLONE_SIGHAND <span style="color: #339933;">|</span> CLONE_VM <span style="color: #339933;">|</span> CLONE_THREAD<span style="color: #339933;">,</span> arg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        perror<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;clone&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> thread_id<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> thread_id<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>thread_id <span style="color: #339933;">=</span> startthread<span style="color: #009900;">&#40;</span>koko<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Couldn't start thread.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* This runs together with koko */</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Using this code we create a new thread running in the same thread group. This means that calling <tt>getpid(2)</tt> will return the same pid. In order to distinguish threads in the same thread group you must use <tt>gettid(2)</tt> to get the thread id.</p>
<p>When creating a thread in the same thread group you cannot get the return value from the function you called. In order to get this value when it finishes you must create a thread in a new thread group which will be assigned a new pid and then call <tt>wait(2)</tt>. To create such a thread you have to remove the <tt>CLONE_THREAD</tt> flag and replace it with <tt>SIGCHLD</tt> which will be the signal that will be send to the parent when the function returns. If we choose to send another signal, then <tt>wait(2)</tt> should be called with the <tt>__WALL</tt> or <tt>__WCLONE</tt> options.</p>
<p>These were the basics of creating threads. However, creating threads is only a part of concurrent programming. We will now create spinlocks using some internal functions of gcc. So, here are lock and unlock functions.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p86code10'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8610"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p86code10"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> lock<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> spinlock<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>__sync_lock_test_and_set<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>spinlock<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> unlock<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> spinlock<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    __sync_lock_release<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>spinlock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>It&#8217;s pretty simple using the functions gcc provides us. You can find more at the <a href="http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/">gcc documentation</a> about <a href="http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Atomic-Builtins.html">atomic builtins</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://fotis.loukos.me/blog/?feed=rss2&amp;p=86</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Site update</title>
		<link>http://fotis.loukos.me/blog/?p=92</link>
		<comments>http://fotis.loukos.me/blog/?p=92#comments</comments>
		<pubDate>Sat, 14 Nov 2009 15:03:53 +0000</pubDate>
		<dc:creator>Fotis</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://fotis.loukos.me/blog/?p=92</guid>
		<description><![CDATA[I have moved my site to another host. So if you have any problems please contact me or leave a comment. More posts will come soon!]]></description>
			<content:encoded><![CDATA[<p>I have moved my site to another host. So if you have any problems please contact me or leave a comment.</p>
<p>More posts will come soon!</p>
]]></content:encoded>
			<wfw:commentRss>http://fotis.loukos.me/blog/?feed=rss2&amp;p=92</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Linux kernel pipe NULL pointer dereference exploit (CVE-2009-3547)</title>
		<link>http://fotis.loukos.me/blog/?p=91</link>
		<comments>http://fotis.loukos.me/blog/?p=91#comments</comments>
		<pubDate>Thu, 05 Nov 2009 00:51:34 +0000</pubDate>
		<dc:creator>Fotis</dc:creator>
				<category><![CDATA[Exploits]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://fotis.loukos.me/blog/?p=91</guid>
		<description><![CDATA[Another exploit for the kernel pipe NULL pointer dereference bug. This one is inspired by Spender&#8216;s great work for his enlightenment framework. It seems to exist at every 2.6 and 2.4 kernel version I&#8217;ve tested! Another sock_sendpage maybe? This sample exploit only works for versions &#62;= 2.6.17. You can download it here. As usual more information in [...]]]></description>
			<content:encoded><![CDATA[<p>Another exploit for the kernel pipe NULL pointer dereference bug. This one is inspired by <a href="http://www.grsecurity.net">Spender</a>&#8216;s great work for his enlightenment framework. It seems to exist at every 2.6 and 2.4 kernel version I&#8217;ve tested! Another sock_sendpage maybe? This sample exploit only works for versions &gt;= 2.6.17. You can download it <a href="http://fotis.loukos.me/security/exploits/gayros.c">here</a>. As usual more information in the code. This time there are some funny quotes too! I haven&#8217;t done a lot of tests, so any feedback, and especially versions you have tested it and it worked, is welcome!</p>
<p>EDIT: New version is out. It adds support for the detection of kernels compiled with spinlock debugging options. Download it <a href="http://fotis.loukos.me/security/exploits/gayros-2.c">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://fotis.loukos.me/blog/?feed=rss2&amp;p=91</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Loading symbols when debugging the kernel and kernel modules</title>
		<link>http://fotis.loukos.me/blog/?p=74</link>
		<comments>http://fotis.loukos.me/blog/?p=74#comments</comments>
		<pubDate>Thu, 29 Oct 2009 19:06:55 +0000</pubDate>
		<dc:creator>Fotis</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Linux Kernel]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://fotis.loukos.me/blog/?p=74</guid>
		<description><![CDATA[Recently I received some comments from a friend about a previous article on linux kernel debugging using kgdb. What he asked me was how could he load symbols from a kernel or a kernel module. So I wrote a quick guide to help you start with kernel debugging. After each step I will show you [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I received some comments from a friend about a previous article on linux kernel debugging using kgdb. What he asked me was how could he load symbols from a kernel or a kernel module. So I wrote a quick guide to help you start with kernel debugging. After each step I will show you the gdb output.</p>
<p>First of all you should start gdb!</p>
<pre>$ gdb
GNU gdb (GDB) 6.8.50.20090628-cvs-debian
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
&lt;http://www.gnu.org/software/gdb/bugs/&gt;.
(gdb)</pre>
<p>Then you should load all kernel symbols from the vmlinux file. This can be found at the directory where you compiled the kernel, most probably /usr/src/linux. Remember to compile the kernel using debug information by setting the appropriate option, it will help you a lot!</p>
<pre>(gdb) file vmlinux
Reading symbols from /home/fotisl/programs/kgdb/vmlinux...done.
(gdb)</pre>
<p>You&#8217;re ready to start debugging! Set the target and use the Alt-SysRq-G sequence as it was described at the previous post. You can now set breakpoints, watch anything you want in memory, step or continue running the kernel!</p>
<pre>(gdb) target remote /dev/pts/12
Remote debugging using /dev/pts/12
kgdb_breakpoint (key=103, tty=0x0) at kernel/kgdb.c:1721
1721            wmb(); /* Sync point after breakpoint */
(gdb)</pre>
<p>Now let&#8217;s see how we can debug kernel modules. I will test the l2cap bluetooth kernel module.</p>
<p>You first need to find the object file which contains the module. For l2cap this is net/bluetooth/l2cap.o in the kernel source tree. Transfer this to the host (or the machine running gdb if you&#8217;re not using a virtual machine). Then load the module in the virtual machine. This creates a new directory in /sys/module named after the module name, i.e. l2cap. Inside this directory, there is another one named sections which contains the addresses where all sections are loaded. We are interested in the .text section so we read the file /sys/module/l2cap/sections/.text.</p>
<pre>$ cat /sys/module/l2cap/sections/.text
0xe0c77000</pre>
<p>We know where the .text section is loaded so we can now load the symbols from l2cap.o using the add-symbol-file gdb command.</p>
<pre>(gdb) add-symbol-file l2cap.o 0xe0c77000
add symbol table from file "l2cap.o" at
        .text_addr = 0xe0c77000
(y or n) y
Reading symbols from /home/fotisl/programs/kgdb/l2cap.o...done.
(gdb)</pre>
<p>If you need to load other sections too, in case they are not contiguous with the text in memory, you need to read their addresses. For example we&#8217;ll load both the .text and the .data sections (you should do .bss too but it&#8217;s omitted since I wanted to write a quick and dirty guide and it&#8217;s already very big!)</p>
<p>Find where both .text and .data are loaded.</p>
<pre>$ cat /sys/module/l2cap/sections/.text
0xe0c77000
$ cat /sys/module/l2cap/sections/.data
0xe0c7b438</pre>
<p>Then you load apart from the .text section the .data too.</p>
<pre>(gdb) add-symbol-file l2cap.o 0xe0c77000 -s .data 0xe0c7b438
add symbol table from file "l2cap.o" at
        .text_addr = 0xe0c77000
        .data_addr = 0xe0c7b438
(y or n) y
Reading symbols from /home/fotisl/programs/kgdb/l2cap.o...done.
(gdb)</pre>
<p>You&#8217;re now ready to start debugging your kernel module!</p>
]]></content:encoded>
			<wfw:commentRss>http://fotis.loukos.me/blog/?feed=rss2&amp;p=74</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ecryptfs NULL pointer dereference exploit (CVE-2009-2908)</title>
		<link>http://fotis.loukos.me/blog/?p=80</link>
		<comments>http://fotis.loukos.me/blog/?p=80#comments</comments>
		<pubDate>Sat, 17 Oct 2009 15:40:49 +0000</pubDate>
		<dc:creator>Fotis</dc:creator>
				<category><![CDATA[Exploits]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://fotis.loukos.me/blog/?p=80</guid>
		<description><![CDATA[Commit afc2b6932f48f200736d3e36ad66fee0ec733136 at the linux kernel is about a NULL pointer dereference that happens under certain circumstances. As many of you already know, NULL pointer dereferences are exploitable and are actually a &#8220;hot topic&#8221; lately. You can find a lot of references, such as Julien Tinnes&#8217; great blog post, Brad Spender&#8216;s enlightenment framework, etc. I [...]]]></description>
			<content:encoded><![CDATA[<p>Commit <a href="http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.31.y.git;a=commit;h=afc2b6932f48f200736d3e36ad66fee0ec733136">afc2b6932f48f200736d3e36ad66fee0ec733136</a> at the linux kernel is about a NULL pointer dereference that happens under certain circumstances. As many of you already know, NULL pointer dereferences are exploitable and are actually a &#8220;hot topic&#8221; lately. You can find a lot of references, such as Julien Tinnes&#8217; great <a href="http://blog.cr0.org/2009/08/linux-null-pointer-dereference-due-to.html">blog post</a>, <a href="http://www.grsecurity.net/">Brad Spender</a>&#8216;s enlightenment framework, etc. I haven&#8217;t seen any exploits for this bug yet so I&#8217;ve written one. You can download it <a href="http://fotis.loukos.me/security/exploits/paokara.c">here</a>. I won&#8217;t go into details here, you can read the source code which is full of helpful comments. A description of the exploit would be actually a copy/paste of all the comments here, so it&#8217;s better to read the entire source code!</p>
]]></content:encoded>
			<wfw:commentRss>http://fotis.loukos.me/blog/?feed=rss2&amp;p=80</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
