Skip to content

Commit 2e49918

Browse files
Avoid checking RestrictedSecurity profile hash during jar verification
If the process of verifying a jar is started before the RestrictedSecurity profile is loaded, the hash calculation is triggered as part of it leading to a nested jar verification and a subsequent error. To avoid that, the hash calulation of a profile is skipped if triggered by a jar verification process and is performed later in the loading process. Signed-off-by: Kostas Tsiounis <[email protected]>
1 parent 659b5b5 commit 2e49918

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

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

+30
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public final class RestrictedSecurity {
7979

8080
private static RestrictedSecurityProperties restricts;
8181

82+
private static boolean profileHashChecked = false;
83+
8284
private static final Set<String> unmodifiableProperties = new HashSet<>();
8385

8486
private static final Map<String, List<String>> supportedPlatformsNSS = new HashMap<>();
@@ -244,6 +246,10 @@ public static boolean isFIPSEnabled() {
244246
*/
245247
public static boolean isServiceAllowed(Service service) {
246248
if (securityEnabled) {
249+
if (!(profileHashChecked || isJarVerifierinStackTrace())) {
250+
profileHashChecked = true;
251+
checkHashValues();
252+
}
247253
return restricts.isRestrictedServiceAllowed(service, true);
248254
}
249255
return true;
@@ -257,6 +263,10 @@ public static boolean isServiceAllowed(Service service) {
257263
*/
258264
public static boolean canServiceBeRegistered(Service service) {
259265
if (securityEnabled) {
266+
if (!profileHashChecked) {
267+
checkHashValues();
268+
profileHashChecked = true;
269+
}
260270
return restricts.isRestrictedServiceAllowed(service, false);
261271
}
262272
return true;
@@ -270,6 +280,10 @@ public static boolean canServiceBeRegistered(Service service) {
270280
*/
271281
public static boolean isProviderAllowed(String providerName) {
272282
if (securityEnabled) {
283+
if (!(profileHashChecked || isJarVerifierinStackTrace())) {
284+
profileHashChecked = true;
285+
checkHashValues();
286+
}
273287
// Remove argument, e.g. -NSS-FIPS, if present.
274288
int pos = providerName.indexOf('-');
275289
if (pos >= 0) {
@@ -289,6 +303,10 @@ public static boolean isProviderAllowed(String providerName) {
289303
*/
290304
public static boolean isProviderAllowed(Class<?> providerClazz) {
291305
if (securityEnabled) {
306+
if (!(profileHashChecked || isJarVerifierinStackTrace())) {
307+
profileHashChecked = true;
308+
checkHashValues();
309+
}
292310
String providerClassName = providerClazz.getName();
293311

294312
// Check if the specified class extends java.security.Provider.
@@ -378,6 +396,18 @@ private static void getProfileID(Properties props) {
378396
}
379397
}
380398

399+
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;
409+
}
410+
381411
private static void checkIfKnownProfileSupported() {
382412
if (profileID.contains("NSS") && !isNSSSupported) {
383413
printStackTraceAndExit("NSS RestrictedSecurity profiles are not supported"

src/java.base/share/classes/sun/security/jca/Providers.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
/*
2727
* ===========================================================================
28-
* (c) Copyright IBM Corp. 2024, 2024 All Rights Reserved
28+
* (c) Copyright IBM Corp. 2024, 2025 All Rights Reserved
2929
* ===========================================================================
3030
*/
3131

@@ -111,7 +111,6 @@ private Providers() {
111111
// triggers a getInstance() call (although that should not happen)
112112
providerList = ProviderList.EMPTY;
113113
providerList = ProviderList.fromSecurityProperties();
114-
RestrictedSecurity.checkHashValues();
115114
}
116115

117116
// Return Sun provider.

0 commit comments

Comments
 (0)