1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2020 Viresh Kumar <viresh.kumar@linaro.org>
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
21 #define MODULE_NAME "init_module.ko"
22
23 static struct stat sb;
24 static void *buf;
25 static int sig_enforce;
26
setup(void)27 static void setup(void)
28 {
29 int fd;
30
31 if (tst_module_signature_enforced())
32 sig_enforce = 1;
33
34 tst_module_exists(MODULE_NAME, NULL);
35
36 fd = SAFE_OPEN(MODULE_NAME, O_RDONLY|O_CLOEXEC);
37 SAFE_FSTAT(fd, &sb);
38 buf = SAFE_MMAP(0, sb.st_size, PROT_READ|PROT_EXEC, MAP_PRIVATE, fd, 0);
39 SAFE_CLOSE(fd);
40 }
41
run(void)42 static void run(void)
43 {
44 if (sig_enforce == 1) {
45 TST_EXP_FAIL(init_module(buf, sb.st_size, "status=valid"), EKEYREJECTED);
46 return;
47 }
48
49 TST_EXP_PASS(init_module(buf, sb.st_size, "status=valid"));
50 if (!TST_PASS)
51 return;
52
53 tst_module_unload(MODULE_NAME);
54 }
55
cleanup(void)56 static void cleanup(void)
57 {
58 munmap(buf, sb.st_size);
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