Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JDK24: Permanently Disable the Security Manager #20625

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ K0A02="Bootstrap method returned null."
K0B00="The Security Manager is deprecated and will be removed in a future release"
K0B01="Library name must not contain a file path: {0}"
K0B02="Enabling a SecurityManager currently unsupported when -XX:+EnableCRIUSupport is specified"
K0B03="Setting a Security Manager is not supported"
K0B04="A command line option has attempted to allow or enable the Security Manager. Enabling a Security Manager is not supported."

#java.lang.Throwable
K0C00="Non-standard List class not permitted in suppressedExceptions serial stream"
Expand Down
9 changes: 8 additions & 1 deletion jcl/src/java.base/share/classes/java/lang/Class.java
Original file line number Diff line number Diff line change
Expand Up @@ -3046,7 +3046,9 @@ public boolean desiredAssertionStatus() {
* array of not more than maxDepth Classes representing the classes of
* running methods on the stack (including native methods). Frames
* representing the VM implementation of java.lang.reflect are not included
* in the list. If stopAtPrivileged is true, the walk will terminate at any
* in the list.
/*[IF JAVA_SPEC_VERSION < 24]
* If stopAtPrivileged is true, the walk will terminate at any
* frame running one of the following methods:
*
* <code><ul>
Expand All @@ -3057,6 +3059,7 @@ public boolean desiredAssertionStatus() {
* </ul></code>
*
* If one of the doPrivileged methods is found, the walk terminate and that frame is NOT included in the returned array.
/*[ENDIF] JAVA_SPEC_VERSION < 24
*
* Notes: <ul>
* <li> This method operates on the defining classes of methods on stack.
Expand All @@ -3067,7 +3070,11 @@ public boolean desiredAssertionStatus() {
*</ul>
*
* @param maxDepth maximum depth to walk the stack, -1 for the entire stack
/*[IF JAVA_SPEC_VERSION >= 24]
* @param stopAtPrivileged has no effect
/*[ELSE] JAVA_SPEC_VERSION >= 24
* @param stopAtPrivileged stop at privileged classes
/*[ENDIF] JAVA_SPEC_VERSION >= 24
* @return the array of the most recent classes on the stack
*/
@CallerSensitive
Expand Down
26 changes: 26 additions & 0 deletions jcl/src/java.base/share/classes/java/lang/System.java
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,9 @@ static void checkTmpDir() {

/*[IF JAVA_SPEC_VERSION >= 9]*/
static void initSecurityManager(ClassLoader applicationClassLoader) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initSecurityManager() can be removed at

System.initSecurityManager(applicationClassLoader);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still needed since initSecurityManager is used to detect settings of the java.security.manager property.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initSecurityManager() reads the system property java.security.manager, and sets throwUOEFromSetSM which can be skipped within setSecurityManager().
System.initSecurityManager(applicationClassLoader) seems not needed for JDK24+.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will still be needed to throw an exception on startup for illegal java.security.manager manager settings triggered by throwErrorOnInit .

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static void initSecurityManager(ClassLoader applicationClassLoader) {
static void initSecurityManager(ClassLoader applicationClassLoader) {
String javaSecurityManager = internalGetProperties().getProperty("java.security.manager"); //$NON-NLS-1$
if (null == javaSecurityManager) {
/*[IF JAVA_SPEC_VERSION >= 18]*/
throwUOEFromSetSM = true;
/*[ELSE] JAVA_SPEC_VERSION >= 18 */
/* Do nothing. */
/*[ENDIF] JAVA_SPEC_VERSION >= 18 */
} else if ("disallow".equals(javaSecurityManager)) { //$NON-NLS-1$
/*[IF JAVA_SPEC_VERSION > 11]*/
throwUOEFromSetSM = true;
/*[ELSE] JAVA_SPEC_VERSION > 11 */
/* Do nothing. */
/*[ENDIF] JAVA_SPEC_VERSION > 11 */
} else {
/*[IF JAVA_SPEC_VERSION >= 24]*/
/*[MSG "K0B04", "A command line option has attempted to allow or enable the Security Manager. Enabling a Security Manager is not supported."]*/
throw new Error(Msg.getString("K0B04")); //$NON-NLS-1$
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
if ("allow".equals(javaSecurityManager)) { //$NON-NLS-1$
/* Do nothing. */
} else {
/*[IF JAVA_SPEC_VERSION >= 17]*/
initialErr.println("WARNING: A command line option has enabled the Security Manager"); //$NON-NLS-1$
initialErr.println("WARNING: The Security Manager is deprecated and will be removed in a future release"); //$NON-NLS-1$
/*[ENDIF] JAVA_SPEC_VERSION >= 17 */
if (javaSecurityManager.isEmpty() || "default".equals(javaSecurityManager)) { //$NON-NLS-1$
setSecurityManager(new SecurityManager());
} else {
try {
Constructor<?> constructor = Class.forName(javaSecurityManager, true, applicationClassLoader).getConstructor();
constructor.setAccessible(true);
setSecurityManager((SecurityManager)constructor.newInstance());
} catch (Throwable e) {
/*[MSG "K0631", "JVM can't set custom SecurityManager due to {0}"]*/
throw new Error(Msg.getString("K0631", e.toString()), e); //$NON-NLS-1$
}
}
}
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
}
}

/*[IF JAVA_SPEC_VERSION >= 24]*/
boolean throwErrorOnInit = false;
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
String javaSecurityManager = internalGetProperties().getProperty("java.security.manager"); //$NON-NLS-1$
if (null == javaSecurityManager) {
/*[IF JAVA_SPEC_VERSION >= 18]*/
Expand All @@ -1273,14 +1276,21 @@ static void initSecurityManager(ClassLoader applicationClassLoader) {
/* Do nothing. */
/*[ENDIF] JAVA_SPEC_VERSION >= 18 */
} else if ("allow".equals(javaSecurityManager)) { //$NON-NLS-1$
/*[IF JAVA_SPEC_VERSION >= 24]*/
throwErrorOnInit = true;
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
/* Do nothing. */
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
} else if ("disallow".equals(javaSecurityManager)) { //$NON-NLS-1$
/*[IF JAVA_SPEC_VERSION > 11]*/
throwUOEFromSetSM = true;
/*[ELSE] JAVA_SPEC_VERSION > 11 */
/* Do nothing. */
/*[ENDIF] JAVA_SPEC_VERSION > 11 */
} else {
/*[IF JAVA_SPEC_VERSION >= 24]*/
throwErrorOnInit = true;
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
/*[IF JAVA_SPEC_VERSION >= 17]*/
initialErr.println("WARNING: A command line option has enabled the Security Manager"); //$NON-NLS-1$
initialErr.println("WARNING: The Security Manager is deprecated and will be removed in a future release"); //$NON-NLS-1$
theresa-m marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -1297,7 +1307,14 @@ static void initSecurityManager(ClassLoader applicationClassLoader) {
throw new Error(Msg.getString("K0631", e.toString()), e); //$NON-NLS-1$
}
}
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
}
/*[IF JAVA_SPEC_VERSION >= 24]*/
if (throwErrorOnInit) {
/*[MSG "K0B04", "A command line option has attempted to allow or enable the Security Manager. Enabling a Security Manager is not supported."]*/
throw new Error(Msg.getString("K0B04")); //$NON-NLS-1$
}
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think throwErrorOnInit isn't needed, the Error can be moved around Line 1278 whenever java.security.manager is detected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I liked the variable since the -Djava.security.manager=disallow case should not throw an exception.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message should be prefixed with "Error: "; see SecurityManagerWarnings.java.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is from java.lang.Error. The example output from https://openjdk.org/jeps/486 is:

java.lang.Error: A command line option has attempted to allow or enable the Security Manager. Enabling a Security Manager is not supported.
        at java.lang.System.initPhase3(java.base@24/System.java:2067)

}
/*[ENDIF] JAVA_SPEC_VERSION >= 9 */

Expand All @@ -1315,17 +1332,25 @@ static boolean allowSecurityManager() {
*
* @param s the new security manager
*
/*[IF JAVA_SPEC_VERSION > 24]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/*[IF JAVA_SPEC_VERSION > 24]
/*[IF JAVA_SPEC_VERSION >= 24]

* @throws UnsupportedOperationException always
/*[ELSE] JAVA_SPEC_VERSION > 24
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/*[ELSE] JAVA_SPEC_VERSION > 24
/*[ELSE] JAVA_SPEC_VERSION >= 24

* @throws SecurityException if the security manager has already been set and its checkPermission method doesn't allow it to be replaced.
/*[IF JAVA_SPEC_VERSION > 11]
* @throws UnsupportedOperationException if s is non-null and a special token "disallow" has been set for system property "java.security.manager"
* which indicates that a security manager is not allowed to be set dynamically.
/*[ENDIF] JAVA_SPEC_VERSION > 11
/*[ENDIF] JAVA_SPEC_VERSION > 24
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/*[ENDIF] JAVA_SPEC_VERSION > 24
/*[ENDIF] JAVA_SPEC_VERSION >= 24

*/
/*[IF JAVA_SPEC_VERSION >= 17]*/
@Deprecated(since="17", forRemoval=true)
@CallerSensitive
/*[ENDIF] JAVA_SPEC_VERSION >= 17 */
public static void setSecurityManager(final SecurityManager s) {
/*[IF JAVA_SPEC_VERSION >= 24]*/
/*[MSG "K0B03", "Setting a Security Manager is not supported"]*/
throw new UnsupportedOperationException(Msg.getString("K0B03")); //$NON-NLS-1$
/*[ELSE] JAVA_SPEC_VERSION >= 24*/
/*[IF CRIU_SUPPORT]*/
if (openj9.internal.criu.InternalCRIUSupport.isCRIUSupportEnabled()) {
/*[MSG "K0B02", "Enabling a SecurityManager currently unsupported when -XX:+EnableCRIUSupport is specified"]*/
Expand Down Expand Up @@ -1403,6 +1428,7 @@ public Void run() {
currentSecurity.checkPermission(com.ibm.oti.util.RuntimePermissions.permissionSetSecurityManager);
}
security = s;
/*[ENDIF] JAVA_SPEC_VERSION >= 24*/
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,13 @@ private boolean debugHelper(Permission perm) {
}

/**
/*[IF JAVA_SPEC_VERSION >= 24]
* Throws java.security.AccessControlException
*
* @param perm is ignored
* @exception java.security.AccessControlException
* is always thrown
/*[ELSE] JAVA_SPEC_VERSION >= 24
* Checks if the permission <code>perm</code> is allowed in this context.
* All ProtectionDomains must grant the permission for it to be granted.
*
Expand All @@ -731,6 +738,7 @@ private boolean debugHelper(Permission perm) {
* thrown when perm is not granted.
* @exception NullPointerException
* if perm is null
/*[ENDIF] JAVA_SPEC_VERSION >= 24
*/
public void checkPermission(Permission perm) throws AccessControlException {
/*[IF JAVA_SPEC_VERSION >= 24]*/
Expand Down
68 changes: 54 additions & 14 deletions jcl/src/java.base/share/classes/java/security/AccessController.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
package java.security;

import com.ibm.oti.util.Msg;
/*[IF JAVA_SPEC_VERSION < 24]*/
import sun.security.util.SecurityConstants;
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

/*[IF JAVA_SPEC_VERSION >= 9]
import jdk.internal.reflect.CallerSensitive;
Expand All @@ -44,30 +46,30 @@
@SuppressWarnings("removal")
/*[ENDIF] JAVA_SPEC_VERSION >= 17 */
public final class AccessController {
/*[IF JAVA_SPEC_VERSION >= 24]*/
private static AccessControlContext ACC_NO_PERM = new AccessControlContext(
new ProtectionDomain[] { new ProtectionDomain(null, null) });
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
static {
// Initialize vm-internal caches
initializeInternal();
}

/*[IF JAVA_SPEC_VERSION >= 24]*/
private static AccessControlContext ACC_NO_PERM = new AccessControlContext(
new ProtectionDomain[] { new ProtectionDomain(null, null) });
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */

static final int OBJS_INDEX_ACC = 0;
static final int OBJS_INDEX_PDS = 1;
static final int OBJS_ARRAY_SIZE = 3;
static final int OBJS_INDEX_PERMS_OR_CACHECHECKED = 2;

private static native void initializeInternal();
private static native void initializeInternal();

/* [PR CMVC 188787] Enabling -Djava.security.debug option within WAS keeps JVM busy */
static final class DebugRecursionDetection {
private static ThreadLocal<String> tlDebug = new ThreadLocal<>();
static ThreadLocal<String> getTlDebug() {
return tlDebug;
/* [PR CMVC 188787] Enabling -Djava.security.debug option within WAS keeps JVM busy */
static final class DebugRecursionDetection {
private static ThreadLocal<String> tlDebug = new ThreadLocal<>();
static ThreadLocal<String> getTlDebug() {
return tlDebug;
}
}
}
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */

/*[PR 1FDIC6B] J9JCL:WIN95 - AccessController missing private no-arg constructor */
/**
Expand All @@ -77,6 +79,7 @@ private AccessController() {
super();
}

/*[IF JAVA_SPEC_VERSION < 24]*/
/**
* The object array returned has following format:
*
Expand Down Expand Up @@ -192,7 +195,6 @@ private static void throwACE(boolean debug, Permission perm, ProtectionDomain pD
}
}

/*[IF JAVA_SPEC_VERSION < 24]*/
/**
* Helper method to check whether the running program is allowed to access the resource
* being guarded by the given Permission argument
Expand Down Expand Up @@ -275,7 +277,6 @@ private static boolean checkPermissionHelper(Permission perm, AccessControlConte
}
return limitedPermImplied;
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

/**
* Helper to print debug stack information for checkPermission().
Expand Down Expand Up @@ -365,15 +366,23 @@ private static boolean debugHelperJEP140(Object[] objects, Permission perm) {
debugPrintStack(debug, perm);
return debug;
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

/**
/*[IF JAVA_SPEC_VERSION >= 24]
* Throws AccessControlException
*
* @param perm is ignored
* @exception AccessControlException is always thrown
/*[ELSE] JAVA_SPEC_VERSION >= 24
* Checks whether the running program is allowed to
* access the resource being guarded by the given
* Permission argument.
*
* @param perm the permission to check
* @exception AccessControlException if access is not allowed.
* NullPointerException if perm is null
/*[ENDIF] JAVA_SPEC_VERSION >= 24
*/
public static void checkPermission(Permission perm) throws AccessControlException {
/*[IF JAVA_SPEC_VERSION >= 24]*/
Expand Down Expand Up @@ -455,6 +464,9 @@ private static void keepalive(Permission... perms) {
}

/**
/*[IF JAVA_SPEC_VERSION >= 24]
* @return an AccessControlContext with no permissions
/*[ELSE] JAVA_SPEC_VERSION >= 24
* Answers the access controller context of the current thread,
* including the inherited ones. It basically retrieves all the
* protection domains from the calling stack and creates an
Expand All @@ -463,6 +475,7 @@ private static void keepalive(Permission... perms) {
* @return an AccessControlContext which captures the current state
*
* @see AccessControlContext
/*[ENDIF] JAVA_SPEC_VERSION >= 24
*/
public static AccessControlContext getContext() {
/*[IF JAVA_SPEC_VERSION >= 24]*/
Expand All @@ -472,6 +485,7 @@ public static AccessControlContext getContext() {
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
}

/*[IF JAVA_SPEC_VERSION < 24]*/
/**
* This is a helper method for getContext() and doPrivilegedWithCombiner methods.
* Answers the access controller context of the current thread including the inherited ones.
Expand Down Expand Up @@ -637,6 +651,7 @@ private static int getNewAuthorizedState(AccessControlContext acc, ProtectionDom
}
return newAuthorizedState;
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be moved to L722 to include toArrayOfProtectionDomains().


/**
* Helper method to combine the ProtectionDomain objects
Expand Down Expand Up @@ -849,7 +864,11 @@ public static <T> T doPrivileged (PrivilegedExceptionAction<T> action, AccessCon
*/
@CallerSensitive
public static <T> T doPrivilegedWithCombiner(PrivilegedAction<T> action) {
/*[IF JAVA_SPEC_VERSION >= 24]*/
return doPrivileged(action, null);
/*[ELSE] JAVA_SPEC_VERSION >= 24*/
return doPrivileged(action, doPrivilegedWithCombinerHelper(null));
/*[ENDIF] JAVA_SPEC_VERSION >= 24*/
}

/**
Expand All @@ -876,9 +895,14 @@ public static <T> T doPrivilegedWithCombiner(PrivilegedAction<T> action) {
public static <T> T doPrivilegedWithCombiner(PrivilegedExceptionAction<T> action)
throws PrivilegedActionException
{
/*[IF JAVA_SPEC_VERSION >= 24]*/
return doPrivileged(action, null);
/*[ELSE] JAVA_SPEC_VERSION >= 24*/
return doPrivileged(action, doPrivilegedWithCombinerHelper(null));
/*[ENDIF] JAVA_SPEC_VERSION >= 24*/
}

/*[IF JAVA_SPEC_VERSION < 24]*/
/**
* Helper method to check if any permission is null
*
Expand All @@ -894,6 +918,7 @@ private static void checkPermsNPE(Permission... perms) {
}
}
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

/**
* Performs the privileged action specified by <code>action</code>.
Expand Down Expand Up @@ -922,7 +947,9 @@ private static void checkPermsNPE(Permission... perms) {
public static <T> T doPrivileged(PrivilegedAction<T> action,
AccessControlContext context, Permission... perms)
{
/*[IF JAVA_SPEC_VERSION < 24]*/
checkPermsNPE(perms);
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keepalive() can be removed for JDK24+.

T result = action.run();
keepalive(context);
keepalive(perms);
Expand Down Expand Up @@ -954,8 +981,13 @@ public static <T> T doPrivileged(PrivilegedAction<T> action,
public static <T> T doPrivilegedWithCombiner(PrivilegedAction<T> action,
AccessControlContext context, Permission... perms)
{
/*[IF JAVA_SPEC_VERSION >= 24]*/
return doPrivileged(action, context, perms); // 24 - perms?
/*[ELSE] JAVA_SPEC_VERSION >= 24*/
checkPermsNPE(perms);
return doPrivileged(action, doPrivilegedWithCombinerHelper(context), perms);
/*[ENDIF] JAVA_SPEC_VERSION >= 24*/

}

/**
Expand Down Expand Up @@ -989,7 +1021,9 @@ public static <T> T doPrivileged(PrivilegedExceptionAction<T> action,
throws PrivilegedActionException
{
try {
/*[IF JAVA_SPEC_VERSION < 24]*/
checkPermsNPE(perms);
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
T result = action.run();
keepalive(context);
keepalive(perms);
Expand Down Expand Up @@ -1029,10 +1063,15 @@ public static <T> T doPrivilegedWithCombiner(PrivilegedExceptionAction<T> action
AccessControlContext context, Permission... perms)
throws PrivilegedActionException
{
/*[IF JAVA_SPEC_VERSION >= 24]*/
return doPrivileged(action, context, perms);
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
checkPermsNPE(perms);
return doPrivileged(action, doPrivilegedWithCombinerHelper(context), perms);
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
}

/*[IF JAVA_SPEC_VERSION < 24]*/
/**
* Helper method to construct an AccessControlContext for doPrivilegedWithCombiner methods.
*
Expand All @@ -1052,5 +1091,6 @@ private static AccessControlContext doPrivilegedWithCombinerHelper(AccessControl
}
return fixedContext;
}
/*[ENDIF] JAVA_SPEC_VERSION < 24*/

}
2 changes: 2 additions & 0 deletions runtime/jcl/common/acccont.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "j9.h"
#include "j9port.h"

#if JAVA_SPEC_VERSION < 24
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file can be removed for JDK24+.

jboolean JNICALL Java_java_security_AccessController_initializeInternal(JNIEnv *env, jclass thisClz)
{
J9JavaVM *javaVM = ((J9VMThread *) env)->javaVM;
Expand Down Expand Up @@ -63,3 +64,4 @@ jboolean JNICALL Java_java_security_AccessController_initializeInternal(JNIEnv *
fail:
return JNI_FALSE;
}
#endif /* JAVA_SPEC_VERSION < 24 */
Loading