Blame SOURCES/rh1929465-improve_system_FIPS_detection.patch

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