xref: /linux/tools/testing/selftests/pidfd/pidfd_info_test.c (revision 56f235da15d02c550c0ae25da9c62ecfe7222d38)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #define _GNU_SOURCE
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <limits.h>
7 #include <linux/types.h>
8 #include <poll.h>
9 #include <pthread.h>
10 #include <sched.h>
11 #include <signal.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <syscall.h>
16 #include <sys/prctl.h>
17 #include <sys/wait.h>
18 #include <unistd.h>
19 #include <sys/socket.h>
20 #include <linux/kcmp.h>
21 #include <sys/stat.h>
22 
23 #include "pidfd.h"
24 #include "../kselftest_harness.h"
25 
26 FIXTURE(pidfd_info)
27 {
28 	pid_t child_pid1;
29 	int child_pidfd1;
30 
31 	pid_t child_pid2;
32 	int child_pidfd2;
33 
34 	pid_t child_pid3;
35 	int child_pidfd3;
36 
37 	pid_t child_pid4;
38 	int child_pidfd4;
39 };
40 
41 FIXTURE_SETUP(pidfd_info)
42 {
43 	int ret;
44 	int ipc_sockets[2];
45 	char c;
46 
47 	ret = socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);
48 	EXPECT_EQ(ret, 0);
49 
50 	self->child_pid1 = create_child(&self->child_pidfd1, 0);
51 	EXPECT_GE(self->child_pid1, 0);
52 
53 	if (self->child_pid1 == 0) {
54 		close(ipc_sockets[0]);
55 
56 		if (write_nointr(ipc_sockets[1], "1", 1) < 0)
57 			_exit(EXIT_FAILURE);
58 
59 		close(ipc_sockets[1]);
60 
61 		pause();
62 		_exit(EXIT_SUCCESS);
63 	}
64 
65 	EXPECT_EQ(close(ipc_sockets[1]), 0);
66 	ASSERT_EQ(read_nointr(ipc_sockets[0], &c, 1), 1);
67 	EXPECT_EQ(close(ipc_sockets[0]), 0);
68 
69 	/* SIGKILL but don't reap. */
70 	EXPECT_EQ(sys_pidfd_send_signal(self->child_pidfd1, SIGKILL, NULL, 0), 0);
71 
72 	ret = socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);
73 	EXPECT_EQ(ret, 0);
74 
75 	self->child_pid2 = create_child(&self->child_pidfd2, 0);
76 	EXPECT_GE(self->child_pid2, 0);
77 
78 	if (self->child_pid2 == 0) {
79 		close(ipc_sockets[0]);
80 
81 		if (write_nointr(ipc_sockets[1], "1", 1) < 0)
82 			_exit(EXIT_FAILURE);
83 
84 		close(ipc_sockets[1]);
85 
86 		pause();
87 		_exit(EXIT_SUCCESS);
88 	}
89 
90 	EXPECT_EQ(close(ipc_sockets[1]), 0);
91 	ASSERT_EQ(read_nointr(ipc_sockets[0], &c, 1), 1);
92 	EXPECT_EQ(close(ipc_sockets[0]), 0);
93 
94 	/* SIGKILL and reap. */
95 	EXPECT_EQ(sys_pidfd_send_signal(self->child_pidfd2, SIGKILL, NULL, 0), 0);
96 	EXPECT_EQ(sys_waitid(P_PID, self->child_pid2, NULL, WEXITED), 0);
97 
98 	self->child_pid3 = create_child(&self->child_pidfd3, CLONE_NEWUSER | CLONE_NEWPID);
99 	EXPECT_GE(self->child_pid3, 0);
100 
101 	if (self->child_pid3 == 0)
102 		_exit(EXIT_SUCCESS);
103 
104 	self->child_pid4 = create_child(&self->child_pidfd4, CLONE_NEWUSER | CLONE_NEWPID);
105 	EXPECT_GE(self->child_pid4, 0);
106 
107 	if (self->child_pid4 == 0)
108 		_exit(EXIT_SUCCESS);
109 
110 	EXPECT_EQ(sys_waitid(P_PID, self->child_pid4, NULL, WEXITED), 0);
111 }
112 
113 FIXTURE_TEARDOWN(pidfd_info)
114 {
115 	sys_pidfd_send_signal(self->child_pidfd1, SIGKILL, NULL, 0);
116 	if (self->child_pidfd1 >= 0)
117 		EXPECT_EQ(0, close(self->child_pidfd1));
118 
119 	sys_waitid(P_PID, self->child_pid1, NULL, WEXITED);
120 
121 	sys_pidfd_send_signal(self->child_pidfd2, SIGKILL, NULL, 0);
122 	if (self->child_pidfd2 >= 0)
123 		EXPECT_EQ(0, close(self->child_pidfd2));
124 
125 	sys_waitid(P_PID, self->child_pid2, NULL, WEXITED);
126 	sys_waitid(P_PID, self->child_pid3, NULL, WEXITED);
127 	sys_waitid(P_PID, self->child_pid4, NULL, WEXITED);
128 }
129 
130 TEST_F(pidfd_info, sigkill_exit)
131 {
132 	struct pidfd_info info = {
133 		.mask = PIDFD_INFO_CGROUPID,
134 	};
135 
136 	/* Process has exited but not been reaped so this must work. */
137 	ASSERT_EQ(ioctl(self->child_pidfd1, PIDFD_GET_INFO, &info), 0);
138 
139 	info.mask = PIDFD_INFO_CGROUPID | PIDFD_INFO_EXIT;
140 	ASSERT_EQ(ioctl(self->child_pidfd1, PIDFD_GET_INFO, &info), 0);
141 	ASSERT_TRUE(!!(info.mask & PIDFD_INFO_CREDS));
142 	/* Process has exited but not been reaped, so no PIDFD_INFO_EXIT information yet. */
143 	ASSERT_FALSE(!!(info.mask & PIDFD_INFO_EXIT));
144 }
145 
146 TEST_F(pidfd_info, sigkill_reaped)
147 {
148 	struct pidfd_info info = {
149 		.mask = PIDFD_INFO_CGROUPID,
150 	};
151 
152 	/* Process has already been reaped and PIDFD_INFO_EXIT hasn't been set. */
153 	ASSERT_NE(ioctl(self->child_pidfd2, PIDFD_GET_INFO, &info), 0);
154 	ASSERT_EQ(errno, ESRCH);
155 
156 	info.mask = PIDFD_INFO_CGROUPID | PIDFD_INFO_EXIT;
157 	ASSERT_EQ(ioctl(self->child_pidfd2, PIDFD_GET_INFO, &info), 0);
158 	ASSERT_FALSE(!!(info.mask & PIDFD_INFO_CREDS));
159 	ASSERT_TRUE(!!(info.mask & PIDFD_INFO_EXIT));
160 	ASSERT_TRUE(WIFSIGNALED(info.exit_code));
161 	ASSERT_EQ(WTERMSIG(info.exit_code), SIGKILL);
162 }
163 
164 TEST_F(pidfd_info, success_exit)
165 {
166 	struct pidfd_info info = {
167 		.mask = PIDFD_INFO_CGROUPID,
168 	};
169 
170 	/* Process has exited but not been reaped so this must work. */
171 	ASSERT_EQ(ioctl(self->child_pidfd3, PIDFD_GET_INFO, &info), 0);
172 
173 	info.mask = PIDFD_INFO_CGROUPID | PIDFD_INFO_EXIT;
174 	ASSERT_EQ(ioctl(self->child_pidfd3, PIDFD_GET_INFO, &info), 0);
175 	ASSERT_TRUE(!!(info.mask & PIDFD_INFO_CREDS));
176 	/* Process has exited but not been reaped, so no PIDFD_INFO_EXIT information yet. */
177 	ASSERT_FALSE(!!(info.mask & PIDFD_INFO_EXIT));
178 }
179 
180 TEST_F(pidfd_info, success_reaped)
181 {
182 	struct pidfd_info info = {
183 		.mask = PIDFD_INFO_CGROUPID,
184 	};
185 
186 	/* Process has already been reaped and PIDFD_INFO_EXIT hasn't been set. */
187 	ASSERT_NE(ioctl(self->child_pidfd4, PIDFD_GET_INFO, &info), 0);
188 	ASSERT_EQ(errno, ESRCH);
189 
190 	info.mask = PIDFD_INFO_CGROUPID | PIDFD_INFO_EXIT;
191 	ASSERT_EQ(ioctl(self->child_pidfd4, PIDFD_GET_INFO, &info), 0);
192 	ASSERT_FALSE(!!(info.mask & PIDFD_INFO_CREDS));
193 	ASSERT_TRUE(!!(info.mask & PIDFD_INFO_EXIT));
194 	ASSERT_TRUE(WIFEXITED(info.exit_code));
195 	ASSERT_EQ(WEXITSTATUS(info.exit_code), 0);
196 }
197 
198 TEST_F(pidfd_info, success_reaped_poll)
199 {
200 	struct pidfd_info info = {
201 		.mask = PIDFD_INFO_CGROUPID | PIDFD_INFO_EXIT,
202 	};
203 	struct pollfd fds = {};
204 	int nevents;
205 
206 	fds.events = POLLIN;
207 	fds.fd = self->child_pidfd2;
208 
209 	nevents = poll(&fds, 1, -1);
210 	ASSERT_EQ(nevents, 1);
211 	ASSERT_TRUE(!!(fds.revents & POLLIN));
212 	ASSERT_TRUE(!!(fds.revents & POLLHUP));
213 
214 	ASSERT_EQ(ioctl(self->child_pidfd2, PIDFD_GET_INFO, &info), 0);
215 	ASSERT_FALSE(!!(info.mask & PIDFD_INFO_CREDS));
216 	ASSERT_TRUE(!!(info.mask & PIDFD_INFO_EXIT));
217 	ASSERT_TRUE(WIFSIGNALED(info.exit_code));
218 	ASSERT_EQ(WTERMSIG(info.exit_code), SIGKILL);
219 }
220 
221 static void *pidfd_info_pause_thread(void *arg)
222 {
223 	pid_t pid_thread = gettid();
224 	int ipc_socket = *(int *)arg;
225 
226 	/* Inform the grand-parent what the tid of this thread is. */
227 	if (write_nointr(ipc_socket, &pid_thread, sizeof(pid_thread)) != sizeof(pid_thread))
228 		return NULL;
229 
230 	close(ipc_socket);
231 
232 	/* Sleep untill we're killed. */
233 	pause();
234 	return NULL;
235 }
236 
237 TEST_F(pidfd_info, thread_group)
238 {
239 	pid_t pid_leader, pid_thread;
240 	pthread_t thread;
241 	int nevents, pidfd_leader, pidfd_thread, pidfd_leader_thread, ret;
242 	int ipc_sockets[2];
243 	struct pollfd fds = {};
244 	struct pidfd_info info = {
245 		.mask = PIDFD_INFO_CGROUPID | PIDFD_INFO_EXIT,
246 	}, info2;
247 
248 	ret = socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);
249 	EXPECT_EQ(ret, 0);
250 
251 	pid_leader = create_child(&pidfd_leader, 0);
252 	EXPECT_GE(pid_leader, 0);
253 
254 	if (pid_leader == 0) {
255 		close(ipc_sockets[0]);
256 
257 		/* The thread will outlive the thread-group leader. */
258 		if (pthread_create(&thread, NULL, pidfd_info_pause_thread, &ipc_sockets[1]))
259 			syscall(__NR_exit, EXIT_FAILURE);
260 
261 		/* Make the thread-group leader exit prematurely. */
262 		syscall(__NR_exit, EXIT_SUCCESS);
263 	}
264 
265 	/* Retrieve the tid of the thread. */
266 	EXPECT_EQ(close(ipc_sockets[1]), 0);
267 	ASSERT_EQ(read_nointr(ipc_sockets[0], &pid_thread, sizeof(pid_thread)), sizeof(pid_thread));
268 	EXPECT_EQ(close(ipc_sockets[0]), 0);
269 
270 	/* Opening a thread as a thread-group leader must fail. */
271 	pidfd_thread = sys_pidfd_open(pid_thread, 0);
272 	ASSERT_LT(pidfd_thread, 0);
273 
274 	/* Opening a thread as a PIDFD_THREAD must succeed. */
275 	pidfd_thread = sys_pidfd_open(pid_thread, PIDFD_THREAD);
276 	ASSERT_GE(pidfd_thread, 0);
277 
278 	/*
279 	 * Opening a PIDFD_THREAD aka thread-specific pidfd based on a
280 	 * thread-group leader must succeed.
281 	 */
282 	pidfd_leader_thread = sys_pidfd_open(pid_leader, PIDFD_THREAD);
283 	ASSERT_GE(pidfd_leader_thread, 0);
284 
285 	/*
286 	 * Note that pidfd_leader is a thread-group pidfd, so polling on it
287 	 * would only notify us once all thread in the thread-group have
288 	 * exited. So we can't poll before we have taken down the whole
289 	 * thread-group.
290 	 */
291 
292 	/* Get PIDFD_GET_INFO using the thread-group leader pidfd. */
293 	ASSERT_EQ(ioctl(pidfd_leader, PIDFD_GET_INFO, &info), 0);
294 	ASSERT_TRUE(!!(info.mask & PIDFD_INFO_CREDS));
295 	/* Process has exited but not been reaped, so no PIDFD_INFO_EXIT information yet. */
296 	ASSERT_FALSE(!!(info.mask & PIDFD_INFO_EXIT));
297 	ASSERT_EQ(info.pid, pid_leader);
298 
299 	/*
300 	 * Now retrieve the same info using the thread specific pidfd
301 	 * for the thread-group leader.
302 	 */
303 	info2.mask = PIDFD_INFO_CGROUPID | PIDFD_INFO_EXIT;
304 	ASSERT_EQ(ioctl(pidfd_leader_thread, PIDFD_GET_INFO, &info2), 0);
305 	ASSERT_TRUE(!!(info2.mask & PIDFD_INFO_CREDS));
306 	/* Process has exited but not been reaped, so no PIDFD_INFO_EXIT information yet. */
307 	ASSERT_FALSE(!!(info2.mask & PIDFD_INFO_EXIT));
308 	ASSERT_EQ(info2.pid, pid_leader);
309 
310 	/* Now try the thread-specific pidfd. */
311 	ASSERT_EQ(ioctl(pidfd_thread, PIDFD_GET_INFO, &info), 0);
312 	ASSERT_TRUE(!!(info.mask & PIDFD_INFO_CREDS));
313 	/* The thread hasn't exited, so no PIDFD_INFO_EXIT information yet. */
314 	ASSERT_FALSE(!!(info.mask & PIDFD_INFO_EXIT));
315 	ASSERT_EQ(info.pid, pid_thread);
316 
317 	/*
318 	 * Take down the whole thread-group. The thread-group leader
319 	 * exited successfully but the thread will now be SIGKILLed.
320 	 * This must be reflected in the recorded exit information.
321 	 */
322 	EXPECT_EQ(sys_pidfd_send_signal(pidfd_leader, SIGKILL, NULL, 0), 0);
323 	EXPECT_EQ(sys_waitid(P_PIDFD, pidfd_leader, NULL, WEXITED), 0);
324 
325 	fds.events = POLLIN;
326 	fds.fd = pidfd_leader;
327 	nevents = poll(&fds, 1, -1);
328 	ASSERT_EQ(nevents, 1);
329 	ASSERT_TRUE(!!(fds.revents & POLLIN));
330 	/* The thread-group leader has been reaped. */
331 	ASSERT_TRUE(!!(fds.revents & POLLHUP));
332 
333 	/*
334 	 * Retrieve exit information for the thread-group leader via the
335 	 * thread-group leader pidfd.
336 	 */
337 	info.mask = PIDFD_INFO_CGROUPID | PIDFD_INFO_EXIT;
338 	ASSERT_EQ(ioctl(pidfd_leader, PIDFD_GET_INFO, &info), 0);
339 	ASSERT_FALSE(!!(info.mask & PIDFD_INFO_CREDS));
340 	ASSERT_TRUE(!!(info.mask & PIDFD_INFO_EXIT));
341 	/* The thread-group leader exited successfully. Only the specific thread was SIGKILLed. */
342 	ASSERT_TRUE(WIFEXITED(info.exit_code));
343 	ASSERT_EQ(WEXITSTATUS(info.exit_code), 0);
344 
345 	/*
346 	 * Retrieve exit information for the thread-group leader via the
347 	 * thread-specific pidfd.
348 	 */
349 	info2.mask = PIDFD_INFO_CGROUPID | PIDFD_INFO_EXIT;
350 	ASSERT_EQ(ioctl(pidfd_leader_thread, PIDFD_GET_INFO, &info2), 0);
351 	ASSERT_FALSE(!!(info2.mask & PIDFD_INFO_CREDS));
352 	ASSERT_TRUE(!!(info2.mask & PIDFD_INFO_EXIT));
353 
354 	/* The thread-group leader exited successfully. Only the specific thread was SIGKILLed. */
355 	ASSERT_TRUE(WIFEXITED(info2.exit_code));
356 	ASSERT_EQ(WEXITSTATUS(info2.exit_code), 0);
357 
358 	/* Retrieve exit information for the thread. */
359 	info.mask = PIDFD_INFO_CGROUPID | PIDFD_INFO_EXIT;
360 	ASSERT_EQ(ioctl(pidfd_thread, PIDFD_GET_INFO, &info), 0);
361 	ASSERT_FALSE(!!(info.mask & PIDFD_INFO_CREDS));
362 	ASSERT_TRUE(!!(info.mask & PIDFD_INFO_EXIT));
363 
364 	/* The thread got SIGKILLed. */
365 	ASSERT_TRUE(WIFSIGNALED(info.exit_code));
366 	ASSERT_EQ(WTERMSIG(info.exit_code), SIGKILL);
367 
368 	EXPECT_EQ(close(pidfd_leader), 0);
369 	EXPECT_EQ(close(pidfd_thread), 0);
370 }
371 
372 static void *pidfd_info_thread_exec(void *arg)
373 {
374 	pid_t pid_thread = gettid();
375 	int ipc_socket = *(int *)arg;
376 
377 	/* Inform the grand-parent what the tid of this thread is. */
378 	if (write_nointr(ipc_socket, &pid_thread, sizeof(pid_thread)) != sizeof(pid_thread))
379 		return NULL;
380 
381 	if (read_nointr(ipc_socket, &pid_thread, sizeof(pid_thread)) != sizeof(pid_thread))
382 		return NULL;
383 
384 	close(ipc_socket);
385 
386 	sys_execveat(AT_FDCWD, "pidfd_exec_helper", NULL, NULL, 0);
387 	return NULL;
388 }
389 
390 TEST_F(pidfd_info, thread_group_exec)
391 {
392 	pid_t pid_leader, pid_thread;
393 	pthread_t thread;
394 	int nevents, pidfd_leader, pidfd_leader_thread, pidfd_thread, ret;
395 	int ipc_sockets[2];
396 	struct pollfd fds = {};
397 	struct pidfd_info info = {
398 		.mask = PIDFD_INFO_CGROUPID | PIDFD_INFO_EXIT,
399 	};
400 
401 	ret = socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);
402 	EXPECT_EQ(ret, 0);
403 
404 	pid_leader = create_child(&pidfd_leader, 0);
405 	EXPECT_GE(pid_leader, 0);
406 
407 	if (pid_leader == 0) {
408 		close(ipc_sockets[0]);
409 
410 		/* The thread will outlive the thread-group leader. */
411 		if (pthread_create(&thread, NULL, pidfd_info_thread_exec, &ipc_sockets[1]))
412 			syscall(__NR_exit, EXIT_FAILURE);
413 
414 		/* Make the thread-group leader exit prematurely. */
415 		syscall(__NR_exit, EXIT_SUCCESS);
416 	}
417 
418 	/* Retrieve the tid of the thread. */
419 	EXPECT_EQ(close(ipc_sockets[1]), 0);
420 	ASSERT_EQ(read_nointr(ipc_sockets[0], &pid_thread, sizeof(pid_thread)), sizeof(pid_thread));
421 
422 	/* Opening a thread as a PIDFD_THREAD must succeed. */
423 	pidfd_thread = sys_pidfd_open(pid_thread, PIDFD_THREAD);
424 	ASSERT_GE(pidfd_thread, 0);
425 
426 	/* Open a thread-specific pidfd for the thread-group leader. */
427 	pidfd_leader_thread = sys_pidfd_open(pid_leader, PIDFD_THREAD);
428 	ASSERT_GE(pidfd_leader_thread, 0);
429 
430 	/*
431 	 * We can poll and wait for the old thread-group leader to exit
432 	 * using a thread-specific pidfd.
433 	 *
434 	 * This only works until the thread has execed. When the thread
435 	 * has execed it will have taken over the old thread-group
436 	 * leaders struct pid. Calling poll after the thread execed will
437 	 * thus block again because a new thread-group has started (Yes,
438 	 * it's fscked.).
439 	 */
440 	fds.events = POLLIN;
441 	fds.fd = pidfd_leader_thread;
442 	nevents = poll(&fds, 1, -1);
443 	ASSERT_EQ(nevents, 1);
444 	/* The thread-group leader has exited. */
445 	ASSERT_TRUE(!!(fds.revents & POLLIN));
446 	/* The thread-group leader hasn't been reaped. */
447 	ASSERT_FALSE(!!(fds.revents & POLLHUP));
448 
449 	/* Now that we've opened a thread-specific pidfd the thread can exec. */
450 	ASSERT_EQ(write_nointr(ipc_sockets[0], &pid_thread, sizeof(pid_thread)), sizeof(pid_thread));
451 	EXPECT_EQ(close(ipc_sockets[0]), 0);
452 
453 	/* Wait until the kernel has SIGKILLed the thread. */
454 	fds.events = POLLHUP;
455 	fds.fd = pidfd_thread;
456 	nevents = poll(&fds, 1, -1);
457 	ASSERT_EQ(nevents, 1);
458 	/* The thread has been reaped. */
459 	ASSERT_TRUE(!!(fds.revents & POLLHUP));
460 
461 	/* Retrieve thread-specific exit info from pidfd. */
462 	ASSERT_EQ(ioctl(pidfd_thread, PIDFD_GET_INFO, &info), 0);
463 	ASSERT_FALSE(!!(info.mask & PIDFD_INFO_CREDS));
464 	ASSERT_TRUE(!!(info.mask & PIDFD_INFO_EXIT));
465 	/*
466 	 * While the kernel will have SIGKILLed the whole thread-group
467 	 * during exec it will cause the individual threads to exit
468 	 * cleanly.
469 	 */
470 	ASSERT_TRUE(WIFEXITED(info.exit_code));
471 	ASSERT_EQ(WEXITSTATUS(info.exit_code), 0);
472 
473 	/*
474 	 * The thread-group leader is still alive, the thread has taken
475 	 * over its struct pid and thus its pid number.
476 	 */
477 	info.mask = PIDFD_INFO_CGROUPID | PIDFD_INFO_EXIT;
478 	ASSERT_EQ(ioctl(pidfd_leader, PIDFD_GET_INFO, &info), 0);
479 	ASSERT_TRUE(!!(info.mask & PIDFD_INFO_CREDS));
480 	ASSERT_FALSE(!!(info.mask & PIDFD_INFO_EXIT));
481 	ASSERT_EQ(info.pid, pid_leader);
482 
483 	/* Take down the thread-group leader. */
484 	EXPECT_EQ(sys_pidfd_send_signal(pidfd_leader, SIGKILL, NULL, 0), 0);
485 	EXPECT_EQ(sys_waitid(P_PIDFD, pidfd_leader, NULL, WEXITED), 0);
486 
487 	/* Retrieve exit information for the thread-group leader. */
488 	info.mask = PIDFD_INFO_CGROUPID | PIDFD_INFO_EXIT;
489 	ASSERT_EQ(ioctl(pidfd_leader, PIDFD_GET_INFO, &info), 0);
490 	ASSERT_FALSE(!!(info.mask & PIDFD_INFO_CREDS));
491 	ASSERT_TRUE(!!(info.mask & PIDFD_INFO_EXIT));
492 
493 	EXPECT_EQ(close(pidfd_leader), 0);
494 	EXPECT_EQ(close(pidfd_thread), 0);
495 }
496 
497 TEST_HARNESS_MAIN
498