module_hooks.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /* module_hooks.c -- module load/unload hooks for libwolfssl.ko
  2. *
  3. * Copyright (C) 2006-2021 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifndef WOLFSSL_LICENSE
  22. #define WOLFSSL_LICENSE "GPL v2"
  23. #endif
  24. #define FIPS_NO_WRAPPERS
  25. #ifdef HAVE_CONFIG_H
  26. #include <config.h>
  27. #endif
  28. #include <wolfssl/wolfcrypt/settings.h>
  29. #include <wolfssl/wolfcrypt/error-crypt.h>
  30. #include <wolfssl/ssl.h>
  31. #ifdef HAVE_FIPS
  32. #include <wolfssl/wolfcrypt/fips_test.h>
  33. #endif
  34. #ifndef NO_CRYPT_TEST
  35. #include <wolfcrypt/test/test.h>
  36. #include <linux/delay.h>
  37. #endif
  38. static int libwolfssl_cleanup(void) {
  39. int ret;
  40. #ifdef WOLFCRYPT_ONLY
  41. ret = wolfCrypt_Cleanup();
  42. if (ret != 0)
  43. pr_err("wolfCrypt_Cleanup() failed: %s\n", wc_GetErrorString(ret));
  44. else
  45. pr_info("wolfCrypt " LIBWOLFSSL_VERSION_STRING " cleanup complete.\n");
  46. #else
  47. ret = wolfSSL_Cleanup();
  48. if (ret != WOLFSSL_SUCCESS)
  49. pr_err("wolfSSL_Cleanup() failed: %s\n", wc_GetErrorString(ret));
  50. else
  51. pr_info("wolfSSL " LIBWOLFSSL_VERSION_STRING " cleanup complete.\n");
  52. #endif
  53. return ret;
  54. }
  55. #ifdef HAVE_LINUXKM_PIE_SUPPORT
  56. extern int wolfCrypt_PIE_first_function(void);
  57. extern int wolfCrypt_PIE_last_function(void);
  58. extern const unsigned int wolfCrypt_PIE_rodata_start[];
  59. extern const unsigned int wolfCrypt_PIE_rodata_end[];
  60. /* cheap portable ad-hoc hash function to confirm bitwise stability of the PIE
  61. * binary image.
  62. */
  63. static unsigned int hash_span(char *start, char *end) {
  64. unsigned int sum = 1;
  65. while (start < end) {
  66. unsigned int rotate_by;
  67. sum ^= *start++;
  68. rotate_by = (sum ^ (sum >> 5)) & 31;
  69. sum = (sum << rotate_by) | (sum >> (32 - rotate_by));
  70. }
  71. return sum;
  72. }
  73. #ifdef USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE
  74. extern struct wolfssl_linuxkm_pie_redirect_table wolfssl_linuxkm_pie_redirect_table;
  75. static int set_up_wolfssl_linuxkm_pie_redirect_table(void);
  76. #endif /* USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE */
  77. #endif /* HAVE_LINUXKM_PIE_SUPPORT */
  78. #ifdef HAVE_FIPS
  79. static void lkmFipsCb(int ok, int err, const char* hash)
  80. {
  81. if ((! ok) || (err != 0))
  82. pr_err("libwolfssl FIPS error: %s\n", wc_GetErrorString(err));
  83. if (err == IN_CORE_FIPS_E) {
  84. pr_err("In-core integrity hash check failure.\n"
  85. "Update verifyCore[] in fips_test.c with new hash \"%s\" and rebuild.\n",
  86. hash ? hash : "<null>");
  87. }
  88. }
  89. #endif
  90. #if defined(WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE) && !defined(CONFIG_MODULE_SIG)
  91. #error WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE requires a CONFIG_MODULE_SIG kernel.
  92. #endif
  93. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
  94. static int __init wolfssl_init(void)
  95. #else
  96. static int wolfssl_init(void)
  97. #endif
  98. {
  99. int ret;
  100. #if defined(CONFIG_MODULE_SIG_FORCE) || defined(WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE)
  101. if (THIS_MODULE->sig_ok == false) {
  102. pr_err("wolfSSL module load aborted -- bad or missing module signature with "
  103. #ifdef CONFIG_MODULE_SIG_FORCE
  104. "CONFIG_MODULE_SIG_FORCE kernel"
  105. #else
  106. "FIPS dynamic hash"
  107. #endif
  108. ".\n");
  109. return -ECANCELED;
  110. }
  111. #endif
  112. #ifdef USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE
  113. ret = set_up_wolfssl_linuxkm_pie_redirect_table();
  114. if (ret < 0)
  115. return ret;
  116. #endif
  117. #ifdef HAVE_LINUXKM_PIE_SUPPORT
  118. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
  119. #define THIS_MODULE_BASE (THIS_MODULE->core_layout.base)
  120. #define THIS_MODULE_TEXT_SIZE (THIS_MODULE->core_layout.text_size)
  121. #define THIS_MODULE_RO_SIZE (THIS_MODULE->core_layout.ro_size)
  122. #else
  123. #define THIS_MODULE_BASE (THIS_MODULE->module_core)
  124. #define THIS_MODULE_TEXT_SIZE (THIS_MODULE->core_text_size)
  125. #define THIS_MODULE_RO_SIZE (THIS_MODULE->core_ro_size)
  126. #endif
  127. {
  128. char *pie_text_start = (char *)wolfCrypt_PIE_first_function;
  129. char *pie_text_end = (char *)wolfCrypt_PIE_last_function;
  130. char *pie_rodata_start = (char *)wolfCrypt_PIE_rodata_start;
  131. char *pie_rodata_end = (char *)wolfCrypt_PIE_rodata_end;
  132. unsigned int text_hash, rodata_hash;
  133. if ((pie_text_start < pie_text_end) &&
  134. (pie_text_start >= (char *)THIS_MODULE_BASE) &&
  135. (pie_text_end - (char *)THIS_MODULE_BASE <= THIS_MODULE_TEXT_SIZE))
  136. {
  137. text_hash = hash_span(pie_text_start, pie_text_end);
  138. } else {
  139. pr_info("out-of-bounds PIE fenceposts! pie_text_start=%px pie_text_end=%px (span=%lu)"
  140. " core_layout.base=%px text_end=%px\n",
  141. pie_text_start,
  142. pie_text_end,
  143. pie_text_end-pie_text_start,
  144. THIS_MODULE_BASE,
  145. (char *)THIS_MODULE_BASE + THIS_MODULE_TEXT_SIZE);
  146. text_hash = 0;
  147. }
  148. if ((pie_rodata_start < pie_rodata_end) &&
  149. (pie_rodata_start >= (char *)THIS_MODULE_BASE + THIS_MODULE_TEXT_SIZE) &&
  150. (pie_rodata_end - (char *)THIS_MODULE_BASE <= THIS_MODULE_RO_SIZE))
  151. {
  152. rodata_hash = hash_span(pie_rodata_start, pie_rodata_end);
  153. } else {
  154. pr_info("out-of-bounds PIE fenceposts! pie_rodata_start=%px pie_rodata_end=%px (span=%lu)"
  155. " core_layout.base+core_layout.text_size=%px rodata_end=%px\n",
  156. pie_rodata_start,
  157. pie_rodata_end,
  158. pie_rodata_end-pie_rodata_start,
  159. (char *)THIS_MODULE_BASE + THIS_MODULE_TEXT_SIZE,
  160. (char *)THIS_MODULE_BASE + THIS_MODULE_RO_SIZE);
  161. rodata_hash = 0;
  162. }
  163. /* note, "%pK" conceals the actual layout information. "%px" exposes
  164. * the true module start address, which is potentially useful to an
  165. * attacker.
  166. */
  167. pr_info("wolfCrypt container hashes (spans): %x (%lu) %x (%lu), module base %pK\n",
  168. text_hash, pie_text_end-pie_text_start,
  169. rodata_hash, pie_rodata_end-pie_rodata_start,
  170. THIS_MODULE_BASE);
  171. }
  172. #endif /* HAVE_LINUXKM_PIE_SUPPORT */
  173. #ifdef HAVE_FIPS
  174. ret = wolfCrypt_SetCb_fips(lkmFipsCb);
  175. if (ret != 0) {
  176. pr_err("wolfCrypt_SetCb_fips() failed: %s\n", wc_GetErrorString(ret));
  177. return -ECANCELED;
  178. }
  179. fipsEntry();
  180. ret = wolfCrypt_GetStatus_fips();
  181. if (ret != 0) {
  182. pr_err("wolfCrypt_GetStatus_fips() failed: %s\n", wc_GetErrorString(ret));
  183. if (ret == IN_CORE_FIPS_E) {
  184. const char *newhash = wolfCrypt_GetCoreHash_fips();
  185. pr_err("Update verifyCore[] in fips_test.c with new hash \"%s\" and rebuild.\n",
  186. newhash ? newhash : "<null>");
  187. }
  188. return -ECANCELED;
  189. }
  190. pr_info("wolfCrypt FIPS ["
  191. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 3)
  192. "ready"
  193. #elif defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) \
  194. && defined(WOLFCRYPT_FIPS_RAND)
  195. "140-2 rand"
  196. #elif defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2)
  197. "140-2"
  198. #else
  199. "140"
  200. #endif
  201. "] POST succeeded.\n");
  202. #endif /* HAVE_FIPS */
  203. #ifdef WOLFCRYPT_ONLY
  204. ret = wolfCrypt_Init();
  205. if (ret != 0) {
  206. pr_err("wolfCrypt_Init() failed: %s\n", wc_GetErrorString(ret));
  207. return -ECANCELED;
  208. }
  209. #else
  210. ret = wolfSSL_Init();
  211. if (ret != WOLFSSL_SUCCESS) {
  212. pr_err("wolfSSL_Init() failed: %s\n", wc_GetErrorString(ret));
  213. return -ECANCELED;
  214. }
  215. #endif
  216. #ifndef NO_CRYPT_TEST
  217. #ifdef WC_RNG_SEED_CB
  218. ret = wc_SetSeed_Cb(wc_GenerateSeed);
  219. if (ret == 0)
  220. #endif
  221. {
  222. ret = wolfcrypt_test(NULL);
  223. }
  224. if (ret < 0) {
  225. pr_err("wolfcrypt self-test failed with return code %d.\n", ret);
  226. (void)libwolfssl_cleanup();
  227. msleep(10);
  228. return -ECANCELED;
  229. }
  230. pr_info("wolfCrypt self-test passed.\n");
  231. #endif
  232. #ifdef WOLFCRYPT_ONLY
  233. pr_info("wolfCrypt " LIBWOLFSSL_VERSION_STRING " loaded%s"
  234. ".\nSee https://www.wolfssl.com/ for more information.\n"
  235. "wolfCrypt Copyright (C) 2006-present wolfSSL Inc. Licensed under " WOLFSSL_LICENSE ".\n",
  236. #ifdef CONFIG_MODULE_SIG
  237. THIS_MODULE->sig_ok ? " with valid module signature" : " without valid module signature"
  238. #else
  239. ""
  240. #endif
  241. );
  242. #else
  243. pr_info("wolfSSL " LIBWOLFSSL_VERSION_STRING " loaded%s"
  244. ".\nSee https://www.wolfssl.com/ for more information.\n"
  245. "wolfSSL Copyright (C) 2006-present wolfSSL Inc. Licensed under " WOLFSSL_LICENSE ".\n",
  246. #ifdef CONFIG_MODULE_SIG
  247. THIS_MODULE->sig_ok ? " with valid module signature" : " without valid module signature"
  248. #else
  249. ""
  250. #endif
  251. );
  252. #endif
  253. return 0;
  254. }
  255. module_init(wolfssl_init);
  256. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
  257. static void __exit wolfssl_exit(void)
  258. #else
  259. static void wolfssl_exit(void)
  260. #endif
  261. {
  262. (void)libwolfssl_cleanup();
  263. return;
  264. }
  265. module_exit(wolfssl_exit);
  266. MODULE_LICENSE(WOLFSSL_LICENSE);
  267. MODULE_AUTHOR("https://www.wolfssl.com/");
  268. MODULE_DESCRIPTION("libwolfssl cryptographic and protocol facilities");
  269. MODULE_VERSION(LIBWOLFSSL_VERSION_STRING);
  270. #ifdef USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE
  271. /* get_current() is an inline or macro, depending on the target -- sidestep the whole issue with a wrapper func. */
  272. static struct task_struct *my_get_current_thread(void) {
  273. return get_current();
  274. }
  275. /* ditto for preempt_count(). */
  276. static int my_preempt_count(void) {
  277. return preempt_count();
  278. }
  279. #if defined(WOLFSSL_LINUXKM_SIMD_X86_IRQ_ALLOWED) && (LINUX_VERSION_CODE < KERNEL_VERSION(5, 14, 0))
  280. static int my_copy_fpregs_to_fpstate(struct fpu *fpu) {
  281. return copy_fpregs_to_fpstate(fpu);
  282. }
  283. static void my_copy_kernel_to_fpregs(union fpregs_state *fpstate) {
  284. copy_kernel_to_fpregs(fpstate);
  285. }
  286. #endif
  287. static int set_up_wolfssl_linuxkm_pie_redirect_table(void) {
  288. memset(
  289. &wolfssl_linuxkm_pie_redirect_table,
  290. 0,
  291. sizeof wolfssl_linuxkm_pie_redirect_table);
  292. #ifndef __ARCH_MEMCMP_NO_REDIRECT
  293. wolfssl_linuxkm_pie_redirect_table.memcmp = memcmp;
  294. #endif
  295. #ifndef __ARCH_MEMCPY_NO_REDIRECT
  296. wolfssl_linuxkm_pie_redirect_table.memcpy = memcpy;
  297. #endif
  298. #ifndef __ARCH_MEMSET_NO_REDIRECT
  299. wolfssl_linuxkm_pie_redirect_table.memset = memset;
  300. #endif
  301. #ifndef __ARCH_MEMMOVE_NO_REDIRECT
  302. wolfssl_linuxkm_pie_redirect_table.memmove = memmove;
  303. #endif
  304. #ifndef __ARCH_STRNCMP_NO_REDIRECT
  305. wolfssl_linuxkm_pie_redirect_table.strncmp = strncmp;
  306. #endif
  307. #ifndef __ARCH_STRLEN_NO_REDIRECT
  308. wolfssl_linuxkm_pie_redirect_table.strlen = strlen;
  309. #endif
  310. #ifndef __ARCH_STRSTR_NO_REDIRECT
  311. wolfssl_linuxkm_pie_redirect_table.strstr = strstr;
  312. #endif
  313. #ifndef __ARCH_STRNCPY_NO_REDIRECT
  314. wolfssl_linuxkm_pie_redirect_table.strncpy = strncpy;
  315. #endif
  316. #ifndef __ARCH_STRNCAT_NO_REDIRECT
  317. wolfssl_linuxkm_pie_redirect_table.strncat = strncat;
  318. #endif
  319. #ifndef __ARCH_STRNCASECMP_NO_REDIRECT
  320. wolfssl_linuxkm_pie_redirect_table.strncasecmp = strncasecmp;
  321. #endif
  322. wolfssl_linuxkm_pie_redirect_table.kstrtoll = kstrtoll;
  323. wolfssl_linuxkm_pie_redirect_table.printk = printk;
  324. wolfssl_linuxkm_pie_redirect_table.snprintf = snprintf;
  325. wolfssl_linuxkm_pie_redirect_table._ctype = _ctype;
  326. wolfssl_linuxkm_pie_redirect_table.kmalloc = kmalloc;
  327. wolfssl_linuxkm_pie_redirect_table.kfree = kfree;
  328. wolfssl_linuxkm_pie_redirect_table.ksize = ksize;
  329. wolfssl_linuxkm_pie_redirect_table.krealloc = krealloc;
  330. #ifdef HAVE_KVMALLOC
  331. wolfssl_linuxkm_pie_redirect_table.kvmalloc_node = kvmalloc_node;
  332. wolfssl_linuxkm_pie_redirect_table.kvfree = kvfree;
  333. #endif
  334. wolfssl_linuxkm_pie_redirect_table.is_vmalloc_addr = is_vmalloc_addr;
  335. wolfssl_linuxkm_pie_redirect_table.kmem_cache_alloc_trace =
  336. kmem_cache_alloc_trace;
  337. wolfssl_linuxkm_pie_redirect_table.kmalloc_order_trace =
  338. kmalloc_order_trace;
  339. wolfssl_linuxkm_pie_redirect_table.get_random_bytes = get_random_bytes;
  340. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  341. wolfssl_linuxkm_pie_redirect_table.getnstimeofday =
  342. getnstimeofday;
  343. #elif LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
  344. wolfssl_linuxkm_pie_redirect_table.current_kernel_time64 =
  345. current_kernel_time64;
  346. #else
  347. wolfssl_linuxkm_pie_redirect_table.ktime_get_coarse_real_ts64 =
  348. ktime_get_coarse_real_ts64;
  349. #endif
  350. wolfssl_linuxkm_pie_redirect_table.get_current = my_get_current_thread;
  351. wolfssl_linuxkm_pie_redirect_table.preempt_count = my_preempt_count;
  352. #ifdef WOLFSSL_LINUXKM_SIMD_X86
  353. wolfssl_linuxkm_pie_redirect_table.irq_fpu_usable = irq_fpu_usable;
  354. #ifdef kernel_fpu_begin
  355. wolfssl_linuxkm_pie_redirect_table.kernel_fpu_begin_mask =
  356. kernel_fpu_begin_mask;
  357. #else
  358. wolfssl_linuxkm_pie_redirect_table.kernel_fpu_begin =
  359. kernel_fpu_begin;
  360. #endif
  361. wolfssl_linuxkm_pie_redirect_table.kernel_fpu_end = kernel_fpu_end;
  362. #ifdef WOLFSSL_LINUXKM_SIMD_X86_IRQ_ALLOWED
  363. #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 14, 0)
  364. wolfssl_linuxkm_pie_redirect_table.copy_fpregs_to_fpstate = my_copy_fpregs_to_fpstate;
  365. wolfssl_linuxkm_pie_redirect_table.copy_kernel_to_fpregs = my_copy_kernel_to_fpregs;
  366. #else
  367. wolfssl_linuxkm_pie_redirect_table.save_fpregs_to_fpstate = save_fpregs_to_fpstate;
  368. wolfssl_linuxkm_pie_redirect_table.__restore_fpregs_from_fpstate = __restore_fpregs_from_fpstate;
  369. wolfssl_linuxkm_pie_redirect_table.xfeatures_mask_all = &xfeatures_mask_all;
  370. #endif
  371. wolfssl_linuxkm_pie_redirect_table.cpu_number = &cpu_number;
  372. wolfssl_linuxkm_pie_redirect_table.nr_cpu_ids = &nr_cpu_ids;
  373. #endif /* WOLFSSL_LINUXKM_SIMD_X86_IRQ_ALLOWED */
  374. #endif
  375. wolfssl_linuxkm_pie_redirect_table.__mutex_init = __mutex_init;
  376. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  377. wolfssl_linuxkm_pie_redirect_table.mutex_lock_nested = mutex_lock_nested;
  378. #else
  379. wolfssl_linuxkm_pie_redirect_table.mutex_lock = mutex_lock;
  380. #endif
  381. wolfssl_linuxkm_pie_redirect_table.mutex_unlock = mutex_unlock;
  382. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  383. wolfssl_linuxkm_pie_redirect_table.mutex_destroy = mutex_destroy;
  384. #endif
  385. #ifdef HAVE_FIPS
  386. wolfssl_linuxkm_pie_redirect_table.wolfCrypt_FIPS_first =
  387. wolfCrypt_FIPS_first;
  388. wolfssl_linuxkm_pie_redirect_table.wolfCrypt_FIPS_last =
  389. wolfCrypt_FIPS_last;
  390. #endif
  391. #if !defined(WOLFCRYPT_ONLY) && !defined(NO_CERTS)
  392. wolfssl_linuxkm_pie_redirect_table.GetCA = GetCA;
  393. #ifndef NO_SKID
  394. wolfssl_linuxkm_pie_redirect_table.GetCAByName = GetCAByName;
  395. #endif
  396. #endif
  397. /* runtime assert that the table has no null slots after initialization. */
  398. {
  399. unsigned long *i;
  400. for (i = (unsigned long *)&wolfssl_linuxkm_pie_redirect_table;
  401. i < (unsigned long *)&wolfssl_linuxkm_pie_redirect_table._last_slot;
  402. ++i)
  403. if (*i == 0) {
  404. pr_err("wolfCrypt container redirect table initialization was incomplete.\n");
  405. return -EFAULT;
  406. }
  407. }
  408. return 0;
  409. }
  410. #endif /* USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE */