fips140-eval-testing.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2021 Google LLC
  4. *
  5. * This file can optionally be built into fips140.ko in order to support certain
  6. * types of testing that the FIPS lab has to do to evaluate the module. It
  7. * should not be included in production builds of the module.
  8. */
  9. /*
  10. * We have to redefine inline to mean always_inline, so that _copy_to_user()
  11. * gets inlined. This is needed for it to be placed into the correct section.
  12. * See fips140_copy_to_user().
  13. *
  14. * We also need to undefine BUILD_FIPS140_KO to allow the use of the code
  15. * patching which copy_to_user() requires.
  16. */
  17. #undef inline
  18. #define inline inline __attribute__((__always_inline__)) __gnu_inline \
  19. __inline_maybe_unused notrace
  20. #undef BUILD_FIPS140_KO
  21. #include <linux/cdev.h>
  22. #include <linux/fs.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include "fips140-module.h"
  26. #include "fips140-eval-testing-uapi.h"
  27. /*
  28. * This option allows deliberately failing the self-tests for a particular
  29. * algorithm.
  30. */
  31. static char *fips140_fail_selftest;
  32. module_param_named(fail_selftest, fips140_fail_selftest, charp, 0);
  33. /* This option allows deliberately failing the integrity check. */
  34. static bool fips140_fail_integrity_check;
  35. module_param_named(fail_integrity_check, fips140_fail_integrity_check, bool, 0);
  36. static dev_t fips140_devnum;
  37. static struct cdev fips140_cdev;
  38. /* Inject a self-test failure (via corrupting the result) if requested. */
  39. void fips140_inject_selftest_failure(const char *impl, u8 *result)
  40. {
  41. if (fips140_fail_selftest && strcmp(impl, fips140_fail_selftest) == 0)
  42. result[0] ^= 0xff;
  43. }
  44. /* Inject an integrity check failure (via corrupting the text) if requested. */
  45. void fips140_inject_integrity_failure(u8 *textcopy)
  46. {
  47. if (fips140_fail_integrity_check)
  48. textcopy[0] ^= 0xff;
  49. }
  50. static long fips140_ioctl_is_approved_service(unsigned long arg)
  51. {
  52. const char *service_name = strndup_user((const char __user *)arg, 256);
  53. long ret;
  54. if (IS_ERR(service_name))
  55. return PTR_ERR(service_name);
  56. ret = fips140_is_approved_service(service_name);
  57. kfree(service_name);
  58. return ret;
  59. }
  60. /*
  61. * Code in fips140.ko is covered by an integrity check by default, and this
  62. * check breaks if copy_to_user() is called. This is because copy_to_user() is
  63. * an inline function that relies on code patching. However, since this is
  64. * "evaluation testing" code which isn't included in the production builds of
  65. * fips140.ko, it's acceptable to just exclude it from the integrity check.
  66. */
  67. static noinline unsigned long __section("text.._fips140_unchecked")
  68. fips140_copy_to_user(void __user *to, const void *from, unsigned long n)
  69. {
  70. return copy_to_user(to, from, n);
  71. }
  72. static long fips140_ioctl_module_version(unsigned long arg)
  73. {
  74. const char *version = fips140_module_version();
  75. size_t len = strlen(version) + 1;
  76. if (len > 256)
  77. return -EOVERFLOW;
  78. if (fips140_copy_to_user((void __user *)arg, version, len))
  79. return -EFAULT;
  80. return 0;
  81. }
  82. static long fips140_ioctl(struct file *file, unsigned int cmd,
  83. unsigned long arg)
  84. {
  85. switch (cmd) {
  86. case FIPS140_IOCTL_IS_APPROVED_SERVICE:
  87. return fips140_ioctl_is_approved_service(arg);
  88. case FIPS140_IOCTL_MODULE_VERSION:
  89. return fips140_ioctl_module_version(arg);
  90. default:
  91. return -ENOTTY;
  92. }
  93. }
  94. static const struct file_operations fips140_fops = {
  95. .unlocked_ioctl = fips140_ioctl,
  96. };
  97. bool fips140_eval_testing_init(void)
  98. {
  99. if (alloc_chrdev_region(&fips140_devnum, 1, 1, "fips140") != 0) {
  100. pr_err("failed to allocate device number\n");
  101. return false;
  102. }
  103. cdev_init(&fips140_cdev, &fips140_fops);
  104. if (cdev_add(&fips140_cdev, fips140_devnum, 1) != 0) {
  105. pr_err("failed to add fips140 character device\n");
  106. return false;
  107. }
  108. return true;
  109. }