Skip to content

Commit 5b55ebb

Browse files
committed
setSecurity manager now throws a SecurityException
1 parent 5a13ce9 commit 5b55ebb

File tree

4 files changed

+26
-44
lines changed

4 files changed

+26
-44
lines changed

docs/reference-manual/native-image/Compatibility.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,16 @@ The `invokedynamic`method and method handles can introduce calls at run time or
4141

4242
Note that `invokedynamic` use cases generated by `javac` for, for example, Java lambda expressions and String concatenation that are supported because they do not change called methods at run time.
4343

44-
### Security Manager
45-
46-
Native Image will produce images as if `-Djava.security.manager` was set to `disallow`.
47-
At image run time, calls to `java.lang.System#setSecurityManager` exit the program with error code `255` if `-Djava.security.manager` is set to anything but `disallow` at program startup.
48-
4944
## Features That May Operate Differently in a Native Image
5045

5146
Native Image implements some Java features differently to the Java VM.
5247

48+
### Security Manager
49+
50+
`java.lang.System#getSecurityManager()` always returns `null` even if the security manager is set via `-Djava.security.manager` at startup.
51+
52+
`java.lang.System#setSecurityManager(SecurityManager)` invoked with a non-null argument throws a `java.lang.SecurityException` if `-Djava.security.manager` is set to anything but `disallow` at program startup.
53+
5354
### Signal Handlers
5455

5556
Registering a signal handler requires a new thread to start that handles the signal and invokes shutdown hooks.

