Browse Source

linuxkm/module_hooks.c: separate cleanup into static libwolfssl_cleanup(), and call it from wolfssl_init() if wolfcrypt_test() fails.

Daniel Pouzzner 3 years ago
parent
commit
542ad0a81b
1 changed files with 23 additions and 16 deletions
  1. 23 16
      linuxkm/module_hooks.c

+ 23 - 16
linuxkm/module_hooks.c

@@ -31,6 +31,26 @@
 #include <linux/delay.h>
 #endif
 
+static int libwolfssl_cleanup(void) {
+    int ret;
+#ifdef WOLFCRYPT_ONLY
+    ret = wolfCrypt_Cleanup();
+    if (ret != 0)
+        pr_err("wolfCrypt_Cleanup() failed: %s", wc_GetErrorString(ret));
+    else
+        pr_info("wolfCrypt " LIBWOLFSSL_VERSION_STRING " cleanup complete.\n");
+#else
+    ret = wolfSSL_Cleanup();
+    if (ret != WOLFSSL_SUCCESS)
+        pr_err("wolfSSL_Cleanup() failed: %s", wc_GetErrorString(ret));
+    else
+        pr_info("wolfSSL " LIBWOLFSSL_VERSION_STRING " cleanup complete.\n");
+#endif
+
+    return ret;
+}
+
+
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
 static int __init wolfssl_init(void)
 #else
@@ -55,7 +75,8 @@ static int wolfssl_init(void)
 #ifndef NO_CRYPT_TEST
     ret = wolfcrypt_test(NULL);
     if (ret < 0) {
-        pr_err("wolfcrypt self-test failed.");
+        pr_err("wolfcrypt self-test failed with return code %d.", ret);
+        (void)libwolfssl_cleanup();
         msleep(10);
         return -ENOTRECOVERABLE;
     }
@@ -81,21 +102,7 @@ static void __exit wolfssl_exit(void)
 static void wolfssl_exit(void)
 #endif
 {
-    int ret;
-#ifdef WOLFCRYPT_ONLY
-    ret = wolfCrypt_Cleanup();
-    if (ret != 0)
-        pr_err("wolfCrypt_Cleanup() failed: %s", wc_GetErrorString(ret));
-    else
-        pr_info("wolfCrypt " LIBWOLFSSL_VERSION_STRING " cleanup complete.\n");
-#else
-    ret = wolfSSL_Cleanup();
-    if (ret != WOLFSSL_SUCCESS)
-        pr_err("wolfSSL_Cleanup() failed: %s", wc_GetErrorString(ret));
-    else
-        pr_info("wolfSSL " LIBWOLFSSL_VERSION_STRING " cleanup complete.\n");
-#endif
-
+    (void)libwolfssl_cleanup();
     return;
 }