Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li><p>Run the untrusted code in its own thread. This for example prevents problems with infinite loops and such, and makes the future steps easier. Have the main thread wait for the thread to finish, and if takes too long, kill it with Thread.stop. Thread.stop is deprecated, but since the untrusted code shouldn't have access to any resources, it would be safe to kill it.</p></li> <li><p>Set a <a href="http://java.sun.com/javase/6/docs/api/java/lang/SecurityManager.html" rel="noreferrer">SecurityManager</a> on that Thread. Create a subclass of SecurityManager which overrides <a href="http://java.sun.com/javase/6/docs/api/java/lang/SecurityManager.html#checkPermission(java.security.Permission)" rel="noreferrer">checkPermission(Permission perm)</a> to simply throw a <a href="http://java.sun.com/javase/6/docs/api/java/lang/SecurityException.html" rel="noreferrer">SecurityException</a> for all permissions except a select few. There's a list of methods and the permissions they require here: <a href="http://java.sun.com/javase/6/docs/technotes/guides/security/permissions.html" rel="noreferrer">Permissions in the Java<sup>TM</sup> 6 SDK</a>.</p></li> <li><p>Use a custom ClassLoader to load the untrusted code. Your class loader would get called for all classes which the untrusted code uses, so you can do things like disable access to individual JDK classes. The thing to do is have a white-list of allowed JDK classes.</p></li> <li><p>You might want to run the untrusted code in a separate JVM. While the previous steps would make the code safe, there's one annoying thing the isolated code can still do: allocate as much memory as it can, which causes the visible footprint of the main application to grow.</p></li> </ol> <p><a href="http://jcp.org/en/jsr/detail?id=121" rel="noreferrer">JSR 121: Application Isolation API Specification</a> was designed to solve this, but unfortunately it doesn't have an implementation yet.</p> <p>This is a pretty detailed topic, and I'm mostly writing this all off the top of my head.</p> <p>But anyway, some imperfect, use-at-your-own-risk, probably buggy (pseudo) code:</p> <p><em>ClassLoader</em></p> <pre><code>class MyClassLoader extends ClassLoader { @Override public Class&lt;?&gt; loadClass(String name) throws ClassNotFoundException { if (name is white-listed JDK class) return super.loadClass(name); return findClass(name); } @Override public Class findClass(String name) { byte[] b = loadClassData(name); return defineClass(name, b, 0, b.length); } private byte[] loadClassData(String name) { // load the untrusted class data here } } </code></pre> <p><em>SecurityManager</em></p> <pre><code>class MySecurityManager extends SecurityManager { private Object secret; public MySecurityManager(Object pass) { secret = pass; } private void disable(Object pass) { if (pass == secret) secret = null; } // ... override checkXXX method(s) here. // Always allow them to succeed when secret==null } </code></pre> <p><em>Thread</em></p> <pre><code>class MyIsolatedThread extends Thread { private Object pass = new Object(); private MyClassLoader loader = new MyClassLoader(); private MySecurityManager sm = new MySecurityManager(pass); public void run() { SecurityManager old = System.getSecurityManager(); System.setSecurityManager(sm); runUntrustedCode(); sm.disable(pass); System.setSecurityManager(old); } private void runUntrustedCode() { try { // run the custom class's main method for example: loader.loadClass("customclassname") .getMethod("main", String[].class) .invoke(null, new Object[]{...}); } catch (Throwable t) {} } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload