xref: /freebsd/lib/libc/tests/gen/posix_spawn_test.c (revision 3de9dc5bf4b438e0d98222dc028982380d5a25d2)
1 /*-
2  * Copyright (c) 2011 Jilles Tjoelker
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * Test program for posix_spawn() and posix_spawnp() as specified by
29  * IEEE Std. 1003.1-2008.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/mman.h>
34 #include <sys/stat.h>
35 #include <sys/wait.h>
36 #include <dlfcn.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <spawn.h>
43 
44 #include <atf-c.h>
45 
46 static const char true_script[] =
47     "#!/usr/bin/env\n"
48     "/usr/bin/true\n";
49 
50 char *myenv[2] = { "answer=42", NULL };
51 
52 ATF_TC_WITHOUT_HEAD(posix_spawn_simple_test);
ATF_TC_BODY(posix_spawn_simple_test,tc)53 ATF_TC_BODY(posix_spawn_simple_test, tc)
54 {
55 	char *myargs[4];
56 	int error, status;
57 	pid_t pid, waitres;
58 
59 	/* Make sure we have no child processes. */
60 	while (waitpid(-1, NULL, 0) != -1)
61 		;
62 	ATF_REQUIRE_MSG(errno == ECHILD, "errno was not ECHILD: %d", errno);
63 
64 	/* Simple test. */
65 	myargs[0] = "sh";
66 	myargs[1] = "-c";
67 	myargs[2] = "exit $answer";
68 	myargs[3] = NULL;
69 	error = posix_spawnp(&pid, myargs[0], NULL, NULL, myargs, myenv);
70 	ATF_REQUIRE(error == 0);
71 	waitres = waitpid(pid, &status, 0);
72 	ATF_REQUIRE(waitres == pid);
73 	ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 42);
74 }
75 
76 ATF_TC_WITHOUT_HEAD(posix_spawn_no_such_command_negative_test);
ATF_TC_BODY(posix_spawn_no_such_command_negative_test,tc)77 ATF_TC_BODY(posix_spawn_no_such_command_negative_test, tc)
78 {
79 	char *myargs[4];
80 	int error, status;
81 	pid_t pid, waitres;
82 
83 	/*
84 	 * If the executable does not exist, the function shall either fail
85 	 * and not create a child process or succeed and create a child
86 	 * process that exits with status 127.
87 	 */
88 	myargs[0] = "/var/empty/nonexistent";
89 	myargs[1] = NULL;
90 	error = posix_spawn(&pid, myargs[0], NULL, NULL, myargs, myenv);
91 	if (error == 0) {
92 		waitres = waitpid(pid, &status, 0);
93 		ATF_REQUIRE(waitres == pid);
94 		ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 127);
95 	} else {
96 		ATF_REQUIRE(error == ENOENT);
97 		waitres = waitpid(-1, NULL, 0);
98 		ATF_REQUIRE(waitres == -1 && errno == ECHILD);
99 	}
100 }
101 
102 ATF_TC_WITHOUT_HEAD(posix_spawnp_enoexec_fallback);
ATF_TC_BODY(posix_spawnp_enoexec_fallback,tc)103 ATF_TC_BODY(posix_spawnp_enoexec_fallback, tc)
104 {
105 	char buf[FILENAME_MAX];
106 	char *myargs[2];
107 	int error, status;
108 	pid_t pid, waitres;
109 
110 	snprintf(buf, sizeof(buf), "%s/spawnp_enoexec.sh",
111 	    atf_tc_get_config_var(tc, "srcdir"));
112 	myargs[0] = buf;
113 	myargs[1] = NULL;
114 	error = posix_spawnp(&pid, myargs[0], NULL, NULL, myargs, myenv);
115 	ATF_REQUIRE(error == 0);
116 	waitres = waitpid(pid, &status, 0);
117 	ATF_REQUIRE(waitres == pid);
118 	ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 42);
119 }
120 
121 ATF_TC_WITHOUT_HEAD(posix_spawnp_enoexec_fallback_null_argv0);
ATF_TC_BODY(posix_spawnp_enoexec_fallback_null_argv0,tc)122 ATF_TC_BODY(posix_spawnp_enoexec_fallback_null_argv0, tc)
123 {
124 	char buf[FILENAME_MAX];
125 	char *myargs[1];
126 	int error;
127 	pid_t pid;
128 
129 	snprintf(buf, sizeof(buf), "%s/spawnp_enoexec.sh",
130 	    atf_tc_get_config_var(tc, "srcdir"));
131 	myargs[0] = NULL;
132 	error = posix_spawnp(&pid, buf, NULL, NULL, myargs, myenv);
133 	ATF_REQUIRE(error == EINVAL);
134 }
135 
136 ATF_TC(posix_spawnp_eacces);
ATF_TC_HEAD(posix_spawnp_eacces,tc)137 ATF_TC_HEAD(posix_spawnp_eacces, tc)
138 {
139 	atf_tc_set_md_var(tc, "descr", "Verify EACCES behavior in posix_spawnp");
140 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
141 }
ATF_TC_BODY(posix_spawnp_eacces,tc)142 ATF_TC_BODY(posix_spawnp_eacces, tc)
143 {
144 	const struct spawnp_eacces_tc {
145 		const char	*pathvar;
146 		int		 error_expected;
147 	} spawnp_eacces_tests[] = {
148 		{ ".",			EACCES }, /* File exists, but not +x */
149 		{ "unsearchable",	ENOENT }, /* File exists, dir not +x */
150 	};
151 	char *myargs[2] = { "eacces", NULL };
152 	int error;
153 
154 	error = mkdir("unsearchable", 0755);
155 	ATF_REQUIRE(error == 0);
156 	error = symlink("/usr/bin/true", "unsearchable/eacces");
157 	ATF_REQUIRE(error == 0);
158 
159 	(void)chmod("unsearchable", 0444);
160 
161 	/* this will create a non-executable file */
162 	atf_utils_create_file("eacces", true_script);
163 
164 	for (size_t i = 0; i < nitems(spawnp_eacces_tests); i++) {
165 		const struct spawnp_eacces_tc *tc = &spawnp_eacces_tests[i];
166 		pid_t pid;
167 
168 		error = setenv("PATH", tc->pathvar, 1);
169 		ATF_REQUIRE_EQ(0, error);
170 
171 		error = posix_spawnp(&pid, myargs[0], NULL, NULL, myargs,
172 		    myenv);
173 		ATF_CHECK_INTEQ_MSG(tc->error_expected, error,
174 		    "path '%s'", tc->pathvar);
175 	}
176 }
177 
178 #define	NUM_DSO	512
179 ATF_TC_WITHOUT_HEAD(posix_spawnp_stackunderflow);
ATF_TC_BODY(posix_spawnp_stackunderflow,tc)180 ATF_TC_BODY(posix_spawnp_stackunderflow, tc)
181 {
182 	struct stat sb;
183 	char dsopath[MAXPATHLEN];
184 	char *myargs[] = { "true", NULL };
185 	void **handles;
186 	char *dsomap;
187 	size_t dsosz;
188 	int error, fd, nfd, status;
189 	pid_t pid, waitres;
190 
191 	/* Make sure we have no child processes. */
192 	while (waitpid(-1, NULL, 0) != -1)
193 		;
194 	ATF_REQUIRE_MSG(errno == ECHILD, "errno was not ECHILD: %d", errno);
195 
196 	(void)snprintf(dsopath, sizeof(dsopath), "%s/libdummy.so",
197 	    atf_tc_get_config_var(tc, "srcdir"));
198 
199 	fd = open(dsopath, O_RDONLY);
200 	ATF_REQUIRE(fd >= 0);
201 
202 	/*
203 	 * We'll open our original shlib and fdlopen() it repeatedly until we
204 	 * have a lot of DSOs open, then we'll trigger a posix_spawnp.  This
205 	 * previously unearthed suboptimal stack usage in rtld that caused
206 	 * posix_spawnp()'s effectively-vforked environment to underflow its
207 	 * stack.
208 	 *
209 	 * We only get one shot to trigger the underflow, as rtld binding the
210 	 * symbols in the exec path in the rfork-child will affect the main
211 	 * process, so we only test that we don't have a problem with a large
212 	 * number of DSOs loaded.
213 	 */
214 	ATF_REQUIRE(fstat(fd, &sb) == 0);
215 	dsosz = sb.st_size;
216 	dsomap = mmap(NULL, dsosz, PROT_READ, MAP_SHARED, fd, 0);
217 	ATF_REQUIRE(dsomap != MAP_FAILED);
218 
219 	handles = calloc(sizeof(*handles), NUM_DSO);
220 	ATF_REQUIRE(handles != NULL);
221 
222 	for (int i = 0; i < NUM_DSO; i++) {
223 		nfd = memfd_create("dsobase", MFD_CLOEXEC);
224 		ATF_REQUIRE(nfd >= 0);
225 		ATF_REQUIRE(ftruncate(nfd, dsosz) == 0);
226 		ATF_REQUIRE(write(nfd, dsomap, dsosz) == dsosz);
227 
228 		handles[i] = fdlopen(nfd, RTLD_LAZY);
229 		ATF_REQUIRE(handles[i] != NULL);
230 		if (i > 0)
231 			ATF_REQUIRE(handles[i] != handles[i - 1]);
232 
233 		close(nfd);
234 	}
235 
236 	error = posix_spawnp(&pid, myargs[0], NULL, NULL, myargs, myenv);
237 	ATF_REQUIRE(error == 0);
238 	waitres = waitpid(pid, &status, 0);
239 	ATF_REQUIRE(waitres == pid);
240 	ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 0);
241 }
242 
ATF_TP_ADD_TCS(tp)243 ATF_TP_ADD_TCS(tp)
244 {
245 
246 	ATF_TP_ADD_TC(tp, posix_spawn_simple_test);
247 	ATF_TP_ADD_TC(tp, posix_spawn_no_such_command_negative_test);
248 	ATF_TP_ADD_TC(tp, posix_spawnp_enoexec_fallback);
249 	ATF_TP_ADD_TC(tp, posix_spawnp_enoexec_fallback_null_argv0);
250 	ATF_TP_ADD_TC(tp, posix_spawnp_eacces);
251 	ATF_TP_ADD_TC(tp, posix_spawnp_stackunderflow);
252 
253 	return (atf_no_error());
254 }
255