Blame SOURCES/rh1929465-improve_system_FIPS_detection.patch

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