docs/security/native-image.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ The security report section of the native image [build output](../reference-manu
9393

9494
## Miscellaneous
9595

96+
Setting the security manager is not allowed. For more information see the [compatibility documentation](../reference-manual/native-image/Compatibility.md#security-manager).
97+
9698
Native Image provides multiple ways to specify a certificate file used to define the default TrustStore.
9799
While the default behavior for `native-image` is to capture and use the default TrustStore from the build-time host environment, this can be changed at run time by setting the "javax.net.ssl.trustStore\*" system properties.
98100
See the [documentation](../reference-manual/native-image/CertificateManagement.md) for more details.

substratevm/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This changelog summarizes major changes to GraalVM Native Image.
1414
* (GR-47937) Make the lambda-class name format in Native-Image consistent with the JDK name format.
1515
* (GR-45651) Methods, fields and constructors of `Object`, primitive classes and array classes are now registered by default for reflection.
1616
* (GR-45651) The Native Image agent now tracks calls to `ClassLoader.findSystemClass`, `ObjectInputStream.resolveClass` and `Bundles.of`, and registers resource bundles as bundle name-locale pairs.
17-
* (GR-49807) Before this change the function `System#setSecurityManager` was always halting program execution with a VM error. This was inconvenient as the VM error prints an uncomprehensible error message and prevents further continuation of the program. For cases where the program is expected to throw an exception when `System#setSecurityManager` is called, execution on Native Image was not possible. Now, `System#setSecurityManager` throws an `java.lang.UnsupportedOperationException` by default. If the property `java.security.manager` is set to `allow` the program will print a user-readable stack trace and exit with code `99`. Value of `java.security.manager` that would be set at build time is completely ignored at run time.
17+
* (GR-49807) Before this change the function `System#setSecurityManager` was always halting program execution with a VM error. This was inconvenient as the VM error prints an uncomprehensible error message and prevents further continuation of the program. For cases where the program is expected to throw an exception when `System#setSecurityManager` is called, execution on Native Image was not possible. Now, `System#setSecurityManager` throws an `java.lang.UnsupportedOperationException` by default. If the property `java.security.manager` is set to anything but `disallow` at program startup this function will throw a `java.lang.SecurityException` according to the Java spec.
1818

1919
## GraalVM for JDK 21 (Internal Version 23.1.0)
2020
* (GR-35746) Lower the default aligned chunk size from 1 MB to 512 KB for the serial and epsilon GCs, reducing memory usage and image size in many cases.

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/JavaLangSubstitutions.java

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import java.util.stream.Stream;
4545

4646
import org.graalvm.nativeimage.ImageSingletons;
47-
import org.graalvm.nativeimage.LogHandler;
4847
import org.graalvm.nativeimage.Platform;
4948
import org.graalvm.nativeimage.Platforms;
5049
import org.graalvm.nativeimage.hosted.FieldValueTransformer;
@@ -67,14 +66,12 @@
6766
import com.oracle.svm.core.hub.DynamicHub;
6867
import com.oracle.svm.core.jdk.JavaLangSubstitutions.ClassValueSupport;
6968
import com.oracle.svm.core.monitor.MonitorSupport;
70-
import com.oracle.svm.core.option.HostedOptionKey;
7169
import com.oracle.svm.core.snippets.SubstrateForeignCallTarget;
7270
import com.oracle.svm.core.thread.JavaThreads;
7371
import com.oracle.svm.core.thread.VMOperation;
7472
import com.oracle.svm.core.util.VMError;
7573
import com.oracle.svm.util.ReflectionUtil;
7674

77-
import jdk.graal.compiler.options.Option;
7875
import jdk.graal.compiler.replacements.nodes.BinaryMathIntrinsicNode;
7976
import jdk.graal.compiler.replacements.nodes.BinaryMathIntrinsicNode.BinaryOperation;
8077
import jdk.graal.compiler.replacements.nodes.UnaryMathIntrinsicNode;
@@ -413,32 +410,26 @@ private static String getProperty(String key, String def) {
413410
@Alias @RecomputeFieldValue(kind = Kind.FromAlias, isFinal = true) //
414411
private static int allowSecurityManager = 1;
415412

413+
/**
414+
* We do not support the {@link SecurityManager} so this method must throw a
415+
* {@link SecurityException} when 'java.security.manager' is set to anything but
416+
* <code>disallow</code>.
417+
*
418+
* @see System#setSecurityManager(SecurityManager)
419+
* @see SecurityManager
420+
*/
416421
@Substitute
417-
@TargetElement(onlyWith = JavaLangSubstitutions.UseSecurityManagerPropertyAtRuntime.class)
418-
private static void setSecurityManager(SecurityManager s) {
419-
/* We read properties interpreted at isolate creation as that is what happens on the JVM */
420-
String smp = SystemPropertiesSupport.singleton().getSavedProperties().get("java.security.manager");
421-
if (smp != null && !smp.equals("disallow")) {
422-
/*
423-
* The strict failure is needed as the security precaution: In case a user does not read
424-
* our documentation, uses this deprecated API marked for removal, and passes
425-
* "-Djava.security.manager=allow" at runtime, and accidentally catches the
426-
* UnsupportedOperationException, we don't want to compromise their security.
427-
*/
428-
System.err.println("""
429-
Fatal error: Property '-Djava.security.manager' is set, but SecurityManager is not supported by Native Image. Please unset this property.
430-
Exiting the program to prevent misinterpretation of the set SecurityManager at:""");
431-
432-
for (var traceElement : new UnsupportedOperationException().getStackTrace()) {
433-
System.err.println("\tat " + traceElement);
422+
private static void setSecurityManager(SecurityManager sm) {
423+
if (sm != null) {
424+
/* Read the property collected at isolate creation as that is what happens on the JVM */
425+
String smp = SystemPropertiesSupport.singleton().getSavedProperties().get("java.security.manager");
426+
if (smp != null && !smp.equals("disallow")) {
427+
throw new SecurityException("Setting the SecurityManager is not supported by Native Image");
428+
} else {
429+
throw new UnsupportedOperationException(
430+
"The Security Manager is deprecated and will be removed in a future release");
434431
}
435-
436-
/* bypasses possible filters on System.exit */
437-
ImageSingletons.lookup(LogHandler.class).fatalError();
438432
}
439-
440-
throw new UnsupportedOperationException(
441-
"The Security Manager is deprecated and will be removed in a future release");
442433
}
443434
}
444435

@@ -686,18 +677,6 @@ public Object transform(Object receiver, Object originalValue) {
686677
/** Dummy class to have a class with the file's name. */
687678
public final class JavaLangSubstitutions {
688679

689-
public static class UseSecurityManagerPropertyAtRuntime implements BooleanSupplier {
690-
public static class Options {
691-
@Option(help = "Used only for testing as exiting the program shadows other working tests, please do not use in production.")//
692-
public static final HostedOptionKey<Boolean> TestingSecurityViolationUseSecurityManagerPropertyAtRuntime = new HostedOptionKey<>(true);
693-
}
694-
695-
@Override
696-
public boolean getAsBoolean() {
697-
return Options.TestingSecurityViolationUseSecurityManagerPropertyAtRuntime.getValue();
698-
}
699-
}
700-
701680
public static final class StringUtil {
702681
/**
703682
* Returns a character from a string at {@code index} position based on the encoding format.

0 commit comments

Comments
 (0)