Skip to content

Commit b408a82

Browse files
minborgJornVernee
andcommittedSep 7, 2023
8314260: Unable to load system libraries on Windows when using a SecurityManager
Co-authored-by: Jorn Vernee <jvernee@openjdk.org> Reviewed-by: jvernee
1 parent 726c9c9 commit b408a82

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed
 

‎src/java.base/share/classes/jdk/internal/foreign/SystemLookup.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.lang.invoke.MethodHandles;
3030
import java.nio.file.Files;
3131
import java.nio.file.Path;
32+
import java.security.AccessController;
33+
import java.security.PrivilegedAction;
3234
import java.util.Objects;
3335
import java.util.Optional;
3436
import java.util.function.Function;
@@ -72,11 +74,24 @@ private static SymbolLookup makeSystemLookup() {
7274
}
7375

7476
private static SymbolLookup makeWindowsLookup() {
75-
Path system32 = Path.of(System.getenv("SystemRoot"), "System32");
77+
@SuppressWarnings("removal")
78+
String systemRoot = AccessController.doPrivileged(new PrivilegedAction<String>() {
79+
@Override
80+
public String run() {
81+
return System.getenv("SystemRoot");
82+
}
83+
});
84+
Path system32 = Path.of(systemRoot, "System32");
7685
Path ucrtbase = system32.resolve("ucrtbase.dll");
7786
Path msvcrt = system32.resolve("msvcrt.dll");
7887

79-
boolean useUCRT = Files.exists(ucrtbase);
88+
@SuppressWarnings("removal")
89+
boolean useUCRT = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
90+
@Override
91+
public Boolean run() {
92+
return Files.exists(ucrtbase);
93+
}
94+
});
8095
Path stdLib = useUCRT ? ucrtbase : msvcrt;
8196
SymbolLookup lookup = libLookup(libs -> libs.load(stdLib));
8297

‎test/jdk/java/foreign/TestLinker.java

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
* @requires jdk.foreign.linker != "UNSUPPORTED"
2828
* @modules java.base/jdk.internal.foreign
2929
* @run testng TestLinker
30+
* @run testng/othervm/policy=security.policy
31+
* -Djava.security.manager=default TestLinker
3032
*/
3133

3234
import jdk.internal.foreign.CABI;

‎test/jdk/java/foreign/security.policy

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
grant codeBase "file:${test.classes}/*" {
2+
// Permissions needed to run the test
3+
permission java.util.PropertyPermission "os.name", "read";
4+
permission java.util.PropertyPermission "NativeTestHelper.DEFAULT_RANDOM.seed", "read";
5+
permission java.lang.RuntimePermission "accessClassInPackage.jdk.internal.foreign";
6+
};
7+

0 commit comments

Comments
 (0)
Please sign in to comment.