Blame SOURCES/rh1929465-improve_system_FIPS_detection.patch

2578b9
diff --git openjdk.orig/make/autoconf/libraries.m4 openjdk/make/autoconf/libraries.m4
2578b9
--- openjdk.orig/make/autoconf/libraries.m4
2578b9
+++ openjdk/make/autoconf/libraries.m4
2578b9
@@ -101,6 +101,7 @@
2578b9
   LIB_SETUP_LIBFFI
2578b9
   LIB_SETUP_BUNDLED_LIBS
2578b9
   LIB_SETUP_MISC_LIBS
2578b9
+  LIB_SETUP_SYSCONF_LIBS
2578b9
   LIB_SETUP_SOLARIS_STLPORT
2578b9
   LIB_TESTS_SETUP_GRAALUNIT
2578b9
 
2578b9
@@ -223,3 +224,62 @@
2578b9
   fi
2578b9
 ])
2578b9
 
2578b9
+################################################################################
2578b9
+# Setup system configuration libraries
2578b9
+################################################################################
2578b9
+AC_DEFUN_ONCE([LIB_SETUP_SYSCONF_LIBS],
2578b9
+[
2578b9
+  ###############################################################################
2578b9
+  #
2578b9
+  # Check for the NSS library
2578b9
+  #
2578b9
+
2578b9
+  AC_MSG_CHECKING([whether to use the system NSS library with the System Configurator (libsysconf)])
2578b9
+
2578b9
+  # default is not available
2578b9
+  DEFAULT_SYSCONF_NSS=no
2578b9
+
2578b9
+  AC_ARG_ENABLE([sysconf-nss], [AS_HELP_STRING([--enable-sysconf-nss],
2578b9
+     [build the System Configurator (libsysconf) using the system NSS library if available @<:@disabled@:>@])],
2578b9
+  [
2578b9
+    case "${enableval}" in
2578b9
+      yes)
2578b9
+        sysconf_nss=yes
2578b9
+        ;;
2578b9
+      *)
2578b9
+        sysconf_nss=no
2578b9
+        ;;
2578b9
+    esac
2578b9
+  ],
2578b9
+  [
2578b9
+    sysconf_nss=${DEFAULT_SYSCONF_NSS}
2578b9
+  ])
2578b9
+  AC_MSG_RESULT([$sysconf_nss])
2578b9
+
2578b9
+  USE_SYSCONF_NSS=false
2578b9
+  if test "x${sysconf_nss}" = "xyes"; then
2578b9
+      PKG_CHECK_MODULES(NSS, nss >= 3.53, [NSS_FOUND=yes], [NSS_FOUND=no])
2578b9
+      if test "x${NSS_FOUND}" = "xyes"; then
2578b9
+         AC_MSG_CHECKING([for system FIPS support in NSS])
2578b9
+         saved_libs="${LIBS}"
2578b9
+         saved_cflags="${CFLAGS}"
2578b9
+         CFLAGS="${CFLAGS} ${NSS_CFLAGS}"
2578b9
+         LIBS="${LIBS} ${NSS_LIBS}"
2578b9
+         AC_LANG_PUSH([C])
2578b9
+         AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <nss3/pk11pub.h>]],
2578b9
+                                         [[SECMOD_GetSystemFIPSEnabled()]])],
2578b9
+                        [AC_MSG_RESULT([yes])],
2578b9
+                        [AC_MSG_RESULT([no])
2578b9
+                        AC_MSG_ERROR([System NSS FIPS detection unavailable])])
2578b9
+         AC_LANG_POP([C])
2578b9
+         CFLAGS="${saved_cflags}"
2578b9
+         LIBS="${saved_libs}"
2578b9
+         USE_SYSCONF_NSS=true
2578b9
+      else
2578b9
+         dnl NSS 3.53 is the one that introduces the SECMOD_GetSystemFIPSEnabled API
2578b9
+         dnl in nss3/pk11pub.h.
2578b9
+         AC_MSG_ERROR([--enable-sysconf-nss specified, but NSS 3.53 or above not found.])
2578b9
+      fi
2578b9
+  fi
2578b9
+  AC_SUBST(USE_SYSCONF_NSS)
2578b9
+])
2578b9
diff --git openjdk.orig/make/autoconf/spec.gmk.in openjdk/make/autoconf/spec.gmk.in
2578b9
--- openjdk.orig/make/autoconf/spec.gmk.in
2578b9
+++ openjdk/make/autoconf/spec.gmk.in
2578b9
@@ -828,6 +828,10 @@
2578b9
 # Libraries
