Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Reflection permission error

I'm trying to load a class via an URLClassLoader (well, it neither works with an normal class loader) and want them to not have any permission.

Therefore, i created my own security manager, which genereates a key on startup, which can only by requested once (in main thread). The security manager has 2 lists, the applicationThread, which will be granted any right and the temporaryList, which will be granted one right just once (it's about the reflection).

As it is very hard to descripe, i decided to upload the whole thing: look at the link below

Ok, coming back: I created a WatchDog thread, which checks if the thread doesn't take too much time.

When i now start to instance two classes from an URLClassLoader, I call exactly 30 methods without getting any errors, but on the 31st call, it tries to check Permissions for the following but this is just happaning after the 30th call.

java.lang.RuntimePermission accessClassInPackage.sun.reflect),

Does anyone know what's going on there?

edit: I had time to strip down the example. http://myxcode.at/securitymanager.zip I found out, that the SecurityManager is not asked synchronous. Just run this small piece of code and have a look at the red lines.

If the red lines come in the very first line, just run the program again, you will find out that it seems a little bit uncontrolled.

The problem more or less is, that i need the security manager to be synchronized. Here is my output for those who cannot face the error(bug?) http://pastebin.com/E9yLRLif

edit2: maybe its about the console? maybe the console is too slow?

like image 405
Philipp Spiess Avatar asked Oct 10 '22 14:10

Philipp Spiess


1 Answers

For me the check occurs when i=15:

checkPermission ( (java.lang.RuntimePermission accessClassInPackage.sun.reflect) ) for Thread[main,5,main]

The reason for the delayed permission check is an inflationThreshold of the ReflectionFactory class which is used by the invoke method of NativeMethodAccessorImpl.java:

public Object invoke(Object obj, Object[] args)
        throws IllegalArgumentException, InvocationTargetException {
    if (++numInvocations > ReflectionFactory.inflationThreshold()) {
        MethodAccessorImpl acc = (MethodAccessorImpl) new MethodAccessorGenerator()
                .generateMethod(method.getDeclaringClass(), method
                        .getName(), method.getParameterTypes(),
                        method.getReturnType(), method
                                .getExceptionTypes(), method
                                .getModifiers());
        parent.setDelegate(acc);
    }

    return invoke0(method, obj, args);
}

To disable the delay you could use Reflection API :)

Field hack = Class.forName("sun.reflect.ReflectionFactory").getDeclaredField("inflationThreshold");
hack.setAccessible(true);
hack.set(null, 0);
like image 55
jeha Avatar answered Oct 13 '22 11:10

jeha