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