Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by sciSpider Java v3.0

...

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 <T>&lt;T&gt; T create(Class<T>Class&lt;T&gt; 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 <T>&lt;T&gt; T create(Class<T>Class&lt;T&gt; 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
  }
}

...

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

References

Wiki Markup
\[[Chan 99|AA. Java References#Chan 99]\] java.lang.reflect AccessibleObject
\[[SCG 07|AA. Java References#SCG 07]\] Guideline 6-4 Be aware of standard APIs that perform Java language access checks against the immediate caller

...

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