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 init_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 "init_module.ko"
23
24 static struct stat sb;
25 static void *buf;
26 static int sig_enforce;
27
setup(void)28 static void setup(void)
29 {
30 int fd;
31 struct tst_kcmdline_var params = TST_KCMDLINE_INIT("module.sig_enforce");
32
33 tst_kcmdline_parse(¶ms, 1);
34 if (params.found)
35 sig_enforce = atoi(params.value);
36
37 tst_module_exists(MODULE_NAME, NULL);
38
39 fd = SAFE_OPEN(MODULE_NAME, O_RDONLY|O_CLOEXEC);
40 SAFE_FSTAT(fd, &sb);
41 buf = SAFE_MMAP(0, sb.st_size, PROT_READ|PROT_EXEC, MAP_PRIVATE, fd, 0);
42 SAFE_CLOSE(fd);
43 }
44
run(void)45 static void run(void)
46 {
47 if (sig_enforce == 1) {
48 tst_res(TINFO, "module signature is enforced");
49 TST_EXP_FAIL(init_module(buf, sb.st_size, "status=valid"), EKEYREJECTED);
50 return;
51 }
52
53 TST_EXP_PASS(init_module(buf, sb.st_size, "status=valid"));
54 if (!TST_PASS)
55 return;
56
57 tst_module_unload(MODULE_NAME);
58 }
59
cleanup(void)60 static void cleanup(void)
61 {
62 munmap(buf, sb.st_size);
63 }
64
65 static struct tst_test test = {
66 .test_all = run,
67 .setup = setup,
68 .cleanup = cleanup,
69 .needs_root = 1,
70 /* lockdown and SecureBoot requires signed modules */
71 .skip_in_lockdown = 1,
72 .skip_in_secureboot = 1,
73 };
74