Blame SOURCES/rh2052829-fips_runtime_nss_detection.patch

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