xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/finit_module/finit_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 finit_module() failure tests.
10  *
11  * [Algorithm]
12  *
13  * Tests various failure scenarios for finit_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_module.h"
21 #include "tst_kconfig.h"
22 #include "tst_capability.h"
23 
24 #define MODULE_NAME	"finit_module.ko"
25 #define TEST_DIR	"test_dir"
26 
27 static char *mod_path;
28 
29 static int fd, fd_zero, fd_invalid = -1, fd_dir;
30 static int kernel_lockdown, secure_boot, sig_enforce;
31 
32 static struct tst_cap cap_req = TST_CAP(TST_CAP_REQ, CAP_SYS_MODULE);
33 static struct tst_cap cap_drop = TST_CAP(TST_CAP_DROP, CAP_SYS_MODULE);
34 
35 struct tcase {
36 	const char *name;
37 	int *fd;
38 	const char *param;
39 	int open_flags;
40 	int flags;
41 	int cap;
42 	int exp_errno;
43 	int skip_in_lockdown;
44 	void (*fix_errno)(struct tcase *tc);
45 };
46 
bad_fd_setup(struct tcase * tc)47 static void bad_fd_setup(struct tcase *tc)
48 {
49 	if (tst_kvercmp(4, 6, 0) < 0)
50 		tc->exp_errno = ENOEXEC;
51 	else
52 		tc->exp_errno = EBADF;
53 }
54 
dir_setup(struct tcase * tc)55 static void dir_setup(struct tcase *tc)
56 {
57 	if (tst_kvercmp(4, 6, 0) < 0)
58 		tc->exp_errno = EISDIR;
59 	else
60 		tc->exp_errno = EINVAL;
61 }
62 
63 static struct tcase tcases[] = {
64 	{"invalid-fd", &fd_invalid, "", O_RDONLY | O_CLOEXEC, 0, 0, 0, 0, bad_fd_setup},
65 	{"zero-fd", &fd_zero, "", O_RDONLY | O_CLOEXEC, 0, 0, EINVAL, 0, NULL},
66 	{"null-param", &fd, NULL, O_RDONLY | O_CLOEXEC, 0, 0, EFAULT, 1, NULL},
67 	{"invalid-param", &fd, "status=invalid", O_RDONLY | O_CLOEXEC, 0, 0, EINVAL, 1, NULL},
68 	{"invalid-flags", &fd, "", O_RDONLY | O_CLOEXEC, -1, 0, EINVAL, 0, NULL},
69 	{"no-perm", &fd, "", O_RDONLY | O_CLOEXEC, 0, 1, EPERM, 0, NULL},
70 	{"module-exists", &fd, "", O_RDONLY | O_CLOEXEC, 0, 0, EEXIST, 1, NULL},
71 	{"module-unsigned", &fd, "", O_RDONLY | O_CLOEXEC, 0, 0, EKEYREJECTED, 1, NULL},
72 	{"file-not-readable", &fd, "", O_WRONLY | O_CLOEXEC, 0, 0, EBADF, 0, NULL},
73 	{"file-readwrite", &fd, "", O_RDWR | O_CLOEXEC, 0, 0, ETXTBSY, 0, NULL},
74 	{"directory", &fd_dir, "", O_RDONLY | O_CLOEXEC, 0, 0, 0, 0, dir_setup},
75 };
76 
setup(void)77 static void setup(void)
78 {
79 	unsigned long int i;
80 	struct tst_kcmdline_var params = TST_KCMDLINE_INIT("module.sig_enforce");
81 
82 	tst_kcmdline_parse(&params, 1);
83 	if (params.found)
84 		sig_enforce = atoi(params.value);
85 
86 	tst_module_exists(MODULE_NAME, &mod_path);
87 
88 	kernel_lockdown = tst_lockdown_enabled() > 0;
89 	secure_boot = tst_secureboot_enabled() > 0;
90 
91 	SAFE_MKDIR(TEST_DIR, 0700);
92 	fd_dir = SAFE_OPEN(TEST_DIR, O_DIRECTORY);
93 
94 	for (i = 0; i < ARRAY_SIZE(tcases); i++) {
95 		if (tcases[i].fix_errno)
96 			tcases[i].fix_errno(&tcases[i]);
97 	}
98 }
99 
cleanup(void)100 static void cleanup(void)
101 {
102 	SAFE_CLOSE(fd_dir);
103 }
104 
run(unsigned int n)105 static void run(unsigned int n)
106 {
107 	struct tcase *tc = &tcases[n];
108 
109 	if (tc->skip_in_lockdown && (kernel_lockdown || secure_boot)) {
110 		tst_res(TCONF, "Cannot load unsigned modules, skipping %s", tc->name);
111 		return;
112 	}
113 
114 	if ((sig_enforce == 1) && (tc->exp_errno != EKEYREJECTED)) {
115 		tst_res(TCONF, "module signature is enforced, skipping %s", tc->name);
116 		return;
117 	}
118 
119 	if ((sig_enforce != 1) && (tc->exp_errno == EKEYREJECTED)) {
120 		tst_res(TCONF, "module signature is not enforced, skipping %s", tc->name);
121 		return;
122 	}
123 
124 	fd = SAFE_OPEN(mod_path, tc->open_flags);
125 
126 	if (tc->cap)
127 		tst_cap_action(&cap_drop);
128 
129 	/* Insert module twice */
130 	if (tc->exp_errno == EEXIST)
131 		tst_module_load(MODULE_NAME, NULL);
132 
133 	TST_EXP_FAIL(finit_module(*tc->fd, tc->param, tc->flags), tc->exp_errno,
134 		     "TestName: %s", tc->name);
135 
136 	if (tc->exp_errno == EEXIST)
137 		tst_module_unload(MODULE_NAME);
138 
139 	if (!TST_PASS && !TST_RET)
140 		tst_module_unload(MODULE_NAME);
141 
142 	if (tc->cap)
143 		tst_cap_action(&cap_req);
144 
145 	SAFE_CLOSE(fd);
146 }
147 
148 static struct tst_test test = {
149 	.tags = (const struct tst_tag[]) {
150 		{"linux-git", "032146cda855"},
151 		{"linux-git", "39d637af5aa7"},
152 		{}
153 	},
154 	.test = run,
155 	.tcnt = ARRAY_SIZE(tcases),
156 	.setup = setup,
157 	.cleanup = cleanup,
158 	.needs_tmpdir = 1,
159 	.needs_root = 1,
160 };
161