Blame SOURCES/rh2052829-fips_runtime_nss_detection.patch

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