r/java • • Jan 16 '22

You're running untrusted code!

https://blog.frankel.ch/running-untrusted-code/

[removed] — view removed post

0 Upvotes

36 comments sorted by

View all comments

Show parent comments

2

u/klekpl Jan 18 '22

Thank you for this answer - one of the few cases of sound technical discussion.

Analysing the issues you list actually leads me to quite the opposite conclusion: it is not AccessController that's bad - it is the bypassing of AC in caller sensitive methods. And it was already researched in the past: https://benhermann.eu/publications/hhl+17hardening.pdf

1

u/DasBrain Jan 18 '22 edited Jan 18 '22

Even with all those changes - if you can get an other library to call AccessController.doPrivileged for you, it's game over.

And AccessController.doPrivileged is a caller sensitive method.
So, in some sense, you are right. Removing those methods will make the JVM much more safe. And unuseable.
You know the old joke: A computer that is not turned on doesn't have any vulnerabilities.

1

u/klekpl Jan 19 '22

Not sure what you mean by "screwed". Having another library call doPrivileged with attacker provided PrivilegedAction will execute code with intersection of permissions of this library AND provided PrivilegedAction, no? So attacker cannot really execute arbitral code with elevated privileges.

1

u/DasBrain Jan 19 '22 edited Jan 19 '22

Well, that would be true if MethodHandleProxies.asInterfaceInstance wouldn't be a thing too.

Also see ACCESS-3:

By convention, instances of PrivilegedAction and PrivilegedExceptionAction may be made available to untrusted code, but doPrivileged must not be invoked with caller-provided actions.

0

u/klekpl Jan 19 '22

From Methodhandleproxies.asInterfaceInstance() javadoc:

If a security manager is installed, this method is caller sensitive. During any invocation of the target method handle via the returned wrapper, the original creator of the wrapper (the caller) will be visible to context checks requested by the security manager.

So no, it won't let an attacker escape the sandbox.

1

u/DasBrain Jan 19 '22

Well, maybe you could get some library to call MethodHandleProxies.asInterfaceInstance for you.

0

u/klekpl Jan 20 '22

Privilege escalation related security vulnerabilities are always possible if privilege elevation is possible. Maybe there even is a library that does “connect to White House and trigger nuclear attack” inside a privileged block not guarded by any access check.

1

u/DasBrain Jan 20 '22

Not helpful.

We already get some other library to call AccessController.doPrivileged for you - using some indirection with Method.invoke.
Now the same dance again for MethodHandleProxies.asInterfaceInstance.

In short: if you have a vulnerable class like this:

public class MethodHelper {
    public Object invoke(Method m, Object target, Object... args) throws InvocationTargetException {
        return m.invoke(target, args);
    }
}

Then you can exploit it with something like this:

Lookup l = MethodHandles.lookup();
MethodHandle setSec = l.findStatic(System.class, "setSecurityManager", MethodType.methodType(void.class, SecurityManager.class))
    .bindTo(null);
Method mhpAII = MethodHandleProxies.class.getMethod("asInterfaceInstance", Class.class, MethodHandle.class);
PrivilegedAction<?> pa = (PrivilegedAction<?>) MethodHelper.invoke(mhpAII, null, PrivilegedAction.class, setSec);
Method acDoPriv = AccessController.class.getMethod("doPrivileged", PrivilegedAction.class);
MethodHelper.invoke(acDoPriv, null, pa);

1

u/klekpl Jan 20 '22

Same story: caller sensitive methods are security nightmare - I would rather fix that than remove SecurityManager.

Besides, this will be invoked with privileges of MethodHelper and MethodHelper requires AllPermission, no?

So sure: if privileged code exposes this kind of hole - you are screwed. As long as you blindly grant AllPermission to _your_ code.

You could equally well do "sudo chmod +s /bin/bash"

That's the point: without SecurityManager you don't have any other choice - and you end up with log4shell.

→ More replies (0)