Blame SOURCES/rh2052829-fips_runtime_nss_detection.patch

4683cf
commit 090ea0389db5c2e0c8ee13652bccd544b17872c2
4683cf
Author: Andrew Hughes <gnu.andrew@redhat.com>
4683cf
Date:   Mon Feb 7 15:33:27 2022 +0000
4683cf
4683cf
    RH2051605: Detect NSS at Runtime for FIPS detection
4683cf
4683cf
diff --git openjdk.orig/src/java.base/linux/native/libsystemconf/systemconf.c openjdk/src/java.base/linux/native/libsystemconf/systemconf.c
4683cf
index caf678a7dd6..8dcb7d9073f 100644
4683cf
--- openjdk.orig/src/java.base/linux/native/libsystemconf/systemconf.c
4683cf
+++ openjdk/src/java.base/linux/native/libsystemconf/systemconf.c
4683cf
@@ -23,26 +23,37 @@
4683cf
  * questions.
4683cf
  */
4683cf
 
4683cf
-#include <dlfcn.h>
4683cf
 #include <jni.h>
4683cf
 #include <jni_util.h>
4683cf
+#include "jvm_md.h"
4683cf
 #include <stdio.h>
4683cf
 
4683cf
 #ifdef SYSCONF_NSS
4683cf
 #include <nss3/pk11pub.h>
4683cf
+#else
4683cf
+#include <dlfcn.h>
4683cf
 #endif //SYSCONF_NSS
4683cf
 
4683cf
 #include "java_security_SystemConfigurator.h"
4683cf
 
4683cf
-#define MSG_MAX_SIZE 96
4683cf
+#define MSG_MAX_SIZE 256
4683cf
+#define FIPS_ENABLED_PATH "/proc/sys/crypto/fips_enabled"
4683cf
+
4683cf
+typedef int (SECMOD_GET_SYSTEM_FIPS_ENABLED_TYPE)(void);
4683cf
 
4683cf
+static SECMOD_GET_SYSTEM_FIPS_ENABLED_TYPE *getSystemFIPSEnabled;
4683cf
 static jmethodID debugPrintlnMethodID = NULL;
4683cf
 static jobject debugObj = NULL;
4683cf
 
4683cf
-// Only used when NSS is unavailable and FIPS_ENABLED_PATH is read
4683cf
-#ifndef SYSCONF_NSS
4683cf
-
4683cf
-#define FIPS_ENABLED_PATH "/proc/sys/crypto/fips_enabled"
4683cf
+static void dbgPrint(JNIEnv *env, const char* msg)
4683cf
+{
4683cf
+    jstring jMsg;
4683cf
+    if (debugObj != NULL) {
4683cf
+        jMsg = (*env)->NewStringUTF(env, msg);
4683cf
+        CHECK_NULL(jMsg);
4683cf
+        (*env)->CallVoidMethod(env, debugObj, debugPrintlnMethodID, jMsg);
4683cf
+    }
4683cf
+}
4683cf
 
4683cf
 static void throwIOException(JNIEnv *env, const char *msg)
4683cf
 {
4683cf
@@ -51,18 +62,61 @@ static void throwIOException(JNIEnv *env, const char *msg)
4683cf
         (*env)->ThrowNew(env, cls, msg);
4683cf
 }
4683cf
 
4683cf
-#endif
4683cf
+static void handle_msg(JNIEnv *env, const char* msg, int msg_bytes)
4683cf
+{
4683cf
+  if (msg_bytes > 0 && msg_bytes < MSG_MAX_SIZE) {
4683cf
+    dbgPrint(env, msg);
4683cf
+  } else {
4683cf
+    dbgPrint(env, "systemconf: cannot render message");
4683cf
+  }
4683cf
+}
4683cf
 
