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
23 #include <linux/cdev.h>
24 #include <linux/fs.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27
28 #include "fips140-module.h"
29 #include "fips140-eval-testing-uapi.h"
30
31 /*
32 * This option allows deliberately failing the self-tests for a particular
33 * algorithm.
34 */
35 static char *fips140_fail_selftest;
36 module_param_named(fail_selftest, fips140_fail_selftest, charp, 0);
37
38 /* This option allows deliberately failing the integrity check. */
39 static bool fips140_fail_integrity_check;
40 module_param_named(fail_integrity_check, fips140_fail_integrity_check, bool, 0);
41
42 static dev_t fips140_devnum;
43 static struct cdev fips140_cdev;
44
45 /* Inject a self-test failure (via corrupting the result) if requested. */
fips140_inject_selftest_failure(const char * impl,u8 * result)46 void fips140_inject_selftest_failure(const char *impl, u8 *result)
47 {
48 if (fips140_fail_selftest && strcmp(impl, fips140_fail_selftest) == 0)
49 result[0] ^= 0xff;
50 }
51
52 /* Inject an integrity check failure (via corrupting the text) if requested. */
fips140_inject_integrity_failure(u8 * textcopy)53 void fips140_inject_integrity_failure(u8 *textcopy)
54 {
55 if (fips140_fail_integrity_check)
56 textcopy[0] ^= 0xff;
57 }
58
fips140_ioctl_is_approved_service(unsigned long arg)59 static long fips140_ioctl_is_approved_service(unsigned long arg)
60 {
61 const char *service_name = strndup_user((const char __user *)arg, 256);
62 long ret;
63
64 if (IS_ERR(service_name))
65 return PTR_ERR(service_name);
66
67 ret = fips140_is_approved_service(service_name);
68
69 kfree(service_name);
70 return ret;
71 }
72
73 /*
74 * Code in fips140.ko is covered by an integrity check by default, and this
75 * check breaks if copy_to_user() is called. This is because copy_to_user() is
76 * an inline function that relies on code patching. However, since this is
77 * "evaluation testing" code which isn't included in the production builds of
78 * fips140.ko, it's acceptable to just exclude it from the integrity check.
79 */
80 static noinline unsigned long __section("text.._fips140_unchecked")
fips140_copy_to_user(void __user * to,const void * from,unsigned long n)81 fips140_copy_to_user(void __user *to, const void *from, unsigned long n)
82 {
83 return copy_to_user(to, from, n);
84 }
85
fips140_ioctl_module_version(unsigned long arg)86 static long fips140_ioctl_module_version(unsigned long arg)
87 {
88 const char *version = fips140_module_version();
89 size_t len = strlen(version) + 1;
90
91 if (len > 256)
92 return -EOVERFLOW;
93
94 if (fips140_copy_to_user((void __user *)arg, version, len))
95 return -EFAULT;
96
97 return 0;
98 }
99
fips140_ioctl(struct file * file,unsigned int cmd,unsigned long arg)100 static long fips140_ioctl(struct file *file, unsigned int cmd,
101 unsigned long arg)
102 {
103 switch (cmd) {
104 case FIPS140_IOCTL_IS_APPROVED_SERVICE:
105 return fips140_ioctl_is_approved_service(arg);
106 case FIPS140_IOCTL_MODULE_VERSION:
107 return fips140_ioctl_module_version(arg);
108 default:
109 return -ENOTTY;
110 }
111 }
112
113 static const struct file_operations fips140_fops = {
114 .unlocked_ioctl = fips140_ioctl,
115 };
116
fips140_eval_testing_init(void)117 bool fips140_eval_testing_init(void)
118 {
119 if (alloc_chrdev_region(&fips140_devnum, 1, 1, "fips140") != 0) {
120 pr_err("failed to allocate device number\n");
121 return false;
122 }
123 cdev_init(&fips140_cdev, &fips140_fops);
124 if (cdev_add(&fips140_cdev, fips140_devnum, 1) != 0) {
125 pr_err("failed to add fips140 character device\n");
126 return false;
127 }
128 return true;
129 }
130