Skip to content

Commit

Permalink
Remove uses of getSecurityManager from java.lang.System for JDK24+
Browse files Browse the repository at this point in the history
These calls are not needed since System.getSecurityManager
 returns null for JDK 24+.

Signed-off-by: Theresa Mammarella <[email protected]>
  • Loading branch information
theresa-m committed Dec 17, 2024
1 parent efe1c67 commit 5204cb3
Showing 1 changed file with 49 additions and 13 deletions.
62 changes: 49 additions & 13 deletions jcl/src/java.base/share/classes/java/lang/System.java
Original file line number Diff line number Diff line change
Expand Up @@ -596,12 +596,13 @@ static URL codeSource(Class<?> callerClass) {
* @param newIn the new value for in.
*/
public static void setIn(InputStream newIn) {
/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(com.ibm.oti.util.RuntimePermissions.permissionSetIO);
}

/*[ENDIF] JAVA_SPEC_VERSION < 24 */
setFieldImpl("in", newIn); //$NON-NLS-1$
}

Expand All @@ -612,11 +613,13 @@ public static void setIn(InputStream newIn) {
* @param newOut the new value for out.
*/
public static void setOut(java.io.PrintStream newOut) {
/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(com.ibm.oti.util.RuntimePermissions.permissionSetIO);
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
setFieldImpl("out", newOut); //$NON-NLS-1$
}

Expand All @@ -627,12 +630,13 @@ public static void setOut(java.io.PrintStream newOut) {
* @param newErr the new value for err.
*/
public static void setErr(java.io.PrintStream newErr) {
/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(com.ibm.oti.util.RuntimePermissions.permissionSetIO);
}

/*[ENDIF] JAVA_SPEC_VERSION < 24 */
setFieldImpl("err", newErr); //$NON-NLS-1$
}

Expand Down Expand Up @@ -914,11 +918,12 @@ public static void gc() {
@SuppressWarnings("dep-ann")
public static String getenv(String var) {
if (var == null) throw new NullPointerException();
/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null)
security.checkPermission(new RuntimePermission("getenv." + var)); //$NON-NLS-1$

/*[ENDIF] JAVA_SPEC_VERSION < 24 */
return ProcessEnvironment.getenv(var);
}

Expand All @@ -927,19 +932,22 @@ public static String getenv(String var) {
* not a copy, so that changes made to the returned
* Properties object will be reflected in subsequent
* calls to {@code getProperty()} and {@code getProperties()}.
/*[IF JAVA_SPEC_VERSION < 24]
* <p>
* Security managers should restrict access to this
* API if possible.
/*[ENDIF] JAVA_SPEC_VERSION < 24
*
* @return the system properties
*/
public static Properties getProperties() {
if (!propertiesInitialized) throw new Error("bootstrap error, system property access before init"); //$NON-NLS-1$
/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null)
security.checkPropertiesAccess();

/*[ENDIF] JAVA_SPEC_VERSION < 24 */
return systemProperties;
}

Expand Down Expand Up @@ -1000,10 +1008,12 @@ public static String getProperty(String prop) {
public static String getProperty(String prop, String defaultValue) {
if (prop.length() == 0) throw new IllegalArgumentException();

/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null)
security.checkPropertyAccess(prop);
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

if (!propertiesInitialized
&& !prop.equals("com.ibm.IgnoreMalformedInput") //$NON-NLS-1$
Expand Down Expand Up @@ -1041,11 +1051,13 @@ public static String setProperty(String prop, String value) {
/*[PR CMVC 80288] should check for empty key */
if (prop.length() == 0) throw new IllegalArgumentException();

/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null)
security.checkPermission(
new PropertyPermission(prop, "write")); //$NON-NLS-1$
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

return (String)systemProperties.setProperty(prop, value);
}
Expand Down Expand Up @@ -1124,10 +1136,11 @@ public static int identityHashCode(Object anObject) {
* @param pathName the path of the file to be loaded
*
* @throws UnsatisfiedLinkError if the library could not be loaded
* @throws SecurityException if the library was not allowed to be loaded
* @throws NullPointerException if pathName is null
/*[IF JAVA_SPEC_VERSION >= 24]
* @throws IllegalCallerException if the caller belongs to a module where native access is not enabled
/*[ELSE] JAVA_SPEC_VERSION >= 24
* @throws SecurityException if the library was not allowed to be loaded
/*[ENDIF] JAVA_SPEC_VERSION >= 24
*/
@CallerSensitive
Expand All @@ -1138,14 +1151,14 @@ public static void load(String pathName) {
/*[IF JAVA_SPEC_VERSION >= 24]*/
Class<?> caller = Reflection.getCallerClass();
Reflection.ensureNativeAccess(caller, System.class, "load", false);
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */

/*[ELSE] JAVA_SPEC_VERSION >= 24 */
@SuppressWarnings("removal")
SecurityManager smngr = System.getSecurityManager();
if (smngr != null) {
smngr.checkLink(pathName);
}
/*[IF JAVA_SPEC_VERSION >= 15]*/
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
/*[IF JAVA_SPEC_VERSION >= 15]*/
/*[IF PLATFORM-mz31 | PLATFORM-mz64]*/
ClassLoader.loadZOSLibrary(getCallerClass(), pathName);
/*[ELSE] PLATFORM-mz31 | PLATFORM-mz64 */
Expand All @@ -1156,9 +1169,9 @@ public static void load(String pathName) {
}
ClassLoader.loadLibrary(getCallerClass(), fileName);
/*[ENDIF] PLATFORM-mz31 | PLATFORM-mz64 */
/*[ELSE] JAVA_SPEC_VERSION >= 15 */
/*[ELSE] JAVA_SPEC_VERSION >= 15 */
ClassLoader.loadLibraryWithPath(pathName, ClassLoader.callerClassLoader(), null);
/*[ENDIF] JAVA_SPEC_VERSION >= 15 */
/*[ENDIF] JAVA_SPEC_VERSION >= 15 */
}

/**
Expand All @@ -1167,10 +1180,11 @@ public static void load(String pathName) {
* @param libName the name of the library to load
*
* @throws UnsatisfiedLinkError if the library could not be loaded
* @throws SecurityException if the library was not allowed to be loaded
* @throws NullPointerException if libName is null
/*[IF JAVA_SPEC_VERSION >= 24]
* @throws IllegalCallerException if the caller belongs to a module where native access is not enabled
/*[ELSE] JAVA_SPEC_VERSION >= 24
* @throws SecurityException if the library was not allowed to be loaded
/*[ENDIF] JAVA_SPEC_VERSION >= 24
*/
@CallerSensitive
Expand All @@ -1193,12 +1207,13 @@ public static void loadLibrary(String libName) {
throw new UnsatisfiedLinkError(Msg.getString("K0B01", libName)); //$NON-NLS-1$
}
}

/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager smngr = System.getSecurityManager();
if (smngr != null) {
smngr.checkLink(libName);
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
/*[IF JAVA_SPEC_VERSION >= 15]*/
Class<?> callerClass = getCallerClass();
/*[ELSE]*/
Expand Down Expand Up @@ -1254,17 +1269,21 @@ public static void runFinalizersOnExit(boolean flag) {
* Sets the system properties. Note that the object which is passed in
* is not copied, so that subsequent changes made to it will be reflected
* in calls to {@code getProperty()} and {@code getProperties()}.
/*[IF JAVA_SPEC_VERSION < 24]
* <p>
* Security managers should restrict access to this
* API if possible.
/*[ENDIF] JAVA_SPEC_VERSION < 24
*
* @param p the properties to set
*/
public static void setProperties(Properties p) {
/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null)
security.checkPropertiesAccess();
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
if (p == null) {
ensureProperties(false);
} else {
Expand Down Expand Up @@ -1525,10 +1544,12 @@ public static String clearProperty(String prop) {
if (!propertiesInitialized) throw new Error("bootstrap error, system property access before init: " + prop); //$NON-NLS-1$

if (prop.length() == 0) throw new IllegalArgumentException();
/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null)
security.checkPermission(new PropertyPermission(prop, "write")); //$NON-NLS-1$
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
return (String)systemProperties.remove(prop);
}

Expand All @@ -1538,11 +1559,12 @@ public static String clearProperty(String prop) {
* @return an unmodifiable Map containing all of the system environment variables.
*/
public static Map<String, String> getenv() {
/*[IF JAVA_SPEC_VERSION < 24]*/
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null)
security.checkPermission(new RuntimePermission("getenv.*")); //$NON-NLS-1$

/*[ENDIF] JAVA_SPEC_VERSION < 24 */
return ProcessEnvironment.getenv();
}

Expand Down Expand Up @@ -1900,6 +1922,7 @@ public static Logger getLogger(String loggerName, ResourceBundle bundle) {
public abstract static class LoggerFinder {
private static volatile LoggerFinder loggerFinder;

/*[IF JAVA_SPEC_VERSION < 24]*/
/**
* Checks needed runtime permissions
*
Expand All @@ -1908,6 +1931,7 @@ public abstract static class LoggerFinder {
protected LoggerFinder() {
verifyPermissions();
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */

/**
* Returns a localizable instance of Logger for the given module
Expand All @@ -1917,10 +1941,14 @@ protected LoggerFinder() {
* @param callerModule The module for which the logger is being requested
* @return an instance of Logger
* @throws NullPointerException if loggerName or callerModule is null
/*[IF JAVA_SPEC_VERSION < 24]
* @throws SecurityException if RuntimePermission("loggerFinder") is not allowed
/*[ENDIF] JAVA_SPEC_VERSION < 24
*/
public Logger getLocalizedLogger(String loggerName, ResourceBundle bundle, Module callerModule) {
/*[IF JAVA_SPEC_VERSION < 24]*/
verifyPermissions();
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
Objects.requireNonNull(loggerName);
Objects.requireNonNull(callerModule);
Logger logger = this.getLogger(loggerName, callerModule);
Expand All @@ -1935,18 +1963,24 @@ public Logger getLocalizedLogger(String loggerName, ResourceBundle bundle, Modul
* @param callerModule The module for which the logger is being requested
* @return a Logger suitable for use within the given module
* @throws NullPointerException if loggerName or callerModule is null
/*[IF JAVA_SPEC_VERSION < 24]
* @throws SecurityException if RuntimePermission("loggerFinder") is not allowed
/*[ENDIF] JAVA_SPEC_VERSION < 24
*/
public abstract Logger getLogger(String loggerName, Module callerModule);

/**
* Returns the LoggerFinder instance
*
* @return the LoggerFinder instance.
/*[IF JAVA_SPEC_VERSION < 24]
* @throws SecurityException if RuntimePermission("loggerFinder") is not allowed
/*[ENDIF] JAVA_SPEC_VERSION < 24
*/
public static LoggerFinder getLoggerFinder() {
/*[IF JAVA_SPEC_VERSION < 24]*/
verifyPermissions();
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
LoggerFinder localFinder = loggerFinder;
if (localFinder == null) {
localFinder = AccessController.doPrivileged(
Expand All @@ -1963,13 +1997,15 @@ public static LoggerFinder getLoggerFinder() {
return localFinder;
}

/*[IF JAVA_SPEC_VERSION < 24]*/
private static void verifyPermissions() {
@SuppressWarnings("removal")
SecurityManager securityManager = System.getSecurityManager();
if (securityManager != null) {
securityManager.checkPermission(com.ibm.oti.util.RuntimePermissions.permissionLoggerFinder);
}
}
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
}

/**
Expand Down

0 comments on commit 5204cb3

Please sign in to comment.