Blame SOURCES/rh1929465-improve_system_FIPS_detection.patch

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