1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2020 Viresh Kumar <[email protected]>
4 */
5
6 /*\
7 * [Description]
8 *
9 * Basic finit_module() tests.
10 *
11 * [Algorithm]
12 *
13 * Inserts a simple module after opening and mmaping the module file.
14 */
15
16 #include <stdlib.h>
17 #include <errno.h>
18 #include "lapi/init_module.h"
19 #include "tst_module.h"
20 #include "tst_kconfig.h"
21
22 #define MODULE_NAME "finit_module.ko"
23
24 static int fd, sig_enforce;
25
26 static char *mod_path;
27
setup(void)28 static void setup(void)
29 {
30 struct tst_kcmdline_var params = TST_KCMDLINE_INIT("module.sig_enforce");
31
32 tst_kcmdline_parse(¶ms, 1);
33 if (params.found)
34 sig_enforce = atoi(params.value);
35
36 tst_module_exists(MODULE_NAME, &mod_path);
37
38 fd = SAFE_OPEN(mod_path, O_RDONLY|O_CLOEXEC);
39 }
40
run(void)41 static void run(void)
42 {
43 if (sig_enforce == 1) {
44 tst_res(TINFO, "module signature is enforced");
45 TST_EXP_FAIL(finit_module(fd, "status=valid", 0), EKEYREJECTED);
46 return;
47 }
48
49 TST_EXP_PASS(finit_module(fd, "status=valid", 0));
50 if (!TST_PASS)
51 return;
52
53 tst_module_unload(MODULE_NAME);
54 }
55
cleanup(void)56 static void cleanup(void)
57 {
58 SAFE_CLOSE(fd);
59 }
60
61 static struct tst_test test = {
62 .test_all = run,
63 .setup = setup,
64 .cleanup = cleanup,
65 .needs_root = 1,
66 /* lockdown and SecureBoot requires signed modules */
67 .skip_in_lockdown = 1,
68 .skip_in_secureboot = 1,
69 };
70