xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/init_module/init_module02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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() failure tests.
10  *
11  * [Algorithm]
12  *
13  * Tests various failure scenarios for init_module().
14  */
15 
16 #include <linux/capability.h>
17 #include <stdlib.h>
18 #include <errno.h>
19 #include "lapi/init_module.h"
20 #include "tst_kconfig.h"
21 #include "tst_module.h"
22 #include "tst_capability.h"
23 
24 #define MODULE_NAME	"init_module.ko"
25 
26 static unsigned long size, zero_size;
27 static int kernel_lockdown, secure_boot, sig_enforce;
28 static void *buf, *faulty_buf, *null_buf;
29 
30 static struct tst_cap cap_req = TST_CAP(TST_CAP_REQ, CAP_SYS_MODULE);
31 static struct tst_cap cap_drop = TST_CAP(TST_CAP_DROP, CAP_SYS_MODULE);
32 
33 static struct tcase {
34 	const char *name;
35 	void **buf;
36 	unsigned long *size;
37 	const char *param;
38 	int cap;
39 	int skip_in_lockdown;
40 	int exp_errno;
41 } tcases[] = {
42 	{"NULL-buffer", &null_buf, &size, "", 0, 0, EFAULT},
43 	{"faulty-buffer", &faulty_buf, &size, "", 0, 0, EFAULT},
44 	{"null-param", &buf, &size, NULL, 0, 1, EFAULT},
45 	{"zero-size", &buf, &zero_size, "", 0, 0, ENOEXEC},
46 	{"invalid_param", &buf, &size, "status=invalid", 0, 1, EINVAL},
47 	{"no-perm", &buf, &size, "", 1, 0, EPERM},
48 	{"module-exists", &buf, &size, "", 0, 1, EEXIST},
49 	{"module-unsigned", &buf, &size, "", 0, 1, EKEYREJECTED},
50 };
51 
setup(void)52 static void setup(void)
53 {
54 	struct stat sb;
55 	int fd;
56 	struct tst_kcmdline_var params = TST_KCMDLINE_INIT("module.sig_enforce");
57 
58 	tst_kcmdline_parse(&params, 1);
59 	if (params.found)
60 		sig_enforce = atoi(params.value);
61 
62 	tst_module_exists(MODULE_NAME, NULL);
63 
64 	kernel_lockdown = tst_lockdown_enabled() > 0;
65 	secure_boot = tst_secureboot_enabled() > 0;
66 	fd = SAFE_OPEN(MODULE_NAME, O_RDONLY|O_CLOEXEC);
67 	SAFE_FSTAT(fd, &sb);
68 	size = sb.st_size;
69 	buf = SAFE_MMAP(0, size, PROT_READ|PROT_EXEC, MAP_PRIVATE, fd, 0);
70 	SAFE_CLOSE(fd);
71 
72 	faulty_buf = tst_get_bad_addr(NULL);
73 }
74 
run(unsigned int n)75 static void run(unsigned int n)
76 {
77 	struct tcase *tc = &tcases[n];
78 
79 	if (tc->skip_in_lockdown && (kernel_lockdown || secure_boot)) {
80 		tst_res(TCONF, "Cannot load unsigned modules, skipping %s", tc->name);
81 		return;
82 	}
83 
84 	if ((sig_enforce == 1) && (tc->exp_errno != EKEYREJECTED)) {
85 		tst_res(TCONF, "module signature is enforced, skipping %s", tc->name);
86 		return;
87 	}
88 
89 	if ((sig_enforce != 1) && (tc->exp_errno == EKEYREJECTED)) {
90 		tst_res(TCONF, "module signature is not enforced, skipping %s", tc->name);
91 		return;
92 	}
93 
94 	if (tc->cap)
95 		tst_cap_action(&cap_drop);
96 
97 	/* Insert module twice */
98 	if (tc->exp_errno == EEXIST)
99 		tst_module_load(MODULE_NAME, NULL);
100 
101 	TST_EXP_FAIL(init_module(*tc->buf, *tc->size, tc->param),
102 		tc->exp_errno, "TestName: %s", tc->name);
103 
104 	if (tc->exp_errno == EEXIST)
105 		tst_module_unload(MODULE_NAME);
106 
107 	if (!TST_PASS && !TST_RET)
108 		tst_module_unload(MODULE_NAME);
109 
110 	if (tc->cap)
111 		tst_cap_action(&cap_req);
112 }
113 
cleanup(void)114 static void cleanup(void)
115 {
116 	munmap(buf, size);
117 }
118 
119 static struct tst_test test = {
120 	.test = run,
121 	.tcnt = ARRAY_SIZE(tcases),
122 	.setup = setup,
123 	.cleanup = cleanup,
124 	.needs_root = 1,
125 };
126