4683cf
-static void dbgPrint(JNIEnv *env, const char* msg)
4683cf
+// Only used when NSS is not linked at build time
4683cf
+#ifndef SYSCONF_NSS
4683cf
+
4683cf
+static void *nss_handle;
4683cf
+
4683cf
+static jboolean loadNSS(JNIEnv *env)
4683cf
 {
4683cf
-    jstring jMsg;
4683cf
-    if (debugObj != NULL) {
4683cf
-        jMsg = (*env)->NewStringUTF(env, msg);
4683cf
-        CHECK_NULL(jMsg);
4683cf
-        (*env)->CallVoidMethod(env, debugObj, debugPrintlnMethodID, jMsg);
4683cf
-    }
4683cf
+  char msg[MSG_MAX_SIZE];
4683cf
+  int msg_bytes;
4683cf
+  const char* errmsg;
4683cf
+
4683cf
+  nss_handle = dlopen(JNI_LIB_NAME("nss3"), RTLD_LAZY);
4683cf
+  if (nss_handle == NULL) {
4683cf
+    errmsg = dlerror();
4683cf
+    msg_bytes = snprintf(msg, MSG_MAX_SIZE, "loadNSS: dlopen: %s\n",
4683cf
+                         errmsg);
4683cf
+    handle_msg(env, msg, msg_bytes);
4683cf
+    return JNI_FALSE;
4683cf
+  }
4683cf
+  dlerror(); /* Clear errors */
4683cf
+  getSystemFIPSEnabled = (SECMOD_GET_SYSTEM_FIPS_ENABLED_TYPE*)dlsym(nss_handle, "SECMOD_GetSystemFIPSEnabled");
4683cf
+  if ((errmsg = dlerror()) != NULL) {
4683cf
+    msg_bytes = snprintf(msg, MSG_MAX_SIZE, "loadNSS: dlsym: %s\n",
4683cf
+                         errmsg);
4683cf
+    handle_msg(env, msg, msg_bytes);
4683cf
+    return JNI_FALSE;
4683cf
+  }
4683cf
+  return JNI_TRUE;
4683cf
+}
4683cf
+
4683cf
+static void closeNSS(JNIEnv *env)
4683cf
+{
4683cf
+  char msg[MSG_MAX_SIZE];
4683cf
+  int msg_bytes;
4683cf
+  const char* errmsg;
4683cf
+
4683cf
+  if (dlclose(nss_handle) != 0) {
4683cf
+    errmsg = dlerror();
4683cf
+    msg_bytes = snprintf(msg, MSG_MAX_SIZE, "closeNSS: dlclose: %s\n",
4683cf
+                         errmsg);
4683cf
+    handle_msg(env, msg, msg_bytes);
4683cf
+  }
4683cf
 }
4683cf
 
4683cf
+#endif
4683cf
+
4683cf
 /*
4683cf
  * Class:     java_security_SystemConfigurator
4683cf
  * Method:    JNI_OnLoad
4683cf
@@ -104,6 +158,14 @@ JNIEXPORT jint JNICALL DEF_JNI_OnLoad(JavaVM *vm, void *reserved)
4683cf
         debugObj = (*env)->NewGlobalRef(env, debugObj);
4683cf
     }
4683cf
 
4683cf
+#ifdef SYSCONF_NSS
4683cf
+    getSystemFIPSEnabled = *SECMOD_GetSystemFIPSEnabled;
4683cf
+#else
4683cf
+    if (loadNSS(env) == JNI_FALSE) {
4683cf
+      dbgPrint(env, "libsystemconf: Failed to load NSS library.");
4683cf
+    }
4683cf
+#endif
4683cf
+
4683cf
     return (*env)->GetVersion(env);
4683cf
 }
4683cf
 
4683cf
@@ -119,6 +181,9 @@ JNIEXPORT void JNICALL DEF_JNI_OnUnload(JavaVM *vm, void *reserved)
4683cf
         if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_2) != JNI_OK) {
4683cf
             return; /* Should not happen */
4683cf
         }
4683cf
+#ifndef SYSCONF_NSS
4683cf
+        closeNSS(env);
4683cf
+#endif
4683cf
         (*env)->DeleteGlobalRef(env, debugObj);
4683cf
     }
4683cf
 }
4683cf
@@ -130,44 +195,30 @@ JNIEXPORT jboolean JNICALL Java_java_security_SystemConfigurator_getSystemFIPSEn
4683cf
     char msg[MSG_MAX_SIZE];
