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