Blame SOURCES/rh2052829-fips_runtime_nss_detection.patch

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