This repository has been archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
kern_sig.c
3771 lines (3274 loc) · 95.2 KB
/
kern_sig.c
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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1995-2016 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
/*
* Copyright (c) 1982, 1986, 1989, 1991, 1993
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
*/
/*
* NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
* support for mandatory and extensible security protections. This notice
* is included in support of clause 2.2 (b) of the Apple Public License,
* Version 2.0.
*/
#define SIGPROP /* include signal properties table */
#include <sys/param.h>
#include <sys/resourcevar.h>
#include <sys/proc_internal.h>
#include <sys/kauth.h>
#include <sys/systm.h>
#include <sys/timeb.h>
#include <sys/times.h>
#include <sys/acct.h>
#include <sys/file_internal.h>
#include <sys/kernel.h>
#include <sys/wait.h>
#include <sys/signalvar.h>
#include <sys/syslog.h>
#include <sys/stat.h>
#include <sys/lock.h>
#include <sys/kdebug.h>
#include <sys/reason.h>
#include <sys/mount.h>
#include <sys/sysproto.h>
#include <security/audit/audit.h>
#include <kern/cpu_number.h>
#include <sys/vm.h>
#include <sys/user.h> /* for coredump */
#include <kern/ast.h> /* for APC support */
#include <kern/kalloc.h>
#include <kern/task.h> /* extern void *get_bsdtask_info(task_t); */
#include <kern/thread.h>
#include <kern/sched_prim.h>
#include <kern/thread_call.h>
#include <kern/policy_internal.h>
#include <mach/exception.h>
#include <mach/task.h>
#include <mach/thread_act.h>
#include <libkern/OSAtomic.h>
#include <sys/sdt.h>
#include <sys/codesign.h>
#include <sys/random.h>
#include <libkern/section_keywords.h>
#if CONFIG_MACF
#include <security/mac_framework.h>
#endif
/*
* Missing prototypes that Mach should export
*
* +++
*/
extern int thread_enable_fpe(thread_t act, int onoff);
extern kern_return_t get_signalact(task_t, thread_t *, int);
extern unsigned int get_useraddr(void);
extern boolean_t task_did_exec(task_t task);
extern boolean_t task_is_exec_copy(task_t task);
extern void vm_shared_region_reslide_stale(void);
/*
* ---
*/
extern void doexception(int exc, mach_exception_code_t code,
mach_exception_subcode_t sub);
static void stop(proc_t, proc_t);
static int cansignal_nomac(proc_t, kauth_cred_t, proc_t, int);
int cansignal(proc_t, kauth_cred_t, proc_t, int);
int killpg1(proc_t, int, int, int, int);
kern_return_t do_bsdexception(int, int, int);
void __posix_sem_syscall_return(kern_return_t);
char *proc_name_address(void *p);
/* implementations in osfmk/kern/sync_sema.c. We do not want port.h in this scope, so void * them */
kern_return_t semaphore_timedwait_signal_trap_internal(mach_port_name_t, mach_port_name_t, unsigned int, clock_res_t, void (*)(kern_return_t));
kern_return_t semaphore_timedwait_trap_internal(mach_port_name_t, unsigned int, clock_res_t, void (*)(kern_return_t));
kern_return_t semaphore_wait_signal_trap_internal(mach_port_name_t, mach_port_name_t, void (*)(kern_return_t));
kern_return_t semaphore_wait_trap_internal(mach_port_name_t, void (*)(kern_return_t));
static int filt_sigattach(struct knote *kn, struct kevent_qos_s *kev);
static void filt_sigdetach(struct knote *kn);
static int filt_signal(struct knote *kn, long hint);
static int filt_signaltouch(struct knote *kn, struct kevent_qos_s *kev);
static int filt_signalprocess(struct knote *kn, struct kevent_qos_s *kev);
SECURITY_READ_ONLY_EARLY(struct filterops) sig_filtops = {
.f_attach = filt_sigattach,
.f_detach = filt_sigdetach,
.f_event = filt_signal,
.f_touch = filt_signaltouch,
.f_process = filt_signalprocess,
};
/* structures and fns for killpg1 iterartion callback and filters */
struct killpg1_filtargs {
bool posix;
proc_t curproc;
};
struct killpg1_iterargs {
proc_t curproc;
kauth_cred_t uc;
int signum;
int nfound;
};
static int killpg1_allfilt(proc_t p, void * arg);
static int killpg1_pgrpfilt(proc_t p, __unused void * arg);
static int killpg1_callback(proc_t p, void * arg);
static int pgsignal_filt(proc_t p, void * arg);
static int pgsignal_callback(proc_t p, void * arg);
static kern_return_t get_signalthread(proc_t, int, thread_t *);
/* flags for psignal_internal */
#define PSIG_LOCKED 0x1
#define PSIG_VFORK 0x2
#define PSIG_THREAD 0x4
#define PSIG_TRY_THREAD 0x8
static os_reason_t build_signal_reason(int signum, const char *procname);
static void psignal_internal(proc_t p, task_t task, thread_t thread, int flavor, int signum, os_reason_t signal_reason);
/*
* NOTE: Source and target may *NOT* overlap! (target is smaller)
*/
static void
sigaltstack_kern_to_user32(struct kern_sigaltstack *in, struct user32_sigaltstack *out)
{
out->ss_sp = CAST_DOWN_EXPLICIT(user32_addr_t, in->ss_sp);
out->ss_size = CAST_DOWN_EXPLICIT(user32_size_t, in->ss_size);
out->ss_flags = in->ss_flags;
}
static void
sigaltstack_kern_to_user64(struct kern_sigaltstack *in, struct user64_sigaltstack *out)
{
out->ss_sp = in->ss_sp;
out->ss_size = in->ss_size;
out->ss_flags = in->ss_flags;
}
/*
* NOTE: Source and target may are permitted to overlap! (source is smaller);
* this works because we copy fields in order from the end of the struct to
* the beginning.
*/
static void
sigaltstack_user32_to_kern(struct user32_sigaltstack *in, struct kern_sigaltstack *out)
{
out->ss_flags = in->ss_flags;
out->ss_size = in->ss_size;
out->ss_sp = CAST_USER_ADDR_T(in->ss_sp);
}
static void
sigaltstack_user64_to_kern(struct user64_sigaltstack *in, struct kern_sigaltstack *out)
{
out->ss_flags = in->ss_flags;
out->ss_size = (user_size_t)in->ss_size;
out->ss_sp = (user_addr_t)in->ss_sp;
}
static void
sigaction_kern_to_user32(struct kern_sigaction *in, struct user32_sigaction *out)
{
/* This assumes 32 bit __sa_handler is of type sig_t */
out->__sigaction_u.__sa_handler = CAST_DOWN_EXPLICIT(user32_addr_t, in->__sigaction_u.__sa_handler);
out->sa_mask = in->sa_mask;
out->sa_flags = in->sa_flags;
}
static void
sigaction_kern_to_user64(struct kern_sigaction *in, struct user64_sigaction *out)
{
/* This assumes 32 bit __sa_handler is of type sig_t */
out->__sigaction_u.__sa_handler = in->__sigaction_u.__sa_handler;
out->sa_mask = in->sa_mask;
out->sa_flags = in->sa_flags;
}
static void
__sigaction_user32_to_kern(struct __user32_sigaction *in, struct __kern_sigaction *out)
{
out->__sigaction_u.__sa_handler = CAST_USER_ADDR_T(in->__sigaction_u.__sa_handler);
out->sa_tramp = CAST_USER_ADDR_T(in->sa_tramp);
out->sa_mask = in->sa_mask;
out->sa_flags = in->sa_flags;
kern_return_t kr;
kr = machine_thread_function_pointers_convert_from_user(current_thread(),
&out->sa_tramp, 1);
assert(kr == KERN_SUCCESS);
}
static void
__sigaction_user64_to_kern(struct __user64_sigaction *in, struct __kern_sigaction *out)
{
out->__sigaction_u.__sa_handler = (user_addr_t)in->__sigaction_u.__sa_handler;
out->sa_tramp = (user_addr_t)in->sa_tramp;
out->sa_mask = in->sa_mask;
out->sa_flags = in->sa_flags;
kern_return_t kr;
kr = machine_thread_function_pointers_convert_from_user(current_thread(),
&out->sa_tramp, 1);
assert(kr == KERN_SUCCESS);
}
#if SIGNAL_DEBUG
void ram_printf(int);
int ram_debug = 0;
unsigned int rdebug_proc = 0;
void
ram_printf(int x)
{
printf("x is %d", x);
}
#endif /* SIGNAL_DEBUG */
void
signal_setast(thread_t sig_actthread)
{
act_set_astbsd(sig_actthread);
}
static int
cansignal_nomac(proc_t src, kauth_cred_t uc_src, proc_t dst, int signum)
{
/* you can signal yourself */
if (src == dst) {
return 1;
}
/* you can't send the init proc SIGKILL, even if root */
if (signum == SIGKILL && dst == initproc) {
return 0;
}
/* otherwise, root can always signal */
if (kauth_cred_issuser(uc_src)) {
return 1;
}
/* processes in the same session can send SIGCONT to each other */
{
struct session *sess_src = SESSION_NULL;
struct session *sess_dst = SESSION_NULL;
/* The session field is protected by the list lock. */
proc_list_lock();
if (src->p_pgrp != PGRP_NULL) {
sess_src = src->p_pgrp->pg_session;
}
if (dst->p_pgrp != PGRP_NULL) {
sess_dst = dst->p_pgrp->pg_session;
}
proc_list_unlock();
/* allow SIGCONT within session and for processes without session */
if (signum == SIGCONT && sess_src == sess_dst) {
return 1;
}
}
/* the source process must be authorized to signal the target */
{
int allowed = 0;
kauth_cred_t uc_dst = NOCRED, uc_ref = NOCRED;
uc_dst = uc_ref = kauth_cred_proc_ref(dst);
/*
* If the real or effective UID of the sender matches the real or saved
* UID of the target, allow the signal to be sent.
*/
if (kauth_cred_getruid(uc_src) == kauth_cred_getruid(uc_dst) ||
kauth_cred_getruid(uc_src) == kauth_cred_getsvuid(uc_dst) ||
kauth_cred_getuid(uc_src) == kauth_cred_getruid(uc_dst) ||
kauth_cred_getuid(uc_src) == kauth_cred_getsvuid(uc_dst)) {
allowed = 1;
}
if (uc_ref != NOCRED) {
kauth_cred_unref(&uc_ref);
uc_ref = NOCRED;
}
return allowed;
}
}
/*
* Can process `src`, with ucred `uc_src`, send the signal `signum` to process
* `dst`? The ucred is referenced by the caller so internal fileds can be used
* safely.
*/
int
cansignal(proc_t src, kauth_cred_t uc_src, proc_t dst, int signum)
{
#if CONFIG_MACF
if (mac_proc_check_signal(src, dst, signum)) {
return 0;
}
#endif
return cansignal_nomac(src, uc_src, dst, signum);
}
/*
* <rdar://problem/21952708> Some signals can be restricted from being handled,
* forcing the default action for that signal. This behavior applies only to
* non-root (EUID != 0) processes, and is configured with the "sigrestrict=x"
* bootarg:
*
* 0 (default): Disallow use of restricted signals. Trying to register a handler
* returns ENOTSUP, which userspace may use to take special action (e.g. abort).
* 1: As above, but return EINVAL. Restricted signals behave similarly to SIGKILL.
* 2: Usual POSIX semantics.
*/
unsigned sigrestrict_arg = 0;
#if PLATFORM_WatchOS
static int
sigrestrictmask(void)
{
if (kauth_getuid() != 0 && sigrestrict_arg != 2) {
return SIGRESTRICTMASK;
}
return 0;
}
static int
signal_is_restricted(proc_t p, int signum)
{
if (sigmask(signum) & sigrestrictmask()) {
if (sigrestrict_arg == 0 &&
task_get_apptype(p->task) == TASK_APPTYPE_APP_DEFAULT) {
return ENOTSUP;
} else {
return EINVAL;
}
}
return 0;
}
#else
static inline int
signal_is_restricted(proc_t p, int signum)
{
(void)p;
(void)signum;
return 0;
}
#endif /* !PLATFORM_WatchOS */
/*
* Returns: 0 Success
* EINVAL
* copyout:EFAULT
* copyin:EFAULT
*
* Notes: Uses current thread as a parameter to inform PPC to enable
* FPU exceptions via setsigvec(); this operation is not proxy
* safe!
*/
/* ARGSUSED */
int
sigaction(proc_t p, struct sigaction_args *uap, __unused int32_t *retval)
{
struct kern_sigaction vec;
struct __kern_sigaction __vec;
struct kern_sigaction *sa = &vec;
struct sigacts *ps = p->p_sigacts;
int signum;
int bit, error = 0;
uint32_t sigreturn_validation = PS_SIGRETURN_VALIDATION_DEFAULT;
signum = uap->signum;
if (signum <= 0 || signum >= NSIG ||
signum == SIGKILL || signum == SIGSTOP) {
return EINVAL;
}
if (uap->nsa) {
if (IS_64BIT_PROCESS(p)) {
struct __user64_sigaction __vec64;
error = copyin(uap->nsa, &__vec64, sizeof(__vec64));
__sigaction_user64_to_kern(&__vec64, &__vec);
} else {
struct __user32_sigaction __vec32;
error = copyin(uap->nsa, &__vec32, sizeof(__vec32));
__sigaction_user32_to_kern(&__vec32, &__vec);
}
if (error) {
return error;
}
sigreturn_validation = (__vec.sa_flags & SA_VALIDATE_SIGRETURN_FROM_SIGTRAMP) ?
PS_SIGRETURN_VALIDATION_ENABLED : PS_SIGRETURN_VALIDATION_DISABLED;
__vec.sa_flags &= SA_USERSPACE_MASK; /* Only pass on valid sa_flags */
if ((__vec.sa_flags & SA_SIGINFO) || __vec.sa_handler != SIG_DFL) {
if ((error = signal_is_restricted(p, signum))) {
if (error == ENOTSUP) {
printf("%s(%d): denied attempt to register action for signal %d\n",
proc_name_address(p), proc_pid(p), signum);
}
return error;
}
}
}
if (uap->osa) {
sa->sa_handler = ps->ps_sigact[signum];
sa->sa_mask = ps->ps_catchmask[signum];
bit = sigmask(signum);
sa->sa_flags = 0;
if ((ps->ps_sigonstack & bit) != 0) {
sa->sa_flags |= SA_ONSTACK;
}
if ((ps->ps_sigintr & bit) == 0) {
sa->sa_flags |= SA_RESTART;
}
if (ps->ps_siginfo & bit) {
sa->sa_flags |= SA_SIGINFO;
}
if (ps->ps_signodefer & bit) {
sa->sa_flags |= SA_NODEFER;
}
if ((signum == SIGCHLD) && (p->p_flag & P_NOCLDSTOP)) {
sa->sa_flags |= SA_NOCLDSTOP;
}
if ((signum == SIGCHLD) && (p->p_flag & P_NOCLDWAIT)) {
sa->sa_flags |= SA_NOCLDWAIT;
}
if (IS_64BIT_PROCESS(p)) {
struct user64_sigaction vec64 = {};
sigaction_kern_to_user64(sa, &vec64);
error = copyout(&vec64, uap->osa, sizeof(vec64));
} else {
struct user32_sigaction vec32 = {};
sigaction_kern_to_user32(sa, &vec32);
error = copyout(&vec32, uap->osa, sizeof(vec32));
}
if (error) {
return error;
}
}
if (uap->nsa) {
uint32_t old_sigreturn_validation = atomic_load_explicit(
&ps->ps_sigreturn_validation, memory_order_relaxed);
if (old_sigreturn_validation == PS_SIGRETURN_VALIDATION_DEFAULT) {
atomic_compare_exchange_strong_explicit(&ps->ps_sigreturn_validation,
&old_sigreturn_validation, sigreturn_validation,
memory_order_relaxed, memory_order_relaxed);
}
error = setsigvec(p, current_thread(), signum, &__vec, FALSE);
}
return error;
}
/* Routines to manipulate bits on all threads */
int
clear_procsiglist(proc_t p, int bit, boolean_t in_signalstart)
{
struct uthread * uth;
thread_t thact;
proc_lock(p);
if (!in_signalstart) {
proc_signalstart(p, 1);
}
if ((p->p_lflag & P_LINVFORK) && p->p_vforkact) {
thact = p->p_vforkact;
uth = (struct uthread *)get_bsdthread_info(thact);
if (uth) {
uth->uu_siglist &= ~bit;
}
if (!in_signalstart) {
proc_signalend(p, 1);
}
proc_unlock(p);
return 0;
}
TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) {
uth->uu_siglist &= ~bit;
}
p->p_siglist &= ~bit;
if (!in_signalstart) {
proc_signalend(p, 1);
}
proc_unlock(p);
return 0;
}
static int
unblock_procsigmask(proc_t p, int bit)
{
struct uthread * uth;
thread_t thact;
proc_lock(p);
proc_signalstart(p, 1);
if ((p->p_lflag & P_LINVFORK) && p->p_vforkact) {
thact = p->p_vforkact;
uth = (struct uthread *)get_bsdthread_info(thact);
if (uth) {
uth->uu_sigmask &= ~bit;
}
p->p_sigmask &= ~bit;
proc_signalend(p, 1);
proc_unlock(p);
return 0;
}
TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) {
uth->uu_sigmask &= ~bit;
}
p->p_sigmask &= ~bit;
proc_signalend(p, 1);
proc_unlock(p);
return 0;
}
static int
block_procsigmask(proc_t p, int bit)
{
struct uthread * uth;
thread_t thact;
proc_lock(p);
proc_signalstart(p, 1);
if ((p->p_lflag & P_LINVFORK) && p->p_vforkact) {
thact = p->p_vforkact;
uth = (struct uthread *)get_bsdthread_info(thact);
if (uth) {
uth->uu_sigmask |= bit;
}
p->p_sigmask |= bit;
proc_signalend(p, 1);
proc_unlock(p);
return 0;
}
TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) {
uth->uu_sigmask |= bit;
}
p->p_sigmask |= bit;
proc_signalend(p, 1);
proc_unlock(p);
return 0;
}
int
set_procsigmask(proc_t p, int bit)
{
struct uthread * uth;
thread_t thact;
proc_lock(p);
proc_signalstart(p, 1);
if ((p->p_lflag & P_LINVFORK) && p->p_vforkact) {
thact = p->p_vforkact;
uth = (struct uthread *)get_bsdthread_info(thact);
if (uth) {
uth->uu_sigmask = bit;
}
p->p_sigmask = bit;
proc_signalend(p, 1);
proc_unlock(p);
return 0;
}
TAILQ_FOREACH(uth, &p->p_uthlist, uu_list) {
uth->uu_sigmask = bit;
}
p->p_sigmask = bit;
proc_signalend(p, 1);
proc_unlock(p);
return 0;
}
/* XXX should be static? */
/*
* Notes: The thread parameter is used in the PPC case to select the
* thread on which the floating point exception will be enabled
* or disabled. We can't simply take current_thread(), since
* this is called from posix_spawn() on the not currently running
* process/thread pair.
*
* We mark thread as unused to alow compilation without warning
* on non-PPC platforms.
*/
int
setsigvec(proc_t p, __unused thread_t thread, int signum, struct __kern_sigaction *sa, boolean_t in_sigstart)
{
struct sigacts *ps = p->p_sigacts;
int bit;
assert(signum < NSIG);
if ((signum == SIGKILL || signum == SIGSTOP) &&
sa->sa_handler != SIG_DFL) {
return EINVAL;
}
bit = sigmask(signum);
/*
* Change setting atomically.
*/
ps->ps_sigact[signum] = sa->sa_handler;
ps->ps_trampact[signum] = sa->sa_tramp;
ps->ps_catchmask[signum] = sa->sa_mask & ~sigcantmask;
if (sa->sa_flags & SA_SIGINFO) {
ps->ps_siginfo |= bit;
} else {
ps->ps_siginfo &= ~bit;
}
if ((sa->sa_flags & SA_RESTART) == 0) {
ps->ps_sigintr |= bit;
} else {
ps->ps_sigintr &= ~bit;
}
if (sa->sa_flags & SA_ONSTACK) {
ps->ps_sigonstack |= bit;
} else {
ps->ps_sigonstack &= ~bit;
}
if (sa->sa_flags & SA_RESETHAND) {
ps->ps_sigreset |= bit;
} else {
ps->ps_sigreset &= ~bit;
}
if (sa->sa_flags & SA_NODEFER) {
ps->ps_signodefer |= bit;
} else {
ps->ps_signodefer &= ~bit;
}
if (signum == SIGCHLD) {
if (sa->sa_flags & SA_NOCLDSTOP) {
OSBitOrAtomic(P_NOCLDSTOP, &p->p_flag);
} else {
OSBitAndAtomic(~((uint32_t)P_NOCLDSTOP), &p->p_flag);
}
if ((sa->sa_flags & SA_NOCLDWAIT) || (sa->sa_handler == SIG_IGN)) {
OSBitOrAtomic(P_NOCLDWAIT, &p->p_flag);
} else {
OSBitAndAtomic(~((uint32_t)P_NOCLDWAIT), &p->p_flag);
}
}
/*
* Set bit in p_sigignore for signals that are set to SIG_IGN,
* and for signals set to SIG_DFL where the default is to ignore.
* However, don't put SIGCONT in p_sigignore,
* as we have to restart the process.
*/
if (sa->sa_handler == SIG_IGN ||
(sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
clear_procsiglist(p, bit, in_sigstart);
if (signum != SIGCONT) {
p->p_sigignore |= bit; /* easier in psignal */
}
p->p_sigcatch &= ~bit;
} else {
p->p_sigignore &= ~bit;
if (sa->sa_handler == SIG_DFL) {
p->p_sigcatch &= ~bit;
} else {
p->p_sigcatch |= bit;
}
}
return 0;
}
/*
* Initialize signal state for process 0;
* set to ignore signals that are ignored by default.
*/
void
siginit(proc_t p)
{
int i;
for (i = 1; i < NSIG; i++) {
if (sigprop[i] & SA_IGNORE && i != SIGCONT) {
p->p_sigignore |= sigmask(i);
}
}
}
/*
* Reset signals for an exec of the specified process.
*/
void
execsigs(proc_t p, thread_t thread)
{
struct sigacts *ps = p->p_sigacts;
int nc, mask;
struct uthread *ut;
ut = (struct uthread *)get_bsdthread_info(thread);
/*
* transfer saved signal states from the process
* back to the current thread.
*
* NOTE: We do this without the process locked,
* because we are guaranteed to be single-threaded
* by this point in exec and the p_siglist is
* only accessed by threads inside the process.
*/
ut->uu_siglist |= p->p_siglist;
p->p_siglist = 0;
/*
* Reset caught signals. Held signals remain held
* through p_sigmask (unless they were caught,
* and are now ignored by default).
*/
while (p->p_sigcatch) {
nc = ffs((unsigned int)p->p_sigcatch);
mask = sigmask(nc);
p->p_sigcatch &= ~mask;
if (sigprop[nc] & SA_IGNORE) {
if (nc != SIGCONT) {
p->p_sigignore |= mask;
}
ut->uu_siglist &= ~mask;
}
ps->ps_sigact[nc] = SIG_DFL;
}
atomic_store_explicit(&ps->ps_sigreturn_validation,
PS_SIGRETURN_VALIDATION_DEFAULT, memory_order_relaxed);
/* Generate random token value used to validate sigreturn arguments */
read_random(&ps->ps_sigreturn_token, sizeof(ps->ps_sigreturn_token));
/*
* Reset stack state to the user stack.
* Clear set of signals caught on the signal stack.
*/
/* thread */
ut->uu_sigstk.ss_flags = SA_DISABLE;
ut->uu_sigstk.ss_size = 0;
ut->uu_sigstk.ss_sp = USER_ADDR_NULL;
ut->uu_flag &= ~UT_ALTSTACK;
/* process */
ps->ps_sigonstack = 0;
}
/*
* Manipulate signal mask.
* Note that we receive new mask, not pointer,
* and return old mask as return value;
* the library stub does the rest.
*/
int
sigprocmask(proc_t p, struct sigprocmask_args *uap, __unused int32_t *retval)
{
int error = 0;
sigset_t oldmask, nmask;
user_addr_t omask = uap->omask;
struct uthread *ut;
ut = (struct uthread *)get_bsdthread_info(current_thread());
oldmask = ut->uu_sigmask;
if (uap->mask == USER_ADDR_NULL) {
/* just want old mask */
goto out;
}
error = copyin(uap->mask, &nmask, sizeof(sigset_t));
if (error) {
goto out;
}
switch (uap->how) {
case SIG_BLOCK:
block_procsigmask(p, (nmask & ~sigcantmask));
signal_setast(current_thread());
break;
case SIG_UNBLOCK:
unblock_procsigmask(p, (nmask & ~sigcantmask));
signal_setast(current_thread());
break;
case SIG_SETMASK:
set_procsigmask(p, (nmask & ~sigcantmask));
signal_setast(current_thread());
break;
default:
error = EINVAL;
break;
}
out:
if (!error && omask != USER_ADDR_NULL) {
copyout(&oldmask, omask, sizeof(sigset_t));
}
return error;
}
int
sigpending(__unused proc_t p, struct sigpending_args *uap, __unused int32_t *retval)
{
struct uthread *ut;
sigset_t pendlist;
ut = (struct uthread *)get_bsdthread_info(current_thread());
pendlist = ut->uu_siglist;
if (uap->osv) {
copyout(&pendlist, uap->osv, sizeof(sigset_t));
}
return 0;
}
/*
* Suspend process until signal, providing mask to be set
* in the meantime. Note nonstandard calling convention:
* libc stub passes mask, not pointer, to save a copyin.
*/
static int
sigcontinue(__unused int error)
{
// struct uthread *ut = get_bsdthread_info(current_thread());
unix_syscall_return(EINTR);
}
int
sigsuspend(proc_t p, struct sigsuspend_args *uap, int32_t *retval)
{
__pthread_testcancel(1);
return sigsuspend_nocancel(p, (struct sigsuspend_nocancel_args *)uap, retval);
}
int
sigsuspend_nocancel(proc_t p, struct sigsuspend_nocancel_args *uap, __unused int32_t *retval)
{
struct uthread *ut;
ut = (struct uthread *)get_bsdthread_info(current_thread());
/*
* When returning from sigpause, we want
* the old mask to be restored after the
* signal handler has finished. Thus, we
* save it here and mark the sigacts structure
* to indicate this.
*/
ut->uu_oldmask = ut->uu_sigmask;
ut->uu_flag |= UT_SAS_OLDMASK;
ut->uu_sigmask = (uap->mask & ~sigcantmask);
(void) tsleep0((caddr_t) p, PPAUSE | PCATCH, "pause", 0, sigcontinue);
/* always return EINTR rather than ERESTART... */
return EINTR;
}
int
__disable_threadsignal(__unused proc_t p,
__unused struct __disable_threadsignal_args *uap,
__unused int32_t *retval)
{
struct uthread *uth;
uth = (struct uthread *)get_bsdthread_info(current_thread());
/* No longer valid to have any signal delivered */
uth->uu_flag |= (UT_NO_SIGMASK | UT_CANCELDISABLE);
return 0;
}
void
__pthread_testcancel(int presyscall)
{
thread_t self = current_thread();
struct uthread * uthread;
uthread = (struct uthread *)get_bsdthread_info(self);
uthread->uu_flag &= ~UT_NOTCANCELPT;
if ((uthread->uu_flag & (UT_CANCELDISABLE | UT_CANCEL | UT_CANCELED)) == UT_CANCEL) {
if (presyscall != 0) {
unix_syscall_return(EINTR);
/* NOTREACHED */
} else {
thread_abort_safely(self);
}
}
}
int
__pthread_markcancel(__unused proc_t p,
struct __pthread_markcancel_args *uap, __unused int32_t *retval)
{
thread_act_t target_act;
int error = 0;
struct uthread *uth;
target_act = (thread_act_t)port_name_to_thread(uap->thread_port,
PORT_TO_THREAD_IN_CURRENT_TASK);