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

Do not disable the security manager on Java 17 VMs and newer as it is deprecated for removal. #2123

Merged
merged 4 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Currently the versioning policy of this project follows [Semantic Versioning v2.
### Fixed
- Bumped gson from 2.9.0 to 2.9.1 ([#2136](https://github.com/spotbugs/spotbugs/pull/2136))
- Fixed InvalidInputException in Eclipse while bug reporting ([#2134](https://github.com/spotbugs/spotbugs/issues/2134))
- Avoid warning on use of security manager on Java 17 and newer. ([#1579](https://github.com/spotbugs/spotbugs/issues/1579))

## 4.7.1 - 2022-06-26
### Fixed
Expand Down
3 changes: 2 additions & 1 deletion spotbugs/src/main/java/edu/umd/cs/findbugs/PluginLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import javax.annotation.Nullable;
import javax.annotation.WillClose;

import edu.umd.cs.findbugs.util.SecurityManagerHandler;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
Expand Down Expand Up @@ -1472,7 +1473,7 @@ static synchronized void loadInitialPlugins() {
// Thread.currentThread().getContextClassLoader().getResource("my.java.policy");
// Policy.getPolicy().refresh();
try {
System.setSecurityManager(null);
SecurityManagerHandler.disableSecurityManager();
} catch (Throwable e) {
assert true; // keep going
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package edu.umd.cs.findbugs.util;

import org.apache.commons.lang3.JavaVersion;
import org.apache.commons.lang3.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Since Java 17, the security manager is deprecated for removal and invoking related methods
* causes a warning to be printed to the console. This intermediate disables the use of
* security manager-related APIs on Java 17 or later, unless using the security manager is
* explicitly configured by setting the <i>edu.umd.cs.findbugs.securityManagerDisabled</i>
* property.
*/
public class SecurityManagerHandler {

private static final Logger LOGGER = LoggerFactory.getLogger(SecurityManagerHandler.class);

/**
* Determines if the security manager is used by SpotBugs.
*/
public static boolean SECURITY_MANAGER_DISABLED;

static {
boolean securityManagerDisabled;
try {
String property = System.getProperty("edu.umd.cs.findbugs.securityManagerDisabled");
if (property != null) {
securityManagerDisabled = Boolean.parseBoolean(property);
} else {
securityManagerDisabled = SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_17);
}
} catch (Throwable t) {
securityManagerDisabled = false;
KengoTODA marked this conversation as resolved.
Show resolved Hide resolved
LOGGER.debug("failed to detect the ability of security manager feature, so treat it as available", t);
}
SECURITY_MANAGER_DISABLED = securityManagerDisabled;
Copy link
Member

Choose a reason for hiding this comment

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

SpotBugs reports a potential bug here, please consider to make this static field final.

edu.umd.cs.findbugs.util.SecurityManagerHandler.SECURITY_MANAGER_DISABLED isn't final but should be
This static field public but not final, and could be changed by malicious code or by accident from another package. The field could be made final to avoid this vulnerability.

}

/**
* Disables the security manager by setting {@link System#setSecurityManager(SecurityManager)}
* to {@code null}.
*/
public static void disableSecurityManager() {
if (SECURITY_MANAGER_DISABLED) {
return;
}
doDisableSecurityManager();
}

/**
* This method is a safeguard for running this library on a JVM that might no longer include
* the security manager API after removal. As the JVM verifies methods lazily, and since this
* method will never be invoked, validation of this method with a missing type can never fail.
*
* As some environments do not support setting the security manager but always return null
* when getting it, we check if a security manager is set before disabling it.
*/
private static void doDisableSecurityManager() {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
System.setSecurityManager(null);
}
}
}