fips140-module.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2021 Google LLC
  4. * Author: Ard Biesheuvel <[email protected]>
  5. *
  6. * This file is the core of fips140.ko, which contains various crypto algorithms
  7. * that are also built into vmlinux. At load time, this module overrides the
  8. * built-in implementations of these algorithms with its implementations. It
  9. * also runs self-tests on these algorithms and verifies the integrity of its
  10. * code and data. If either of these steps fails, the kernel will panic.
  11. *
  12. * This module is intended to be loaded at early boot time in order to meet
  13. * FIPS 140 and NIAP FPT_TST_EXT.1 requirements. It shouldn't be used if you
  14. * don't need to meet these requirements.
  15. */
  16. /*
  17. * Since this .c file is the real entry point of fips140.ko, it needs to be
  18. * compiled normally, so undo the hacks that were done in fips140-defs.h.
  19. */
  20. #define MODULE
  21. #undef KBUILD_MODFILE
  22. #undef __DISABLE_EXPORTS
  23. #include <linux/ctype.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/module.h>
  26. #include <crypto/aead.h>
  27. #include <crypto/aes.h>
  28. #include <crypto/hash.h>
  29. #include <crypto/sha2.h>
  30. #include <crypto/skcipher.h>
  31. #include <crypto/rng.h>
  32. #include <trace/hooks/fips140.h>
  33. #include "fips140-module.h"
  34. #include "internal.h"
  35. /*
  36. * FIPS 140-2 prefers the use of HMAC with a public key over a plain hash.
  37. */
  38. u8 __initdata fips140_integ_hmac_key[] = "The quick brown fox jumps over the lazy dog";
  39. /* this is populated by the build tool */
  40. u8 __initdata fips140_integ_hmac_digest[SHA256_DIGEST_SIZE];
  41. const u32 __initcall_start_marker __section(".initcalls._start");
  42. const u32 __initcall_end_marker __section(".initcalls._end");
  43. const u8 __fips140_text_start __section(".text.._start");
  44. const u8 __fips140_text_end __section(".text.._end");
  45. const u8 __fips140_rodata_start __section(".rodata.._start");
  46. const u8 __fips140_rodata_end __section(".rodata.._end");
  47. /*
  48. * We need this little detour to prevent Clang from detecting out of bounds
  49. * accesses to __fips140_text_start and __fips140_rodata_start, which only exist
  50. * to delineate the section, and so their sizes are not relevant to us.
  51. */
  52. const u32 *__initcall_start = &__initcall_start_marker;
  53. const u8 *__text_start = &__fips140_text_start;
  54. const u8 *__rodata_start = &__fips140_rodata_start;
  55. /*
  56. * The list of the crypto API algorithms (by cra_name) that will be unregistered
  57. * by this module, in preparation for the module registering its own
  58. * implementation(s) of them.
  59. *
  60. * All algorithms that will be declared as FIPS-approved in the module
  61. * certification must be listed here, to ensure that the non-FIPS-approved
  62. * implementations of these algorithms in the kernel image aren't used.
  63. *
  64. * For every algorithm in this list, the module should contain all the "same"
  65. * implementations that the kernel image does, including the C implementation as
  66. * well as any architecture-specific implementations. This is needed to avoid
  67. * performance regressions as well as the possibility of an algorithm being
  68. * unavailable on some CPUs. E.g., "xcbc(aes)" isn't in this list, as the
  69. * module doesn't have a C implementation of it (and it won't be FIPS-approved).
  70. *
  71. * Due to a quirk in the FIPS requirements, "gcm(aes)" isn't actually able to be
  72. * FIPS-approved. However, we otherwise treat it the same as the algorithms
  73. * that will be FIPS-approved, and therefore it's included in this list.
  74. *
  75. * When adding a new algorithm here, make sure to consider whether it needs a
  76. * self-test added to fips140_selftests[] as well.
  77. */
  78. static const struct {
  79. const char *name;
  80. bool approved;
  81. } fips140_algs_to_replace[] = {
  82. {"aes", true},
  83. {"cmac(aes)", true},
  84. {"ecb(aes)", true},
  85. {"cbc(aes)", true},
  86. {"cts(cbc(aes))", true},
  87. {"ctr(aes)", true},
  88. {"xts(aes)", true},
  89. {"gcm(aes)", false},
  90. {"hmac(sha1)", true},
  91. {"hmac(sha224)", true},
  92. {"hmac(sha256)", true},
  93. {"hmac(sha384)", true},
  94. {"hmac(sha512)", true},
  95. {"sha1", true},
  96. {"sha224", true},
  97. {"sha256", true},
  98. {"sha384", true},
  99. {"sha512", true},
  100. {"stdrng", true},
  101. {"jitterentropy_rng", false},
  102. };
  103. static bool __init fips140_should_unregister_alg(struct crypto_alg *alg)
  104. {
  105. int i;
  106. /*
  107. * All software algorithms are synchronous, hardware algorithms must
  108. * be covered by their own FIPS 140 certification.
  109. */
  110. if (alg->cra_flags & CRYPTO_ALG_ASYNC)
  111. return false;
  112. for (i = 0; i < ARRAY_SIZE(fips140_algs_to_replace); i++) {
  113. if (!strcmp(alg->cra_name, fips140_algs_to_replace[i].name))
  114. return true;
  115. }
  116. return false;
  117. }
  118. /*
  119. * FIPS 140-3 service indicators. FIPS 140-3 requires that all services
  120. * "provide an indicator when the service utilises an approved cryptographic
  121. * algorithm, security function or process in an approved manner". What this
  122. * means is very debatable, even with the help of the FIPS 140-3 Implementation
  123. * Guidance document. However, it was decided that a function that takes in an
  124. * algorithm name and returns whether that algorithm is approved or not will
  125. * meet this requirement. Note, this relies on some properties of the module:
  126. *
  127. * - The module doesn't distinguish between "services" and "algorithms"; its
  128. * services are simply its algorithms.
  129. *
  130. * - The status of an approved algorithm is never non-approved, since (a) the
  131. * module doesn't support operating in a non-approved mode, such as a mode
  132. * where the self-tests are skipped; (b) there are no cases where the module
  133. * supports non-approved settings for approved algorithms, e.g.
  134. * non-approved key sizes; and (c) this function isn't available to be
  135. * called until the module_init function has completed, so it's guaranteed
  136. * that the self-tests and integrity check have already passed.
  137. *
  138. * - The module does support some non-approved algorithms, so a single static
  139. * indicator ("return true;") would not be acceptable.
  140. */
  141. bool fips140_is_approved_service(const char *name)
  142. {
  143. size_t i;
  144. for (i = 0; i < ARRAY_SIZE(fips140_algs_to_replace); i++) {
  145. if (!strcmp(name, fips140_algs_to_replace[i].name))
  146. return fips140_algs_to_replace[i].approved;
  147. }
  148. return false;
  149. }
  150. EXPORT_SYMBOL_GPL(fips140_is_approved_service);
  151. /*
  152. * FIPS 140-3 requires that modules provide a "service" that outputs "the name
  153. * or module identifier and the versioning information that can be correlated
  154. * with a validation record". This function meets that requirement.
  155. *
  156. * Note: the module also prints this same information to the kernel log when it
  157. * is loaded. That might meet the requirement by itself. However, given the
  158. * vagueness of what counts as a "service", we provide this function too, just
  159. * in case the certification lab or CMVP is happier with an explicit function.
  160. *
  161. * Note: /sys/modules/fips140/scmversion also provides versioning information
  162. * about the module. However that file just shows the bare git commit ID, so it
  163. * probably isn't sufficient to meet the FIPS requirement, which seems to want
  164. * the "official" module name and version number used in the FIPS certificate.
  165. */
  166. const char *fips140_module_version(void)
  167. {
  168. return FIPS140_MODULE_NAME " " FIPS140_MODULE_VERSION;
  169. }
  170. EXPORT_SYMBOL_GPL(fips140_module_version);
  171. static LIST_HEAD(existing_live_algos);
  172. /*
  173. * Release a list of algorithms which have been removed from crypto_alg_list.
  174. *
  175. * Note that even though the list is a private list, we have to hold
  176. * crypto_alg_sem while iterating through it because crypto_unregister_alg() may
  177. * run concurrently (as we haven't taken a reference to the algorithms on the
  178. * list), and crypto_unregister_alg() will remove the algorithm from whichever
  179. * list it happens to be on, while holding crypto_alg_sem. That's okay, since
  180. * in that case crypto_unregister_alg() will handle the crypto_alg_put().
  181. */
  182. static void fips140_remove_final(struct list_head *list)
  183. {
  184. struct crypto_alg *alg;
  185. struct crypto_alg *n;
  186. /*
  187. * We need to take crypto_alg_sem to safely traverse the list (see
  188. * comment above), but we have to drop it when doing each
  189. * crypto_alg_put() as that may take crypto_alg_sem again.
  190. */
  191. down_write(&crypto_alg_sem);
  192. list_for_each_entry_safe(alg, n, list, cra_list) {
  193. list_del_init(&alg->cra_list);
  194. up_write(&crypto_alg_sem);
  195. crypto_alg_put(alg);
  196. down_write(&crypto_alg_sem);
  197. }
  198. up_write(&crypto_alg_sem);
  199. }
  200. static void __init unregister_existing_fips140_algos(void)
  201. {
  202. struct crypto_alg *alg, *tmp;
  203. LIST_HEAD(remove_list);
  204. LIST_HEAD(spawns);
  205. down_write(&crypto_alg_sem);
  206. /*
  207. * Find all registered algorithms that we care about, and move them to a
  208. * private list so that they are no longer exposed via the algo lookup
  209. * API. Subsequently, we will unregister them if they are not in active
  210. * use. If they are, we can't fully unregister them but we can ensure
  211. * that new users won't use them.
  212. */
  213. list_for_each_entry_safe(alg, tmp, &crypto_alg_list, cra_list) {
  214. if (!fips140_should_unregister_alg(alg))
  215. continue;
  216. if (refcount_read(&alg->cra_refcnt) == 1) {
  217. /*
  218. * This algorithm is not currently in use, but there may
  219. * be template instances holding references to it via
  220. * spawns. So let's tear it down like
  221. * crypto_unregister_alg() would, but without releasing
  222. * the lock, to prevent races with concurrent TFM
  223. * allocations.
  224. */
  225. alg->cra_flags |= CRYPTO_ALG_DEAD;
  226. list_move(&alg->cra_list, &remove_list);
  227. crypto_remove_spawns(alg, &spawns, NULL);
  228. } else {
  229. /*
  230. * This algorithm is live, i.e. it has TFMs allocated,
  231. * so we can't fully unregister it. It's not necessary
  232. * to dynamically redirect existing users to the FIPS
  233. * code, given that they can't be relying on FIPS
  234. * certified crypto in the first place. However, we do
  235. * need to ensure that new users will get the FIPS code.
  236. *
  237. * In most cases, setting alg->cra_priority to 0
  238. * achieves this. However, that isn't enough for
  239. * algorithms like "hmac(sha256)" that need to be
  240. * instantiated from a template, since existing
  241. * algorithms always take priority over a template being
  242. * instantiated. Therefore, we move the algorithm to
  243. * a private list so that algorithm lookups won't find
  244. * it anymore. To further distinguish it from the FIPS
  245. * algorithms, we also append "+orig" to its name.
  246. */
  247. pr_info("found already-live algorithm '%s' ('%s')\n",
  248. alg->cra_name, alg->cra_driver_name);
  249. alg->cra_priority = 0;
  250. strlcat(alg->cra_name, "+orig", CRYPTO_MAX_ALG_NAME);
  251. strlcat(alg->cra_driver_name, "+orig",
  252. CRYPTO_MAX_ALG_NAME);
  253. list_move(&alg->cra_list, &existing_live_algos);
  254. }
  255. }
  256. up_write(&crypto_alg_sem);
  257. fips140_remove_final(&remove_list);
  258. fips140_remove_final(&spawns);
  259. }
  260. static void __init unapply_text_relocations(void *section, int section_size,
  261. const Elf64_Rela *rela, int numrels)
  262. {
  263. while (numrels--) {
  264. u32 *place = (u32 *)(section + rela->r_offset);
  265. BUG_ON(rela->r_offset >= section_size);
  266. switch (ELF64_R_TYPE(rela->r_info)) {
  267. #ifdef CONFIG_ARM64
  268. case R_AARCH64_ABS32: /* for KCFI */
  269. *place = 0;
  270. break;
  271. case R_AARCH64_JUMP26:
  272. case R_AARCH64_CALL26:
  273. *place &= ~GENMASK(25, 0);
  274. break;
  275. case R_AARCH64_ADR_PREL_LO21:
  276. case R_AARCH64_ADR_PREL_PG_HI21:
  277. case R_AARCH64_ADR_PREL_PG_HI21_NC:
  278. *place &= ~(GENMASK(30, 29) | GENMASK(23, 5));
  279. break;
  280. case R_AARCH64_ADD_ABS_LO12_NC:
  281. case R_AARCH64_LDST8_ABS_LO12_NC:
  282. case R_AARCH64_LDST16_ABS_LO12_NC:
  283. case R_AARCH64_LDST32_ABS_LO12_NC:
  284. case R_AARCH64_LDST64_ABS_LO12_NC:
  285. case R_AARCH64_LDST128_ABS_LO12_NC:
  286. *place &= ~GENMASK(21, 10);
  287. break;
  288. default:
  289. pr_err("unhandled relocation type %llu\n",
  290. ELF64_R_TYPE(rela->r_info));
  291. BUG();
  292. #else
  293. #error
  294. #endif
  295. }
  296. rela++;
  297. }
  298. }
  299. static void __init unapply_rodata_relocations(void *section, int section_size,
  300. const Elf64_Rela *rela, int numrels)
  301. {
  302. while (numrels--) {
  303. void *place = section + rela->r_offset;
  304. BUG_ON(rela->r_offset >= section_size);
  305. switch (ELF64_R_TYPE(rela->r_info)) {
  306. #ifdef CONFIG_ARM64
  307. case R_AARCH64_ABS64:
  308. *(u64 *)place = 0;
  309. break;
  310. default:
  311. pr_err("unhandled relocation type %llu\n",
  312. ELF64_R_TYPE(rela->r_info));
  313. BUG();
  314. #else
  315. #error
  316. #endif
  317. }
  318. rela++;
  319. }
  320. }
  321. enum {
  322. PACIASP = 0xd503233f,
  323. AUTIASP = 0xd50323bf,
  324. SCS_PUSH = 0xf800865e,
  325. SCS_POP = 0xf85f8e5e,
  326. };
  327. /*
  328. * To make the integrity check work with dynamic Shadow Call Stack (SCS),
  329. * replace all instructions that push or pop from the SCS with the Pointer
  330. * Authentication Code (PAC) instructions that were present originally.
  331. */
  332. static void __init unapply_scs_patch(void *section, int section_size)
  333. {
  334. #if defined(CONFIG_ARM64) && defined(CONFIG_UNWIND_PATCH_PAC_INTO_SCS)
  335. u32 *insns = section;
  336. int i;
  337. for (i = 0; i < section_size / sizeof(insns[0]); i++) {
  338. if (insns[i] == SCS_PUSH)
  339. insns[i] = PACIASP;
  340. else if (insns[i] == SCS_POP)
  341. insns[i] = AUTIASP;
  342. }
  343. #endif
  344. }
  345. #ifdef CONFIG_CRYPTO_FIPS140_MOD_DEBUG_INTEGRITY_CHECK
  346. static struct {
  347. const void *text;
  348. int textsize;
  349. const void *rodata;
  350. int rodatasize;
  351. } saved_integrity_check_info;
  352. static ssize_t fips140_text_read(struct file *file, char __user *to,
  353. size_t count, loff_t *ppos)
  354. {
  355. return simple_read_from_buffer(to, count, ppos,
  356. saved_integrity_check_info.text,
  357. saved_integrity_check_info.textsize);
  358. }
  359. static ssize_t fips140_rodata_read(struct file *file, char __user *to,
  360. size_t count, loff_t *ppos)
  361. {
  362. return simple_read_from_buffer(to, count, ppos,
  363. saved_integrity_check_info.rodata,
  364. saved_integrity_check_info.rodatasize);
  365. }
  366. static const struct file_operations fips140_text_fops = {
  367. .read = fips140_text_read,
  368. };
  369. static const struct file_operations fips140_rodata_fops = {
  370. .read = fips140_rodata_read,
  371. };
  372. static void fips140_init_integrity_debug_files(const void *text, int textsize,
  373. const void *rodata,
  374. int rodatasize)
  375. {
  376. struct dentry *dir;
  377. dir = debugfs_create_dir("fips140", NULL);
  378. saved_integrity_check_info.text = kmemdup(text, textsize, GFP_KERNEL);
  379. saved_integrity_check_info.textsize = textsize;
  380. if (saved_integrity_check_info.text)
  381. debugfs_create_file("text", 0400, dir, NULL,
  382. &fips140_text_fops);
  383. saved_integrity_check_info.rodata = kmemdup(rodata, rodatasize,
  384. GFP_KERNEL);
  385. saved_integrity_check_info.rodatasize = rodatasize;
  386. if (saved_integrity_check_info.rodata)
  387. debugfs_create_file("rodata", 0400, dir, NULL,
  388. &fips140_rodata_fops);
  389. }
  390. #else /* CONFIG_CRYPTO_FIPS140_MOD_DEBUG_INTEGRITY_CHECK */
  391. static void fips140_init_integrity_debug_files(const void *text, int textsize,
  392. const void *rodata,
  393. int rodatasize)
  394. {
  395. }
  396. #endif /* !CONFIG_CRYPTO_FIPS140_MOD_DEBUG_INTEGRITY_CHECK */
  397. extern struct {
  398. u32 offset;
  399. u32 count;
  400. } fips140_rela_text, fips140_rela_rodata;
  401. static bool __init check_fips140_module_hmac(void)
  402. {
  403. struct crypto_shash *tfm = NULL;
  404. SHASH_DESC_ON_STACK(desc, dontcare);
  405. u8 digest[SHA256_DIGEST_SIZE];
  406. void *textcopy, *rodatacopy;
  407. int textsize, rodatasize;
  408. bool ok = false;
  409. int err;
  410. textsize = &__fips140_text_end - &__fips140_text_start;
  411. rodatasize = &__fips140_rodata_end - &__fips140_rodata_start;
  412. pr_info("text size : 0x%x\n", textsize);
  413. pr_info("rodata size: 0x%x\n", rodatasize);
  414. textcopy = kmalloc(textsize + rodatasize, GFP_KERNEL);
  415. if (!textcopy) {
  416. pr_err("Failed to allocate memory for copy of .text\n");
  417. goto out;
  418. }
  419. rodatacopy = textcopy + textsize;
  420. memcpy(textcopy, __text_start, textsize);
  421. memcpy(rodatacopy, __rodata_start, rodatasize);
  422. // apply the relocations in reverse on the copies of .text and .rodata
  423. unapply_text_relocations(textcopy, textsize,
  424. offset_to_ptr(&fips140_rela_text.offset),
  425. fips140_rela_text.count);
  426. unapply_rodata_relocations(rodatacopy, rodatasize,
  427. offset_to_ptr(&fips140_rela_rodata.offset),
  428. fips140_rela_rodata.count);
  429. unapply_scs_patch(textcopy, textsize);
  430. fips140_init_integrity_debug_files(textcopy, textsize,
  431. rodatacopy, rodatasize);
  432. fips140_inject_integrity_failure(textcopy);
  433. tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
  434. if (IS_ERR(tfm)) {
  435. pr_err("failed to allocate hmac tfm (%ld)\n", PTR_ERR(tfm));
  436. tfm = NULL;
  437. goto out;
  438. }
  439. desc->tfm = tfm;
  440. pr_info("using '%s' for integrity check\n",
  441. crypto_shash_driver_name(tfm));
  442. err = crypto_shash_setkey(tfm, fips140_integ_hmac_key,
  443. strlen(fips140_integ_hmac_key)) ?:
  444. crypto_shash_init(desc) ?:
  445. crypto_shash_update(desc, textcopy, textsize) ?:
  446. crypto_shash_finup(desc, rodatacopy, rodatasize, digest);
  447. /* Zeroizing this is important; see the comment below. */
  448. shash_desc_zero(desc);
  449. if (err) {
  450. pr_err("failed to calculate hmac shash (%d)\n", err);
  451. goto out;
  452. }
  453. if (memcmp(digest, fips140_integ_hmac_digest, sizeof(digest))) {
  454. pr_err("provided_digest : %*phN\n", (int)sizeof(digest),
  455. fips140_integ_hmac_digest);
  456. pr_err("calculated digest: %*phN\n", (int)sizeof(digest),
  457. digest);
  458. goto out;
  459. }
  460. ok = true;
  461. out:
  462. /*
  463. * FIPS 140-3 requires that all "temporary value(s) generated during the
  464. * integrity test" be zeroized (ref: FIPS 140-3 IG 9.7.B). There is no
  465. * technical reason to do this given that these values are public
  466. * information, but this is the requirement so we follow it.
  467. */
  468. crypto_free_shash(tfm);
  469. memzero_explicit(digest, sizeof(digest));
  470. kfree_sensitive(textcopy);
  471. return ok;
  472. }
  473. static void fips140_sha256(void *p, const u8 *data, unsigned int len, u8 *out,
  474. int *hook_inuse)
  475. {
  476. sha256(data, len, out);
  477. *hook_inuse = 1;
  478. }
  479. static void fips140_aes_expandkey(void *p, struct crypto_aes_ctx *ctx,
  480. const u8 *in_key, unsigned int key_len,
  481. int *err)
  482. {
  483. *err = aes_expandkey(ctx, in_key, key_len);
  484. }
  485. static void fips140_aes_encrypt(void *priv, const struct crypto_aes_ctx *ctx,
  486. u8 *out, const u8 *in, int *hook_inuse)
  487. {
  488. aes_encrypt(ctx, out, in);
  489. *hook_inuse = 1;
  490. }
  491. static void fips140_aes_decrypt(void *priv, const struct crypto_aes_ctx *ctx,
  492. u8 *out, const u8 *in, int *hook_inuse)
  493. {
  494. aes_decrypt(ctx, out, in);
  495. *hook_inuse = 1;
  496. }
  497. static bool update_fips140_library_routines(void)
  498. {
  499. int ret;
  500. ret = register_trace_android_vh_sha256(fips140_sha256, NULL) ?:
  501. register_trace_android_vh_aes_expandkey(fips140_aes_expandkey, NULL) ?:
  502. register_trace_android_vh_aes_encrypt(fips140_aes_encrypt, NULL) ?:
  503. register_trace_android_vh_aes_decrypt(fips140_aes_decrypt, NULL);
  504. return ret == 0;
  505. }
  506. /*
  507. * Initialize the FIPS 140 module.
  508. *
  509. * Note: this routine iterates over the contents of the initcall section, which
  510. * consists of an array of function pointers that was emitted by the linker
  511. * rather than the compiler. This means that these function pointers lack the
  512. * usual CFI stubs that the compiler emits when CFI codegen is enabled. So
  513. * let's disable CFI locally when handling the initcall array, to avoid
  514. * surpises.
  515. */
  516. static int __init __attribute__((__no_sanitize__("cfi")))
  517. fips140_init(void)
  518. {
  519. const u32 *initcall;
  520. pr_info("loading " FIPS140_MODULE_NAME " " FIPS140_MODULE_VERSION "\n");
  521. fips140_init_thread = current;
  522. unregister_existing_fips140_algos();
  523. /* iterate over all init routines present in this module and call them */
  524. for (initcall = __initcall_start + 1;
  525. initcall < &__initcall_end_marker;
  526. initcall++) {
  527. int (*init)(void) = offset_to_ptr(initcall);
  528. int err = init();
  529. /*
  530. * ENODEV is expected from initcalls that only register
  531. * algorithms that depend on non-present CPU features. Besides
  532. * that, errors aren't expected here.
  533. */
  534. if (err && err != -ENODEV) {
  535. pr_err("initcall %ps() failed: %d\n", init, err);
  536. goto panic;
  537. }
  538. }
  539. if (!fips140_run_selftests())
  540. goto panic;
  541. /*
  542. * It may seem backward to perform the integrity check last, but this
  543. * is intentional: the check itself uses hmac(sha256) which is one of
  544. * the algorithms that are replaced with versions from this module, and
  545. * the integrity check must use the replacement version. Also, to be
  546. * ready for FIPS 140-3, the integrity check algorithm must have already
  547. * been self-tested.
  548. */
  549. if (!check_fips140_module_hmac()) {
  550. if (!IS_ENABLED(CONFIG_CRYPTO_FIPS140_MOD_DEBUG_INTEGRITY_CHECK)) {
  551. pr_crit("integrity check failed -- giving up!\n");
  552. goto panic;
  553. }
  554. pr_crit("ignoring integrity check failure due to debug mode\n");
  555. } else {
  556. pr_info("integrity check passed\n");
  557. }
  558. complete_all(&fips140_tests_done);
  559. if (!update_fips140_library_routines())
  560. goto panic;
  561. if (!fips140_eval_testing_init())
  562. goto panic;
  563. pr_info("module successfully loaded\n");
  564. return 0;
  565. panic:
  566. panic("FIPS 140 module load failure");
  567. }
  568. module_init(fips140_init);
  569. MODULE_IMPORT_NS(CRYPTO_INTERNAL);
  570. MODULE_LICENSE("GPL v2");
  571. /*
  572. * Below are copies of some selected "crypto-related" helper functions that are
  573. * used by fips140.ko but are not already built into it, due to them being
  574. * defined in a file that cannot easily be built into fips140.ko (e.g.,
  575. * crypto/algapi.c) instead of one that can (e.g., most files in lib/).
  576. *
  577. * There is no hard rule about what needs to be included here, as this is for
  578. * FIPS certifiability, not any technical reason. FIPS modules are supposed to
  579. * implement the "crypto" themselves, but to do so they are allowed to call
  580. * non-cryptographic helper functions from outside the module. Something like
  581. * memcpy() is "clearly" non-cryptographic. However, there is is ambiguity
  582. * about functions like crypto_inc() which aren't cryptographic by themselves,
  583. * but are more closely associated with cryptography than e.g. memcpy(). To err
  584. * on the side of caution, we define copies of some selected functions below so
  585. * that calls to them from within fips140.ko will remain in fips140.ko.
  586. */
  587. static inline void crypto_inc_byte(u8 *a, unsigned int size)
  588. {
  589. u8 *b = (a + size);
  590. u8 c;
  591. for (; size; size--) {
  592. c = *--b + 1;
  593. *b = c;
  594. if (c)
  595. break;
  596. }
  597. }
  598. void crypto_inc(u8 *a, unsigned int size)
  599. {
  600. __be32 *b = (__be32 *)(a + size);
  601. u32 c;
  602. if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
  603. IS_ALIGNED((unsigned long)b, __alignof__(*b)))
  604. for (; size >= 4; size -= 4) {
  605. c = be32_to_cpu(*--b) + 1;
  606. *b = cpu_to_be32(c);
  607. if (likely(c))
  608. return;
  609. }
  610. crypto_inc_byte(a, size);
  611. }