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