Blame SOURCES/rh1929465-improve_system_FIPS_detection.patch

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