Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by NavBot

...

Code Block
bgColor#ccccff
private final void makeAccessible() { // private final
  String fieldName = "i";"i"; // hardcode
  C c = new C();
  // ...
} 

class C {
  private int i = 10; // private
}

...

Code Block
bgColor#FFcccc
package Safe;
public class Trusted {
  Trusted() { } // package private constructor
  public static &lt;T&gt;<T> T create(Class&lt;T&gt;Class<T> c) throws InstantiationException, IllegalAccessException {
    return c.newInstance();
  }
}

package Attacker;
import Safe.Trusted;

public class Attack {
  public static void main(String[] args) throws InstantiationException, IllegalAccessException {
    System.out.println(Trusted.create(Trusted.class)); // succeeds
  }
}

...

Code Block
bgColor#ccccff
package Safe;
import java.beans.Beans;

public class Trusted {
  Trusted() { }

  public static &lt;T&gt;<T> T create(Class&lt;T&gt;Class<T> c) {
    try {     
      ClassLoader cl = new SafeClassLoader();
      Object b = Beans.instantiate(cl, c.getName());
      return c.cast(b);
    } catch(Throwable t) { t.printStackTrace(); /* forward to handler */ }
    return null;
  }
}

// code outside the package
package Attacker;
import Safe.Trusted;

public class Attack {
  public static void main(String[] args) {
    Object o = Trusted.create(Trusted.class); // throws java.lang.IllegalAccessException, o = null
  }
}

...

SEC02-J. Do not expose standard APIs that may bypass Security Manager checks to untrusted code&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      02. Platform Security (SEC)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      SEC04-J. Do not rely on the default automatic signature verification provided by URLClassLoader and java.util.jar