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 if ((signumf & __SIGQUEUE_TID) == 0) {
2042 if ((p = pfind_any(pid)) == NULL)
2043 return (ESRCH);
2044 td2 = NULL;
2045 } else {
2046 p = td->td_proc;
2047 td2 = tdfind((lwpid_t)pid, p->p_pid);
2048 if (td2 == NULL)
2049 return (ESRCH);
2050 }
2051
2052 error = p_cansignal(td, p, signum);
2053 if (error == 0 && signum != 0) {
2054 ksiginfo_init(&ksi);
2055 ksi.ksi_flags = KSI_SIGQ;
2056 ksi.ksi_signo = signum;
2057 ksi.ksi_code = SI_QUEUE;
2058 ksi.ksi_pid = td->td_proc->p_pid;
2059 ksi.ksi_uid = td->td_ucred->cr_ruid;
2060 ksi.ksi_value = *value;
2061 error = tdsendsignal(p, td2, ksi.ksi_signo, &ksi);
2062 }
2063 PROC_UNLOCK(p);
2064 return (error);
2065 }
2066
2067 /*
2068 * Send a signal to a process group. If checktty is 1,
2069 * limit to members which have a controlling terminal.
2070 */
2071 void
pgsignal(struct pgrp * pgrp,int sig,int checkctty,ksiginfo_t * ksi)2072 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
2073 {
2074 struct proc *p;
2075
2076 if (pgrp) {
2077 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
2078 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
2079 PROC_LOCK(p);
2080 if (p->p_state == PRS_NORMAL &&
2081 (checkctty == 0 || p->p_flag & P_CONTROLT))
2082 pksignal(p, sig, ksi);
2083 PROC_UNLOCK(p);
2084 }
2085 }
2086 }
2087
2088 /*
2089 * Recalculate the signal mask and reset the signal disposition after
2090 * usermode frame for delivery is formed. Should be called after
2091 * mach-specific routine, because sysent->sv_sendsig() needs correct
2092 * ps_siginfo and signal mask.
2093 */
2094 static void
postsig_done(int sig,struct thread * td,struct sigacts * ps)2095 postsig_done(int sig, struct thread *td, struct sigacts *ps)
2096 {
2097 sigset_t mask;
2098
2099 mtx_assert(&ps->ps_mtx, MA_OWNED);
2100 td->td_ru.ru_nsignals++;
2101 mask = ps->ps_catchmask[_SIG_IDX(sig)];
2102 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2103 SIGADDSET(mask, sig);
2104 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
2105 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
2106 if (SIGISMEMBER(ps->ps_sigreset, sig))
2107 sigdflt(ps, sig);
2108 }
2109
2110 /*
2111 * Send a signal caused by a trap to the current thread. If it will be
2112 * caught immediately, deliver it with correct code. Otherwise, post it
2113 * normally.
2114 */
2115 void
trapsignal(struct thread * td,ksiginfo_t * ksi)2116 trapsignal(struct thread *td, ksiginfo_t *ksi)
2117 {
2118 struct sigacts *ps;
2119 struct proc *p;
2120 sigset_t sigmask;
2121 int sig;
2122
2123 p = td->td_proc;
2124 sig = ksi->ksi_signo;
2125 KASSERT(_SIG_VALID(sig), ("invalid signal"));
2126
2127 sigfastblock_fetch(td);
2128 PROC_LOCK(p);
2129 ps = p->p_sigacts;
2130 mtx_lock(&ps->ps_mtx);
2131 sigmask = td->td_sigmask;
2132 if (td->td_sigblock_val != 0)
2133 SIGSETOR(sigmask, fastblock_mask);
2134 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
2135 !SIGISMEMBER(sigmask, sig)) {
2136 #ifdef KTRACE
2137 if (KTRPOINT(curthread, KTR_PSIG))
2138 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
2139 &td->td_sigmask, ksi->ksi_code);
2140 #endif
2141 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
2142 ksi, &td->td_sigmask);
2143 postsig_done(sig, td, ps);
2144 mtx_unlock(&ps->ps_mtx);
2145 } else {
2146 /*
2147 * Avoid a possible infinite loop if the thread
2148 * masking the signal or process is ignoring the
2149 * signal.
2150 */
2151 if (kern_forcesigexit && (SIGISMEMBER(sigmask, sig) ||
2152 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
2153 SIGDELSET(td->td_sigmask, sig);
2154 SIGDELSET(ps->ps_sigcatch, sig);
2155 SIGDELSET(ps->ps_sigignore, sig);
2156 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2157 td->td_pflags &= ~TDP_SIGFASTBLOCK;
2158 td->td_sigblock_val = 0;
2159 }
2160 mtx_unlock(&ps->ps_mtx);
2161 p->p_sig = sig; /* XXX to verify code */
2162 tdsendsignal(p, td, sig, ksi);
2163 }
2164 PROC_UNLOCK(p);
2165 }
2166
2167 static struct thread *
sigtd(struct proc * p,int sig,bool fast_sigblock)2168 sigtd(struct proc *p, int sig, bool fast_sigblock)
2169 {
2170 struct thread *td, *signal_td;
2171
2172 PROC_LOCK_ASSERT(p, MA_OWNED);
2173 MPASS(!fast_sigblock || p == curproc);
2174
2175 /*
2176 * Check if current thread can handle the signal without
2177 * switching context to another thread.
2178 */
2179 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig) &&
2180 (!fast_sigblock || curthread->td_sigblock_val == 0))
2181 return (curthread);
2182
2183 /* Find a non-stopped thread that does not mask the signal. */
2184 signal_td = NULL;
2185 FOREACH_THREAD_IN_PROC(p, td) {
2186 if (!SIGISMEMBER(td->td_sigmask, sig) && (!fast_sigblock ||
2187 td != curthread || td->td_sigblock_val == 0) &&
2188 (td->td_flags & TDF_BOUNDARY) == 0) {
2189 signal_td = td;
2190 break;
2191 }
2192 }
2193 /* Select random (first) thread if no better match was found. */
2194 if (signal_td == NULL)
2195 signal_td = FIRST_THREAD_IN_PROC(p);
2196 return (signal_td);
2197 }
2198
2199 /*
2200 * Send the signal to the process. If the signal has an action, the action
2201 * is usually performed by the target process rather than the caller; we add
2202 * the signal to the set of pending signals for the process.
2203 *
2204 * Exceptions:
2205 * o When a stop signal is sent to a sleeping process that takes the
2206 * default action, the process is stopped without awakening it.
2207 * o SIGCONT restarts stopped processes (or puts them back to sleep)
2208 * regardless of the signal action (eg, blocked or ignored).
2209 *
2210 * Other ignored signals are discarded immediately.
2211 *
2212 * NB: This function may be entered from the debugger via the "kill" DDB
2213 * command. There is little that can be done to mitigate the possibly messy
2214 * side effects of this unwise possibility.
2215 */
2216 void
kern_psignal(struct proc * p,int sig)2217 kern_psignal(struct proc *p, int sig)
2218 {
2219 ksiginfo_t ksi;
2220
2221 ksiginfo_init(&ksi);
2222 ksi.ksi_signo = sig;
2223 ksi.ksi_code = SI_KERNEL;
2224 (void) tdsendsignal(p, NULL, sig, &ksi);
2225 }
2226
2227 int
pksignal(struct proc * p,int sig,ksiginfo_t * ksi)2228 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
2229 {
2230
2231 return (tdsendsignal(p, NULL, sig, ksi));
2232 }
2233
2234 /* Utility function for finding a thread to send signal event to. */
2235 int
sigev_findtd(struct proc * p,struct sigevent * sigev,struct thread ** ttd)2236 sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **ttd)
2237 {
2238 struct thread *td;
2239
2240 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
2241 td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
2242 if (td == NULL)
2243 return (ESRCH);
2244 *ttd = td;
2245 } else {
2246 *ttd = NULL;
2247 PROC_LOCK(p);
2248 }
2249 return (0);
2250 }
2251
2252 void
tdsignal(struct thread * td,int sig)2253 tdsignal(struct thread *td, int sig)
2254 {
2255 ksiginfo_t ksi;
2256
2257 ksiginfo_init(&ksi);
2258 ksi.ksi_signo = sig;
2259 ksi.ksi_code = SI_KERNEL;
2260 (void) tdsendsignal(td->td_proc, td, sig, &ksi);
2261 }
2262
2263 void
tdksignal(struct thread * td,int sig,ksiginfo_t * ksi)2264 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2265 {
2266
2267 (void) tdsendsignal(td->td_proc, td, sig, ksi);
2268 }
2269
2270 static void
sig_sleepq_abort(struct thread * td,int intrval)2271 sig_sleepq_abort(struct thread *td, int intrval)
2272 {
2273 THREAD_LOCK_ASSERT(td, MA_OWNED);
2274
2275 if (intrval == 0 && (td->td_flags & TDF_SIGWAIT) == 0)
2276 thread_unlock(td);
2277 else
2278 sleepq_abort(td, intrval);
2279 }
2280
2281 int
tdsendsignal(struct proc * p,struct thread * td,int sig,ksiginfo_t * ksi)2282 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2283 {
2284 sig_t action;
2285 sigqueue_t *sigqueue;
2286 struct sigacts *ps;
2287 int intrval, prop, ret;
2288
2289 MPASS(td == NULL || p == td->td_proc);
2290 PROC_LOCK_ASSERT(p, MA_OWNED);
2291
2292 if (!_SIG_VALID(sig))
2293 panic("%s(): invalid signal %d", __func__, sig);
2294
2295 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
2296
2297 /*
2298 * IEEE Std 1003.1-2001: return success when killing a zombie.
2299 */
2300 if (p->p_state == PRS_ZOMBIE) {
2301 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2302 ksiginfo_tryfree(ksi);
2303 return (0);
2304 }
2305
2306 ps = p->p_sigacts;
2307 KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig);
2308 prop = sigprop(sig);
2309
2310 if (td == NULL) {
2311 td = sigtd(p, sig, false);
2312 sigqueue = &p->p_sigqueue;
2313 } else
2314 sigqueue = &td->td_sigqueue;
2315
2316 SDT_PROBE3(proc, , , signal__send, td, p, sig);
2317
2318 /*
2319 * If the signal is being ignored, then we forget about it
2320 * immediately, except when the target process executes
2321 * sigwait(). (Note: we don't set SIGCONT in ps_sigignore,
2322 * and if it is set to SIG_IGN, action will be SIG_DFL here.)
2323 */
2324 mtx_lock(&ps->ps_mtx);
2325 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2326 if (kern_sig_discard_ign &&
2327 (p->p_sysent->sv_flags & SV_SIG_DISCIGN) == 0) {
2328 SDT_PROBE3(proc, , , signal__discard, td, p, sig);
2329
2330 mtx_unlock(&ps->ps_mtx);
2331 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2332 ksiginfo_tryfree(ksi);
2333 return (0);
2334 } else {
2335 action = SIG_CATCH;
2336 intrval = 0;
2337 }
2338 } else {
2339 if (SIGISMEMBER(td->td_sigmask, sig))
2340 action = SIG_HOLD;
2341 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2342 action = SIG_CATCH;
2343 else
2344 action = SIG_DFL;
2345 if (SIGISMEMBER(ps->ps_sigintr, sig))
2346 intrval = EINTR;
2347 else
2348 intrval = ERESTART;
2349 }
2350 mtx_unlock(&ps->ps_mtx);
2351
2352 if (prop & SIGPROP_CONT)
2353 sigqueue_delete_stopmask_proc(p);
2354 else if (prop & SIGPROP_STOP) {
2355 if (pt_attach_transparent &&
2356 (p->p_flag & P_TRACED) != 0 &&
2357 (p->p_flag2 & P2_PTRACE_FSTP) != 0) {
2358 PROC_SLOCK(p);
2359 sig_handle_first_stop(NULL, p, sig);
2360 PROC_SUNLOCK(p);
2361 return (0);
2362 }
2363
2364 /*
2365 * If sending a tty stop signal to a member of an orphaned
2366 * process group, discard the signal here if the action
2367 * is default; don't stop the process below if sleeping,
2368 * and don't clear any pending SIGCONT.
2369 */
2370 if ((prop & SIGPROP_TTYSTOP) != 0 &&
2371 (p->p_pgrp->pg_flags & PGRP_ORPHANED) != 0 &&
2372 action == SIG_DFL) {
2373 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2374 ksiginfo_tryfree(ksi);
2375 return (0);
2376 }
2377 sigqueue_delete_proc(p, SIGCONT);
2378 if (p->p_flag & P_CONTINUED) {
2379 p->p_flag &= ~P_CONTINUED;
2380 PROC_LOCK(p->p_pptr);
2381 sigqueue_take(p->p_ksi);
2382 PROC_UNLOCK(p->p_pptr);
2383 }
2384 }
2385
2386 ret = sigqueue_add(sigqueue, sig, ksi);
2387 if (ret != 0)
2388 return (ret);
2389 signotify(td);
2390 /*
2391 * Defer further processing for signals which are held,
2392 * except that stopped processes must be continued by SIGCONT.
2393 */
2394 if (action == SIG_HOLD &&
2395 !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG)))
2396 return (0);
2397
2398 /*
2399 * Some signals have a process-wide effect and a per-thread
2400 * component. Most processing occurs when the process next
2401 * tries to cross the user boundary, however there are some
2402 * times when processing needs to be done immediately, such as
2403 * waking up threads so that they can cross the user boundary.
2404 * We try to do the per-process part here.
2405 */
2406 if (P_SHOULDSTOP(p)) {
2407 KASSERT(!(p->p_flag & P_WEXIT),
2408 ("signal to stopped but exiting process"));
2409 if (sig == SIGKILL) {
2410 /*
2411 * If traced process is already stopped,
2412 * then no further action is necessary.
2413 */
2414 if (p->p_flag & P_TRACED)
2415 return (0);
2416 /*
2417 * SIGKILL sets process running.
2418 * It will die elsewhere.
2419 * All threads must be restarted.
2420 */
2421 p->p_flag &= ~P_STOPPED_SIG;
2422 goto runfast;
2423 }
2424
2425 if (prop & SIGPROP_CONT) {
2426 /*
2427 * If traced process is already stopped,
2428 * then no further action is necessary.
2429 */
2430 if (p->p_flag & P_TRACED)
2431 return (0);
2432 /*
2433 * If SIGCONT is default (or ignored), we continue the
2434 * process but don't leave the signal in sigqueue as
2435 * it has no further action. If SIGCONT is held, we
2436 * continue the process and leave the signal in
2437 * sigqueue. If the process catches SIGCONT, let it
2438 * handle the signal itself. If it isn't waiting on
2439 * an event, it goes back to run state.
2440 * Otherwise, process goes back to sleep state.
2441 */
2442 p->p_flag &= ~P_STOPPED_SIG;
2443 PROC_SLOCK(p);
2444 if (p->p_numthreads == p->p_suspcount) {
2445 PROC_SUNLOCK(p);
2446 PROC_LOCK(p->p_pptr);
2447 childproc_continued(p);
2448 PROC_UNLOCK(p->p_pptr);
2449 PROC_SLOCK(p);
2450 }
2451 if (action == SIG_DFL) {
2452 thread_unsuspend(p);
2453 PROC_SUNLOCK(p);
2454 sigqueue_delete(sigqueue, sig);
2455 goto out_cont;
2456 }
2457 if (action == SIG_CATCH) {
2458 /*
2459 * The process wants to catch it so it needs
2460 * to run at least one thread, but which one?
2461 */
2462 PROC_SUNLOCK(p);
2463 goto runfast;
2464 }
2465 /*
2466 * The signal is not ignored or caught.
2467 */
2468 thread_unsuspend(p);
2469 PROC_SUNLOCK(p);
2470 goto out_cont;
2471 }
2472
2473 if (prop & SIGPROP_STOP) {
2474 /*
2475 * If traced process is already stopped,
2476 * then no further action is necessary.
2477 */
2478 if (p->p_flag & P_TRACED)
2479 return (0);
2480 /*
2481 * Already stopped, don't need to stop again
2482 * (If we did the shell could get confused).
2483 * Just make sure the signal STOP bit set.
2484 */
2485 p->p_flag |= P_STOPPED_SIG;
2486 sigqueue_delete(sigqueue, sig);
2487 return (0);
2488 }
2489
2490 /*
2491 * All other kinds of signals:
2492 * If a thread is sleeping interruptibly, simulate a
2493 * wakeup so that when it is continued it will be made
2494 * runnable and can look at the signal. However, don't make
2495 * the PROCESS runnable, leave it stopped.
2496 * It may run a bit until it hits a thread_suspend_check().
2497 */
2498 PROC_SLOCK(p);
2499 thread_lock(td);
2500 if (TD_CAN_ABORT(td))
2501 sig_sleepq_abort(td, intrval);
2502 else
2503 thread_unlock(td);
2504 PROC_SUNLOCK(p);
2505 return (0);
2506 /*
2507 * Mutexes are short lived. Threads waiting on them will
2508 * hit thread_suspend_check() soon.
2509 */
2510 } else if (p->p_state == PRS_NORMAL) {
2511 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2512 tdsigwakeup(td, sig, action, intrval);
2513 return (0);
2514 }
2515
2516 MPASS(action == SIG_DFL);
2517
2518 if (prop & SIGPROP_STOP) {
2519 if (p->p_flag & (P_PPWAIT|P_WEXIT))
2520 return (0);
2521 p->p_flag |= P_STOPPED_SIG;
2522 p->p_xsig = sig;
2523 PROC_SLOCK(p);
2524 sig_suspend_threads(td, p);
2525 if (p->p_numthreads == p->p_suspcount) {
2526 /*
2527 * only thread sending signal to another
2528 * process can reach here, if thread is sending
2529 * signal to its process, because thread does
2530 * not suspend itself here, p_numthreads
2531 * should never be equal to p_suspcount.
2532 */
2533 thread_stopped(p);
2534 PROC_SUNLOCK(p);
2535 sigqueue_delete_proc(p, p->p_xsig);
2536 } else
2537 PROC_SUNLOCK(p);
2538 return (0);
2539 }
2540 } else {
2541 /* Not in "NORMAL" state. discard the signal. */
2542 sigqueue_delete(sigqueue, sig);
2543 return (0);
2544 }
2545
2546 /*
2547 * The process is not stopped so we need to apply the signal to all the
2548 * running threads.
2549 */
2550 runfast:
2551 tdsigwakeup(td, sig, action, intrval);
2552 PROC_SLOCK(p);
2553 thread_unsuspend(p);
2554 PROC_SUNLOCK(p);
2555 out_cont:
2556 itimer_proc_continue(p);
2557 kqtimer_proc_continue(p);
2558
2559 return (0);
2560 }
2561
2562 /*
2563 * The force of a signal has been directed against a single
2564 * thread. We need to see what we can do about knocking it
2565 * out of any sleep it may be in etc.
2566 */
2567 static void
tdsigwakeup(struct thread * td,int sig,sig_t action,int intrval)2568 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2569 {
2570 struct proc *p = td->td_proc;
2571 int prop;
2572
2573 PROC_LOCK_ASSERT(p, MA_OWNED);
2574 prop = sigprop(sig);
2575
2576 PROC_SLOCK(p);
2577 thread_lock(td);
2578 /*
2579 * Bring the priority of a thread up if we want it to get
2580 * killed in this lifetime. Be careful to avoid bumping the
2581 * priority of the idle thread, since we still allow to signal
2582 * kernel processes.
2583 */
2584 if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 &&
2585 td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2586 sched_prio(td, PUSER);
2587 if (TD_ON_SLEEPQ(td)) {
2588 /*
2589 * If thread is sleeping uninterruptibly
2590 * we can't interrupt the sleep... the signal will
2591 * be noticed when the process returns through
2592 * trap() or syscall().
2593 */
2594 if ((td->td_flags & TDF_SINTR) == 0)
2595 goto out;
2596 /*
2597 * If SIGCONT is default (or ignored) and process is
2598 * asleep, we are finished; the process should not
2599 * be awakened.
2600 */
2601 if ((prop & SIGPROP_CONT) && action == SIG_DFL) {
2602 thread_unlock(td);
2603 PROC_SUNLOCK(p);
2604 sigqueue_delete(&p->p_sigqueue, sig);
2605 /*
2606 * It may be on either list in this state.
2607 * Remove from both for now.
2608 */
2609 sigqueue_delete(&td->td_sigqueue, sig);
2610 return;
2611 }
2612
2613 /*
2614 * Don't awaken a sleeping thread for SIGSTOP if the
2615 * STOP signal is deferred.
2616 */
2617 if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY |
2618 TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
2619 goto out;
2620
2621 /*
2622 * Give low priority threads a better chance to run.
2623 */
2624 if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2625 sched_prio(td, PUSER);
2626
2627 sig_sleepq_abort(td, intrval);
2628 PROC_SUNLOCK(p);
2629 return;
2630 }
2631
2632 /*
2633 * Other states do nothing with the signal immediately,
2634 * other than kicking ourselves if we are running.
2635 * It will either never be noticed, or noticed very soon.
2636 */
2637 #ifdef SMP
2638 if (TD_IS_RUNNING(td) && td != curthread)
2639 forward_signal(td);
2640 #endif
2641
2642 out:
2643 PROC_SUNLOCK(p);
2644 thread_unlock(td);
2645 }
2646
2647 static void
ptrace_coredumpreq(struct thread * td,struct proc * p,struct thr_coredump_req * tcq)2648 ptrace_coredumpreq(struct thread *td, struct proc *p,
2649 struct thr_coredump_req *tcq)
2650 {
2651 struct coredump_vnode_ctx wctx;
2652 struct coredump_writer cdw;
2653 void *rl_cookie;
2654
2655 if (p->p_sysent->sv_coredump == NULL) {
2656 tcq->tc_error = ENOSYS;
2657 return;
2658 }
2659
2660 memset(&wctx, 0, sizeof(wctx));
2661 wctx.vp = tcq->tc_vp;
2662 wctx.fcred = NOCRED;
2663
2664 memset(&cdw, 0, sizeof(wctx));
2665 cdw.ctx = &wctx;
2666 cdw.write_fn = core_vn_write;
2667 cdw.extend_fn = core_vn_extend;
2668
2669 rl_cookie = vn_rangelock_wlock(tcq->tc_vp, 0, OFF_MAX);
2670 tcq->tc_error = p->p_sysent->sv_coredump(td, &cdw,
2671 tcq->tc_limit, tcq->tc_flags);
2672 vn_rangelock_unlock(tcq->tc_vp, rl_cookie);
2673 }
2674
2675 static void
ptrace_syscallreq(struct thread * td,struct proc * p,struct thr_syscall_req * tsr)2676 ptrace_syscallreq(struct thread *td, struct proc *p,
2677 struct thr_syscall_req *tsr)
2678 {
2679 struct sysentvec *sv;
2680 struct sysent *se;
2681 register_t rv_saved[2];
2682 int error, nerror;
2683 int sc;
2684 bool audited, sy_thr_static;
2685
2686 sv = p->p_sysent;
2687 if (sv->sv_table == NULL || sv->sv_size < tsr->ts_sa.code) {
2688 tsr->ts_ret.sr_error = ENOSYS;
2689 return;
2690 }
2691
2692 sc = tsr->ts_sa.code;
2693 if (sc == SYS_syscall || sc == SYS___syscall) {
2694 sc = tsr->ts_sa.args[0];
2695 memmove(&tsr->ts_sa.args[0], &tsr->ts_sa.args[1],
2696 sizeof(register_t) * (tsr->ts_nargs - 1));
2697 }
2698
2699 tsr->ts_sa.callp = se = &sv->sv_table[sc];
2700
2701 VM_CNT_INC(v_syscall);
2702 td->td_pticks = 0;
2703 if (__predict_false(td->td_cowgen != atomic_load_int(
2704 &td->td_proc->p_cowgen)))
2705 thread_cow_update(td);
2706
2707 td->td_sa = tsr->ts_sa;
2708
2709 #ifdef CAPABILITY_MODE
2710 if ((se->sy_flags & SYF_CAPENABLED) == 0) {
2711 if (CAP_TRACING(td))
2712 ktrcapfail(CAPFAIL_SYSCALL, NULL);
2713 if (IN_CAPABILITY_MODE(td)) {
2714 tsr->ts_ret.sr_error = ECAPMODE;
2715 return;
2716 }
2717 }
2718 #endif
2719
2720 sy_thr_static = (se->sy_thrcnt & SY_THR_STATIC) != 0;
2721 audited = AUDIT_SYSCALL_ENTER(sc, td) != 0;
2722
2723 if (!sy_thr_static) {
2724 error = syscall_thread_enter(td, &se);
2725 sy_thr_static = (se->sy_thrcnt & SY_THR_STATIC) != 0;
2726 if (error != 0) {
2727 tsr->ts_ret.sr_error = error;
2728 return;
2729 }
2730 }
2731
2732 rv_saved[0] = td->td_retval[0];
2733 rv_saved[1] = td->td_retval[1];
2734 nerror = td->td_errno;
2735 td->td_retval[0] = 0;
2736 td->td_retval[1] = 0;
2737
2738 #ifdef KDTRACE_HOOKS
2739 if (se->sy_entry != 0)
2740 (*systrace_probe_func)(&tsr->ts_sa, SYSTRACE_ENTRY, 0);
2741 #endif
2742 tsr->ts_ret.sr_error = se->sy_call(td, tsr->ts_sa.args);
2743 #ifdef KDTRACE_HOOKS
2744 if (se->sy_return != 0)
2745 (*systrace_probe_func)(&tsr->ts_sa, SYSTRACE_RETURN,
2746 tsr->ts_ret.sr_error != 0 ? -1 : td->td_retval[0]);
2747 #endif
2748
2749 tsr->ts_ret.sr_retval[0] = td->td_retval[0];
2750 tsr->ts_ret.sr_retval[1] = td->td_retval[1];
2751 td->td_retval[0] = rv_saved[0];
2752 td->td_retval[1] = rv_saved[1];
2753 td->td_errno = nerror;
2754
2755 if (audited)
2756 AUDIT_SYSCALL_EXIT(error, td);
2757 if (!sy_thr_static)
2758 syscall_thread_exit(td, se);
2759 }
2760
2761 static void
ptrace_remotereq(struct thread * td,int flag)2762 ptrace_remotereq(struct thread *td, int flag)
2763 {
2764 struct proc *p;
2765
2766 MPASS(td == curthread);
2767 p = td->td_proc;
2768 PROC_LOCK_ASSERT(p, MA_OWNED);
2769 if ((td->td_dbgflags & flag) == 0)
2770 return;
2771 KASSERT((p->p_flag & P_STOPPED_TRACE) != 0, ("not stopped"));
2772 KASSERT(td->td_remotereq != NULL, ("td_remotereq is NULL"));
2773
2774 PROC_UNLOCK(p);
2775 switch (flag) {
2776 case TDB_COREDUMPREQ:
2777 ptrace_coredumpreq(td, p, td->td_remotereq);
2778 break;
2779 case TDB_SCREMOTEREQ:
2780 ptrace_syscallreq(td, p, td->td_remotereq);
2781 break;
2782 default:
2783 __unreachable();
2784 }
2785 PROC_LOCK(p);
2786
2787 MPASS((td->td_dbgflags & flag) != 0);
2788 td->td_dbgflags &= ~flag;
2789 td->td_remotereq = NULL;
2790 wakeup(p);
2791 }
2792
2793 /*
2794 * Suspend threads of the process p, either by directly setting the
2795 * inhibitor for the thread sleeping interruptibly, or by making the
2796 * thread suspend at the userspace boundary by scheduling a suspend AST.
2797 *
2798 * Returns true if some threads were suspended directly from the
2799 * sleeping state, and false if all threads are forced to process AST.
2800 */
2801 static bool
sig_suspend_threads(struct thread * td,struct proc * p)2802 sig_suspend_threads(struct thread *td, struct proc *p)
2803 {
2804 struct thread *td2;
2805 bool res;
2806
2807 PROC_LOCK_ASSERT(p, MA_OWNED);
2808 PROC_SLOCK_ASSERT(p, MA_OWNED);
2809
2810 res = false;
2811 FOREACH_THREAD_IN_PROC(p, td2) {
2812 thread_lock(td2);
2813 ast_sched_locked(td2, TDA_SUSPEND);
2814 if (TD_IS_SLEEPING(td2) && (td2->td_flags & TDF_SINTR) != 0) {
2815 if (td2->td_flags & TDF_SBDRY) {
2816 /*
2817 * Once a thread is asleep with
2818 * TDF_SBDRY and without TDF_SERESTART
2819 * or TDF_SEINTR set, it should never
2820 * become suspended due to this check.
2821 */
2822 KASSERT(!TD_IS_SUSPENDED(td2),
2823 ("thread with deferred stops suspended"));
2824 if (TD_SBDRY_INTR(td2)) {
2825 sleepq_abort(td2, TD_SBDRY_ERRNO(td2));
2826 continue;
2827 }
2828 } else if (!TD_IS_SUSPENDED(td2)) {
2829 thread_suspend_one(td2);
2830 res = true;
2831 }
2832 } else if (!TD_IS_SUSPENDED(td2)) {
2833 #ifdef SMP
2834 if (TD_IS_RUNNING(td2) && td2 != td)
2835 forward_signal(td2);
2836 #endif
2837 }
2838 thread_unlock(td2);
2839 }
2840 return (res);
2841 }
2842
2843 static void
sig_handle_first_stop(struct thread * td,struct proc * p,int sig)2844 sig_handle_first_stop(struct thread *td, struct proc *p, int sig)
2845 {
2846 if (td != NULL && (td->td_dbgflags & TDB_FSTP) == 0 &&
2847 ((p->p_flag2 & P2_PTRACE_FSTP) != 0 || p->p_xthread != NULL))
2848 return;
2849
2850 p->p_xsig = sig;
2851 p->p_xthread = td;
2852
2853 /*
2854 * If we are on sleepqueue already, let sleepqueue
2855 * code decide if it needs to go sleep after attach.
2856 */
2857 if (td != NULL && td->td_wchan == NULL)
2858 td->td_dbgflags &= ~TDB_FSTP;
2859
2860 p->p_flag2 &= ~P2_PTRACE_FSTP;
2861 p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE;
2862 if (sig_suspend_threads(td, p) && td == NULL)
2863 thread_stopped(p);
2864 }
2865
2866 /*
2867 * Stop the process for an event deemed interesting to the debugger. If si is
2868 * non-NULL, this is a signal exchange; the new signal requested by the
2869 * debugger will be returned for handling. If si is NULL, this is some other
2870 * type of interesting event. The debugger may request a signal be delivered in
2871 * that case as well, however it will be deferred until it can be handled.
2872 */
2873 int
ptracestop(struct thread * td,int sig,ksiginfo_t * si)2874 ptracestop(struct thread *td, int sig, ksiginfo_t *si)
2875 {
2876 struct proc *p = td->td_proc;
2877 struct thread *td2;
2878 ksiginfo_t ksi;
2879
2880 PROC_LOCK_ASSERT(p, MA_OWNED);
2881 KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
2882 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2883 &p->p_mtx.lock_object, "Stopping for traced signal");
2884
2885 td->td_xsig = sig;
2886
2887 if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) {
2888 td->td_dbgflags |= TDB_XSIG;
2889 CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d",
2890 td->td_tid, p->p_pid, td->td_dbgflags, sig);
2891 PROC_SLOCK(p);
2892 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2893 if (P_KILLED(p)) {
2894 /*
2895 * Ensure that, if we've been PT_KILLed, the
2896 * exit status reflects that. Another thread
2897 * may also be in ptracestop(), having just
2898 * received the SIGKILL, but this thread was
2899 * unsuspended first.
2900 */
2901 td->td_dbgflags &= ~TDB_XSIG;
2902 td->td_xsig = SIGKILL;
2903 p->p_ptevents = 0;
2904 break;
2905 }
2906 if (p->p_flag & P_SINGLE_EXIT &&
2907 !(td->td_dbgflags & TDB_EXIT)) {
2908 /*
2909 * Ignore ptrace stops except for thread exit
2910 * events when the process exits.
2911 */
2912 td->td_dbgflags &= ~TDB_XSIG;
2913 PROC_SUNLOCK(p);
2914 return (0);
2915 }
2916
2917 /*
2918 * Make wait(2) work. Ensure that right after the
2919 * attach, the thread which was decided to become the
2920 * leader of attach gets reported to the waiter.
2921 * Otherwise, just avoid overwriting another thread's
2922 * assignment to p_xthread. If another thread has
2923 * already set p_xthread, the current thread will get
2924 * a chance to report itself upon the next iteration.
2925 */
2926 sig_handle_first_stop(td, p, sig);
2927
2928 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
2929 td->td_dbgflags &= ~TDB_STOPATFORK;
2930 }
2931 stopme:
2932 td->td_dbgflags |= TDB_SSWITCH;
2933 thread_suspend_switch(td, p);
2934 td->td_dbgflags &= ~TDB_SSWITCH;
2935 if ((td->td_dbgflags & (TDB_COREDUMPREQ |
2936 TDB_SCREMOTEREQ)) != 0) {
2937 MPASS((td->td_dbgflags & (TDB_COREDUMPREQ |
2938 TDB_SCREMOTEREQ)) !=
2939 (TDB_COREDUMPREQ | TDB_SCREMOTEREQ));
2940 PROC_SUNLOCK(p);
2941 ptrace_remotereq(td, td->td_dbgflags &
2942 (TDB_COREDUMPREQ | TDB_SCREMOTEREQ));
2943 PROC_SLOCK(p);
2944 goto stopme;
2945 }
2946 if (p->p_xthread == td)
2947 p->p_xthread = NULL;
2948 if (!(p->p_flag & P_TRACED))
2949 break;
2950 if (td->td_dbgflags & TDB_SUSPEND) {
2951 if (p->p_flag & P_SINGLE_EXIT)
2952 break;
2953 goto stopme;
2954 }
2955 }
2956 PROC_SUNLOCK(p);
2957 }
2958
2959 if (si != NULL && sig == td->td_xsig) {
2960 /* Parent wants us to take the original signal unchanged. */
2961 si->ksi_flags |= KSI_HEAD;
2962 if (sigqueue_add(&td->td_sigqueue, sig, si) != 0)
2963 si->ksi_signo = 0;
2964 } else if (td->td_xsig != 0) {
2965 /*
2966 * If parent wants us to take a new signal, then it will leave
2967 * it in td->td_xsig; otherwise we just look for signals again.
2968 */
2969 ksiginfo_init(&ksi);
2970 ksi.ksi_signo = td->td_xsig;
2971 ksi.ksi_flags |= KSI_PTRACE;
2972 td2 = sigtd(p, td->td_xsig, false);
2973 tdsendsignal(p, td2, td->td_xsig, &ksi);
2974 if (td != td2)
2975 return (0);
2976 }
2977
2978 return (td->td_xsig);
2979 }
2980
2981 static void
reschedule_signals(struct proc * p,sigset_t block,int flags)2982 reschedule_signals(struct proc *p, sigset_t block, int flags)
2983 {
2984 struct sigacts *ps;
2985 struct thread *td;
2986 int sig;
2987 bool fastblk, pslocked;
2988
2989 PROC_LOCK_ASSERT(p, MA_OWNED);
2990 ps = p->p_sigacts;
2991 pslocked = (flags & SIGPROCMASK_PS_LOCKED) != 0;
2992 mtx_assert(&ps->ps_mtx, pslocked ? MA_OWNED : MA_NOTOWNED);
2993 if (SIGISEMPTY(p->p_siglist))
2994 return;
2995 SIGSETAND(block, p->p_siglist);
2996 fastblk = (flags & SIGPROCMASK_FASTBLK) != 0;
2997 SIG_FOREACH(sig, &block) {
2998 td = sigtd(p, sig, fastblk);
2999
3000 /*
3001 * If sigtd() selected us despite sigfastblock is
3002 * blocking, do not activate AST or wake us, to avoid
3003 * loop in AST handler.
3004 */
3005 if (fastblk && td == curthread)
3006 continue;
3007
3008 signotify(td);
3009 if (!pslocked)
3010 mtx_lock(&ps->ps_mtx);
3011 if (p->p_flag & P_TRACED ||
3012 (SIGISMEMBER(ps->ps_sigcatch, sig) &&
3013 !SIGISMEMBER(td->td_sigmask, sig))) {
3014 tdsigwakeup(td, sig, SIG_CATCH,
3015 (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
3016 ERESTART));
3017 }
3018 if (!pslocked)
3019 mtx_unlock(&ps->ps_mtx);
3020 }
3021 }
3022
3023 void
tdsigcleanup(struct thread * td)3024 tdsigcleanup(struct thread *td)
3025 {
3026 struct proc *p;
3027 sigset_t unblocked;
3028
3029 p = td->td_proc;
3030 PROC_LOCK_ASSERT(p, MA_OWNED);
3031
3032 sigqueue_flush(&td->td_sigqueue);
3033 if (p->p_numthreads == 1)
3034 return;
3035
3036 /*
3037 * Since we cannot handle signals, notify signal post code
3038 * about this by filling the sigmask.
3039 *
3040 * Also, if needed, wake up thread(s) that do not block the
3041 * same signals as the exiting thread, since the thread might
3042 * have been selected for delivery and woken up.
3043 */
3044 SIGFILLSET(unblocked);
3045 SIGSETNAND(unblocked, td->td_sigmask);
3046 SIGFILLSET(td->td_sigmask);
3047 reschedule_signals(p, unblocked, 0);
3048
3049 }
3050
3051 static int
sigdeferstop_curr_flags(int cflags)3052 sigdeferstop_curr_flags(int cflags)
3053 {
3054
3055 MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 ||
3056 (cflags & TDF_SBDRY) != 0);
3057 return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART));
3058 }
3059
3060 /*
3061 * Defer the delivery of SIGSTOP for the current thread, according to
3062 * the requested mode. Returns previous flags, which must be restored
3063 * by sigallowstop().
3064 *
3065 * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and
3066 * cleared by the current thread, which allow the lock-less read-only
3067 * accesses below.
3068 */
3069 int
sigdeferstop_impl(int mode)3070 sigdeferstop_impl(int mode)
3071 {
3072 struct thread *td;
3073 int cflags, nflags;
3074
3075 td = curthread;
3076 cflags = sigdeferstop_curr_flags(td->td_flags);
3077 switch (mode) {
3078 case SIGDEFERSTOP_NOP:
3079 nflags = cflags;
3080 break;
3081 case SIGDEFERSTOP_OFF:
3082 nflags = 0;
3083 break;
3084 case SIGDEFERSTOP_SILENT:
3085 nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART);
3086 break;
3087 case SIGDEFERSTOP_EINTR:
3088 nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART;
3089 break;
3090 case SIGDEFERSTOP_ERESTART:
3091 nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR;
3092 break;
3093 default:
3094 panic("sigdeferstop: invalid mode %x", mode);
3095 break;
3096 }
3097 if (cflags == nflags)
3098 return (SIGDEFERSTOP_VAL_NCHG);
3099 thread_lock(td);
3100 td->td_flags = (td->td_flags & ~cflags) | nflags;
3101 thread_unlock(td);
3102 return (cflags);
3103 }
3104
3105 /*
3106 * Restores the STOP handling mode, typically permitting the delivery
3107 * of SIGSTOP for the current thread. This does not immediately
3108 * suspend if a stop was posted. Instead, the thread will suspend
3109 * either via ast() or a subsequent interruptible sleep.
3110 */
3111 void
sigallowstop_impl(int prev)3112 sigallowstop_impl(int prev)
3113 {
3114 struct thread *td;
3115 int cflags;
3116
3117 KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop"));
3118 KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0,
3119 ("sigallowstop: incorrect previous mode %x", prev));
3120 td = curthread;
3121 cflags = sigdeferstop_curr_flags(td->td_flags);
3122 if (cflags != prev) {
3123 thread_lock(td);
3124 td->td_flags = (td->td_flags & ~cflags) | prev;
3125 thread_unlock(td);
3126 }
3127 }
3128
3129 enum sigstatus {
3130 SIGSTATUS_HANDLE,
3131 SIGSTATUS_HANDLED,
3132 SIGSTATUS_IGNORE,
3133 SIGSTATUS_SBDRY_STOP,
3134 };
3135
3136 /*
3137 * The thread has signal "sig" pending. Figure out what to do with it:
3138 *
3139 * _HANDLE -> the caller should handle the signal
3140 * _HANDLED -> handled internally, reload pending signal set
3141 * _IGNORE -> ignored, remove from the set of pending signals and try the
3142 * next pending signal
3143 * _SBDRY_STOP -> the signal should stop the thread but this is not
3144 * permitted in the current context
3145 */
3146 static enum sigstatus
sigprocess(struct thread * td,int sig)3147 sigprocess(struct thread *td, int sig)
3148 {
3149 struct proc *p;
3150 struct sigacts *ps;
3151 struct sigqueue *queue;
3152 ksiginfo_t ksi;
3153 int prop;
3154
3155 KASSERT(_SIG_VALID(sig), ("%s: invalid signal %d", __func__, sig));
3156
3157 p = td->td_proc;
3158 ps = p->p_sigacts;
3159 mtx_assert(&ps->ps_mtx, MA_OWNED);
3160 PROC_LOCK_ASSERT(p, MA_OWNED);
3161
3162 /*
3163 * We should allow pending but ignored signals below
3164 * if there is sigwait() active, or P_TRACED was
3165 * on when they were posted.
3166 */
3167 if (SIGISMEMBER(ps->ps_sigignore, sig) &&
3168 (p->p_flag & P_TRACED) == 0 &&
3169 (td->td_flags & TDF_SIGWAIT) == 0) {
3170 return (SIGSTATUS_IGNORE);
3171 }
3172
3173 /*
3174 * If the process is going to single-thread mode to prepare
3175 * for exit, there is no sense in delivering any signal
3176 * to usermode. Another important consequence is that
3177 * msleep(..., PCATCH, ...) now is only interruptible by a
3178 * suspend request.
3179 */
3180 if ((p->p_flag2 & P2_WEXIT) != 0)
3181 return (SIGSTATUS_IGNORE);
3182
3183 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) {
3184 /*
3185 * If traced, always stop.
3186 * Remove old signal from queue before the stop.
3187 * XXX shrug off debugger, it causes siginfo to
3188 * be thrown away.
3189 */
3190 queue = &td->td_sigqueue;
3191 ksiginfo_init(&ksi);
3192 if (sigqueue_get(queue, sig, &ksi) == 0) {
3193 queue = &p->p_sigqueue;
3194 sigqueue_get(queue, sig, &ksi);
3195 }
3196 td->td_si = ksi.ksi_info;
3197
3198 mtx_unlock(&ps->ps_mtx);
3199 sig = ptracestop(td, sig, &ksi);
3200 mtx_lock(&ps->ps_mtx);
3201
3202 td->td_si.si_signo = 0;
3203
3204 /*
3205 * Keep looking if the debugger discarded or
3206 * replaced the signal.
3207 */
3208 if (sig == 0)
3209 return (SIGSTATUS_HANDLED);
3210
3211 /*
3212 * If the signal became masked, re-queue it.
3213 */
3214 if (SIGISMEMBER(td->td_sigmask, sig)) {
3215 ksi.ksi_flags |= KSI_HEAD;
3216 sigqueue_add(&p->p_sigqueue, sig, &ksi);
3217 return (SIGSTATUS_HANDLED);
3218 }
3219
3220 /*
3221 * If the traced bit got turned off, requeue the signal and
3222 * reload the set of pending signals. This ensures that p_sig*
3223 * and p_sigact are consistent.
3224 */
3225 if ((p->p_flag & P_TRACED) == 0) {
3226 if ((ksi.ksi_flags & KSI_PTRACE) == 0) {
3227 ksi.ksi_flags |= KSI_HEAD;
3228 sigqueue_add(queue, sig, &ksi);
3229 }
3230 return (SIGSTATUS_HANDLED);
3231 }
3232 }
3233
3234 /*
3235 * Decide whether the signal should be returned.
3236 * Return the signal's number, or fall through
3237 * to clear it from the pending mask.
3238 */
3239 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
3240 case (intptr_t)SIG_DFL:
3241 /*
3242 * Don't take default actions on system processes.
3243 */
3244 if (p->p_pid <= 1) {
3245 #ifdef DIAGNOSTIC
3246 /*
3247 * Are you sure you want to ignore SIGSEGV
3248 * in init? XXX
3249 */
3250 printf("Process (pid %lu) got signal %d\n",
3251 (u_long)p->p_pid, sig);
3252 #endif
3253 return (SIGSTATUS_IGNORE);
3254 }
3255
3256 /*
3257 * If there is a pending stop signal to process with
3258 * default action, stop here, then clear the signal.
3259 * Traced or exiting processes should ignore stops.
3260 * Additionally, a member of an orphaned process group
3261 * should ignore tty stops.
3262 */
3263 prop = sigprop(sig);
3264 if (prop & SIGPROP_STOP) {
3265 mtx_unlock(&ps->ps_mtx);
3266 if ((p->p_flag & (P_TRACED | P_WEXIT |
3267 P_SINGLE_EXIT)) != 0 || ((p->p_pgrp->
3268 pg_flags & PGRP_ORPHANED) != 0 &&
3269 (prop & SIGPROP_TTYSTOP) != 0)) {
3270 mtx_lock(&ps->ps_mtx);
3271 return (SIGSTATUS_IGNORE);
3272 }
3273 if (TD_SBDRY_INTR(td)) {
3274 KASSERT((td->td_flags & TDF_SBDRY) != 0,
3275 ("lost TDF_SBDRY"));
3276 mtx_lock(&ps->ps_mtx);
3277 return (SIGSTATUS_SBDRY_STOP);
3278 }
3279 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
3280 &p->p_mtx.lock_object, "Catching SIGSTOP");
3281 sigqueue_delete(&td->td_sigqueue, sig);
3282 sigqueue_delete(&p->p_sigqueue, sig);
3283 p->p_flag |= P_STOPPED_SIG;
3284 p->p_xsig = sig;
3285 PROC_SLOCK(p);
3286 sig_suspend_threads(td, p);
3287 thread_suspend_switch(td, p);
3288 PROC_SUNLOCK(p);
3289 mtx_lock(&ps->ps_mtx);
3290 return (SIGSTATUS_HANDLED);
3291 } else if ((prop & SIGPROP_IGNORE) != 0 &&
3292 (td->td_flags & TDF_SIGWAIT) == 0) {
3293 /*
3294 * Default action is to ignore; drop it if
3295 * not in kern_sigtimedwait().
3296 */
3297 return (SIGSTATUS_IGNORE);
3298 } else {
3299 return (SIGSTATUS_HANDLE);
3300 }
3301
3302 case (intptr_t)SIG_IGN:
3303 if ((td->td_flags & TDF_SIGWAIT) == 0)
3304 return (SIGSTATUS_IGNORE);
3305 else
3306 return (SIGSTATUS_HANDLE);
3307
3308 default:
3309 /*
3310 * This signal has an action, let postsig() process it.
3311 */
3312 return (SIGSTATUS_HANDLE);
3313 }
3314 }
3315
3316 /*
3317 * If the current process has received a signal (should be caught or cause
3318 * termination, should interrupt current syscall), return the signal number.
3319 * Stop signals with default action are processed immediately, then cleared;
3320 * they aren't returned. This is checked after each entry to the system for
3321 * a syscall or trap (though this can usually be done without calling
3322 * issignal by checking the pending signal masks in cursig.) The normal call
3323 * sequence is
3324 *
3325 * while (sig = cursig(curthread))
3326 * postsig(sig);
3327 */
3328 static int
issignal(struct thread * td)3329 issignal(struct thread *td)
3330 {
3331 struct proc *p;
3332 sigset_t sigpending;
3333 int sig;
3334
3335 p = td->td_proc;
3336 PROC_LOCK_ASSERT(p, MA_OWNED);
3337
3338 for (;;) {
3339 sigpending = td->td_sigqueue.sq_signals;
3340 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
3341 SIGSETNAND(sigpending, td->td_sigmask);
3342
3343 if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags &
3344 (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
3345 SIG_STOPSIGMASK(sigpending);
3346 if (SIGISEMPTY(sigpending)) /* no signal to send */
3347 return (0);
3348
3349 /*
3350 * Do fast sigblock if requested by usermode. Since
3351 * we do know that there was a signal pending at this
3352 * point, set the FAST_SIGBLOCK_PEND as indicator for
3353 * usermode to perform a dummy call to
3354 * FAST_SIGBLOCK_UNBLOCK, which causes immediate
3355 * delivery of postponed pending signal.
3356 */
3357 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
3358 if (td->td_sigblock_val != 0)
3359 SIGSETNAND(sigpending, fastblock_mask);
3360 if (SIGISEMPTY(sigpending)) {
3361 td->td_pflags |= TDP_SIGFASTPENDING;
3362 return (0);
3363 }
3364 }
3365
3366 if (!pt_attach_transparent &&
3367 (p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED &&
3368 (p->p_flag2 & P2_PTRACE_FSTP) != 0 &&
3369 SIGISMEMBER(sigpending, SIGSTOP)) {
3370 /*
3371 * If debugger just attached, always consume
3372 * SIGSTOP from ptrace(PT_ATTACH) first, to
3373 * execute the debugger attach ritual in
3374 * order.
3375 */
3376 td->td_dbgflags |= TDB_FSTP;
3377 SIGEMPTYSET(sigpending);
3378 SIGADDSET(sigpending, SIGSTOP);
3379 }
3380
3381 SIG_FOREACH(sig, &sigpending) {
3382 switch (sigprocess(td, sig)) {
3383 case SIGSTATUS_HANDLE:
3384 return (sig);
3385 case SIGSTATUS_HANDLED:
3386 goto next;
3387 case SIGSTATUS_IGNORE:
3388 sigqueue_delete(&td->td_sigqueue, sig);
3389 sigqueue_delete(&p->p_sigqueue, sig);
3390 break;
3391 case SIGSTATUS_SBDRY_STOP:
3392 return (-1);
3393 }
3394 }
3395 next:;
3396 }
3397 }
3398
3399 void
thread_stopped(struct proc * p)3400 thread_stopped(struct proc *p)
3401 {
3402 int n;
3403
3404 PROC_LOCK_ASSERT(p, MA_OWNED);
3405 PROC_SLOCK_ASSERT(p, MA_OWNED);
3406 n = p->p_suspcount;
3407 if (p == curproc)
3408 n++;
3409 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
3410 PROC_SUNLOCK(p);
3411 p->p_flag &= ~P_WAITED;
3412 PROC_LOCK(p->p_pptr);
3413 childproc_stopped(p, (p->p_flag & P_TRACED) ?
3414 CLD_TRAPPED : CLD_STOPPED);
3415 PROC_UNLOCK(p->p_pptr);
3416 PROC_SLOCK(p);
3417 }
3418 }
3419
3420 /*
3421 * Take the action for the specified signal
3422 * from the current set of pending signals.
3423 */
3424 int
postsig(int sig)3425 postsig(int sig)
3426 {
3427 struct thread *td;
3428 struct proc *p;
3429 struct sigacts *ps;
3430 sig_t action;
3431 ksiginfo_t ksi;
3432 sigset_t returnmask;
3433
3434 KASSERT(sig != 0, ("postsig"));
3435
3436 td = curthread;
3437 p = td->td_proc;
3438 PROC_LOCK_ASSERT(p, MA_OWNED);
3439 ps = p->p_sigacts;
3440 mtx_assert(&ps->ps_mtx, MA_OWNED);
3441 ksiginfo_init(&ksi);
3442 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
3443 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
3444 return (0);
3445 ksi.ksi_signo = sig;
3446 if (ksi.ksi_code == SI_TIMER)
3447 itimer_accept(p, ksi.ksi_timerid, &ksi);
3448 action = ps->ps_sigact[_SIG_IDX(sig)];
3449 #ifdef KTRACE
3450 if (KTRPOINT(td, KTR_PSIG))
3451 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
3452 &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
3453 #endif
3454
3455 if (action == SIG_DFL) {
3456 /*
3457 * Default action, where the default is to kill
3458 * the process. (Other cases were ignored above.)
3459 */
3460 mtx_unlock(&ps->ps_mtx);
3461 proc_td_siginfo_capture(td, &ksi.ksi_info);
3462 sigexit(td, sig);
3463 /* NOTREACHED */
3464 } else {
3465 /*
3466 * If we get here, the signal must be caught.
3467 */
3468 KASSERT(action != SIG_IGN, ("postsig action %p", action));
3469 KASSERT(!SIGISMEMBER(td->td_sigmask, sig),
3470 ("postsig action: blocked sig %d", sig));
3471
3472 /*
3473 * Set the new mask value and also defer further
3474 * occurrences of this signal.
3475 *
3476 * Special case: user has done a sigsuspend. Here the
3477 * current mask is not of interest, but rather the
3478 * mask from before the sigsuspend is what we want
3479 * restored after the signal processing is completed.
3480 */
3481 if (td->td_pflags & TDP_OLDMASK) {
3482 returnmask = td->td_oldsigmask;
3483 td->td_pflags &= ~TDP_OLDMASK;
3484 } else
3485 returnmask = td->td_sigmask;
3486
3487 if (p->p_sig == sig) {
3488 p->p_sig = 0;
3489 }
3490 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
3491 postsig_done(sig, td, ps);
3492 }
3493 return (1);
3494 }
3495
3496 int
sig_ast_checksusp(struct thread * td)3497 sig_ast_checksusp(struct thread *td)
3498 {
3499 struct proc *p __diagused;
3500 int ret;
3501
3502 p = td->td_proc;
3503 PROC_LOCK_ASSERT(p, MA_OWNED);
3504
3505 if (!td_ast_pending(td, TDA_SUSPEND))
3506 return (0);
3507
3508 ret = thread_suspend_check(1);
3509 MPASS(ret == 0 || ret == EINTR || ret == ERESTART);
3510 return (ret);
3511 }
3512
3513 int
sig_ast_needsigchk(struct thread * td)3514 sig_ast_needsigchk(struct thread *td)
3515 {
3516 struct proc *p;
3517 struct sigacts *ps;
3518 int ret, sig;
3519
3520 p = td->td_proc;
3521 PROC_LOCK_ASSERT(p, MA_OWNED);
3522
3523 if (!td_ast_pending(td, TDA_SIG))
3524 return (0);
3525
3526 ps = p->p_sigacts;
3527 mtx_lock(&ps->ps_mtx);
3528 sig = cursig(td);
3529 if (sig == -1) {
3530 mtx_unlock(&ps->ps_mtx);
3531 KASSERT((td->td_flags & TDF_SBDRY) != 0, ("lost TDF_SBDRY"));
3532 KASSERT(TD_SBDRY_INTR(td),
3533 ("lost TDF_SERESTART of TDF_SEINTR"));
3534 KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) !=
3535 (TDF_SEINTR | TDF_SERESTART),
3536 ("both TDF_SEINTR and TDF_SERESTART"));
3537 ret = TD_SBDRY_ERRNO(td);
3538 } else if (sig != 0) {
3539 ret = SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : ERESTART;
3540 mtx_unlock(&ps->ps_mtx);
3541 } else {
3542 mtx_unlock(&ps->ps_mtx);
3543 ret = 0;
3544 }
3545
3546 /*
3547 * Do not go into sleep if this thread was the ptrace(2)
3548 * attach leader. cursig() consumed SIGSTOP from PT_ATTACH,
3549 * but we usually act on the signal by interrupting sleep, and
3550 * should do that here as well.
3551 */
3552 if ((td->td_dbgflags & TDB_FSTP) != 0) {
3553 if (ret == 0)
3554 ret = EINTR;
3555 td->td_dbgflags &= ~TDB_FSTP;
3556 }
3557
3558 return (ret);
3559 }
3560
3561 int
sig_intr(void)3562 sig_intr(void)
3563 {
3564 struct thread *td;
3565 struct proc *p;
3566 int ret;
3567
3568 td = curthread;
3569 if (!td_ast_pending(td, TDA_SIG) && !td_ast_pending(td, TDA_SUSPEND))
3570 return (0);
3571
3572 p = td->td_proc;
3573
3574 PROC_LOCK(p);
3575 ret = sig_ast_checksusp(td);
3576 if (ret == 0)
3577 ret = sig_ast_needsigchk(td);
3578 PROC_UNLOCK(p);
3579 return (ret);
3580 }
3581
3582 bool
curproc_sigkilled(void)3583 curproc_sigkilled(void)
3584 {
3585 struct thread *td;
3586 struct proc *p;
3587 struct sigacts *ps;
3588 bool res;
3589
3590 td = curthread;
3591 if (!td_ast_pending(td, TDA_SIG))
3592 return (false);
3593
3594 p = td->td_proc;
3595 PROC_LOCK(p);
3596 ps = p->p_sigacts;
3597 mtx_lock(&ps->ps_mtx);
3598 res = SIGISMEMBER(td->td_sigqueue.sq_signals, SIGKILL) ||
3599 SIGISMEMBER(p->p_sigqueue.sq_signals, SIGKILL);
3600 mtx_unlock(&ps->ps_mtx);
3601 PROC_UNLOCK(p);
3602 return (res);
3603 }
3604
3605 void
proc_wkilled(struct proc * p)3606 proc_wkilled(struct proc *p)
3607 {
3608
3609 PROC_LOCK_ASSERT(p, MA_OWNED);
3610 if ((p->p_flag & P_WKILLED) == 0)
3611 p->p_flag |= P_WKILLED;
3612 }
3613
3614 /*
3615 * Kill the current process for stated reason.
3616 */
3617 void
killproc(struct proc * p,const char * why)3618 killproc(struct proc *p, const char *why)
3619 {
3620
3621 PROC_LOCK_ASSERT(p, MA_OWNED);
3622 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid,
3623 p->p_comm);
3624 log(LOG_ERR, "pid %d (%s), jid %d, uid %d, was killed: %s\n",
3625 p->p_pid, p->p_comm, p->p_ucred->cr_prison->pr_id,
3626 p->p_ucred->cr_uid, why);
3627 proc_wkilled(p);
3628 kern_psignal(p, SIGKILL);
3629 }
3630
3631 /*
3632 * Send queued SIGCHLD to parent when child process's state
3633 * is changed.
3634 */
3635 static void
sigparent(struct proc * p,int reason,int status)3636 sigparent(struct proc *p, int reason, int status)
3637 {
3638 PROC_LOCK_ASSERT(p, MA_OWNED);
3639 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3640
3641 if (p->p_ksi != NULL) {
3642 p->p_ksi->ksi_signo = SIGCHLD;
3643 p->p_ksi->ksi_code = reason;
3644 p->p_ksi->ksi_status = status;
3645 p->p_ksi->ksi_pid = p->p_pid;
3646 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
3647 if (KSI_ONQ(p->p_ksi))
3648 return;
3649 }
3650
3651 /*
3652 * Do not consume p_ksi if parent is zombie, since signal is
3653 * dropped immediately. Instead, keep it since it might be
3654 * useful for reaper.
3655 */
3656 if (p->p_pptr->p_state != PRS_ZOMBIE)
3657 pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
3658 }
3659
3660 static void
childproc_jobstate(struct proc * p,int reason,int sig)3661 childproc_jobstate(struct proc *p, int reason, int sig)
3662 {
3663 struct sigacts *ps;
3664
3665 PROC_LOCK_ASSERT(p, MA_OWNED);
3666 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3667
3668 /*
3669 * Wake up parent sleeping in kern_wait(), also send
3670 * SIGCHLD to parent, but SIGCHLD does not guarantee
3671 * that parent will awake, because parent may masked
3672 * the signal.
3673 */
3674 p->p_pptr->p_flag |= P_STATCHILD;
3675 wakeup(p->p_pptr);
3676
3677 ps = p->p_pptr->p_sigacts;
3678 mtx_lock(&ps->ps_mtx);
3679 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
3680 mtx_unlock(&ps->ps_mtx);
3681 sigparent(p, reason, sig);
3682 } else
3683 mtx_unlock(&ps->ps_mtx);
3684 }
3685
3686 void
childproc_stopped(struct proc * p,int reason)3687 childproc_stopped(struct proc *p, int reason)
3688 {
3689
3690 childproc_jobstate(p, reason, p->p_xsig);
3691 }
3692
3693 void
childproc_continued(struct proc * p)3694 childproc_continued(struct proc *p)
3695 {
3696 PROC_LOCK_ASSERT(p, MA_OWNED);
3697 p->p_flag |= P_CONTINUED;
3698 p->p_xsig = SIGCONT;
3699 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
3700 }
3701
3702 void
childproc_exited(struct proc * p)3703 childproc_exited(struct proc *p)
3704 {
3705 int reason, status;
3706
3707 if (WCOREDUMP(p->p_xsig)) {
3708 reason = CLD_DUMPED;
3709 status = WTERMSIG(p->p_xsig);
3710 } else if (WIFSIGNALED(p->p_xsig)) {
3711 reason = CLD_KILLED;
3712 status = WTERMSIG(p->p_xsig);
3713 } else {
3714 reason = CLD_EXITED;
3715 status = p->p_xexit;
3716 }
3717 /*
3718 * XXX avoid calling wakeup(p->p_pptr), the work is
3719 * done in exit1().
3720 */
3721 sigparent(p, reason, status);
3722 }
3723
3724 /*
3725 * Nonexistent system call-- signal process (may want to handle it). Flag
3726 * error in case process won't see signal immediately (blocked or ignored).
3727 */
3728 #ifndef _SYS_SYSPROTO_H_
3729 struct nosys_args {
3730 int dummy;
3731 };
3732 #endif
3733 /* ARGSUSED */
3734 int
nosys(struct thread * td,struct nosys_args * args)3735 nosys(struct thread *td, struct nosys_args *args)
3736 {
3737 return (kern_nosys(td, args->dummy));
3738 }
3739
3740 int
kern_nosys(struct thread * td,int dummy)3741 kern_nosys(struct thread *td, int dummy)
3742 {
3743 struct proc *p;
3744
3745 p = td->td_proc;
3746
3747 if (SV_PROC_FLAG(p, SV_SIGSYS) != 0 && kern_signosys) {
3748 PROC_LOCK(p);
3749 tdsignal(td, SIGSYS);
3750 PROC_UNLOCK(p);
3751 }
3752 if (kern_lognosys == 1 || kern_lognosys == 3) {
3753 uprintf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
3754 td->td_sa.code);
3755 }
3756 if (kern_lognosys == 2 || kern_lognosys == 3 ||
3757 (p->p_pid == 1 && (kern_lognosys & 3) == 0)) {
3758 printf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
3759 td->td_sa.code);
3760 }
3761 return (ENOSYS);
3762 }
3763
3764 /*
3765 * Send a SIGIO or SIGURG signal to a process or process group using stored
3766 * credentials rather than those of the current process.
3767 */
3768 void
pgsigio(struct sigio ** sigiop,int sig,int checkctty)3769 pgsigio(struct sigio **sigiop, int sig, int checkctty)
3770 {
3771 ksiginfo_t ksi;
3772 struct sigio *sigio;
3773
3774 ksiginfo_init(&ksi);
3775 ksi.ksi_signo = sig;
3776 ksi.ksi_code = SI_KERNEL;
3777
3778 SIGIO_LOCK();
3779 sigio = *sigiop;
3780 if (sigio == NULL) {
3781 SIGIO_UNLOCK();
3782 return;
3783 }
3784 if (sigio->sio_pgid > 0) {
3785 PROC_LOCK(sigio->sio_proc);
3786 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
3787 kern_psignal(sigio->sio_proc, sig);
3788 PROC_UNLOCK(sigio->sio_proc);
3789 } else if (sigio->sio_pgid < 0) {
3790 struct proc *p;
3791
3792 PGRP_LOCK(sigio->sio_pgrp);
3793 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3794 PROC_LOCK(p);
3795 if (p->p_state == PRS_NORMAL &&
3796 CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3797 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
3798 kern_psignal(p, sig);
3799 PROC_UNLOCK(p);
3800 }
3801 PGRP_UNLOCK(sigio->sio_pgrp);
3802 }
3803 SIGIO_UNLOCK();
3804 }
3805
3806 static int
filt_sigattach(struct knote * kn)3807 filt_sigattach(struct knote *kn)
3808 {
3809 struct proc *p = curproc;
3810
3811 kn->kn_ptr.p_proc = p;
3812 kn->kn_flags |= EV_CLEAR; /* automatically set */
3813
3814 knlist_add(p->p_klist, kn, 0);
3815
3816 return (0);
3817 }
3818
3819 static void
filt_sigdetach(struct knote * kn)3820 filt_sigdetach(struct knote *kn)
3821 {
3822 knlist_remove(kn->kn_knlist, kn, 0);
3823 }
3824
3825 /*
3826 * signal knotes are shared with proc knotes, so we apply a mask to
3827 * the hint in order to differentiate them from process hints. This
3828 * could be avoided by using a signal-specific knote list, but probably
3829 * isn't worth the trouble.
3830 */
3831 static int
filt_signal(struct knote * kn,long hint)3832 filt_signal(struct knote *kn, long hint)
3833 {
3834
3835 if (hint & NOTE_SIGNAL) {
3836 hint &= ~NOTE_SIGNAL;
3837
3838 if (kn->kn_id == hint)
3839 kn->kn_data++;
3840 }
3841 return (kn->kn_data != 0);
3842 }
3843
3844 struct sigacts *
sigacts_alloc(void)3845 sigacts_alloc(void)
3846 {
3847 struct sigacts *ps;
3848
3849 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3850 refcount_init(&ps->ps_refcnt, 1);
3851 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
3852 return (ps);
3853 }
3854
3855 void
sigacts_free(struct sigacts * ps)3856 sigacts_free(struct sigacts *ps)
3857 {
3858
3859 if (refcount_release(&ps->ps_refcnt) == 0)
3860 return;
3861 mtx_destroy(&ps->ps_mtx);
3862 free(ps, M_SUBPROC);
3863 }
3864
3865 struct sigacts *
sigacts_hold(struct sigacts * ps)3866 sigacts_hold(struct sigacts *ps)
3867 {
3868
3869 refcount_acquire(&ps->ps_refcnt);
3870 return (ps);
3871 }
3872
3873 void
sigacts_copy(struct sigacts * dest,struct sigacts * src)3874 sigacts_copy(struct sigacts *dest, struct sigacts *src)
3875 {
3876
3877 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
3878 mtx_lock(&src->ps_mtx);
3879 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
3880 mtx_unlock(&src->ps_mtx);
3881 }
3882
3883 int
sigacts_shared(struct sigacts * ps)3884 sigacts_shared(struct sigacts *ps)
3885 {
3886
3887 return (ps->ps_refcnt > 1);
3888 }
3889
3890 void
sig_drop_caught(struct proc * p)3891 sig_drop_caught(struct proc *p)
3892 {
3893 int sig;
3894 struct sigacts *ps;
3895
3896 ps = p->p_sigacts;
3897 PROC_LOCK_ASSERT(p, MA_OWNED);
3898 mtx_assert(&ps->ps_mtx, MA_OWNED);
3899 SIG_FOREACH(sig, &ps->ps_sigcatch) {
3900 sigdflt(ps, sig);
3901 if ((sigprop(sig) & SIGPROP_IGNORE) != 0)
3902 sigqueue_delete_proc(p, sig);
3903 }
3904 }
3905
3906 static void
sigfastblock_failed(struct thread * td,bool sendsig,bool write)3907 sigfastblock_failed(struct thread *td, bool sendsig, bool write)
3908 {
3909 ksiginfo_t ksi;
3910
3911 /*
3912 * Prevent further fetches and SIGSEGVs, allowing thread to
3913 * issue syscalls despite corruption.
3914 */
3915 sigfastblock_clear(td);
3916
3917 if (!sendsig)
3918 return;
3919 ksiginfo_init_trap(&ksi);
3920 ksi.ksi_signo = SIGSEGV;
3921 ksi.ksi_code = write ? SEGV_ACCERR : SEGV_MAPERR;
3922 ksi.ksi_addr = td->td_sigblock_ptr;
3923 trapsignal(td, &ksi);
3924 }
3925
3926 static bool
sigfastblock_fetch_sig(struct thread * td,bool sendsig,uint32_t * valp)3927 sigfastblock_fetch_sig(struct thread *td, bool sendsig, uint32_t *valp)
3928 {
3929 uint32_t res;
3930
3931 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
3932 return (true);
3933 if (fueword32((void *)td->td_sigblock_ptr, &res) == -1) {
3934 sigfastblock_failed(td, sendsig, false);
3935 return (false);
3936 }
3937 *valp = res;
3938 td->td_sigblock_val = res & ~SIGFASTBLOCK_FLAGS;
3939 return (true);
3940 }
3941
3942 static void
sigfastblock_resched(struct thread * td,bool resched)3943 sigfastblock_resched(struct thread *td, bool resched)
3944 {
3945 struct proc *p;
3946
3947 if (resched) {
3948 p = td->td_proc;
3949 PROC_LOCK(p);
3950 reschedule_signals(p, td->td_sigmask, 0);
3951 PROC_UNLOCK(p);
3952 }
3953 ast_sched(td, TDA_SIG);
3954 }
3955
3956 int
sys_sigfastblock(struct thread * td,struct sigfastblock_args * uap)3957 sys_sigfastblock(struct thread *td, struct sigfastblock_args *uap)
3958 {
3959 struct proc *p;
3960 int error, res;
3961 uint32_t oldval;
3962
3963 error = 0;
3964 p = td->td_proc;
3965 switch (uap->cmd) {
3966 case SIGFASTBLOCK_SETPTR:
3967 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
3968 error = EBUSY;
3969 break;
3970 }
3971 if (((uintptr_t)(uap->ptr) & (sizeof(uint32_t) - 1)) != 0) {
3972 error = EINVAL;
3973 break;
3974 }
3975 td->td_pflags |= TDP_SIGFASTBLOCK;
3976 td->td_sigblock_ptr = uap->ptr;
3977 break;
3978
3979 case SIGFASTBLOCK_UNBLOCK:
3980 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
3981 error = EINVAL;
3982 break;
3983 }
3984
3985 for (;;) {
3986 res = casueword32(td->td_sigblock_ptr,
3987 SIGFASTBLOCK_PEND, &oldval, 0);
3988 if (res == -1) {
3989 error = EFAULT;
3990 sigfastblock_failed(td, false, true);
3991 break;
3992 }
3993 if (res == 0)
3994 break;
3995 MPASS(res == 1);
3996 if (oldval != SIGFASTBLOCK_PEND) {
3997 error = EBUSY;
3998 break;
3999 }
4000 error = thread_check_susp(td, false);
4001 if (error != 0)
4002 break;
4003 }
4004 if (error != 0)
4005 break;
4006
4007 /*
4008 * td_sigblock_val is cleared there, but not on a
4009 * syscall exit. The end effect is that a single
4010 * interruptible sleep, while user sigblock word is
4011 * set, might return EINTR or ERESTART to usermode
4012 * without delivering signal. All further sleeps,
4013 * until userspace clears the word and does
4014 * sigfastblock(UNBLOCK), observe current word and no
4015 * longer get interrupted. It is slight
4016 * non-conformance, with alternative to have read the
4017 * sigblock word on each syscall entry.
4018 */
4019 td->td_sigblock_val = 0;
4020
4021 /*
4022 * Rely on normal ast mechanism to deliver pending
4023 * signals to current thread. But notify others about
4024 * fake unblock.
4025 */
4026 sigfastblock_resched(td, error == 0 && p->p_numthreads != 1);
4027
4028 break;
4029
4030 case SIGFASTBLOCK_UNSETPTR:
4031 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4032 error = EINVAL;
4033 break;
4034 }
4035 if (!sigfastblock_fetch_sig(td, false, &oldval)) {
4036 error = EFAULT;
4037 break;
4038 }
4039 if (oldval != 0 && oldval != SIGFASTBLOCK_PEND) {
4040 error = EBUSY;
4041 break;
4042 }
4043 sigfastblock_clear(td);
4044 break;
4045
4046 default:
4047 error = EINVAL;
4048 break;
4049 }
4050 return (error);
4051 }
4052
4053 void
sigfastblock_clear(struct thread * td)4054 sigfastblock_clear(struct thread *td)
4055 {
4056 bool resched;
4057
4058 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4059 return;
4060 td->td_sigblock_val = 0;
4061 resched = (td->td_pflags & TDP_SIGFASTPENDING) != 0 ||
4062 SIGPENDING(td);
4063 td->td_pflags &= ~(TDP_SIGFASTBLOCK | TDP_SIGFASTPENDING);
4064 sigfastblock_resched(td, resched);
4065 }
4066
4067 void
sigfastblock_fetch(struct thread * td)4068 sigfastblock_fetch(struct thread *td)
4069 {
4070 uint32_t val;
4071
4072 (void)sigfastblock_fetch_sig(td, true, &val);
4073 }
4074
4075 static void
sigfastblock_setpend1(struct thread * td)4076 sigfastblock_setpend1(struct thread *td)
4077 {
4078 int res;
4079 uint32_t oldval;
4080
4081 if ((td->td_pflags & TDP_SIGFASTPENDING) == 0)
4082 return;
4083 res = fueword32((void *)td->td_sigblock_ptr, &oldval);
4084 if (res == -1) {
4085 sigfastblock_failed(td, true, false);
4086 return;
4087 }
4088 for (;;) {
4089 res = casueword32(td->td_sigblock_ptr, oldval, &oldval,
4090 oldval | SIGFASTBLOCK_PEND);
4091 if (res == -1) {
4092 sigfastblock_failed(td, true, true);
4093 return;
4094 }
4095 if (res == 0) {
4096 td->td_sigblock_val = oldval & ~SIGFASTBLOCK_FLAGS;
4097 td->td_pflags &= ~TDP_SIGFASTPENDING;
4098 break;
4099 }
4100 MPASS(res == 1);
4101 if (thread_check_susp(td, false) != 0)
4102 break;
4103 }
4104 }
4105
4106 static void
sigfastblock_setpend(struct thread * td,bool resched)4107 sigfastblock_setpend(struct thread *td, bool resched)
4108 {
4109 struct proc *p;
4110
4111 sigfastblock_setpend1(td);
4112 if (resched) {
4113 p = td->td_proc;
4114 PROC_LOCK(p);
4115 reschedule_signals(p, fastblock_mask, SIGPROCMASK_FASTBLK);
4116 PROC_UNLOCK(p);
4117 }
4118 }
4119