Skip to content

Commit

Permalink
Remove some permission checks for jdk24+
Browse files Browse the repository at this point in the history
SecurityConstants.GET_CLASSLOADER_PERMISSION removed upstream in
* 8344299: SM cleanup in javax.naming modules

Signed-off-by: Keith W. Campbell <[email protected]>
  • Loading branch information
keithc-ca committed Nov 29, 2024
1 parent 57096c6 commit f3830e8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 31 deletions.
23 changes: 16 additions & 7 deletions jcl/src/java.base/share/classes/java/lang/Class.java
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,9 @@ private static Class<?> forNameHelper(
String className, boolean initializeBoolean, ClassLoader classLoader,
Class<?> caller, boolean isAdapter) throws ClassNotFoundException
{
/*[IF JAVA_SPEC_VERSION >= 24]*/
return forNameImpl(className, initializeBoolean, classLoader);
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
@SuppressWarnings("removal")
SecurityManager sm = null;
if (J9VMInternals.initialized) {
Expand Down Expand Up @@ -590,6 +593,7 @@ private static Class<?> forNameHelper(
J9VMInternals.initialize(c);
}
return c;
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
}
/*[ENDIF] JAVA_SPEC_VERSION >= 18 */

Expand Down Expand Up @@ -678,14 +682,15 @@ private static Class<?> forName(Module module, String name, Class<?> caller)
@CallerSensitive
private static Class<?> forNameHelper(Module module, String name, Class<?> caller, boolean isAdapter)
{
@SuppressWarnings("removal")
SecurityManager sm = null;
ClassLoader classLoader;
Class<?> c;

if ((null == module) || (null == name)) {
throw new NullPointerException();
}
ClassLoader classLoader;
Class<?> c;
/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager sm = null;

if (J9VMInternals.initialized) {
sm = System.getSecurityManager();
}
Expand All @@ -702,7 +707,9 @@ public ClassLoader run() {
return module.getClassLoader();
}
});
} else {
} else
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
{
classLoader = module.getClassLoader();
}

