Blame SOURCES/rh1929465-improve_system_FIPS_detection.patch

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