xref: /openbsd-src/regress/lib/libc/sys/t_fork.c (revision 49a6e16f2c2c8e509184b1f777366d1a6f337e1c)
1 /*	$OpenBSD: t_fork.c,v 1.5 2021/12/13 16:56:48 deraadt Exp $	*/
2 /*	$NetBSD: t_fork.c,v 1.4 2019/04/06 15:41:54 kamil Exp $	*/
3 
4 /*-
5  * Copyright (c) 2018, 2019 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 #include "macros.h"
30 
31 #include <sys/types.h>
32 #include <sys/signal.h>
33 #ifdef __OpenBSD__
34 #include <sys/proc.h>
35 #endif
36 #include <sys/sysctl.h>
37 #include <sys/wait.h>
38 #include <sched.h>
39 #include <signal.h>
40 #include <stdbool.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <err.h>
44 #include <errno.h>
45 
46 #include "atf-c.h"
47 
48 #ifdef VFORK
49 #define FORK vfork
50 #else
51 #define FORK fork
52 #endif
53 
54 /*
55  * A child process cannot call atf functions and expect them to magically
56  * work like in the parent.
57  * The printf(3) messaging from a child will not work out of the box as well
58  * without estabilishing a communication protocol with its parent. To not
59  * overcomplicate the tests - do not log from a child and use err(3)/errx(3)
60  * wrapped with ASSERT_EQ()/ASSERT_NEQ() as that is guaranteed to work.
61  */
62 #define ASSERT_EQ(x, y)								\
63 do {										\
64 	uintmax_t vx = (x);							\
65 	uintmax_t vy = (y);							\
66 	int ret = vx == vy;							\
67 	if (!ret)								\
68 		errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: "		\
69 		    "%s(%ju) == %s(%ju)", __FILE__, __LINE__, __func__,		\
70 		    #x, vx, #y, vy);						\
71 } while (/*CONSTCOND*/0)
72 
73 #define ASSERT_NEQ(x, y)							\
74 do {										\
75 	uintmax_t vx = (x);							\
76 	uintmax_t vy = (y);							\
77 	int ret = vx != vy;							\
78 	if (!ret)								\
79 		errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: "		\
80 		    "%s(%ju) != %s(%ju)", __FILE__, __LINE__, __func__,		\
81 		    #x, vx, #y, vy);						\
82 } while (/*CONSTCOND*/0)
83 
84 static pid_t
await_stopped_child(pid_t process)85 await_stopped_child(pid_t process)
86 {
87 	struct kinfo_proc2 *p = NULL;
88 	size_t i, len;
89 	pid_t child = -1;
90 
91 	int name[] = {
92 		[0] = CTL_KERN,
93 		[1] = KERN_PROC2,
94 		[2] = KERN_PROC_ALL,
95 		[3] = 0,
96 		[4] = sizeof(struct kinfo_proc2),
97 		[5] = 0
98 	};
99 
100 	const size_t namelen = __arraycount(name);
101 
102 	/* Await the process becoming a zombie */
103 	while(1) {
104 		name[5] = 0;
105 
106 		ASSERT_EQ(sysctl(name, namelen, 0, &len, NULL, 0), 0);
107 
108 		ASSERT_EQ(reallocarr(&p, len, sizeof(struct kinfo_proc2)), 0);
109 
110 		name[5] = len;
111 
112 		ASSERT_EQ(sysctl(name, namelen, p, &len, NULL, 0), 0);
113 
114 		for (i = 0; i < len/sizeof(struct kinfo_proc2); i++) {
115 			if (p[i].p_pid == getpid())
116 				continue;
117 			if (p[i].p_ppid != process)
118 				continue;
119 			if (p[i].p_stat != LSSTOP)
120 				continue;
121 			child = p[i].p_pid;
122 			break;
123 		}
124 
125 		if (child != -1)
126 			break;
127 
128 		ASSERT_EQ(usleep(1000), 0);
129 	}
130 
131 	/* Free the buffer */
132 	ASSERT_EQ(reallocarr(&p, 0, sizeof(struct kinfo_proc2)), 0);
133 
134 	return child;
135 }
136 
137 static void
raise_raw(int sig)138 raise_raw(int sig)
139 {
140 	int rv, status;
141 	pid_t child, parent, watcher, wpid;
142 	int expect_core = (sig == SIGABRT) ? 1 : 0;
143 
144 	/*
145 	 * Spawn a dedicated thread to watch for a stopped child and emit
146 	 * the SIGKILL signal to it.
147 	 *
148 	 * This is required in vfork(2)ing parent and optional in fork(2).
149 	 *
150 	 * vfork(2) might clobber watcher, this means that it's safer and
151 	 * simpler to reparent this process to initproc and forget about it.
152 	 */
153 	if (sig == SIGSTOP
154 #ifndef VFORK
155 	    || (sig == SIGTSTP || sig == SIGTTIN || sig == SIGTTOU)
156 #endif
157 	    ) {
158 
159 		parent = getpid();
160 
161 		watcher = fork();
162 		ATF_REQUIRE(watcher != 1);
163 		if (watcher == 0) {
164 			/* Double fork(2) trick to reparent to initproc */
165 			watcher = fork();
166 			ASSERT_NEQ(watcher, -1);
167 			if (watcher != 0)
168 				_exit(0);
169 
170 			child = await_stopped_child(parent);
171 
172 			errno = 0;
173 			rv = kill(child, SIGKILL);
174 			ASSERT_EQ(rv, 0);
175 			ASSERT_EQ(errno, 0);
176 
177 			/* This exit value will be collected by initproc */
178 			_exit(0);
179 		}
180 
181 		wpid = waitpid(watcher, &status, 0);
182 
183 		ATF_REQUIRE_EQ(wpid, watcher);
184 
185 		ATF_REQUIRE(WIFEXITED(status));
186 		ATF_REQUIRE(!WIFCONTINUED(status));
187 		ATF_REQUIRE(!WIFSIGNALED(status));
188 		ATF_REQUIRE(!WIFSTOPPED(status));
189 		ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
190 	}
191 
192 	child = FORK();
193 	ATF_REQUIRE(child != 1);
194 	if (child == 0) {
195 		rv = raise(sig);
196 		ASSERT_EQ(rv, 0);
197 		_exit(0);
198 	}
199 	wpid = waitpid(child, &status, 0);
200 
201 	ATF_REQUIRE_EQ(wpid, child);
202 
203 	switch (sig) {
204 	case SIGKILL:
205 	case SIGABRT:
206 	case SIGHUP:
207 		ATF_REQUIRE(!WIFEXITED(status));
208 		ATF_REQUIRE(!WIFCONTINUED(status));
209 		ATF_REQUIRE(WIFSIGNALED(status));
210 		ATF_REQUIRE(!WIFSTOPPED(status));
211 		ATF_REQUIRE_EQ(WTERMSIG(status), sig);
212 		ATF_REQUIRE_EQ(!!WCOREDUMP(status), expect_core);
213 		break;
214 #ifdef VFORK
215 	case SIGTSTP:
216 	case SIGTTIN:
217 	case SIGTTOU:
218 #endif
219 	case SIGCONT:
220 		ATF_REQUIRE(WIFEXITED(status));
221 		ATF_REQUIRE(!WIFCONTINUED(status));
222 		ATF_REQUIRE(!WIFSIGNALED(status));
223 		ATF_REQUIRE(!WIFSTOPPED(status));
224 		ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
225 		break;
226 #ifndef VFORK
227 	case SIGTSTP:
228 	case SIGTTIN:
229 	case SIGTTOU:
230 #endif
231 	case SIGSTOP:
232 		ATF_REQUIRE(!WIFEXITED(status));
233 		ATF_REQUIRE(!WIFCONTINUED(status));
234 		ATF_REQUIRE(WIFSIGNALED(status));
235 		ATF_REQUIRE(!WIFSTOPPED(status));
236 		ATF_REQUIRE_EQ(WTERMSIG(status), SIGKILL);
237 		ATF_REQUIRE_EQ(!!WCOREDUMP(status), 0);
238 	}
239 }
240 
241 #define RAISE(test, sig)							\
242 ATF_TC(test);									\
243 ATF_TC_HEAD(test, tc)								\
244 {										\
245 										\
246 	atf_tc_set_md_var(tc, "descr",						\
247 	    "raise " #sig " in a child");					\
248 }										\
249 										\
250 ATF_TC_BODY(test, tc)								\
251 {										\
252 										\
253 	raise_raw(sig);								\
254 }
255 
RAISE(raise1,SIGKILL)256 RAISE(raise1, SIGKILL) /* non-maskable */
257 RAISE(raise2, SIGSTOP) /* non-maskable */
258 RAISE(raise3, SIGTSTP) /* ignored in vfork(2) */
259 RAISE(raise4, SIGTTIN) /* ignored in vfork(2) */
260 RAISE(raise5, SIGTTOU) /* ignored in vfork(2) */
261 RAISE(raise6, SIGABRT) /* regular abort trap */
262 RAISE(raise7, SIGHUP)  /* hangup */
263 RAISE(raise8, SIGCONT) /* continued? */
264 
265 /// ----------------------------------------------------------------------------
266 
267 static int
268 clone_func(void *arg __unused)
269 {
270 
271 	return 0;
272 }
273 
274 static void
nested_raw(const char * fn,volatile int flags)275 nested_raw(const char *fn, volatile int flags)
276 {
277 	int status;
278 	pid_t child, child2, wpid;
279 	const size_t stack_size = 1024 * 1024;
280 	void *stack, *stack_base;
281 
282 	stack = malloc(stack_size);
283 	ATF_REQUIRE(stack != NULL);
284 
285 #ifdef __MACHINE_STACK_GROWS_UP
286 	stack_base = stack;
287 #else
288 	stack_base = (char *)stack + stack_size;
289 #endif
290 
291 	flags |= SIGCHLD;
292 
293 	child = FORK();
294 	ATF_REQUIRE(child != 1);
295 	if (child == 0) {
296 		if (strcmp(fn, "fork") == 0)
297 			child2 = fork();
298 		else if (strcmp(fn, "vfork") == 0)
299 			child2 = vfork();
300 #ifndef __OpenBSD__
301 		else if (strcmp(fn, "clone") == 0)
302 			child2 = __clone(clone_func, stack_base, flags, NULL);
303 #endif
304 		else
305 			__unreachable();
306 
307 		ASSERT_NEQ(child2, -1);
308 
309 		if ((strcmp(fn, "fork") == 0) || (strcmp(fn, "vfork") == 0)) {
310 			if (child2 == 0)
311 				_exit(0);
312 		}
313 
314 		wpid = waitpid(child2, &status, 0);
315 		ASSERT_EQ(child2, wpid);
316 		ASSERT_EQ(!!WIFEXITED(status), true);
317 		ASSERT_EQ(!!WIFCONTINUED(status), false);
318 		ASSERT_EQ(!!WIFSIGNALED(status), false);
319 		ASSERT_EQ(!!WIFSTOPPED(status), false);
320 		ASSERT_EQ(WEXITSTATUS(status), 0);
321 
322 		_exit(0);
323 	}
324 	wpid = waitpid(child, &status, 0);
325 
326 	ATF_REQUIRE_EQ(wpid, child);
327 	ATF_REQUIRE_EQ(!!WIFEXITED(status), true);
328 	ATF_REQUIRE_EQ(!!WIFCONTINUED(status), false);
329 	ATF_REQUIRE_EQ(!!WIFSIGNALED(status), false);
330 	ATF_REQUIRE_EQ(!!WIFSTOPPED(status), false);
331 	ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
332 }
333 
334 #define NESTED(test, fn, flags)							\
335 ATF_TC(test);									\
336 ATF_TC_HEAD(test, tc)								\
337 {										\
338 										\
339 	atf_tc_set_md_var(tc, "descr",						\
340 	    "Test nested " #fn " in a child");					\
341 }										\
342 										\
343 ATF_TC_BODY(test, tc)								\
344 {										\
345 										\
346 	nested_raw(#fn, flags);							\
347 }
348 
349 NESTED(nested_fork, fork, 0)
350 NESTED(nested_vfork, vfork, 0)
351 #ifndef __OpenBSD__
352 NESTED(nested_clone, clone, 0)
NESTED(nested_clone_vm,clone,CLONE_VM)353 NESTED(nested_clone_vm, clone, CLONE_VM)
354 NESTED(nested_clone_fs, clone, CLONE_FS)
355 NESTED(nested_clone_files, clone, CLONE_FILES)
356 //NESTED(nested_clone_sighand, clone, CLONE_SIGHAND) // XXX
357 NESTED(nested_clone_vfork, clone, CLONE_VFORK)
358 #endif
359 
360 ATF_TP_ADD_TCS(tp)
361 {
362 	ATF_TP_ADD_TC(tp, raise1);
363 	ATF_TP_ADD_TC(tp, raise2);
364 	ATF_TP_ADD_TC(tp, raise3);
365 	ATF_TP_ADD_TC(tp, raise4);
366 	ATF_TP_ADD_TC(tp, raise5);
367 	ATF_TP_ADD_TC(tp, raise6);
368 	ATF_TP_ADD_TC(tp, raise7);
369 	ATF_TP_ADD_TC(tp, raise8);
370 
371 	ATF_TP_ADD_TC(tp, nested_fork);
372 	ATF_TP_ADD_TC(tp, nested_vfork);
373 #ifndef __OpenBSD__
374 	ATF_TP_ADD_TC(tp, nested_clone);
375 	ATF_TP_ADD_TC(tp, nested_clone_vm);
376 	ATF_TP_ADD_TC(tp, nested_clone_fs);
377 	ATF_TP_ADD_TC(tp, nested_clone_files);
378 //	ATF_TP_ADD_TC(tp, nested_clone_sighand); // XXX
379 	ATF_TP_ADD_TC(tp, nested_clone_vfork);
380 #endif
381 
382 	return atf_no_error();
383 }
384