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 <errno.h>
17 #include "lapi/init_module.h"
18 #include "tst_module.h"
19
20 #define MODULE_NAME "init_module.ko"
21
22 static struct stat sb;
23 static void *buf;
24
setup(void)25 static void setup(void)
26 {
27 int fd;
28
29 tst_module_exists(MODULE_NAME, NULL);
30
31 fd = SAFE_OPEN(MODULE_NAME, O_RDONLY|O_CLOEXEC);
32 SAFE_FSTAT(fd, &sb);
33 buf = SAFE_MMAP(0, sb.st_size, PROT_READ|PROT_EXEC, MAP_PRIVATE, fd, 0);
34 SAFE_CLOSE(fd);
35 }
36
run(void)37 static void run(void)
38 {
39 TST_EXP_PASS(init_module(buf, sb.st_size, "status=valid"));
40 if (!TST_PASS)
41 return;
42
43 tst_module_unload(MODULE_NAME);
44 }
45
cleanup(void)46 static void cleanup(void)
47 {
48 munmap(buf, sb.st_size);
49 }
50
51 static struct tst_test test = {
52 .test_all = run,
53 .setup = setup,
54 .cleanup = cleanup,
55 .needs_root = 1,
56 /* lockdown requires signed modules */
57 .skip_in_lockdown = 1,
58 };
59