1 /*
2 * Copyright (c) 2023 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /*
30 * These tests verify that proc_pidinfo returns the expected exit reason and
31 * namespace for signal-related process termination.
32 */
33
34 #include <darwintest.h>
35 #include <signal.h>
36 #include <libproc.h>
37 #include <sys/wait.h>
38 #include <sys/reason.h>
39 #include <stdlib.h>
40 #include <dispatch/dispatch.h>
41 #include <unistd.h>
42 #include <mach/kern_return.h>
43 #include <mach/mach.h>
44 #include <mach/mach_port.h>
45 #include <mach/message.h>
46 #include <mach/port.h>
47
48
49 T_GLOBAL_META(
50 T_META_NAMESPACE("xnu.misc"),
51 T_META_RADAR_COMPONENT_NAME("xnu"),
52 T_META_RADAR_COMPONENT_VERSION("misc"),
53 T_META_RUN_CONCURRENTLY(true));
54
55 static dispatch_queue_t exit_queue;
56
57 static void
cleanup(void)58 cleanup(void)
59 {
60 dispatch_release(exit_queue);
61 }
62
63 static void
64 dispatch_test(void (^test)(void))
65 {
66 // Use dispatch to schedule DISPATCH_PROC_EXIT blocks to read out exit reasons
67 exit_queue = dispatch_queue_create("exit queue", DISPATCH_QUEUE_SERIAL);
68 dispatch_async(dispatch_get_main_queue(), ^{
69 test();
70 });
71 T_ATEND(cleanup);
72 dispatch_main();
73 }
74
75 static bool
audit_token_for_pid(pid_t pid,audit_token_t * token)76 audit_token_for_pid(pid_t pid, audit_token_t *token)
77 {
78 kern_return_t err;
79 task_name_t task_name = TASK_NAME_NULL;
80 mach_msg_type_number_t info_size = TASK_AUDIT_TOKEN_COUNT;
81
82 err = task_name_for_pid(mach_task_self(), pid, &task_name);
83 if (err != KERN_SUCCESS) {
84 T_LOG("task_for_pid returned %d\n", err);
85 return false;
86 }
87
88 err = task_info(task_name, TASK_AUDIT_TOKEN, (integer_t *)token, &info_size);
89 if (err != KERN_SUCCESS) {
90 T_LOG("task_info returned %d\n", err);
91 return false;
92 }
93
94 return true;
95 }
96
97 static void
check_exit_reason(int pid,uint64_t expected_reason_namespace,uint64_t expected_signal)98 check_exit_reason(int pid, uint64_t expected_reason_namespace, uint64_t expected_signal)
99 {
100 T_LOG("check_exit_reason %d", expected_signal);
101 int ret, status;
102 struct proc_exitreasonbasicinfo exit_reason;
103
104 ret = proc_pidinfo(pid, PROC_PIDEXITREASONBASICINFO, 1, &exit_reason, PROC_PIDEXITREASONBASICINFOSIZE);
105 T_WITH_ERRNO; T_QUIET; T_ASSERT_EQ(ret, PROC_PIDEXITREASONBASICINFOSIZE, "retrieve basic exit reason info");
106
107 waitpid(pid, &status, 0);
108 T_QUIET; T_EXPECT_FALSE(WIFEXITED(status), "process did not exit normally");
109 T_QUIET; T_EXPECT_TRUE(WIFSIGNALED(status), "process was terminated because of a signal");
110 T_QUIET; T_EXPECT_EQ(WTERMSIG(status), expected_signal, "process should terminate due to signal %llu", expected_signal);
111
112 T_EXPECT_EQ(exit_reason.beri_namespace, expected_reason_namespace, "expect OS_REASON_SIGNAL");
113 T_EXPECT_EQ(exit_reason.beri_code, expected_signal, "expect reason code: %llu", expected_signal);
114 }
115
116 static void
wait_collect_exit_reason(int pid,int signal)117 wait_collect_exit_reason(int pid, int signal)
118 {
119 dispatch_source_t ds_proc = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, pid, DISPATCH_PROC_EXIT, exit_queue);
120 dispatch_semaphore_t sem = dispatch_semaphore_create(0);
121 dispatch_source_set_event_handler(ds_proc, ^{
122 check_exit_reason(pid, OS_REASON_SIGNAL, signal);
123 dispatch_semaphore_signal(sem);
124 });
125 dispatch_activate(ds_proc);
126
127 // Wait till exit reason is processed
128 dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
129 dispatch_release(ds_proc);
130 dispatch_release(sem);
131 }
132
133 static void
wait_with_timeout_expected(int pid,int seconds)134 wait_with_timeout_expected(int pid, int seconds)
135 {
136 long timeout = 0;
137 dispatch_source_t ds_proc = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, pid, DISPATCH_PROC_EXIT, exit_queue);
138 dispatch_semaphore_t sem = dispatch_semaphore_create(0);
139 dispatch_time_t milestone = dispatch_time(DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC);;
140 dispatch_source_set_event_handler(ds_proc, ^{
141 dispatch_semaphore_signal(sem);
142 });
143 dispatch_activate(ds_proc);
144
145 // Wait till exit reason is processed or timeout
146 timeout = dispatch_semaphore_wait(sem, milestone);
147 T_QUIET; T_EXPECT_TRUE(timeout != 0, "process exited and was not expected to");
148 dispatch_release(ds_proc);
149 dispatch_release(sem);
150 }
151
152 static void
__test_exit_reason_abort()153 __test_exit_reason_abort()
154 {
155 pid_t child = fork();
156 if (child > 0) {
157 wait_collect_exit_reason(child, SIGABRT);
158 } else {
159 abort();
160 }
161 }
162
163 T_DECL(test_exit_reason_abort, "tests exit reason for abort()", T_META_TAG_VM_PREFERRED)
164 {
165 dispatch_test(^{
166 __test_exit_reason_abort();
167 T_END;
168 });
169 }
170
171 static void
__test_exit_reason_external_signal(int signal)172 __test_exit_reason_external_signal(int signal)
173 {
174 T_LOG("Testing external signal %d", signal);
175 pid_t child = fork();
176 if (child > 0) {
177 // Send external signal
178 kill(child, signal);
179 wait_collect_exit_reason(child, signal);
180 } else {
181 pause();
182 }
183 }
184
185 static void
__test_exit_reason_delegate_signal(int signal)186 __test_exit_reason_delegate_signal(int signal)
187 {
188 int ret = 0;
189 audit_token_t instigator = INVALID_AUDIT_TOKEN_VALUE;
190 audit_token_t token = INVALID_AUDIT_TOKEN_VALUE;
191 pid_t child = fork();
192 if (child > 0) {
193 audit_token_for_pid(child, &token);
194 // Send signal to the child with its audit token
195 ret = proc_signal_delegate(instigator, token, signal);
196 T_EXPECT_EQ_INT(ret, 0, "expect proc_signal_delegate return: %d", ret);
197 wait_collect_exit_reason(child, signal);
198 // Send signal to the child with its audit token who has exited by now
199 ret = proc_signal_delegate(instigator, token, signal);
200 T_EXPECT_EQ_INT(ret, ESRCH, "expect no such process return: %d", ret);
201 } else {
202 pause();
203 // This exit should not hit, but we exit abnormally in case something went wrong
204 _exit(-1);
205 }
206 }
207
208 static void
__test_exit_reason_delegate_terminate()209 __test_exit_reason_delegate_terminate()
210 {
211 int ret = 0;
212 audit_token_t instigator = INVALID_AUDIT_TOKEN_VALUE;
213 audit_token_t token = INVALID_AUDIT_TOKEN_VALUE;
214 pid_t child = fork();
215 int sentsignal = 0;
216 if (child > 0) {
217 audit_token_for_pid(child, &token);
218 // Send signal to the child with its audit token
219 ret = proc_terminate_delegate(instigator, token, &sentsignal);
220 T_EXPECT_EQ_INT(ret, 0, "expect proc_terminate_delegate return: %d", ret);
221 T_EXPECT_TRUE(sentsignal == SIGTERM || sentsignal == SIGKILL, "sentsignal retval %d", sentsignal);
222 wait_collect_exit_reason(child, SIGTERM);
223 // Send signal to the child with its audit token who has exited by now
224 ret = proc_terminate_delegate(instigator, token, &sentsignal);
225 T_EXPECT_EQ_INT(ret, ESRCH, "expect no such process return: %d", ret);
226 } else {
227 pause();
228 // This exit should not hit, but we exit abnormally in case something went wrong
229 _exit(-1);
230 }
231 }
232
233 static void
__test_exit_reason_signal_with_audittoken(int signal)234 __test_exit_reason_signal_with_audittoken(int signal)
235 {
236 int ret = 0;
237 audit_token_t token = INVALID_AUDIT_TOKEN_VALUE;
238 pid_t child = fork();
239 if (child > 0) {
240 audit_token_for_pid(child, &token);
241 // Send signal to the child with its audit token
242 ret = proc_signal_with_audittoken(&token, signal);
243 wait_collect_exit_reason(child, signal);
244 T_EXPECT_EQ_INT(ret, 0, "expect proc_signal_with_audittoken return: %d", ret);
245 // Send signal to the child with its audit token who has exited by now
246 ret = proc_signal_with_audittoken(&token, signal);
247 T_EXPECT_EQ_INT(ret, ESRCH, "expect no such process return: %d", ret);
248 } else {
249 pause();
250 // This exit should not hit, but we exit abnormally in case something went wrong
251 _exit(-1);
252 }
253 }
254
255 static void
__test_exit_reason_signal_with_audittoken_fail_bad_token(int signal)256 __test_exit_reason_signal_with_audittoken_fail_bad_token(int signal)
257 {
258 int ret = 0;
259 audit_token_t token = INVALID_AUDIT_TOKEN_VALUE;
260 pid_t child = fork();
261 if (child > 0) {
262 audit_token_for_pid(child, &token);
263 // Send signal to the child with its audit token, modified so pidversion is bad
264 token.val[7] += 1;
265 ret = proc_signal_with_audittoken(&token, signal);
266 wait_with_timeout_expected(child, 2);
267 T_EXPECT_EQ_INT(ret, ESRCH, "expect bad audit token return: %d", ret);
268 // Cleanup child
269 kill(child, signal);
270 } else {
271 pause();
272 }
273 }
274
275 static void
__test_exit_reason_signal_with_audittoken_fail_null_token(int signal)276 __test_exit_reason_signal_with_audittoken_fail_null_token(int signal)
277 {
278 int ret = 0;
279 pid_t child = fork();
280 if (child > 0) {
281 // Send signal to the child with null audit token
282 ret = proc_signal_with_audittoken(NULL, signal);
283 wait_with_timeout_expected(child, 2);
284 T_EXPECT_EQ_INT(ret, EINVAL, "expect null audit token return: %d", ret);
285 // Cleanup child
286 kill(child, signal);
287 } else {
288 pause();
289 }
290 }
291
292 static void
__test_exit_reason_signal_with_audittoken_fail_bad_signal(int signal)293 __test_exit_reason_signal_with_audittoken_fail_bad_signal(int signal)
294 {
295 int ret = 0;
296 audit_token_t token = INVALID_AUDIT_TOKEN_VALUE;
297 pid_t child = fork();
298 if (child > 0) {
299 audit_token_for_pid(child, &token);
300 ret = proc_signal_with_audittoken(&token, signal);
301 wait_with_timeout_expected(child, 2);
302 T_EXPECT_EQ_INT(ret, EINVAL, "expect invalid sig num return: %d", ret);
303 kill(child, signal);
304 } else {
305 pause();
306 }
307 }
308
309 T_DECL(proc_signal_delegate_success, "proc_signal_delegate should work", T_META_TAG_VM_PREFERRED)
310 {
311 dispatch_test(^{
312 __test_exit_reason_delegate_signal(SIGABRT);
313 __test_exit_reason_delegate_signal(SIGKILL);
314 __test_exit_reason_delegate_signal(SIGSYS);
315 __test_exit_reason_delegate_signal(SIGUSR1);
316 __test_exit_reason_delegate_terminate();
317 T_END;
318 });
319 }
320
321 T_DECL(proc_signal_with_audittoken_success, "proc_signal_with_audittoken should work", T_META_TAG_VM_PREFERRED)
322 {
323 dispatch_test(^{
324 __test_exit_reason_signal_with_audittoken(SIGABRT);
325 __test_exit_reason_signal_with_audittoken(SIGKILL);
326 __test_exit_reason_signal_with_audittoken(SIGSYS);
327 __test_exit_reason_signal_with_audittoken(SIGUSR1);
328 T_END;
329 });
330 }
331
332 T_DECL(proc_signal_with_audittoken_fail_bad_token, "proc_signal_with_audittoken should fail with invalid audit token", T_META_TAG_VM_PREFERRED)
333 {
334 dispatch_test(^{
335 __test_exit_reason_signal_with_audittoken_fail_bad_token(SIGKILL);
336 T_END;
337 });
338 }
339
340 T_DECL(proc_signal_with_audittoken_fail_null_token, "proc_signal_with_audittoken should fail with a null audit token", T_META_TAG_VM_PREFERRED)
341 {
342 dispatch_test(^{
343 __test_exit_reason_signal_with_audittoken_fail_null_token(SIGKILL);
344 T_END;
345 });
346 }
347
348 T_DECL(proc_signal_with_audittoken_fail_bad_signal, "proc_signal_with_audittoken should fail with invalid signals", T_META_TAG_VM_PREFERRED)
349 {
350 dispatch_test(^{
351 __test_exit_reason_signal_with_audittoken_fail_bad_signal(0);
352 __test_exit_reason_signal_with_audittoken_fail_bad_signal(NSIG + 1);
353 T_END;
354 });
355 }
356
357 T_DECL(test_exit_reason_external_signal, "tests exit reason for external signals", T_META_TAG_VM_PREFERRED)
358 {
359 dispatch_test(^{
360 __test_exit_reason_external_signal(SIGABRT);
361 __test_exit_reason_external_signal(SIGKILL);
362 __test_exit_reason_external_signal(SIGSYS);
363 __test_exit_reason_external_signal(SIGUSR1);
364 T_END;
365 });
366 }
367
368 struct pthread_kill_helper_args {
369 pthread_t *pthread;
370 int signal;
371 };
372
373 static void
pthread_kill_helper(void * msg)374 pthread_kill_helper(void *msg)
375 {
376 struct pthread_kill_helper_args *args = (struct pthread_kill_helper_args *)msg;
377 pthread_kill(*args->pthread, args->signal);
378 }
379
380 static void
__test_exit_reason_pthread_kill_self(int signal)381 __test_exit_reason_pthread_kill_self(int signal)
382 {
383 T_LOG("Testing pthread_kill for signal %d", signal);
384 pid_t child = fork();
385 if (child > 0) {
386 wait_collect_exit_reason(child, signal);
387 } else {
388 pthread_t t;
389 struct pthread_kill_helper_args args = {&t, signal};
390 pthread_create(&t, NULL, pthread_kill_helper, (void *)&args);
391 pthread_join(t, NULL);
392 }
393 }
394
395 T_DECL(test_exit_reason_pthread_kill_self, "tests exit reason for pthread_kill on caller thread", T_META_TAG_VM_PREFERRED)
396 {
397 dispatch_test(^{
398 __test_exit_reason_pthread_kill_self(SIGABRT);
399 __test_exit_reason_pthread_kill_self(SIGKILL);
400 __test_exit_reason_pthread_kill_self(SIGSYS);
401 __test_exit_reason_pthread_kill_self(SIGUSR1);
402 T_END;
403 });
404 }
405