module_hooks.c 27 KB

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