1 /*
2 * Copyright (c) 1995-2016 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 * Copyright (c) 1982, 1986, 1989, 1991, 1993
30 * The Regents of the University of California. All rights reserved.
31 * (c) UNIX System Laboratories, Inc.
32 * All or some portions of this file are derived from material licensed
33 * to the University of California by American Telephone and Telegraph
34 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
35 * the permission of UNIX System Laboratories, Inc.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
66 */
67 /*
68 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
69 * support for mandatory and extensible security protections. This notice
70 * is included in support of clause 2.2 (b) of the Apple Public License,
71 * Version 2.0.
72 */
73
74 #define SIGPROP /* include signal properties table */
75 #include <sys/param.h>
76 #include <sys/resourcevar.h>
77 #include <sys/proc_internal.h>
78 #include <sys/kauth.h>
79 #include <sys/systm.h>
80 #include <sys/timeb.h>
81 #include <sys/times.h>
82 #include <sys/acct.h>
83 #include <sys/file_internal.h>
84 #include <sys/kernel.h>
85 #include <sys/wait.h>
86 #include <sys/signalvar.h>
87 #include <sys/syslog.h>
88 #include <sys/stat.h>
89 #include <sys/lock.h>
90 #include <sys/kdebug.h>
91 #include <sys/reason.h>
92
93 #include <sys/mount.h>
94 #include <sys/sysproto.h>
95
96 #include <security/audit/audit.h>
97
98 #include <kern/cpu_number.h>
99
100 #include <sys/vm.h>
101 #include <sys/user.h> /* for coredump */
102 #include <kern/ast.h> /* for APC support */
103 #include <kern/kalloc.h>
104 #include <kern/task.h> /* extern void *get_bsdtask_info(task_t); */
105 #include <kern/thread.h>
106 #include <kern/sched_prim.h>
107 #include <kern/thread_call.h>
108 #include <kern/policy_internal.h>
109 #include <kern/sync_sema.h>
110
111 #include <vm/vm_shared_region_xnu.h>
112
113 #include <os/log.h>
114
115 #include <mach/exception.h>
116 #include <mach/task.h>
117 #include <mach/thread_act.h>
118 #include <libkern/OSAtomic.h>
119
120 #include <sys/sdt.h>
121 #include <sys/codesign.h>
122 #include <sys/random.h>
123 #include <libkern/section_keywords.h>
124
125 #if CONFIG_MACF
126 #include <security/mac_framework.h>
127 #endif
128
129 /*
130 * Missing prototypes that Mach should export
131 *
132 * +++
133 */
134 extern int thread_enable_fpe(thread_t act, int onoff);
135 extern kern_return_t get_signalact(task_t, thread_t *, int);
136 extern unsigned int get_useraddr(void);
137 extern boolean_t task_did_exec(task_t task);
138 extern boolean_t task_is_exec_copy(task_t task);
139
140 /*
141 * ---
142 */
143
144 extern void doexception(int exc, mach_exception_code_t code,
145 mach_exception_subcode_t sub);
146
147 static void stop(proc_t, proc_t);
148 int cansignal_nomac(proc_t, kauth_cred_t, proc_t, int);
149 int cansignal(proc_t, kauth_cred_t, proc_t, int);
150 int killpg1(proc_t, int, int, int, int);
151 kern_return_t do_bsdexception(int, int, int);
152 void __posix_sem_syscall_return(kern_return_t);
153 char *proc_name_address(void *p);
154
155 static int filt_sigattach(struct knote *kn, struct kevent_qos_s *kev);
156 static void filt_sigdetach(struct knote *kn);
157 static int filt_signal(struct knote *kn, long hint);
158 static int filt_signaltouch(struct knote *kn, struct kevent_qos_s *kev);
159 static int filt_signalprocess(struct knote *kn, struct kevent_qos_s *kev);
160
161 SECURITY_READ_ONLY_EARLY(struct filterops) sig_filtops = {
162 .f_attach = filt_sigattach,
163 .f_detach = filt_sigdetach,
164 .f_event = filt_signal,
165 .f_touch = filt_signaltouch,
166 .f_process = filt_signalprocess,
167 };
168
169 /* structures and fns for killpg1 iterartion callback and filters */
170 struct killpg1_filtargs {
171 bool posix;
172 proc_t curproc;
173 };
174
175 struct killpg1_iterargs {
176 proc_t curproc;
177 kauth_cred_t uc;
178 int signum;
179 int nfound;
180 };
181
182 static int killpg1_allfilt(proc_t p, void * arg);
183 static int killpg1_callback(proc_t p, void * arg);
184
185 static int pgsignal_callback(proc_t p, void * arg);
186 static kern_return_t get_signalthread(proc_t, int, thread_t *);
187
188
189 /* flags for psignal_internal */
190 #define PSIG_LOCKED 0x1
191 #define PSIG_VFORK 0x2
192 #define PSIG_THREAD 0x4
193 #define PSIG_TRY_THREAD 0x8
194
195 static os_reason_t build_signal_reason(int signum, const char *procname);
196 static void psignal_internal(proc_t p, task_t task, thread_t thread, int flavor, int signum, os_reason_t signal_reason);
197
198 /*
199 * NOTE: Source and target may *NOT* overlap! (target is smaller)
200 */
201 static void
sigaltstack_kern_to_user32(struct kern_sigaltstack * in,struct user32_sigaltstack * out)202 sigaltstack_kern_to_user32(struct kern_sigaltstack *in, struct user32_sigaltstack *out)
203 {
204 out->ss_sp = CAST_DOWN_EXPLICIT(user32_addr_t, in->ss_sp);
205 out->ss_size = CAST_DOWN_EXPLICIT(user32_size_t, in->ss_size);
206 out->ss_flags = in->ss_flags;
207 }
208
209 static void
sigaltstack_kern_to_user64(struct kern_sigaltstack * in,struct user64_sigaltstack * out)210 sigaltstack_kern_to_user64(struct kern_sigaltstack *in, struct user64_sigaltstack *out)
211 {
212 out->ss_sp = in->ss_sp;
213 out->ss_size = in->ss_size;
214 out->ss_flags = in->ss_flags;
215 }
216
217 /*
218 * NOTE: Source and target may are permitted to overlap! (source is smaller);
219 * this works because we copy fields in order from the end of the struct to
220 * the beginning.
221 */
222 static void
sigaltstack_user32_to_kern(struct user32_sigaltstack * in,struct kern_sigaltstack * out)223 sigaltstack_user32_to_kern(struct user32_sigaltstack *in, struct kern_sigaltstack *out)
224 {
225 out->ss_flags = in->ss_flags;
226 out->ss_size = in->ss_size;
227 out->ss_sp = CAST_USER_ADDR_T(in->ss_sp);
228 }
229 static void
sigaltstack_user64_to_kern(struct user64_sigaltstack * in,struct kern_sigaltstack * out)230 sigaltstack_user64_to_kern(struct user64_sigaltstack *in, struct kern_sigaltstack *out)
231 {
232 out->ss_flags = in->ss_flags;
233 out->ss_size = (user_size_t)in->ss_size;
234 out->ss_sp = (user_addr_t)in->ss_sp;
235 }
236
237 static void
sigaction_kern_to_user32(struct kern_sigaction * in,struct user32_sigaction * out)238 sigaction_kern_to_user32(struct kern_sigaction *in, struct user32_sigaction *out)
239 {
240 /* This assumes 32 bit __sa_handler is of type sig_t */
241 out->__sigaction_u.__sa_handler = CAST_DOWN_EXPLICIT(user32_addr_t, in->__sigaction_u.__sa_handler);
242 out->sa_mask = in->sa_mask;
243 out->sa_flags = in->sa_flags;
244 }
245 static void
sigaction_kern_to_user64(struct kern_sigaction * in,struct user64_sigaction * out)246 sigaction_kern_to_user64(struct kern_sigaction *in, struct user64_sigaction *out)
247 {
248 /* This assumes 32 bit __sa_handler is of type sig_t */
249 out->__sigaction_u.__sa_handler = in->__sigaction_u.__sa_handler;
250 out->sa_mask = in->sa_mask;
251 out->sa_flags = in->sa_flags;
252 }
253
254 static void
__sigaction_user32_to_kern(struct __user32_sigaction * in,struct __kern_sigaction * out)255 __sigaction_user32_to_kern(struct __user32_sigaction *in, struct __kern_sigaction *out)
256 {
257 out->__sigaction_u.__sa_handler = CAST_USER_ADDR_T(in->__sigaction_u.__sa_handler);
258 out->sa_tramp = CAST_USER_ADDR_T(in->sa_tramp);
259 out->sa_mask = in->sa_mask;
260 out->sa_flags = in->sa_flags;
261
262 kern_return_t kr;
263 kr = machine_thread_function_pointers_convert_from_user(current_thread(),
264 &out->sa_tramp, 1);
265 assert(kr == KERN_SUCCESS);
266 }
267
268 static void
__sigaction_user64_to_kern(struct __user64_sigaction * in,struct __kern_sigaction * out)269 __sigaction_user64_to_kern(struct __user64_sigaction *in, struct __kern_sigaction *out)
270 {
271 out->__sigaction_u.__sa_handler = (user_addr_t)in->__sigaction_u.__sa_handler;
272 out->sa_tramp = (user_addr_t)in->sa_tramp;
273 out->sa_mask = in->sa_mask;
274 out->sa_flags = in->sa_flags;
275
276 kern_return_t kr;
277 kr = machine_thread_function_pointers_convert_from_user(current_thread(),
278 &out->sa_tramp, 1);
279 assert(kr == KERN_SUCCESS);
280 }
281
282 #if SIGNAL_DEBUG
283 void ram_printf(int);
284 int ram_debug = 0;
285 unsigned int rdebug_proc = 0;
286 void
ram_printf(int x)287 ram_printf(int x)
288 {
289 printf("x is %d", x);
290 }
291 #endif /* SIGNAL_DEBUG */
292
293
294 void
signal_setast(thread_t sig_actthread)295 signal_setast(thread_t sig_actthread)
296 {
297 act_set_astbsd(sig_actthread);
298 }
299
300 int
cansignal_nomac(proc_t src,kauth_cred_t uc_src,proc_t dst,int signum)301 cansignal_nomac(proc_t src, kauth_cred_t uc_src, proc_t dst, int signum)
302 {
303 /* you can signal yourself */
304 if (src == dst) {
305 return 1;
306 }
307
308 /* you can't send the init proc SIGKILL, even if root */
309 if (signum == SIGKILL && dst == initproc) {
310 return 0;
311 }
312
313 /* otherwise, root can always signal */
314 if (kauth_cred_issuser(uc_src)) {
315 return 1;
316 }
317
318 /* processes in the same session can send SIGCONT to each other */
319 if (signum == SIGCONT && proc_sessionid(src) == proc_sessionid(dst)) {
320 return 1;
321 }
322
323 #if XNU_TARGET_OS_IOS
324 // Allow debugging of third party drivers on iOS
325 if (proc_is_third_party_debuggable_driver(dst)) {
326 return 1;
327 }
328 #endif /* XNU_TARGET_OS_IOS */
329
330 /* the source process must be authorized to signal the target */
331 {
332 int allowed = 0;
333 kauth_cred_t uc_dst = NOCRED, uc_ref = NOCRED;
334
335 uc_dst = uc_ref = kauth_cred_proc_ref(dst);
336
337 /*
338 * If the real or effective UID of the sender matches the real or saved
339 * UID of the target, allow the signal to be sent.
340 */
341 if (kauth_cred_getruid(uc_src) == kauth_cred_getruid(uc_dst) ||
342 kauth_cred_getruid(uc_src) == kauth_cred_getsvuid(uc_dst) ||
343 kauth_cred_getuid(uc_src) == kauth_cred_getruid(uc_dst) ||
344 kauth_cred_getuid(uc_src) == kauth_cred_getsvuid(uc_dst)) {
345 allowed = 1;
346 }
347
348 if (uc_ref != NOCRED) {
349 kauth_cred_unref(&uc_ref);
350 uc_ref = NOCRED;
351 }
352
353 return allowed;
354 }
355 }
356
357 /*
358 * Can process `src`, with ucred `uc_src`, send the signal `signum` to process
359 * `dst`? The ucred is referenced by the caller so internal fileds can be used
360 * safely.
361 */
362 int
cansignal(proc_t src,kauth_cred_t uc_src,proc_t dst,int signum)363 cansignal(proc_t src, kauth_cred_t uc_src, proc_t dst, int signum)
364 {
365 #if CONFIG_MACF
366 struct proc_ident dst_ident = proc_ident(dst);
367 if (mac_proc_check_signal(src, NULL, &dst_ident, signum)) {
368 return 0;
369 }
370 #endif
371
372 return cansignal_nomac(src, uc_src, dst, signum);
373 }
374
375 /*
376 * <rdar://problem/21952708> Some signals can be restricted from being handled,
377 * forcing the default action for that signal. This behavior applies only to
378 * non-root (EUID != 0) processes, and is configured with the "sigrestrict=x"
379 * bootarg:
380 *
381 * 0 (default): Disallow use of restricted signals. Trying to register a handler
382 * returns ENOTSUP, which userspace may use to take special action (e.g. abort).
383 * 1: As above, but return EINVAL. Restricted signals behave similarly to SIGKILL.
384 * 2: Usual POSIX semantics.
385 */
386 static TUNABLE(unsigned, sigrestrict_arg, "sigrestrict", 0);
387
388 #if XNU_PLATFORM_WatchOS
389 static int
sigrestrictmask(void)390 sigrestrictmask(void)
391 {
392 if (kauth_getuid() != 0 && sigrestrict_arg != 2) {
393 return SIGRESTRICTMASK;
394 }
395 return 0;
396 }
397
398 static int
signal_is_restricted(proc_t p,int signum)399 signal_is_restricted(proc_t p, int signum)
400 {
401 if (sigmask(signum) & sigrestrictmask()) {
402 if (sigrestrict_arg == 0 &&
403 task_get_apptype(proc_task(p)) == TASK_APPTYPE_APP_DEFAULT) {
404 return ENOTSUP;
405 } else {
406 return EINVAL;
407 }
408 }
409 return 0;
410 }
411
412 #else
413
414 static inline int
signal_is_restricted(proc_t p,int signum)415 signal_is_restricted(proc_t p, int signum)
416 {
417 (void)p;
418 (void)signum;
419 return 0;
420 }
421 #endif /* !XNU_PLATFORM_WatchOS */
422
423 /*
424 * Returns: 0 Success
425 * EINVAL
426 * copyout:EFAULT
427 * copyin:EFAULT
428 *
429 * Notes: Uses current thread as a parameter to inform PPC to enable
430 * FPU exceptions via setsigvec(); this operation is not proxy
431 * safe!
432 */
433 /* ARGSUSED */
434 int
sigaction(proc_t p,struct sigaction_args * uap,__unused int32_t * retval)435 sigaction(proc_t p, struct sigaction_args *uap, __unused int32_t *retval)
436 {
437 struct kern_sigaction vec;
438 struct __kern_sigaction __vec;
439
440 struct kern_sigaction *sa = &vec;
441 struct sigacts *ps = &p->p_sigacts;
442
443 int signum;
444 int bit, error = 0;
445 uint32_t sigreturn_validation = PS_SIGRETURN_VALIDATION_DEFAULT;
446
447 signum = uap->signum;
448 if (signum <= 0 || signum >= NSIG ||
449 signum == SIGKILL || signum == SIGSTOP) {
450 return EINVAL;
451 }
452
453 if (uap->nsa) {
454 if (IS_64BIT_PROCESS(p)) {
455 struct __user64_sigaction __vec64;
456 error = copyin(uap->nsa, &__vec64, sizeof(__vec64));
457 __sigaction_user64_to_kern(&__vec64, &__vec);
458 } else {
459 struct __user32_sigaction __vec32;
460 error = copyin(uap->nsa, &__vec32, sizeof(__vec32));
461 __sigaction_user32_to_kern(&__vec32, &__vec);
462 }
463 if (error) {
464 return error;
465 }
466
467 sigreturn_validation = (__vec.sa_flags & SA_VALIDATE_SIGRETURN_FROM_SIGTRAMP) ?
468 PS_SIGRETURN_VALIDATION_ENABLED : PS_SIGRETURN_VALIDATION_DISABLED;
469 __vec.sa_flags &= SA_USERSPACE_MASK; /* Only pass on valid sa_flags */
470
471 if ((__vec.sa_flags & SA_SIGINFO) || __vec.sa_handler != SIG_DFL) {
472 if ((error = signal_is_restricted(p, signum))) {
473 if (error == ENOTSUP) {
474 printf("%s(%d): denied attempt to register action for signal %d\n",
475 proc_name_address(p), proc_pid(p), signum);
476 }
477 return error;
478 }
479 }
480 }
481
482 if (uap->osa) {
483 sa->sa_handler = SIGACTION(p, signum);
484 sa->sa_mask = ps->ps_catchmask[signum];
485 bit = sigmask(signum);
486 sa->sa_flags = 0;
487 if ((ps->ps_sigonstack & bit) != 0) {
488 sa->sa_flags |= SA_ONSTACK;
489 }
490 if ((ps->ps_sigintr & bit) == 0) {
491 sa->sa_flags |= SA_RESTART;
492 }
493 if (ps->ps_siginfo & bit) {
494 sa->sa_flags |= SA_SIGINFO;
495 }
496 if (ps->ps_signodefer & bit) {
497 sa->sa_flags |= SA_NODEFER;
498 }
499 if ((signum == SIGCHLD) && (p->p_flag & P_NOCLDSTOP)) {
500 sa->sa_flags |= SA_NOCLDSTOP;
501 }
502 if ((signum == SIGCHLD) && (p->p_flag & P_NOCLDWAIT)) {
503 sa->sa_flags |= SA_NOCLDWAIT;
504 }
505
506 if (IS_64BIT_PROCESS(p)) {
507 struct user64_sigaction vec64 = {};
508 sigaction_kern_to_user64(sa, &vec64);
509 error = copyout(&vec64, uap->osa, sizeof(vec64));
510 } else {
511 struct user32_sigaction vec32 = {};
512 sigaction_kern_to_user32(sa, &vec32);
513 error = copyout(&vec32, uap->osa, sizeof(vec32));
514 }
515 if (error) {
516 return error;
517 }
518 }
519
520 if (uap->nsa) {
521 uint32_t old_sigreturn_validation = atomic_load_explicit(
522 &ps->ps_sigreturn_validation, memory_order_relaxed);
523 if (old_sigreturn_validation == PS_SIGRETURN_VALIDATION_DEFAULT) {
524 atomic_compare_exchange_strong_explicit(&ps->ps_sigreturn_validation,
525 &old_sigreturn_validation, sigreturn_validation,
526 memory_order_relaxed, memory_order_relaxed);
527 }
528 error = setsigvec(p, current_thread(), signum, &__vec, FALSE);
529 }
530
531 return error;
532 }
533
534 /* Routines to manipulate bits on all threads */
535 int
clear_procsiglist(proc_t p,int bit,boolean_t in_signalstart)536 clear_procsiglist(proc_t p, int bit, boolean_t in_signalstart)
537 {
538 struct uthread * uth;
539
540 proc_lock(p);
541 if (!in_signalstart) {
542 proc_signalstart(p, 1);
543 }
544
545
546 TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) {
547 uth->uu_siglist &= ~bit;
548 }
549 p->p_siglist &= ~bit;
550 if (!in_signalstart) {
551 proc_signalend(p, 1);
552 }
553 proc_unlock(p);
554
555 return 0;
556 }
557
558
559 static int
unblock_procsigmask(proc_t p,int bit)560 unblock_procsigmask(proc_t p, int bit)
561 {
562 struct uthread * uth;
563
564 proc_lock(p);
565 proc_signalstart(p, 1);
566
567
568 TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) {
569 uth->uu_sigmask &= ~bit;
570 }
571 p->p_sigmask &= ~bit;
572
573 proc_signalend(p, 1);
574 proc_unlock(p);
575 return 0;
576 }
577
578 static int
block_procsigmask(proc_t p,int bit)579 block_procsigmask(proc_t p, int bit)
580 {
581 struct uthread * uth;
582
583 proc_lock(p);
584 proc_signalstart(p, 1);
585
586
587 TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) {
588 uth->uu_sigmask |= bit;
589 }
590 p->p_sigmask |= bit;
591
592 proc_signalend(p, 1);
593 proc_unlock(p);
594 return 0;
595 }
596
597 int
set_procsigmask(proc_t p,int bit)598 set_procsigmask(proc_t p, int bit)
599 {
600 struct uthread * uth;
601
602 proc_lock(p);
603 proc_signalstart(p, 1);
604
605
606 TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) {
607 uth->uu_sigmask = bit;
608 }
609 p->p_sigmask = bit;
610 proc_signalend(p, 1);
611 proc_unlock(p);
612
613 return 0;
614 }
615
616 /* XXX should be static? */
617 /*
618 * Notes: The thread parameter is used in the PPC case to select the
619 * thread on which the floating point exception will be enabled
620 * or disabled. We can't simply take current_thread(), since
621 * this is called from posix_spawn() on the not currently running
622 * process/thread pair.
623 *
624 * We mark thread as unused to alow compilation without warning
625 * on non-PPC platforms.
626 */
627 int
setsigvec(proc_t p,__unused thread_t thread,int signum,struct __kern_sigaction * sa,boolean_t in_sigstart)628 setsigvec(proc_t p, __unused thread_t thread, int signum, struct __kern_sigaction *sa, boolean_t in_sigstart)
629 {
630 struct sigacts *ps = &p->p_sigacts;
631 int bit;
632
633 assert(signum < NSIG);
634
635 if ((signum == SIGKILL || signum == SIGSTOP) &&
636 sa->sa_handler != SIG_DFL) {
637 return EINVAL;
638 }
639 bit = sigmask(signum);
640 /*
641 * Change setting atomically.
642 */
643 proc_set_sigact_trampact(p, signum, sa->sa_handler, sa->sa_tramp);
644 ps->ps_catchmask[signum] = sa->sa_mask & ~sigcantmask;
645 if (sa->sa_flags & SA_SIGINFO) {
646 ps->ps_siginfo |= bit;
647 } else {
648 ps->ps_siginfo &= ~bit;
649 }
650 if ((sa->sa_flags & SA_RESTART) == 0) {
651 ps->ps_sigintr |= bit;
652 } else {
653 ps->ps_sigintr &= ~bit;
654 }
655 if (sa->sa_flags & SA_ONSTACK) {
656 ps->ps_sigonstack |= bit;
657 } else {
658 ps->ps_sigonstack &= ~bit;
659 }
660 if (sa->sa_flags & SA_RESETHAND) {
661 ps->ps_sigreset |= bit;
662 } else {
663 ps->ps_sigreset &= ~bit;
664 }
665 if (sa->sa_flags & SA_NODEFER) {
666 ps->ps_signodefer |= bit;
667 } else {
668 ps->ps_signodefer &= ~bit;
669 }
670 if (signum == SIGCHLD) {
671 if (sa->sa_flags & SA_NOCLDSTOP) {
672 OSBitOrAtomic(P_NOCLDSTOP, &p->p_flag);
673 } else {
674 OSBitAndAtomic(~((uint32_t)P_NOCLDSTOP), &p->p_flag);
675 }
676 if ((sa->sa_flags & SA_NOCLDWAIT) || (sa->sa_handler == SIG_IGN)) {
677 OSBitOrAtomic(P_NOCLDWAIT, &p->p_flag);
678 } else {
679 OSBitAndAtomic(~((uint32_t)P_NOCLDWAIT), &p->p_flag);
680 }
681 }
682
683 /*
684 * Set bit in p_sigignore for signals that are set to SIG_IGN,
685 * and for signals set to SIG_DFL where the default is to ignore.
686 * However, don't put SIGCONT in p_sigignore,
687 * as we have to restart the process.
688 */
689 if (sa->sa_handler == SIG_IGN ||
690 (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
691 clear_procsiglist(p, bit, in_sigstart);
692 if (signum != SIGCONT) {
693 p->p_sigignore |= bit; /* easier in psignal */
694 }
695 p->p_sigcatch &= ~bit;
696 } else {
697 p->p_sigignore &= ~bit;
698 if (sa->sa_handler == SIG_DFL) {
699 p->p_sigcatch &= ~bit;
700 } else {
701 p->p_sigcatch |= bit;
702 }
703 }
704 return 0;
705 }
706
707 /*
708 * Initialize signal state for process 0;
709 * set to ignore signals that are ignored by default.
710 */
711 void
siginit(proc_t p)712 siginit(proc_t p)
713 {
714 int i;
715
716 for (i = 1; i < NSIG; i++) {
717 if (sigprop[i] & SA_IGNORE && i != SIGCONT) {
718 p->p_sigignore |= sigmask(i);
719 }
720 }
721 }
722
723 /*
724 * Reset signals for an exec of the specified process.
725 */
726 void
execsigs(proc_t p,thread_t thread)727 execsigs(proc_t p, thread_t thread)
728 {
729 struct sigacts *ps = &p->p_sigacts;
730 int nc, mask;
731 struct uthread *ut;
732
733 ut = (struct uthread *)get_bsdthread_info(thread);
734
735 /*
736 * transfer saved signal states from the process
737 * back to the current thread.
738 *
739 * NOTE: We do this without the process locked,
740 * because we are guaranteed to be single-threaded
741 * by this point in exec and the p_siglist is
742 * only accessed by threads inside the process.
743 */
744 ut->uu_siglist |= p->p_siglist;
745 p->p_siglist = 0;
746
747 /*
748 * Reset caught signals. Held signals remain held
749 * through p_sigmask (unless they were caught,
750 * and are now ignored by default).
751 */
752 proc_reset_sigact(p, p->p_sigcatch);
753 while (p->p_sigcatch) {
754 nc = ffs((unsigned int)p->p_sigcatch);
755 mask = sigmask(nc);
756 p->p_sigcatch &= ~mask;
757 if (sigprop[nc] & SA_IGNORE) {
758 if (nc != SIGCONT) {
759 p->p_sigignore |= mask;
760 }
761 ut->uu_siglist &= ~mask;
762 }
763 }
764
765 atomic_store_explicit(&ps->ps_sigreturn_validation,
766 PS_SIGRETURN_VALIDATION_DEFAULT, memory_order_relaxed);
767
768 /*
769 * Reset stack state to the user stack.
770 * Clear set of signals caught on the signal stack.
771 */
772 /* thread */
773 ut->uu_sigstk.ss_flags = SA_DISABLE;
774 ut->uu_sigstk.ss_size = 0;
775 ut->uu_sigstk.ss_sp = USER_ADDR_NULL;
776 ut->uu_flag &= ~UT_ALTSTACK;
777 /* process */
778 ps->ps_sigonstack = 0;
779 }
780
781 /*
782 * Manipulate signal mask.
783 * Note that we receive new mask, not pointer,
784 * and return old mask as return value;
785 * the library stub does the rest.
786 */
787 int
sigprocmask(proc_t p,struct sigprocmask_args * uap,__unused int32_t * retval)788 sigprocmask(proc_t p, struct sigprocmask_args *uap, __unused int32_t *retval)
789 {
790 int error = 0;
791 sigset_t oldmask, nmask;
792 user_addr_t omask = uap->omask;
793 struct uthread *ut;
794
795 ut = current_uthread();
796 oldmask = ut->uu_sigmask;
797
798 if (uap->mask == USER_ADDR_NULL) {
799 /* just want old mask */
800 goto out;
801 }
802 error = copyin(uap->mask, &nmask, sizeof(sigset_t));
803 if (error) {
804 goto out;
805 }
806
807 switch (uap->how) {
808 case SIG_BLOCK:
809 block_procsigmask(p, (nmask & ~sigcantmask));
810 signal_setast(current_thread());
811 break;
812
813 case SIG_UNBLOCK:
814 unblock_procsigmask(p, (nmask & ~sigcantmask));
815 signal_setast(current_thread());
816 break;
817
818 case SIG_SETMASK:
819 set_procsigmask(p, (nmask & ~sigcantmask));
820 signal_setast(current_thread());
821 break;
822
823 default:
824 error = EINVAL;
825 break;
826 }
827 out:
828 if (!error && omask != USER_ADDR_NULL) {
829 copyout(&oldmask, omask, sizeof(sigset_t));
830 }
831 return error;
832 }
833
834 int
sigpending(__unused proc_t p,struct sigpending_args * uap,__unused int32_t * retval)835 sigpending(__unused proc_t p, struct sigpending_args *uap, __unused int32_t *retval)
836 {
837 struct uthread *ut;
838 sigset_t pendlist;
839
840 ut = current_uthread();
841 pendlist = ut->uu_siglist;
842
843 if (uap->osv) {
844 copyout(&pendlist, uap->osv, sizeof(sigset_t));
845 }
846 return 0;
847 }
848
849 /*
850 * Suspend process until signal, providing mask to be set
851 * in the meantime. Note nonstandard calling convention:
852 * libc stub passes mask, not pointer, to save a copyin.
853 */
854
855 static int
sigcontinue(__unused int error)856 sigcontinue(__unused int error)
857 {
858 // struct uthread *ut = current_uthread();
859 unix_syscall_return(EINTR);
860 }
861
862 int
sigsuspend(proc_t p,struct sigsuspend_args * uap,int32_t * retval)863 sigsuspend(proc_t p, struct sigsuspend_args *uap, int32_t *retval)
864 {
865 __pthread_testcancel(1);
866 return sigsuspend_nocancel(p, (struct sigsuspend_nocancel_args *)uap, retval);
867 }
868
869 int
sigsuspend_nocancel(proc_t p,struct sigsuspend_nocancel_args * uap,__unused int32_t * retval)870 sigsuspend_nocancel(proc_t p, struct sigsuspend_nocancel_args *uap, __unused int32_t *retval)
871 {
872 struct uthread *ut;
873
874 ut = current_uthread();
875
876 /*
877 * When returning from sigpause, we want
878 * the old mask to be restored after the
879 * signal handler has finished. Thus, we
880 * save it here and mark the sigacts structure
881 * to indicate this.
882 */
883 ut->uu_oldmask = ut->uu_sigmask;
884 ut->uu_flag |= UT_SAS_OLDMASK;
885 ut->uu_sigmask = (uap->mask & ~sigcantmask);
886 (void) tsleep0((caddr_t) p, PPAUSE | PCATCH, "pause", 0, sigcontinue);
887 /* always return EINTR rather than ERESTART... */
888 return EINTR;
889 }
890
891
892 int
__disable_threadsignal(__unused proc_t p,__unused struct __disable_threadsignal_args * uap,__unused int32_t * retval)893 __disable_threadsignal(__unused proc_t p,
894 __unused struct __disable_threadsignal_args *uap,
895 __unused int32_t *retval)
896 {
897 struct uthread *uth;
898
899 uth = current_uthread();
900
901 /* No longer valid to have any signal delivered */
902 uth->uu_flag |= (UT_NO_SIGMASK | UT_CANCELDISABLE);
903
904 return 0;
905 }
906
907 void
__pthread_testcancel(int presyscall)908 __pthread_testcancel(int presyscall)
909 {
910 thread_t self = current_thread();
911 struct uthread * uthread;
912
913 uthread = (struct uthread *)get_bsdthread_info(self);
914
915
916 uthread->uu_flag &= ~UT_NOTCANCELPT;
917
918 if ((uthread->uu_flag & (UT_CANCELDISABLE | UT_CANCEL | UT_CANCELED)) == UT_CANCEL) {
919 if (presyscall != 0) {
920 unix_syscall_return(EINTR);
921 /* NOTREACHED */
922 } else {
923 thread_abort_safely(self);
924 }
925 }
926 }
927
928
929
930 int
__pthread_markcancel(__unused proc_t p,struct __pthread_markcancel_args * uap,__unused int32_t * retval)931 __pthread_markcancel(__unused proc_t p,
932 struct __pthread_markcancel_args *uap, __unused int32_t *retval)
933 {
934 thread_act_t target_act;
935 int error = 0;
936 struct uthread *uth;
937
938 target_act = (thread_act_t)port_name_to_thread(uap->thread_port,
939 PORT_INTRANS_THREAD_IN_CURRENT_TASK);
940
941 if (target_act == THR_ACT_NULL) {
942 return ESRCH;
943 }
944
945 uth = (struct uthread *)get_bsdthread_info(target_act);
946
947 if ((uth->uu_flag & (UT_CANCEL | UT_CANCELED)) == 0) {
948 uth->uu_flag |= (UT_CANCEL | UT_NO_SIGMASK);
949 if (((uth->uu_flag & UT_NOTCANCELPT) == 0)
950 && ((uth->uu_flag & UT_CANCELDISABLE) == 0)) {
951 thread_abort_safely(target_act);
952 }
953 }
954
955 thread_deallocate(target_act);
956 return error;
957 }
958
959 /* if action =0 ; return the cancellation state ,
960 * if marked for cancellation, make the thread canceled
961 * if action = 1 ; Enable the cancel handling
962 * if action = 2; Disable the cancel handling
963 */
964 int
__pthread_canceled(__unused proc_t p,struct __pthread_canceled_args * uap,__unused int32_t * retval)965 __pthread_canceled(__unused proc_t p,
966 struct __pthread_canceled_args *uap, __unused int32_t *retval)
967 {
968 thread_act_t thread;
969 struct uthread *uth;
970 int action = uap->action;
971
972 thread = current_thread();
973 uth = (struct uthread *)get_bsdthread_info(thread);
974
975 switch (action) {
976 case 1:
977 uth->uu_flag &= ~UT_CANCELDISABLE;
978 return 0;
979 case 2:
980 uth->uu_flag |= UT_CANCELDISABLE;
981 return 0;
982 case 0:
983 default:
984 if ((uth->uu_flag & (UT_CANCELDISABLE | UT_CANCEL | UT_CANCELED)) == UT_CANCEL) {
985 uth->uu_flag &= ~UT_CANCEL;
986 uth->uu_flag |= (UT_CANCELED | UT_NO_SIGMASK);
987 return 0;
988 }
989 return EINVAL;
990 }
991 return EINVAL;
992 }
993
994 __attribute__((noreturn))
995 void
__posix_sem_syscall_return(kern_return_t kern_result)996 __posix_sem_syscall_return(kern_return_t kern_result)
997 {
998 int error = 0;
999
1000 if (kern_result == KERN_SUCCESS) {
1001 error = 0;
1002 } else if (kern_result == KERN_ABORTED) {
1003 error = EINTR;
1004 } else if (kern_result == KERN_OPERATION_TIMED_OUT) {
1005 error = ETIMEDOUT;
1006 } else {
1007 error = EINVAL;
1008 }
1009 unix_syscall_return(error);
1010 /* does not return */
1011 }
1012
1013 /*
1014 * Returns: 0 Success
1015 * EINTR
1016 * ETIMEDOUT
1017 * EINVAL
1018 * EFAULT if timespec is NULL
1019 */
1020 int
__semwait_signal(proc_t p,struct __semwait_signal_args * uap,int32_t * retval)1021 __semwait_signal(proc_t p, struct __semwait_signal_args *uap,
1022 int32_t *retval)
1023 {
1024 __pthread_testcancel(0);
1025 return __semwait_signal_nocancel(p, (struct __semwait_signal_nocancel_args *)uap, retval);
1026 }
1027
1028 int
__semwait_signal_nocancel(__unused proc_t p,struct __semwait_signal_nocancel_args * uap,__unused int32_t * retval)1029 __semwait_signal_nocancel(__unused proc_t p, struct __semwait_signal_nocancel_args *uap,
1030 __unused int32_t *retval)
1031 {
1032 kern_return_t kern_result;
1033 mach_timespec_t then;
1034 struct timespec now;
1035 struct user_timespec ts;
1036 boolean_t truncated_timeout = FALSE;
1037
1038 if (uap->timeout) {
1039 ts.tv_sec = (user_time_t)uap->tv_sec;
1040 ts.tv_nsec = uap->tv_nsec;
1041
1042 if ((ts.tv_sec & 0xFFFFFFFF00000000ULL) != 0) {
1043 ts.tv_sec = 0xFFFFFFFF;
1044 ts.tv_nsec = 0;
1045 truncated_timeout = TRUE;
1046 }
1047
1048 if (uap->relative) {
1049 then.tv_sec = (unsigned int)ts.tv_sec;
1050 then.tv_nsec = (clock_res_t)ts.tv_nsec;
1051 } else {
1052 nanotime(&now);
1053
1054 /* if time has elapsed, set time to null timepsec to bailout rightaway */
1055 if (now.tv_sec == ts.tv_sec ?
1056 now.tv_nsec > ts.tv_nsec :
1057 now.tv_sec > ts.tv_sec) {
1058 then.tv_sec = 0;
1059 then.tv_nsec = 0;
1060 } else {
1061 then.tv_sec = (unsigned int)(ts.tv_sec - now.tv_sec);
1062 then.tv_nsec = (clock_res_t)(ts.tv_nsec - now.tv_nsec);
1063 if (then.tv_nsec < 0) {
1064 then.tv_nsec += NSEC_PER_SEC;
1065 then.tv_sec--;
1066 }
1067 }
1068 }
1069
1070 if (uap->mutex_sem == 0) {
1071 kern_result = semaphore_timedwait_trap_internal((mach_port_name_t)uap->cond_sem, then.tv_sec, then.tv_nsec, __posix_sem_syscall_return);
1072 } else {
1073 kern_result = semaphore_timedwait_signal_trap_internal(uap->cond_sem, uap->mutex_sem, then.tv_sec, then.tv_nsec, __posix_sem_syscall_return);
1074 }
1075 } else {
1076 if (uap->mutex_sem == 0) {
1077 kern_result = semaphore_wait_trap_internal(uap->cond_sem, __posix_sem_syscall_return);
1078 } else {
1079 kern_result = semaphore_wait_signal_trap_internal(uap->cond_sem, uap->mutex_sem, __posix_sem_syscall_return);
1080 }
1081 }
1082
1083 if (kern_result == KERN_SUCCESS && !truncated_timeout) {
1084 return 0;
1085 } else if (kern_result == KERN_SUCCESS && truncated_timeout) {
1086 return EINTR; /* simulate an exceptional condition because Mach doesn't support a longer timeout */
1087 } else if (kern_result == KERN_ABORTED) {
1088 return EINTR;
1089 } else if (kern_result == KERN_OPERATION_TIMED_OUT) {
1090 return ETIMEDOUT;
1091 } else {
1092 return EINVAL;
1093 }
1094 }
1095
1096
1097 int
__pthread_kill(__unused proc_t p,struct __pthread_kill_args * uap,__unused int32_t * retval)1098 __pthread_kill(__unused proc_t p, struct __pthread_kill_args *uap,
1099 __unused int32_t *retval)
1100 {
1101 thread_t target_act;
1102 int error = 0;
1103 int signum = uap->sig;
1104 struct uthread *uth;
1105
1106 target_act = (thread_t)port_name_to_thread(uap->thread_port,
1107 PORT_INTRANS_OPTIONS_NONE);
1108
1109 if (target_act == THREAD_NULL) {
1110 return ESRCH;
1111 }
1112 if ((u_int)signum >= NSIG) {
1113 error = EINVAL;
1114 goto out;
1115 }
1116
1117 uth = (struct uthread *)get_bsdthread_info(target_act);
1118
1119 if (uth->uu_flag & UT_NO_SIGMASK) {
1120 error = ESRCH;
1121 goto out;
1122 }
1123
1124 /*
1125 * workq threads must have kills enabled through either
1126 * BSDTHREAD_CTL_WORKQ_ALLOW_KILL or BSDTHREAD_CTL_WORKQ_ALLOW_SIGMASK
1127 */
1128 if ((thread_get_tag(target_act) & THREAD_TAG_WORKQUEUE) &&
1129 !(uth->uu_workq_pthread_kill_allowed || p->p_workq_allow_sigmask)) {
1130 error = ENOTSUP;
1131 goto out;
1132 }
1133
1134 if (signum) {
1135 psignal_uthread(target_act, signum);
1136 }
1137 out:
1138 thread_deallocate(target_act);
1139 return error;
1140 }
1141
1142
1143 int
__pthread_sigmask(__unused proc_t p,struct __pthread_sigmask_args * uap,__unused int32_t * retval)1144 __pthread_sigmask(__unused proc_t p, struct __pthread_sigmask_args *uap,
1145 __unused int32_t *retval)
1146 {
1147 user_addr_t set = uap->set;
1148 user_addr_t oset = uap->oset;
1149 sigset_t nset;
1150 int error = 0;
1151 struct uthread *ut;
1152 sigset_t oldset;
1153
1154 ut = current_uthread();
1155 oldset = ut->uu_sigmask;
1156
1157 if (set == USER_ADDR_NULL) {
1158 /* need only old mask */
1159 goto out;
1160 }
1161
1162 error = copyin(set, &nset, sizeof(sigset_t));
1163 if (error) {
1164 goto out;
1165 }
1166
1167 switch (uap->how) {
1168 case SIG_BLOCK:
1169 ut->uu_sigmask |= (nset & ~sigcantmask);
1170 break;
1171
1172 case SIG_UNBLOCK:
1173 ut->uu_sigmask &= ~(nset);
1174 signal_setast(current_thread());
1175 break;
1176
1177 case SIG_SETMASK:
1178 ut->uu_sigmask = (nset & ~sigcantmask);
1179 signal_setast(current_thread());
1180 break;
1181
1182 default:
1183 error = EINVAL;
1184 }
1185 out:
1186 if (!error && oset != USER_ADDR_NULL) {
1187 copyout(&oldset, oset, sizeof(sigset_t));
1188 }
1189
1190 return error;
1191 }
1192
1193 /*
1194 * Returns: 0 Success
1195 * EINVAL
1196 * copyin:EFAULT
1197 * copyout:EFAULT
1198 */
1199 int
__sigwait(proc_t p,struct __sigwait_args * uap,int32_t * retval)1200 __sigwait(proc_t p, struct __sigwait_args *uap, int32_t *retval)
1201 {
1202 __pthread_testcancel(1);
1203 return __sigwait_nocancel(p, (struct __sigwait_nocancel_args *)uap, retval);
1204 }
1205
1206 int
__sigwait_nocancel(proc_t p,struct __sigwait_nocancel_args * uap,__unused int32_t * retval)1207 __sigwait_nocancel(proc_t p, struct __sigwait_nocancel_args *uap, __unused int32_t *retval)
1208 {
1209 struct uthread *ut;
1210 struct uthread *uth;
1211 int error = 0;
1212 sigset_t mask;
1213 sigset_t siglist;
1214 sigset_t sigw = 0;
1215 int signum;
1216
1217 ut = current_uthread();
1218
1219 if (uap->set == USER_ADDR_NULL) {
1220 return EINVAL;
1221 }
1222
1223 error = copyin(uap->set, &mask, sizeof(sigset_t));
1224 if (error) {
1225 return error;
1226 }
1227
1228 siglist = (mask & ~sigcantmask);
1229
1230 if (siglist == 0) {
1231 return EINVAL;
1232 }
1233
1234 proc_lock(p);
1235
1236 proc_signalstart(p, 1);
1237 TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) {
1238 if ((sigw = uth->uu_siglist & siglist)) {
1239 break;
1240 }
1241 }
1242 proc_signalend(p, 1);
1243
1244 if (sigw) {
1245 /* The signal was pending on a thread */
1246 goto sigwait1;
1247 }
1248 /*
1249 * When returning from sigwait, we want
1250 * the old mask to be restored after the
1251 * signal handler has finished. Thus, we
1252 * save it here and mark the sigacts structure
1253 * to indicate this.
1254 */
1255 uth = ut; /* wait for it to be delivered to us */
1256 ut->uu_oldmask = ut->uu_sigmask;
1257 ut->uu_flag |= UT_SAS_OLDMASK;
1258 if (siglist == (sigset_t)0) {
1259 proc_unlock(p);
1260 return EINVAL;
1261 }
1262 /* SIGKILL and SIGSTOP are not maskable as well */
1263 ut->uu_sigmask = ~(siglist | sigcantmask);
1264 ut->uu_sigwait = siglist;
1265
1266 /* No Continuations for now */
1267 error = msleep((caddr_t)&ut->uu_sigwait, &p->p_mlock, PPAUSE | PCATCH, "pause", 0);
1268
1269 if (error == ERESTART) {
1270 error = 0;
1271 }
1272
1273 sigw = (ut->uu_sigwait & siglist);
1274 ut->uu_sigmask = ut->uu_oldmask;
1275 ut->uu_oldmask = 0;
1276 ut->uu_flag &= ~UT_SAS_OLDMASK;
1277 sigwait1:
1278 ut->uu_sigwait = 0;
1279 if (!error) {
1280 signum = ffs((unsigned int)sigw);
1281 if (!signum) {
1282 panic("sigwait with no signal wakeup");
1283 }
1284 /* Clear the pending signal in the thread it was delivered */
1285 uth->uu_siglist &= ~(sigmask(signum));
1286
1287 #if CONFIG_DTRACE
1288 DTRACE_PROC2(signal__clear, int, signum, siginfo_t *, &(ut->t_dtrace_siginfo));
1289 #endif
1290
1291 proc_unlock(p);
1292 if (uap->sig != USER_ADDR_NULL) {
1293 error = copyout(&signum, uap->sig, sizeof(int));
1294 }
1295 } else {
1296 proc_unlock(p);
1297 }
1298
1299 return error;
1300 }
1301
1302 int
sigaltstack(__unused proc_t p,struct sigaltstack_args * uap,__unused int32_t * retval)1303 sigaltstack(__unused proc_t p, struct sigaltstack_args *uap, __unused int32_t *retval)
1304 {
1305 struct kern_sigaltstack ss;
1306 struct kern_sigaltstack *pstk;
1307 int error;
1308 struct uthread *uth;
1309 int onstack;
1310
1311 uth = current_uthread();
1312
1313 pstk = &uth->uu_sigstk;
1314 if ((uth->uu_flag & UT_ALTSTACK) == 0) {
1315 uth->uu_sigstk.ss_flags |= SA_DISABLE;
1316 }
1317 onstack = pstk->ss_flags & SA_ONSTACK;
1318 if (uap->oss) {
1319 if (IS_64BIT_PROCESS(p)) {
1320 struct user64_sigaltstack ss64 = {};
1321 sigaltstack_kern_to_user64(pstk, &ss64);
1322 error = copyout(&ss64, uap->oss, sizeof(ss64));
1323 } else {
1324 struct user32_sigaltstack ss32 = {};
1325 sigaltstack_kern_to_user32(pstk, &ss32);
1326 error = copyout(&ss32, uap->oss, sizeof(ss32));
1327 }
1328 if (error) {
1329 return error;
1330 }
1331 }
1332 if (uap->nss == USER_ADDR_NULL) {
1333 return 0;
1334 }
1335 if (IS_64BIT_PROCESS(p)) {
1336 struct user64_sigaltstack ss64;
1337 error = copyin(uap->nss, &ss64, sizeof(ss64));
1338 sigaltstack_user64_to_kern(&ss64, &ss);
1339 } else {
1340 struct user32_sigaltstack ss32;
1341 error = copyin(uap->nss, &ss32, sizeof(ss32));
1342 sigaltstack_user32_to_kern(&ss32, &ss);
1343 }
1344 if (error) {
1345 return error;
1346 }
1347 if ((ss.ss_flags & ~SA_DISABLE) != 0) {
1348 return EINVAL;
1349 }
1350
1351 if (ss.ss_flags & SA_DISABLE) {
1352 /* if we are here we are not in the signal handler ;so no need to check */
1353 if (uth->uu_sigstk.ss_flags & SA_ONSTACK) {
1354 return EINVAL;
1355 }
1356 uth->uu_flag &= ~UT_ALTSTACK;
1357 uth->uu_sigstk.ss_flags = ss.ss_flags;
1358 return 0;
1359 }
1360 if (onstack) {
1361 return EPERM;
1362 }
1363 /* The older stacksize was 8K, enforce that one so no compat problems */
1364 #define OLDMINSIGSTKSZ 8*1024
1365 if (ss.ss_size < OLDMINSIGSTKSZ) {
1366 return ENOMEM;
1367 }
1368 uth->uu_flag |= UT_ALTSTACK;
1369 uth->uu_sigstk = ss;
1370 return 0;
1371 }
1372
1373 int
kill(proc_t cp,struct kill_args * uap,__unused int32_t * retval)1374 kill(proc_t cp, struct kill_args *uap, __unused int32_t *retval)
1375 {
1376 proc_t p;
1377 kauth_cred_t uc = kauth_cred_get();
1378 int posix = uap->posix; /* !0 if posix behaviour desired */
1379
1380 AUDIT_ARG(pid, uap->pid);
1381 AUDIT_ARG(signum, uap->signum);
1382
1383 if ((u_int)uap->signum >= NSIG) {
1384 return EINVAL;
1385 }
1386 if (uap->pid > 0) {
1387 /* kill single process */
1388 if ((p = proc_find(uap->pid)) == NULL) {
1389 if ((p = pzfind(uap->pid)) != NULL) {
1390 /*
1391 * POSIX 1003.1-2001 requires returning success when killing a
1392 * zombie; see Rationale for kill(2).
1393 */
1394 return 0;
1395 }
1396 return ESRCH;
1397 }
1398 AUDIT_ARG(process, p);
1399 if (!cansignal(cp, uc, p, uap->signum)) {
1400 proc_rele(p);
1401 return EPERM;
1402 }
1403 if (uap->signum) {
1404 psignal(p, uap->signum);
1405 }
1406 proc_rele(p);
1407 return 0;
1408 }
1409 switch (uap->pid) {
1410 case -1: /* broadcast signal */
1411 return killpg1(cp, uap->signum, 0, 1, posix);
1412 case 0: /* signal own process group */
1413 return killpg1(cp, uap->signum, 0, 0, posix);
1414 default: /* negative explicit process group */
1415 return killpg1(cp, uap->signum, -(uap->pid), 0, posix);
1416 }
1417 /* NOTREACHED */
1418 }
1419
1420 os_reason_t
build_userspace_exit_reason(uint32_t reason_namespace,uint64_t reason_code,user_addr_t payload,uint32_t payload_size,user_addr_t reason_string,uint64_t reason_flags)1421 build_userspace_exit_reason(uint32_t reason_namespace, uint64_t reason_code, user_addr_t payload, uint32_t payload_size,
1422 user_addr_t reason_string, uint64_t reason_flags)
1423 {
1424 os_reason_t exit_reason = OS_REASON_NULL;
1425
1426 int error = 0;
1427 int num_items_to_copy = 0;
1428 uint32_t user_data_to_copy = 0;
1429 char *reason_user_desc = NULL;
1430 size_t reason_user_desc_len = 0;
1431
1432 exit_reason = os_reason_create(reason_namespace, reason_code);
1433 if (exit_reason == OS_REASON_NULL) {
1434 os_log(OS_LOG_DEFAULT, "build_userspace_exit_reason: failed to allocate exit reason\n");
1435 return exit_reason;
1436 }
1437
1438 exit_reason->osr_flags |= OS_REASON_FLAG_FROM_USERSPACE;
1439
1440 /*
1441 * Only apply flags that are allowed to be passed from userspace.
1442 */
1443 reason_flags = reason_flags & OS_REASON_FLAG_MASK_ALLOWED_FROM_USER;
1444 exit_reason->osr_flags |= reason_flags;
1445
1446 if (!(exit_reason->osr_flags & OS_REASON_FLAG_NO_CRASH_REPORT)) {
1447 exit_reason->osr_flags |= OS_REASON_FLAG_GENERATE_CRASH_REPORT;
1448 }
1449
1450 if (payload != USER_ADDR_NULL) {
1451 if (payload_size == 0) {
1452 os_log(OS_LOG_DEFAULT, "build_userspace_exit_reason: exit reason with namespace %u,"
1453 " nonzero payload but zero length\n", reason_namespace);
1454 exit_reason->osr_flags |= OS_REASON_FLAG_BAD_PARAMS;
1455 payload = USER_ADDR_NULL;
1456 } else {
1457 num_items_to_copy++;
1458
1459 if (payload_size > EXIT_REASON_PAYLOAD_MAX_LEN) {
1460 exit_reason->osr_flags |= OS_REASON_FLAG_PAYLOAD_TRUNCATED;
1461 payload_size = EXIT_REASON_PAYLOAD_MAX_LEN;
1462 }
1463
1464 user_data_to_copy += payload_size;
1465 }
1466 }
1467
1468 if (reason_string != USER_ADDR_NULL) {
1469 reason_user_desc = (char *)kalloc_data(EXIT_REASON_USER_DESC_MAX_LEN, Z_WAITOK);
1470
1471 if (reason_user_desc != NULL) {
1472 error = copyinstr(reason_string, (void *) reason_user_desc,
1473 EXIT_REASON_USER_DESC_MAX_LEN, &reason_user_desc_len);
1474
1475 if (error == 0) {
1476 num_items_to_copy++;
1477 user_data_to_copy += reason_user_desc_len;
1478 } else if (error == ENAMETOOLONG) {
1479 num_items_to_copy++;
1480 reason_user_desc[EXIT_REASON_USER_DESC_MAX_LEN - 1] = '\0';
1481 user_data_to_copy += reason_user_desc_len;
1482 } else {
1483 exit_reason->osr_flags |= OS_REASON_FLAG_FAILED_DATA_COPYIN;
1484 kfree_data(reason_user_desc, EXIT_REASON_USER_DESC_MAX_LEN);
1485 reason_user_desc = NULL;
1486 reason_user_desc_len = 0;
1487 }
1488 }
1489 }
1490
1491 if (num_items_to_copy != 0) {
1492 uint32_t reason_buffer_size_estimate = 0;
1493 mach_vm_address_t data_addr = 0;
1494
1495 reason_buffer_size_estimate = kcdata_estimate_required_buffer_size(num_items_to_copy, user_data_to_copy);
1496
1497 error = os_reason_alloc_buffer(exit_reason, reason_buffer_size_estimate);
1498 if (error != 0) {
1499 os_log(OS_LOG_DEFAULT, "build_userspace_exit_reason: failed to allocate signal reason buffer\n");
1500 goto out_failed_copyin;
1501 }
1502
1503 if (reason_user_desc != NULL && reason_user_desc_len != 0) {
1504 if (KERN_SUCCESS == kcdata_get_memory_addr(&exit_reason->osr_kcd_descriptor,
1505 EXIT_REASON_USER_DESC,
1506 (uint32_t)reason_user_desc_len,
1507 &data_addr)) {
1508 kcdata_memcpy(&exit_reason->osr_kcd_descriptor, (mach_vm_address_t) data_addr,
1509 reason_user_desc, (uint32_t)reason_user_desc_len);
1510 } else {
1511 os_log(OS_LOG_DEFAULT, "build_userspace_exit_reason: failed to allocate space for reason string\n");
1512 goto out_failed_copyin;
1513 }
1514 }
1515
1516 if (payload != USER_ADDR_NULL) {
1517 if (KERN_SUCCESS ==
1518 kcdata_get_memory_addr(&exit_reason->osr_kcd_descriptor,
1519 EXIT_REASON_USER_PAYLOAD,
1520 payload_size,
1521 &data_addr)) {
1522 error = copyin(payload, (void *) data_addr, payload_size);
1523 if (error) {
1524 os_log(OS_LOG_DEFAULT, "build_userspace_exit_reason: failed to copy in payload data with error %d\n", error);
1525 goto out_failed_copyin;
1526 }
1527 } else {
1528 os_log(OS_LOG_DEFAULT, "build_userspace_exit_reason: failed to allocate space for payload data\n");
1529 goto out_failed_copyin;
1530 }
1531 }
1532 }
1533
1534 if (reason_user_desc != NULL) {
1535 kfree_data(reason_user_desc, EXIT_REASON_USER_DESC_MAX_LEN);
1536 reason_user_desc = NULL;
1537 reason_user_desc_len = 0;
1538 }
1539
1540 return exit_reason;
1541
1542 out_failed_copyin:
1543
1544 if (reason_user_desc != NULL) {
1545 kfree_data(reason_user_desc, EXIT_REASON_USER_DESC_MAX_LEN);
1546 reason_user_desc = NULL;
1547 reason_user_desc_len = 0;
1548 }
1549
1550 exit_reason->osr_flags |= OS_REASON_FLAG_FAILED_DATA_COPYIN;
1551 os_reason_alloc_buffer(exit_reason, 0);
1552 return exit_reason;
1553 }
1554
1555 static int
terminate_with_payload_internal(struct proc * cur_proc,int target_pid,uint32_t reason_namespace,uint64_t reason_code,user_addr_t payload,uint32_t payload_size,user_addr_t reason_string,uint64_t reason_flags)1556 terminate_with_payload_internal(struct proc *cur_proc, int target_pid, uint32_t reason_namespace,
1557 uint64_t reason_code, user_addr_t payload, uint32_t payload_size,
1558 user_addr_t reason_string, uint64_t reason_flags)
1559 {
1560 proc_t target_proc = PROC_NULL;
1561 kauth_cred_t cur_cred = kauth_cred_get();
1562
1563 os_reason_t signal_reason = OS_REASON_NULL;
1564
1565 AUDIT_ARG(pid, target_pid);
1566 if ((target_pid <= 0)) {
1567 return EINVAL;
1568 }
1569
1570 target_proc = proc_find(target_pid);
1571 if (target_proc == PROC_NULL) {
1572 return ESRCH;
1573 }
1574
1575 AUDIT_ARG(process, target_proc);
1576
1577 if (!cansignal(cur_proc, cur_cred, target_proc, SIGKILL)) {
1578 proc_rele(target_proc);
1579 return EPERM;
1580 }
1581
1582 if (target_pid != proc_getpid(cur_proc)) {
1583 /*
1584 * FLAG_ABORT should only be set on terminate_with_reason(getpid()) that
1585 * was a fallback from an unsuccessful abort_with_reason(). In that case
1586 * caller's pid matches the target one. Otherwise remove the flag.
1587 */
1588 reason_flags &= ~((typeof(reason_flags))OS_REASON_FLAG_ABORT);
1589 }
1590
1591 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
1592 proc_getpid(target_proc), reason_namespace,
1593 reason_code, 0, 0);
1594
1595 signal_reason = build_userspace_exit_reason(reason_namespace, reason_code, payload, payload_size,
1596 reason_string, (reason_flags | OS_REASON_FLAG_NO_CRASHED_TID));
1597
1598 if (target_pid == proc_getpid(cur_proc)) {
1599 /*
1600 * psignal_thread_with_reason() will pend a SIGKILL on the specified thread or
1601 * return if the thread and/or task are already terminating. Either way, the
1602 * current thread won't return to userspace.
1603 */
1604 psignal_thread_with_reason(target_proc, current_thread(), SIGKILL, signal_reason);
1605 } else {
1606 psignal_with_reason(target_proc, SIGKILL, signal_reason);
1607 }
1608
1609 proc_rele(target_proc);
1610
1611 return 0;
1612 }
1613
1614 int
terminate_with_payload(struct proc * cur_proc,struct terminate_with_payload_args * args,__unused int32_t * retval)1615 terminate_with_payload(struct proc *cur_proc, struct terminate_with_payload_args *args,
1616 __unused int32_t *retval)
1617 {
1618 return terminate_with_payload_internal(cur_proc, args->pid, args->reason_namespace, args->reason_code, args->payload,
1619 args->payload_size, args->reason_string, args->reason_flags);
1620 }
1621
1622 static int
killpg1_allfilt(proc_t p,void * arg)1623 killpg1_allfilt(proc_t p, void * arg)
1624 {
1625 struct killpg1_filtargs * kfargp = (struct killpg1_filtargs *)arg;
1626
1627 /*
1628 * Don't signal initproc, a system process, or the current process if POSIX
1629 * isn't specified.
1630 */
1631 return proc_getpid(p) > 1 && !(p->p_flag & P_SYSTEM) &&
1632 (kfargp->posix ? true : p != kfargp->curproc);
1633 }
1634
1635 static int
killpg1_callback(proc_t p,void * arg)1636 killpg1_callback(proc_t p, void *arg)
1637 {
1638 struct killpg1_iterargs *kargp = (struct killpg1_iterargs *)arg;
1639 int signum = kargp->signum;
1640
1641 if (proc_list_exited(p)) {
1642 /*
1643 * Count zombies as found for the purposes of signalling, since POSIX
1644 * 1003.1-2001 sees signalling zombies as successful. If killpg(2) or
1645 * kill(2) with pid -1 only finds zombies that can be signalled, it
1646 * shouldn't return ESRCH. See the Rationale for kill(2).
1647 *
1648 * Don't call into MAC -- it's not expecting signal checks for exited
1649 * processes.
1650 */
1651 if (cansignal_nomac(kargp->curproc, kargp->uc, p, signum)) {
1652 kargp->nfound++;
1653 }
1654 } else if (cansignal(kargp->curproc, kargp->uc, p, signum)) {
1655 kargp->nfound++;
1656
1657 if (signum != 0) {
1658 psignal(p, signum);
1659 }
1660 }
1661
1662 return PROC_RETURNED;
1663 }
1664
1665 /*
1666 * Common code for kill process group/broadcast kill.
1667 */
1668 int
killpg1(proc_t curproc,int signum,int pgid,int all,int posix)1669 killpg1(proc_t curproc, int signum, int pgid, int all, int posix)
1670 {
1671 kauth_cred_t uc;
1672 struct pgrp *pgrp;
1673 int error = 0;
1674
1675 uc = kauth_cred_proc_ref(curproc);
1676 struct killpg1_iterargs karg = {
1677 .curproc = curproc, .uc = uc, .nfound = 0, .signum = signum
1678 };
1679
1680 if (all) {
1681 /*
1682 * Broadcast to all processes that the user can signal (pid was -1).
1683 */
1684 struct killpg1_filtargs kfarg = {
1685 .posix = posix, .curproc = curproc
1686 };
1687 proc_iterate(PROC_ALLPROCLIST | PROC_ZOMBPROCLIST, killpg1_callback,
1688 &karg, killpg1_allfilt, &kfarg);
1689 } else {
1690 if (pgid == 0) {
1691 /*
1692 * Send to current the current process' process group.
1693 */
1694 pgrp = proc_pgrp(curproc, NULL);
1695 } else {
1696 pgrp = pgrp_find(pgid);
1697 if (pgrp == NULL) {
1698 error = ESRCH;
1699 goto out;
1700 }
1701 }
1702
1703 pgrp_iterate(pgrp, killpg1_callback, &karg, ^bool (proc_t p) {
1704 if (p == kernproc || p == initproc) {
1705 return false;
1706 }
1707 /* XXX shouldn't this allow signalling zombies? */
1708 return !(p->p_flag & P_SYSTEM) && p->p_stat != SZOMB;
1709 });
1710 pgrp_rele(pgrp);
1711 }
1712 error = (karg.nfound > 0 ? 0 : (posix ? EPERM : ESRCH));
1713 out:
1714 kauth_cred_unref(&uc);
1715 return error;
1716 }
1717
1718 /*
1719 * Send a signal to a process group.
1720 */
1721 void
gsignal(int pgid,int signum)1722 gsignal(int pgid, int signum)
1723 {
1724 struct pgrp *pgrp;
1725
1726 if (pgid && (pgrp = pgrp_find(pgid))) {
1727 pgsignal(pgrp, signum, 0);
1728 pgrp_rele(pgrp);
1729 }
1730 }
1731
1732 /*
1733 * Send a signal to a process group. If checkctty is 1,
1734 * limit to members which have a controlling terminal.
1735 */
1736
1737 static int
pgsignal_callback(proc_t p,void * arg)1738 pgsignal_callback(proc_t p, void * arg)
1739 {
1740 int signum = *(int*)arg;
1741
1742 psignal(p, signum);
1743 return PROC_RETURNED;
1744 }
1745
1746 void
pgsignal(struct pgrp * pgrp,int signum,int checkctty)1747 pgsignal(struct pgrp *pgrp, int signum, int checkctty)
1748 {
1749 if (pgrp == PGRP_NULL) {
1750 return;
1751 }
1752
1753 bool (^filter)(proc_t) = ^bool (proc_t p) {
1754 return p->p_flag & P_CONTROLT;
1755 };
1756
1757 pgrp_iterate(pgrp, pgsignal_callback, &signum, checkctty ? filter : NULL);
1758 }
1759
1760
1761 void
tty_pgsignal_locked(struct tty * tp,int signum,int checkctty)1762 tty_pgsignal_locked(struct tty *tp, int signum, int checkctty)
1763 {
1764 struct pgrp * pg;
1765
1766 pg = tty_pgrp_locked(tp);
1767 if (pg != PGRP_NULL) {
1768 tty_unlock(tp);
1769 pgsignal(pg, signum, checkctty);
1770 pgrp_rele(pg);
1771 tty_lock(tp);
1772 }
1773 }
1774 /*
1775 * Send a signal caused by a trap to a specific thread.
1776 */
1777 void
threadsignal(thread_t sig_actthread,int signum,mach_exception_code_t code,boolean_t set_exitreason)1778 threadsignal(thread_t sig_actthread, int signum, mach_exception_code_t code, boolean_t set_exitreason)
1779 {
1780 struct uthread *uth;
1781 struct task * sig_task;
1782 proc_t p;
1783 int mask;
1784
1785 if ((u_int)signum >= NSIG || signum == 0) {
1786 return;
1787 }
1788
1789 mask = sigmask(signum);
1790 if ((mask & threadmask) == 0) {
1791 return;
1792 }
1793 sig_task = get_threadtask(sig_actthread);
1794 p = (proc_t)(get_bsdtask_info(sig_task));
1795
1796 uth = get_bsdthread_info(sig_actthread);
1797
1798 proc_lock(p);
1799 if (!(p->p_lflag & P_LTRACED) && (p->p_sigignore & mask)) {
1800 proc_unlock(p);
1801 return;
1802 }
1803
1804 uth->uu_siglist |= mask;
1805 uth->uu_code = code;
1806
1807 /* Attempt to establish whether the signal will be fatal (mirrors logic in psignal_internal()) */
1808 if (set_exitreason && ((p->p_lflag & P_LTRACED) || (!(uth->uu_sigwait & mask)
1809 && !(uth->uu_sigmask & mask) && !(p->p_sigcatch & mask))) &&
1810 !(mask & stopsigmask) && !(mask & contsigmask)) {
1811 if (uth->uu_exit_reason == OS_REASON_NULL) {
1812 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
1813 proc_getpid(p), OS_REASON_SIGNAL, signum, 0, 0);
1814
1815 os_reason_t signal_reason = build_signal_reason(signum, "exc handler");
1816
1817 set_thread_exit_reason(sig_actthread, signal_reason, TRUE);
1818
1819 /* We dropped/consumed the reference in set_thread_exit_reason() */
1820 signal_reason = OS_REASON_NULL;
1821 }
1822 }
1823
1824 proc_unlock(p);
1825
1826 /* mark on process as well */
1827 signal_setast(sig_actthread);
1828 }
1829
1830 /* Called with proc locked */
1831 static void
set_thread_extra_flags(task_t task,struct uthread * uth,os_reason_t reason)1832 set_thread_extra_flags(task_t task, struct uthread *uth, os_reason_t reason)
1833 {
1834 extern int vm_shared_region_reslide_restrict;
1835 boolean_t reslide_shared_region = FALSE;
1836 boolean_t driver = task_is_driver(task);
1837 assert(uth != NULL);
1838 /*
1839 * Check whether the userland fault address falls within the shared
1840 * region and notify userland if so. To limit the occurrences of shared
1841 * cache resliding - and its associated memory tax - only investigate the
1842 * fault if it is consequence of accessing unmapped memory (SIGSEGV) or
1843 * accessing with incorrect permissions (SIGBUS - KERN_PROTECTION_FAILURE).
1844 *
1845 * This allows launchd to apply special policies around this fault type.
1846 */
1847 if (reason->osr_namespace == OS_REASON_SIGNAL &&
1848 (reason->osr_code == SIGSEGV ||
1849 (reason->osr_code == SIGBUS && uth->uu_code == KERN_PROTECTION_FAILURE))) {
1850 mach_vm_address_t fault_address = uth->uu_subcode;
1851
1852 /* Address is in userland, so we hard clear any non-canonical bits to 0 here */
1853 fault_address = VM_USER_STRIP_PTR(fault_address);
1854
1855 if (fault_address >= SHARED_REGION_BASE &&
1856 fault_address <= SHARED_REGION_BASE + SHARED_REGION_SIZE) {
1857 /*
1858 * Always report whether the fault happened within the shared cache
1859 * region, but only stale the slide if the resliding is extended
1860 * to all processes or if the process faulting is a platform one.
1861 */
1862 reason->osr_flags |= OS_REASON_FLAG_SHAREDREGION_FAULT;
1863
1864 #if __has_feature(ptrauth_calls)
1865 if (!vm_shared_region_reslide_restrict || task_is_hardened_binary(current_task())) {
1866 reslide_shared_region = TRUE;
1867 }
1868 #endif /* __has_feature(ptrauth_calls) */
1869 }
1870
1871 if (driver) {
1872 /*
1873 * Always reslide the DriverKit shared region if the driver faulted.
1874 * The memory cost is acceptable because the DriverKit shared cache is small
1875 * and there are relatively few driver processes.
1876 */
1877 reslide_shared_region = TRUE;
1878 }
1879 }
1880
1881 if (reslide_shared_region) {
1882 vm_shared_region_reslide_stale(driver);
1883 }
1884 }
1885
1886 void
set_thread_exit_reason(void * th,void * reason,boolean_t proc_locked)1887 set_thread_exit_reason(void *th, void *reason, boolean_t proc_locked)
1888 {
1889 struct uthread *targ_uth = get_bsdthread_info(th);
1890 struct task *targ_task = get_threadtask(th);
1891 proc_t targ_proc = NULL;
1892
1893 os_reason_t exit_reason = (os_reason_t)reason;
1894
1895 if (exit_reason == OS_REASON_NULL) {
1896 return;
1897 }
1898
1899 if (!proc_locked) {
1900 targ_proc = (proc_t)(get_bsdtask_info(targ_task));
1901
1902 proc_lock(targ_proc);
1903 }
1904
1905 set_thread_extra_flags(targ_task, targ_uth, exit_reason);
1906
1907 if (targ_uth->uu_exit_reason == OS_REASON_NULL) {
1908 targ_uth->uu_exit_reason = exit_reason;
1909 } else {
1910 /* The caller expects that we drop a reference on the exit reason */
1911 os_reason_free(exit_reason);
1912 }
1913
1914 if (!proc_locked) {
1915 assert(targ_proc != NULL);
1916 proc_unlock(targ_proc);
1917 }
1918 }
1919
1920 /*
1921 * get_signalthread
1922 *
1923 * Picks an appropriate thread from a process to target with a signal.
1924 *
1925 * Called with proc locked.
1926 * Returns thread with BSD ast set.
1927 *
1928 * We attempt to deliver a proc-wide signal to the first thread in the task.
1929 * This allows single threaded applications which use signals to
1930 * be able to be linked with multithreaded libraries.
1931 */
1932 static kern_return_t
get_signalthread(proc_t p,int signum,thread_t * thr)1933 get_signalthread(proc_t p, int signum, thread_t * thr)
1934 {
1935 struct uthread *uth;
1936 sigset_t mask = sigmask(signum);
1937 bool skip_wqthreads = true;
1938
1939 *thr = THREAD_NULL;
1940
1941
1942 again:
1943 TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) {
1944 if (((uth->uu_flag & UT_NO_SIGMASK) == 0) &&
1945 (((uth->uu_sigmask & mask) == 0) || (uth->uu_sigwait & mask))) {
1946 thread_t th = get_machthread(uth);
1947 if (skip_wqthreads && (thread_get_tag(th) & THREAD_TAG_WORKQUEUE)) {
1948 /* Workqueue threads may be parked in the kernel unable to
1949 * deliver signals for an extended period of time, so skip them
1950 * in favor of pthreads in a first pass. (rdar://50054475). */
1951 } else if (check_actforsig(proc_task(p), th, 1) == KERN_SUCCESS) {
1952 *thr = th;
1953 return KERN_SUCCESS;
1954 }
1955 }
1956 }
1957 if (skip_wqthreads) {
1958 skip_wqthreads = false;
1959 goto again;
1960 }
1961 if (get_signalact(proc_task(p), thr, 1) == KERN_SUCCESS) {
1962 return KERN_SUCCESS;
1963 }
1964
1965 return KERN_FAILURE;
1966 }
1967
1968 static os_reason_t
build_signal_reason(int signum,const char * procname)1969 build_signal_reason(int signum, const char *procname)
1970 {
1971 os_reason_t signal_reason = OS_REASON_NULL;
1972 proc_t sender_proc = current_proc();
1973 const uint32_t proc_name_length = sizeof(sender_proc->p_name);
1974 uint32_t reason_buffer_size_estimate = 0;
1975 const char *default_sender_procname = "unknown";
1976 mach_vm_address_t data_addr;
1977 int ret;
1978
1979 signal_reason = os_reason_create(OS_REASON_SIGNAL, signum);
1980 if (signal_reason == OS_REASON_NULL) {
1981 printf("build_signal_reason: unable to allocate signal reason structure.\n");
1982 return signal_reason;
1983 }
1984
1985 reason_buffer_size_estimate = kcdata_estimate_required_buffer_size(2, sizeof(sender_proc->p_name) +
1986 sizeof(pid_t));
1987
1988 ret = os_reason_alloc_buffer_noblock(signal_reason, reason_buffer_size_estimate);
1989 if (ret != 0) {
1990 printf("build_signal_reason: unable to allocate signal reason buffer.\n");
1991 return signal_reason;
1992 }
1993
1994 if (KERN_SUCCESS == kcdata_get_memory_addr(&signal_reason->osr_kcd_descriptor, KCDATA_TYPE_PID,
1995 sizeof(pid_t), &data_addr)) {
1996 pid_t pid = proc_getpid(sender_proc);
1997 kcdata_memcpy(&signal_reason->osr_kcd_descriptor, data_addr, &pid, sizeof(pid));
1998 } else {
1999 printf("build_signal_reason: exceeded space in signal reason buf, unable to log PID\n");
2000 }
2001
2002 if (KERN_SUCCESS == kcdata_get_memory_addr(&signal_reason->osr_kcd_descriptor, KCDATA_TYPE_PROCNAME,
2003 proc_name_length, &data_addr)) {
2004 if (procname) {
2005 char truncated_procname[proc_name_length];
2006 strncpy((char *) &truncated_procname, procname, proc_name_length);
2007 truncated_procname[proc_name_length - 1] = '\0';
2008
2009 kcdata_memcpy(&signal_reason->osr_kcd_descriptor, data_addr, truncated_procname,
2010 (uint32_t)strlen((char *) &truncated_procname));
2011 } else if (*sender_proc->p_name) {
2012 kcdata_memcpy(&signal_reason->osr_kcd_descriptor, data_addr, &sender_proc->p_name,
2013 sizeof(sender_proc->p_name));
2014 } else {
2015 kcdata_memcpy(&signal_reason->osr_kcd_descriptor, data_addr, &default_sender_procname,
2016 (uint32_t)strlen(default_sender_procname) + 1);
2017 }
2018 } else {
2019 printf("build_signal_reason: exceeded space in signal reason buf, unable to log procname\n");
2020 }
2021
2022 return signal_reason;
2023 }
2024
2025 /*
2026 * Send the signal to the process. If the signal has an action, the action
2027 * is usually performed by the target process rather than the caller; we add
2028 * the signal to the set of pending signals for the process.
2029 *
2030 * Always drops a reference on a signal_reason if one is provided, whether via
2031 * passing it to a thread or deallocating directly.
2032 *
2033 * Exceptions:
2034 * o When a stop signal is sent to a sleeping process that takes the
2035 * default action, the process is stopped without awakening it.
2036 * o SIGCONT restarts stopped processes (or puts them back to sleep)
2037 * regardless of the signal action (eg, blocked or ignored).
2038 *
2039 * Other ignored signals are discarded immediately.
2040 */
2041 static void
psignal_internal(proc_t p,task_t task,thread_t thread,int flavor,int signum,os_reason_t signal_reason)2042 psignal_internal(proc_t p, task_t task, thread_t thread, int flavor, int signum, os_reason_t signal_reason)
2043 {
2044 int prop;
2045 user_addr_t action = USER_ADDR_NULL;
2046 proc_t sig_proc;
2047 thread_t sig_thread;
2048 task_t sig_task;
2049 int mask;
2050 struct uthread *uth;
2051 kern_return_t kret;
2052 uid_t r_uid;
2053 proc_t pp;
2054 kauth_cred_t my_cred;
2055 char *launchd_exit_reason_desc = NULL;
2056 boolean_t update_thread_policy = FALSE;
2057
2058 if ((u_int)signum >= NSIG || signum == 0) {
2059 panic("psignal: bad signal number %d", signum);
2060 }
2061
2062 mask = sigmask(signum);
2063 prop = sigprop[signum];
2064
2065 #if SIGNAL_DEBUG
2066 if (rdebug_proc && (p != PROC_NULL) && (p == rdebug_proc)) {
2067 ram_printf(3);
2068 }
2069 #endif /* SIGNAL_DEBUG */
2070
2071 /* catch unexpected initproc kills early for easier debuggging */
2072 if (signum == SIGKILL && p == initproc) {
2073 if (signal_reason == NULL) {
2074 panic_plain("unexpected SIGKILL of %s %s (no reason provided)",
2075 (p->p_name[0] != '\0' ? p->p_name : "initproc"),
2076 ((proc_getcsflags(p) & CS_KILLED) ? "(CS_KILLED)" : ""));
2077 } else {
2078 launchd_exit_reason_desc = exit_reason_get_string_desc(signal_reason);
2079 panic_plain("unexpected SIGKILL of %s %s with reason -- namespace %d code 0x%llx description %." LAUNCHD_PANIC_REASON_STRING_MAXLEN "s",
2080 (p->p_name[0] != '\0' ? p->p_name : "initproc"),
2081 ((proc_getcsflags(p) & CS_KILLED) ? "(CS_KILLED)" : ""),
2082 signal_reason->osr_namespace, signal_reason->osr_code,
2083 launchd_exit_reason_desc ? launchd_exit_reason_desc : "none");
2084 }
2085 }
2086
2087 /*
2088 * We will need the task pointer later. Grab it now to
2089 * check for a zombie process. Also don't send signals
2090 * to kernel internal tasks.
2091 */
2092 if (flavor & PSIG_VFORK) {
2093 sig_task = task;
2094 sig_thread = thread;
2095 sig_proc = p;
2096 } else if (flavor & PSIG_THREAD) {
2097 sig_task = get_threadtask(thread);
2098 sig_thread = thread;
2099 sig_proc = (proc_t)get_bsdtask_info(sig_task);
2100 } else if (flavor & PSIG_TRY_THREAD) {
2101 assert((thread == current_thread()) && (p == current_proc()));
2102 sig_task = proc_task(p);
2103 sig_thread = thread;
2104 sig_proc = p;
2105 } else {
2106 sig_task = proc_task(p);
2107 sig_thread = THREAD_NULL;
2108 sig_proc = p;
2109 }
2110
2111 if ((sig_task == TASK_NULL) || is_kerneltask(sig_task)) {
2112 os_reason_free(signal_reason);
2113 return;
2114 }
2115
2116 if ((flavor & (PSIG_VFORK | PSIG_THREAD)) == 0) {
2117 proc_knote(sig_proc, NOTE_SIGNAL | signum);
2118 }
2119
2120 if ((flavor & PSIG_LOCKED) == 0) {
2121 proc_signalstart(sig_proc, 0);
2122 }
2123
2124 /* Don't send signals to a process that has ignored them. */
2125 if (((flavor & PSIG_VFORK) == 0) && ((sig_proc->p_lflag & P_LTRACED) == 0) && (sig_proc->p_sigignore & mask)) {
2126 DTRACE_PROC3(signal__discard, thread_t, sig_thread, proc_t, sig_proc, int, signum);
2127 goto sigout_unlocked;
2128 }
2129
2130 /*
2131 * The proc_lock prevents the targeted thread from being deallocated
2132 * or handling the signal until we're done signaling it.
2133 *
2134 * Once the proc_lock is dropped, we have no guarantee the thread or uthread exists anymore.
2135 *
2136 * XXX: What if the thread goes inactive after the thread passes bsd ast point?
2137 */
2138 proc_lock(sig_proc);
2139
2140 /*
2141 * Don't send signals to a process which has already exited and thus
2142 * committed to a particular p_xstat exit code.
2143 * Additionally, don't abort the process running 'reboot'.
2144 */
2145 if (ISSET(sig_proc->p_flag, P_REBOOT) || ISSET(sig_proc->p_lflag, P_LEXIT)) {
2146 DTRACE_PROC3(signal__discard, thread_t, sig_thread, proc_t, sig_proc, int, signum);
2147 goto sigout_locked;
2148 }
2149
2150 if (flavor & PSIG_VFORK) {
2151 action = SIG_DFL;
2152 act_set_astbsd(sig_thread);
2153 kret = KERN_SUCCESS;
2154 } else if (flavor & PSIG_TRY_THREAD) {
2155 uth = get_bsdthread_info(sig_thread);
2156 if (((uth->uu_flag & UT_NO_SIGMASK) == 0) &&
2157 (((uth->uu_sigmask & mask) == 0) || (uth->uu_sigwait & mask)) &&
2158 ((kret = check_actforsig(proc_task(sig_proc), sig_thread, 1)) == KERN_SUCCESS)) {
2159 /* deliver to specified thread */
2160 } else {
2161 /* deliver to any willing thread */
2162 kret = get_signalthread(sig_proc, signum, &sig_thread);
2163 }
2164 } else if (flavor & PSIG_THREAD) {
2165 /* If successful return with ast set */
2166 kret = check_actforsig(sig_task, sig_thread, 1);
2167 } else {
2168 /* If successful return with ast set */
2169 kret = get_signalthread(sig_proc, signum, &sig_thread);
2170 }
2171
2172 if (kret != KERN_SUCCESS) {
2173 DTRACE_PROC3(signal__discard, thread_t, sig_thread, proc_t, sig_proc, int, signum);
2174 proc_unlock(sig_proc);
2175 goto sigout_unlocked;
2176 }
2177
2178 uth = get_bsdthread_info(sig_thread);
2179
2180 /*
2181 * If proc is traced, always give parent a chance.
2182 */
2183
2184 if ((flavor & PSIG_VFORK) == 0) {
2185 if (sig_proc->p_lflag & P_LTRACED) {
2186 action = SIG_DFL;
2187 } else {
2188 /*
2189 * If the signal is being ignored,
2190 * then we forget about it immediately.
2191 * (Note: we don't set SIGCONT in p_sigignore,
2192 * and if it is set to SIG_IGN,
2193 * action will be SIG_DFL here.)
2194 */
2195 if (sig_proc->p_sigignore & mask) {
2196 goto sigout_locked;
2197 }
2198
2199 if (uth->uu_sigwait & mask) {
2200 action = KERN_SIG_WAIT;
2201 } else if (uth->uu_sigmask & mask) {
2202 action = KERN_SIG_HOLD;
2203 } else if (sig_proc->p_sigcatch & mask) {
2204 action = KERN_SIG_CATCH;
2205 } else {
2206 action = SIG_DFL;
2207 }
2208 }
2209 }
2210
2211 /* TODO: p_nice isn't hooked up to the scheduler... */
2212 if (sig_proc->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
2213 (sig_proc->p_lflag & P_LTRACED) == 0) {
2214 sig_proc->p_nice = NZERO;
2215 }
2216
2217 if (prop & SA_CONT) {
2218 uth->uu_siglist &= ~stopsigmask;
2219 }
2220
2221 if (prop & SA_STOP) {
2222 struct pgrp *pg;
2223 /*
2224 * If sending a tty stop signal to a member of an orphaned
2225 * process group, discard the signal here if the action
2226 * is default; don't stop the process below if sleeping,
2227 * and don't clear any pending SIGCONT.
2228 */
2229 pg = proc_pgrp(sig_proc, NULL);
2230 if (prop & SA_TTYSTOP && pg->pg_jobc == 0 &&
2231 action == SIG_DFL) {
2232 pgrp_rele(pg);
2233 goto sigout_locked;
2234 }
2235 pgrp_rele(pg);
2236 uth->uu_siglist &= ~contsigmask;
2237 }
2238
2239 uth->uu_siglist |= mask;
2240
2241 /*
2242 * Defer further processing for signals which are held,
2243 * except that stopped processes must be continued by SIGCONT.
2244 */
2245 if ((action == KERN_SIG_HOLD) && ((prop & SA_CONT) == 0 || sig_proc->p_stat != SSTOP)) {
2246 goto sigout_locked;
2247 }
2248
2249 /*
2250 * SIGKILL priority twiddling moved here from above because
2251 * it needs sig_thread. Could merge it into large switch
2252 * below if we didn't care about priority for tracing
2253 * as SIGKILL's action is always SIG_DFL.
2254 *
2255 * TODO: p_nice isn't hooked up to the scheduler...
2256 */
2257 if ((signum == SIGKILL) && (sig_proc->p_nice > NZERO)) {
2258 sig_proc->p_nice = NZERO;
2259 }
2260
2261 /*
2262 * Process is traced - wake it up (if not already
2263 * stopped) so that it can discover the signal in
2264 * issig() and stop for the parent.
2265 */
2266 if (sig_proc->p_lflag & P_LTRACED) {
2267 if (sig_proc->p_stat != SSTOP) {
2268 goto runlocked;
2269 } else {
2270 goto sigout_locked;
2271 }
2272 }
2273
2274 if ((flavor & PSIG_VFORK) != 0) {
2275 goto runlocked;
2276 }
2277
2278 if (action == KERN_SIG_WAIT) {
2279 #if CONFIG_DTRACE
2280 /*
2281 * DTrace proc signal-clear returns a siginfo_t. Collect the needed info.
2282 */
2283 r_uid = kauth_getruid(); /* per thread credential; protected by our thread context */
2284
2285 bzero((caddr_t)&(uth->t_dtrace_siginfo), sizeof(uth->t_dtrace_siginfo));
2286
2287 uth->t_dtrace_siginfo.si_signo = signum;
2288 uth->t_dtrace_siginfo.si_pid = proc_getpid(current_proc());
2289 uth->t_dtrace_siginfo.si_status = W_EXITCODE(signum, 0);
2290 uth->t_dtrace_siginfo.si_uid = r_uid;
2291 uth->t_dtrace_siginfo.si_code = 0;
2292 #endif
2293 uth->uu_sigwait = mask;
2294 uth->uu_siglist &= ~mask;
2295 wakeup(&uth->uu_sigwait);
2296 /* if it is SIGCONT resume whole process */
2297 if (prop & SA_CONT) {
2298 OSBitOrAtomic(P_CONTINUED, &sig_proc->p_flag);
2299 sig_proc->p_contproc = proc_getpid(current_proc());
2300 (void) task_resume_internal(sig_task);
2301 }
2302 goto sigout_locked;
2303 }
2304
2305 if (action != SIG_DFL) {
2306 /*
2307 * User wants to catch the signal.
2308 * Wake up the thread, but don't un-suspend it
2309 * (except for SIGCONT).
2310 */
2311 if (prop & SA_CONT) {
2312 OSBitOrAtomic(P_CONTINUED, &sig_proc->p_flag);
2313 (void) task_resume_internal(sig_task);
2314 sig_proc->p_stat = SRUN;
2315 } else if (sig_proc->p_stat == SSTOP) {
2316 goto sigout_locked;
2317 }
2318 /*
2319 * Fill out siginfo structure information to pass to the
2320 * signalled process/thread sigaction handler, when it
2321 * wakes up. si_code is 0 because this is an ordinary
2322 * signal, not a SIGCHLD, and so si_status is the signal
2323 * number itself, instead of the child process exit status.
2324 * We shift this left because it will be shifted right before
2325 * it is passed to user space. kind of ugly to use W_EXITCODE
2326 * this way, but it beats defining a new macro.
2327 *
2328 * Note: Avoid the SIGCHLD recursion case!
2329 */
2330 if (signum != SIGCHLD) {
2331 r_uid = kauth_getruid();
2332
2333 sig_proc->si_pid = proc_getpid(current_proc());
2334 sig_proc->si_status = W_EXITCODE(signum, 0);
2335 sig_proc->si_uid = r_uid;
2336 sig_proc->si_code = 0;
2337 }
2338
2339 goto runlocked;
2340 } else {
2341 /* Default action - varies */
2342 if (mask & stopsigmask) {
2343 assert(signal_reason == NULL);
2344 /*
2345 * These are the signals which by default
2346 * stop a process.
2347 *
2348 * Don't clog system with children of init
2349 * stopped from the keyboard.
2350 */
2351 if (!(prop & SA_STOP) && sig_proc->p_pptr == initproc) {
2352 uth->uu_siglist &= ~mask;
2353 proc_unlock(sig_proc);
2354 /* siglock still locked, proc_lock not locked */
2355 psignal_locked(sig_proc, SIGKILL);
2356 goto sigout_unlocked;
2357 }
2358
2359 /*
2360 * Stop the task
2361 * if task hasn't already been stopped by
2362 * a signal.
2363 */
2364 uth->uu_siglist &= ~mask;
2365 if (sig_proc->p_stat != SSTOP) {
2366 sig_proc->p_xstat = signum;
2367 sig_proc->p_stat = SSTOP;
2368 OSBitAndAtomic(~((uint32_t)P_CONTINUED), &sig_proc->p_flag);
2369 sig_proc->p_lflag &= ~P_LWAITED;
2370 proc_signalend(sig_proc, 1);
2371 proc_unlock(sig_proc);
2372
2373 pp = proc_parentholdref(sig_proc);
2374 proc_signalstart(sig_proc, 0);
2375 stop(sig_proc, pp);
2376 if ((pp != PROC_NULL) && ((pp->p_flag & P_NOCLDSTOP) == 0)) {
2377 my_cred = kauth_cred_proc_ref(sig_proc);
2378 r_uid = kauth_cred_getruid(my_cred);
2379 kauth_cred_unref(&my_cred);
2380
2381 proc_lock(sig_proc);
2382 pp->si_pid = proc_getpid(sig_proc);
2383 /*
2384 * POSIX: sigaction for a stopped child
2385 * when sent to the parent must set the
2386 * child's signal number into si_status.
2387 */
2388 if (signum != SIGSTOP) {
2389 pp->si_status = WEXITSTATUS(sig_proc->p_xstat);
2390 } else {
2391 pp->si_status = W_EXITCODE(signum, signum);
2392 }
2393 pp->si_code = CLD_STOPPED;
2394 pp->si_uid = r_uid;
2395 proc_unlock(sig_proc);
2396
2397 psignal(pp, SIGCHLD);
2398 }
2399 if (pp != PROC_NULL) {
2400 proc_parentdropref(pp, 0);
2401 }
2402
2403 goto sigout_unlocked;
2404 }
2405
2406 goto sigout_locked;
2407 }
2408
2409 DTRACE_PROC3(signal__send, thread_t, sig_thread, proc_t, p, int, signum);
2410
2411 switch (signum) {
2412 /*
2413 * Signals ignored by default have been dealt
2414 * with already, since their bits are on in
2415 * p_sigignore.
2416 */
2417
2418 case SIGKILL:
2419 /*
2420 * Kill signal always sets process running and
2421 * unsuspends it.
2422 */
2423 /*
2424 * Process will be running after 'run'
2425 */
2426 sig_proc->p_stat = SRUN;
2427 /*
2428 * In scenarios where suspend/resume are racing
2429 * the signal we are missing AST_BSD by the time
2430 * we get here, set again to avoid races. This
2431 * was the scenario with spindump enabled shutdowns.
2432 * We would need to cover this approp down the line.
2433 */
2434 act_set_astbsd(sig_thread);
2435 kret = thread_abort(sig_thread);
2436 update_thread_policy = (kret == KERN_SUCCESS);
2437
2438 if (uth->uu_exit_reason == OS_REASON_NULL) {
2439 if (signal_reason == OS_REASON_NULL) {
2440 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
2441 proc_getpid(sig_proc), OS_REASON_SIGNAL, signum, 0, 0);
2442
2443 signal_reason = build_signal_reason(signum, NULL);
2444 }
2445
2446 os_reason_ref(signal_reason);
2447 set_thread_exit_reason(sig_thread, signal_reason, TRUE);
2448 }
2449
2450 goto sigout_locked;
2451
2452 case SIGCONT:
2453 /*
2454 * Let the process run. If it's sleeping on an
2455 * event, it remains so.
2456 */
2457 assert(signal_reason == NULL);
2458 OSBitOrAtomic(P_CONTINUED, &sig_proc->p_flag);
2459 sig_proc->p_contproc = proc_getpid(sig_proc);
2460 sig_proc->p_xstat = signum;
2461
2462 (void) task_resume_internal(sig_task);
2463
2464 /*
2465 * When processing a SIGCONT, we need to check
2466 * to see if there are signals pending that
2467 * were not delivered because we had been
2468 * previously stopped. If that's the case,
2469 * we need to thread_abort_safely() to trigger
2470 * interruption of the current system call to
2471 * cause their handlers to fire. If it's only
2472 * the SIGCONT, then don't wake up.
2473 */
2474 if (((flavor & (PSIG_VFORK | PSIG_THREAD)) == 0) && (((uth->uu_siglist & ~uth->uu_sigmask) & ~sig_proc->p_sigignore) & ~mask)) {
2475 uth->uu_siglist &= ~mask;
2476 sig_proc->p_stat = SRUN;
2477 goto runlocked;
2478 }
2479
2480 uth->uu_siglist &= ~mask;
2481 sig_proc->p_stat = SRUN;
2482 goto sigout_locked;
2483
2484 default:
2485 {
2486 /*
2487 * A signal which has a default action of killing
2488 * the process, and for which there is no handler,
2489 * needs to act like SIGKILL
2490 *
2491 * The thread_sstop condition is a remnant of a fix
2492 * where PSIG_THREAD exit reasons were not set
2493 * correctly (93593933). We keep the behavior with
2494 * SSTOP the same as before.
2495 */
2496 const bool default_kill = (action == SIG_DFL) && (prop & SA_KILL);
2497 const bool thread_sstop = (flavor & PSIG_THREAD) && (sig_proc->p_stat == SSTOP);
2498
2499 if (default_kill && !thread_sstop) {
2500 sig_proc->p_stat = SRUN;
2501 kret = thread_abort(sig_thread);
2502 update_thread_policy = (kret == KERN_SUCCESS);
2503
2504 if (uth->uu_exit_reason == OS_REASON_NULL) {
2505 if (signal_reason == OS_REASON_NULL) {
2506 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
2507 proc_getpid(sig_proc), OS_REASON_SIGNAL, signum, 0, 0);
2508
2509 signal_reason = build_signal_reason(signum, NULL);
2510 }
2511
2512 os_reason_ref(signal_reason);
2513 set_thread_exit_reason(sig_thread, signal_reason, TRUE);
2514 }
2515
2516 goto sigout_locked;
2517 }
2518
2519 /*
2520 * All other signals wake up the process, but don't
2521 * resume it.
2522 */
2523 if (sig_proc->p_stat == SSTOP) {
2524 goto sigout_locked;
2525 }
2526 goto runlocked;
2527 }
2528 }
2529 }
2530 /*NOTREACHED*/
2531
2532 runlocked:
2533 /*
2534 * If we're being traced (possibly because someone attached us
2535 * while we were stopped), check for a signal from the debugger.
2536 */
2537 if (sig_proc->p_stat == SSTOP) {
2538 if ((sig_proc->p_lflag & P_LTRACED) != 0 && sig_proc->p_xstat != 0) {
2539 uth->uu_siglist |= sigmask(sig_proc->p_xstat);
2540 }
2541
2542 if ((flavor & PSIG_VFORK) != 0) {
2543 sig_proc->p_stat = SRUN;
2544 }
2545 } else {
2546 /*
2547 * setrunnable(p) in BSD and
2548 * Wake up the thread if it is interruptible.
2549 */
2550 sig_proc->p_stat = SRUN;
2551 if ((flavor & PSIG_VFORK) == 0) {
2552 thread_abort_safely(sig_thread);
2553 }
2554 }
2555
2556 sigout_locked:
2557 if (update_thread_policy) {
2558 /*
2559 * Update the thread policy to heading to terminate, increase priority if
2560 * necessary. This needs to be done before we drop the proc lock because the
2561 * thread can take the fatal signal once it's dropped.
2562 */
2563 proc_set_thread_policy(sig_thread, TASK_POLICY_ATTRIBUTE, TASK_POLICY_TERMINATED, TASK_POLICY_ENABLE);
2564 }
2565
2566 proc_unlock(sig_proc);
2567
2568 sigout_unlocked:
2569 os_reason_free(signal_reason);
2570 if ((flavor & PSIG_LOCKED) == 0) {
2571 proc_signalend(sig_proc, 0);
2572 }
2573 }
2574
2575 void
psignal(proc_t p,int signum)2576 psignal(proc_t p, int signum)
2577 {
2578 psignal_internal(p, NULL, NULL, 0, signum, NULL);
2579 }
2580
2581 void
psignal_with_reason(proc_t p,int signum,struct os_reason * signal_reason)2582 psignal_with_reason(proc_t p, int signum, struct os_reason *signal_reason)
2583 {
2584 psignal_internal(p, NULL, NULL, 0, signum, signal_reason);
2585 }
2586
2587 void
psignal_sigkill_with_reason(struct proc * p,struct os_reason * signal_reason)2588 psignal_sigkill_with_reason(struct proc *p, struct os_reason *signal_reason)
2589 {
2590 psignal_internal(p, NULL, NULL, 0, SIGKILL, signal_reason);
2591 }
2592
2593 void
psignal_locked(proc_t p,int signum)2594 psignal_locked(proc_t p, int signum)
2595 {
2596 psignal_internal(p, NULL, NULL, PSIG_LOCKED, signum, NULL);
2597 }
2598
2599 void
psignal_vfork_with_reason(proc_t p,task_t new_task,thread_t thread,int signum,struct os_reason * signal_reason)2600 psignal_vfork_with_reason(proc_t p, task_t new_task, thread_t thread, int signum, struct os_reason *signal_reason)
2601 {
2602 psignal_internal(p, new_task, thread, PSIG_VFORK, signum, signal_reason);
2603 }
2604
2605 void
psignal_vfork(proc_t p,task_t new_task,thread_t thread,int signum)2606 psignal_vfork(proc_t p, task_t new_task, thread_t thread, int signum)
2607 {
2608 psignal_internal(p, new_task, thread, PSIG_VFORK, signum, NULL);
2609 }
2610
2611 void
psignal_uthread(thread_t thread,int signum)2612 psignal_uthread(thread_t thread, int signum)
2613 {
2614 psignal_internal(PROC_NULL, TASK_NULL, thread, PSIG_THREAD, signum, NULL);
2615 }
2616
2617 /* same as psignal(), but prefer delivery to 'thread' if possible */
2618 void
psignal_try_thread(proc_t p,thread_t thread,int signum)2619 psignal_try_thread(proc_t p, thread_t thread, int signum)
2620 {
2621 psignal_internal(p, NULL, thread, PSIG_TRY_THREAD, signum, NULL);
2622 }
2623
2624 void
psignal_try_thread_with_reason(proc_t p,thread_t thread,int signum,struct os_reason * signal_reason)2625 psignal_try_thread_with_reason(proc_t p, thread_t thread, int signum, struct os_reason *signal_reason)
2626 {
2627 psignal_internal(p, TASK_NULL, thread, PSIG_TRY_THREAD, signum, signal_reason);
2628 }
2629
2630 void
psignal_thread_with_reason(proc_t p,thread_t thread,int signum,struct os_reason * signal_reason)2631 psignal_thread_with_reason(proc_t p, thread_t thread, int signum, struct os_reason *signal_reason)
2632 {
2633 psignal_internal(p, TASK_NULL, thread, PSIG_THREAD, signum, signal_reason);
2634 }
2635
2636 void
psignal_sigkill_try_thread_with_reason(proc_t p,thread_t thread,struct os_reason * signal_reason)2637 psignal_sigkill_try_thread_with_reason(proc_t p, thread_t thread, struct os_reason *signal_reason)
2638 {
2639 psignal_try_thread_with_reason(p, thread, SIGKILL, signal_reason);
2640 }
2641
2642 /*
2643 * If the current process has received a signal (should be caught or cause
2644 * termination, should interrupt current syscall), return the signal number.
2645 * Stop signals with default action are processed immediately, then cleared;
2646 * they aren't returned. This is checked after each entry to the system for
2647 * a syscall or trap (though this can usually be done without calling issignal
2648 * by checking the pending signal masks in the CURSIG macro.) The normal call
2649 * sequence is
2650 *
2651 * while (signum = CURSIG(curproc))
2652 * postsig(signum);
2653 */
2654 int
issignal_locked(proc_t p)2655 issignal_locked(proc_t p)
2656 {
2657 int signum, mask, prop, sigbits;
2658 thread_t cur_act;
2659 struct uthread * ut;
2660 proc_t pp;
2661 kauth_cred_t my_cred;
2662 int retval = 0;
2663 uid_t r_uid;
2664
2665 cur_act = current_thread();
2666
2667 #if SIGNAL_DEBUG
2668 if (rdebug_proc && (p == rdebug_proc)) {
2669 ram_printf(3);
2670 }
2671 #endif /* SIGNAL_DEBUG */
2672
2673 /*
2674 * Try to grab the signal lock.
2675 */
2676 if (sig_try_locked(p) <= 0) {
2677 return 0;
2678 }
2679
2680 proc_signalstart(p, 1);
2681
2682 ut = get_bsdthread_info(cur_act);
2683 for (;;) {
2684 sigbits = ut->uu_siglist & ~ut->uu_sigmask;
2685
2686 if (p->p_lflag & P_LPPWAIT) {
2687 sigbits &= ~stopsigmask;
2688 }
2689 if (sigbits == 0) { /* no signal to send */
2690 retval = 0;
2691 goto out;
2692 }
2693
2694 signum = ffs((unsigned int)sigbits);
2695 mask = sigmask(signum);
2696 prop = sigprop[signum];
2697
2698 /*
2699 * We should see pending but ignored signals
2700 * only if P_LTRACED was on when they were posted.
2701 */
2702 if (mask & p->p_sigignore && (p->p_lflag & P_LTRACED) == 0) {
2703 ut->uu_siglist &= ~mask;
2704 continue;
2705 }
2706
2707 if (p->p_lflag & P_LTRACED && (p->p_lflag & P_LPPWAIT) == 0) {
2708 /*
2709 * If traced, deliver the signal to the debugger, and wait to be
2710 * released.
2711 */
2712 task_t task;
2713 p->p_xstat = signum;
2714
2715 if (p->p_lflag & P_LSIGEXC) {
2716 p->sigwait = TRUE;
2717 p->sigwait_thread = cur_act;
2718 p->p_stat = SSTOP;
2719 OSBitAndAtomic(~((uint32_t)P_CONTINUED), &p->p_flag);
2720 p->p_lflag &= ~P_LWAITED;
2721 ut->uu_siglist &= ~mask; /* clear the current signal from the pending list */
2722 proc_signalend(p, 1);
2723 proc_unlock(p);
2724 do_bsdexception(EXC_SOFTWARE, EXC_SOFT_SIGNAL, signum);
2725 proc_lock(p);
2726 proc_signalstart(p, 1);
2727 } else {
2728 proc_unlock(p);
2729 my_cred = kauth_cred_proc_ref(p);
2730 r_uid = kauth_cred_getruid(my_cred);
2731 kauth_cred_unref(&my_cred);
2732
2733 /*
2734 * XXX Have to really stop for debuggers;
2735 * XXX stop() doesn't do the right thing.
2736 */
2737 task = proc_task(p);
2738 task_suspend_internal(task);
2739
2740 proc_lock(p);
2741 p->sigwait = TRUE;
2742 p->sigwait_thread = cur_act;
2743 p->p_stat = SSTOP;
2744 OSBitAndAtomic(~((uint32_t)P_CONTINUED), &p->p_flag);
2745 p->p_lflag &= ~P_LWAITED;
2746 ut->uu_siglist &= ~mask;
2747
2748 proc_signalend(p, 1);
2749 proc_unlock(p);
2750
2751 pp = proc_parentholdref(p);
2752 if (pp != PROC_NULL) {
2753 proc_lock(pp);
2754 pp->si_pid = proc_getpid(p);
2755 pp->p_xhighbits = p->p_xhighbits;
2756 p->p_xhighbits = 0;
2757 pp->si_status = p->p_xstat;
2758 pp->si_code = CLD_TRAPPED;
2759 pp->si_uid = r_uid;
2760 proc_unlock(pp);
2761
2762 psignal(pp, SIGCHLD);
2763 proc_list_lock();
2764 wakeup((caddr_t)pp);
2765 proc_parentdropref(pp, 1);
2766 proc_list_unlock();
2767 }
2768
2769 assert_wait((caddr_t)&p->sigwait, (THREAD_INTERRUPTIBLE));
2770 thread_block(THREAD_CONTINUE_NULL);
2771 proc_lock(p);
2772 proc_signalstart(p, 1);
2773 }
2774
2775 p->sigwait = FALSE;
2776 p->sigwait_thread = NULL;
2777 wakeup((caddr_t)&p->sigwait_thread);
2778
2779 if (signum == SIGKILL || ut->uu_siglist & sigmask(SIGKILL)) {
2780 /*
2781 * Deliver a pending sigkill even if it's not the current signal.
2782 * Necessary for PT_KILL, which should not be delivered to the
2783 * debugger, but we can't differentiate it from any other KILL.
2784 */
2785 signum = SIGKILL;
2786 goto deliver_sig;
2787 }
2788
2789 /* We may have to quit. */
2790 if (thread_should_abort(current_thread())) {
2791 retval = 0;
2792 goto out;
2793 }
2794
2795 /*
2796 * If parent wants us to take the signal,
2797 * then it will leave it in p->p_xstat;
2798 * otherwise we just look for signals again.
2799 */
2800 signum = p->p_xstat;
2801 if (signum == 0) {
2802 continue;
2803 }
2804
2805 /*
2806 * Put the new signal into p_siglist. If the
2807 * signal is being masked, look for other signals.
2808 */
2809 mask = sigmask(signum);
2810 ut->uu_siglist |= mask;
2811 if (ut->uu_sigmask & mask) {
2812 continue;
2813 }
2814 }
2815
2816 /*
2817 * Decide whether the signal should be returned.
2818 * Return the signal's number, or fall through
2819 * to clear it from the pending mask.
2820 */
2821
2822 switch ((long)SIGACTION(p, signum)) {
2823 case (long)SIG_DFL:
2824 /*
2825 * If there is a pending stop signal to process
2826 * with default action, stop here,
2827 * then clear the signal. However,
2828 * if process is member of an orphaned
2829 * process group, ignore tty stop signals.
2830 */
2831 if (prop & SA_STOP) {
2832 struct pgrp * pg;
2833
2834 proc_unlock(p);
2835 pg = proc_pgrp(p, NULL);
2836 if (p->p_lflag & P_LTRACED ||
2837 (pg->pg_jobc == 0 &&
2838 prop & SA_TTYSTOP)) {
2839 proc_lock(p);
2840 pgrp_rele(pg);
2841 break; /* ignore signal */
2842 }
2843 pgrp_rele(pg);
2844 if (p->p_stat != SSTOP) {
2845 proc_lock(p);
2846 p->p_xstat = signum;
2847 p->p_stat = SSTOP;
2848 p->p_lflag &= ~P_LWAITED;
2849 proc_signalend(p, 1);
2850 proc_unlock(p);
2851
2852 pp = proc_parentholdref(p);
2853 proc_signalstart(p, 0);
2854 stop(p, pp);
2855 if ((pp != PROC_NULL) && ((pp->p_flag & P_NOCLDSTOP) == 0)) {
2856 my_cred = kauth_cred_proc_ref(p);
2857 r_uid = kauth_cred_getruid(my_cred);
2858 kauth_cred_unref(&my_cred);
2859
2860 proc_lock(pp);
2861 pp->si_pid = proc_getpid(p);
2862 pp->si_status = WEXITSTATUS(p->p_xstat);
2863 pp->si_code = CLD_STOPPED;
2864 pp->si_uid = r_uid;
2865 proc_unlock(pp);
2866
2867 psignal(pp, SIGCHLD);
2868 }
2869 if (pp != PROC_NULL) {
2870 proc_parentdropref(pp, 0);
2871 }
2872 }
2873 proc_lock(p);
2874 break;
2875 } else if (prop & SA_IGNORE) {
2876 /*
2877 * Except for SIGCONT, shouldn't get here.
2878 * Default action is to ignore; drop it.
2879 */
2880 break; /* ignore signal */
2881 } else {
2882 goto deliver_sig;
2883 }
2884
2885 case (long)SIG_IGN:
2886 /*
2887 * Masking above should prevent us ever trying
2888 * to take action on an ignored signal other
2889 * than SIGCONT, unless process is traced.
2890 */
2891 if ((prop & SA_CONT) == 0 &&
2892 (p->p_lflag & P_LTRACED) == 0) {
2893 printf("issignal\n");
2894 }
2895 break; /* ignore signal */
2896
2897 default:
2898 /* This signal has an action - deliver it. */
2899 goto deliver_sig;
2900 }
2901
2902 /* If we dropped through, the signal was ignored - remove it from pending list. */
2903 ut->uu_siglist &= ~mask;
2904 } /* for(;;) */
2905
2906 /* NOTREACHED */
2907
2908 deliver_sig:
2909 ut->uu_siglist &= ~mask;
2910 retval = signum;
2911
2912 out:
2913 proc_signalend(p, 1);
2914 return retval;
2915 }
2916
2917 /* called from _sleep */
2918 int
CURSIG(proc_t p)2919 CURSIG(proc_t p)
2920 {
2921 int signum, mask, prop, sigbits;
2922 thread_t cur_act;
2923 struct uthread * ut;
2924 int retnum = 0;
2925
2926
2927 cur_act = current_thread();
2928
2929 ut = get_bsdthread_info(cur_act);
2930
2931 if (ut->uu_siglist == 0) {
2932 return 0;
2933 }
2934
2935 if (((ut->uu_siglist & ~ut->uu_sigmask) == 0) && ((p->p_lflag & P_LTRACED) == 0)) {
2936 return 0;
2937 }
2938
2939 sigbits = ut->uu_siglist & ~ut->uu_sigmask;
2940
2941 for (;;) {
2942 if (p->p_lflag & P_LPPWAIT) {
2943 sigbits &= ~stopsigmask;
2944 }
2945 if (sigbits == 0) { /* no signal to send */
2946 return retnum;
2947 }
2948
2949 signum = ffs((unsigned int)sigbits);
2950 mask = sigmask(signum);
2951 prop = sigprop[signum];
2952 sigbits &= ~mask; /* take the signal out */
2953
2954 /*
2955 * We should see pending but ignored signals
2956 * only if P_LTRACED was on when they were posted.
2957 */
2958 if (mask & p->p_sigignore && (p->p_lflag & P_LTRACED) == 0) {
2959 continue;
2960 }
2961
2962 if (p->p_lflag & P_LTRACED && (p->p_lflag & P_LPPWAIT) == 0) {
2963 return signum;
2964 }
2965
2966 /*
2967 * Decide whether the signal should be returned.
2968 * Return the signal's number, or fall through
2969 * to clear it from the pending mask.
2970 */
2971
2972 switch ((long)SIGACTION(p, signum)) {
2973 case (long)SIG_DFL:
2974 /*
2975 * If there is a pending stop signal to process
2976 * with default action, stop here,
2977 * then clear the signal. However,
2978 * if process is member of an orphaned
2979 * process group, ignore tty stop signals.
2980 */
2981 if (prop & SA_STOP) {
2982 struct pgrp *pg;
2983
2984 pg = proc_pgrp(p, NULL);
2985
2986 if (p->p_lflag & P_LTRACED ||
2987 (pg->pg_jobc == 0 &&
2988 prop & SA_TTYSTOP)) {
2989 pgrp_rele(pg);
2990 break; /* == ignore */
2991 }
2992 pgrp_rele(pg);
2993 retnum = signum;
2994 break;
2995 } else if (prop & SA_IGNORE) {
2996 /*
2997 * Except for SIGCONT, shouldn't get here.
2998 * Default action is to ignore; drop it.
2999 */
3000 break; /* == ignore */
3001 } else {
3002 return signum;
3003 }
3004 /*NOTREACHED*/
3005
3006 case (long)SIG_IGN:
3007 /*
3008 * Masking above should prevent us ever trying
3009 * to take action on an ignored signal other
3010 * than SIGCONT, unless process is traced.
3011 */
3012 if ((prop & SA_CONT) == 0 &&
3013 (p->p_lflag & P_LTRACED) == 0) {
3014 printf("issignal\n");
3015 }
3016 break; /* == ignore */
3017
3018 default:
3019 /*
3020 * This signal has an action, let
3021 * postsig() process it.
3022 */
3023 return signum;
3024 }
3025 }
3026 /* NOTREACHED */
3027 }
3028
3029 /*
3030 * Put the argument process into the stopped state and notify the parent
3031 * via wakeup. Signals are handled elsewhere. The process must not be
3032 * on the run queue.
3033 */
3034 static void
stop(proc_t p,proc_t parent)3035 stop(proc_t p, proc_t parent)
3036 {
3037 OSBitAndAtomic(~((uint32_t)P_CONTINUED), &p->p_flag);
3038 if ((parent != PROC_NULL) && (parent->p_stat != SSTOP)) {
3039 proc_list_lock();
3040 wakeup((caddr_t)parent);
3041 proc_list_unlock();
3042 }
3043 (void) task_suspend_internal(proc_task(p));
3044 }
3045
3046 /*
3047 * Take the action for the specified signal
3048 * from the current set of pending signals.
3049 */
3050 void
postsig_locked(int signum)3051 postsig_locked(int signum)
3052 {
3053 proc_t p = current_proc();
3054 struct sigacts *ps = &p->p_sigacts;
3055 user_addr_t catcher;
3056 uint32_t code;
3057 int mask, returnmask;
3058 struct uthread * ut;
3059 os_reason_t ut_exit_reason = OS_REASON_NULL;
3060 int coredump_flags = 0;
3061
3062 #if DIAGNOSTIC
3063 if (signum == 0) {
3064 panic("postsig");
3065 }
3066 #endif
3067
3068 /*
3069 * Try to grab the signal lock.
3070 */
3071 if (sig_try_locked(p) <= 0) {
3072 return;
3073 }
3074
3075 proc_signalstart(p, 1);
3076
3077 ut = current_uthread();
3078 mask = sigmask(signum);
3079 ut->uu_siglist &= ~mask;
3080 catcher = SIGACTION(p, signum);
3081 if (catcher == SIG_DFL) {
3082 /*
3083 * Default catcher, where the default is to kill
3084 * the process. (Other cases were ignored above.)
3085 */
3086
3087 /*
3088 * exit_with_reason() below will consume a reference to the thread's exit reason, so we take another
3089 * reference so the thread still has one even after we call exit_with_reason(). The thread's reference will
3090 * ultimately be destroyed in uthread_cleanup().
3091 */
3092 ut_exit_reason = ut->uu_exit_reason;
3093 os_reason_ref(ut_exit_reason);
3094
3095 p->p_acflag |= AXSIG;
3096 if (sigprop[signum] & SA_CORE) {
3097 p->p_sigacts.ps_sig = signum;
3098 proc_signalend(p, 1);
3099 proc_unlock(p);
3100 if (task_is_driver(proc_task(p))) {
3101 coredump_flags |= COREDUMP_FULLFSYNC;
3102 }
3103 #if CONFIG_COREDUMP
3104 if (coredump(p, 0, coredump_flags) == 0) {
3105 signum |= WCOREFLAG;
3106 }
3107 #endif
3108 } else {
3109 proc_signalend(p, 1);
3110 proc_unlock(p);
3111 }
3112
3113 #if CONFIG_DTRACE
3114 bzero((caddr_t)&(ut->t_dtrace_siginfo), sizeof(ut->t_dtrace_siginfo));
3115
3116 ut->t_dtrace_siginfo.si_signo = signum;
3117 ut->t_dtrace_siginfo.si_pid = p->si_pid;
3118 ut->t_dtrace_siginfo.si_uid = p->si_uid;
3119 ut->t_dtrace_siginfo.si_status = WEXITSTATUS(p->si_status);
3120
3121 /* Fire DTrace proc:::fault probe when signal is generated by hardware. */
3122 switch (signum) {
3123 case SIGILL: case SIGBUS: case SIGSEGV: case SIGFPE: case SIGTRAP:
3124 DTRACE_PROC2(fault, int, (int)(ut->uu_code), siginfo_t *, &(ut->t_dtrace_siginfo));
3125 break;
3126 default:
3127 break;
3128 }
3129
3130
3131 DTRACE_PROC3(signal__handle, int, signum, siginfo_t *, &(ut->t_dtrace_siginfo),
3132 void (*)(void), SIG_DFL);
3133 #endif
3134
3135 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_FRCEXIT) | DBG_FUNC_NONE,
3136 proc_getpid(p), W_EXITCODE(0, signum), 3, 0, 0);
3137
3138 exit_with_reason(p, W_EXITCODE(0, signum), (int *)NULL, TRUE, TRUE, 0, ut_exit_reason);
3139
3140 proc_lock(p);
3141 return;
3142 } else {
3143 /*
3144 * If we get here, the signal must be caught.
3145 */
3146 #if DIAGNOSTIC
3147 if (catcher == SIG_IGN || (ut->uu_sigmask & mask)) {
3148 log(LOG_WARNING,
3149 "postsig: processing masked or ignored signal\n");
3150 }
3151 #endif
3152
3153 /*
3154 * Set the new mask value and also defer further
3155 * occurences of this signal.
3156 *
3157 * Special case: user has done a sigpause. Here the
3158 * current mask is not of interest, but rather the
3159 * mask from before the sigpause is what we want
3160 * restored after the signal processing is completed.
3161 */
3162 if (ut->uu_flag & UT_SAS_OLDMASK) {
3163 returnmask = ut->uu_oldmask;
3164 ut->uu_flag &= ~UT_SAS_OLDMASK;
3165 ut->uu_oldmask = 0;
3166 } else {
3167 returnmask = ut->uu_sigmask;
3168 }
3169 ut->uu_sigmask |= ps->ps_catchmask[signum];
3170 if ((ps->ps_signodefer & mask) == 0) {
3171 ut->uu_sigmask |= mask;
3172 }
3173 sigset_t siginfo = ps->ps_siginfo;
3174 if ((signum != SIGILL) && (signum != SIGTRAP) && (ps->ps_sigreset & mask)) {
3175 if ((signum != SIGCONT) && (sigprop[signum] & SA_IGNORE)) {
3176 p->p_sigignore |= mask;
3177 }
3178 if (SIGACTION(p, signum) != SIG_DFL) {
3179 proc_set_sigact(p, signum, SIG_DFL);
3180 }
3181 ps->ps_siginfo &= ~mask;
3182 ps->ps_signodefer &= ~mask;
3183 }
3184
3185 if (ps->ps_sig != signum) {
3186 code = 0;
3187 } else {
3188 code = ps->ps_code;
3189 ps->ps_code = 0;
3190 }
3191 OSIncrementAtomicLong(&p->p_stats->p_ru.ru_nsignals);
3192 sendsig(p, catcher, signum, returnmask, code, siginfo);
3193 }
3194 proc_signalend(p, 1);
3195 }
3196
3197 /*
3198 * Attach a signal knote to the list of knotes for this process.
3199 *
3200 * Signal knotes share the knote list with proc knotes. This
3201 * could be avoided by using a signal-specific knote list, but
3202 * probably isn't worth the trouble.
3203 */
3204
3205 static int
filt_sigattach(struct knote * kn,__unused struct kevent_qos_s * kev)3206 filt_sigattach(struct knote *kn, __unused struct kevent_qos_s *kev)
3207 {
3208 proc_t p = current_proc(); /* can attach only to oneself */
3209
3210 proc_klist_lock();
3211
3212 kn->kn_proc = p;
3213 kn->kn_flags |= EV_CLEAR; /* automatically set */
3214 kn->kn_sdata = 0; /* incoming data is ignored */
3215
3216 KNOTE_ATTACH(&p->p_klist, kn);
3217
3218 proc_klist_unlock();
3219
3220 /* edge-triggered events can't have fired before we attached */
3221 return 0;
3222 }
3223
3224 /*
3225 * remove the knote from the process list, if it hasn't already
3226 * been removed by exit processing.
3227 */
3228
3229 static void
filt_sigdetach(struct knote * kn)3230 filt_sigdetach(struct knote *kn)
3231 {
3232 proc_t p;
3233
3234 proc_klist_lock();
3235 p = kn->kn_proc;
3236 if (p != NULL) {
3237 kn->kn_proc = NULL;
3238 KNOTE_DETACH(&p->p_klist, kn);
3239 }
3240 proc_klist_unlock();
3241 }
3242
3243 /*
3244 * Post an event to the signal filter. Because we share the same list
3245 * as process knotes, we have to filter out and handle only signal events.
3246 *
3247 * We assume that we process fdt_invalidate() before we post the NOTE_EXIT for
3248 * a process during exit. Therefore, since signal filters can only be
3249 * set up "in-process", we should have already torn down the kqueue
3250 * hosting the EVFILT_SIGNAL knote and should never see NOTE_EXIT.
3251 */
3252 static int
filt_signal(struct knote * kn,long hint)3253 filt_signal(struct knote *kn, long hint)
3254 {
3255 if (hint & NOTE_SIGNAL) {
3256 hint &= ~NOTE_SIGNAL;
3257
3258 if (kn->kn_id == (unsigned int)hint) {
3259 kn->kn_hook32++;
3260 }
3261 } else if (hint & NOTE_EXIT) {
3262 panic("filt_signal: detected NOTE_EXIT event");
3263 }
3264
3265 return kn->kn_hook32 != 0;
3266 }
3267
3268 static int
filt_signaltouch(struct knote * kn,struct kevent_qos_s * kev)3269 filt_signaltouch(struct knote *kn, struct kevent_qos_s *kev)
3270 {
3271 #pragma unused(kev)
3272
3273 int res;
3274
3275 proc_klist_lock();
3276
3277 /*
3278 * No data to save - just capture if it is already fired
3279 */
3280 res = (kn->kn_hook32 > 0);
3281
3282 proc_klist_unlock();
3283
3284 return res;
3285 }
3286
3287 static int
filt_signalprocess(struct knote * kn,struct kevent_qos_s * kev)3288 filt_signalprocess(struct knote *kn, struct kevent_qos_s *kev)
3289 {
3290 int res = 0;
3291
3292 /*
3293 * Snapshot the event data.
3294 */
3295
3296 proc_klist_lock();
3297 if (kn->kn_hook32) {
3298 knote_fill_kevent(kn, kev, kn->kn_hook32);
3299 kn->kn_hook32 = 0;
3300 res = 1;
3301 }
3302 proc_klist_unlock();
3303 return res;
3304 }
3305
3306 void
bsd_ast(thread_t thread)3307 bsd_ast(thread_t thread)
3308 {
3309 proc_t p = current_proc();
3310 struct uthread *ut = get_bsdthread_info(thread);
3311 int signum;
3312 static int bsd_init_done = 0;
3313
3314 if (p == NULL) {
3315 return;
3316 }
3317
3318 if (timerisset(&p->p_vtimer_user.it_value)) {
3319 uint32_t microsecs;
3320
3321 task_vtimer_update(proc_task(p), TASK_VTIMER_USER, µsecs);
3322
3323 if (!itimerdecr(p, &p->p_vtimer_user, microsecs)) {
3324 if (timerisset(&p->p_vtimer_user.it_value)) {
3325 task_vtimer_set(proc_task(p), TASK_VTIMER_USER);
3326 } else {
3327 task_vtimer_clear(proc_task(p), TASK_VTIMER_USER);
3328 }
3329
3330 psignal_try_thread(p, thread, SIGVTALRM);
3331 }
3332 }
3333
3334 if (timerisset(&p->p_vtimer_prof.it_value)) {
3335 uint32_t microsecs;
3336
3337 task_vtimer_update(proc_task(p), TASK_VTIMER_PROF, µsecs);
3338
3339 if (!itimerdecr(p, &p->p_vtimer_prof, microsecs)) {
3340 if (timerisset(&p->p_vtimer_prof.it_value)) {
3341 task_vtimer_set(proc_task(p), TASK_VTIMER_PROF);
3342 } else {
3343 task_vtimer_clear(proc_task(p), TASK_VTIMER_PROF);
3344 }
3345
3346 psignal_try_thread(p, thread, SIGPROF);
3347 }
3348 }
3349
3350 if (timerisset(&p->p_rlim_cpu)) {
3351 struct timeval tv;
3352
3353 task_vtimer_update(proc_task(p), TASK_VTIMER_RLIM, (uint32_t *) &tv.tv_usec);
3354
3355 proc_spinlock(p);
3356 if (p->p_rlim_cpu.tv_sec > 0 || p->p_rlim_cpu.tv_usec > tv.tv_usec) {
3357 tv.tv_sec = 0;
3358 timersub(&p->p_rlim_cpu, &tv, &p->p_rlim_cpu);
3359 proc_spinunlock(p);
3360 } else {
3361 timerclear(&p->p_rlim_cpu);
3362 proc_spinunlock(p);
3363
3364 task_vtimer_clear(proc_task(p), TASK_VTIMER_RLIM);
3365
3366 psignal_try_thread(p, thread, SIGXCPU);
3367 }
3368 }
3369
3370 #if CONFIG_DTRACE
3371 if (ut->t_dtrace_sig) {
3372 uint8_t dt_action_sig = ut->t_dtrace_sig;
3373 ut->t_dtrace_sig = 0;
3374 psignal(p, dt_action_sig);
3375 }
3376
3377 if (ut->t_dtrace_stop) {
3378 ut->t_dtrace_stop = 0;
3379 proc_lock(p);
3380 p->p_dtrace_stop = 1;
3381 proc_unlock(p);
3382 (void)task_suspend_internal(proc_task(p));
3383 }
3384
3385 if (ut->t_dtrace_resumepid) {
3386 proc_t resumeproc = proc_find((int)ut->t_dtrace_resumepid);
3387 ut->t_dtrace_resumepid = 0;
3388 if (resumeproc != PROC_NULL) {
3389 proc_lock(resumeproc);
3390 /* We only act on processes stopped by dtrace */
3391 if (resumeproc->p_dtrace_stop) {
3392 resumeproc->p_dtrace_stop = 0;
3393 proc_unlock(resumeproc);
3394 task_resume_internal(proc_task(resumeproc));
3395 } else {
3396 proc_unlock(resumeproc);
3397 }
3398 proc_rele(resumeproc);
3399 }
3400 }
3401
3402 #endif /* CONFIG_DTRACE */
3403
3404 proc_lock(p);
3405 if (CHECK_SIGNALS(p, current_thread(), ut)) {
3406 while ((signum = issignal_locked(p))) {
3407 postsig_locked(signum);
3408 }
3409 }
3410 proc_unlock(p);
3411
3412 if (!bsd_init_done) {
3413 bsd_init_done = 1;
3414 bsdinit_task();
3415 }
3416 }
3417
3418 /* ptrace set runnable */
3419 void
pt_setrunnable(proc_t p)3420 pt_setrunnable(proc_t p)
3421 {
3422 task_t task;
3423
3424 task = proc_task(p);
3425
3426 if (p->p_lflag & P_LTRACED) {
3427 proc_lock(p);
3428 p->p_stat = SRUN;
3429 proc_unlock(p);
3430 if (p->sigwait) {
3431 wakeup((caddr_t)&(p->sigwait));
3432 if ((p->p_lflag & P_LSIGEXC) == 0) { // 5878479
3433 task_release(task);
3434 }
3435 }
3436 }
3437 }
3438
3439 kern_return_t
do_bsdexception(int exc,int code,int sub)3440 do_bsdexception(
3441 int exc,
3442 int code,
3443 int sub)
3444 {
3445 mach_exception_data_type_t codes[EXCEPTION_CODE_MAX];
3446
3447 codes[0] = code;
3448 codes[1] = sub;
3449 return bsd_exception(exc, codes, 2);
3450 }
3451
3452 int
proc_pendingsignals(proc_t p,sigset_t mask)3453 proc_pendingsignals(proc_t p, sigset_t mask)
3454 {
3455 struct uthread * uth;
3456 sigset_t bits = 0;
3457
3458 proc_lock(p);
3459 /* If the process is in proc exit return no signal info */
3460 if (p->p_lflag & P_LPEXIT) {
3461 goto out;
3462 }
3463
3464
3465 bits = 0;
3466 TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) {
3467 bits |= (((uth->uu_siglist & ~uth->uu_sigmask) & ~p->p_sigignore) & mask);
3468 }
3469 out:
3470 proc_unlock(p);
3471 return bits;
3472 }
3473
3474 int
thread_issignal(proc_t p,thread_t th,sigset_t mask)3475 thread_issignal(proc_t p, thread_t th, sigset_t mask)
3476 {
3477 struct uthread * uth;
3478 sigset_t bits = 0;
3479
3480 proc_lock(p);
3481 uth = (struct uthread *)get_bsdthread_info(th);
3482 if (uth) {
3483 bits = (((uth->uu_siglist & ~uth->uu_sigmask) & ~p->p_sigignore) & mask);
3484 }
3485 proc_unlock(p);
3486 return bits;
3487 }
3488
3489 /*
3490 * Allow external reads of the sigprop array.
3491 */
3492 int
hassigprop(int sig,int prop)3493 hassigprop(int sig, int prop)
3494 {
3495 return sigprop[sig] & prop;
3496 }
3497
3498 void
pgsigio(pid_t pgid,int sig)3499 pgsigio(pid_t pgid, int sig)
3500 {
3501 proc_t p = PROC_NULL;
3502
3503 if (pgid < 0) {
3504 gsignal(-(pgid), sig);
3505 } else if (pgid > 0 && (p = proc_find(pgid)) != 0) {
3506 psignal(p, sig);
3507 }
3508 if (p != PROC_NULL) {
3509 proc_rele(p);
3510 }
3511 }
3512
3513 void
proc_signalstart(proc_t p,int locked)3514 proc_signalstart(proc_t p, int locked)
3515 {
3516 if (!locked) {
3517 proc_lock(p);
3518 }
3519
3520 if (p->p_signalholder == current_thread()) {
3521 panic("proc_signalstart: thread attempting to signal a process for which it holds the signal lock");
3522 }
3523
3524 p->p_sigwaitcnt++;
3525 while ((p->p_lflag & P_LINSIGNAL) == P_LINSIGNAL) {
3526 msleep(&p->p_sigmask, &p->p_mlock, 0, "proc_signstart", NULL);
3527 }
3528 p->p_sigwaitcnt--;
3529
3530 p->p_lflag |= P_LINSIGNAL;
3531 p->p_signalholder = current_thread();
3532 if (!locked) {
3533 proc_unlock(p);
3534 }
3535 }
3536
3537 void
proc_signalend(proc_t p,int locked)3538 proc_signalend(proc_t p, int locked)
3539 {
3540 if (!locked) {
3541 proc_lock(p);
3542 }
3543 p->p_lflag &= ~P_LINSIGNAL;
3544
3545 if (p->p_sigwaitcnt > 0) {
3546 wakeup(&p->p_sigmask);
3547 }
3548
3549 p->p_signalholder = NULL;
3550 if (!locked) {
3551 proc_unlock(p);
3552 }
3553 }
3554
3555 void
sig_lock_to_exit(proc_t p)3556 sig_lock_to_exit(proc_t p)
3557 {
3558 thread_t self = current_thread();
3559
3560 p->exit_thread = self;
3561 proc_unlock(p);
3562
3563 task_hold_and_wait(proc_task(p), true);
3564
3565 proc_lock(p);
3566 }
3567
3568 int
sig_try_locked(proc_t p)3569 sig_try_locked(proc_t p)
3570 {
3571 thread_t self = current_thread();
3572
3573 while (p->sigwait || p->exit_thread) {
3574 if (p->exit_thread) {
3575 return 0;
3576 }
3577 msleep((caddr_t)&p->sigwait_thread, &p->p_mlock, PCATCH | PDROP, 0, 0);
3578 if (thread_should_abort(self)) {
3579 /*
3580 * Terminate request - clean up.
3581 */
3582 proc_lock(p);
3583 return -1;
3584 }
3585 proc_lock(p);
3586 }
3587 return 1;
3588 }
3589