Blame SOURCES/rh1929465-improve_system_FIPS_detection.patch

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