Skip to content

Commit fa5078e

Browse files
Address review comments
1 parent 9a21f3b commit fa5078e

File tree

1 file changed

+23
-37
lines changed

1 file changed

+23
-37
lines changed

closed/src/java.base/share/classes/openj9/internal/security/RestrictedSecurity.java

+23-37
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package openj9.internal.security;
2525

26+
import java.lang.StackWalker.Option;
2627
import java.nio.charset.StandardCharsets;
2728
import java.security.MessageDigest;
2829
import java.security.NoSuchAlgorithmException;
@@ -79,8 +80,6 @@ public final class RestrictedSecurity {
7980

8081
private static RestrictedSecurityProperties restricts;
8182

82-
private static boolean profileHashChecked = false;
83-
8483
private static final Set<String> unmodifiableProperties = new HashSet<>();
8584

8685
private static final Map<String, List<String>> supportedPlatformsNSS = new HashMap<>();
@@ -163,20 +162,6 @@ private RestrictedSecurity() {
163162
super();
164163
}
165164

166-
/**
167-
* Check loaded profiles' hash values.
168-
*
169-
* In order to avoid unintentional changes in profiles and incentivize
170-
* extending profiles, instead of altering them, a digest of the profile
171-
* is calculated and compared to the expected value.
172-
*/
173-
public static void checkHashValues() {
174-
if (profileParser != null) {
175-
profileParser.checkHashValues();
176-
profileParser = null;
177-
}
178-
}
179-
180165
/**
181166
* Check if restricted security mode is enabled.
182167
*
@@ -246,9 +231,10 @@ public static boolean isFIPSEnabled() {
246231
*/
247232
public static boolean isServiceAllowed(Service service) {
248233
if (securityEnabled) {
249-
if (!(profileHashChecked || isJarVerifierinStackTrace())) {
250-
profileHashChecked = true;
251-
checkHashValues();
234+
ProfileParser parser = profileParser;
235+
if ((parser != null) && !isJarVerifierInStackTrace()) {
236+
profileParser = null;
237+
parser.checkHashValues();
252238
}
253239
return restricts.isRestrictedServiceAllowed(service, true);
254240
}
@@ -263,9 +249,10 @@ public static boolean isServiceAllowed(Service service) {
263249
*/
264250
public static boolean canServiceBeRegistered(Service service) {
265251
if (securityEnabled) {
266-
if (!(profileHashChecked || isJarVerifierinStackTrace())) {
267-
profileHashChecked = true;
268-
checkHashValues();
252+
ProfileParser parser = profileParser;
253+
if ((parser != null) && !isJarVerifierInStackTrace()) {
254+
profileParser = null;
255+
parser.checkHashValues();
269256
}
270257
return restricts.isRestrictedServiceAllowed(service, false);
271258
}
@@ -280,9 +267,10 @@ public static boolean canServiceBeRegistered(Service service) {
280267
*/
281268
public static boolean isProviderAllowed(String providerName) {
282269
if (securityEnabled) {
283-
if (!(profileHashChecked || isJarVerifierinStackTrace())) {
284-
profileHashChecked = true;
285-
checkHashValues();
270+
ProfileParser parser = profileParser;
271+
if ((parser != null) && !isJarVerifierInStackTrace()) {
272+
profileParser = null;
273+
parser.checkHashValues();
286274
}
287275
// Remove argument, e.g. -NSS-FIPS, if present.
288276
int pos = providerName.indexOf('-');
@@ -303,9 +291,10 @@ public static boolean isProviderAllowed(String providerName) {
303291
*/
304292
public static boolean isProviderAllowed(Class<?> providerClazz) {
305293
if (securityEnabled) {
306-
if (!(profileHashChecked || isJarVerifierinStackTrace())) {
307-
profileHashChecked = true;
308-
checkHashValues();
294+
ProfileParser parser = profileParser;
295+
if ((parser != null) && !isJarVerifierInStackTrace()) {
296+
profileParser = null;
297+
parser.checkHashValues();
309298
}
310299
String providerClassName = providerClazz.getName();
311300

@@ -397,15 +386,12 @@ private static void getProfileID(Properties props) {
397386
}
398387

399388
private static boolean isJarVerifierinStackTrace() {
400-
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
401-
for (int i = 1; i < elements.length; i++) {
402-
StackTraceElement stackTraceElement = elements[i];
403-
if ("java.util.jar.JarVerifier".equals(stackTraceElement.getClassName())
404-
&& "java.base".equals(stackTraceElement.getModuleName())) {
405-
return true;
406-
}
407-
}
408-
return false;
389+
return StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE)
390+
.walk(sf -> sf.map(s -> s.toStackTraceElement())
391+
.anyMatch(s -> ("java.util.jar.JarVerifier".equals(s.getClassName())
392+
&& "java.base".equals(s.getModuleName()))
393+
)
394+
);
409395
}
410396

411397
private static void checkIfKnownProfileSupported() {

0 commit comments

Comments
 (0)