• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 <errno.h>
18 #include "lapi/init_module.h"
19 #include "tst_module.h"
20 #include "tst_capability.h"
21 
22 #define MODULE_NAME	"finit_module.ko"
23 #define TEST_DIR	"test_dir"
24 
25 static char *mod_path;
26 
27 static int fd, fd_zero, fd_invalid = -1, fd_dir;
28 
29 static struct tst_cap cap_req = TST_CAP(TST_CAP_REQ, CAP_SYS_MODULE);
30 static struct tst_cap cap_drop = TST_CAP(TST_CAP_DROP, CAP_SYS_MODULE);
31 
32 struct tcase {
33 	const char *name;
34 	int *fd;
35 	const char *param;
36 	int open_flags;
37 	int flags;
38 	int cap;
39 	int exp_errno;
40 	void (*fix_errno)(struct tcase *tc);
41 };
42 
bad_fd_setup(struct tcase * tc)43 static void bad_fd_setup(struct tcase *tc)
44 {
45 	if (tst_kvercmp(4, 6, 0) < 0)
46 		tc->exp_errno = ENOEXEC;
47 	else
48 		tc->exp_errno = EBADF;
49 }
50 
wo_file_setup(struct tcase * tc)51 static void wo_file_setup(struct tcase *tc)
52 {
53 	if (tst_kvercmp(4, 6, 0) < 0)
54 		tc->exp_errno = EBADF;
55 	else
56 		tc->exp_errno = ETXTBSY;
57 }
58 
dir_setup(struct tcase * tc)59 static void dir_setup(struct tcase *tc)
60 {
61 	if (tst_kvercmp(4, 6, 0) < 0)
62 		tc->exp_errno = EISDIR;
63 	else
64 		tc->exp_errno = EINVAL;
65 }
66 
67 static struct tcase tcases[] = {
68 	{"invalid-fd", &fd_invalid, "", O_RDONLY | O_CLOEXEC, 0, 0, 0, bad_fd_setup},
69 	{"zero-fd", &fd_zero, "", O_RDONLY | O_CLOEXEC, 0, 0, EINVAL, NULL},
70 	{"null-param", &fd, NULL, O_RDONLY | O_CLOEXEC, 0, 0, EFAULT, NULL},
71 	{"invalid-param", &fd, "status=invalid", O_RDONLY | O_CLOEXEC, 0, 0, EINVAL, NULL},
72 	{"invalid-flags", &fd, "", O_RDONLY | O_CLOEXEC, -1, 0, EINVAL, NULL},
73 	{"no-perm", &fd, "", O_RDONLY | O_CLOEXEC, 0, 1, EPERM, NULL},
74 	{"module-exists", &fd, "", O_RDONLY | O_CLOEXEC, 0, 0, EEXIST, NULL},
75 	{"file-not-readable", &fd, "", O_WRONLY | O_CLOEXEC, 0, 0, 0, wo_file_setup},
76 	{"directory", &fd_dir, "", O_RDONLY | O_CLOEXEC, 0, 0, 0, dir_setup},
77 };
78 
setup(void)79 static void setup(void)
80 {
81 	unsigned long int i;
82 
83 	finit_module_supported_by_kernel();
84 
85 	tst_module_exists(MODULE_NAME, &mod_path);
86 
87 	SAFE_MKDIR(TEST_DIR, 0700);
88 	fd_dir = SAFE_OPEN(TEST_DIR, O_DIRECTORY);
89 
90 	for (i = 0; i < ARRAY_SIZE(tcases); i++) {
91 		if (tcases[i].fix_errno)
92 			tcases[i].fix_errno(&tcases[i]);
93 	}
94 }
95 
cleanup(void)96 static void cleanup(void)
97 {
98 	SAFE_CLOSE(fd_dir);
99 }
100 
run(unsigned int n)101 static void run(unsigned int n)
102 {
103 	struct tcase *tc = &tcases[n];
104 
105 	fd = SAFE_OPEN(mod_path, tc->open_flags);
106 
107 	if (tc->cap)
108 		tst_cap_action(&cap_drop);
109 
110 	/* Insert module twice */
111 	if (tc->exp_errno == EEXIST)
112 		tst_module_load(MODULE_NAME, NULL);
113 
114 	TST_EXP_FAIL(finit_module(*tc->fd, tc->param, tc->flags), tc->exp_errno,
115 		     "TestName: %s", tc->name);
116 
117 	if (tc->exp_errno == EEXIST)
118 		tst_module_unload(MODULE_NAME);
119 
120 	if (!TST_PASS && !TST_RET)
121 		tst_module_unload(MODULE_NAME);
122 
123 	if (tc->cap)
124 		tst_cap_action(&cap_req);
125 
126 	SAFE_CLOSE(fd);
127 }
128 
129 static struct tst_test test = {
130 	.test = run,
131 	.tcnt = ARRAY_SIZE(tcases),
132 	.setup = setup,
133 	.cleanup = cleanup,
134 	.needs_tmpdir = 1,
135 	.needs_root = 1,
136 };
137