Skip to content

Commit 9d7c13e

Browse files
committedAug 15, 2022
8155246: Throw error if default java.security file is missing
Reviewed-by: mullan
1 parent e89abb7 commit 9d7c13e

File tree

4 files changed

+108
-18
lines changed

4 files changed

+108
-18
lines changed
 

‎src/java.base/share/classes/java/security/Security.java

+4-18
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
* implementation-specific location, which is typically the properties file
4848
* {@code conf/security/java.security} in the Java installation directory.
4949
*
50+
* @implNote If the properties file fails to load, the JDK implementation will
51+
* throw an unspecified error when initializing the {@code Security} class.
52+
*
5053
* @author Benjamin Renaud
5154
* @since 1.1
5255
*/
@@ -183,28 +186,11 @@ private static void initialize() {
183186
}
184187

185188
if (!loadedProps) {
186-
initializeStatic();
187-
if (sdebug != null) {
188-
sdebug.println("unable to load security properties " +
189-
"-- using defaults");
190-
}
189+
throw new InternalError("java.security file missing");
191190
}
192191

193192
}
194193

195-
/*
196-
* Initialize to default values, if <java.home>/lib/java.security
197-
* is not found.
198-
*/
199-
private static void initializeStatic() {
200-
props.put("security.provider.1", "sun.security.provider.Sun");
201-
props.put("security.provider.2", "sun.security.rsa.SunRsaSign");
202-
props.put("security.provider.3", "sun.security.ssl.SunJSSE");
203-
props.put("security.provider.4", "com.sun.crypto.provider.SunJCE");
204-
props.put("security.provider.5", "sun.security.jgss.SunProvider");
205-
props.put("security.provider.6", "com.sun.security.sasl.Provider");
206-
}
207-
208194
/**
209195
* Don't let anyone instantiate this.
210196
*/

‎src/java.base/share/conf/security/java.security

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
# the command line, set the key security.overridePropertiesFile
2323
# to false in the master security properties file. It is set to true
2424
# by default.
25+
#
26+
# If this properties file fails to load, the JDK implementation will throw
27+
# an unspecified error when initializing the java.security.Security class.
2528

2629
# In this file, various security properties are set for use by
2730
# java.security classes. This is where users can statically register
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import jdk.test.lib.process.OutputAnalyzer;
25+
import jdk.test.lib.process.ProcessTools;
26+
27+
import java.io.IOException;
28+
import java.io.UncheckedIOException;
29+
import java.nio.file.*;
30+
31+
import java.security.Security;
32+
import java.util.Arrays;
33+
import java.util.Optional;
34+
35+
/*
36+
* @test
37+
* @summary Throw error if default java.security file is missing
38+
* @bug 8155246
39+
* @library /test/lib
40+
* @run main ConfigFileTest
41+
*/
42+
public class ConfigFileTest {
43+
44+
public static void main(String[] args) throws Exception {
45+
Path copyJdkDir = Path.of("./jdk-8155246-tmpdir");
46+
Path copiedJava = Optional.of(
47+
Path.of(copyJdkDir.toString(), "bin", "java"))
48+
.orElseThrow(() -> new RuntimeException("Unable to locate new JDK")
49+
);
50+
51+
if (args.length == 1) {
52+
// set up is complete. Run code to exercise loading of java.security
53+
System.out.println(Arrays.toString(Security.getProviders()));
54+
} else {
55+
Files.createDirectory(copyJdkDir);
56+
Path jdkTestDir = Path.of(Optional.of(System.getProperty("test.jdk"))
57+
.orElseThrow(() -> new RuntimeException("Couldn't load JDK Test Dir"))
58+
);
59+
60+
copyJDKMinusJavaSecurity(jdkTestDir, copyJdkDir);
61+
String extraPropsFile = Path.of(System.getProperty("test.src"), "override.props").toString();
62+
63+
// exercise some debug flags while we're here
64+
// launch JDK without java.security file being present or specified
65+
exerciseSecurity(copiedJava.toString(), "-cp", System.getProperty("test.classes"),
66+
"-Djava.security.debug=all", "-Djavax.net.debug=all", "ConfigFileTest", "runner");
67+
68+
// test the override functionality also. Should not be allowed since
69+
// "security.overridePropertiesFile=true" Security property is missing.
70+
exerciseSecurity(copiedJava.toString(), "-cp", System.getProperty("test.classes"),
71+
"-Djava.security.debug=all", "-Djavax.net.debug=all",
72+
"-Djava.security.properties==file://" + extraPropsFile, "ConfigFileTest", "runner");
73+
}
74+
}
75+
76+
private static void exerciseSecurity(String... args) throws Exception {
77+
ProcessBuilder process = new ProcessBuilder(args);
78+
OutputAnalyzer oa = ProcessTools.executeProcess(process);
79+
oa.shouldHaveExitValue(1).shouldContain("java.security file missing");
80+
}
81+
82+
private static void copyJDKMinusJavaSecurity(Path src, Path dst) throws Exception {
83+
Files.walk(src)
84+
.skip(1)
85+
.filter(p -> !p.toString().endsWith("java.security"))
86+
.forEach(file -> {
87+
try {
88+
Files.copy(file, dst.resolve(src.relativize(file)), StandardCopyOption.COPY_ATTRIBUTES);
89+
} catch (IOException ioe) {
90+
throw new UncheckedIOException(ioe);
91+
}
92+
});
93+
}
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
security.provider.1=sun.security.provider.Sun
2+
security.provider.2=sun.security.rsa.SunRsaSign
3+
security.provider.3=sun.security.ssl.SunJSSE
4+
security.provider.4=com.sun.crypto.provider.SunJCE
5+
security.provider.5=sun.security.jgss.SunProvider
6+
security.provider.6=com.sun.security.sasl.Provider
7+

0 commit comments

Comments
 (0)
Please sign in to comment.