2578b9
 #
2578b9
 
2578b9
+USE_SYSCONF_NSS:=@USE_SYSCONF_NSS@
2578b9
+NSS_LIBS:=@NSS_LIBS@
2578b9
+NSS_CFLAGS:=@NSS_CFLAGS@
2578b9
+
2578b9
 USE_EXTERNAL_LCMS:=@USE_EXTERNAL_LCMS@
2578b9
 LCMS_CFLAGS:=@LCMS_CFLAGS@
2578b9
 LCMS_LIBS:=@LCMS_LIBS@
2578b9
diff --git openjdk.orig/make/lib/Lib-java.base.gmk openjdk/make/lib/Lib-java.base.gmk
2578b9
--- openjdk.orig/make/lib/Lib-java.base.gmk
2578b9
+++ openjdk/make/lib/Lib-java.base.gmk
2578b9
@@ -179,6 +179,31 @@
2578b9
 endif
2578b9
 
2578b9
 ################################################################################
2578b9
+# Create the systemconf library
2578b9
+
2578b9
+LIBSYSTEMCONF_CFLAGS :=
2578b9
+LIBSYSTEMCONF_CXXFLAGS :=
2578b9
+
2578b9
+ifeq ($(USE_SYSCONF_NSS), true)
2578b9
+  LIBSYSTEMCONF_CFLAGS += $(NSS_CFLAGS) -DSYSCONF_NSS
2578b9
+  LIBSYSTEMCONF_CXXFLAGS += $(NSS_CFLAGS) -DSYSCONF_NSS
2578b9
+endif
2578b9
+
2578b9
+ifeq ($(OPENJDK_BUILD_OS), linux)
2578b9
+  $(eval $(call SetupJdkLibrary, BUILD_LIBSYSTEMCONF, \
2578b9
+      NAME := systemconf, \
2578b9
+      OPTIMIZATION := LOW, \
2578b9
+      CFLAGS := $(CFLAGS_JDKLIB) $(LIBSYSTEMCONF_CFLAGS), \
2578b9
+      CXXFLAGS := $(CXXFLAGS_JDKLIB) $(LIBSYSTEMCONF_CXXFLAGS), \
2578b9
+      LDFLAGS := $(LDFLAGS_JDKLIB) \
2578b9
+          $(call SET_SHARED_LIBRARY_ORIGIN), \
2578b9
+      LIBS_unix := $(LIBDL) $(NSS_LIBS), \
2578b9
+  ))
2578b9
+
2578b9
+  TARGETS += $(BUILD_LIBSYSTEMCONF)
2578b9
+endif
2578b9
+
2578b9
+################################################################################
2578b9
 # Create the symbols file for static builds.
2578b9
 
2578b9
 ifeq ($(STATIC_BUILD), true)
2578b9
diff --git openjdk.orig/make/nb_native/nbproject/configurations.xml openjdk/make/nb_native/nbproject/configurations.xml
2578b9
--- openjdk.orig/make/nb_native/nbproject/configurations.xml
2578b9
+++ openjdk/make/nb_native/nbproject/configurations.xml
2578b9
@@ -2950,6 +2950,9 @@
2578b9
                   <in>LinuxWatchService.c</in>
2578b9
                 </df>
2578b9
               </df>
