Blame SOURCES/rh1929465-improve_system_FIPS_detection.patch

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