From 542ad0a81b3a4d907a21bde131927218e89a7e42 Mon Sep 17 00:00:00 2001 From: Daniel Pouzzner Date: Wed, 16 Dec 2020 15:17:12 -0600 Subject: [PATCH] linuxkm/module_hooks.c: separate cleanup into static libwolfssl_cleanup(), and call it from wolfssl_init() if wolfcrypt_test() fails. --- linuxkm/module_hooks.c | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/linuxkm/module_hooks.c b/linuxkm/module_hooks.c index b1ea5231e..9bc8cb80a 100644 --- a/linuxkm/module_hooks.c +++ b/linuxkm/module_hooks.c @@ -31,6 +31,26 @@ #include #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; }