2578b9
+              <df name="libsystemconf">
2578b9
+                <in>systemconf.c</in>
2578b9
+              </df>
2578b9
             </df>
2578b9
           </df>
2578b9
           <df name="macosx">
2578b9
@@ -29301,6 +29304,11 @@
2578b9
             tool="0"
2578b9
             flavor2="0">
2578b9
       </item>
2578b9
+      
2578b9
+            ex="false"
2578b9
+            tool="0"
2578b9
+            flavor2="0">
2578b9
+      </item>
2578b9
       
2578b9
             ex="false"
2578b9
             tool="3"
2578b9
diff --git openjdk.orig/make/scripts/compare_exceptions.sh.incl openjdk/make/scripts/compare_exceptions.sh.incl
2578b9
--- openjdk.orig/make/scripts/compare_exceptions.sh.incl
2578b9
+++ openjdk/make/scripts/compare_exceptions.sh.incl
2578b9
@@ -179,6 +179,7 @@
2578b9
       ./lib/libsplashscreen.so
2578b9
       ./lib/libsunec.so
2578b9
       ./lib/libsunwjdga.so
2578b9
+      ./lib/libsystemconf.so
2578b9
       ./lib/libunpack.so
2578b9
       ./lib/libverify.so
2578b9
       ./lib/libzip.so
2578b9
@@ -289,6 +290,7 @@
2578b9
       ./lib/libsplashscreen.so
2578b9
       ./lib/libsunec.so
2578b9
       ./lib/libsunwjdga.so
2578b9
+      ./lib/libsystemconf.so
2578b9
       ./lib/libunpack.so
2578b9
       ./lib/libverify.so
2578b9
       ./lib/libzip.so
