Ard Biesheuvel | 2db9143 | 2021-03-23 10:54:38 +0100 | [diff] [blame] | 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 | /* |
| 11 | * We have to redefine inline to mean always_inline, so that _copy_to_user() |
| 12 | * gets inlined. This is needed for it to be placed into the correct section. |
| 13 | * See fips140_copy_to_user(). |
| 14 | * |
| 15 | * We also need to undefine BUILD_FIPS140_KO to allow the use of the code |
| 16 | * patching which copy_to_user() requires. |
| 17 | */ |
| 18 | #undef inline |
| 19 | #define inline inline __attribute__((__always_inline__)) __gnu_inline \ |
| 20 | __inline_maybe_unused notrace |
| 21 | #undef BUILD_FIPS140_KO |
| 22 | |
Eric Biggers | e8b59bc | 2023-08-24 16:58:19 +0000 | [diff] [blame^] | 23 | /* |
| 24 | * Since this .c file contains real module parameters for fips140.ko, it needs |
| 25 | * to be compiled normally, so undo the hacks that were done in fips140-defs.h. |
| 26 | */ |
| 27 | #define MODULE |
| 28 | #undef KBUILD_MODFILE |
| 29 | #undef __DISABLE_EXPORTS |
| 30 | |
Ard Biesheuvel | 2db9143 | 2021-03-23 10:54:38 +0100 | [diff] [blame] | 31 | #include <linux/cdev.h> |
| 32 | #include <linux/fs.h> |
| 33 | #include <linux/module.h> |
| 34 | #include <linux/slab.h> |
| 35 | |
| 36 | #include "fips140-module.h" |
| 37 | #include "fips140-eval-testing-uapi.h" |
| 38 | |
| 39 | /* |
| 40 | * This option allows deliberately failing the self-tests for a particular |
| 41 | * algorithm. |
| 42 | */ |
| 43 | static char *fips140_fail_selftest; |
| 44 | module_param_named(fail_selftest, fips140_fail_selftest, charp, 0); |
| 45 | |
| 46 | /* This option allows deliberately failing the integrity check. */ |
| 47 | static bool fips140_fail_integrity_check; |
| 48 | module_param_named(fail_integrity_check, fips140_fail_integrity_check, bool, 0); |
| 49 | |
| 50 | static dev_t fips140_devnum; |
| 51 | static struct cdev fips140_cdev; |
| 52 | |
| 53 | /* Inject a self-test failure (via corrupting the result) if requested. */ |
| 54 | void fips140_inject_selftest_failure(const char *impl, u8 *result) |
| 55 | { |
| 56 | if (fips140_fail_selftest && strcmp(impl, fips140_fail_selftest) == 0) |
| 57 | result[0] ^= 0xff; |
| 58 | } |
| 59 | |
| 60 | /* Inject an integrity check failure (via corrupting the text) if requested. */ |
| 61 | void fips140_inject_integrity_failure(u8 *textcopy) |
| 62 | { |
| 63 | if (fips140_fail_integrity_check) |
| 64 | textcopy[0] ^= 0xff; |
| 65 | } |
| 66 | |
| 67 | static long fips140_ioctl_is_approved_service(unsigned long arg) |
| 68 | { |
| 69 | const char *service_name = strndup_user((const char __user *)arg, 256); |
| 70 | long ret; |
| 71 | |
| 72 | if (IS_ERR(service_name)) |
| 73 | return PTR_ERR(service_name); |
| 74 | |
| 75 | ret = fips140_is_approved_service(service_name); |
| 76 | |
| 77 | kfree(service_name); |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Code in fips140.ko is covered by an integrity check by default, and this |
| 83 | * check breaks if copy_to_user() is called. This is because copy_to_user() is |
| 84 | * an inline function that relies on code patching. However, since this is |
| 85 | * "evaluation testing" code which isn't included in the production builds of |
| 86 | * fips140.ko, it's acceptable to just exclude it from the integrity check. |
| 87 | */ |
| 88 | static noinline unsigned long __section("text.._fips140_unchecked") |
| 89 | fips140_copy_to_user(void __user *to, const void *from, unsigned long n) |
| 90 | { |
| 91 | return copy_to_user(to, from, n); |
| 92 | } |
| 93 | |
| 94 | static long fips140_ioctl_module_version(unsigned long arg) |
| 95 | { |
| 96 | const char *version = fips140_module_version(); |
| 97 | size_t len = strlen(version) + 1; |
| 98 | |
| 99 | if (len > 256) |
| 100 | return -EOVERFLOW; |
| 101 | |
| 102 | if (fips140_copy_to_user((void __user *)arg, version, len)) |
| 103 | return -EFAULT; |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static long fips140_ioctl(struct file *file, unsigned int cmd, |
| 109 | unsigned long arg) |
| 110 | { |
| 111 | switch (cmd) { |
| 112 | case FIPS140_IOCTL_IS_APPROVED_SERVICE: |
| 113 | return fips140_ioctl_is_approved_service(arg); |
| 114 | case FIPS140_IOCTL_MODULE_VERSION: |
| 115 | return fips140_ioctl_module_version(arg); |
| 116 | default: |
| 117 | return -ENOTTY; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | static const struct file_operations fips140_fops = { |
| 122 | .unlocked_ioctl = fips140_ioctl, |
| 123 | }; |
| 124 | |
| 125 | bool fips140_eval_testing_init(void) |
| 126 | { |
| 127 | if (alloc_chrdev_region(&fips140_devnum, 1, 1, "fips140") != 0) { |
| 128 | pr_err("failed to allocate device number\n"); |
| 129 | return false; |
| 130 | } |
| 131 | cdev_init(&fips140_cdev, &fips140_fops); |
| 132 | if (cdev_add(&fips140_cdev, fips140_devnum, 1) != 0) { |
| 133 | pr_err("failed to add fips140 character device\n"); |
| 134 | return false; |
| 135 | } |
| 136 | return true; |
| 137 | } |