|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package sun.invoke.util; |
|
|
|
import java.lang.reflect.Modifier; |
|
import static java.lang.reflect.Modifier.*; |
|
import sun.reflect.Reflection; |
|
|
|
|
|
|
|
|
|
*/ |
|
public class VerifyAccess { |
|
|
|
private VerifyAccess() { } |
|
|
|
private static final int PACKAGE_ONLY = 0; |
|
private static final int PACKAGE_ALLOWED = java.lang.invoke.MethodHandles.Lookup.PACKAGE; |
|
private static final int PROTECTED_OR_PACKAGE_ALLOWED = (PACKAGE_ALLOWED|PROTECTED); |
|
private static final int ALL_ACCESS_MODES = (PUBLIC|PRIVATE|PROTECTED|PACKAGE_ONLY); |
|
private static final boolean ALLOW_NESTMATE_ACCESS = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static boolean isMemberAccessible(Class<?> refc, |
|
Class<?> defc, |
|
int mods, |
|
Class<?> lookupClass, |
|
int allowedModes) { |
|
if (allowedModes == 0) return false; |
|
assert((allowedModes & PUBLIC) != 0 && |
|
(allowedModes & ~(ALL_ACCESS_MODES|PACKAGE_ALLOWED)) == 0); |
|
|
|
if (!isClassAccessible(refc, lookupClass, allowedModes)) { |
|
return false; |
|
} |
|
|
|
if (defc == lookupClass && |
|
(allowedModes & PRIVATE) != 0) |
|
return true; |
|
switch (mods & ALL_ACCESS_MODES) { |
|
case PUBLIC: |
|
return true; |
|
case PROTECTED: |
|
assert !defc.isInterface(); |
|
if ((allowedModes & PROTECTED_OR_PACKAGE_ALLOWED) != 0 && |
|
isSamePackage(defc, lookupClass)) |
|
return true; |
|
if ((allowedModes & PROTECTED) == 0) |
|
return false; |
|
// Protected members are accessible by subclasses, which does not include interfaces. |
|
// Interfaces are types, not classes. They should not have access to |
|
|
|
if ((mods & STATIC) != 0 && |
|
!isRelatedClass(refc, lookupClass)) |
|
return false; |
|
if ((allowedModes & PROTECTED) != 0 && |
|
isSubClass(lookupClass, defc)) |
|
return true; |
|
return false; |
|
case PACKAGE_ONLY: |
|
assert !defc.isInterface(); |
|
return ((allowedModes & PACKAGE_ALLOWED) != 0 && |
|
isSamePackage(defc, lookupClass)); |
|
case PRIVATE: |
|
|
|
return (ALLOW_NESTMATE_ACCESS && |
|
(allowedModes & PRIVATE) != 0 && |
|
isSamePackageMember(defc, lookupClass)); |
|
default: |
|
throw new IllegalArgumentException("bad modifiers: "+Modifier.toString(mods)); |
|
} |
|
} |
|
|
|
static boolean isRelatedClass(Class<?> refc, Class<?> lookupClass) { |
|
return (refc == lookupClass || |
|
isSubClass(refc, lookupClass) || |
|
isSubClass(lookupClass, refc)); |
|
} |
|
|
|
static boolean isSubClass(Class<?> lookupClass, Class<?> defc) { |
|
return defc.isAssignableFrom(lookupClass) && |
|
!lookupClass.isInterface(); |
|
} |
|
|
|
static int getClassModifiers(Class<?> c) { |
|
// This would return the mask stored by javac for the source-level modifiers. |
|
// return c.getModifiers(); |
|
// But what we need for JVM access checks are the actual bits from the class header. |
|
|
|
if (c.isArray() || c.isPrimitive()) |
|
return c.getModifiers(); |
|
return Reflection.getClassAccessFlags(c); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static boolean isClassAccessible(Class<?> refc, Class<?> lookupClass, |
|
int allowedModes) { |
|
if (allowedModes == 0) return false; |
|
assert((allowedModes & PUBLIC) != 0 && |
|
(allowedModes & ~(ALL_ACCESS_MODES|PACKAGE_ALLOWED)) == 0); |
|
int mods = getClassModifiers(refc); |
|
if (isPublic(mods)) |
|
return true; |
|
if ((allowedModes & PACKAGE_ALLOWED) != 0 && |
|
isSamePackage(lookupClass, refc)) |
|
return true; |
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static boolean isTypeVisible(Class<?> type, Class<?> refc) { |
|
if (type == refc) { |
|
return true; |
|
} |
|
while (type.isArray()) type = type.getComponentType(); |
|
if (type.isPrimitive() || type == Object.class) { |
|
return true; |
|
} |
|
ClassLoader typeLoader = type.getClassLoader(); |
|
ClassLoader refcLoader = refc.getClassLoader(); |
|
if (typeLoader == refcLoader) { |
|
return true; |
|
} |
|
if (refcLoader == null && typeLoader != null) { |
|
return false; |
|
} |
|
if (typeLoader == null && type.getName().startsWith("java.")) { |
|
// Note: The API for actually loading classes, ClassLoader.defineClass, |
|
// guarantees that classes with names beginning "java." cannot be aliased, |
|
|
|
return true; |
|
} |
|
|
|
// Do it the hard way: Look up the type name from the refc loader. |
|
// |
|
// Force the refc loader to report and commit to a particular binding for this type name (type.getName()). |
|
// |
|
// In principle, this query might force the loader to load some unrelated class, |
|
// which would cause this query to fail (and the original caller to give up). |
|
// This would be wasted effort, but it is expected to be very rare, occurring |
|
// only when an attacker is attempting to create a type alias. |
|
// In the normal case, one class loader will simply delegate to the other, |
|
// and the same type will be visible through both, with no extra loading. |
|
// |
|
// It is important to go through Class.forName instead of ClassLoader.loadClass |
|
// because Class.forName goes through the JVM system dictionary, which records |
|
// the class lookup once for all. This means that even if a not-well-behaved class loader |
|
// would "change its mind" about the meaning of the name, the Class.forName request |
|
// will use the result cached in the JVM system dictionary. Note that the JVM system dictionary |
|
// will record the first successful result. Unsuccessful results are not stored. |
|
// |
|
// We use doPrivileged in order to allow an unprivileged caller to ask an arbitrary |
|
// class loader about the binding of the proposed name (type.getName()). |
|
// The looked up type ("res") is compared for equality against the proposed |
|
// type ("type") and then is discarded. Thus, the worst that can happen to |
|
// the "child" class loader is that it is bothered to load and report a class |
|
// that differs from "type"; this happens once due to JVM system dictionary |
|
// memoization. And the caller never gets to look at the alternate type binding |
|
|
|
final String name = type.getName(); |
|
Class<?> res = java.security.AccessController.doPrivileged( |
|
new java.security.PrivilegedAction<Class>() { |
|
public Class<?> run() { |
|
try { |
|
return Class.forName(name, false, refcLoader); |
|
} catch (ClassNotFoundException | LinkageError e) { |
|
return null; |
|
} |
|
} |
|
}); |
|
return (type == res); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static boolean isTypeVisible(java.lang.invoke.MethodType type, Class<?> refc) { |
|
for (int n = -1, max = type.parameterCount(); n < max; n++) { |
|
Class<?> ptype = (n < 0 ? type.returnType() : type.parameterType(n)); |
|
if (!isTypeVisible(ptype, refc)) |
|
return false; |
|
} |
|
return true; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static boolean isSamePackage(Class<?> class1, Class<?> class2) { |
|
assert(!class1.isArray() && !class2.isArray()); |
|
if (class1 == class2) |
|
return true; |
|
if (class1.getClassLoader() != class2.getClassLoader()) |
|
return false; |
|
String name1 = class1.getName(), name2 = class2.getName(); |
|
int dot = name1.lastIndexOf('.'); |
|
if (dot != name2.lastIndexOf('.')) |
|
return false; |
|
for (int i = 0; i < dot; i++) { |
|
if (name1.charAt(i) != name2.charAt(i)) |
|
return false; |
|
} |
|
return true; |
|
} |
|
|
|
|
|
*/ |
|
public static String getPackageName(Class<?> cls) { |
|
assert(!cls.isArray()); |
|
String name = cls.getName(); |
|
int dot = name.lastIndexOf('.'); |
|
if (dot < 0) return ""; |
|
return name.substring(0, dot); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static boolean isSamePackageMember(Class<?> class1, Class<?> class2) { |
|
if (class1 == class2) |
|
return true; |
|
if (!isSamePackage(class1, class2)) |
|
return false; |
|
if (getOutermostEnclosingClass(class1) != getOutermostEnclosingClass(class2)) |
|
return false; |
|
return true; |
|
} |
|
|
|
private static Class<?> getOutermostEnclosingClass(Class<?> c) { |
|
Class<?> pkgmem = c; |
|
for (Class<?> enc = c; (enc = enc.getEnclosingClass()) != null; ) |
|
pkgmem = enc; |
|
return pkgmem; |
|
} |
|
|
|
private static boolean loadersAreRelated(ClassLoader loader1, ClassLoader loader2, |
|
boolean loader1MustBeParent) { |
|
if (loader1 == loader2 || loader1 == null |
|
|| (loader2 == null && !loader1MustBeParent)) { |
|
return true; |
|
} |
|
for (ClassLoader scan2 = loader2; |
|
scan2 != null; scan2 = scan2.getParent()) { |
|
if (scan2 == loader1) return true; |
|
} |
|
if (loader1MustBeParent) return false; |
|
|
|
for (ClassLoader scan1 = loader1; |
|
scan1 != null; scan1 = scan1.getParent()) { |
|
if (scan1 == loader2) return true; |
|
} |
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static boolean classLoaderIsAncestor(Class<?> parentClass, Class<?> childClass) { |
|
return loadersAreRelated(parentClass.getClassLoader(), childClass.getClassLoader(), true); |
|
} |
|
} |