2578b9
diff --git openjdk.orig/src/java.base/linux/native/libsystemconf/systemconf.c openjdk/src/java.base/linux/native/libsystemconf/systemconf.c
2578b9
new file mode 100644
2578b9
--- /dev/null
2578b9
+++ openjdk/src/java.base/linux/native/libsystemconf/systemconf.c
2578b9
@@ -0,0 +1,168 @@
2578b9
+/*
2578b9
+ * Copyright (c) 2021, Red Hat, Inc.
2578b9
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2578b9
+ *
2578b9
+ * This code is free software; you can redistribute it and/or modify it
2578b9
+ * under the terms of the GNU General Public License version 2 only, as
2578b9
+ * published by the Free Software Foundation.  Oracle designates this
2578b9
+ * particular file as subject to the "Classpath" exception as provided
2578b9
+ * by Oracle in the LICENSE file that accompanied this code.
2578b9
+ *
2578b9
+ * This code is distributed in the hope that it will be useful, but WITHOUT
2578b9
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2578b9
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2578b9
+ * version 2 for more details (a copy is included in the LICENSE file that
2578b9
+ * accompanied this code).
2578b9
+ *
2578b9
+ * You should have received a copy of the GNU General Public License version
2578b9
+ * 2 along with this work; if not, write to the Free Software Foundation,
2578b9
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2578b9
+ *
2578b9
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2578b9
+ * or visit www.oracle.com if you need additional information or have any
2578b9
+ * questions.
2578b9
+ */
2578b9
+
2578b9
+#include <dlfcn.h>
2578b9
+#include <jni.h>
2578b9
+#include <jni_util.h>
2578b9
+#include <stdio.h>
2578b9
+
2578b9
+#ifdef SYSCONF_NSS
2578b9
+#include <nss3/pk11pub.h>
2578b9
+#endif //SYSCONF_NSS
2578b9
+
2578b9
+#include "java_security_SystemConfigurator.h"
2578b9
+
2578b9
+#define FIPS_ENABLED_PATH "/proc/sys/crypto/fips_enabled"
2578b9
+#define MSG_MAX_SIZE 96
2578b9
+
2578b9
+static jmethodID debugPrintlnMethodID = NULL;
2578b9
+static jobject debugObj = NULL;
2578b9
+
2578b9
+static void throwIOException(JNIEnv *env, const char *msg);
2578b9
+static void dbgPrint(JNIEnv *env, const char* msg);
2578b9
+
2578b9
+/*
2578b9
+ * Class:     java_security_SystemConfigurator
2578b9
+ * Method:    JNI_OnLoad
2578b9
+ */
2578b9
+JNIEXPORT jint JNICALL DEF_JNI_OnLoad(JavaVM *vm, void *reserved)
2578b9
+{
2578b9
+    JNIEnv *env;
2578b9
+    jclass sysConfCls, debugCls;
2578b9
+    jfieldID sdebugFld;
2578b9
+
2578b9
+    if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_2) != JNI_OK) {
2578b9
+        return JNI_EVERSION; /* JNI version not supported */
2578b9
+    }
2578b9
+
2578b9
+    sysConfCls = (*env)->FindClass(env,"java/security/SystemConfigurator");
2578b9
+    if (sysConfCls == NULL) {
2578b9
+        printf("libsystemconf: SystemConfigurator class not found\n");
2578b9
+        return JNI_ERR;
2578b9
+    }
2578b9
+    sdebugFld = (*env)->GetStaticFieldID(env, sysConfCls,
2578b9
+            "sdebug", "Lsun/security/util/Debug;");
2578b9
+    if (sdebugFld == NULL) {
2578b9
+        printf("libsystemconf: SystemConfigurator::sdebug field not found\n");
2578b9
+        return JNI_ERR;
2578b9
+    }
2578b9
+    debugObj = (*env)->GetStaticObjectField(env, sysConfCls, sdebugFld);
2578b9
+    if (debugObj != NULL) {
2578b9
+        debugCls = (*env)->FindClass(env,"sun/security/util/Debug");
2578b9
+        if (debugCls == NULL) {
2578b9
+            printf("libsystemconf: Debug class not found\n");
2578b9
+            return JNI_ERR;
2578b9
+        }
2578b9
+        debugPrintlnMethodID = (*env)->GetMethodID(env, debugCls,
2578b9
+                "println", "(Ljava/lang/String;)V");
2578b9
+        if (debugPrintlnMethodID == NULL) {
2578b9
+            printf("libsystemconf: Debug::println(String) method not found\n");
2578b9
+            return JNI_ERR;
2578b9
+        }
2578b9
+        debugObj = (*env)->NewGlobalRef(env, debugObj);
2578b9
+    }
2578b9
+
2578b9
+    return (*env)->GetVersion(env);
2578b9
+}
2578b9
+
2578b9
+/*
2578b9
+ * Class:     java_security_SystemConfigurator
2578b9
+ * Method:    JNI_OnUnload
2578b9
+ */
2578b9
+JNIEXPORT void JNICALL DEF_JNI_OnUnload(JavaVM *vm, void *reserved)
2578b9
+{
2578b9
+    JNIEnv *env;
2578b9
+
2578b9
+    if (debugObj != NULL) {
2578b9
+        if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_2) != JNI_OK) {
2578b9
+            return; /* Should not happen */
2578b9
+        }
2578b9
+        (*env)->DeleteGlobalRef(env, debugObj);
2578b9
+    }
2578b9
+}
2578b9
+
2578b9
+JNIEXPORT jboolean JNICALL Java_java_security_SystemConfigurator_getSystemFIPSEnabled
2578b9
+  (JNIEnv *env, jclass cls)
2578b9
+{
2578b9
+    int fips_enabled;
2578b9
+    char msg[MSG_MAX_SIZE];
2578b9
+    int msg_bytes;
2578b9
+
2578b9
+#ifdef SYSCONF_NSS
2578b9
+
2578b9
+    dbgPrint(env, "getSystemFIPSEnabled: calling SECMOD_GetSystemFIPSEnabled");
2578b9
+    fips_enabled = SECMOD_GetSystemFIPSEnabled();
2578b9
+    msg_bytes = snprintf(msg, MSG_MAX_SIZE, "getSystemFIPSEnabled:" \
2578b9
+            " SECMOD_GetSystemFIPSEnabled returned 0x%x", fips_enabled);
2578b9
+    if (msg_bytes > 0 && msg_bytes < MSG_MAX_SIZE) {
2578b9
+        dbgPrint(env, msg);
2578b9
+    } else {
2578b9
+        dbgPrint(env, "getSystemFIPSEnabled: cannot render" \
2578b9
+                " SECMOD_GetSystemFIPSEnabled return value");
2578b9
+    }
2578b9
+    return (fips_enabled == 1 ? JNI_TRUE : JNI_FALSE);
2578b9
+
2578b9
+#else // SYSCONF_NSS
2578b9
+
2578b9
+    FILE *fe;
2578b9
+
2578b9
+    dbgPrint(env, "getSystemFIPSEnabled: reading " FIPS_ENABLED_PATH);
2578b9
+    if ((fe = fopen(FIPS_ENABLED_PATH, "r")) == NULL) {
2578b9
+        throwIOException(env, "Cannot open " FIPS_ENABLED_PATH);
2578b9
+    }
2578b9
+    fips_enabled = fgetc(fe);
2578b9
+    fclose(fe);
2578b9
+    if (fips_enabled == EOF) {
2578b9
+        throwIOException(env, "Cannot read " FIPS_ENABLED_PATH);
2578b9
+    }
2578b9
+    msg_bytes = snprintf(msg, MSG_MAX_SIZE, "getSystemFIPSEnabled:" \
2578b9
+            " read character is '%c'", fips_enabled);
2578b9
+    if (msg_bytes > 0 && msg_bytes < MSG_MAX_SIZE) {
2578b9
+        dbgPrint(env, msg);
2578b9
+    } else {
2578b9
+        dbgPrint(env, "getSystemFIPSEnabled: cannot render" \
2578b9
+                " read character");
2578b9
+    }
2578b9
+    return (fips_enabled == '1' ? JNI_TRUE : JNI_FALSE);
2578b9
+
2578b9
+#endif // SYSCONF_NSS
2578b9
+}
2578b9
+
2578b9
+static void throwIOException(JNIEnv *env, const char *msg)
2578b9
+{
2578b9
+    jclass cls = (*env)->FindClass(env, "java/io/IOException");
2578b9
+    if (cls != 0)
2578b9
+        (*env)->ThrowNew(env, cls, msg);
2578b9
+}
2578b9
+
2578b9
+static void dbgPrint(JNIEnv *env, const char* msg)
2578b9
+{
2578b9
+    jstring jMsg;
2578b9
+    if (debugObj != NULL) {
2578b9
+        jMsg = (*env)->NewStringUTF(env, msg);
2578b9
+        CHECK_NULL(jMsg);
2578b9
+        (*env)->CallVoidMethod(env, debugObj, debugPrintlnMethodID, jMsg);
2578b9
+    }
2578b9
+}
2578b9
diff --git openjdk.orig/src/java.base/share/classes/java/security/SystemConfigurator.java openjdk/src/java.base/share/classes/java/security/SystemConfigurator.java
2578b9
--- openjdk.orig/src/java.base/share/classes/java/security/SystemConfigurator.java
2578b9
+++ openjdk/src/java.base/share/classes/java/security/SystemConfigurator.java
2578b9
@@ -1,5 +1,5 @@
2578b9
 /*
2578b9
- * Copyright (c) 2019, 2020, Red Hat, Inc.
2578b9
+ * Copyright (c) 2019, 2021, Red Hat, Inc.
2578b9
  *
2578b9
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2578b9
  *
2578b9
@@ -30,13 +30,9 @@
2578b9
 import java.io.FileInputStream;
2578b9
 import java.io.IOException;
2578b9
 
2578b9
-import java.nio.file.Files;
2578b9
-import java.nio.file.Path;
2578b9
-
2578b9
 import java.util.Iterator;
2578b9
 import java.util.Map.Entry;
2578b9
 import java.util.Properties;
2578b9
-import java.util.regex.Pattern;
2578b9
 
2578b9
 import sun.security.util.Debug;
2578b9
 
2578b9
@@ -58,10 +54,21 @@
2578b9
     private static final String CRYPTO_POLICIES_JAVA_CONFIG =
2578b9
             CRYPTO_POLICIES_BASE_DIR + "/back-ends/java.config";
2578b9
 
2578b9
-    private static final String CRYPTO_POLICIES_CONFIG =
2578b9
-            CRYPTO_POLICIES_BASE_DIR + "/config";
2578b9
+    private static boolean systemFipsEnabled = false;
2578b9
+
2578b9
+    private static final String SYSTEMCONF_NATIVE_LIB = "systemconf";
2578b9
+
2578b9
+    private static native boolean getSystemFIPSEnabled()
2578b9
+            throws IOException;
2578b9
 
2578b9
-    private static boolean systemFipsEnabled = false;
2578b9
+    static {
2578b9
+        AccessController.doPrivileged(new PrivilegedAction<Void>() {
2578b9
+            public Void run() {
2578b9
+                System.loadLibrary(SYSTEMCONF_NATIVE_LIB);
2578b9
+                return null;
2578b9
+            }
2578b9
+        });
2578b9
+    }
2578b9
 
2578b9
     /*
2578b9
      * Invoked when java.security.Security class is initialized, if
2578b9
@@ -170,16 +177,34 @@
2578b9
     }
2578b9
 
2578b9
     /*
2578b9
-     * FIPS is enabled only if crypto-policies are set to "FIPS"
2578b9
-     * and the com.redhat.fips property is true.
2578b9
+     * OpenJDK FIPS mode will be enabled only if the com.redhat.fips
2578b9
+     * system property is true (default) and the system is in FIPS mode.
2578b9
+     *
2578b9
+     * There are 2 possible ways in which OpenJDK detects that the system
2578b9
+     * is in FIPS mode: 1) if the NSS SECMOD_GetSystemFIPSEnabled API is
2578b9
+     * available at OpenJDK's built-time, it is called; 2) otherwise, the
2578b9
+     * /proc/sys/crypto/fips_enabled file is read.
2578b9
      */
