module_hooks.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /* module_hooks.c -- module load/unload hooks for libwolfssl.ko
  2. *
  3. * Copyright (C) 2006-2023 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. #define WOLFSSL_NEED_LINUX_CURRENT
  26. #ifdef HAVE_CONFIG_H
  27. #include <config.h>
  28. #endif
  29. #include <wolfssl/wolfcrypt/settings.h>
  30. #include <wolfssl/wolfcrypt/error-crypt.h>
  31. #ifdef WOLFCRYPT_ONLY
  32. #include <wolfssl/version.h>
  33. #else
  34. #include <wolfssl/ssl.h>
  35. #endif
  36. #ifdef HAVE_FIPS
  37. #include <wolfssl/wolfcrypt/fips_test.h>
  38. #endif
  39. #ifndef NO_CRYPT_TEST
  40. #include <wolfcrypt/test/test.h>
  41. #include <linux/delay.h>
  42. #endif
  43. static int libwolfssl_cleanup(void) {
  44. int ret;
  45. #ifdef WOLFCRYPT_ONLY
  46. ret = wolfCrypt_Cleanup();
  47. if (ret != 0)
  48. pr_err("wolfCrypt_Cleanup() failed: %s\n", wc_GetErrorString(ret));
  49. else
  50. pr_info("wolfCrypt " LIBWOLFSSL_VERSION_STRING " cleanup complete.\n");
  51. #else
  52. ret = wolfSSL_Cleanup();
  53. if (ret != WOLFSSL_SUCCESS)
  54. pr_err("wolfSSL_Cleanup() failed: %s\n", wc_GetErrorString(ret));
  55. else
  56. pr_info("wolfSSL " LIBWOLFSSL_VERSION_STRING " cleanup complete.\n");
  57. #endif
  58. return ret;
  59. }
  60. #ifdef HAVE_LINUXKM_PIE_SUPPORT
  61. extern int wolfCrypt_PIE_first_function(void);
  62. extern int wolfCrypt_PIE_last_function(void);
  63. extern const unsigned int wolfCrypt_PIE_rodata_start[];
  64. extern const unsigned int wolfCrypt_PIE_rodata_end[];
  65. /* cheap portable ad-hoc hash function to confirm bitwise stability of the PIE
  66. * binary image.
  67. */
  68. static unsigned int hash_span(char *start, char *end) {
  69. unsigned int sum = 1;
  70. while (start < end) {
  71. unsigned int rotate_by;
  72. sum ^= *start++;
  73. rotate_by = (sum ^ (sum >> 5)) & 31;
  74. sum = (sum << rotate_by) | (sum >> (32 - rotate_by));
  75. }
  76. return sum;
  77. }
  78. #ifdef USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE
  79. extern struct wolfssl_linuxkm_pie_redirect_table wolfssl_linuxkm_pie_redirect_table;
  80. static int set_up_wolfssl_linuxkm_pie_redirect_table(void);
  81. #endif /* USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE */
  82. #endif /* HAVE_LINUXKM_PIE_SUPPORT */
  83. #ifdef HAVE_FIPS
  84. static void lkmFipsCb(int ok, int err, const char* hash)
  85. {
  86. if ((! ok) || (err != 0))
  87. pr_err("libwolfssl FIPS error: %s\n", wc_GetErrorString(err));
  88. if (err == IN_CORE_FIPS_E) {
  89. pr_err("In-core integrity hash check failure.\n"
  90. "Update verifyCore[] in fips_test.c with new hash \"%s\" and rebuild.\n",
  91. hash ? hash : "<null>");
  92. }
  93. }
  94. #endif
  95. #ifdef WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE
  96. #ifndef CONFIG_MODULE_SIG
  97. #error WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE requires a CONFIG_MODULE_SIG kernel.
  98. #endif
  99. static int updateFipsHash(void);
  100. #endif
  101. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
  102. static int __init wolfssl_init(void)
  103. #else
  104. static int wolfssl_init(void)
  105. #endif
  106. {
  107. int ret;
  108. #ifdef WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE
  109. if (THIS_MODULE->sig_ok == false) {
  110. pr_err("wolfSSL module load aborted -- bad or missing module signature with FIPS dynamic hash.\n");
  111. return -ECANCELED;
  112. }
  113. ret = updateFipsHash();
  114. if (ret < 0) {
  115. pr_err("wolfSSL module load aborted -- updateFipsHash: %s\n",wc_GetErrorString(ret));
  116. return -ECANCELED;
  117. }
  118. #endif
  119. #ifdef USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE
  120. ret = set_up_wolfssl_linuxkm_pie_redirect_table();
  121. if (ret < 0)
  122. return ret;
  123. #endif
  124. #ifdef HAVE_LINUXKM_PIE_SUPPORT
  125. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
  126. #define THIS_MODULE_BASE (THIS_MODULE->core_layout.base)
  127. #define THIS_MODULE_TEXT_SIZE (THIS_MODULE->core_layout.text_size)
  128. #define THIS_MODULE_RO_SIZE (THIS_MODULE->core_layout.ro_size)
  129. #else
  130. #define THIS_MODULE_BASE (THIS_MODULE->module_core)
  131. #define THIS_MODULE_TEXT_SIZE (THIS_MODULE->core_text_size)
  132. #define THIS_MODULE_RO_SIZE (THIS_MODULE->core_ro_size)
  133. #endif
  134. {
  135. char *pie_text_start = (char *)wolfCrypt_PIE_first_function;
  136. char *pie_text_end = (char *)wolfCrypt_PIE_last_function;
  137. char *pie_rodata_start = (char *)wolfCrypt_PIE_rodata_start;
  138. char *pie_rodata_end = (char *)wolfCrypt_PIE_rodata_end;
  139. unsigned int text_hash, rodata_hash;
  140. if ((pie_text_start < pie_text_end) &&
  141. (pie_text_start >= (char *)THIS_MODULE_BASE) &&
  142. (pie_text_end - (char *)THIS_MODULE_BASE <= THIS_MODULE_TEXT_SIZE))
  143. {
  144. text_hash = hash_span(pie_text_start, pie_text_end);
  145. } else {
  146. pr_info("out-of-bounds PIE fenceposts! pie_text_start=%px pie_text_end=%px (span=%lu)"
  147. " core_layout.base=%px text_end=%px\n",
  148. pie_text_start,
  149. pie_text_end,
  150. pie_text_end-pie_text_start,
  151. THIS_MODULE_BASE,
  152. (char *)THIS_MODULE_BASE + THIS_MODULE_TEXT_SIZE);
  153. text_hash = 0;
  154. }
  155. if ((pie_rodata_start < pie_rodata_end) && // cppcheck-suppress comparePointers
  156. (pie_rodata_start >= (char *)THIS_MODULE_BASE + THIS_MODULE_TEXT_SIZE) &&
  157. (pie_rodata_end - (char *)THIS_MODULE_BASE <= THIS_MODULE_RO_SIZE))
  158. {
  159. rodata_hash = hash_span(pie_rodata_start, pie_rodata_end);
  160. } else {
  161. pr_info("out-of-bounds PIE fenceposts! pie_rodata_start=%px pie_rodata_end=%px (span=%lu)"
  162. " core_layout.base+core_layout.text_size=%px rodata_end=%px\n",
  163. pie_rodata_start,
  164. pie_rodata_end,
  165. pie_rodata_end-pie_rodata_start,
  166. (char *)THIS_MODULE_BASE + THIS_MODULE_TEXT_SIZE,
  167. (char *)THIS_MODULE_BASE + THIS_MODULE_RO_SIZE);
  168. rodata_hash = 0;
  169. }
  170. /* note, "%pK" conceals the actual layout information. "%px" exposes
  171. * the true module start address, which is potentially useful to an
  172. * attacker.
  173. */
  174. pr_info("wolfCrypt container hashes (spans): %x (%lu) %x (%lu), module base %pK\n",
  175. text_hash, pie_text_end-pie_text_start,
  176. rodata_hash, pie_rodata_end-pie_rodata_start,
  177. THIS_MODULE_BASE);
  178. }
  179. #endif /* HAVE_LINUXKM_PIE_SUPPORT */
  180. #ifdef HAVE_FIPS
  181. ret = wolfCrypt_SetCb_fips(lkmFipsCb);
  182. if (ret != 0) {
  183. pr_err("wolfCrypt_SetCb_fips() failed: %s\n", wc_GetErrorString(ret));
  184. return -ECANCELED;
  185. }
  186. fipsEntry();
  187. ret = wolfCrypt_GetStatus_fips();
  188. if (ret != 0) {
  189. pr_err("wolfCrypt_GetStatus_fips() failed: %s\n", wc_GetErrorString(ret));
  190. if (ret == IN_CORE_FIPS_E) {
  191. const char *newhash = wolfCrypt_GetCoreHash_fips();
  192. pr_err("Update verifyCore[] in fips_test.c with new hash \"%s\" and rebuild.\n",
  193. newhash ? newhash : "<null>");
  194. }
  195. return -ECANCELED;
  196. }
  197. pr_info("wolfCrypt FIPS ["
  198. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 3)
  199. "ready"
  200. #elif defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) \
  201. && defined(WOLFCRYPT_FIPS_RAND)
  202. "140-2 rand"
  203. #elif defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2)
  204. "140-2"
  205. #else
  206. "140"
  207. #endif
  208. "] POST succeeded.\n");
  209. #endif /* HAVE_FIPS */
  210. #ifdef WOLFCRYPT_ONLY
  211. ret = wolfCrypt_Init();
  212. if (ret != 0) {
  213. pr_err("wolfCrypt_Init() failed: %s\n", wc_GetErrorString(ret));
  214. return -ECANCELED;
  215. }
  216. #else
  217. ret = wolfSSL_Init();
  218. if (ret != WOLFSSL_SUCCESS) {
  219. pr_err("wolfSSL_Init() failed: %s\n", wc_GetErrorString(ret));
  220. return -ECANCELED;
  221. }
  222. #endif
  223. #ifndef NO_CRYPT_TEST
  224. #ifdef WC_RNG_SEED_CB
  225. ret = wc_SetSeed_Cb(wc_GenerateSeed);
  226. if (ret == 0)
  227. #endif
  228. {
  229. ret = wolfcrypt_test(NULL);
  230. }
  231. if (ret < 0) {
  232. pr_err("wolfcrypt self-test failed with return code %d.\n", ret);
  233. (void)libwolfssl_cleanup();
  234. msleep(10);
  235. return -ECANCELED;
  236. }
  237. pr_info("wolfCrypt self-test passed.\n");
  238. #endif
  239. #ifdef WOLFCRYPT_ONLY
  240. pr_info("wolfCrypt " LIBWOLFSSL_VERSION_STRING " loaded%s"
  241. ".\nSee https://www.wolfssl.com/ for more information.\n"
  242. "wolfCrypt Copyright (C) 2006-present wolfSSL Inc. Licensed under " WOLFSSL_LICENSE ".\n",
  243. #ifdef CONFIG_MODULE_SIG
  244. THIS_MODULE->sig_ok ? " with valid module signature" : " without valid module signature"
  245. #else
  246. ""
  247. #endif
  248. );
  249. #else
  250. pr_info("wolfSSL " LIBWOLFSSL_VERSION_STRING " loaded%s"
  251. ".\nSee https://www.wolfssl.com/ for more information.\n"
  252. "wolfSSL Copyright (C) 2006-present wolfSSL Inc. Licensed under " WOLFSSL_LICENSE ".\n",
  253. #ifdef CONFIG_MODULE_SIG
  254. THIS_MODULE->sig_ok ? " with valid module signature" : " without valid module signature"
  255. #else
  256. ""
  257. #endif
  258. );
  259. #endif
  260. return 0;
  261. }
  262. module_init(wolfssl_init);
  263. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
  264. static void __exit wolfssl_exit(void)
  265. #else
  266. static void wolfssl_exit(void)
  267. #endif
  268. {
  269. (void)libwolfssl_cleanup();
  270. return;
  271. }
  272. module_exit(wolfssl_exit);
  273. MODULE_LICENSE(WOLFSSL_LICENSE);
  274. MODULE_AUTHOR("https://www.wolfssl.com/");
  275. MODULE_DESCRIPTION("libwolfssl cryptographic and protocol facilities");
  276. MODULE_VERSION(LIBWOLFSSL_VERSION_STRING);
  277. #ifdef USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE
  278. /* get_current() is an inline or macro, depending on the target -- sidestep the whole issue with a wrapper func. */
  279. static struct task_struct *my_get_current_thread(void) {
  280. return get_current();
  281. }
  282. /* ditto for preempt_count(). */
  283. static int my_preempt_count(void) {
  284. return preempt_count();
  285. }
  286. #if defined(WOLFSSL_LINUXKM_SIMD_X86) && (LINUX_VERSION_CODE < KERNEL_VERSION(5, 14, 0))
  287. static int my_copy_fpregs_to_fpstate(struct fpu *fpu) {
  288. return copy_fpregs_to_fpstate(fpu);
  289. }
  290. static void my_copy_kernel_to_fpregs(union fpregs_state *fpstate) {
  291. copy_kernel_to_fpregs(fpstate);
  292. }
  293. #endif
  294. static int set_up_wolfssl_linuxkm_pie_redirect_table(void) {
  295. memset(
  296. &wolfssl_linuxkm_pie_redirect_table,
  297. 0,
  298. sizeof wolfssl_linuxkm_pie_redirect_table);
  299. #ifndef __ARCH_MEMCMP_NO_REDIRECT
  300. wolfssl_linuxkm_pie_redirect_table.memcmp = memcmp;
  301. #endif
  302. #ifndef __ARCH_MEMCPY_NO_REDIRECT
  303. wolfssl_linuxkm_pie_redirect_table.memcpy = memcpy;
  304. #endif
  305. #ifndef __ARCH_MEMSET_NO_REDIRECT
  306. wolfssl_linuxkm_pie_redirect_table.memset = memset;
  307. #endif
  308. #ifndef __ARCH_MEMMOVE_NO_REDIRECT
  309. wolfssl_linuxkm_pie_redirect_table.memmove = memmove;
  310. #endif
  311. #ifndef __ARCH_STRCMP_NO_REDIRECT
  312. wolfssl_linuxkm_pie_redirect_table.strcmp = strcmp;
  313. #endif
  314. #ifndef __ARCH_STRNCMP_NO_REDIRECT
  315. wolfssl_linuxkm_pie_redirect_table.strncmp = strncmp;
  316. #endif
  317. #ifndef __ARCH_STRCASECMP_NO_REDIRECT
  318. wolfssl_linuxkm_pie_redirect_table.strcasecmp = strcasecmp;
  319. #endif
  320. #ifndef __ARCH_STRNCASECMP_NO_REDIRECT
  321. wolfssl_linuxkm_pie_redirect_table.strncasecmp = strncasecmp;
  322. #endif
  323. #ifndef __ARCH_STRLEN_NO_REDIRECT
  324. wolfssl_linuxkm_pie_redirect_table.strlen = strlen;
  325. #endif
  326. #ifndef __ARCH_STRSTR_NO_REDIRECT
  327. wolfssl_linuxkm_pie_redirect_table.strstr = strstr;
  328. #endif
  329. #ifndef __ARCH_STRNCPY_NO_REDIRECT
  330. wolfssl_linuxkm_pie_redirect_table.strncpy = strncpy;
  331. #endif
  332. #ifndef __ARCH_STRNCAT_NO_REDIRECT
  333. wolfssl_linuxkm_pie_redirect_table.strncat = strncat;
  334. #endif
  335. wolfssl_linuxkm_pie_redirect_table.kstrtoll = kstrtoll;
  336. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)
  337. wolfssl_linuxkm_pie_redirect_table._printk = _printk;
  338. #else
  339. wolfssl_linuxkm_pie_redirect_table.printk = printk;
  340. #endif
  341. wolfssl_linuxkm_pie_redirect_table.snprintf = snprintf;
  342. wolfssl_linuxkm_pie_redirect_table._ctype = _ctype;
  343. wolfssl_linuxkm_pie_redirect_table.kmalloc = kmalloc;
  344. wolfssl_linuxkm_pie_redirect_table.kfree = kfree;
  345. wolfssl_linuxkm_pie_redirect_table.ksize = ksize;
  346. wolfssl_linuxkm_pie_redirect_table.krealloc = krealloc;
  347. #ifdef HAVE_KVMALLOC
  348. wolfssl_linuxkm_pie_redirect_table.kvmalloc_node = kvmalloc_node;
  349. wolfssl_linuxkm_pie_redirect_table.kvfree = kvfree;
  350. #endif
  351. wolfssl_linuxkm_pie_redirect_table.is_vmalloc_addr = is_vmalloc_addr;
  352. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
  353. wolfssl_linuxkm_pie_redirect_table.kmalloc_trace =
  354. kmalloc_trace;
  355. #else
  356. wolfssl_linuxkm_pie_redirect_table.kmem_cache_alloc_trace =
  357. kmem_cache_alloc_trace;
  358. wolfssl_linuxkm_pie_redirect_table.kmalloc_order_trace =
  359. kmalloc_order_trace;
  360. #endif
  361. wolfssl_linuxkm_pie_redirect_table.get_random_bytes = get_random_bytes;
  362. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  363. wolfssl_linuxkm_pie_redirect_table.getnstimeofday =
  364. getnstimeofday;
  365. #elif LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
  366. wolfssl_linuxkm_pie_redirect_table.current_kernel_time64 =
  367. current_kernel_time64;
  368. #else
  369. wolfssl_linuxkm_pie_redirect_table.ktime_get_coarse_real_ts64 =
  370. ktime_get_coarse_real_ts64;
  371. #endif
  372. wolfssl_linuxkm_pie_redirect_table.get_current = my_get_current_thread;
  373. wolfssl_linuxkm_pie_redirect_table.preempt_count = my_preempt_count;
  374. #ifdef WOLFSSL_LINUXKM_SIMD_X86
  375. wolfssl_linuxkm_pie_redirect_table.irq_fpu_usable = irq_fpu_usable;
  376. #ifdef kernel_fpu_begin
  377. wolfssl_linuxkm_pie_redirect_table.kernel_fpu_begin_mask =
  378. kernel_fpu_begin_mask;
  379. #else
  380. wolfssl_linuxkm_pie_redirect_table.kernel_fpu_begin =
  381. kernel_fpu_begin;
  382. #endif
  383. wolfssl_linuxkm_pie_redirect_table.kernel_fpu_end = kernel_fpu_end;
  384. #ifdef LINUXKM_SIMD_IRQ
  385. #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 14, 0)
  386. wolfssl_linuxkm_pie_redirect_table.copy_fpregs_to_fpstate = my_copy_fpregs_to_fpstate;
  387. wolfssl_linuxkm_pie_redirect_table.copy_kernel_to_fpregs = my_copy_kernel_to_fpregs;
  388. #elif LINUX_VERSION_CODE < KERNEL_VERSION(5, 16, 0)
  389. wolfssl_linuxkm_pie_redirect_table.save_fpregs_to_fpstate = save_fpregs_to_fpstate;
  390. wolfssl_linuxkm_pie_redirect_table.__restore_fpregs_from_fpstate = __restore_fpregs_from_fpstate;
  391. wolfssl_linuxkm_pie_redirect_table.xfeatures_mask_all = &xfeatures_mask_all;
  392. /*
  393. * #else
  394. * wolfssl_linuxkm_pie_redirect_table.save_fpregs_to_fpstate = save_fpregs_to_fpstate;
  395. * wolfssl_linuxkm_pie_redirect_table.restore_fpregs_from_fpstate = restore_fpregs_from_fpstate;
  396. * wolfssl_linuxkm_pie_redirect_table.fpu_kernel_cfg = &fpu_kernel_cfg;
  397. */
  398. #endif
  399. #endif
  400. #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 2, 0)
  401. wolfssl_linuxkm_pie_redirect_table.cpu_number = &cpu_number;
  402. #else
  403. wolfssl_linuxkm_pie_redirect_table.pcpu_hot = &pcpu_hot;
  404. #endif
  405. wolfssl_linuxkm_pie_redirect_table.nr_cpu_ids = &nr_cpu_ids;
  406. #endif
  407. wolfssl_linuxkm_pie_redirect_table.__mutex_init = __mutex_init;
  408. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  409. wolfssl_linuxkm_pie_redirect_table.mutex_lock_nested = mutex_lock_nested;
  410. #else
  411. wolfssl_linuxkm_pie_redirect_table.mutex_lock = mutex_lock;
  412. #endif
  413. wolfssl_linuxkm_pie_redirect_table.mutex_unlock = mutex_unlock;
  414. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  415. wolfssl_linuxkm_pie_redirect_table.mutex_destroy = mutex_destroy;
  416. #endif
  417. #ifdef HAVE_FIPS
  418. wolfssl_linuxkm_pie_redirect_table.wolfCrypt_FIPS_first =
  419. wolfCrypt_FIPS_first;
  420. wolfssl_linuxkm_pie_redirect_table.wolfCrypt_FIPS_last =
  421. wolfCrypt_FIPS_last;
  422. #endif
  423. #if !defined(WOLFCRYPT_ONLY) && !defined(NO_CERTS)
  424. wolfssl_linuxkm_pie_redirect_table.GetCA = GetCA;
  425. #ifndef NO_SKID
  426. wolfssl_linuxkm_pie_redirect_table.GetCAByName = GetCAByName;
  427. #endif
  428. #endif
  429. /* runtime assert that the table has no null slots after initialization. */
  430. {
  431. unsigned long *i;
  432. for (i = (unsigned long *)&wolfssl_linuxkm_pie_redirect_table;
  433. i < (unsigned long *)&wolfssl_linuxkm_pie_redirect_table._last_slot;
  434. ++i)
  435. if (*i == 0) {
  436. pr_err("wolfCrypt container redirect table initialization was incomplete.\n");
  437. return -EFAULT;
  438. }
  439. }
  440. return 0;
  441. }
  442. #endif /* USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE */
  443. #ifdef WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE
  444. #include <wolfssl/wolfcrypt/coding.h>
  445. PRAGMA_GCC_DIAG_PUSH;
  446. PRAGMA_GCC("GCC diagnostic ignored \"-Wnested-externs\"");
  447. PRAGMA_GCC("GCC diagnostic ignored \"-Wpointer-arith\"");
  448. #include <crypto/hash.h>
  449. PRAGMA_GCC_DIAG_POP;
  450. extern char verifyCore[WC_SHA256_DIGEST_SIZE*2 + 1];
  451. extern const char coreKey[WC_SHA256_DIGEST_SIZE*2 + 1];
  452. extern const unsigned int wolfCrypt_FIPS_ro_start[];
  453. extern const unsigned int wolfCrypt_FIPS_ro_end[];
  454. #define FIPS_IN_CORE_KEY_SZ 32
  455. #define FIPS_IN_CORE_VERIFY_SZ FIPS_IN_CORE_KEY_SZ
  456. typedef int (*fips_address_function)(void);
  457. #define MAX_FIPS_DATA_SZ 100000
  458. #define MAX_FIPS_CODE_SZ 1000000
  459. extern int GenBase16_Hash(const byte* in, int length, char* out, int outSz);
  460. static int updateFipsHash(void)
  461. {
  462. struct crypto_shash *tfm = NULL;
  463. struct shash_desc *desc = NULL;
  464. word32 verifySz = FIPS_IN_CORE_VERIFY_SZ;
  465. word32 binCoreSz = FIPS_IN_CORE_KEY_SZ;
  466. int ret;
  467. byte *hash = NULL;
  468. char *base16_hash = NULL;
  469. byte *binCoreKey = NULL;
  470. byte *binVerify = NULL;
  471. fips_address_function first = wolfCrypt_FIPS_first;
  472. fips_address_function last = wolfCrypt_FIPS_last;
  473. char* start = (char*)wolfCrypt_FIPS_ro_start;
  474. char* end = (char*)wolfCrypt_FIPS_ro_end;
  475. unsigned long code_sz = (unsigned long)last - (unsigned long)first;
  476. unsigned long data_sz = (unsigned long)end - (unsigned long)start;
  477. if (data_sz == 0 || data_sz > MAX_FIPS_DATA_SZ)
  478. return BAD_FUNC_ARG; /* bad fips data size */
  479. if (code_sz == 0 || code_sz > MAX_FIPS_CODE_SZ)
  480. return BAD_FUNC_ARG; /* bad fips code size */
  481. hash = XMALLOC(WC_SHA256_DIGEST_SIZE, 0, DYNAMIC_TYPE_TMP_BUFFER);
  482. if (hash == NULL) {
  483. ret = MEMORY_E;
  484. goto out;
  485. }
  486. base16_hash = XMALLOC(WC_SHA256_DIGEST_SIZE*2 + 1, 0, DYNAMIC_TYPE_TMP_BUFFER);
  487. if (base16_hash == NULL) {
  488. ret = MEMORY_E;
  489. goto out;
  490. }
  491. binCoreKey = XMALLOC(binCoreSz, 0, DYNAMIC_TYPE_TMP_BUFFER);
  492. if (binCoreKey == NULL) {
  493. ret = MEMORY_E;
  494. goto out;
  495. }
  496. binVerify = XMALLOC(verifySz, 0, DYNAMIC_TYPE_TMP_BUFFER);
  497. if (binVerify == NULL) {
  498. ret = MEMORY_E;
  499. goto out;
  500. }
  501. {
  502. word32 base16_out_len = binCoreSz;
  503. ret = Base16_Decode((const byte *)coreKey, sizeof coreKey - 1, binCoreKey, &base16_out_len);
  504. if (ret != 0) {
  505. pr_err("Base16_Decode for coreKey: %s\n", wc_GetErrorString(ret));
  506. goto out;
  507. }
  508. if (base16_out_len != binCoreSz) {
  509. pr_err("unexpected output length %u for coreKey from Base16_Decode.\n",base16_out_len);
  510. ret = BAD_STATE_E;
  511. goto out;
  512. }
  513. }
  514. tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
  515. if (IS_ERR(tfm)) {
  516. if (PTR_ERR(tfm) == -ENOMEM) {
  517. pr_err("crypto_alloc_shash failed: out of memory\n");
  518. ret = MEMORY_E;
  519. } else if (PTR_ERR(tfm) == -ENOENT) {
  520. pr_err("crypto_alloc_shash failed: kernel is missing hmac(sha256) implementation\n");
  521. pr_err("check for CONFIG_CRYPTO_SHA256 and CONFIG_CRYPTO_HMAC.\n");
  522. ret = NOT_COMPILED_IN;
  523. } else {
  524. pr_err("crypto_alloc_shash failed with ret %ld\n",PTR_ERR(tfm));
  525. ret = HASH_TYPE_E;
  526. }
  527. tfm = NULL;
  528. goto out;
  529. }
  530. {
  531. size_t desc_size = crypto_shash_descsize(tfm) + sizeof *desc;
  532. desc = XMALLOC(desc_size, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  533. if (desc == NULL) {
  534. pr_err("failed allocating desc.");
  535. ret = MEMORY_E;
  536. goto out;
  537. }
  538. XMEMSET(desc, 0, desc_size);
  539. }
  540. ret = crypto_shash_setkey(tfm, binCoreKey, binCoreSz);
  541. if (ret) {
  542. pr_err("crypto_ahash_setkey failed: err %d\n", ret);
  543. ret = BAD_STATE_E;
  544. goto out;
  545. }
  546. desc->tfm = tfm;
  547. ret = crypto_shash_init(desc);
  548. if (ret) {
  549. pr_err("crypto_shash_init failed: err %d\n", ret);
  550. ret = BAD_STATE_E;
  551. goto out;
  552. }
  553. ret = crypto_shash_update(desc, (byte *)(wc_ptr_t)first, (word32)code_sz);
  554. if (ret) {
  555. pr_err("crypto_shash_update failed: err %d\n", ret);
  556. ret = BAD_STATE_E;
  557. goto out;
  558. }
  559. /* don't hash verifyCore or changing verifyCore will change hash */
  560. if (verifyCore >= start && verifyCore < end) {
  561. data_sz = (unsigned long)verifyCore - (unsigned long)start;
  562. ret = crypto_shash_update(desc, (byte*)start, (word32)data_sz);
  563. if (ret) {
  564. pr_err("crypto_shash_update failed: err %d\n", ret);
  565. ret = BAD_STATE_E;
  566. goto out;
  567. }
  568. start = (char*)verifyCore + sizeof(verifyCore);
  569. data_sz = (unsigned long)end - (unsigned long)start;
  570. }
  571. ret = crypto_shash_update(desc, (byte*)start, (word32)data_sz);
  572. if (ret) {
  573. pr_err("crypto_shash_update failed: err %d\n", ret);
  574. ret = BAD_STATE_E;
  575. goto out;
  576. }
  577. ret = crypto_shash_final(desc, hash);
  578. if (ret) {
  579. pr_err("crypto_shash_final failed: err %d\n", ret);
  580. ret = BAD_STATE_E;
  581. goto out;
  582. }
  583. ret = GenBase16_Hash(hash, WC_SHA256_DIGEST_SIZE, base16_hash, WC_SHA256_DIGEST_SIZE*2 + 1);
  584. if (ret != 0) {
  585. pr_err("GenBase16_Hash failed: %s\n", wc_GetErrorString(ret));
  586. goto out;
  587. }
  588. {
  589. word32 base16_out_len = verifySz;
  590. ret = Base16_Decode((const byte *)verifyCore, sizeof verifyCore - 1, binVerify, &base16_out_len);
  591. if (ret != 0) {
  592. pr_err("Base16_Decode for verifyCore: %s\n", wc_GetErrorString(ret));
  593. goto out;
  594. }
  595. if (base16_out_len != binCoreSz) {
  596. pr_err("unexpected output length %u for verifyCore from Base16_Decode.\n",base16_out_len);
  597. ret = BAD_STATE_E;
  598. goto out;
  599. }
  600. }
  601. if (XMEMCMP(hash, binVerify, WC_SHA256_DIGEST_SIZE) == 0)
  602. pr_info("updateFipsHash: verifyCore already matches.\n");
  603. else {
  604. XMEMCPY(verifyCore, base16_hash, WC_SHA256_DIGEST_SIZE*2 + 1);
  605. pr_info("updateFipsHash: verifyCore updated.\n");
  606. }
  607. ret = 0;
  608. out:
  609. if (tfm != NULL)
  610. crypto_free_shash(tfm);
  611. if (desc != NULL)
  612. XFREE(desc, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  613. if (hash != NULL)
  614. XFREE(hash, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  615. if (base16_hash != NULL)
  616. XFREE(base16_hash, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  617. if (binCoreKey != NULL)
  618. XFREE(binCoreKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  619. if (binVerify != NULL)
  620. XFREE(binVerify, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  621. return ret;
  622. }
  623. #endif /* WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE */