1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include "opt_capsicum.h"
38 #include "opt_ktrace.h"
39
40 #include <sys/param.h>
41 #include <sys/capsicum.h>
42 #include <sys/ctype.h>
43 #include <sys/systm.h>
44 #include <sys/signalvar.h>
45 #include <sys/vnode.h>
46 #include <sys/acct.h>
47 #include <sys/capsicum.h>
48 #include <sys/condvar.h>
49 #include <sys/devctl.h>
50 #include <sys/event.h>
51 #include <sys/exec.h>
52 #include <sys/fcntl.h>
53 #include <sys/imgact.h>
54 #include <sys/jail.h>
55 #include <sys/kernel.h>
56 #include <sys/ktr.h>
57 #include <sys/ktrace.h>
58 #include <sys/limits.h>
59 #include <sys/lock.h>
60 #include <sys/malloc.h>
61 #include <sys/mutex.h>
62 #include <sys/refcount.h>
63 #include <sys/namei.h>
64 #include <sys/proc.h>
65 #include <sys/procdesc.h>
66 #include <sys/ptrace.h>
67 #include <sys/posix4.h>
68 #include <sys/racct.h>
69 #include <sys/resourcevar.h>
70 #include <sys/sdt.h>
71 #include <sys/sbuf.h>
72 #include <sys/sleepqueue.h>
73 #include <sys/smp.h>
74 #include <sys/stat.h>
75 #include <sys/sx.h>
76 #include <sys/syscall.h>
77 #include <sys/syscallsubr.h>
78 #include <sys/sysctl.h>
79 #include <sys/sysent.h>
80 #include <sys/syslog.h>
81 #include <sys/sysproto.h>
82 #include <sys/timers.h>
83 #include <sys/ucoredump.h>
84 #include <sys/unistd.h>
85 #include <sys/vmmeter.h>
86 #include <sys/wait.h>
87 #include <vm/vm.h>
88 #include <vm/vm_extern.h>
89 #include <vm/uma.h>
90
91 #include <machine/cpu.h>
92
93 #include <security/audit/audit.h>
94
95 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
96
97 SDT_PROVIDER_DECLARE(proc);
98 SDT_PROBE_DEFINE3(proc, , , signal__send,
99 "struct thread *", "struct proc *", "int");
100 SDT_PROBE_DEFINE2(proc, , , signal__clear,
101 "int", "ksiginfo_t *");
102 SDT_PROBE_DEFINE3(proc, , , signal__discard,
103 "struct thread *", "struct proc *", "int");
104
105 static int killpg1(struct thread *td, int sig, int pgid, int all,
106 ksiginfo_t *ksi);
107 static int issignal(struct thread *td);
108 static void reschedule_signals(struct proc *p, sigset_t block, int flags);
109 static int sigprop(int sig);
110 static void tdsigwakeup(struct thread *, int, sig_t, int);
111 static bool sig_suspend_threads(struct thread *, struct proc *);
112 static int filt_sigattach(struct knote *kn);
113 static void filt_sigdetach(struct knote *kn);
114 static int filt_signal(struct knote *kn, long hint);
115 static struct thread *sigtd(struct proc *p, int sig, bool fast_sigblock);
116 static void sigqueue_start(void *);
117 static void sigfastblock_setpend(struct thread *td, bool resched);
118 static void sig_handle_first_stop(struct thread *td, struct proc *p,
119 int sig);
120
121 static uma_zone_t ksiginfo_zone = NULL;
122 const struct filterops sig_filtops = {
123 .f_isfd = 0,
124 .f_attach = filt_sigattach,
125 .f_detach = filt_sigdetach,
126 .f_event = filt_signal,
127 .f_copy = knote_triv_copy,
128 };
129
130 static int kern_forcesigexit = 1;
131 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
132 &kern_forcesigexit, 0, "Force trap signal to be handled");
133
134 static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
135 "POSIX real time signal");
136
137 static int max_pending_per_proc = 128;
138 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
139 &max_pending_per_proc, 0, "Max pending signals per proc");
140
141 static int preallocate_siginfo = 1024;
142 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RDTUN,
143 &preallocate_siginfo, 0, "Preallocated signal memory size");
144
145 static int signal_overflow = 0;
146 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
147 &signal_overflow, 0, "Number of signals overflew");
148
149 static int signal_alloc_fail = 0;
150 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
151 &signal_alloc_fail, 0, "signals failed to be allocated");
152
153 static int kern_lognosys = 0;
154 SYSCTL_INT(_kern, OID_AUTO, lognosys, CTLFLAG_RWTUN, &kern_lognosys, 0,
155 "Log invalid syscalls");
156
157 static int kern_signosys = 1;
158 SYSCTL_INT(_kern, OID_AUTO, signosys, CTLFLAG_RWTUN, &kern_signosys, 0,
159 "Send SIGSYS on return from invalid syscall");
160
161 __read_frequently bool sigfastblock_fetch_always = false;
162 SYSCTL_BOOL(_kern, OID_AUTO, sigfastblock_fetch_always, CTLFLAG_RWTUN,
163 &sigfastblock_fetch_always, 0,
164 "Fetch sigfastblock word on each syscall entry for proper "
165 "blocking semantic");
166
167 static bool kern_sig_discard_ign = true;
168 SYSCTL_BOOL(_kern, OID_AUTO, sig_discard_ign, CTLFLAG_RWTUN,
169 &kern_sig_discard_ign, 0,
170 "Discard ignored signals on delivery, otherwise queue them to "
171 "the target queue");
172
173 bool pt_attach_transparent = true;
174 SYSCTL_BOOL(_debug, OID_AUTO, ptrace_attach_transparent, CTLFLAG_RWTUN,
175 &pt_attach_transparent, 0,
176 "Hide wakes from PT_ATTACH on interruptible sleeps");
177
178 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
179
180 /*
181 * Policy -- Can ucred cr1 send SIGIO to process cr2?
182 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
183 * in the right situations.
184 */
185 #define CANSIGIO(cr1, cr2) \
186 ((cr1)->cr_uid == 0 || \
187 (cr1)->cr_ruid == (cr2)->cr_ruid || \
188 (cr1)->cr_uid == (cr2)->cr_ruid || \
189 (cr1)->cr_ruid == (cr2)->cr_uid || \
190 (cr1)->cr_uid == (cr2)->cr_uid)
191
192 /*
193 * Signal properties and actions.
194 * The array below categorizes the signals and their default actions
195 * according to the following properties:
196 */
197 #define SIGPROP_KILL 0x01 /* terminates process by default */
198 #define SIGPROP_CORE 0x02 /* ditto and coredumps */
199 #define SIGPROP_STOP 0x04 /* suspend process */
200 #define SIGPROP_TTYSTOP 0x08 /* ditto, from tty */
201 #define SIGPROP_IGNORE 0x10 /* ignore by default */
202 #define SIGPROP_CONT 0x20 /* continue if suspended */
203
204 static const int sigproptbl[NSIG] = {
205 [SIGHUP] = SIGPROP_KILL,
206 [SIGINT] = SIGPROP_KILL,
207 [SIGQUIT] = SIGPROP_KILL | SIGPROP_CORE,
208 [SIGILL] = SIGPROP_KILL | SIGPROP_CORE,
209 [SIGTRAP] = SIGPROP_KILL | SIGPROP_CORE,
210 [SIGABRT] = SIGPROP_KILL | SIGPROP_CORE,
211 [SIGEMT] = SIGPROP_KILL | SIGPROP_CORE,
212 [SIGFPE] = SIGPROP_KILL | SIGPROP_CORE,
213 [SIGKILL] = SIGPROP_KILL,
214 [SIGBUS] = SIGPROP_KILL | SIGPROP_CORE,
215 [SIGSEGV] = SIGPROP_KILL | SIGPROP_CORE,
216 [SIGSYS] = SIGPROP_KILL | SIGPROP_CORE,
217 [SIGPIPE] = SIGPROP_KILL,
218 [SIGALRM] = SIGPROP_KILL,
219 [SIGTERM] = SIGPROP_KILL,
220 [SIGURG] = SIGPROP_IGNORE,
221 [SIGSTOP] = SIGPROP_STOP,
222 [SIGTSTP] = SIGPROP_STOP | SIGPROP_TTYSTOP,
223 [SIGCONT] = SIGPROP_IGNORE | SIGPROP_CONT,
224 [SIGCHLD] = SIGPROP_IGNORE,
225 [SIGTTIN] = SIGPROP_STOP | SIGPROP_TTYSTOP,
226 [SIGTTOU] = SIGPROP_STOP | SIGPROP_TTYSTOP,
227 [SIGIO] = SIGPROP_IGNORE,
228 [SIGXCPU] = SIGPROP_KILL,
229 [SIGXFSZ] = SIGPROP_KILL,
230 [SIGVTALRM] = SIGPROP_KILL,
231 [SIGPROF] = SIGPROP_KILL,
232 [SIGWINCH] = SIGPROP_IGNORE,
233 [SIGINFO] = SIGPROP_IGNORE,
234 [SIGUSR1] = SIGPROP_KILL,
235 [SIGUSR2] = SIGPROP_KILL,
236 };
237
238 #define _SIG_FOREACH_ADVANCE(i, set) ({ \
239 int __found; \
240 for (;;) { \
241 if (__bits != 0) { \
242 int __sig = ffs(__bits); \
243 __bits &= ~(1u << (__sig - 1)); \
244 sig = __i * sizeof((set)->__bits[0]) * NBBY + __sig; \
245 __found = 1; \
246 break; \
247 } \
248 if (++__i == _SIG_WORDS) { \
249 __found = 0; \
250 break; \
251 } \
252 __bits = (set)->__bits[__i]; \
253 } \
254 __found != 0; \
255 })
256
257 #define SIG_FOREACH(i, set) \
258 for (int32_t __i = -1, __bits = 0; \
259 _SIG_FOREACH_ADVANCE(i, set); ) \
260
261 static sigset_t fastblock_mask;
262
263 static void
ast_sig(struct thread * td,int tda)264 ast_sig(struct thread *td, int tda)
265 {
266 struct proc *p;
267 int old_boundary, sig;
268 bool resched_sigs;
269
270 p = td->td_proc;
271
272 #ifdef DIAGNOSTIC
273 if (p->p_numthreads == 1 && (tda & (TDAI(TDA_SIG) |
274 TDAI(TDA_AST))) == 0) {
275 PROC_LOCK(p);
276 thread_lock(td);
277 /*
278 * Note that TDA_SIG should be re-read from
279 * td_ast, since signal might have been delivered
280 * after we cleared td_flags above. This is one of
281 * the reason for looping check for AST condition.
282 * See comment in userret() about P_PPWAIT.
283 */
284 if ((p->p_flag & P_PPWAIT) == 0 &&
285 (td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
286 if (SIGPENDING(td) && ((tda | td->td_ast) &
287 (TDAI(TDA_SIG) | TDAI(TDA_AST))) == 0) {
288 thread_unlock(td); /* fix dumps */
289 panic(
290 "failed2 to set signal flags for ast p %p "
291 "td %p tda %#x td_ast %#x fl %#x",
292 p, td, tda, td->td_ast, td->td_flags);
293 }
294 }
295 thread_unlock(td);
296 PROC_UNLOCK(p);
297 }
298 #endif
299
300 /*
301 * Check for signals. Unlocked reads of p_pendingcnt or
302 * p_siglist might cause process-directed signal to be handled
303 * later.
304 */
305 if ((tda & TDAI(TDA_SIG)) != 0 || p->p_pendingcnt > 0 ||
306 !SIGISEMPTY(p->p_siglist)) {
307 sigfastblock_fetch(td);
308 PROC_LOCK(p);
309 old_boundary = ~TDB_BOUNDARY | (td->td_dbgflags & TDB_BOUNDARY);
310 td->td_dbgflags |= TDB_BOUNDARY;
311 mtx_lock(&p->p_sigacts->ps_mtx);
312 while ((sig = cursig(td)) != 0) {
313 KASSERT(sig >= 0, ("sig %d", sig));
314 postsig(sig);
315 }
316 mtx_unlock(&p->p_sigacts->ps_mtx);
317 td->td_dbgflags &= old_boundary;
318 PROC_UNLOCK(p);
319 resched_sigs = true;
320 } else {
321 resched_sigs = false;
322 }
323
324 /*
325 * Handle deferred update of the fast sigblock value, after
326 * the postsig() loop was performed.
327 */
328 sigfastblock_setpend(td, resched_sigs);
329
330 /*
331 * Clear td_sa.code: signal to ptrace that syscall arguments
332 * are unavailable after this point. This AST handler is the
333 * last chance for ptracestop() to signal the tracer before
334 * the tracee returns to userspace.
335 */
336 td->td_sa.code = 0;
337 }
338
339 static void
ast_sigsuspend(struct thread * td,int tda __unused)340 ast_sigsuspend(struct thread *td, int tda __unused)
341 {
342 MPASS((td->td_pflags & TDP_OLDMASK) != 0);
343 td->td_pflags &= ~TDP_OLDMASK;
344 kern_sigprocmask(td, SIG_SETMASK, &td->td_oldsigmask, NULL, 0);
345 }
346
347 static void
sigqueue_start(void * dummy __unused)348 sigqueue_start(void *dummy __unused)
349 {
350 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
351 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
352 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
353 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
354 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
355 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
356 SIGFILLSET(fastblock_mask);
357 SIG_CANTMASK(fastblock_mask);
358 ast_register(TDA_SIG, ASTR_UNCOND, 0, ast_sig);
359
360 /*
361 * TDA_PSELECT is for the case where the signal mask should be restored
362 * before delivering any signals so that we do not deliver any that are
363 * blocked by the normal thread mask. It is mutually exclusive with
364 * TDA_SIGSUSPEND, which should be used if we *do* want to deliver
365 * signals that are normally blocked, e.g., if it interrupted our sleep.
366 */
367 ast_register(TDA_PSELECT, ASTR_ASTF_REQUIRED | ASTR_TDP,
368 TDP_OLDMASK, ast_sigsuspend);
369 ast_register(TDA_SIGSUSPEND, ASTR_ASTF_REQUIRED | ASTR_TDP,
370 TDP_OLDMASK, ast_sigsuspend);
371 }
372
373 ksiginfo_t *
ksiginfo_alloc(int mwait)374 ksiginfo_alloc(int mwait)
375 {
376 MPASS(mwait == M_WAITOK || mwait == M_NOWAIT);
377
378 if (ksiginfo_zone == NULL)
379 return (NULL);
380 return (uma_zalloc(ksiginfo_zone, mwait | M_ZERO));
381 }
382
383 void
ksiginfo_free(ksiginfo_t * ksi)384 ksiginfo_free(ksiginfo_t *ksi)
385 {
386 uma_zfree(ksiginfo_zone, ksi);
387 }
388
389 static __inline bool
ksiginfo_tryfree(ksiginfo_t * ksi)390 ksiginfo_tryfree(ksiginfo_t *ksi)
391 {
392 if ((ksi->ksi_flags & KSI_EXT) == 0) {
393 uma_zfree(ksiginfo_zone, ksi);
394 return (true);
395 }
396 return (false);
397 }
398
399 void
sigqueue_init(sigqueue_t * list,struct proc * p)400 sigqueue_init(sigqueue_t *list, struct proc *p)
401 {
402 SIGEMPTYSET(list->sq_signals);
403 SIGEMPTYSET(list->sq_kill);
404 SIGEMPTYSET(list->sq_ptrace);
405 TAILQ_INIT(&list->sq_list);
406 list->sq_proc = p;
407 list->sq_flags = SQ_INIT;
408 }
409
410 /*
411 * Get a signal's ksiginfo.
412 * Return:
413 * 0 - signal not found
414 * others - signal number
415 */
416 static int
sigqueue_get(sigqueue_t * sq,int signo,ksiginfo_t * si)417 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
418 {
419 struct proc *p = sq->sq_proc;
420 struct ksiginfo *ksi, *next;
421 int count = 0;
422
423 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
424
425 if (!SIGISMEMBER(sq->sq_signals, signo))
426 return (0);
427
428 if (SIGISMEMBER(sq->sq_ptrace, signo)) {
429 count++;
430 SIGDELSET(sq->sq_ptrace, signo);
431 si->ksi_flags |= KSI_PTRACE;
432 }
433 if (SIGISMEMBER(sq->sq_kill, signo)) {
434 count++;
435 if (count == 1)
436 SIGDELSET(sq->sq_kill, signo);
437 }
438
439 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
440 if (ksi->ksi_signo == signo) {
441 if (count == 0) {
442 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
443 ksi->ksi_sigq = NULL;
444 ksiginfo_copy(ksi, si);
445 if (ksiginfo_tryfree(ksi) && p != NULL)
446 p->p_pendingcnt--;
447 }
448 if (++count > 1)
449 break;
450 }
451 }
452
453 if (count <= 1)
454 SIGDELSET(sq->sq_signals, signo);
455 si->ksi_signo = signo;
456 return (signo);
457 }
458
459 void
sigqueue_take(ksiginfo_t * ksi)460 sigqueue_take(ksiginfo_t *ksi)
461 {
462 struct ksiginfo *kp;
463 struct proc *p;
464 sigqueue_t *sq;
465
466 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
467 return;
468
469 p = sq->sq_proc;
470 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
471 ksi->ksi_sigq = NULL;
472 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
473 p->p_pendingcnt--;
474
475 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
476 kp = TAILQ_NEXT(kp, ksi_link)) {
477 if (kp->ksi_signo == ksi->ksi_signo)
478 break;
479 }
480 if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) &&
481 !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo))
482 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
483 }
484
485 static int
sigqueue_add(sigqueue_t * sq,int signo,ksiginfo_t * si)486 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
487 {
488 struct proc *p = sq->sq_proc;
489 struct ksiginfo *ksi;
490 int ret = 0;
491
492 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
493
494 /*
495 * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path
496 * for these signals.
497 */
498 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
499 SIGADDSET(sq->sq_kill, signo);
500 goto out_set_bit;
501 }
502
503 /* directly insert the ksi, don't copy it */
504 if (si->ksi_flags & KSI_INS) {
505 if (si->ksi_flags & KSI_HEAD)
506 TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
507 else
508 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
509 si->ksi_sigq = sq;
510 goto out_set_bit;
511 }
512
513 if (__predict_false(ksiginfo_zone == NULL)) {
514 SIGADDSET(sq->sq_kill, signo);
515 goto out_set_bit;
516 }
517
518 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
519 signal_overflow++;
520 ret = EAGAIN;
521 } else if ((ksi = ksiginfo_alloc(M_NOWAIT)) == NULL) {
522 signal_alloc_fail++;
523 ret = EAGAIN;
524 } else {
525 if (p != NULL)
526 p->p_pendingcnt++;
527 ksiginfo_copy(si, ksi);
528 ksi->ksi_signo = signo;
529 if (si->ksi_flags & KSI_HEAD)
530 TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
531 else
532 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
533 ksi->ksi_sigq = sq;
534 }
535
536 if (ret != 0) {
537 if ((si->ksi_flags & KSI_PTRACE) != 0) {
538 SIGADDSET(sq->sq_ptrace, signo);
539 ret = 0;
540 goto out_set_bit;
541 } else if ((si->ksi_flags & KSI_TRAP) != 0 ||
542 (si->ksi_flags & KSI_SIGQ) == 0) {
543 SIGADDSET(sq->sq_kill, signo);
544 ret = 0;
545 goto out_set_bit;
546 }
547 return (ret);
548 }
549
550 out_set_bit:
551 SIGADDSET(sq->sq_signals, signo);
552 return (ret);
553 }
554
555 void
sigqueue_flush(sigqueue_t * sq)556 sigqueue_flush(sigqueue_t *sq)
557 {
558 struct proc *p = sq->sq_proc;
559 ksiginfo_t *ksi;
560
561 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
562
563 if (p != NULL)
564 PROC_LOCK_ASSERT(p, MA_OWNED);
565
566 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
567 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
568 ksi->ksi_sigq = NULL;
569 if (ksiginfo_tryfree(ksi) && p != NULL)
570 p->p_pendingcnt--;
571 }
572
573 SIGEMPTYSET(sq->sq_signals);
574 SIGEMPTYSET(sq->sq_kill);
575 SIGEMPTYSET(sq->sq_ptrace);
576 }
577
578 static void
sigqueue_move_set(sigqueue_t * src,sigqueue_t * dst,const sigset_t * set)579 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
580 {
581 sigset_t tmp;
582 struct proc *p1, *p2;
583 ksiginfo_t *ksi, *next;
584
585 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
586 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
587 p1 = src->sq_proc;
588 p2 = dst->sq_proc;
589 /* Move siginfo to target list */
590 TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
591 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
592 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
593 if (p1 != NULL)
594 p1->p_pendingcnt--;
595 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
596 ksi->ksi_sigq = dst;
597 if (p2 != NULL)
598 p2->p_pendingcnt++;
599 }
600 }
601
602 /* Move pending bits to target list */
603 tmp = src->sq_kill;
604 SIGSETAND(tmp, *set);
605 SIGSETOR(dst->sq_kill, tmp);
606 SIGSETNAND(src->sq_kill, tmp);
607
608 tmp = src->sq_ptrace;
609 SIGSETAND(tmp, *set);
610 SIGSETOR(dst->sq_ptrace, tmp);
611 SIGSETNAND(src->sq_ptrace, tmp);
612
613 tmp = src->sq_signals;
614 SIGSETAND(tmp, *set);
615 SIGSETOR(dst->sq_signals, tmp);
616 SIGSETNAND(src->sq_signals, tmp);
617 }
618
619 #if 0
620 static void
621 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
622 {
623 sigset_t set;
624
625 SIGEMPTYSET(set);
626 SIGADDSET(set, signo);
627 sigqueue_move_set(src, dst, &set);
628 }
629 #endif
630
631 static void
sigqueue_delete_set(sigqueue_t * sq,const sigset_t * set)632 sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
633 {
634 struct proc *p = sq->sq_proc;
635 ksiginfo_t *ksi, *next;
636
637 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
638
639 /* Remove siginfo queue */
640 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
641 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
642 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
643 ksi->ksi_sigq = NULL;
644 if (ksiginfo_tryfree(ksi) && p != NULL)
645 p->p_pendingcnt--;
646 }
647 }
648 SIGSETNAND(sq->sq_kill, *set);
649 SIGSETNAND(sq->sq_ptrace, *set);
650 SIGSETNAND(sq->sq_signals, *set);
651 }
652
653 void
sigqueue_delete(sigqueue_t * sq,int signo)654 sigqueue_delete(sigqueue_t *sq, int signo)
655 {
656 sigset_t set;
657
658 SIGEMPTYSET(set);
659 SIGADDSET(set, signo);
660 sigqueue_delete_set(sq, &set);
661 }
662
663 /* Remove a set of signals for a process */
664 static void
sigqueue_delete_set_proc(struct proc * p,const sigset_t * set)665 sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
666 {
667 sigqueue_t worklist;
668 struct thread *td0;
669
670 PROC_LOCK_ASSERT(p, MA_OWNED);
671
672 sigqueue_init(&worklist, NULL);
673 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
674
675 FOREACH_THREAD_IN_PROC(p, td0)
676 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
677
678 sigqueue_flush(&worklist);
679 }
680
681 void
sigqueue_delete_proc(struct proc * p,int signo)682 sigqueue_delete_proc(struct proc *p, int signo)
683 {
684 sigset_t set;
685
686 SIGEMPTYSET(set);
687 SIGADDSET(set, signo);
688 sigqueue_delete_set_proc(p, &set);
689 }
690
691 static void
sigqueue_delete_stopmask_proc(struct proc * p)692 sigqueue_delete_stopmask_proc(struct proc *p)
693 {
694 sigset_t set;
695
696 SIGEMPTYSET(set);
697 SIGADDSET(set, SIGSTOP);
698 SIGADDSET(set, SIGTSTP);
699 SIGADDSET(set, SIGTTIN);
700 SIGADDSET(set, SIGTTOU);
701 sigqueue_delete_set_proc(p, &set);
702 }
703
704 /*
705 * Determine signal that should be delivered to thread td, the current
706 * thread, 0 if none. If there is a pending stop signal with default
707 * action, the process stops in issignal().
708 */
709 int
cursig(struct thread * td)710 cursig(struct thread *td)
711 {
712 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
713 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
714 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
715 return (SIGPENDING(td) ? issignal(td) : 0);
716 }
717
718 /*
719 * Arrange for ast() to handle unmasked pending signals on return to user
720 * mode. This must be called whenever a signal is added to td_sigqueue or
721 * unmasked in td_sigmask.
722 */
723 void
signotify(struct thread * td)724 signotify(struct thread *td)
725 {
726
727 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
728
729 if (SIGPENDING(td))
730 ast_sched(td, TDA_SIG);
731 }
732
733 /*
734 * Returns 1 (true) if altstack is configured for the thread, and the
735 * passed stack bottom address falls into the altstack range. Handles
736 * the 43 compat special case where the alt stack size is zero.
737 */
738 int
sigonstack(size_t sp)739 sigonstack(size_t sp)
740 {
741 struct thread *td;
742
743 td = curthread;
744 if ((td->td_pflags & TDP_ALTSTACK) == 0)
745 return (0);
746 #if defined(COMPAT_43)
747 if (SV_PROC_FLAG(td->td_proc, SV_AOUT) && td->td_sigstk.ss_size == 0)
748 return ((td->td_sigstk.ss_flags & SS_ONSTACK) != 0);
749 #endif
750 return (sp >= (size_t)td->td_sigstk.ss_sp &&
751 sp < td->td_sigstk.ss_size + (size_t)td->td_sigstk.ss_sp);
752 }
753
754 static __inline int
sigprop(int sig)755 sigprop(int sig)
756 {
757
758 if (sig > 0 && sig < nitems(sigproptbl))
759 return (sigproptbl[sig]);
760 return (0);
761 }
762
763 bool
sig_do_core(int sig)764 sig_do_core(int sig)
765 {
766
767 return ((sigprop(sig) & SIGPROP_CORE) != 0);
768 }
769
770 static bool
sigact_flag_test(const struct sigaction * act,int flag)771 sigact_flag_test(const struct sigaction *act, int flag)
772 {
773
774 /*
775 * SA_SIGINFO is reset when signal disposition is set to
776 * ignore or default. Other flags are kept according to user
777 * settings.
778 */
779 return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO ||
780 ((__sighandler_t *)act->sa_sigaction != SIG_IGN &&
781 (__sighandler_t *)act->sa_sigaction != SIG_DFL)));
782 }
783
784 /*
785 * kern_sigaction
786 * sigaction
787 * freebsd4_sigaction
788 * osigaction
789 */
790 int
kern_sigaction(struct thread * td,int sig,const struct sigaction * act,struct sigaction * oact,int flags)791 kern_sigaction(struct thread *td, int sig, const struct sigaction *act,
792 struct sigaction *oact, int flags)
793 {
794 struct sigacts *ps;
795 struct proc *p = td->td_proc;
796
797 if (!_SIG_VALID(sig))
798 return (EINVAL);
799 if (act != NULL && act->sa_handler != SIG_DFL &&
800 act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK |
801 SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER |
802 SA_NOCLDWAIT | SA_SIGINFO)) != 0)
803 return (EINVAL);
804
805 PROC_LOCK(p);
806 ps = p->p_sigacts;
807 mtx_lock(&ps->ps_mtx);
808 if (oact) {
809 memset(oact, 0, sizeof(*oact));
810 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
811 if (SIGISMEMBER(ps->ps_sigonstack, sig))
812 oact->sa_flags |= SA_ONSTACK;
813 if (!SIGISMEMBER(ps->ps_sigintr, sig))
814 oact->sa_flags |= SA_RESTART;
815 if (SIGISMEMBER(ps->ps_sigreset, sig))
816 oact->sa_flags |= SA_RESETHAND;
817 if (SIGISMEMBER(ps->ps_signodefer, sig))
818 oact->sa_flags |= SA_NODEFER;
819 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
820 oact->sa_flags |= SA_SIGINFO;
821 oact->sa_sigaction =
822 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
823 } else
824 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
825 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
826 oact->sa_flags |= SA_NOCLDSTOP;
827 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
828 oact->sa_flags |= SA_NOCLDWAIT;
829 }
830 if (act) {
831 if ((sig == SIGKILL || sig == SIGSTOP) &&
832 act->sa_handler != SIG_DFL) {
833 mtx_unlock(&ps->ps_mtx);
834 PROC_UNLOCK(p);
835 return (EINVAL);
836 }
837
838 /*
839 * Change setting atomically.
840 */
841
842 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
843 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
844 if (sigact_flag_test(act, SA_SIGINFO)) {
845 ps->ps_sigact[_SIG_IDX(sig)] =
846 (__sighandler_t *)act->sa_sigaction;
847 SIGADDSET(ps->ps_siginfo, sig);
848 } else {
849 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
850 SIGDELSET(ps->ps_siginfo, sig);
851 }
852 if (!sigact_flag_test(act, SA_RESTART))
853 SIGADDSET(ps->ps_sigintr, sig);
854 else
855 SIGDELSET(ps->ps_sigintr, sig);
856 if (sigact_flag_test(act, SA_ONSTACK))
857 SIGADDSET(ps->ps_sigonstack, sig);
858 else
859 SIGDELSET(ps->ps_sigonstack, sig);
860 if (sigact_flag_test(act, SA_RESETHAND))
861 SIGADDSET(ps->ps_sigreset, sig);
862 else
863 SIGDELSET(ps->ps_sigreset, sig);
864 if (sigact_flag_test(act, SA_NODEFER))
865 SIGADDSET(ps->ps_signodefer, sig);
866 else
867 SIGDELSET(ps->ps_signodefer, sig);
868 if (sig == SIGCHLD) {
869 if (act->sa_flags & SA_NOCLDSTOP)
870 ps->ps_flag |= PS_NOCLDSTOP;
871 else
872 ps->ps_flag &= ~PS_NOCLDSTOP;
873 if (act->sa_flags & SA_NOCLDWAIT) {
874 /*
875 * Paranoia: since SA_NOCLDWAIT is implemented
876 * by reparenting the dying child to PID 1 (and
877 * trust it to reap the zombie), PID 1 itself
878 * is forbidden to set SA_NOCLDWAIT.
879 */
880 if (p->p_pid == 1)
881 ps->ps_flag &= ~PS_NOCLDWAIT;
882 else
883 ps->ps_flag |= PS_NOCLDWAIT;
884 } else
885 ps->ps_flag &= ~PS_NOCLDWAIT;
886 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
887 ps->ps_flag |= PS_CLDSIGIGN;
888 else
889 ps->ps_flag &= ~PS_CLDSIGIGN;
890 }
891 /*
892 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
893 * and for signals set to SIG_DFL where the default is to
894 * ignore. However, don't put SIGCONT in ps_sigignore, as we
895 * have to restart the process.
896 */
897 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
898 (sigprop(sig) & SIGPROP_IGNORE &&
899 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
900 /* never to be seen again */
901 sigqueue_delete_proc(p, sig);
902 if (sig != SIGCONT)
903 /* easier in psignal */
904 SIGADDSET(ps->ps_sigignore, sig);
905 SIGDELSET(ps->ps_sigcatch, sig);
906 } else {
907 SIGDELSET(ps->ps_sigignore, sig);
908 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
909 SIGDELSET(ps->ps_sigcatch, sig);
910 else
911 SIGADDSET(ps->ps_sigcatch, sig);
912 }
913 #ifdef COMPAT_FREEBSD4
914 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
915 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
916 (flags & KSA_FREEBSD4) == 0)
917 SIGDELSET(ps->ps_freebsd4, sig);
918 else
919 SIGADDSET(ps->ps_freebsd4, sig);
920 #endif
921 #ifdef COMPAT_43
922 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
923 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
924 (flags & KSA_OSIGSET) == 0)
925 SIGDELSET(ps->ps_osigset, sig);
926 else
927 SIGADDSET(ps->ps_osigset, sig);
928 #endif
929 }
930 mtx_unlock(&ps->ps_mtx);
931 PROC_UNLOCK(p);
932 return (0);
933 }
934
935 #ifndef _SYS_SYSPROTO_H_
936 struct sigaction_args {
937 int sig;
938 struct sigaction *act;
939 struct sigaction *oact;
940 };
941 #endif
942 int
sys_sigaction(struct thread * td,struct sigaction_args * uap)943 sys_sigaction(struct thread *td, struct sigaction_args *uap)
944 {
945 struct sigaction act, oact;
946 struct sigaction *actp, *oactp;
947 int error;
948
949 actp = (uap->act != NULL) ? &act : NULL;
950 oactp = (uap->oact != NULL) ? &oact : NULL;
951 if (actp) {
952 error = copyin(uap->act, actp, sizeof(act));
953 if (error)
954 return (error);
955 }
956 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
957 if (oactp && !error)
958 error = copyout(oactp, uap->oact, sizeof(oact));
959 return (error);
960 }
961
962 #ifdef COMPAT_FREEBSD4
963 #ifndef _SYS_SYSPROTO_H_
964 struct freebsd4_sigaction_args {
965 int sig;
966 struct sigaction *act;
967 struct sigaction *oact;
968 };
969 #endif
970 int
freebsd4_sigaction(struct thread * td,struct freebsd4_sigaction_args * uap)971 freebsd4_sigaction(struct thread *td, struct freebsd4_sigaction_args *uap)
972 {
973 struct sigaction act, oact;
974 struct sigaction *actp, *oactp;
975 int error;
976
977 actp = (uap->act != NULL) ? &act : NULL;
978 oactp = (uap->oact != NULL) ? &oact : NULL;
979 if (actp) {
980 error = copyin(uap->act, actp, sizeof(act));
981 if (error)
982 return (error);
983 }
984 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
985 if (oactp && !error)
986 error = copyout(oactp, uap->oact, sizeof(oact));
987 return (error);
988 }
989 #endif /* COMAPT_FREEBSD4 */
990
991 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
992 #ifndef _SYS_SYSPROTO_H_
993 struct osigaction_args {
994 int signum;
995 struct osigaction *nsa;
996 struct osigaction *osa;
997 };
998 #endif
999 int
osigaction(struct thread * td,struct osigaction_args * uap)1000 osigaction(struct thread *td, struct osigaction_args *uap)
1001 {
1002 struct osigaction sa;
1003 struct sigaction nsa, osa;
1004 struct sigaction *nsap, *osap;
1005 int error;
1006
1007 if (uap->signum <= 0 || uap->signum >= ONSIG)
1008 return (EINVAL);
1009
1010 nsap = (uap->nsa != NULL) ? &nsa : NULL;
1011 osap = (uap->osa != NULL) ? &osa : NULL;
1012
1013 if (nsap) {
1014 error = copyin(uap->nsa, &sa, sizeof(sa));
1015 if (error)
1016 return (error);
1017 nsap->sa_handler = sa.sa_handler;
1018 nsap->sa_flags = sa.sa_flags;
1019 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
1020 }
1021 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1022 if (osap && !error) {
1023 sa.sa_handler = osap->sa_handler;
1024 sa.sa_flags = osap->sa_flags;
1025 SIG2OSIG(osap->sa_mask, sa.sa_mask);
1026 error = copyout(&sa, uap->osa, sizeof(sa));
1027 }
1028 return (error);
1029 }
1030
1031 #if !defined(__i386__)
1032 /* Avoid replicating the same stub everywhere */
1033 int
osigreturn(struct thread * td,struct osigreturn_args * uap)1034 osigreturn(struct thread *td, struct osigreturn_args *uap)
1035 {
1036 return (kern_nosys(td, 0));
1037 }
1038 #endif
1039 #endif /* COMPAT_43 */
1040
1041 /*
1042 * Initialize signal state for process 0;
1043 * set to ignore signals that are ignored by default.
1044 */
1045 void
siginit(struct proc * p)1046 siginit(struct proc *p)
1047 {
1048 int i;
1049 struct sigacts *ps;
1050
1051 PROC_LOCK(p);
1052 ps = p->p_sigacts;
1053 mtx_lock(&ps->ps_mtx);
1054 for (i = 1; i <= NSIG; i++) {
1055 if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) {
1056 SIGADDSET(ps->ps_sigignore, i);
1057 }
1058 }
1059 mtx_unlock(&ps->ps_mtx);
1060 PROC_UNLOCK(p);
1061 }
1062
1063 /*
1064 * Reset specified signal to the default disposition.
1065 */
1066 static void
sigdflt(struct sigacts * ps,int sig)1067 sigdflt(struct sigacts *ps, int sig)
1068 {
1069
1070 mtx_assert(&ps->ps_mtx, MA_OWNED);
1071 SIGDELSET(ps->ps_sigcatch, sig);
1072 if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT)
1073 SIGADDSET(ps->ps_sigignore, sig);
1074 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1075 SIGDELSET(ps->ps_siginfo, sig);
1076 }
1077
1078 /*
1079 * Reset signals for an exec of the specified process.
1080 */
1081 void
execsigs(struct proc * p)1082 execsigs(struct proc *p)
1083 {
1084 struct sigacts *ps;
1085 struct thread *td;
1086
1087 /*
1088 * Reset caught signals. Held signals remain held
1089 * through td_sigmask (unless they were caught,
1090 * and are now ignored by default).
1091 */
1092 PROC_LOCK_ASSERT(p, MA_OWNED);
1093 ps = p->p_sigacts;
1094 mtx_lock(&ps->ps_mtx);
1095 sig_drop_caught(p);
1096
1097 /*
1098 * Reset stack state to the user stack.
1099 * Clear set of signals caught on the signal stack.
1100 */
1101 td = curthread;
1102 MPASS(td->td_proc == p);
1103 td->td_sigstk.ss_flags = SS_DISABLE;
1104 td->td_sigstk.ss_size = 0;
1105 td->td_sigstk.ss_sp = 0;
1106 td->td_pflags &= ~TDP_ALTSTACK;
1107 /*
1108 * Reset no zombies if child dies flag as Solaris does.
1109 */
1110 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
1111 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
1112 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
1113 mtx_unlock(&ps->ps_mtx);
1114 }
1115
1116 /*
1117 * kern_sigprocmask()
1118 *
1119 * Manipulate signal mask.
1120 */
1121 int
kern_sigprocmask(struct thread * td,int how,sigset_t * set,sigset_t * oset,int flags)1122 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
1123 int flags)
1124 {
1125 sigset_t new_block, oset1;
1126 struct proc *p;
1127 int error;
1128
1129 p = td->td_proc;
1130 if ((flags & SIGPROCMASK_PROC_LOCKED) != 0)
1131 PROC_LOCK_ASSERT(p, MA_OWNED);
1132 else
1133 PROC_LOCK(p);
1134 mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0
1135 ? MA_OWNED : MA_NOTOWNED);
1136 if (oset != NULL)
1137 *oset = td->td_sigmask;
1138
1139 error = 0;
1140 if (set != NULL) {
1141 switch (how) {
1142 case SIG_BLOCK:
1143 SIG_CANTMASK(*set);
1144 oset1 = td->td_sigmask;
1145 SIGSETOR(td->td_sigmask, *set);
1146 new_block = td->td_sigmask;
1147 SIGSETNAND(new_block, oset1);
1148 break;
1149 case SIG_UNBLOCK:
1150 SIGSETNAND(td->td_sigmask, *set);
1151 signotify(td);
1152 goto out;
1153 case SIG_SETMASK:
1154 SIG_CANTMASK(*set);
1155 oset1 = td->td_sigmask;
1156 if (flags & SIGPROCMASK_OLD)
1157 SIGSETLO(td->td_sigmask, *set);
1158 else
1159 td->td_sigmask = *set;
1160 new_block = td->td_sigmask;
1161 SIGSETNAND(new_block, oset1);
1162 signotify(td);
1163 break;
1164 default:
1165 error = EINVAL;
1166 goto out;
1167 }
1168
1169 /*
1170 * The new_block set contains signals that were not previously
1171 * blocked, but are blocked now.
1172 *
1173 * In case we block any signal that was not previously blocked
1174 * for td, and process has the signal pending, try to schedule
1175 * signal delivery to some thread that does not block the
1176 * signal, possibly waking it up.
1177 */
1178 if (p->p_numthreads != 1)
1179 reschedule_signals(p, new_block, flags);
1180 }
1181
1182 out:
1183 if (!(flags & SIGPROCMASK_PROC_LOCKED))
1184 PROC_UNLOCK(p);
1185 return (error);
1186 }
1187
1188 #ifndef _SYS_SYSPROTO_H_
1189 struct sigprocmask_args {
1190 int how;
1191 const sigset_t *set;
1192 sigset_t *oset;
1193 };
1194 #endif
1195 int
sys_sigprocmask(struct thread * td,struct sigprocmask_args * uap)1196 sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap)
1197 {
1198 sigset_t set, oset;
1199 sigset_t *setp, *osetp;
1200 int error;
1201
1202 setp = (uap->set != NULL) ? &set : NULL;
1203 osetp = (uap->oset != NULL) ? &oset : NULL;
1204 if (setp) {
1205 error = copyin(uap->set, setp, sizeof(set));
1206 if (error)
1207 return (error);
1208 }
1209 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1210 if (osetp && !error) {
1211 error = copyout(osetp, uap->oset, sizeof(oset));
1212 }
1213 return (error);
1214 }
1215
1216 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1217 #ifndef _SYS_SYSPROTO_H_
1218 struct osigprocmask_args {
1219 int how;
1220 osigset_t mask;
1221 };
1222 #endif
1223 int
osigprocmask(struct thread * td,struct osigprocmask_args * uap)1224 osigprocmask(struct thread *td, struct osigprocmask_args *uap)
1225 {
1226 sigset_t set, oset;
1227 int error;
1228
1229 OSIG2SIG(uap->mask, set);
1230 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1231 SIG2OSIG(oset, td->td_retval[0]);
1232 return (error);
1233 }
1234 #endif /* COMPAT_43 */
1235
1236 int
sys_sigwait(struct thread * td,struct sigwait_args * uap)1237 sys_sigwait(struct thread *td, struct sigwait_args *uap)
1238 {
1239 ksiginfo_t ksi;
1240 sigset_t set;
1241 int error;
1242
1243 error = copyin(uap->set, &set, sizeof(set));
1244 if (error) {
1245 td->td_retval[0] = error;
1246 return (0);
1247 }
1248
1249 error = kern_sigtimedwait(td, set, &ksi, NULL);
1250 if (error) {
1251 /*
1252 * sigwait() function shall not return EINTR, but
1253 * the syscall does. Non-ancient libc provides the
1254 * wrapper which hides EINTR. Otherwise, EINTR return
1255 * is used by libthr to handle required cancellation
1256 * point in the sigwait().
1257 */
1258 if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1259 return (ERESTART);
1260 td->td_retval[0] = error;
1261 return (0);
1262 }
1263
1264 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1265 td->td_retval[0] = error;
1266 return (0);
1267 }
1268
1269 int
sys_sigtimedwait(struct thread * td,struct sigtimedwait_args * uap)1270 sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1271 {
1272 struct timespec ts;
1273 struct timespec *timeout;
1274 sigset_t set;
1275 ksiginfo_t ksi;
1276 int error;
1277
1278 if (uap->timeout) {
1279 error = copyin(uap->timeout, &ts, sizeof(ts));
1280 if (error)
1281 return (error);
1282
1283 timeout = &ts;
1284 } else
1285 timeout = NULL;
1286
1287 error = copyin(uap->set, &set, sizeof(set));
1288 if (error)
1289 return (error);
1290
1291 error = kern_sigtimedwait(td, set, &ksi, timeout);
1292 if (error)
1293 return (error);
1294
1295 if (uap->info)
1296 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1297
1298 if (error == 0)
1299 td->td_retval[0] = ksi.ksi_signo;
1300 return (error);
1301 }
1302
1303 int
sys_sigwaitinfo(struct thread * td,struct sigwaitinfo_args * uap)1304 sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1305 {
1306 ksiginfo_t ksi;
1307 sigset_t set;
1308 int error;
1309
1310 error = copyin(uap->set, &set, sizeof(set));
1311 if (error)
1312 return (error);
1313
1314 error = kern_sigtimedwait(td, set, &ksi, NULL);
1315 if (error)
1316 return (error);
1317
1318 if (uap->info)
1319 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1320
1321 if (error == 0)
1322 td->td_retval[0] = ksi.ksi_signo;
1323 return (error);
1324 }
1325
1326 static void
proc_td_siginfo_capture(struct thread * td,siginfo_t * si)1327 proc_td_siginfo_capture(struct thread *td, siginfo_t *si)
1328 {
1329 struct thread *thr;
1330
1331 FOREACH_THREAD_IN_PROC(td->td_proc, thr) {
1332 if (thr == td)
1333 thr->td_si = *si;
1334 else
1335 thr->td_si.si_signo = 0;
1336 }
1337 }
1338
1339 int
kern_sigtimedwait(struct thread * td,sigset_t waitset,ksiginfo_t * ksi,struct timespec * timeout)1340 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1341 struct timespec *timeout)
1342 {
1343 struct sigacts *ps;
1344 sigset_t saved_mask, new_block;
1345 struct proc *p;
1346 int error, sig, timevalid = 0;
1347 sbintime_t sbt, precision, tsbt;
1348 struct timespec ts;
1349 bool traced;
1350
1351 p = td->td_proc;
1352 error = 0;
1353 traced = false;
1354
1355 /* Ensure the sigfastblock value is up to date. */
1356 sigfastblock_fetch(td);
1357
1358 if (timeout != NULL) {
1359 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1360 timevalid = 1;
1361 ts = *timeout;
1362 if (ts.tv_sec < INT32_MAX / 2) {
1363 tsbt = tstosbt(ts);
1364 precision = tsbt;
1365 precision >>= tc_precexp;
1366 if (TIMESEL(&sbt, tsbt))
1367 sbt += tc_tick_sbt;
1368 sbt += tsbt;
1369 } else
1370 precision = sbt = 0;
1371 }
1372 } else
1373 precision = sbt = 0;
1374 ksiginfo_init(ksi);
1375 /* Some signals can not be waited for. */
1376 SIG_CANTMASK(waitset);
1377 ps = p->p_sigacts;
1378 PROC_LOCK(p);
1379 saved_mask = td->td_sigmask;
1380 SIGSETNAND(td->td_sigmask, waitset);
1381 if ((p->p_sysent->sv_flags & SV_SIG_DISCIGN) != 0 ||
1382 !kern_sig_discard_ign) {
1383 thread_lock(td);
1384 td->td_flags |= TDF_SIGWAIT;
1385 thread_unlock(td);
1386 }
1387 for (;;) {
1388 mtx_lock(&ps->ps_mtx);
1389 sig = cursig(td);
1390 mtx_unlock(&ps->ps_mtx);
1391 KASSERT(sig >= 0, ("sig %d", sig));
1392 if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1393 if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1394 sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1395 error = 0;
1396 break;
1397 }
1398 }
1399
1400 if (error != 0)
1401 break;
1402
1403 /*
1404 * POSIX says this must be checked after looking for pending
1405 * signals.
1406 */
1407 if (timeout != NULL && !timevalid) {
1408 error = EINVAL;
1409 break;
1410 }
1411
1412 if (traced) {
1413 error = EINTR;
1414 break;
1415 }
1416
1417 error = msleep_sbt(&p->p_sigacts, &p->p_mtx, PPAUSE | PCATCH,
1418 "sigwait", sbt, precision, C_ABSOLUTE);
1419
1420 /* The syscalls can not be restarted. */
1421 if (error == ERESTART)
1422 error = EINTR;
1423
1424 /*
1425 * If PTRACE_SCE or PTRACE_SCX were set after
1426 * userspace entered the syscall, return spurious
1427 * EINTR after wait was done. Only do this as last
1428 * resort after rechecking for possible queued signals
1429 * and expired timeouts.
1430 */
1431 if (error == 0 && (p->p_ptevents & PTRACE_SYSCALL) != 0)
1432 traced = true;
1433 }
1434 thread_lock(td);
1435 td->td_flags &= ~TDF_SIGWAIT;
1436 thread_unlock(td);
1437
1438 new_block = saved_mask;
1439 SIGSETNAND(new_block, td->td_sigmask);
1440 td->td_sigmask = saved_mask;
1441 /*
1442 * Fewer signals can be delivered to us, reschedule signal
1443 * notification.
1444 */
1445 if (p->p_numthreads != 1)
1446 reschedule_signals(p, new_block, 0);
1447
1448 if (error == 0) {
1449 SDT_PROBE2(proc, , , signal__clear, sig, ksi);
1450
1451 if (ksi->ksi_code == SI_TIMER)
1452 itimer_accept(p, ksi->ksi_timerid, ksi);
1453
1454 #ifdef KTRACE
1455 if (KTRPOINT(td, KTR_PSIG)) {
1456 sig_t action;
1457
1458 mtx_lock(&ps->ps_mtx);
1459 action = ps->ps_sigact[_SIG_IDX(sig)];
1460 mtx_unlock(&ps->ps_mtx);
1461 ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
1462 }
1463 #endif
1464 if (sig == SIGKILL) {
1465 proc_td_siginfo_capture(td, &ksi->ksi_info);
1466 sigexit(td, sig);
1467 }
1468 }
1469 PROC_UNLOCK(p);
1470 return (error);
1471 }
1472
1473 #ifndef _SYS_SYSPROTO_H_
1474 struct sigpending_args {
1475 sigset_t *set;
1476 };
1477 #endif
1478 int
sys_sigpending(struct thread * td,struct sigpending_args * uap)1479 sys_sigpending(struct thread *td, struct sigpending_args *uap)
1480 {
1481 struct proc *p = td->td_proc;
1482 sigset_t pending;
1483
1484 PROC_LOCK(p);
1485 pending = p->p_sigqueue.sq_signals;
1486 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1487 PROC_UNLOCK(p);
1488 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1489 }
1490
1491 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1492 #ifndef _SYS_SYSPROTO_H_
1493 struct osigpending_args {
1494 int dummy;
1495 };
1496 #endif
1497 int
osigpending(struct thread * td,struct osigpending_args * uap)1498 osigpending(struct thread *td, struct osigpending_args *uap)
1499 {
1500 struct proc *p = td->td_proc;
1501 sigset_t pending;
1502
1503 PROC_LOCK(p);
1504 pending = p->p_sigqueue.sq_signals;
1505 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1506 PROC_UNLOCK(p);
1507 SIG2OSIG(pending, td->td_retval[0]);
1508 return (0);
1509 }
1510 #endif /* COMPAT_43 */
1511
1512 #if defined(COMPAT_43)
1513 /*
1514 * Generalized interface signal handler, 4.3-compatible.
1515 */
1516 #ifndef _SYS_SYSPROTO_H_
1517 struct osigvec_args {
1518 int signum;
1519 struct sigvec *nsv;
1520 struct sigvec *osv;
1521 };
1522 #endif
1523 /* ARGSUSED */
1524 int
osigvec(struct thread * td,struct osigvec_args * uap)1525 osigvec(struct thread *td, struct osigvec_args *uap)
1526 {
1527 struct sigvec vec;
1528 struct sigaction nsa, osa;
1529 struct sigaction *nsap, *osap;
1530 int error;
1531
1532 if (uap->signum <= 0 || uap->signum >= ONSIG)
1533 return (EINVAL);
1534 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1535 osap = (uap->osv != NULL) ? &osa : NULL;
1536 if (nsap) {
1537 error = copyin(uap->nsv, &vec, sizeof(vec));
1538 if (error)
1539 return (error);
1540 nsap->sa_handler = vec.sv_handler;
1541 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1542 nsap->sa_flags = vec.sv_flags;
1543 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1544 }
1545 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1546 if (osap && !error) {
1547 vec.sv_handler = osap->sa_handler;
1548 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1549 vec.sv_flags = osap->sa_flags;
1550 vec.sv_flags &= ~SA_NOCLDWAIT;
1551 vec.sv_flags ^= SA_RESTART;
1552 error = copyout(&vec, uap->osv, sizeof(vec));
1553 }
1554 return (error);
1555 }
1556
1557 #ifndef _SYS_SYSPROTO_H_
1558 struct osigblock_args {
1559 int mask;
1560 };
1561 #endif
1562 int
osigblock(struct thread * td,struct osigblock_args * uap)1563 osigblock(struct thread *td, struct osigblock_args *uap)
1564 {
1565 sigset_t set, oset;
1566
1567 OSIG2SIG(uap->mask, set);
1568 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1569 SIG2OSIG(oset, td->td_retval[0]);
1570 return (0);
1571 }
1572
1573 #ifndef _SYS_SYSPROTO_H_
1574 struct osigsetmask_args {
1575 int mask;
1576 };
1577 #endif
1578 int
osigsetmask(struct thread * td,struct osigsetmask_args * uap)1579 osigsetmask(struct thread *td, struct osigsetmask_args *uap)
1580 {
1581 sigset_t set, oset;
1582
1583 OSIG2SIG(uap->mask, set);
1584 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1585 SIG2OSIG(oset, td->td_retval[0]);
1586 return (0);
1587 }
1588 #endif /* COMPAT_43 */
1589
1590 /*
1591 * Suspend calling thread until signal, providing mask to be set in the
1592 * meantime.
1593 */
1594 #ifndef _SYS_SYSPROTO_H_
1595 struct sigsuspend_args {
1596 const sigset_t *sigmask;
1597 };
1598 #endif
1599 /* ARGSUSED */
1600 int
sys_sigsuspend(struct thread * td,struct sigsuspend_args * uap)1601 sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap)
1602 {
1603 sigset_t mask;
1604 int error;
1605
1606 error = copyin(uap->sigmask, &mask, sizeof(mask));
1607 if (error)
1608 return (error);
1609 return (kern_sigsuspend(td, mask));
1610 }
1611
1612 int
kern_sigsuspend(struct thread * td,sigset_t mask)1613 kern_sigsuspend(struct thread *td, sigset_t mask)
1614 {
1615 struct proc *p = td->td_proc;
1616 int has_sig, sig;
1617
1618 /* Ensure the sigfastblock value is up to date. */
1619 sigfastblock_fetch(td);
1620
1621 /*
1622 * When returning from sigsuspend, we want
1623 * the old mask to be restored after the
1624 * signal handler has finished. Thus, we
1625 * save it here and mark the sigacts structure
1626 * to indicate this.
1627 */
1628 PROC_LOCK(p);
1629 kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
1630 SIGPROCMASK_PROC_LOCKED);
1631 td->td_pflags |= TDP_OLDMASK;
1632 ast_sched(td, TDA_SIGSUSPEND);
1633
1634 /*
1635 * Process signals now. Otherwise, we can get spurious wakeup
1636 * due to signal entered process queue, but delivered to other
1637 * thread. But sigsuspend should return only on signal
1638 * delivery.
1639 */
1640 (p->p_sysent->sv_set_syscall_retval)(td, EINTR);
1641 for (has_sig = 0; !has_sig;) {
1642 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE | PCATCH,
1643 "sigsusp", 0) == 0)
1644 /* void */;
1645 thread_suspend_check(0);
1646 mtx_lock(&p->p_sigacts->ps_mtx);
1647 while ((sig = cursig(td)) != 0) {
1648 KASSERT(sig >= 0, ("sig %d", sig));
1649 has_sig += postsig(sig);
1650 }
1651 mtx_unlock(&p->p_sigacts->ps_mtx);
1652
1653 /*
1654 * If PTRACE_SCE or PTRACE_SCX were set after
1655 * userspace entered the syscall, return spurious
1656 * EINTR.
1657 */
1658 if ((p->p_ptevents & PTRACE_SYSCALL) != 0)
1659 has_sig += 1;
1660 }
1661 PROC_UNLOCK(p);
1662 td->td_errno = EINTR;
1663 td->td_pflags |= TDP_NERRNO;
1664 return (EJUSTRETURN);
1665 }
1666
1667 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1668 /*
1669 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1670 * convention: libc stub passes mask, not pointer, to save a copyin.
1671 */
1672 #ifndef _SYS_SYSPROTO_H_
1673 struct osigsuspend_args {
1674 osigset_t mask;
1675 };
1676 #endif
1677 /* ARGSUSED */
1678 int
osigsuspend(struct thread * td,struct osigsuspend_args * uap)1679 osigsuspend(struct thread *td, struct osigsuspend_args *uap)
1680 {
1681 sigset_t mask;
1682
1683 OSIG2SIG(uap->mask, mask);
1684 return (kern_sigsuspend(td, mask));
1685 }
1686 #endif /* COMPAT_43 */
1687
1688 #if defined(COMPAT_43)
1689 #ifndef _SYS_SYSPROTO_H_
1690 struct osigstack_args {
1691 struct sigstack *nss;
1692 struct sigstack *oss;
1693 };
1694 #endif
1695 /* ARGSUSED */
1696 int
osigstack(struct thread * td,struct osigstack_args * uap)1697 osigstack(struct thread *td, struct osigstack_args *uap)
1698 {
1699 struct sigstack nss, oss;
1700 int error = 0;
1701
1702 if (uap->nss != NULL) {
1703 error = copyin(uap->nss, &nss, sizeof(nss));
1704 if (error)
1705 return (error);
1706 }
1707 oss.ss_sp = td->td_sigstk.ss_sp;
1708 oss.ss_onstack = sigonstack(cpu_getstack(td));
1709 if (uap->nss != NULL) {
1710 td->td_sigstk.ss_sp = nss.ss_sp;
1711 td->td_sigstk.ss_size = 0;
1712 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1713 td->td_pflags |= TDP_ALTSTACK;
1714 }
1715 if (uap->oss != NULL)
1716 error = copyout(&oss, uap->oss, sizeof(oss));
1717
1718 return (error);
1719 }
1720 #endif /* COMPAT_43 */
1721
1722 #ifndef _SYS_SYSPROTO_H_
1723 struct sigaltstack_args {
1724 stack_t *ss;
1725 stack_t *oss;
1726 };
1727 #endif
1728 /* ARGSUSED */
1729 int
sys_sigaltstack(struct thread * td,struct sigaltstack_args * uap)1730 sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap)
1731 {
1732 stack_t ss, oss;
1733 int error;
1734
1735 if (uap->ss != NULL) {
1736 error = copyin(uap->ss, &ss, sizeof(ss));
1737 if (error)
1738 return (error);
1739 }
1740 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1741 (uap->oss != NULL) ? &oss : NULL);
1742 if (error)
1743 return (error);
1744 if (uap->oss != NULL)
1745 error = copyout(&oss, uap->oss, sizeof(stack_t));
1746 return (error);
1747 }
1748
1749 int
kern_sigaltstack(struct thread * td,stack_t * ss,stack_t * oss)1750 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1751 {
1752 struct proc *p = td->td_proc;
1753 int oonstack;
1754
1755 oonstack = sigonstack(cpu_getstack(td));
1756
1757 if (oss != NULL) {
1758 *oss = td->td_sigstk;
1759 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1760 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1761 }
1762
1763 if (ss != NULL) {
1764 if (oonstack)
1765 return (EPERM);
1766 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1767 return (EINVAL);
1768 if (!(ss->ss_flags & SS_DISABLE)) {
1769 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1770 return (ENOMEM);
1771
1772 td->td_sigstk = *ss;
1773 td->td_pflags |= TDP_ALTSTACK;
1774 } else {
1775 td->td_pflags &= ~TDP_ALTSTACK;
1776 }
1777 }
1778 return (0);
1779 }
1780
1781 struct killpg1_ctx {
1782 struct thread *td;
1783 ksiginfo_t *ksi;
1784 int sig;
1785 bool sent;
1786 bool found;
1787 int ret;
1788 };
1789
1790 static void
killpg1_sendsig_locked(struct proc * p,struct killpg1_ctx * arg)1791 killpg1_sendsig_locked(struct proc *p, struct killpg1_ctx *arg)
1792 {
1793 int err;
1794
1795 err = p_cansignal(arg->td, p, arg->sig);
1796 if (err == 0 && arg->sig != 0)
1797 pksignal(p, arg->sig, arg->ksi);
1798 if (err != ESRCH)
1799 arg->found = true;
1800 if (err == 0)
1801 arg->sent = true;
1802 else if (arg->ret == 0 && err != ESRCH && err != EPERM)
1803 arg->ret = err;
1804 }
1805
1806 static void
killpg1_sendsig(struct proc * p,bool notself,struct killpg1_ctx * arg)1807 killpg1_sendsig(struct proc *p, bool notself, struct killpg1_ctx *arg)
1808 {
1809
1810 if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 ||
1811 (notself && p == arg->td->td_proc) || p->p_state == PRS_NEW)
1812 return;
1813
1814 PROC_LOCK(p);
1815 killpg1_sendsig_locked(p, arg);
1816 PROC_UNLOCK(p);
1817 }
1818
1819 static void
kill_processes_prison_cb(struct proc * p,void * arg)1820 kill_processes_prison_cb(struct proc *p, void *arg)
1821 {
1822 struct killpg1_ctx *ctx = arg;
1823
1824 if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 ||
1825 (p == ctx->td->td_proc) || p->p_state == PRS_NEW)
1826 return;
1827
1828 killpg1_sendsig_locked(p, ctx);
1829 }
1830
1831 /*
1832 * Common code for kill process group/broadcast kill.
1833 * td is the calling thread, as usual.
1834 */
1835 static int
killpg1(struct thread * td,int sig,int pgid,int all,ksiginfo_t * ksi)1836 killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1837 {
1838 struct proc *p;
1839 struct pgrp *pgrp;
1840 struct killpg1_ctx arg;
1841
1842 arg.td = td;
1843 arg.ksi = ksi;
1844 arg.sig = sig;
1845 arg.sent = false;
1846 arg.found = false;
1847 arg.ret = 0;
1848 if (all) {
1849 /*
1850 * broadcast
1851 */
1852 prison_proc_iterate(td->td_ucred->cr_prison,
1853 kill_processes_prison_cb, &arg);
1854 } else {
1855 again:
1856 sx_slock(&proctree_lock);
1857 if (pgid == 0) {
1858 /*
1859 * zero pgid means send to my process group.
1860 */
1861 pgrp = td->td_proc->p_pgrp;
1862 PGRP_LOCK(pgrp);
1863 } else {
1864 pgrp = pgfind(pgid);
1865 if (pgrp == NULL) {
1866 sx_sunlock(&proctree_lock);
1867 return (ESRCH);
1868 }
1869 }
1870 sx_sunlock(&proctree_lock);
1871 if (!sx_try_xlock(&pgrp->pg_killsx)) {
1872 PGRP_UNLOCK(pgrp);
1873 sx_xlock(&pgrp->pg_killsx);
1874 sx_xunlock(&pgrp->pg_killsx);
1875 goto again;
1876 }
1877 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1878 killpg1_sendsig(p, false, &arg);
1879 }
1880 PGRP_UNLOCK(pgrp);
1881 sx_xunlock(&pgrp->pg_killsx);
1882 }
1883 MPASS(arg.ret != 0 || arg.found || !arg.sent);
1884 if (arg.ret == 0 && !arg.sent)
1885 arg.ret = arg.found ? EPERM : ESRCH;
1886 return (arg.ret);
1887 }
1888
1889 #ifndef _SYS_SYSPROTO_H_
1890 struct kill_args {
1891 int pid;
1892 int signum;
1893 };
1894 #endif
1895 /* ARGSUSED */
1896 int
sys_kill(struct thread * td,struct kill_args * uap)1897 sys_kill(struct thread *td, struct kill_args *uap)
1898 {
1899
1900 return (kern_kill(td, uap->pid, uap->signum));
1901 }
1902
1903 int
kern_kill(struct thread * td,pid_t pid,int signum)1904 kern_kill(struct thread *td, pid_t pid, int signum)
1905 {
1906 ksiginfo_t ksi;
1907 struct proc *p;
1908 int error;
1909
1910 /*
1911 * A process in capability mode can send signals only to himself.
1912 * The main rationale behind this is that abort(3) is implemented as
1913 * kill(getpid(), SIGABRT).
1914 */
1915 if (pid != td->td_proc->p_pid) {
1916 if (CAP_TRACING(td))
1917 ktrcapfail(CAPFAIL_SIGNAL, &signum);
1918 if (IN_CAPABILITY_MODE(td))
1919 return (ECAPMODE);
1920 }
1921
1922 AUDIT_ARG_SIGNUM(signum);
1923 AUDIT_ARG_PID(pid);
1924 if ((u_int)signum > _SIG_MAXSIG)
1925 return (EINVAL);
1926
1927 ksiginfo_init(&ksi);
1928 ksi.ksi_signo = signum;
1929 ksi.ksi_code = SI_USER;
1930 ksi.ksi_pid = td->td_proc->p_pid;
1931 ksi.ksi_uid = td->td_ucred->cr_ruid;
1932
1933 if (pid > 0) {
1934 /* kill single process */
1935 if ((p = pfind_any(pid)) == NULL)
1936 return (ESRCH);
1937 AUDIT_ARG_PROCESS(p);
1938 error = p_cansignal(td, p, signum);
1939 if (error == 0 && signum)
1940 pksignal(p, signum, &ksi);
1941 PROC_UNLOCK(p);
1942 return (error);
1943 }
1944 switch (pid) {
1945 case -1: /* broadcast signal */
1946 return (killpg1(td, signum, 0, 1, &ksi));
1947 case 0: /* signal own process group */
1948 return (killpg1(td, signum, 0, 0, &ksi));
1949 default: /* negative explicit process group */
1950 return (killpg1(td, signum, -pid, 0, &ksi));
1951 }
1952 /* NOTREACHED */
1953 }
1954
1955 int
sys_pdkill(struct thread * td,struct pdkill_args * uap)1956 sys_pdkill(struct thread *td, struct pdkill_args *uap)
1957 {
1958 struct proc *p;
1959 int error;
1960
1961 AUDIT_ARG_SIGNUM(uap->signum);
1962 AUDIT_ARG_FD(uap->fd);
1963 if ((u_int)uap->signum > _SIG_MAXSIG)
1964 return (EINVAL);
1965
1966 error = procdesc_find(td, uap->fd, &cap_pdkill_rights, &p);
1967 if (error)
1968 return (error);
1969 AUDIT_ARG_PROCESS(p);
1970 error = p_cansignal(td, p, uap->signum);
1971 if (error == 0 && uap->signum)
1972 kern_psignal(p, uap->signum);
1973 PROC_UNLOCK(p);
1974 return (error);
1975 }
1976
1977 #if defined(COMPAT_43)
1978 #ifndef _SYS_SYSPROTO_H_
1979 struct okillpg_args {
1980 int pgid;
1981 int signum;
1982 };
1983 #endif
1984 /* ARGSUSED */
1985 int
okillpg(struct thread * td,struct okillpg_args * uap)1986 okillpg(struct thread *td, struct okillpg_args *uap)
1987 {
1988 ksiginfo_t ksi;
1989
1990 AUDIT_ARG_SIGNUM(uap->signum);
1991 AUDIT_ARG_PID(uap->pgid);
1992 if ((u_int)uap->signum > _SIG_MAXSIG)
1993 return (EINVAL);
1994
1995 ksiginfo_init(&ksi);
1996 ksi.ksi_signo = uap->signum;
1997 ksi.ksi_code = SI_USER;
1998 ksi.ksi_pid = td->td_proc->p_pid;
1999 ksi.ksi_uid = td->td_ucred->cr_ruid;
2000 return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
2001 }
2002 #endif /* COMPAT_43 */
2003
2004 #ifndef _SYS_SYSPROTO_H_
2005 struct sigqueue_args {
2006 pid_t pid;
2007 int signum;
2008 /* union sigval */ void *value;
2009 };
2010 #endif
2011 int
sys_sigqueue(struct thread * td,struct sigqueue_args * uap)2012 sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
2013 {
2014 union sigval sv;
2015
2016 sv.sival_ptr = uap->value;
2017
2018 return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2019 }
2020
2021 int
kern_sigqueue(struct thread * td,pid_t pid,int signumf,union sigval * value)2022 kern_sigqueue(struct thread *td, pid_t pid, int signumf, union sigval *value)
2023 {
2024 ksiginfo_t ksi;
2025 struct proc *p;
2026 struct thread *td2;
2027 u_int signum;
2028 int error;
2029
2030 signum = signumf & ~__SIGQUEUE_TID;
2031 if (signum > _SIG_MAXSIG)
2032 return (EINVAL);
2033
2034 /*
2035 * Specification says sigqueue can only send signal to
2036 * single process.
2037 */
2038 if (pid <= 0)
2039 return (EINVAL);
2040
2041 /*
2042 * A process in capability mode can send signals only to itself.
2043 */
2044 if (pid != td->td_proc->p_pid) {
2045 if (CAP_TRACING(td))
2046 ktrcapfail(CAPFAIL_SIGNAL, &signum);
2047 if (IN_CAPABILITY_MODE(td))
2048 return (ECAPMODE);
2049 }
2050
2051 if ((signumf & __SIGQUEUE_TID) == 0) {
2052 if ((p = pfind_any(pid)) == NULL)
2053 return (ESRCH);
2054 td2 = NULL;
2055 } else {
2056 p = td->td_proc;
2057 td2 = tdfind((lwpid_t)pid, p->p_pid);
2058 if (td2 == NULL)
2059 return (ESRCH);
2060 }
2061
2062 error = p_cansignal(td, p, signum);
2063 if (error == 0 && signum != 0) {
2064 ksiginfo_init(&ksi);
2065 ksi.ksi_flags = KSI_SIGQ;
2066 ksi.ksi_signo = signum;
2067 ksi.ksi_code = SI_QUEUE;
2068 ksi.ksi_pid = td->td_proc->p_pid;
2069 ksi.ksi_uid = td->td_ucred->cr_ruid;
2070 ksi.ksi_value = *value;
2071 error = tdsendsignal(p, td2, ksi.ksi_signo, &ksi);
2072 }
2073 PROC_UNLOCK(p);
2074 return (error);
2075 }
2076
2077 /*
2078 * Send a signal to a process group. If checktty is 1,
2079 * limit to members which have a controlling terminal.
2080 */
2081 void
pgsignal(struct pgrp * pgrp,int sig,int checkctty,ksiginfo_t * ksi)2082 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
2083 {
2084 struct proc *p;
2085
2086 if (pgrp) {
2087 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
2088 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
2089 PROC_LOCK(p);
2090 if (p->p_state == PRS_NORMAL &&
2091 (checkctty == 0 || p->p_flag & P_CONTROLT))
2092 pksignal(p, sig, ksi);
2093 PROC_UNLOCK(p);
2094 }
2095 }
2096 }
2097
2098 /*
2099 * Recalculate the signal mask and reset the signal disposition after
2100 * usermode frame for delivery is formed. Should be called after
2101 * mach-specific routine, because sysent->sv_sendsig() needs correct
2102 * ps_siginfo and signal mask.
2103 */
2104 static void
postsig_done(int sig,struct thread * td,struct sigacts * ps)2105 postsig_done(int sig, struct thread *td, struct sigacts *ps)
2106 {
2107 sigset_t mask;
2108
2109 mtx_assert(&ps->ps_mtx, MA_OWNED);
2110 td->td_ru.ru_nsignals++;
2111 mask = ps->ps_catchmask[_SIG_IDX(sig)];
2112 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2113 SIGADDSET(mask, sig);
2114 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
2115 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
2116 if (SIGISMEMBER(ps->ps_sigreset, sig))
2117 sigdflt(ps, sig);
2118 }
2119
2120 /*
2121 * Send a signal caused by a trap to the current thread. If it will be
2122 * caught immediately, deliver it with correct code. Otherwise, post it
2123 * normally.
2124 */
2125 void
trapsignal(struct thread * td,ksiginfo_t * ksi)2126 trapsignal(struct thread *td, ksiginfo_t *ksi)
2127 {
2128 struct sigacts *ps;
2129 struct proc *p;
2130 sigset_t sigmask;
2131 int sig;
2132
2133 p = td->td_proc;
2134 sig = ksi->ksi_signo;
2135 KASSERT(_SIG_VALID(sig), ("invalid signal"));
2136
2137 sigfastblock_fetch(td);
2138 PROC_LOCK(p);
2139 ps = p->p_sigacts;
2140 mtx_lock(&ps->ps_mtx);
2141 sigmask = td->td_sigmask;
2142 if (td->td_sigblock_val != 0)
2143 SIGSETOR(sigmask, fastblock_mask);
2144 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
2145 !SIGISMEMBER(sigmask, sig)) {
2146 #ifdef KTRACE
2147 if (KTRPOINT(curthread, KTR_PSIG))
2148 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
2149 &td->td_sigmask, ksi->ksi_code);
2150 #endif
2151 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
2152 ksi, &td->td_sigmask);
2153 postsig_done(sig, td, ps);
2154 mtx_unlock(&ps->ps_mtx);
2155 } else {
2156 /*
2157 * Avoid a possible infinite loop if the thread
2158 * masking the signal or process is ignoring the
2159 * signal.
2160 */
2161 if (kern_forcesigexit && (SIGISMEMBER(sigmask, sig) ||
2162 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
2163 SIGDELSET(td->td_sigmask, sig);
2164 SIGDELSET(ps->ps_sigcatch, sig);
2165 SIGDELSET(ps->ps_sigignore, sig);
2166 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2167 td->td_pflags &= ~TDP_SIGFASTBLOCK;
2168 td->td_sigblock_val = 0;
2169 }
2170 mtx_unlock(&ps->ps_mtx);
2171 p->p_sig = sig; /* XXX to verify code */
2172 tdsendsignal(p, td, sig, ksi);
2173 }
2174 PROC_UNLOCK(p);
2175 }
2176
2177 static struct thread *
sigtd(struct proc * p,int sig,bool fast_sigblock)2178 sigtd(struct proc *p, int sig, bool fast_sigblock)
2179 {
2180 struct thread *td, *signal_td;
2181
2182 PROC_LOCK_ASSERT(p, MA_OWNED);
2183 MPASS(!fast_sigblock || p == curproc);
2184
2185 /*
2186 * Check if current thread can handle the signal without
2187 * switching context to another thread.
2188 */
2189 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig) &&
2190 (!fast_sigblock || curthread->td_sigblock_val == 0))
2191 return (curthread);
2192
2193 /* Find a non-stopped thread that does not mask the signal. */
2194 signal_td = NULL;
2195 FOREACH_THREAD_IN_PROC(p, td) {
2196 if (!SIGISMEMBER(td->td_sigmask, sig) && (!fast_sigblock ||
2197 td != curthread || td->td_sigblock_val == 0) &&
2198 (td->td_flags & TDF_BOUNDARY) == 0) {
2199 signal_td = td;
2200 break;
2201 }
2202 }
2203 /* Select random (first) thread if no better match was found. */
2204 if (signal_td == NULL)
2205 signal_td = FIRST_THREAD_IN_PROC(p);
2206 return (signal_td);
2207 }
2208
2209 /*
2210 * Send the signal to the process. If the signal has an action, the action
2211 * is usually performed by the target process rather than the caller; we add
2212 * the signal to the set of pending signals for the process.
2213 *
2214 * Exceptions:
2215 * o When a stop signal is sent to a sleeping process that takes the
2216 * default action, the process is stopped without awakening it.
2217 * o SIGCONT restarts stopped processes (or puts them back to sleep)
2218 * regardless of the signal action (eg, blocked or ignored).
2219 *
2220 * Other ignored signals are discarded immediately.
2221 *
2222 * NB: This function may be entered from the debugger via the "kill" DDB
2223 * command. There is little that can be done to mitigate the possibly messy
2224 * side effects of this unwise possibility.
2225 */
2226 void
kern_psignal(struct proc * p,int sig)2227 kern_psignal(struct proc *p, int sig)
2228 {
2229 ksiginfo_t ksi;
2230
2231 ksiginfo_init(&ksi);
2232 ksi.ksi_signo = sig;
2233 ksi.ksi_code = SI_KERNEL;
2234 (void) tdsendsignal(p, NULL, sig, &ksi);
2235 }
2236
2237 int
pksignal(struct proc * p,int sig,ksiginfo_t * ksi)2238 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
2239 {
2240
2241 return (tdsendsignal(p, NULL, sig, ksi));
2242 }
2243
2244 /* Utility function for finding a thread to send signal event to. */
2245 int
sigev_findtd(struct proc * p,struct sigevent * sigev,struct thread ** ttd)2246 sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **ttd)
2247 {
2248 struct thread *td;
2249
2250 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
2251 td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
2252 if (td == NULL)
2253 return (ESRCH);
2254 *ttd = td;
2255 } else {
2256 *ttd = NULL;
2257 PROC_LOCK(p);
2258 }
2259 return (0);
2260 }
2261
2262 void
tdsignal(struct thread * td,int sig)2263 tdsignal(struct thread *td, int sig)
2264 {
2265 ksiginfo_t ksi;
2266
2267 ksiginfo_init(&ksi);
2268 ksi.ksi_signo = sig;
2269 ksi.ksi_code = SI_KERNEL;
2270 (void) tdsendsignal(td->td_proc, td, sig, &ksi);
2271 }
2272
2273 void
tdksignal(struct thread * td,int sig,ksiginfo_t * ksi)2274 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2275 {
2276
2277 (void) tdsendsignal(td->td_proc, td, sig, ksi);
2278 }
2279
2280 static void
sig_sleepq_abort(struct thread * td,int intrval)2281 sig_sleepq_abort(struct thread *td, int intrval)
2282 {
2283 THREAD_LOCK_ASSERT(td, MA_OWNED);
2284
2285 if (intrval == 0 && (td->td_flags & TDF_SIGWAIT) == 0)
2286 thread_unlock(td);
2287 else
2288 sleepq_abort(td, intrval);
2289 }
2290
2291 int
tdsendsignal(struct proc * p,struct thread * td,int sig,ksiginfo_t * ksi)2292 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2293 {
2294 sig_t action;
2295 sigqueue_t *sigqueue;
2296 struct sigacts *ps;
2297 int intrval, prop, ret;
2298
2299 MPASS(td == NULL || p == td->td_proc);
2300 PROC_LOCK_ASSERT(p, MA_OWNED);
2301
2302 if (!_SIG_VALID(sig))
2303 panic("%s(): invalid signal %d", __func__, sig);
2304
2305 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
2306
2307 /*
2308 * IEEE Std 1003.1-2001: return success when killing a zombie.
2309 */
2310 if (p->p_state == PRS_ZOMBIE) {
2311 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2312 ksiginfo_tryfree(ksi);
2313 return (0);
2314 }
2315
2316 ps = p->p_sigacts;
2317 KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig);
2318 prop = sigprop(sig);
2319
2320 if (td == NULL) {
2321 td = sigtd(p, sig, false);
2322 sigqueue = &p->p_sigqueue;
2323 } else
2324 sigqueue = &td->td_sigqueue;
2325
2326 SDT_PROBE3(proc, , , signal__send, td, p, sig);
2327
2328 /*
2329 * If the signal is being ignored, then we forget about it
2330 * immediately, except when the target process executes
2331 * sigwait(). (Note: we don't set SIGCONT in ps_sigignore,
2332 * and if it is set to SIG_IGN, action will be SIG_DFL here.)
2333 */
2334 mtx_lock(&ps->ps_mtx);
2335 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2336 if (kern_sig_discard_ign &&
2337 (p->p_sysent->sv_flags & SV_SIG_DISCIGN) == 0) {
2338 SDT_PROBE3(proc, , , signal__discard, td, p, sig);
2339
2340 mtx_unlock(&ps->ps_mtx);
2341 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2342 ksiginfo_tryfree(ksi);
2343 return (0);
2344 } else {
2345 action = SIG_CATCH;
2346 intrval = 0;
2347 }
2348 } else {
2349 if (SIGISMEMBER(td->td_sigmask, sig))
2350 action = SIG_HOLD;
2351 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2352 action = SIG_CATCH;
2353 else
2354 action = SIG_DFL;
2355 if (SIGISMEMBER(ps->ps_sigintr, sig))
2356 intrval = EINTR;
2357 else
2358 intrval = ERESTART;
2359 }
2360 mtx_unlock(&ps->ps_mtx);
2361
2362 if (prop & SIGPROP_CONT)
2363 sigqueue_delete_stopmask_proc(p);
2364 else if (prop & SIGPROP_STOP) {
2365 if (pt_attach_transparent &&
2366 (p->p_flag & P_TRACED) != 0 &&
2367 (p->p_flag2 & P2_PTRACE_FSTP) != 0) {
2368 PROC_SLOCK(p);
2369 sig_handle_first_stop(NULL, p, sig);
2370 PROC_SUNLOCK(p);
2371 return (0);
2372 }
2373
2374 /*
2375 * If sending a tty stop signal to a member of an orphaned
2376 * process group, discard the signal here if the action
2377 * is default; don't stop the process below if sleeping,
2378 * and don't clear any pending SIGCONT.
2379 */
2380 if ((prop & SIGPROP_TTYSTOP) != 0 &&
2381 (p->p_pgrp->pg_flags & PGRP_ORPHANED) != 0 &&
2382 action == SIG_DFL) {
2383 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2384 ksiginfo_tryfree(ksi);
2385 return (0);
2386 }
2387 sigqueue_delete_proc(p, SIGCONT);
2388 if (p->p_flag & P_CONTINUED) {
2389 p->p_flag &= ~P_CONTINUED;
2390 PROC_LOCK(p->p_pptr);
2391 sigqueue_take(p->p_ksi);
2392 PROC_UNLOCK(p->p_pptr);
2393 }
2394 }
2395
2396 ret = sigqueue_add(sigqueue, sig, ksi);
2397 if (ret != 0)
2398 return (ret);
2399 signotify(td);
2400 /*
2401 * Defer further processing for signals which are held,
2402 * except that stopped processes must be continued by SIGCONT.
2403 */
2404 if (action == SIG_HOLD &&
2405 !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG)))
2406 return (0);
2407
2408 /*
2409 * Some signals have a process-wide effect and a per-thread
2410 * component. Most processing occurs when the process next
2411 * tries to cross the user boundary, however there are some
2412 * times when processing needs to be done immediately, such as
2413 * waking up threads so that they can cross the user boundary.
2414 * We try to do the per-process part here.
2415 */
2416 if (P_SHOULDSTOP(p)) {
2417 KASSERT(!(p->p_flag & P_WEXIT),
2418 ("signal to stopped but exiting process"));
2419 if (sig == SIGKILL) {
2420 /*
2421 * If traced process is already stopped,
2422 * then no further action is necessary.
2423 */
2424 if (p->p_flag & P_TRACED)
2425 return (0);
2426 /*
2427 * SIGKILL sets process running.
2428 * It will die elsewhere.
2429 * All threads must be restarted.
2430 */
2431 p->p_flag &= ~P_STOPPED_SIG;
2432 goto runfast;
2433 }
2434
2435 if (prop & SIGPROP_CONT) {
2436 /*
2437 * If traced process is already stopped,
2438 * then no further action is necessary.
2439 */
2440 if (p->p_flag & P_TRACED)
2441 return (0);
2442 /*
2443 * If SIGCONT is default (or ignored), we continue the
2444 * process but don't leave the signal in sigqueue as
2445 * it has no further action. If SIGCONT is held, we
2446 * continue the process and leave the signal in
2447 * sigqueue. If the process catches SIGCONT, let it
2448 * handle the signal itself. If it isn't waiting on
2449 * an event, it goes back to run state.
2450 * Otherwise, process goes back to sleep state.
2451 */
2452 p->p_flag &= ~P_STOPPED_SIG;
2453 PROC_SLOCK(p);
2454 if (p->p_numthreads == p->p_suspcount) {
2455 PROC_SUNLOCK(p);
2456 PROC_LOCK(p->p_pptr);
2457 childproc_continued(p);
2458 PROC_UNLOCK(p->p_pptr);
2459 PROC_SLOCK(p);
2460 }
2461 if (action == SIG_DFL) {
2462 thread_unsuspend(p);
2463 PROC_SUNLOCK(p);
2464 sigqueue_delete(sigqueue, sig);
2465 goto out_cont;
2466 }
2467 if (action == SIG_CATCH) {
2468 /*
2469 * The process wants to catch it so it needs
2470 * to run at least one thread, but which one?
2471 */
2472 PROC_SUNLOCK(p);
2473 goto runfast;
2474 }
2475 /*
2476 * The signal is not ignored or caught.
2477 */
2478 thread_unsuspend(p);
2479 PROC_SUNLOCK(p);
2480 goto out_cont;
2481 }
2482
2483 if (prop & SIGPROP_STOP) {
2484 /*
2485 * If traced process is already stopped,
2486 * then no further action is necessary.
2487 */
2488 if (p->p_flag & P_TRACED)
2489 return (0);
2490 /*
2491 * Already stopped, don't need to stop again
2492 * (If we did the shell could get confused).
2493 * Just make sure the signal STOP bit set.
2494 */
2495 p->p_flag |= P_STOPPED_SIG;
2496 sigqueue_delete(sigqueue, sig);
2497 return (0);
2498 }
2499
2500 /*
2501 * All other kinds of signals:
2502 * If a thread is sleeping interruptibly, simulate a
2503 * wakeup so that when it is continued it will be made
2504 * runnable and can look at the signal. However, don't make
2505 * the PROCESS runnable, leave it stopped.
2506 * It may run a bit until it hits a thread_suspend_check().
2507 */
2508 PROC_SLOCK(p);
2509 thread_lock(td);
2510 if (TD_CAN_ABORT(td))
2511 sig_sleepq_abort(td, intrval);
2512 else
2513 thread_unlock(td);
2514 PROC_SUNLOCK(p);
2515 return (0);
2516 /*
2517 * Mutexes are short lived. Threads waiting on them will
2518 * hit thread_suspend_check() soon.
2519 */
2520 } else if (p->p_state == PRS_NORMAL) {
2521 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2522 tdsigwakeup(td, sig, action, intrval);
2523 return (0);
2524 }
2525
2526 MPASS(action == SIG_DFL);
2527
2528 if (prop & SIGPROP_STOP) {
2529 if (p->p_flag & (P_PPWAIT|P_WEXIT))
2530 return (0);
2531 p->p_flag |= P_STOPPED_SIG;
2532 p->p_xsig = sig;
2533 PROC_SLOCK(p);
2534 sig_suspend_threads(td, p);
2535 if (p->p_numthreads == p->p_suspcount) {
2536 /*
2537 * only thread sending signal to another
2538 * process can reach here, if thread is sending
2539 * signal to its process, because thread does
2540 * not suspend itself here, p_numthreads
2541 * should never be equal to p_suspcount.
2542 */
2543 thread_stopped(p);
2544 PROC_SUNLOCK(p);
2545 sigqueue_delete_proc(p, p->p_xsig);
2546 } else
2547 PROC_SUNLOCK(p);
2548 return (0);
2549 }
2550 } else {
2551 /* Not in "NORMAL" state. discard the signal. */
2552 sigqueue_delete(sigqueue, sig);
2553 return (0);
2554 }
2555
2556 /*
2557 * The process is not stopped so we need to apply the signal to all the
2558 * running threads.
2559 */
2560 runfast:
2561 tdsigwakeup(td, sig, action, intrval);
2562 PROC_SLOCK(p);
2563 thread_unsuspend(p);
2564 PROC_SUNLOCK(p);
2565 out_cont:
2566 itimer_proc_continue(p);
2567 kqtimer_proc_continue(p);
2568
2569 return (0);
2570 }
2571
2572 /*
2573 * The force of a signal has been directed against a single
2574 * thread. We need to see what we can do about knocking it
2575 * out of any sleep it may be in etc.
2576 */
2577 static void
tdsigwakeup(struct thread * td,int sig,sig_t action,int intrval)2578 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2579 {
2580 struct proc *p = td->td_proc;
2581 int prop;
2582
2583 PROC_LOCK_ASSERT(p, MA_OWNED);
2584 prop = sigprop(sig);
2585
2586 PROC_SLOCK(p);
2587 thread_lock(td);
2588 /*
2589 * Bring the priority of a thread up if we want it to get
2590 * killed in this lifetime. Be careful to avoid bumping the
2591 * priority of the idle thread, since we still allow to signal
2592 * kernel processes.
2593 */
2594 if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 &&
2595 td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2596 sched_prio(td, PUSER);
2597 if (TD_ON_SLEEPQ(td)) {
2598 /*
2599 * If thread is sleeping uninterruptibly
2600 * we can't interrupt the sleep... the signal will
2601 * be noticed when the process returns through
2602 * trap() or syscall().
2603 */
2604 if ((td->td_flags & TDF_SINTR) == 0)
2605 goto out;
2606 /*
2607 * If SIGCONT is default (or ignored) and process is
2608 * asleep, we are finished; the process should not
2609 * be awakened.
2610 */
2611 if ((prop & SIGPROP_CONT) && action == SIG_DFL) {
2612 thread_unlock(td);
2613 PROC_SUNLOCK(p);
2614 sigqueue_delete(&p->p_sigqueue, sig);
2615 /*
2616 * It may be on either list in this state.
2617 * Remove from both for now.
2618 */
2619 sigqueue_delete(&td->td_sigqueue, sig);
2620 return;
2621 }
2622
2623 /*
2624 * Don't awaken a sleeping thread for SIGSTOP if the
2625 * STOP signal is deferred.
2626 */
2627 if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY |
2628 TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
2629 goto out;
2630
2631 /*
2632 * Give low priority threads a better chance to run.
2633 */
2634 if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2635 sched_prio(td, PUSER);
2636
2637 sig_sleepq_abort(td, intrval);
2638 PROC_SUNLOCK(p);
2639 return;
2640 }
2641
2642 /*
2643 * Other states do nothing with the signal immediately,
2644 * other than kicking ourselves if we are running.
2645 * It will either never be noticed, or noticed very soon.
2646 */
2647 #ifdef SMP
2648 if (TD_IS_RUNNING(td) && td != curthread)
2649 forward_signal(td);
2650 #endif
2651
2652 out:
2653 PROC_SUNLOCK(p);
2654 thread_unlock(td);
2655 }
2656
2657 static void
ptrace_coredumpreq(struct thread * td,struct proc * p,struct thr_coredump_req * tcq)2658 ptrace_coredumpreq(struct thread *td, struct proc *p,
2659 struct thr_coredump_req *tcq)
2660 {
2661 struct coredump_vnode_ctx wctx;
2662 struct coredump_writer cdw;
2663 void *rl_cookie;
2664
2665 if (p->p_sysent->sv_coredump == NULL) {
2666 tcq->tc_error = ENOSYS;
2667 return;
2668 }
2669
2670 memset(&wctx, 0, sizeof(wctx));
2671 wctx.vp = tcq->tc_vp;
2672 wctx.fcred = NOCRED;
2673
2674 memset(&cdw, 0, sizeof(wctx));
2675 cdw.ctx = &wctx;
2676 cdw.write_fn = core_vn_write;
2677 cdw.extend_fn = core_vn_extend;
2678
2679 rl_cookie = vn_rangelock_wlock(tcq->tc_vp, 0, OFF_MAX);
2680 tcq->tc_error = p->p_sysent->sv_coredump(td, &cdw,
2681 tcq->tc_limit, tcq->tc_flags);
2682 vn_rangelock_unlock(tcq->tc_vp, rl_cookie);
2683 }
2684
2685 static void
ptrace_syscallreq(struct thread * td,struct proc * p,struct thr_syscall_req * tsr)2686 ptrace_syscallreq(struct thread *td, struct proc *p,
2687 struct thr_syscall_req *tsr)
2688 {
2689 struct sysentvec *sv;
2690 struct sysent *se;
2691 register_t rv_saved[2];
2692 unsigned int sc;
2693 int error, nerror;
2694 bool audited, sy_thr_static;
2695
2696 sc = tsr->ts_sa.code;
2697 if (sc == SYS_syscall || sc == SYS___syscall) {
2698 if (tsr->ts_nargs == 0) {
2699 tsr->ts_ret.sr_error = EINVAL;
2700 return;
2701 }
2702 sc = tsr->ts_sa.args[0];
2703 memmove(&tsr->ts_sa.args[0], &tsr->ts_sa.args[1],
2704 sizeof(register_t) * (tsr->ts_nargs - 1));
2705 }
2706
2707 sv = p->p_sysent;
2708 if (sv->sv_table == NULL || sc >= sv->sv_size) {
2709 tsr->ts_ret.sr_error = ENOSYS;
2710 return;
2711 }
2712 tsr->ts_sa.callp = se = &sv->sv_table[sc];
2713
2714 VM_CNT_INC(v_syscall);
2715 td->td_pticks = 0;
2716 if (__predict_false(td->td_cowgen != atomic_load_int(
2717 &td->td_proc->p_cowgen)))
2718 thread_cow_update(td);
2719
2720 td->td_sa = tsr->ts_sa;
2721
2722 #ifdef CAPABILITY_MODE
2723 if ((se->sy_flags & SYF_CAPENABLED) == 0) {
2724 if (CAP_TRACING(td))
2725 ktrcapfail(CAPFAIL_SYSCALL, NULL);
2726 if (IN_CAPABILITY_MODE(td)) {
2727 tsr->ts_ret.sr_error = ECAPMODE;
2728 return;
2729 }
2730 }
2731 #endif
2732
2733 sy_thr_static = (se->sy_thrcnt & SY_THR_STATIC) != 0;
2734 audited = AUDIT_SYSCALL_ENTER(sc, td) != 0;
2735
2736 if (!sy_thr_static) {
2737 error = syscall_thread_enter(td, &se);
2738 sy_thr_static = (se->sy_thrcnt & SY_THR_STATIC) != 0;
2739 if (error != 0) {
2740 tsr->ts_ret.sr_error = error;
2741 return;
2742 }
2743 }
2744
2745 rv_saved[0] = td->td_retval[0];
2746 rv_saved[1] = td->td_retval[1];
2747 nerror = td->td_errno;
2748 td->td_retval[0] = 0;
2749 td->td_retval[1] = 0;
2750
2751 #ifdef KDTRACE_HOOKS
2752 if (se->sy_entry != 0)
2753 (*systrace_probe_func)(&tsr->ts_sa, SYSTRACE_ENTRY, 0);
2754 #endif
2755 tsr->ts_ret.sr_error = se->sy_call(td, tsr->ts_sa.args);
2756 #ifdef KDTRACE_HOOKS
2757 if (se->sy_return != 0)
2758 (*systrace_probe_func)(&tsr->ts_sa, SYSTRACE_RETURN,
2759 tsr->ts_ret.sr_error != 0 ? -1 : td->td_retval[0]);
2760 #endif
2761
2762 tsr->ts_ret.sr_retval[0] = td->td_retval[0];
2763 tsr->ts_ret.sr_retval[1] = td->td_retval[1];
2764 td->td_retval[0] = rv_saved[0];
2765 td->td_retval[1] = rv_saved[1];
2766 td->td_errno = nerror;
2767
2768 if (audited)
2769 AUDIT_SYSCALL_EXIT(error, td);
2770 if (!sy_thr_static)
2771 syscall_thread_exit(td, se);
2772 }
2773
2774 static void
ptrace_remotereq(struct thread * td,int flag)2775 ptrace_remotereq(struct thread *td, int flag)
2776 {
2777 struct proc *p;
2778
2779 MPASS(td == curthread);
2780 p = td->td_proc;
2781 PROC_LOCK_ASSERT(p, MA_OWNED);
2782 if ((td->td_dbgflags & flag) == 0)
2783 return;
2784 KASSERT((p->p_flag & P_STOPPED_TRACE) != 0, ("not stopped"));
2785 KASSERT(td->td_remotereq != NULL, ("td_remotereq is NULL"));
2786
2787 PROC_UNLOCK(p);
2788 switch (flag) {
2789 case TDB_COREDUMPREQ:
2790 ptrace_coredumpreq(td, p, td->td_remotereq);
2791 break;
2792 case TDB_SCREMOTEREQ:
2793 ptrace_syscallreq(td, p, td->td_remotereq);
2794 break;
2795 default:
2796 __unreachable();
2797 }
2798 PROC_LOCK(p);
2799
2800 MPASS((td->td_dbgflags & flag) != 0);
2801 td->td_dbgflags &= ~flag;
2802 td->td_remotereq = NULL;
2803 wakeup(p);
2804 }
2805
2806 /*
2807 * Suspend threads of the process p, either by directly setting the
2808 * inhibitor for the thread sleeping interruptibly, or by making the
2809 * thread suspend at the userspace boundary by scheduling a suspend AST.
2810 *
2811 * Returns true if some threads were suspended directly from the
2812 * sleeping state, and false if all threads are forced to process AST.
2813 */
2814 static bool
sig_suspend_threads(struct thread * td,struct proc * p)2815 sig_suspend_threads(struct thread *td, struct proc *p)
2816 {
2817 struct thread *td2;
2818 bool res;
2819
2820 PROC_LOCK_ASSERT(p, MA_OWNED);
2821 PROC_SLOCK_ASSERT(p, MA_OWNED);
2822
2823 res = false;
2824 FOREACH_THREAD_IN_PROC(p, td2) {
2825 thread_lock(td2);
2826 ast_sched_locked(td2, TDA_SUSPEND);
2827 if (TD_IS_SLEEPING(td2) && (td2->td_flags & TDF_SINTR) != 0) {
2828 if (td2->td_flags & TDF_SBDRY) {
2829 /*
2830 * Once a thread is asleep with
2831 * TDF_SBDRY and without TDF_SERESTART
2832 * or TDF_SEINTR set, it should never
2833 * become suspended due to this check.
2834 */
2835 KASSERT(!TD_IS_SUSPENDED(td2),
2836 ("thread with deferred stops suspended"));
2837 if (TD_SBDRY_INTR(td2)) {
2838 sleepq_abort(td2, TD_SBDRY_ERRNO(td2));
2839 continue;
2840 }
2841 } else if (!TD_IS_SUSPENDED(td2)) {
2842 thread_suspend_one(td2);
2843 res = true;
2844 }
2845 } else if (!TD_IS_SUSPENDED(td2)) {
2846 #ifdef SMP
2847 if (TD_IS_RUNNING(td2) && td2 != td)
2848 forward_signal(td2);
2849 #endif
2850 }
2851 thread_unlock(td2);
2852 }
2853 return (res);
2854 }
2855
2856 static void
sig_handle_first_stop(struct thread * td,struct proc * p,int sig)2857 sig_handle_first_stop(struct thread *td, struct proc *p, int sig)
2858 {
2859 if (td != NULL && (td->td_dbgflags & TDB_FSTP) == 0 &&
2860 ((p->p_flag2 & P2_PTRACE_FSTP) != 0 || p->p_xthread != NULL))
2861 return;
2862
2863 p->p_xsig = sig;
2864 p->p_xthread = td;
2865
2866 /*
2867 * If we are on sleepqueue already, let sleepqueue
2868 * code decide if it needs to go sleep after attach.
2869 */
2870 if (td != NULL && td->td_wchan == NULL)
2871 td->td_dbgflags &= ~TDB_FSTP;
2872
2873 p->p_flag2 &= ~P2_PTRACE_FSTP;
2874 p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE;
2875 if (sig_suspend_threads(td, p) && td == NULL)
2876 thread_stopped(p);
2877 }
2878
2879 /*
2880 * Stop the process for an event deemed interesting to the debugger. If si is
2881 * non-NULL, this is a signal exchange; the new signal requested by the
2882 * debugger will be returned for handling. If si is NULL, this is some other
2883 * type of interesting event. The debugger may request a signal be delivered in
2884 * that case as well, however it will be deferred until it can be handled.
2885 */
2886 int
ptracestop(struct thread * td,int sig,ksiginfo_t * si)2887 ptracestop(struct thread *td, int sig, ksiginfo_t *si)
2888 {
2889 struct proc *p = td->td_proc;
2890 struct thread *td2;
2891 ksiginfo_t ksi;
2892
2893 PROC_LOCK_ASSERT(p, MA_OWNED);
2894 KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
2895 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2896 &p->p_mtx.lock_object, "Stopping for traced signal");
2897
2898 td->td_xsig = sig;
2899
2900 if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) {
2901 td->td_dbgflags |= TDB_XSIG;
2902 CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d",
2903 td->td_tid, p->p_pid, td->td_dbgflags, sig);
2904 PROC_SLOCK(p);
2905 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2906 if (P_KILLED(p)) {
2907 /*
2908 * Ensure that, if we've been PT_KILLed, the
2909 * exit status reflects that. Another thread
2910 * may also be in ptracestop(), having just
2911 * received the SIGKILL, but this thread was
2912 * unsuspended first.
2913 */
2914 td->td_dbgflags &= ~TDB_XSIG;
2915 td->td_xsig = SIGKILL;
2916 p->p_ptevents = 0;
2917 break;
2918 }
2919 if (p->p_flag & P_SINGLE_EXIT &&
2920 !(td->td_dbgflags & TDB_EXIT)) {
2921 /*
2922 * Ignore ptrace stops except for thread exit
2923 * events when the process exits.
2924 */
2925 td->td_dbgflags &= ~TDB_XSIG;
2926 PROC_SUNLOCK(p);
2927 return (0);
2928 }
2929
2930 /*
2931 * Make wait(2) work. Ensure that right after the
2932 * attach, the thread which was decided to become the
2933 * leader of attach gets reported to the waiter.
2934 * Otherwise, just avoid overwriting another thread's
2935 * assignment to p_xthread. If another thread has
2936 * already set p_xthread, the current thread will get
2937 * a chance to report itself upon the next iteration.
2938 */
2939 sig_handle_first_stop(td, p, sig);
2940
2941 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
2942 td->td_dbgflags &= ~TDB_STOPATFORK;
2943 }
2944 stopme:
2945 td->td_dbgflags |= TDB_SSWITCH;
2946 thread_suspend_switch(td, p);
2947 td->td_dbgflags &= ~TDB_SSWITCH;
2948 if ((td->td_dbgflags & (TDB_COREDUMPREQ |
2949 TDB_SCREMOTEREQ)) != 0) {
2950 MPASS((td->td_dbgflags & (TDB_COREDUMPREQ |
2951 TDB_SCREMOTEREQ)) !=
2952 (TDB_COREDUMPREQ | TDB_SCREMOTEREQ));
2953 PROC_SUNLOCK(p);
2954 ptrace_remotereq(td, td->td_dbgflags &
2955 (TDB_COREDUMPREQ | TDB_SCREMOTEREQ));
2956 PROC_SLOCK(p);
2957 goto stopme;
2958 }
2959 if (p->p_xthread == td)
2960 p->p_xthread = NULL;
2961 if (!(p->p_flag & P_TRACED))
2962 break;
2963 if (td->td_dbgflags & TDB_SUSPEND) {
2964 if (p->p_flag & P_SINGLE_EXIT)
2965 break;
2966 goto stopme;
2967 }
2968 }
2969 PROC_SUNLOCK(p);
2970 }
2971
2972 if (si != NULL && sig == td->td_xsig) {
2973 /* Parent wants us to take the original signal unchanged. */
2974 si->ksi_flags |= KSI_HEAD;
2975 if (sigqueue_add(&td->td_sigqueue, sig, si) != 0)
2976 si->ksi_signo = 0;
2977 } else if (td->td_xsig != 0) {
2978 /*
2979 * If parent wants us to take a new signal, then it will leave
2980 * it in td->td_xsig; otherwise we just look for signals again.
2981 */
2982 ksiginfo_init(&ksi);
2983 ksi.ksi_signo = td->td_xsig;
2984 ksi.ksi_flags |= KSI_PTRACE;
2985 td2 = sigtd(p, td->td_xsig, false);
2986 tdsendsignal(p, td2, td->td_xsig, &ksi);
2987 if (td != td2)
2988 return (0);
2989 }
2990
2991 return (td->td_xsig);
2992 }
2993
2994 static void
reschedule_signals(struct proc * p,sigset_t block,int flags)2995 reschedule_signals(struct proc *p, sigset_t block, int flags)
2996 {
2997 struct sigacts *ps;
2998 struct thread *td;
2999 int sig;
3000 bool fastblk, pslocked;
3001
3002 PROC_LOCK_ASSERT(p, MA_OWNED);
3003 ps = p->p_sigacts;
3004 pslocked = (flags & SIGPROCMASK_PS_LOCKED) != 0;
3005 mtx_assert(&ps->ps_mtx, pslocked ? MA_OWNED : MA_NOTOWNED);
3006 if (SIGISEMPTY(p->p_siglist))
3007 return;
3008 SIGSETAND(block, p->p_siglist);
3009 fastblk = (flags & SIGPROCMASK_FASTBLK) != 0;
3010 SIG_FOREACH(sig, &block) {
3011 td = sigtd(p, sig, fastblk);
3012
3013 /*
3014 * If sigtd() selected us despite sigfastblock is
3015 * blocking, do not activate AST or wake us, to avoid
3016 * loop in AST handler.
3017 */
3018 if (fastblk && td == curthread)
3019 continue;
3020
3021 signotify(td);
3022 if (!pslocked)
3023 mtx_lock(&ps->ps_mtx);
3024 if (p->p_flag & P_TRACED ||
3025 (SIGISMEMBER(ps->ps_sigcatch, sig) &&
3026 !SIGISMEMBER(td->td_sigmask, sig))) {
3027 tdsigwakeup(td, sig, SIG_CATCH,
3028 (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
3029 ERESTART));
3030 }
3031 if (!pslocked)
3032 mtx_unlock(&ps->ps_mtx);
3033 }
3034 }
3035
3036 void
tdsigcleanup(struct thread * td)3037 tdsigcleanup(struct thread *td)
3038 {
3039 struct proc *p;
3040 sigset_t unblocked;
3041
3042 p = td->td_proc;
3043 PROC_LOCK_ASSERT(p, MA_OWNED);
3044
3045 sigqueue_flush(&td->td_sigqueue);
3046 if (p->p_numthreads == 1)
3047 return;
3048
3049 /*
3050 * Since we cannot handle signals, notify signal post code
3051 * about this by filling the sigmask.
3052 *
3053 * Also, if needed, wake up thread(s) that do not block the
3054 * same signals as the exiting thread, since the thread might
3055 * have been selected for delivery and woken up.
3056 */
3057 SIGFILLSET(unblocked);
3058 SIGSETNAND(unblocked, td->td_sigmask);
3059 SIGFILLSET(td->td_sigmask);
3060 reschedule_signals(p, unblocked, 0);
3061
3062 }
3063
3064 static int
sigdeferstop_curr_flags(int cflags)3065 sigdeferstop_curr_flags(int cflags)
3066 {
3067
3068 MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 ||
3069 (cflags & TDF_SBDRY) != 0);
3070 return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART));
3071 }
3072
3073 /*
3074 * Defer the delivery of SIGSTOP for the current thread, according to
3075 * the requested mode. Returns previous flags, which must be restored
3076 * by sigallowstop().
3077 *
3078 * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and
3079 * cleared by the current thread, which allow the lock-less read-only
3080 * accesses below.
3081 */
3082 int
sigdeferstop_impl(int mode)3083 sigdeferstop_impl(int mode)
3084 {
3085 struct thread *td;
3086 int cflags, nflags;
3087
3088 td = curthread;
3089 cflags = sigdeferstop_curr_flags(td->td_flags);
3090 switch (mode) {
3091 case SIGDEFERSTOP_NOP:
3092 nflags = cflags;
3093 break;
3094 case SIGDEFERSTOP_OFF:
3095 nflags = 0;
3096 break;
3097 case SIGDEFERSTOP_SILENT:
3098 nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART);
3099 break;
3100 case SIGDEFERSTOP_EINTR:
3101 nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART;
3102 break;
3103 case SIGDEFERSTOP_ERESTART:
3104 nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR;
3105 break;
3106 default:
3107 panic("sigdeferstop: invalid mode %x", mode);
3108 break;
3109 }
3110 if (cflags == nflags)
3111 return (SIGDEFERSTOP_VAL_NCHG);
3112 thread_lock(td);
3113 td->td_flags = (td->td_flags & ~cflags) | nflags;
3114 thread_unlock(td);
3115 return (cflags);
3116 }
3117
3118 /*
3119 * Restores the STOP handling mode, typically permitting the delivery
3120 * of SIGSTOP for the current thread. This does not immediately
3121 * suspend if a stop was posted. Instead, the thread will suspend
3122 * either via ast() or a subsequent interruptible sleep.
3123 */
3124 void
sigallowstop_impl(int prev)3125 sigallowstop_impl(int prev)
3126 {
3127 struct thread *td;
3128 int cflags;
3129
3130 KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop"));
3131 KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0,
3132 ("sigallowstop: incorrect previous mode %x", prev));
3133 td = curthread;
3134 cflags = sigdeferstop_curr_flags(td->td_flags);
3135 if (cflags != prev) {
3136 thread_lock(td);
3137 td->td_flags = (td->td_flags & ~cflags) | prev;
3138 thread_unlock(td);
3139 }
3140 }
3141
3142 enum sigstatus {
3143 SIGSTATUS_HANDLE,
3144 SIGSTATUS_HANDLED,
3145 SIGSTATUS_IGNORE,
3146 SIGSTATUS_SBDRY_STOP,
3147 };
3148
3149 /*
3150 * The thread has signal "sig" pending. Figure out what to do with it:
3151 *
3152 * _HANDLE -> the caller should handle the signal
3153 * _HANDLED -> handled internally, reload pending signal set
3154 * _IGNORE -> ignored, remove from the set of pending signals and try the
3155 * next pending signal
3156 * _SBDRY_STOP -> the signal should stop the thread but this is not
3157 * permitted in the current context
3158 */
3159 static enum sigstatus
sigprocess(struct thread * td,int sig)3160 sigprocess(struct thread *td, int sig)
3161 {
3162 struct proc *p;
3163 struct sigacts *ps;
3164 struct sigqueue *queue;
3165 ksiginfo_t ksi;
3166 int prop;
3167
3168 KASSERT(_SIG_VALID(sig), ("%s: invalid signal %d", __func__, sig));
3169
3170 p = td->td_proc;
3171 ps = p->p_sigacts;
3172 mtx_assert(&ps->ps_mtx, MA_OWNED);
3173 PROC_LOCK_ASSERT(p, MA_OWNED);
3174
3175 /*
3176 * We should allow pending but ignored signals below
3177 * if there is sigwait() active, or P_TRACED was
3178 * on when they were posted.
3179 */
3180 if (SIGISMEMBER(ps->ps_sigignore, sig) &&
3181 (p->p_flag & P_TRACED) == 0 &&
3182 (td->td_flags & TDF_SIGWAIT) == 0) {
3183 return (SIGSTATUS_IGNORE);
3184 }
3185
3186 /*
3187 * If the process is going to single-thread mode to prepare
3188 * for exit, there is no sense in delivering any signal
3189 * to usermode. Another important consequence is that
3190 * msleep(..., PCATCH, ...) now is only interruptible by a
3191 * suspend request.
3192 */
3193 if ((p->p_flag2 & P2_WEXIT) != 0)
3194 return (SIGSTATUS_IGNORE);
3195
3196 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) {
3197 /*
3198 * If traced, always stop.
3199 * Remove old signal from queue before the stop.
3200 * XXX shrug off debugger, it causes siginfo to
3201 * be thrown away.
3202 */
3203 queue = &td->td_sigqueue;
3204 ksiginfo_init(&ksi);
3205 if (sigqueue_get(queue, sig, &ksi) == 0) {
3206 queue = &p->p_sigqueue;
3207 sigqueue_get(queue, sig, &ksi);
3208 }
3209 td->td_si = ksi.ksi_info;
3210
3211 mtx_unlock(&ps->ps_mtx);
3212 sig = ptracestop(td, sig, &ksi);
3213 mtx_lock(&ps->ps_mtx);
3214
3215 td->td_si.si_signo = 0;
3216
3217 /*
3218 * Keep looking if the debugger discarded or
3219 * replaced the signal.
3220 */
3221 if (sig == 0)
3222 return (SIGSTATUS_HANDLED);
3223
3224 /*
3225 * If the signal became masked, re-queue it.
3226 */
3227 if (SIGISMEMBER(td->td_sigmask, sig)) {
3228 ksi.ksi_flags |= KSI_HEAD;
3229 sigqueue_add(&p->p_sigqueue, sig, &ksi);
3230 return (SIGSTATUS_HANDLED);
3231 }
3232
3233 /*
3234 * If the traced bit got turned off, requeue the signal and
3235 * reload the set of pending signals. This ensures that p_sig*
3236 * and p_sigact are consistent.
3237 */
3238 if ((p->p_flag & P_TRACED) == 0) {
3239 if ((ksi.ksi_flags & KSI_PTRACE) == 0) {
3240 ksi.ksi_flags |= KSI_HEAD;
3241 sigqueue_add(queue, sig, &ksi);
3242 }
3243 return (SIGSTATUS_HANDLED);
3244 }
3245 }
3246
3247 /*
3248 * Decide whether the signal should be returned.
3249 * Return the signal's number, or fall through
3250 * to clear it from the pending mask.
3251 */
3252 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
3253 case (intptr_t)SIG_DFL:
3254 /*
3255 * Don't take default actions on system processes.
3256 */
3257 if (p->p_pid <= 1) {
3258 #ifdef DIAGNOSTIC
3259 /*
3260 * Are you sure you want to ignore SIGSEGV
3261 * in init? XXX
3262 */
3263 printf("Process (pid %lu) got signal %d\n",
3264 (u_long)p->p_pid, sig);
3265 #endif
3266 return (SIGSTATUS_IGNORE);
3267 }
3268
3269 /*
3270 * If there is a pending stop signal to process with
3271 * default action, stop here, then clear the signal.
3272 * Traced or exiting processes should ignore stops.
3273 * Additionally, a member of an orphaned process group
3274 * should ignore tty stops.
3275 */
3276 prop = sigprop(sig);
3277 if (prop & SIGPROP_STOP) {
3278 mtx_unlock(&ps->ps_mtx);
3279 if ((p->p_flag & (P_TRACED | P_WEXIT |
3280 P_SINGLE_EXIT)) != 0 || ((p->p_pgrp->
3281 pg_flags & PGRP_ORPHANED) != 0 &&
3282 (prop & SIGPROP_TTYSTOP) != 0)) {
3283 mtx_lock(&ps->ps_mtx);
3284 return (SIGSTATUS_IGNORE);
3285 }
3286 if (TD_SBDRY_INTR(td)) {
3287 KASSERT((td->td_flags & TDF_SBDRY) != 0,
3288 ("lost TDF_SBDRY"));
3289 mtx_lock(&ps->ps_mtx);
3290 return (SIGSTATUS_SBDRY_STOP);
3291 }
3292 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
3293 &p->p_mtx.lock_object, "Catching SIGSTOP");
3294 sigqueue_delete(&td->td_sigqueue, sig);
3295 sigqueue_delete(&p->p_sigqueue, sig);
3296 p->p_flag |= P_STOPPED_SIG;
3297 p->p_xsig = sig;
3298 PROC_SLOCK(p);
3299 sig_suspend_threads(td, p);
3300 thread_suspend_switch(td, p);
3301 PROC_SUNLOCK(p);
3302 mtx_lock(&ps->ps_mtx);
3303 return (SIGSTATUS_HANDLED);
3304 } else if ((prop & SIGPROP_IGNORE) != 0 &&
3305 (td->td_flags & TDF_SIGWAIT) == 0) {
3306 /*
3307 * Default action is to ignore; drop it if
3308 * not in kern_sigtimedwait().
3309 */
3310 return (SIGSTATUS_IGNORE);
3311 } else {
3312 return (SIGSTATUS_HANDLE);
3313 }
3314
3315 case (intptr_t)SIG_IGN:
3316 if ((td->td_flags & TDF_SIGWAIT) == 0)
3317 return (SIGSTATUS_IGNORE);
3318 else
3319 return (SIGSTATUS_HANDLE);
3320
3321 default:
3322 /*
3323 * This signal has an action, let postsig() process it.
3324 */
3325 return (SIGSTATUS_HANDLE);
3326 }
3327 }
3328
3329 /*
3330 * If the current process has received a signal (should be caught or cause
3331 * termination, should interrupt current syscall), return the signal number.
3332 * Stop signals with default action are processed immediately, then cleared;
3333 * they aren't returned. This is checked after each entry to the system for
3334 * a syscall or trap (though this can usually be done without calling
3335 * issignal by checking the pending signal masks in cursig.) The normal call
3336 * sequence is
3337 *
3338 * while (sig = cursig(curthread))
3339 * postsig(sig);
3340 */
3341 static int
issignal(struct thread * td)3342 issignal(struct thread *td)
3343 {
3344 struct proc *p;
3345 sigset_t sigpending;
3346 int sig;
3347
3348 p = td->td_proc;
3349 PROC_LOCK_ASSERT(p, MA_OWNED);
3350
3351 for (;;) {
3352 sigpending = td->td_sigqueue.sq_signals;
3353 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
3354 SIGSETNAND(sigpending, td->td_sigmask);
3355
3356 if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags &
3357 (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
3358 SIG_STOPSIGMASK(sigpending);
3359 if (SIGISEMPTY(sigpending)) /* no signal to send */
3360 return (0);
3361
3362 /*
3363 * Do fast sigblock if requested by usermode. Since
3364 * we do know that there was a signal pending at this
3365 * point, set the FAST_SIGBLOCK_PEND as indicator for
3366 * usermode to perform a dummy call to
3367 * FAST_SIGBLOCK_UNBLOCK, which causes immediate
3368 * delivery of postponed pending signal.
3369 */
3370 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
3371 if (td->td_sigblock_val != 0)
3372 SIGSETNAND(sigpending, fastblock_mask);
3373 if (SIGISEMPTY(sigpending)) {
3374 td->td_pflags |= TDP_SIGFASTPENDING;
3375 return (0);
3376 }
3377 }
3378
3379 if (!pt_attach_transparent &&
3380 (p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED &&
3381 (p->p_flag2 & P2_PTRACE_FSTP) != 0 &&
3382 SIGISMEMBER(sigpending, SIGSTOP)) {
3383 /*
3384 * If debugger just attached, always consume
3385 * SIGSTOP from ptrace(PT_ATTACH) first, to
3386 * execute the debugger attach ritual in
3387 * order.
3388 */
3389 td->td_dbgflags |= TDB_FSTP;
3390 SIGEMPTYSET(sigpending);
3391 SIGADDSET(sigpending, SIGSTOP);
3392 }
3393
3394 SIG_FOREACH(sig, &sigpending) {
3395 switch (sigprocess(td, sig)) {
3396 case SIGSTATUS_HANDLE:
3397 return (sig);
3398 case SIGSTATUS_HANDLED:
3399 goto next;
3400 case SIGSTATUS_IGNORE:
3401 sigqueue_delete(&td->td_sigqueue, sig);
3402 sigqueue_delete(&p->p_sigqueue, sig);
3403 break;
3404 case SIGSTATUS_SBDRY_STOP:
3405 return (-1);
3406 }
3407 }
3408 next:;
3409 }
3410 }
3411
3412 void
thread_stopped(struct proc * p)3413 thread_stopped(struct proc *p)
3414 {
3415 int n;
3416
3417 PROC_LOCK_ASSERT(p, MA_OWNED);
3418 PROC_SLOCK_ASSERT(p, MA_OWNED);
3419 n = p->p_suspcount;
3420 if (p == curproc)
3421 n++;
3422 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
3423 PROC_SUNLOCK(p);
3424 p->p_flag &= ~P_WAITED;
3425 PROC_LOCK(p->p_pptr);
3426 childproc_stopped(p, (p->p_flag & P_TRACED) ?
3427 CLD_TRAPPED : CLD_STOPPED);
3428 PROC_UNLOCK(p->p_pptr);
3429 PROC_SLOCK(p);
3430 }
3431 }
3432
3433 /*
3434 * Take the action for the specified signal
3435 * from the current set of pending signals.
3436 */
3437 int
postsig(int sig)3438 postsig(int sig)
3439 {
3440 struct thread *td;
3441 struct proc *p;
3442 struct sigacts *ps;
3443 sig_t action;
3444 ksiginfo_t ksi;
3445 sigset_t returnmask;
3446
3447 KASSERT(sig != 0, ("postsig"));
3448
3449 td = curthread;
3450 p = td->td_proc;
3451 PROC_LOCK_ASSERT(p, MA_OWNED);
3452 ps = p->p_sigacts;
3453 mtx_assert(&ps->ps_mtx, MA_OWNED);
3454 ksiginfo_init(&ksi);
3455 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
3456 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
3457 return (0);
3458 ksi.ksi_signo = sig;
3459 if (ksi.ksi_code == SI_TIMER)
3460 itimer_accept(p, ksi.ksi_timerid, &ksi);
3461 action = ps->ps_sigact[_SIG_IDX(sig)];
3462 #ifdef KTRACE
3463 if (KTRPOINT(td, KTR_PSIG))
3464 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
3465 &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
3466 #endif
3467
3468 if (action == SIG_DFL) {
3469 /*
3470 * Default action, where the default is to kill
3471 * the process. (Other cases were ignored above.)
3472 */
3473 mtx_unlock(&ps->ps_mtx);
3474 proc_td_siginfo_capture(td, &ksi.ksi_info);
3475 sigexit(td, sig);
3476 /* NOTREACHED */
3477 } else {
3478 /*
3479 * If we get here, the signal must be caught.
3480 */
3481 KASSERT(action != SIG_IGN, ("postsig action %p", action));
3482 KASSERT(!SIGISMEMBER(td->td_sigmask, sig),
3483 ("postsig action: blocked sig %d", sig));
3484
3485 /*
3486 * Set the new mask value and also defer further
3487 * occurrences of this signal.
3488 *
3489 * Special case: user has done a sigsuspend. Here the
3490 * current mask is not of interest, but rather the
3491 * mask from before the sigsuspend is what we want
3492 * restored after the signal processing is completed.
3493 */
3494 if (td->td_pflags & TDP_OLDMASK) {
3495 returnmask = td->td_oldsigmask;
3496 td->td_pflags &= ~TDP_OLDMASK;
3497 } else
3498 returnmask = td->td_sigmask;
3499
3500 if (p->p_sig == sig) {
3501 p->p_sig = 0;
3502 }
3503 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
3504 postsig_done(sig, td, ps);
3505 }
3506 return (1);
3507 }
3508
3509 int
sig_ast_checksusp(struct thread * td)3510 sig_ast_checksusp(struct thread *td)
3511 {
3512 struct proc *p __diagused;
3513 int ret;
3514
3515 p = td->td_proc;
3516 PROC_LOCK_ASSERT(p, MA_OWNED);
3517
3518 if (!td_ast_pending(td, TDA_SUSPEND))
3519 return (0);
3520
3521 ret = thread_suspend_check(1);
3522 MPASS(ret == 0 || ret == EINTR || ret == ERESTART);
3523 return (ret);
3524 }
3525
3526 int
sig_ast_needsigchk(struct thread * td)3527 sig_ast_needsigchk(struct thread *td)
3528 {
3529 struct proc *p;
3530 struct sigacts *ps;
3531 int ret, sig;
3532
3533 p = td->td_proc;
3534 PROC_LOCK_ASSERT(p, MA_OWNED);
3535
3536 if (!td_ast_pending(td, TDA_SIG))
3537 return (0);
3538
3539 ps = p->p_sigacts;
3540 mtx_lock(&ps->ps_mtx);
3541 sig = cursig(td);
3542 if (sig == -1) {
3543 mtx_unlock(&ps->ps_mtx);
3544 KASSERT((td->td_flags & TDF_SBDRY) != 0, ("lost TDF_SBDRY"));
3545 KASSERT(TD_SBDRY_INTR(td),
3546 ("lost TDF_SERESTART of TDF_SEINTR"));
3547 KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) !=
3548 (TDF_SEINTR | TDF_SERESTART),
3549 ("both TDF_SEINTR and TDF_SERESTART"));
3550 ret = TD_SBDRY_ERRNO(td);
3551 } else if (sig != 0) {
3552 ret = SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : ERESTART;
3553 mtx_unlock(&ps->ps_mtx);
3554 } else {
3555 mtx_unlock(&ps->ps_mtx);
3556 ret = 0;
3557 }
3558
3559 /*
3560 * Do not go into sleep if this thread was the ptrace(2)
3561 * attach leader. cursig() consumed SIGSTOP from PT_ATTACH,
3562 * but we usually act on the signal by interrupting sleep, and
3563 * should do that here as well.
3564 */
3565 if ((td->td_dbgflags & TDB_FSTP) != 0) {
3566 if (ret == 0)
3567 ret = EINTR;
3568 td->td_dbgflags &= ~TDB_FSTP;
3569 }
3570
3571 return (ret);
3572 }
3573
3574 int
sig_intr(void)3575 sig_intr(void)
3576 {
3577 struct thread *td;
3578 struct proc *p;
3579 int ret;
3580
3581 td = curthread;
3582 if (!td_ast_pending(td, TDA_SIG) && !td_ast_pending(td, TDA_SUSPEND))
3583 return (0);
3584
3585 p = td->td_proc;
3586
3587 PROC_LOCK(p);
3588 ret = sig_ast_checksusp(td);
3589 if (ret == 0)
3590 ret = sig_ast_needsigchk(td);
3591 PROC_UNLOCK(p);
3592 return (ret);
3593 }
3594
3595 bool
curproc_sigkilled(void)3596 curproc_sigkilled(void)
3597 {
3598 struct thread *td;
3599 struct proc *p;
3600 struct sigacts *ps;
3601 bool res;
3602
3603 td = curthread;
3604 if (!td_ast_pending(td, TDA_SIG))
3605 return (false);
3606
3607 p = td->td_proc;
3608 PROC_LOCK(p);
3609 ps = p->p_sigacts;
3610 mtx_lock(&ps->ps_mtx);
3611 res = SIGISMEMBER(td->td_sigqueue.sq_signals, SIGKILL) ||
3612 SIGISMEMBER(p->p_sigqueue.sq_signals, SIGKILL);
3613 mtx_unlock(&ps->ps_mtx);
3614 PROC_UNLOCK(p);
3615 return (res);
3616 }
3617
3618 void
proc_wkilled(struct proc * p)3619 proc_wkilled(struct proc *p)
3620 {
3621
3622 PROC_LOCK_ASSERT(p, MA_OWNED);
3623 if ((p->p_flag & P_WKILLED) == 0)
3624 p->p_flag |= P_WKILLED;
3625 }
3626
3627 /*
3628 * Kill the current process for stated reason.
3629 */
3630 void
killproc(struct proc * p,const char * why)3631 killproc(struct proc *p, const char *why)
3632 {
3633
3634 PROC_LOCK_ASSERT(p, MA_OWNED);
3635 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid,
3636 p->p_comm);
3637 log(LOG_ERR, "pid %d (%s), jid %d, uid %d, was killed: %s\n",
3638 p->p_pid, p->p_comm, p->p_ucred->cr_prison->pr_id,
3639 p->p_ucred->cr_uid, why);
3640 proc_wkilled(p);
3641 kern_psignal(p, SIGKILL);
3642 }
3643
3644 /*
3645 * Send queued SIGCHLD to parent when child process's state
3646 * is changed.
3647 */
3648 static void
sigparent(struct proc * p,int reason,int status)3649 sigparent(struct proc *p, int reason, int status)
3650 {
3651 PROC_LOCK_ASSERT(p, MA_OWNED);
3652 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3653
3654 if (p->p_ksi != NULL) {
3655 p->p_ksi->ksi_signo = SIGCHLD;
3656 p->p_ksi->ksi_code = reason;
3657 p->p_ksi->ksi_status = status;
3658 p->p_ksi->ksi_pid = p->p_pid;
3659 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
3660 if (KSI_ONQ(p->p_ksi))
3661 return;
3662 }
3663
3664 /*
3665 * Do not consume p_ksi if parent is zombie, since signal is
3666 * dropped immediately. Instead, keep it since it might be
3667 * useful for reaper.
3668 */
3669 if (p->p_pptr->p_state != PRS_ZOMBIE)
3670 pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
3671 }
3672
3673 static void
childproc_jobstate(struct proc * p,int reason,int sig)3674 childproc_jobstate(struct proc *p, int reason, int sig)
3675 {
3676 struct sigacts *ps;
3677
3678 PROC_LOCK_ASSERT(p, MA_OWNED);
3679 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3680
3681 /*
3682 * Wake up parent sleeping in kern_wait(), also send
3683 * SIGCHLD to parent, but SIGCHLD does not guarantee
3684 * that parent will awake, because parent may masked
3685 * the signal.
3686 */
3687 p->p_pptr->p_flag |= P_STATCHILD;
3688 wakeup(p->p_pptr);
3689
3690 ps = p->p_pptr->p_sigacts;
3691 mtx_lock(&ps->ps_mtx);
3692 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
3693 mtx_unlock(&ps->ps_mtx);
3694 sigparent(p, reason, sig);
3695 } else
3696 mtx_unlock(&ps->ps_mtx);
3697 }
3698
3699 void
childproc_stopped(struct proc * p,int reason)3700 childproc_stopped(struct proc *p, int reason)
3701 {
3702
3703 childproc_jobstate(p, reason, p->p_xsig);
3704 }
3705
3706 void
childproc_continued(struct proc * p)3707 childproc_continued(struct proc *p)
3708 {
3709 PROC_LOCK_ASSERT(p, MA_OWNED);
3710 p->p_flag |= P_CONTINUED;
3711 p->p_xsig = SIGCONT;
3712 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
3713 }
3714
3715 void
childproc_exited(struct proc * p)3716 childproc_exited(struct proc *p)
3717 {
3718 int reason, status;
3719
3720 if (WCOREDUMP(p->p_xsig)) {
3721 reason = CLD_DUMPED;
3722 status = WTERMSIG(p->p_xsig);
3723 } else if (WIFSIGNALED(p->p_xsig)) {
3724 reason = CLD_KILLED;
3725 status = WTERMSIG(p->p_xsig);
3726 } else {
3727 reason = CLD_EXITED;
3728 status = p->p_xexit;
3729 }
3730 /*
3731 * XXX avoid calling wakeup(p->p_pptr), the work is
3732 * done in exit1().
3733 */
3734 sigparent(p, reason, status);
3735 }
3736
3737 /*
3738 * Nonexistent system call-- signal process (may want to handle it). Flag
3739 * error in case process won't see signal immediately (blocked or ignored).
3740 */
3741 #ifndef _SYS_SYSPROTO_H_
3742 struct nosys_args {
3743 int dummy;
3744 };
3745 #endif
3746 /* ARGSUSED */
3747 int
nosys(struct thread * td,struct nosys_args * args)3748 nosys(struct thread *td, struct nosys_args *args)
3749 {
3750 return (kern_nosys(td, args->dummy));
3751 }
3752
3753 int
kern_nosys(struct thread * td,int dummy)3754 kern_nosys(struct thread *td, int dummy)
3755 {
3756 struct proc *p;
3757
3758 p = td->td_proc;
3759
3760 if (SV_PROC_FLAG(p, SV_SIGSYS) != 0 && kern_signosys) {
3761 PROC_LOCK(p);
3762 tdsignal(td, SIGSYS);
3763 PROC_UNLOCK(p);
3764 }
3765 if (kern_lognosys == 1 || kern_lognosys == 3) {
3766 uprintf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
3767 td->td_sa.code);
3768 }
3769 if (kern_lognosys == 2 || kern_lognosys == 3 ||
3770 (p->p_pid == 1 && (kern_lognosys & 3) == 0)) {
3771 printf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
3772 td->td_sa.code);
3773 }
3774 return (ENOSYS);
3775 }
3776
3777 /*
3778 * Send a SIGIO or SIGURG signal to a process or process group using stored
3779 * credentials rather than those of the current process.
3780 */
3781 void
pgsigio(struct sigio ** sigiop,int sig,int checkctty)3782 pgsigio(struct sigio **sigiop, int sig, int checkctty)
3783 {
3784 ksiginfo_t ksi;
3785 struct sigio *sigio;
3786
3787 ksiginfo_init(&ksi);
3788 ksi.ksi_signo = sig;
3789 ksi.ksi_code = SI_KERNEL;
3790
3791 SIGIO_LOCK();
3792 sigio = *sigiop;
3793 if (sigio == NULL) {
3794 SIGIO_UNLOCK();
3795 return;
3796 }
3797 if (sigio->sio_pgid > 0) {
3798 PROC_LOCK(sigio->sio_proc);
3799 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
3800 kern_psignal(sigio->sio_proc, sig);
3801 PROC_UNLOCK(sigio->sio_proc);
3802 } else if (sigio->sio_pgid < 0) {
3803 struct proc *p;
3804
3805 PGRP_LOCK(sigio->sio_pgrp);
3806 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3807 PROC_LOCK(p);
3808 if (p->p_state == PRS_NORMAL &&
3809 CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3810 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
3811 kern_psignal(p, sig);
3812 PROC_UNLOCK(p);
3813 }
3814 PGRP_UNLOCK(sigio->sio_pgrp);
3815 }
3816 SIGIO_UNLOCK();
3817 }
3818
3819 static int
filt_sigattach(struct knote * kn)3820 filt_sigattach(struct knote *kn)
3821 {
3822 struct proc *p = curproc;
3823
3824 kn->kn_ptr.p_proc = p;
3825 kn->kn_flags |= EV_CLEAR; /* automatically set */
3826
3827 knlist_add(p->p_klist, kn, 0);
3828
3829 return (0);
3830 }
3831
3832 static void
filt_sigdetach(struct knote * kn)3833 filt_sigdetach(struct knote *kn)
3834 {
3835 knlist_remove(kn->kn_knlist, kn, 0);
3836 }
3837
3838 /*
3839 * signal knotes are shared with proc knotes, so we apply a mask to
3840 * the hint in order to differentiate them from process hints. This
3841 * could be avoided by using a signal-specific knote list, but probably
3842 * isn't worth the trouble.
3843 */
3844 static int
filt_signal(struct knote * kn,long hint)3845 filt_signal(struct knote *kn, long hint)
3846 {
3847
3848 if (hint & NOTE_SIGNAL) {
3849 hint &= ~NOTE_SIGNAL;
3850
3851 if (kn->kn_id == hint)
3852 kn->kn_data++;
3853 }
3854 return (kn->kn_data != 0);
3855 }
3856
3857 struct sigacts *
sigacts_alloc(void)3858 sigacts_alloc(void)
3859 {
3860 struct sigacts *ps;
3861
3862 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3863 refcount_init(&ps->ps_refcnt, 1);
3864 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
3865 return (ps);
3866 }
3867
3868 void
sigacts_free(struct sigacts * ps)3869 sigacts_free(struct sigacts *ps)
3870 {
3871
3872 if (refcount_release(&ps->ps_refcnt) == 0)
3873 return;
3874 mtx_destroy(&ps->ps_mtx);
3875 free(ps, M_SUBPROC);
3876 }
3877
3878 struct sigacts *
sigacts_hold(struct sigacts * ps)3879 sigacts_hold(struct sigacts *ps)
3880 {
3881
3882 refcount_acquire(&ps->ps_refcnt);
3883 return (ps);
3884 }
3885
3886 void
sigacts_copy(struct sigacts * dest,struct sigacts * src)3887 sigacts_copy(struct sigacts *dest, struct sigacts *src)
3888 {
3889
3890 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
3891 mtx_lock(&src->ps_mtx);
3892 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
3893 mtx_unlock(&src->ps_mtx);
3894 }
3895
3896 int
sigacts_shared(struct sigacts * ps)3897 sigacts_shared(struct sigacts *ps)
3898 {
3899
3900 return (ps->ps_refcnt > 1);
3901 }
3902
3903 void
sig_drop_caught(struct proc * p)3904 sig_drop_caught(struct proc *p)
3905 {
3906 int sig;
3907 struct sigacts *ps;
3908
3909 ps = p->p_sigacts;
3910 PROC_LOCK_ASSERT(p, MA_OWNED);
3911 mtx_assert(&ps->ps_mtx, MA_OWNED);
3912 SIG_FOREACH(sig, &ps->ps_sigcatch) {
3913 sigdflt(ps, sig);
3914 if ((sigprop(sig) & SIGPROP_IGNORE) != 0)
3915 sigqueue_delete_proc(p, sig);
3916 }
3917 }
3918
3919 static void
sigfastblock_failed(struct thread * td,bool sendsig,bool write)3920 sigfastblock_failed(struct thread *td, bool sendsig, bool write)
3921 {
3922 ksiginfo_t ksi;
3923
3924 /*
3925 * Prevent further fetches and SIGSEGVs, allowing thread to
3926 * issue syscalls despite corruption.
3927 */
3928 sigfastblock_clear(td);
3929
3930 if (!sendsig)
3931 return;
3932 ksiginfo_init_trap(&ksi);
3933 ksi.ksi_signo = SIGSEGV;
3934 ksi.ksi_code = write ? SEGV_ACCERR : SEGV_MAPERR;
3935 ksi.ksi_addr = td->td_sigblock_ptr;
3936 trapsignal(td, &ksi);
3937 }
3938
3939 static bool
sigfastblock_fetch_sig(struct thread * td,bool sendsig,uint32_t * valp)3940 sigfastblock_fetch_sig(struct thread *td, bool sendsig, uint32_t *valp)
3941 {
3942 uint32_t res;
3943
3944 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
3945 return (true);
3946 if (fueword32((void *)td->td_sigblock_ptr, &res) == -1) {
3947 sigfastblock_failed(td, sendsig, false);
3948 return (false);
3949 }
3950 *valp = res;
3951 td->td_sigblock_val = res & ~SIGFASTBLOCK_FLAGS;
3952 return (true);
3953 }
3954
3955 static void
sigfastblock_resched(struct thread * td,bool resched)3956 sigfastblock_resched(struct thread *td, bool resched)
3957 {
3958 struct proc *p;
3959
3960 if (resched) {
3961 p = td->td_proc;
3962 PROC_LOCK(p);
3963 reschedule_signals(p, td->td_sigmask, 0);
3964 PROC_UNLOCK(p);
3965 }
3966 ast_sched(td, TDA_SIG);
3967 }
3968
3969 int
sys_sigfastblock(struct thread * td,struct sigfastblock_args * uap)3970 sys_sigfastblock(struct thread *td, struct sigfastblock_args *uap)
3971 {
3972 struct proc *p;
3973 int error, res;
3974 uint32_t oldval;
3975
3976 error = 0;
3977 p = td->td_proc;
3978 switch (uap->cmd) {
3979 case SIGFASTBLOCK_SETPTR:
3980 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
3981 error = EBUSY;
3982 break;
3983 }
3984 if (((uintptr_t)(uap->ptr) & (sizeof(uint32_t) - 1)) != 0) {
3985 error = EINVAL;
3986 break;
3987 }
3988 td->td_pflags |= TDP_SIGFASTBLOCK;
3989 td->td_sigblock_ptr = uap->ptr;
3990 break;
3991
3992 case SIGFASTBLOCK_UNBLOCK:
3993 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
3994 error = EINVAL;
3995 break;
3996 }
3997
3998 for (;;) {
3999 res = casueword32(td->td_sigblock_ptr,
4000 SIGFASTBLOCK_PEND, &oldval, 0);
4001 if (res == -1) {
4002 error = EFAULT;
4003 sigfastblock_failed(td, false, true);
4004 break;
4005 }
4006 if (res == 0)
4007 break;
4008 MPASS(res == 1);
4009 if (oldval != SIGFASTBLOCK_PEND) {
4010 error = EBUSY;
4011 break;
4012 }
4013 error = thread_check_susp(td, false);
4014 if (error != 0)
4015 break;
4016 }
4017 if (error != 0)
4018 break;
4019
4020 /*
4021 * td_sigblock_val is cleared there, but not on a
4022 * syscall exit. The end effect is that a single
4023 * interruptible sleep, while user sigblock word is
4024 * set, might return EINTR or ERESTART to usermode
4025 * without delivering signal. All further sleeps,
4026 * until userspace clears the word and does
4027 * sigfastblock(UNBLOCK), observe current word and no
4028 * longer get interrupted. It is slight
4029 * non-conformance, with alternative to have read the
4030 * sigblock word on each syscall entry.
4031 */
4032 td->td_sigblock_val = 0;
4033
4034 /*
4035 * Rely on normal ast mechanism to deliver pending
4036 * signals to current thread. But notify others about
4037 * fake unblock.
4038 */
4039 sigfastblock_resched(td, error == 0 && p->p_numthreads != 1);
4040
4041 break;
4042
4043 case SIGFASTBLOCK_UNSETPTR:
4044 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4045 error = EINVAL;
4046 break;
4047 }
4048 if (!sigfastblock_fetch_sig(td, false, &oldval)) {
4049 error = EFAULT;
4050 break;
4051 }
4052 if (oldval != 0 && oldval != SIGFASTBLOCK_PEND) {
4053 error = EBUSY;
4054 break;
4055 }
4056 sigfastblock_clear(td);
4057 break;
4058
4059 default:
4060 error = EINVAL;
4061 break;
4062 }
4063 return (error);
4064 }
4065
4066 void
sigfastblock_clear(struct thread * td)4067 sigfastblock_clear(struct thread *td)
4068 {
4069 bool resched;
4070
4071 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4072 return;
4073 td->td_sigblock_val = 0;
4074 resched = (td->td_pflags & TDP_SIGFASTPENDING) != 0 ||
4075 SIGPENDING(td);
4076 td->td_pflags &= ~(TDP_SIGFASTBLOCK | TDP_SIGFASTPENDING);
4077 sigfastblock_resched(td, resched);
4078 }
4079
4080 void
sigfastblock_fetch(struct thread * td)4081 sigfastblock_fetch(struct thread *td)
4082 {
4083 uint32_t val;
4084
4085 (void)sigfastblock_fetch_sig(td, true, &val);
4086 }
4087
4088 static void
sigfastblock_setpend1(struct thread * td)4089 sigfastblock_setpend1(struct thread *td)
4090 {
4091 int res;
4092 uint32_t oldval;
4093
4094 if ((td->td_pflags & TDP_SIGFASTPENDING) == 0)
4095 return;
4096 res = fueword32((void *)td->td_sigblock_ptr, &oldval);
4097 if (res == -1) {
4098 sigfastblock_failed(td, true, false);
4099 return;
4100 }
4101 for (;;) {
4102 res = casueword32(td->td_sigblock_ptr, oldval, &oldval,
4103 oldval | SIGFASTBLOCK_PEND);
4104 if (res == -1) {
4105 sigfastblock_failed(td, true, true);
4106 return;
4107 }
4108 if (res == 0) {
4109 td->td_sigblock_val = oldval & ~SIGFASTBLOCK_FLAGS;
4110 td->td_pflags &= ~TDP_SIGFASTPENDING;
4111 break;
4112 }
4113 MPASS(res == 1);
4114 if (thread_check_susp(td, false) != 0)
4115 break;
4116 }
4117 }
4118
4119 static void
sigfastblock_setpend(struct thread * td,bool resched)4120 sigfastblock_setpend(struct thread *td, bool resched)
4121 {
4122 struct proc *p;
4123
4124 sigfastblock_setpend1(td);
4125 if (resched) {
4126 p = td->td_proc;
4127 PROC_LOCK(p);
4128 reschedule_signals(p, fastblock_mask, SIGPROCMASK_FASTBLK);
4129 PROC_UNLOCK(p);
4130 }
4131 }
4132