2578b9
     private static boolean enableFips() throws Exception {
2578b9
         boolean shouldEnable = Boolean.valueOf(System.getProperty("com.redhat.fips", "true"));
2578b9
         if (shouldEnable) {
2578b9
-            String cryptoPoliciesConfig = new String(Files.readAllBytes(Path.of(CRYPTO_POLICIES_CONFIG)));
2578b9
-            if (sdebug != null) { sdebug.println("Crypto config:\n" + cryptoPoliciesConfig); }
2578b9
-            Pattern pattern = Pattern.compile("^FIPS$", Pattern.MULTILINE);
2578b9
-            return pattern.matcher(cryptoPoliciesConfig).find();
2578b9
+            if (sdebug != null) {
2578b9
+                sdebug.println("Calling getSystemFIPSEnabled (libsystemconf)...");
2578b9
+            }
2578b9
+            try {
2578b9
+                shouldEnable = getSystemFIPSEnabled();
2578b9
+                if (sdebug != null) {
2578b9
+                    sdebug.println("Call to getSystemFIPSEnabled (libsystemconf) returned: "
2578b9
+                            + shouldEnable);
2578b9
+                }
2578b9
+                return shouldEnable;
2578b9
+            } catch (IOException e) {
2578b9
+                if (sdebug != null) {
2578b9
+                    sdebug.println("Call to getSystemFIPSEnabled (libsystemconf) failed:");
2578b9
+                    sdebug.println(e.getMessage());
2578b9
+                }
2578b9
+                throw e;
2578b9
+            }
2578b9
         } else {
2578b9
             return false;
2578b9
         }