<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Virtual Void?</title>
	<atom:link href="http://virtualvoid.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://virtualvoid.wordpress.com</link>
	<description></description>
	<lastBuildDate>Fri, 06 Mar 2009 12:12:58 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='virtualvoid.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/ae0c8f5244cc15bfa3bc761830b160f9?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Virtual Void?</title>
		<link>http://virtualvoid.wordpress.com</link>
	</image>
			<item>
		<title>Breaking the JVM</title>
		<link>http://virtualvoid.wordpress.com/2009/03/06/breaking-the-jvm/</link>
		<comments>http://virtualvoid.wordpress.com/2009/03/06/breaking-the-jvm/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 12:12:58 +0000</pubDate>
		<dc:creator>gambistics</dc:creator>
				<category><![CDATA[Under the hood]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jvm]]></category>

		<guid isPermaLink="false">http://virtualvoid.wordpress.com/?p=6</guid>
		<description><![CDATA[In my recently finished thesis, I stressed the importance of the JVM verifier and briefly talked about the security issues rogue bytecode might introduce if not detected by the verifier. I did so, just by assumption, only theoretically knowing that an attack might work (and probably how), but without having tried it in practice.
So now, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=virtualvoid.wordpress.com&blog=343707&post=6&subd=virtualvoid&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>In my recently finished thesis, I stressed the importance of the JVM verifier and briefly talked about the security issues rogue bytecode might introduce if not detected by the verifier. I did so, just by assumption, only theoretically knowing that an attack might work (and probably how), but without having tried it in practice.</p>
<p>So now, I turned off the verifier and looked what happened&#8230;</p>
<p>I generated a simple class with <a href="http://asm.objectweb.org">ASM</a> which looks like this when disassembled:</p>
<pre class="brush: java;">
% javap -c MemoryTools
public class MemoryTools extends java.lang.Object{
public MemoryTools();
  Code:
   0:	aload_0
   1:	invokespecial	#9; //Method java/lang/Object.&quot;&lt;init&gt;&quot;:()V
   4:	return

public static int addressOfObject(java.lang.Object);
  Code:
   0:	aload_0
   1:	ireturn
}
</pre>
<p>It has just a no-op constructor and one method <code>addressOfObject</code>. It loads the object reference passed as first parameter and tries to return it as if it were an integer. We assume that a reference to an object is just a 32-bit pointer to some memory structure representing the object, which basically is an integer. This bytecode sequence is, of course, rejected by the JVM bytecode verifier. To test the class I wrote another class with a main-method:</p>
<pre class="brush: java;">
public class MemoryLoader {
	public static void main(String[] args) {
		System.out.println(MemoryTools.addressOfObject(new Object()));
	}
}
</pre>
<p>Executing the code fails as expected:</p>
<pre class="brush: java;">
% java -cp .:bin MemoryLoader
Exception in thread &quot;main&quot; java.lang.VerifyError: (class: MemoryTools, method: addressOfObject signature: (Ljava/lang/Object;)I) Expecting to find integer on stack
at MemoryLoader.main(MemoryLoader.java:3)
</pre>
<p>The verifier is doing its job. When verifying the <code>ireturn</code> instruction, the verifier notices that an object reference is on the stack, which <code>ireturn</code> can&#8217;t handle. When generating bytecode, e.g. when writing a compiler, verifier errors are common at testing and expected. In our particular case, however, we want to test what happens without the verifier.</p>
<p>The Sun JVM has some options which affect the verifier, most notably <code>-Xbootclasspath</code>. Classes from the bootclasspath are simply not verified at all. So, let&#8217;s try:</p>
<pre class="brush: java;">
% java -Xbootclasspath/a:.:bin MemoryLoader
=============== DEBUG MESSAGE: illegal bytecode sequence - method not verified ================
Exception in thread &quot;main&quot; java.lang.NullPointerException
at MemoryTools.addressOfObject(MemoryTools.java)
at MemoryLoader.main(MemoryLoader.java:3)
</pre>
<p>Even with the verifier turned off, now the execution fails (before it was the loading) with a debug message and a seemingly misplaced <code>NullPointerException</code>. If the JVM finds illegal bytecode sequences on its own, perhaps the verifier isn&#8217;t needed at all?!?</p>
<p>Some googling turned up parts of the <a href="http://xref.jsecurity.net/openjdk-6/langtools/db/d67/interpreter_8cpp-source.html">source code</a> of the Sun Hotspot VM&#8217;s interpreter, where this error message is defined. An entry point to the error message was stuffed into some dispatch table, from where it was difficult to examine when the message actually was shown. I had more luck in <a href="http://xref.jsecurity.net/openjdk-6/langtools/d5/d5b/hotspot_2agent_2src_2share_2classes_2sun_2jvm_2hotspot_2interpreter_2_byte_codes_8java-source.html#l00414">another file</a> listing all the bytecodes in a table next to their return types. So I guessed, that the JVM bytecode interpreter probably does some simple verification of pairs of bytecodes by checking if the two bytecode instructions can be next to each other based on their types. How to outsmart this simple verification? Nothing as simple as that:</p>
<pre class="brush: java;">
% javap -c MemoryTools
public class MemoryTools extends java.lang.Object{
//...
public static int addressOfObject(java.lang.Object);
  Code:
   0:    aload_0
   1:    nop
   2:    ireturn
}
</pre>
<p>A <code>nop</code> between <code>aload</code> and <code>ireturn</code> might already defeats the simple verification. Since both pairs of instructions (<code>aload</code>,<code>nop</code>) and (<code>nop</code>,<code>ireturn</code>) are potentially valid, the interpreter might be deceived. Let&#8217;s try again:</p>
<pre class="brush: java;">
% java -Xbootclasspath/a:.:bin MemoryLoader
-1331261416
</pre>
<p>Here we are.</p>
<p>Getting the memory address of an object is somewhat boring. Reading arbitrary memory of the JVM runtime process, however, would be nice&#8230; Since a reference is just a pointer to the object&#8217;s internal structure, there probably are bytecode instructions to dereference the pointer to access members as well. I tried this simple class:</p>
<pre class="brush: java;">
public class Data {
	public int x;
}
</pre>
<p>In order to do something useful with this, we have to know the internal structure of an object. I didn&#8217;t make the effort to go through the sources of the JVM again, but found some hints <a href="http://www.codeinstructions.com/2008/12/java-objects-memory-structure.html">here</a>. A java object in the Sun JVM, accordingly, has an 8-byte header followed by the fields ordered by decreasing field-length. In pseudo-C syntax the structure of <code>Data</code> would look like this:</p>
<pre class="brush: cpp;">
struct Data{
  int hashCode;
  void* clazz;
  int x;
};
</pre>
<p>On a 32-bit platform the field x should have an offset of 8 from the beginning of the structure. Given a pointer to an object of class <code>Data p</code>, retrieving <code>Data.x</code> can be expressed in terms of pseudo-C syntax as the dereferencing of a pointer offset by 8:</p>
<pre class="brush: cpp;">
char *p; // pointer to an object of type Data
int x = *((int *)(p + 8));
</pre>
<p>Vice versa this means, that reading an integer from an arbitrary address can be realized by subtracting 8 from that address, treating the address as a pointer to <code>Data</code> and reading <code>Data.x</code>. In bytecode that looks like this:</p>
<pre class="brush: java;">
% javap -c MemoryTools
public class MemoryTools extends java.lang.Object{
//...
public static int readInt(int);
  Code:
   0:	iload_0
   1:	bipush	8
   3:	isub
   4:	nop
   5:	getfield	#21; //Field Data.x:I
   8:	ireturn
}
</pre>
<p>I wrote another short program, which, using <code>readInt</code>, should read 0&#215;48 bytes of memory from the start of a byte-array object and write it to the standard output.</p>
<pre class="brush: java;">
public class MemoryLoader {
	public static void int2bytes(int i,byte[] ar){
		ar[3] = (byte)((i&gt;&gt;24) &amp; 0xff);
		ar[2] = (byte)((i&gt;&gt;16) &amp; 0xff);
		ar[1] = (byte)((i&gt;&gt;8) &amp; 0xff);
		ar[0] = (byte)(i &amp; 0xff);
	}
	public static void main(String[] args) throws IOException {
                byte[] res = new byte[4];
		Object test = &quot;das ist das haus des nikolaus&quot;.getBytes();

		int pos = MemoryTools.addressOfObject(test);
		for (int i=0;i&lt;0x30;i+=4){
			int2bytes(MemoryTools.readInt(pos+i),res);
			System.out.write(res);
		}
	}
}
</pre>
<p>Piping the output through <code>hexdump</code> shows:</p>
<pre class="brush: java;">
% java -Xbootclasspath/a:.:bin MemoryLoader|hexdump -C
00000000  01 00 00 00 00 08 b4 8c  1d 00 00 00 64 61 73 20  |............das |
00000010  69 73 74 20 64 61 73 20  68 61 75 73 20 64 65 73  |ist das haus des|
00000020  20 6e 69 6b 6f 6c 61 75  73 00 00 00 00 00 00 00  | nikolaus.......|
00000030
</pre>
<p>The hashCode is 1 (because it wasn&#8217;t calculated yet), then the pointer to byte-array&#8217;s internal class structure follows. The next 4 bytes represent the length of the byte-array, 0&#215;1d, then the content bytes themselves follow.</p>
<p>These experiments probably only work on a 32-bit JVM, since the memory layout of structures is different on x64. I used Ubuntu intrepid, and the OpenJDK 6:</p>
<p><code><br />
java version "1.6.0_0"<br />
IcedTea6 1.3.1 (6b12-0ubuntu6.1) Runtime Environment (build 1.6.0_0-b12)<br />
OpenJDK Server VM (build 1.6.0_0-b12, mixed mode)<br />
</code></p>
<p>You can get the source code from <a href="http://github.com/jrudolph/rogue-bytecodes/">github</a>. Please try yourself and tell from the journeys into the depth of the JVM <em>you</em> made.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/virtualvoid.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/virtualvoid.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/virtualvoid.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/virtualvoid.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/virtualvoid.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/virtualvoid.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/virtualvoid.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/virtualvoid.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/virtualvoid.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/virtualvoid.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=virtualvoid.wordpress.com&blog=343707&post=6&subd=virtualvoid&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://virtualvoid.wordpress.com/2009/03/06/breaking-the-jvm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/64f6e19203e7846687b256f2ae4b6b0d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">johannes</media:title>
		</media:content>
	</item>
		<item>
		<title>Hello world!</title>
		<link>http://virtualvoid.wordpress.com/2006/08/07/hello-world/</link>
		<comments>http://virtualvoid.wordpress.com/2006/08/07/hello-world/#comments</comments>
		<pubDate>Mon, 07 Aug 2006 16:34:05 +0000</pubDate>
		<dc:creator>gambistics</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Yeah I&#8217;m here. Read the about page to get started  
       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=virtualvoid.wordpress.com&blog=343707&post=1&subd=virtualvoid&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Yeah I&#8217;m here. Read the <a href="http://virtualvoid.wordpress.com/about/" title="about">about page</a> to get started <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/virtualvoid.wordpress.com/1/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/virtualvoid.wordpress.com/1/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/virtualvoid.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/virtualvoid.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/virtualvoid.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/virtualvoid.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/virtualvoid.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/virtualvoid.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/virtualvoid.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/virtualvoid.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/virtualvoid.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/virtualvoid.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=virtualvoid.wordpress.com&blog=343707&post=1&subd=virtualvoid&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://virtualvoid.wordpress.com/2006/08/07/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/64f6e19203e7846687b256f2ae4b6b0d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">johannes</media:title>
		</media:content>
	</item>
	</channel>
</rss>