4683cf
     int msg_bytes;
4683cf
 
4683cf
-#ifdef SYSCONF_NSS
4683cf
-
4683cf
-    dbgPrint(env, "getSystemFIPSEnabled: calling SECMOD_GetSystemFIPSEnabled");
4683cf
-    fips_enabled = SECMOD_GetSystemFIPSEnabled();
4683cf
-    msg_bytes = snprintf(msg, MSG_MAX_SIZE, "getSystemFIPSEnabled:" \
4683cf
-            " SECMOD_GetSystemFIPSEnabled returned 0x%x", fips_enabled);
4683cf
-    if (msg_bytes > 0 && msg_bytes < MSG_MAX_SIZE) {
4683cf
-        dbgPrint(env, msg);
4683cf
+    if (getSystemFIPSEnabled != NULL) {
4683cf
+      dbgPrint(env, "getSystemFIPSEnabled: calling SECMOD_GetSystemFIPSEnabled");
4683cf
+      fips_enabled = (*getSystemFIPSEnabled)();
4683cf
+      msg_bytes = snprintf(msg, MSG_MAX_SIZE, "getSystemFIPSEnabled:"   \
4683cf
+                           " SECMOD_GetSystemFIPSEnabled returned 0x%x", fips_enabled);
4683cf
+      handle_msg(env, msg, msg_bytes);
4683cf
+      return (fips_enabled == 1 ? JNI_TRUE : JNI_FALSE);
4683cf
     } else {
4683cf
-        dbgPrint(env, "getSystemFIPSEnabled: cannot render" \
4683cf
-                " SECMOD_GetSystemFIPSEnabled return value");
4683cf
-    }
4683cf
-    return (fips_enabled == 1 ? JNI_TRUE : JNI_FALSE);
4683cf
+      FILE *fe;
4683cf
 
4683cf
-#else // SYSCONF_NSS
4683cf
-
4683cf
-    FILE *fe;
4683cf
-
4683cf
-    dbgPrint(env, "getSystemFIPSEnabled: reading " FIPS_ENABLED_PATH);
4683cf
-    if ((fe = fopen(FIPS_ENABLED_PATH, "r")) == NULL) {
4683cf
+      dbgPrint(env, "getSystemFIPSEnabled: reading " FIPS_ENABLED_PATH);
4683cf
+      if ((fe = fopen(FIPS_ENABLED_PATH, "r")) == NULL) {
4683cf
         throwIOException(env, "Cannot open " FIPS_ENABLED_PATH);
4683cf
         return JNI_FALSE;
4683cf
-    }
4683cf
-    fips_enabled = fgetc(fe);
4683cf
-    fclose(fe);
4683cf
-    if (fips_enabled == EOF) {
4683cf
+      }
4683cf
+      fips_enabled = fgetc(fe);
4683cf
+      fclose(fe);
4683cf
+      if (fips_enabled == EOF) {
4683cf
         throwIOException(env, "Cannot read " FIPS_ENABLED_PATH);
4683cf
         return JNI_FALSE;
4683cf
+      }
4683cf
+      msg_bytes = snprintf(msg, MSG_MAX_SIZE, "getSystemFIPSEnabled:"   \
4683cf
+                           " read character is '%c'", fips_enabled);
4683cf
+      handle_msg(env, msg, msg_bytes);
4683cf
+      return (fips_enabled == '1' ? JNI_TRUE : JNI_FALSE);
4683cf
     }
4683cf
-    msg_bytes = snprintf(msg, MSG_MAX_SIZE, "getSystemFIPSEnabled:" \
4683cf
-            " read character is '%c'", fips_enabled);
4683cf
-    if (msg_bytes > 0 && msg_bytes < MSG_MAX_SIZE) {
4683cf
-        dbgPrint(env, msg);
4683cf
-    } else {
4683cf
-        dbgPrint(env, "getSystemFIPSEnabled: cannot render" \
4683cf
-                " read character");
4683cf
-    }
4683cf
-    return (fips_enabled == '1' ? JNI_TRUE : JNI_FALSE);
4683cf
-
4683cf
-#endif // SYSCONF_NSS
4683cf
 }