title: Java Sandboxing Security Policy
body: <p>I am following this <a href="https://blog.jayway.com/2014/06/13/sandboxing-plugins-in-java/" rel="nofollow noreferrer">tutorial, Sandboxing plugins in Java</a> on how to achieve sandboxing when plugins are used.</p>
  <p>This is my main class where I load the class from the Jar file and run the interface function:</p>
  <pre><code>import java.io.File;
  import java.net.URL;
  import java.security.Policy;
  
  public class Main {
  
      public static void main(String[] args){
  
          Policy.setPolicy(new SandboxSecurityPolicy());
          System.setSecurityManager(new SecurityManager());
  
  
          try {
              File file = new File(&quot;JarDummy.jar&quot;);
              URL urlJar = file.toURI().toURL();
  
              ClassLoader pluginLoader = new PluginClassLoader(urlJar);
  
              Class&lt;?&gt; pluginClass = pluginLoader.loadClass(&quot;ZEngine&quot;);
              IEngine plugin = (IEngine) pluginClass.newInstance();
  
              plugin.build(&quot;file.txt&quot;);
  
          }catch (Exception e){
              e.printStackTrace();
          }
      }
  
  
  }
  </code></pre>
  <p>This is my interface:</p>
  <pre><code>public interface IEngine {
  
      void build(String filename);
  }
  </code></pre>
  <p>This is my plugin class loader:</p>
  <pre><code>import java.net.URL;
  import java.net.URLClassLoader;
  
  public class PluginClassLoader extends URLClassLoader {
      public PluginClassLoader(URL jarFileUrl) {
          super(new URL[] {jarFileUrl});
      }
  }
  </code></pre>
  <p>And this is my policy where different permissions are given if the class is loaded from the plugin:</p>
  <pre><code>import sun.plugin.security.PluginClassLoader;
  import java.security.AllPermission;
  import java.security.PermissionCollection;
  import java.security.Permissions;
  import java.security.Policy;
  import java.security.ProtectionDomain;
  
  public class SandboxSecurityPolicy extends Policy {
  
      @Override
      public PermissionCollection getPermissions(ProtectionDomain domain) {
          if (isPlugin(domain)) {
              return pluginPermissions();
          }
          else {
              return applicationPermissions();
          }
      }
  
      private boolean isPlugin(ProtectionDomain domain) {
          return domain.getClassLoader() instanceof PluginClassLoader;
      }
  
      private PermissionCollection pluginPermissions() {
          Permissions permissions = new Permissions(); // No permissions
          return permissions;
      }
  
      private PermissionCollection applicationPermissions() {
          Permissions permissions = new Permissions();
          permissions.add(new AllPermission());
          return permissions;
      }
  }
  </code></pre>
  <p>When I run the main class and code executes the <code>plugin.build()</code>, I am getting a stackoverflow exception with the following trace:</p>
  <pre><code>*** java.lang.instrument ASSERTION FAILED ***: &quot;!errorOutstanding&quot; with message transform method call failed at JPLISAgent.c line: 844
  *** java.lang.instrument ASSERTION FAILED ***: &quot;!errorOutstanding&quot; with message transform method call failed at JPLISAgent.c line: 844
  *** java.lang.instrument ASSERTION FAILED ***: &quot;!errorOutstanding&quot; with message transform method call failed at JPLISAgent.c line: 844
  Exception in thread &quot;main&quot; java.lang.StackOverflowError
      at sun.nio.cs.UTF_8.updatePositions(UTF_8.java:77)
      at sun.nio.cs.UTF_8.access$200(UTF_8.java:57)
      at sun.nio.cs.UTF_8$Encoder.encodeArrayLoop(UTF_8.java:636)
      at sun.nio.cs.UTF_8$Encoder.encodeLoop(UTF_8.java:691)
      at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:579)
      at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:271)
      at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125)
      at java.io.OutputStreamWriter.write(OutputStreamWriter.java:207)
      at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:129)
      at java.io.PrintStream.write(PrintStream.java:526)
      at java.io.PrintStream.print(PrintStream.java:669)
      at java.io.PrintStream.println(PrintStream.java:806)
      at SandboxSecurityPolicy.getPermissions(SandboxSecurityPolicy.java:12)
      at java.security.Policy.implies(Policy.java:721)
      at java.security.ProtectionDomain.implies(ProtectionDomain.java:285)
      at java.security.AccessControlContext.checkPermission(AccessControlContext.java:450)
      at java.security.AccessController.checkPermission(AccessController.java:884)
      at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
      at java.lang.SecurityManager.checkPackageAccess(SecurityManager.java:1564)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:318)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      at SandboxSecurityPolicy.isPlugin(SandboxSecurityPolicy.java:23)
      at SandboxSecurityPolicy.getPermissions(SandboxSecurityPolicy.java:13)
  </code></pre>
  <p>I am not sure why that <code>instanceOf</code> is creating a recursion leading to this overflow. I appreciate your help. Thanks!</p>
  <p>UPDATE: I ran the same code on a different machine, it worked fine. It seems like a JDK bug.</p>