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