Expand Down Expand Up @@ -799,9 +806,10 @@ public Class<?>[] getClasses() {
@CallerSensitive
public ClassLoader getClassLoader() {
if (null != classLoader) {
if (classLoader == ClassLoader.bootstrapClassLoader) {
if (classLoader == ClassLoader.bootstrapClassLoader) {
return null;
}
/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (null != security) {
Expand All @@ -810,6 +818,7 @@ public ClassLoader getClassLoader() {
security.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
}
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
}
return classLoader;
}
Expand Down
50 changes: 28 additions & 22 deletions jcl/src/java.base/share/classes/java/lang/ClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@ protected final Class<?> findSystemClass (String className) throws ClassNotFound
*/
@CallerSensitive
public final ClassLoader getParent() {
/*[IF JAVA_SPEC_VERSION < 24]*/
if (parent == null) {
return null;
}
Expand All @@ -848,6 +849,7 @@ public final ClassLoader getParent() {
security.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
}
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
return parent;
}

Expand Down Expand Up @@ -1066,15 +1068,17 @@ static ClassLoader getClassLoader(Class<?> clz) {
*/
@CallerSensitive
public static ClassLoader getPlatformClassLoader() {
ClassLoader platformClassLoader = ClassLoaders.platformClassLoader();
/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
ClassLoader platformClassLoader = ClassLoaders.platformClassLoader();
if (security != null) {
ClassLoader callersClassLoader = callerClassLoader();
if (needsClassLoaderPermissionCheck(callersClassLoader, platformClassLoader)) {
security.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
}
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
return platformClassLoader;
}

Expand Down Expand Up @@ -1121,32 +1125,33 @@ public static ClassLoader getSystemClassLoader () {
if (initSystemClassLoader) {
initSystemClassLoader = false;

String userLoader = System.internalGetProperties().getProperty("java.system.class.loader"); //$NON-NLS-1$
if (userLoader != null) {
try {
Class<?> loaderClass = Class.forName(userLoader, true, applicationClassLoader);
Constructor<?> constructor = loaderClass.getConstructor(new Class<?>[]{classLoaderClass});
applicationClassLoader = (ClassLoader)constructor.newInstance(new Object[]{applicationClassLoader});
/*[PR JAZZ103 87642] Setting -Djava.system.class.loader on the command line doesn't update VMAccess.setExtClassLoader() */
/* Find the extension class loader */
ClassLoader tempLoader = applicationClassLoader;
while (tempLoader.parent != null) {
tempLoader = tempLoader.parent;
}
VMAccess.setExtClassLoader(tempLoader);
} catch (Throwable e) {
if (e instanceof InvocationTargetException) {
throw new Error(e.getCause());
} else {
throw new Error(e);
}
String userLoader = System.internalGetProperties().getProperty("java.system.class.loader"); //$NON-NLS-1$
if (userLoader != null) {
try {
Class<?> loaderClass = Class.forName(userLoader, true, applicationClassLoader);
Constructor<?> constructor = loaderClass.getConstructor(new Class<?>[]{classLoaderClass});
applicationClassLoader = (ClassLoader)constructor.newInstance(new Object[]{applicationClassLoader});
/*[PR JAZZ103 87642] Setting -Djava.system.class.loader on the command line doesn't update VMAccess.setExtClassLoader() */
/* Find the extension class loader */
ClassLoader tempLoader = applicationClassLoader;
while (tempLoader.parent != null) {
tempLoader = tempLoader.parent;
}
VMAccess.setExtClassLoader(tempLoader);
} catch (Throwable e) {
if (e instanceof InvocationTargetException) {
throw new Error(e.getCause());
} else {
throw new Error(e);
}
}
}
}
}
}

ClassLoader sysLoader = applicationClassLoader;
/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
Expand All @@ -1155,6 +1160,7 @@ public static ClassLoader getSystemClassLoader () {
security.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
}
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

return sysLoader;
}
Expand Down Expand Up @@ -2578,7 +2584,7 @@ public final boolean isRegisteredAsParallelCapable() {
}
/*[ENDIF] JAVA_SPEC_VERSION >= 9 */

/*[IF JAVA_SPEC_VERSION >= 19]*/
/*[IF (19 <= JAVA_SPEC_VERSION) & (JAVA_SPEC_VERSION < 24)]*/
static void checkClassLoaderPermission(ClassLoader classLoader, Class<?> caller) {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
Expand All @@ -2590,7 +2596,7 @@ static void checkClassLoaderPermission(ClassLoader classLoader, Class<?> caller)
}
}
}
/*[ENDIF] JAVA_SPEC_VERSION >= 19 */
/*[ENDIF] (19 <= JAVA_SPEC_VERSION) & (JAVA_SPEC_VERSION < 24) */

/*[IF JAVA_SPEC_VERSION >= 24]*/
static NativeLibraries nativeLibrariesFor(ClassLoader loader) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,13 @@ static final ArrayList<Class<?>> parseIntoClasses(String methodDescriptor, Class
static MethodType fromMethodDescriptorStringInternal(String methodDescriptor, ClassLoader loader) {
ClassLoader classLoader = loader;
if (classLoader == null) {
/*[IF JAVA_SPEC_VERSION >= 14]*/
/*[IF (14 <= JAVA_SPEC_VERSION) & (JAVA_SPEC_VERSION < 24)]*/
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(sun.security.util.SecurityConstants.GET_CLASSLOADER_PERMISSION);
}
/*[ENDIF] JAVA_SPEC_VERSION >= 14 */
/*[ENDIF] (14 <= JAVA_SPEC_VERSION) & (JAVA_SPEC_VERSION < 24) */
classLoader = ClassLoader.getSystemClassLoader();
}

Expand Down

0 comments on commit f3830e8

Please sign in to comment.