Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 | /* * Copyright (c) 2000-2007 Apple Inc. All rights reserved. */ #include <mach/mach_types.h> #include <mach/exception_types.h> #include <sys/param.h> #include <sys/proc_internal.h> #include <sys/user.h> #include <sys/signal.h> #include <sys/ucontext.h> #include <sys/sysproto.h> #include <sys/systm.h> #include <sys/ux_exception.h> #include <arm/signal.h> #include <sys/signalvar.h> #include <sys/kdebug.h> #include <sys/sdt.h> #include <sys/wait.h> #include <kern/thread.h> #include <mach/arm/thread_status.h> #include <arm64/proc_reg.h> #include <sys/reason.h> #include <kern/assert.h> #include <kern/ast.h> #include <pexpert/pexpert.h> #include <sys/random.h> extern struct arm_saved_state *get_user_regs(thread_t); extern user_addr_t thread_get_cthread_self(void); extern kern_return_t thread_getstatus(thread_t act, int flavor, thread_state_t tstate, mach_msg_type_number_t *count); extern kern_return_t thread_getstatus_to_user(thread_t act, int flavor, thread_state_t tstate, mach_msg_type_number_t *count, thread_set_status_flags_t); extern kern_return_t machine_thread_state_convert_to_user(thread_t act, int flavor, thread_state_t tstate, mach_msg_type_number_t *count, thread_set_status_flags_t); extern kern_return_t thread_setstatus(thread_t thread, int flavor, thread_state_t tstate, mach_msg_type_number_t count); extern kern_return_t thread_setstatus_from_user(thread_t thread, int flavor, thread_state_t tstate, mach_msg_type_number_t count, thread_state_t old_tstate, mach_msg_type_number_t old_count, thread_set_status_flags_t flags, audit_token_t *audit); extern task_t current_task(void); extern bool task_needs_user_signed_thread_state(task_t); /* XXX Put these someplace smarter... */ typedef struct mcontext32 mcontext32_t; typedef struct mcontext64 mcontext64_t; /* Signal handler flavors supported */ /* These defns should match the libplatform implmn */ #define UC_TRAD 1 #define UC_FLAVOR 30 #define UC_SET_ALT_STACK 0x40000000 #define UC_RESET_ALT_STACK 0x80000000 /* The following are valid mcontext sizes */ #define UC_FLAVOR_SIZE32 ((ARM_THREAD_STATE_COUNT + ARM_EXCEPTION_STATE_COUNT + ARM_VFP_STATE_COUNT) * sizeof(int)) #define UC_FLAVOR_SIZE64 ((ARM_THREAD_STATE64_COUNT + ARM_EXCEPTION_STATE64_COUNT + ARM_NEON_STATE64_COUNT) * sizeof(int)) #if __arm64__ #define C_64_REDZONE_LEN 128 #endif #define TRUNC_TO_16_BYTES(addr) (addr & ~0xf) static int sendsig_get_state32(thread_t th_act, arm_thread_state_t *ts, mcontext32_t *mcp) { void *tstate; mach_msg_type_number_t state_count; assert(!proc_is64bit_data(current_proc())); tstate = (void *) ts; state_count = ARM_THREAD_STATE_COUNT; if (thread_getstatus(th_act, ARM_THREAD_STATE, (thread_state_t) tstate, &state_count) != KERN_SUCCESS) { return EINVAL; } mcp->ss = *ts; tstate = (void *) &mcp->ss; state_count = ARM_THREAD_STATE_COUNT; if (machine_thread_state_convert_to_user(th_act, ARM_THREAD_STATE, (thread_state_t) tstate, &state_count, TSSF_FLAGS_NONE) != KERN_SUCCESS) { return EINVAL; } tstate = (void *) &mcp->es; state_count = ARM_EXCEPTION_STATE_COUNT; if (thread_getstatus(th_act, ARM_EXCEPTION_STATE, (thread_state_t) tstate, &state_count) != KERN_SUCCESS) { return EINVAL; } tstate = (void *) &mcp->fs; state_count = ARM_VFP_STATE_COUNT; if (thread_getstatus_to_user(th_act, ARM_VFP_STATE, (thread_state_t) tstate, &state_count, TSSF_FLAGS_NONE) != KERN_SUCCESS) { return EINVAL; } return 0; } static TUNABLE(bool, pac_sigreturn_token, "pac_sigreturn_token", true); #if defined(__arm64__) struct user_sigframe64 { /* We can pass the last two args in registers for ARM64 */ user64_siginfo_t sinfo; struct user_ucontext64 uctx; mcontext64_t mctx; }; static int sendsig_get_state64(thread_t th_act, arm_thread_state64_t *ts, mcontext64_t *mcp) { void *tstate; mach_msg_type_number_t state_count; assert(proc_is64bit_data(current_proc())); tstate = (void *) ts; state_count = ARM_THREAD_STATE64_COUNT; if (thread_getstatus(th_act, ARM_THREAD_STATE64, (thread_state_t) tstate, &state_count) != KERN_SUCCESS) { return EINVAL; } mcp->ss = *ts; tstate = (void *) &mcp->ss; state_count = ARM_THREAD_STATE64_COUNT; thread_set_status_flags_t flags = TSSF_STASH_SIGRETURN_TOKEN; if (pac_sigreturn_token || task_needs_user_signed_thread_state(current_task())) { flags |= TSSF_THREAD_USER_DIV; } if (machine_thread_state_convert_to_user(th_act, ARM_THREAD_STATE64, (thread_state_t) tstate, &state_count, flags) != KERN_SUCCESS) { return EINVAL; } tstate = (void *) &mcp->es; state_count = ARM_EXCEPTION_STATE64_COUNT; if (thread_getstatus(th_act, ARM_EXCEPTION_STATE64, (thread_state_t) tstate, &state_count) != KERN_SUCCESS) { return EINVAL; } tstate = (void *) &mcp->ns; state_count = ARM_NEON_STATE64_COUNT; if (thread_getstatus_to_user(th_act, ARM_NEON_STATE64, (thread_state_t) tstate, &state_count, TSSF_FLAGS_NONE) != KERN_SUCCESS) { return EINVAL; } return 0; } static void sendsig_fill_uctx64(user_ucontext64_t *uctx, int oonstack, int mask, user64_addr_t sp, user64_size_t stack_size, user64_addr_t p_mctx) { bzero(uctx, sizeof(*uctx)); uctx->uc_onstack = oonstack; uctx->uc_sigmask = mask; uctx->uc_stack.ss_sp = sp; uctx->uc_stack.ss_size = stack_size; if (oonstack) { uctx->uc_stack.ss_flags |= SS_ONSTACK; } uctx->uc_link = (user64_addr_t)0; uctx->uc_mcsize = (user64_size_t) UC_FLAVOR_SIZE64; uctx->uc_mcontext64 = (user64_addr_t) p_mctx; } static kern_return_t sendsig_set_thread_state64(arm_thread_state64_t *regs, user64_addr_t catcher, int infostyle, int sig, user64_addr_t p_sinfo, user64_addr_t p_uctx, user64_addr_t token, user64_addr_t trampact, user64_addr_t sp, thread_t th_act) { assert(proc_is64bit_data(current_proc())); regs->x[0] = catcher; regs->x[1] = infostyle; regs->x[2] = sig; regs->x[3] = p_sinfo; regs->x[4] = p_uctx; regs->x[5] = token; regs->pc = trampact; regs->cpsr = PSR64_USER64_DEFAULT; regs->sp = sp; return thread_setstatus(th_act, ARM_THREAD_STATE64, (void *)regs, ARM_THREAD_STATE64_COUNT); } #endif /* defined(__arm64__) */ static void sendsig_fill_uctx32(user_ucontext32_t *uctx, int oonstack, int mask, user_addr_t sp, user_size_t stack_size, user_addr_t p_mctx) { bzero(uctx, sizeof(*uctx)); uctx->uc_onstack = oonstack; uctx->uc_sigmask = mask; uctx->uc_stack.ss_sp = (user32_addr_t) sp; uctx->uc_stack.ss_size = (user32_size_t) stack_size; if (oonstack) { uctx->uc_stack.ss_flags |= SS_ONSTACK; } uctx->uc_link = (user32_addr_t)0; uctx->uc_mcsize = (user32_size_t) UC_FLAVOR_SIZE32; uctx->uc_mcontext = (user32_addr_t) p_mctx; } static kern_return_t sendsig_set_thread_state32(arm_thread_state_t *regs, user32_addr_t catcher, int infostyle, int sig, user32_addr_t p_sinfo, user32_addr_t trampact, user32_addr_t sp, thread_t th_act) { assert(!proc_is64bit_data(current_proc())); regs->r[0] = catcher; regs->r[1] = infostyle; regs->r[2] = sig; regs->r[3] = p_sinfo; if (trampact & 1) { regs->pc = trampact & ~1; #if defined(__arm64__) regs->cpsr = PSR64_USER32_DEFAULT | PSR64_MODE_USER32_THUMB; #else #error Unknown architecture. #endif } else { regs->pc = trampact; regs->cpsr = PSR_USERDFLT; } regs->sp = sp; return thread_setstatus(th_act, ARM_THREAD_STATE, (void *)regs, ARM_THREAD_STATE_COUNT); } #if CONFIG_DTRACE static void sendsig_do_dtrace(uthread_t ut, user_siginfo_t *sinfo, int sig, user_addr_t catcher) { bzero((caddr_t)&(ut->t_dtrace_siginfo), sizeof(ut->t_dtrace_siginfo)); ut->t_dtrace_siginfo.si_signo = sinfo->si_signo; ut->t_dtrace_siginfo.si_code = sinfo->si_code; ut->t_dtrace_siginfo.si_pid = sinfo->si_pid; ut->t_dtrace_siginfo.si_uid = sinfo->si_uid; ut->t_dtrace_siginfo.si_status = sinfo->si_status; /* XXX truncates faulting address to void * */ ut->t_dtrace_siginfo.si_addr = CAST_DOWN_EXPLICIT(void *, sinfo->si_addr); /* Fire DTrace proc:::fault probe when signal is generated by hardware. */ switch (sig) { case SIGILL: case SIGBUS: case SIGSEGV: case SIGFPE: case SIGTRAP: DTRACE_PROC2(fault, int, (int)(ut->uu_code), siginfo_t *, &(ut->t_dtrace_siginfo)); break; default: break; } /* XXX truncates faulting address to uintptr_t */ DTRACE_PROC3(signal__handle, int, sig, siginfo_t *, &(ut->t_dtrace_siginfo), void (*)(void), CAST_DOWN(uintptr_t, catcher)); } #endif struct user_sigframe32 { user32_addr_t puctx; user32_addr_t token; user32_siginfo_t sinfo; struct user_ucontext32 uctx; mcontext32_t mctx; }; /* * Send an interrupt to process. * */ void sendsig( struct proc * p, user_addr_t catcher, int sig, int mask, __unused uint32_t code, sigset_t siginfo ) { union { struct ts32 { arm_thread_state_t ss; } ts32; #if defined(__arm64__) struct ts64 { arm_thread_state64_t ss; } ts64; #endif } ts; union { struct user_sigframe32 uf32; #if defined(__arm64__) struct user_sigframe64 uf64; #endif } user_frame; user_siginfo_t sinfo; user_addr_t sp = 0, trampact; struct sigacts *ps = &p->p_sigacts; int oonstack, infostyle; thread_t th_act; struct uthread *ut; user_size_t stack_size = 0; user_addr_t p_uctx, token_uctx; kern_return_t kr; th_act = current_thread(); ut = get_bsdthread_info(th_act); assert(p == current_proc()); bzero(&ts, sizeof(ts)); bzero(&user_frame, sizeof(user_frame)); if (siginfo & sigmask(sig)) { infostyle = UC_FLAVOR; } else { infostyle = UC_TRAD; } trampact = SIGTRAMP(p, sig); oonstack = ut->uu_sigstk.ss_flags & SA_ONSTACK; if (ut->uu_pending_sigreturn == 0) { /* Generate random token value used to validate sigreturn arguments */ read_random(&ut->uu_sigreturn_token, sizeof(ut->uu_sigreturn_token)); do { read_random(&ut->uu_sigreturn_diversifier, sizeof(ut->uu_sigreturn_diversifier)); ut->uu_sigreturn_diversifier &= __DARWIN_ARM_THREAD_STATE64_USER_DIVERSIFIER_MASK; } while (ut->uu_sigreturn_diversifier == 0); } ut->uu_pending_sigreturn++; /* * Get sundry thread state. */ if (proc_is64bit_data(p)) { #ifdef __arm64__ int ret = 0; if ((ret = sendsig_get_state64(th_act, &ts.ts64.ss, &user_frame.uf64.mctx)) != 0) { #if DEVELOPMENT || DEBUG printf("process [%s][%d] sendsig_get_state64 failed with ret %d, expected 0", p->p_comm, proc_getpid(p), ret); #endif goto bad2; } #else #error Unsupported architecture #endif } else { int ret = 0; if ((ret = sendsig_get_state32(th_act, &ts.ts32.ss, &user_frame.uf32.mctx)) != 0) { #if DEVELOPMENT || DEBUG printf("process [%s][%d] sendsig_get_state32 failed with ret %d, expected 0", p->p_comm, proc_getpid(p), ret); #endif goto bad2; } } /* * Figure out where our new stack lives. */ if ((ut->uu_flag & UT_ALTSTACK) && !oonstack && (ps->ps_sigonstack & sigmask(sig))) { sp = ut->uu_sigstk.ss_sp; stack_size = ut->uu_sigstk.ss_size; sp += stack_size; ut->uu_sigstk.ss_flags |= SA_ONSTACK; } else { /* * Get stack pointer, and allocate enough space * for signal handler data. */ if (proc_is64bit_data(p)) { #if defined(__arm64__) sp = CAST_USER_ADDR_T(ts.ts64.ss.sp); #else #error Unsupported architecture #endif } else { sp = CAST_USER_ADDR_T(ts.ts32.ss.sp); } } /* Make sure to move stack pointer down for room for metadata */ if (proc_is64bit_data(p)) { #if defined(__arm64__) sp = (sp - sizeof(user_frame.uf64) - C_64_REDZONE_LEN); sp = TRUNC_TO_16_BYTES(sp); #else #error Unsupported architecture #endif } else { sp -= sizeof(user_frame.uf32); } proc_unlock(p); /* * Fill in ucontext (points to mcontext, i.e. thread states). */ if (proc_is64bit_data(p)) { #if defined(__arm64__) sendsig_fill_uctx64(&user_frame.uf64.uctx, oonstack, mask, sp, (user64_size_t)stack_size, (user64_addr_t)&((struct user_sigframe64*)sp)->mctx); #else #error Unsupported architecture #endif } else { sendsig_fill_uctx32(&user_frame.uf32.uctx, oonstack, mask, sp, (user32_size_t)stack_size, (user32_addr_t)&((struct user_sigframe32*)sp)->mctx); } /* * Setup siginfo. */ bzero((caddr_t) &sinfo, sizeof(sinfo)); sinfo.si_signo = sig; if (proc_is64bit_data(p)) { #if defined(__arm64__) sinfo.si_addr = ts.ts64.ss.pc; sinfo.pad[0] = ts.ts64.ss.sp; #else #error Unsupported architecture #endif } else { sinfo.si_addr = ts.ts32.ss.pc; sinfo.pad[0] = ts.ts32.ss.sp; } switch (sig) { case SIGILL: #ifdef BER_XXX if (mctx.ss.srr1 & (1 << (31 - SRR1_PRG_ILL_INS_BIT))) { sinfo.si_code = ILL_ILLOPC; } else if (mctx.ss.srr1 & (1 << (31 - SRR1_PRG_PRV_INS_BIT))) { sinfo.si_code = ILL_PRVOPC; } else if (mctx.ss.srr1 & (1 << (31 - SRR1_PRG_TRAP_BIT))) { sinfo.si_code = ILL_ILLTRP; } else { sinfo.si_code = ILL_NOOP; } #else sinfo.si_code = ILL_ILLTRP; #endif break; case SIGFPE: switch (ut->uu_code) { case EXC_ARM_FP_UF: sinfo.si_code = FPE_FLTUND; break; case EXC_ARM_FP_OF: sinfo.si_code = FPE_FLTOVF; break; case EXC_ARM_FP_IO: sinfo.si_code = FPE_FLTINV; break; case EXC_ARM_FP_DZ: sinfo.si_code = FPE_FLTDIV; break; case EXC_ARM_FP_ID: sinfo.si_code = FPE_FLTINV; break; case EXC_ARM_FP_IX: sinfo.si_code = FPE_FLTRES; break; default: sinfo.si_code = FPE_NOOP; break; } break; case SIGBUS: if (proc_is64bit_data(p)) { #if defined(__arm64__) sinfo.si_addr = user_frame.uf64.mctx.es.far; #else #error Unsupported architecture #endif } else { sinfo.si_addr = user_frame.uf32.mctx.es.far; } sinfo.si_code = BUS_ADRALN; break; case SIGSEGV: if (proc_is64bit_data(p)) { #if defined(__arm64__) sinfo.si_addr = user_frame.uf64.mctx.es.far; #else #error Unsupported architecture #endif } else { sinfo.si_addr = user_frame.uf32.mctx.es.far; } #ifdef BER_XXX /* First check in srr1 and then in dsisr */ if (mctx.ss.srr1 & (1 << (31 - DSISR_PROT_BIT))) { sinfo.si_code = SEGV_ACCERR; } else if (mctx.es.dsisr & (1 << (31 - DSISR_PROT_BIT))) { sinfo.si_code = SEGV_ACCERR; } else { sinfo.si_code = SEGV_MAPERR; } #else sinfo.si_code = SEGV_ACCERR; #endif break; default: { int status_and_exitcode; /* * All other signals need to fill out a minimum set of * information for the siginfo structure passed into * the signal handler, if SA_SIGINFO was specified. * * p->si_status actually contains both the status and * the exit code; we save it off in its own variable * for later breakdown. */ proc_lock(p); sinfo.si_pid = p->si_pid; p->si_pid = 0; status_and_exitcode = p->si_status; p->si_status = 0; sinfo.si_uid = p->si_uid; p->si_uid = 0; sinfo.si_code = p->si_code; p->si_code = 0; proc_unlock(p); if (sinfo.si_code == CLD_EXITED) { if (WIFEXITED(status_and_exitcode)) { sinfo.si_code = CLD_EXITED; } else if (WIFSIGNALED(status_and_exitcode)) { if (WCOREDUMP(status_and_exitcode)) { sinfo.si_code = CLD_DUMPED; status_and_exitcode = W_EXITCODE(status_and_exitcode, status_and_exitcode); } else { sinfo.si_code = CLD_KILLED; status_and_exitcode = W_EXITCODE(status_and_exitcode, status_and_exitcode); } } } /* * The recorded status contains the exit code and the * signal information, but the information to be passed * in the siginfo to the handler is supposed to only * contain the status, so we have to shift it out. */ sinfo.si_status = (WEXITSTATUS(status_and_exitcode) & 0x00FFFFFF) | (((uint32_t)(p->p_xhighbits) << 24) & 0xFF000000); p->p_xhighbits = 0; break; } } #if CONFIG_DTRACE sendsig_do_dtrace(ut, &sinfo, sig, catcher); #endif /* CONFIG_DTRACE */ /* * Copy signal-handling frame out to user space, set thread state. */ if (proc_is64bit_data(p)) { #if defined(__arm64__) user64_addr_t token; /* * mctx filled in when we get state. uctx filled in by * sendsig_fill_uctx64(). We fill in the sinfo now. */ siginfo_user_to_user64(&sinfo, &user_frame.uf64.sinfo); p_uctx = (user_addr_t)&((struct user_sigframe64*)sp)->uctx; /* * Generate the validation token for sigreturn */ token_uctx = p_uctx; kr = machine_thread_siguctx_pointer_convert_to_user(th_act, &token_uctx); assert(kr == KERN_SUCCESS); token = (user64_addr_t)token_uctx ^ (user64_addr_t)ut->uu_sigreturn_token; int ret = 0; if ((ret = copyout(&user_frame.uf64, sp, sizeof(user_frame.uf64))) != 0) { #if DEVELOPMENT || DEBUG printf("process [%s][%d] copyout of user_frame to (sp, size) = (0x%llx, %zu) failed with ret %d, expected 0\n", p->p_comm, proc_getpid(p), sp, sizeof(user_frame.uf64), ret); #endif goto bad; } if ((kr = sendsig_set_thread_state64(&ts.ts64.ss, catcher, infostyle, sig, (user64_addr_t)&((struct user_sigframe64*)sp)->sinfo, (user64_addr_t)p_uctx, token, trampact, sp, th_act)) != KERN_SUCCESS) { #if DEVELOPMENT || DEBUG printf("process [%s][%d] sendsig_set_thread_state64 failed with kr %d, expected 0", p->p_comm, proc_getpid(p), kr); #endif goto bad; } #else #error Unsupported architecture #endif } else { user32_addr_t token; /* * mctx filled in when we get state. uctx filled in by * sendsig_fill_uctx32(). We fill in the sinfo, *pointer* * to uctx and token now. */ siginfo_user_to_user32(&sinfo, &user_frame.uf32.sinfo); p_uctx = (user_addr_t)&((struct user_sigframe32*)sp)->uctx; /* * Generate the validation token for sigreturn */ token_uctx = (user_addr_t)p_uctx; kr = machine_thread_siguctx_pointer_convert_to_user(th_act, &token_uctx); assert(kr == KERN_SUCCESS); token = (user32_addr_t)token_uctx ^ (user32_addr_t)ut->uu_sigreturn_token; user_frame.uf32.puctx = (user32_addr_t)p_uctx; user_frame.uf32.token = token; if (copyout(&user_frame.uf32, sp, sizeof(user_frame.uf32)) != 0) { goto bad; } if (sendsig_set_thread_state32(&ts.ts32.ss, CAST_DOWN_EXPLICIT(user32_addr_t, catcher), infostyle, sig, (user32_addr_t)&((struct user_sigframe32*)sp)->sinfo, CAST_DOWN_EXPLICIT(user32_addr_t, trampact), CAST_DOWN_EXPLICIT(user32_addr_t, sp), th_act) != KERN_SUCCESS) { goto bad; } } proc_lock(p); return; bad: proc_lock(p); bad2: assert(ut->uu_pending_sigreturn > 0); ut->uu_pending_sigreturn--; proc_set_sigact(p, SIGILL, SIG_DFL); int sigill_mask = sigmask(SIGILL); p->p_sigignore &= ~sigill_mask; p->p_sigcatch &= ~sigill_mask; ut->uu_sigmask &= ~sigill_mask; /* sendsig is called with signal lock held */ proc_unlock(p); /* * Something went wrong when delivering the signal to the thread (e.g copyout failed). * Where possible, attribute the failure to the specific thread, otherwise we'll pick * a different one and generate a misleading crash report: * rdar://149082274 (Misattributed thread in crash logs for failed signal delivery) * * Note that such a crash report will list both SIGILL and the original signal that * we failed to deliver (as the termination reason). */ os_reason_t exit_reason = os_reason_create(OS_REASON_SIGNAL, sig); psignal_try_thread_with_reason_locked(p, current_thread(), SIGILL, exit_reason /* possibly NULL */); proc_lock(p); } /* * System call to cleanup state after a signal * has been taken. Reset signal mask and * stack state from context left by sendsig (above). * Return to previous * context left by sendsig. * Check carefully to * make sure that the user has not * modified the * spr to gain improper priviledges. */ static int sigreturn_copyin_ctx32(struct user_ucontext32 *uctx, mcontext32_t *mctx, user_addr_t uctx_addr) { int error; assert(!proc_is64bit_data(current_proc())); error = copyin(uctx_addr, uctx, sizeof(*uctx)); if (error) { return error; } /* validate the machine context size */ switch (uctx->uc_mcsize) { case UC_FLAVOR_SIZE32: break; default: return EINVAL; } assert(uctx->uc_mcsize == sizeof(*mctx)); error = copyin((user_addr_t)uctx->uc_mcontext, mctx, uctx->uc_mcsize); if (error) { return error; } return 0; } static int sigreturn_set_state32(thread_t th_act, mcontext32_t *mctx) { assert(!proc_is64bit_data(current_proc())); audit_token_t *audit = NULL; /* validate the thread state, set/reset appropriate mode bits in cpsr */ #if defined(__arm64__) mctx->ss.cpsr = (mctx->ss.cpsr & ~PSR64_MODE_MASK) | PSR64_USER32_DEFAULT; #else #error Unknown architecture. #endif if (thread_setstatus_from_user(th_act, ARM_THREAD_STATE, (void *)&mctx->ss, ARM_THREAD_STATE_COUNT, NULL, 0, TSSF_FLAGS_NONE, audit) != KERN_SUCCESS) { return EINVAL; } if (thread_setstatus_from_user(th_act, ARM_VFP_STATE, (void *)&mctx->fs, ARM_VFP_STATE_COUNT, NULL, 0, TSSF_FLAGS_NONE, audit) != KERN_SUCCESS) { return EINVAL; } return 0; } #if defined(__arm64__) static int sigreturn_copyin_ctx64(struct user_ucontext64 *uctx, mcontext64_t *mctx, user_addr_t uctx_addr) { int error; assert(proc_is64bit_data(current_proc())); error = copyin(uctx_addr, uctx, sizeof(*uctx)); if (error) { return error; } /* validate the machine context size */ switch (uctx->uc_mcsize) { case UC_FLAVOR_SIZE64: break; default: return EINVAL; } assert(uctx->uc_mcsize == sizeof(*mctx)); error = copyin((user_addr_t)uctx->uc_mcontext64, mctx, uctx->uc_mcsize); if (error) { return error; } return 0; } static int sigreturn_set_state64(thread_t th_act, mcontext64_t *mctx, thread_set_status_flags_t tssf_flags) { assert(proc_is64bit_data(current_proc())); audit_token_t *audit = NULL; /* validate the thread state, set/reset appropriate mode bits in cpsr */ mctx->ss.cpsr = (mctx->ss.cpsr & ~PSR64_MODE_MASK) | PSR64_USER64_DEFAULT; if (thread_setstatus_from_user(th_act, ARM_THREAD_STATE64, (void *)&mctx->ss, ARM_THREAD_STATE64_COUNT, NULL, 0, tssf_flags, audit) != KERN_SUCCESS) { return EINVAL; } if (thread_setstatus_from_user(th_act, ARM_NEON_STATE64, (void *)&mctx->ns, ARM_NEON_STATE64_COUNT, NULL, 0, TSSF_FLAGS_NONE, audit) != KERN_SUCCESS) { return EINVAL; } return 0; } #endif /* defined(__arm64__) */ /* ARGSUSED */ int sigreturn( struct proc * p, struct sigreturn_args * uap, __unused int *retval) { union { user_ucontext32_t uc32; #if defined(__arm64__) user_ucontext64_t uc64; #endif } uctx; union { mcontext32_t mc32; #if defined(__arm64__) mcontext64_t mc64; #endif } mctx; struct sigacts *ps = &p->p_sigacts; int error, sigmask = 0, onstack = 0; thread_t th_act; struct uthread *ut; uint32_t sigreturn_validation; user_addr_t token_uctx; kern_return_t kr; th_act = current_thread(); ut = (struct uthread *) get_bsdthread_info(th_act); /* see osfmk/kern/restartable.c */ act_set_ast_reset_pcs(TASK_NULL, th_act); /* * If we are being asked to change the altstack flag on the thread, we * just set/reset it and return (the uap->uctx is not used). */ if ((unsigned int)uap->infostyle == UC_SET_ALT_STACK) { ut->uu_sigstk.ss_flags |= SA_ONSTACK; return 0; } else if ((unsigned int)uap->infostyle == UC_RESET_ALT_STACK) { ut->uu_sigstk.ss_flags &= ~SA_ONSTACK; return 0; } if (proc_is64bit_data(p)) { #if defined(__arm64__) error = sigreturn_copyin_ctx64(&uctx.uc64, &mctx.mc64, uap->uctx); if (error != 0) { return error; } onstack = uctx.uc64.uc_onstack; sigmask = uctx.uc64.uc_sigmask; #else #error Unsupported architecture #endif } else { error = sigreturn_copyin_ctx32(&uctx.uc32, &mctx.mc32, uap->uctx); if (error != 0) { return error; } onstack = uctx.uc32.uc_onstack; sigmask = uctx.uc32.uc_sigmask; } if ((onstack & 01)) { ut->uu_sigstk.ss_flags |= SA_ONSTACK; } else { ut->uu_sigstk.ss_flags &= ~SA_ONSTACK; } ut->uu_sigmask = sigmask & ~sigcantmask; if (ut->uu_siglist & ~ut->uu_sigmask) { signal_setast(current_thread()); } sigreturn_validation = atomic_load_explicit( &ps->ps_sigreturn_validation, memory_order_relaxed); token_uctx = uap->uctx; kr = machine_thread_siguctx_pointer_convert_to_user(th_act, &token_uctx); assert(kr == KERN_SUCCESS); if (proc_is64bit_data(p)) { #if defined(__arm64__) user64_addr_t token; token = (user64_addr_t)token_uctx ^ (user64_addr_t)ut->uu_sigreturn_token; thread_set_status_flags_t tssf_flags = TSSF_FLAGS_NONE; if ((user64_addr_t)uap->token != token) { #if DEVELOPMENT || DEBUG printf("process %s[%d] sigreturn token mismatch: received 0x%llx expected 0x%llx\n", p->p_comm, proc_getpid(p), (user64_addr_t)uap->token, token); #endif /* DEVELOPMENT || DEBUG */ if (sigreturn_validation != PS_SIGRETURN_VALIDATION_DISABLED) { return EINVAL; } } if (sigreturn_validation != PS_SIGRETURN_VALIDATION_DISABLED) { tssf_flags |= TSSF_CHECK_SIGRETURN_TOKEN; if (pac_sigreturn_token || task_needs_user_signed_thread_state(current_task())) { tssf_flags |= TSSF_ALLOW_ONLY_MATCHING_TOKEN | TSSF_THREAD_USER_DIV; } } error = sigreturn_set_state64(th_act, &mctx.mc64, tssf_flags); if (error != 0) { #if DEVELOPMENT || DEBUG printf("process %s[%d] sigreturn set_state64 error %d\n", p->p_comm, proc_getpid(p), error); #endif /* DEVELOPMENT || DEBUG */ return error; } #else #error Unsupported architecture #endif } else { user32_addr_t token; token = (user32_addr_t)token_uctx ^ (user32_addr_t)ut->uu_sigreturn_token; if ((user32_addr_t)uap->token != token) { #if DEVELOPMENT || DEBUG printf("process %s[%d] sigreturn token mismatch: received 0x%x expected 0x%x\n", p->p_comm, proc_getpid(p), (user32_addr_t)uap->token, token); #endif /* DEVELOPMENT || DEBUG */ if (sigreturn_validation != PS_SIGRETURN_VALIDATION_DISABLED) { return EINVAL; } } error = sigreturn_set_state32(th_act, &mctx.mc32); if (error != 0) { #if DEVELOPMENT || DEBUG printf("process %s[%d] sigreturn sigreturn_set_state32 error %d\n", p->p_comm, proc_getpid(p), error); #endif /* DEVELOPMENT || DEBUG */ return error; } } /* Decrement the pending sigreturn count */ if (ut->uu_pending_sigreturn > 0) { ut->uu_pending_sigreturn--; } return EJUSTRETURN; } /* * machine_exception() performs machine-dependent translation * of a mach exception to a unix signal. */ int machine_exception(int exception, __unused mach_exception_code_t code, __unused mach_exception_subcode_t subcode) { switch (exception) { case EXC_BAD_INSTRUCTION: return SIGILL; case EXC_ARITHMETIC: return SIGFPE; } return 0; } |