xref: /openbsd/sys/kern/kern_sig.c (revision a09e9584)
1 /*	$OpenBSD: kern_sig.c,v 1.330 2024/06/03 12:48:25 claudio Exp $	*/
2 /*	$NetBSD: kern_sig.c,v 1.54 1996/04/22 01:38:32 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Theo de Raadt. All rights reserved.
6  * Copyright (c) 1982, 1986, 1989, 1991, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  * (c) UNIX System Laboratories, Inc.
9  * All or some portions of this file are derived from material licensed
10  * to the University of California by American Telephone and Telegraph
11  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12  * the permission of UNIX System Laboratories, Inc.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)kern_sig.c	8.7 (Berkeley) 4/18/94
39  */
40 
41 #include <sys/param.h>
42 #include <sys/signalvar.h>
43 #include <sys/queue.h>
44 #include <sys/namei.h>
45 #include <sys/vnode.h>
46 #include <sys/event.h>
47 #include <sys/proc.h>
48 #include <sys/systm.h>
49 #include <sys/acct.h>
50 #include <sys/fcntl.h>
51 #include <sys/filedesc.h>
52 #include <sys/wait.h>
53 #include <sys/ktrace.h>
54 #include <sys/stat.h>
55 #include <sys/malloc.h>
56 #include <sys/pool.h>
57 #include <sys/sched.h>
58 #include <sys/user.h>
59 #include <sys/syslog.h>
60 #include <sys/ttycom.h>
61 #include <sys/pledge.h>
62 #include <sys/witness.h>
63 #include <sys/exec_elf.h>
64 
65 #include <sys/mount.h>
66 #include <sys/syscallargs.h>
67 
68 #include <uvm/uvm_extern.h>
69 #include <machine/tcb.h>
70 
71 int nosuidcoredump = 1;
72 
73 int	filt_sigattach(struct knote *kn);
74 void	filt_sigdetach(struct knote *kn);
75 int	filt_signal(struct knote *kn, long hint);
76 
77 const struct filterops sig_filtops = {
78 	.f_flags	= 0,
79 	.f_attach	= filt_sigattach,
80 	.f_detach	= filt_sigdetach,
81 	.f_event	= filt_signal,
82 };
83 
84 /*
85  * The array below categorizes the signals and their default actions.
86  */
87 const int sigprop[NSIG] = {
88 	0,			/* unused */
89 	SA_KILL,		/* SIGHUP */
90 	SA_KILL,		/* SIGINT */
91 	SA_KILL|SA_CORE,	/* SIGQUIT */
92 	SA_KILL|SA_CORE,	/* SIGILL */
93 	SA_KILL|SA_CORE,	/* SIGTRAP */
94 	SA_KILL|SA_CORE,	/* SIGABRT */
95 	SA_KILL|SA_CORE,	/* SIGEMT */
96 	SA_KILL|SA_CORE,	/* SIGFPE */
97 	SA_KILL,		/* SIGKILL */
98 	SA_KILL|SA_CORE,	/* SIGBUS */
99 	SA_KILL|SA_CORE,	/* SIGSEGV */
100 	SA_KILL|SA_CORE,	/* SIGSYS */
101 	SA_KILL,		/* SIGPIPE */
102 	SA_KILL,		/* SIGALRM */
103 	SA_KILL,		/* SIGTERM */
104 	SA_IGNORE,		/* SIGURG */
105 	SA_STOP,		/* SIGSTOP */
106 	SA_STOP|SA_TTYSTOP,	/* SIGTSTP */
107 	SA_IGNORE|SA_CONT,	/* SIGCONT */
108 	SA_IGNORE,		/* SIGCHLD */
109 	SA_STOP|SA_TTYSTOP,	/* SIGTTIN */
110 	SA_STOP|SA_TTYSTOP,	/* SIGTTOU */
111 	SA_IGNORE,		/* SIGIO */
112 	SA_KILL,		/* SIGXCPU */
113 	SA_KILL,		/* SIGXFSZ */
114 	SA_KILL,		/* SIGVTALRM */
115 	SA_KILL,		/* SIGPROF */
116 	SA_IGNORE,		/* SIGWINCH  */
117 	SA_IGNORE,		/* SIGINFO */
118 	SA_KILL,		/* SIGUSR1 */
119 	SA_KILL,		/* SIGUSR2 */
120 	SA_IGNORE,		/* SIGTHR */
121 };
122 
123 #define	CONTSIGMASK	(sigmask(SIGCONT))
124 #define	STOPSIGMASK	(sigmask(SIGSTOP) | sigmask(SIGTSTP) | \
125 			    sigmask(SIGTTIN) | sigmask(SIGTTOU))
126 
127 void setsigvec(struct proc *, int, struct sigaction *);
128 
129 void proc_stop(struct proc *p, int);
130 void proc_stop_sweep(void *);
131 void *proc_stop_si;
132 
133 void setsigctx(struct proc *, int, struct sigctx *);
134 void postsig_done(struct proc *, int, sigset_t, int);
135 void postsig(struct proc *, int, struct sigctx *);
136 int cansignal(struct proc *, struct process *, int);
137 
138 struct pool sigacts_pool;	/* memory pool for sigacts structures */
139 
140 void sigio_del(struct sigiolst *);
141 void sigio_unlink(struct sigio_ref *, struct sigiolst *);
142 struct mutex sigio_lock = MUTEX_INITIALIZER(IPL_HIGH);
143 
144 /*
145  * Can thread p, send the signal signum to process qr?
146  */
147 int
cansignal(struct proc * p,struct process * qr,int signum)148 cansignal(struct proc *p, struct process *qr, int signum)
149 {
150 	struct process *pr = p->p_p;
151 	struct ucred *uc = p->p_ucred;
152 	struct ucred *quc = qr->ps_ucred;
153 
154 	if (uc->cr_uid == 0)
155 		return (1);		/* root can always signal */
156 
157 	if (pr == qr)
158 		return (1);		/* process can always signal itself */
159 
160 	/* optimization: if the same creds then the tests below will pass */
161 	if (uc == quc)
162 		return (1);
163 
164 	if (signum == SIGCONT && qr->ps_session == pr->ps_session)
165 		return (1);		/* SIGCONT in session */
166 
167 	/*
168 	 * Using kill(), only certain signals can be sent to setugid
169 	 * child processes
170 	 */
171 	if (qr->ps_flags & PS_SUGID) {
172 		switch (signum) {
173 		case 0:
174 		case SIGKILL:
175 		case SIGINT:
176 		case SIGTERM:
177 		case SIGALRM:
178 		case SIGSTOP:
179 		case SIGTTIN:
180 		case SIGTTOU:
181 		case SIGTSTP:
182 		case SIGHUP:
183 		case SIGUSR1:
184 		case SIGUSR2:
185 			if (uc->cr_ruid == quc->cr_ruid ||
186 			    uc->cr_uid == quc->cr_ruid)
187 				return (1);
188 		}
189 		return (0);
190 	}
191 
192 	if (uc->cr_ruid == quc->cr_ruid ||
193 	    uc->cr_ruid == quc->cr_svuid ||
194 	    uc->cr_uid == quc->cr_ruid ||
195 	    uc->cr_uid == quc->cr_svuid)
196 		return (1);
197 	return (0);
198 }
199 
200 /*
201  * Initialize signal-related data structures.
202  */
203 void
signal_init(void)204 signal_init(void)
205 {
206 	proc_stop_si = softintr_establish(IPL_SOFTCLOCK, proc_stop_sweep,
207 	    NULL);
208 	if (proc_stop_si == NULL)
209 		panic("signal_init failed to register softintr");
210 
211 	pool_init(&sigacts_pool, sizeof(struct sigacts), 0, IPL_NONE,
212 	    PR_WAITOK, "sigapl", NULL);
213 }
214 
215 /*
216  * Initialize a new sigaltstack structure.
217  */
218 void
sigstkinit(struct sigaltstack * ss)219 sigstkinit(struct sigaltstack *ss)
220 {
221 	ss->ss_flags = SS_DISABLE;
222 	ss->ss_size = 0;
223 	ss->ss_sp = NULL;
224 }
225 
226 /*
227  * Create an initial sigacts structure, using the same signal state
228  * as pr.
229  */
230 struct sigacts *
sigactsinit(struct process * pr)231 sigactsinit(struct process *pr)
232 {
233 	struct sigacts *ps;
234 
235 	ps = pool_get(&sigacts_pool, PR_WAITOK);
236 	memcpy(ps, pr->ps_sigacts, sizeof(struct sigacts));
237 	return (ps);
238 }
239 
240 /*
241  * Release a sigacts structure.
242  */
243 void
sigactsfree(struct sigacts * ps)244 sigactsfree(struct sigacts *ps)
245 {
246 	pool_put(&sigacts_pool, ps);
247 }
248 
249 int
sys_sigaction(struct proc * p,void * v,register_t * retval)250 sys_sigaction(struct proc *p, void *v, register_t *retval)
251 {
252 	struct sys_sigaction_args /* {
253 		syscallarg(int) signum;
254 		syscallarg(const struct sigaction *) nsa;
255 		syscallarg(struct sigaction *) osa;
256 	} */ *uap = v;
257 	struct sigaction vec;
258 #ifdef KTRACE
259 	struct sigaction ovec;
260 #endif
261 	struct sigaction *sa;
262 	const struct sigaction *nsa;
263 	struct sigaction *osa;
264 	struct sigacts *ps = p->p_p->ps_sigacts;
265 	int signum;
266 	int bit, error;
267 
268 	signum = SCARG(uap, signum);
269 	nsa = SCARG(uap, nsa);
270 	osa = SCARG(uap, osa);
271 
272 	if (signum <= 0 || signum >= NSIG ||
273 	    (nsa && (signum == SIGKILL || signum == SIGSTOP)))
274 		return (EINVAL);
275 	sa = &vec;
276 	if (osa) {
277 		mtx_enter(&p->p_p->ps_mtx);
278 		sa->sa_handler = ps->ps_sigact[signum];
279 		sa->sa_mask = ps->ps_catchmask[signum];
280 		bit = sigmask(signum);
281 		sa->sa_flags = 0;
282 		if ((ps->ps_sigonstack & bit) != 0)
283 			sa->sa_flags |= SA_ONSTACK;
284 		if ((ps->ps_sigintr & bit) == 0)
285 			sa->sa_flags |= SA_RESTART;
286 		if ((ps->ps_sigreset & bit) != 0)
287 			sa->sa_flags |= SA_RESETHAND;
288 		if ((ps->ps_siginfo & bit) != 0)
289 			sa->sa_flags |= SA_SIGINFO;
290 		if (signum == SIGCHLD) {
291 			if ((ps->ps_sigflags & SAS_NOCLDSTOP) != 0)
292 				sa->sa_flags |= SA_NOCLDSTOP;
293 			if ((ps->ps_sigflags & SAS_NOCLDWAIT) != 0)
294 				sa->sa_flags |= SA_NOCLDWAIT;
295 		}
296 		mtx_leave(&p->p_p->ps_mtx);
297 		if ((sa->sa_mask & bit) == 0)
298 			sa->sa_flags |= SA_NODEFER;
299 		sa->sa_mask &= ~bit;
300 		error = copyout(sa, osa, sizeof (vec));
301 		if (error)
302 			return (error);
303 #ifdef KTRACE
304 		if (KTRPOINT(p, KTR_STRUCT))
305 			ovec = vec;
306 #endif
307 	}
308 	if (nsa) {
309 		error = copyin(nsa, sa, sizeof (vec));
310 		if (error)
311 			return (error);
312 #ifdef KTRACE
313 		if (KTRPOINT(p, KTR_STRUCT))
314 			ktrsigaction(p, sa);
315 #endif
316 		setsigvec(p, signum, sa);
317 	}
318 #ifdef KTRACE
319 	if (osa && KTRPOINT(p, KTR_STRUCT))
320 		ktrsigaction(p, &ovec);
321 #endif
322 	return (0);
323 }
324 
325 void
setsigvec(struct proc * p,int signum,struct sigaction * sa)326 setsigvec(struct proc *p, int signum, struct sigaction *sa)
327 {
328 	struct sigacts *ps = p->p_p->ps_sigacts;
329 	int bit;
330 
331 	bit = sigmask(signum);
332 
333 	mtx_enter(&p->p_p->ps_mtx);
334 	ps->ps_sigact[signum] = sa->sa_handler;
335 	if ((sa->sa_flags & SA_NODEFER) == 0)
336 		sa->sa_mask |= sigmask(signum);
337 	ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask;
338 	if (signum == SIGCHLD) {
339 		if (sa->sa_flags & SA_NOCLDSTOP)
340 			atomic_setbits_int(&ps->ps_sigflags, SAS_NOCLDSTOP);
341 		else
342 			atomic_clearbits_int(&ps->ps_sigflags, SAS_NOCLDSTOP);
343 		/*
344 		 * If the SA_NOCLDWAIT flag is set or the handler
345 		 * is SIG_IGN we reparent the dying child to PID 1
346 		 * (init) which will reap the zombie.  Because we use
347 		 * init to do our dirty work we never set SAS_NOCLDWAIT
348 		 * for PID 1.
349 		 * XXX exit1 rework means this is unnecessary?
350 		 */
351 		if (initprocess->ps_sigacts != ps &&
352 		    ((sa->sa_flags & SA_NOCLDWAIT) ||
353 		    sa->sa_handler == SIG_IGN))
354 			atomic_setbits_int(&ps->ps_sigflags, SAS_NOCLDWAIT);
355 		else
356 			atomic_clearbits_int(&ps->ps_sigflags, SAS_NOCLDWAIT);
357 	}
358 	if ((sa->sa_flags & SA_RESETHAND) != 0)
359 		ps->ps_sigreset |= bit;
360 	else
361 		ps->ps_sigreset &= ~bit;
362 	if ((sa->sa_flags & SA_SIGINFO) != 0)
363 		ps->ps_siginfo |= bit;
364 	else
365 		ps->ps_siginfo &= ~bit;
366 	if ((sa->sa_flags & SA_RESTART) == 0)
367 		ps->ps_sigintr |= bit;
368 	else
369 		ps->ps_sigintr &= ~bit;
370 	if ((sa->sa_flags & SA_ONSTACK) != 0)
371 		ps->ps_sigonstack |= bit;
372 	else
373 		ps->ps_sigonstack &= ~bit;
374 	/*
375 	 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
376 	 * and for signals set to SIG_DFL where the default is to ignore.
377 	 * However, don't put SIGCONT in ps_sigignore,
378 	 * as we have to restart the process.
379 	 */
380 	if (sa->sa_handler == SIG_IGN ||
381 	    (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
382 		atomic_clearbits_int(&p->p_siglist, bit);
383 		atomic_clearbits_int(&p->p_p->ps_siglist, bit);
384 		if (signum != SIGCONT)
385 			ps->ps_sigignore |= bit;	/* easier in psignal */
386 		ps->ps_sigcatch &= ~bit;
387 	} else {
388 		ps->ps_sigignore &= ~bit;
389 		if (sa->sa_handler == SIG_DFL)
390 			ps->ps_sigcatch &= ~bit;
391 		else
392 			ps->ps_sigcatch |= bit;
393 	}
394 	mtx_leave(&p->p_p->ps_mtx);
395 }
396 
397 /*
398  * Initialize signal state for process 0;
399  * set to ignore signals that are ignored by default.
400  */
401 void
siginit(struct sigacts * ps)402 siginit(struct sigacts *ps)
403 {
404 	int i;
405 
406 	for (i = 0; i < NSIG; i++)
407 		if (sigprop[i] & SA_IGNORE && i != SIGCONT)
408 			ps->ps_sigignore |= sigmask(i);
409 	ps->ps_sigflags = SAS_NOCLDWAIT | SAS_NOCLDSTOP;
410 }
411 
412 /*
413  * Reset signals for an exec by the specified thread.
414  */
415 void
execsigs(struct proc * p)416 execsigs(struct proc *p)
417 {
418 	struct sigacts *ps;
419 	int nc, mask;
420 
421 	ps = p->p_p->ps_sigacts;
422 	mtx_enter(&p->p_p->ps_mtx);
423 
424 	/*
425 	 * Reset caught signals.  Held signals remain held
426 	 * through p_sigmask (unless they were caught,
427 	 * and are now ignored by default).
428 	 */
429 	while (ps->ps_sigcatch) {
430 		nc = ffs((long)ps->ps_sigcatch);
431 		mask = sigmask(nc);
432 		ps->ps_sigcatch &= ~mask;
433 		if (sigprop[nc] & SA_IGNORE) {
434 			if (nc != SIGCONT)
435 				ps->ps_sigignore |= mask;
436 			atomic_clearbits_int(&p->p_siglist, mask);
437 			atomic_clearbits_int(&p->p_p->ps_siglist, mask);
438 		}
439 		ps->ps_sigact[nc] = SIG_DFL;
440 	}
441 	/*
442 	 * Reset stack state to the user stack.
443 	 * Clear set of signals caught on the signal stack.
444 	 */
445 	sigstkinit(&p->p_sigstk);
446 	atomic_clearbits_int(&ps->ps_sigflags, SAS_NOCLDWAIT);
447 	if (ps->ps_sigact[SIGCHLD] == SIG_IGN)
448 		ps->ps_sigact[SIGCHLD] = SIG_DFL;
449 	mtx_leave(&p->p_p->ps_mtx);
450 }
451 
452 /*
453  * Manipulate signal mask.
454  * Note that we receive new mask, not pointer,
455  * and return old mask as return value;
456  * the library stub does the rest.
457  */
458 int
sys_sigprocmask(struct proc * p,void * v,register_t * retval)459 sys_sigprocmask(struct proc *p, void *v, register_t *retval)
460 {
461 	struct sys_sigprocmask_args /* {
462 		syscallarg(int) how;
463 		syscallarg(sigset_t) mask;
464 	} */ *uap = v;
465 	int error = 0;
466 	sigset_t mask;
467 
468 	KASSERT(p == curproc);
469 
470 	*retval = p->p_sigmask;
471 	mask = SCARG(uap, mask) &~ sigcantmask;
472 
473 	switch (SCARG(uap, how)) {
474 	case SIG_BLOCK:
475 		SET(p->p_sigmask, mask);
476 		break;
477 	case SIG_UNBLOCK:
478 		CLR(p->p_sigmask, mask);
479 		break;
480 	case SIG_SETMASK:
481 		p->p_sigmask = mask;
482 		break;
483 	default:
484 		error = EINVAL;
485 		break;
486 	}
487 	return (error);
488 }
489 
490 int
sys_sigpending(struct proc * p,void * v,register_t * retval)491 sys_sigpending(struct proc *p, void *v, register_t *retval)
492 {
493 	*retval = p->p_siglist | p->p_p->ps_siglist;
494 	return (0);
495 }
496 
497 /*
498  * Temporarily replace calling proc's signal mask for the duration of a
499  * system call.  Original signal mask will be restored by userret().
500  */
501 void
dosigsuspend(struct proc * p,sigset_t newmask)502 dosigsuspend(struct proc *p, sigset_t newmask)
503 {
504 	KASSERT(p == curproc);
505 
506 	p->p_oldmask = p->p_sigmask;
507 	p->p_sigmask = newmask;
508 	atomic_setbits_int(&p->p_flag, P_SIGSUSPEND);
509 }
510 
511 /*
512  * Suspend thread until signal, providing mask to be set
513  * in the meantime.  Note nonstandard calling convention:
514  * libc stub passes mask, not pointer, to save a copyin.
515  */
516 int
sys_sigsuspend(struct proc * p,void * v,register_t * retval)517 sys_sigsuspend(struct proc *p, void *v, register_t *retval)
518 {
519 	struct sys_sigsuspend_args /* {
520 		syscallarg(int) mask;
521 	} */ *uap = v;
522 
523 	dosigsuspend(p, SCARG(uap, mask) &~ sigcantmask);
524 	while (tsleep_nsec(&nowake, PPAUSE|PCATCH, "sigsusp", INFSLP) == 0)
525 		continue;
526 	/* always return EINTR rather than ERESTART... */
527 	return (EINTR);
528 }
529 
530 int
sigonstack(size_t stack)531 sigonstack(size_t stack)
532 {
533 	const struct sigaltstack *ss = &curproc->p_sigstk;
534 
535 	return (ss->ss_flags & SS_DISABLE ? 0 :
536 	    (stack - (size_t)ss->ss_sp < ss->ss_size));
537 }
538 
539 int
sys_sigaltstack(struct proc * p,void * v,register_t * retval)540 sys_sigaltstack(struct proc *p, void *v, register_t *retval)
541 {
542 	struct sys_sigaltstack_args /* {
543 		syscallarg(const struct sigaltstack *) nss;
544 		syscallarg(struct sigaltstack *) oss;
545 	} */ *uap = v;
546 	struct sigaltstack ss;
547 	const struct sigaltstack *nss;
548 	struct sigaltstack *oss;
549 	int onstack = sigonstack(PROC_STACK(p));
550 	int error;
551 
552 	nss = SCARG(uap, nss);
553 	oss = SCARG(uap, oss);
554 
555 	if (oss != NULL) {
556 		ss = p->p_sigstk;
557 		if (onstack)
558 			ss.ss_flags |= SS_ONSTACK;
559 		if ((error = copyout(&ss, oss, sizeof(ss))))
560 			return (error);
561 	}
562 	if (nss == NULL)
563 		return (0);
564 	error = copyin(nss, &ss, sizeof(ss));
565 	if (error)
566 		return (error);
567 	if (onstack)
568 		return (EPERM);
569 	if (ss.ss_flags & ~SS_DISABLE)
570 		return (EINVAL);
571 	if (ss.ss_flags & SS_DISABLE) {
572 		p->p_sigstk.ss_flags = ss.ss_flags;
573 		return (0);
574 	}
575 	if (ss.ss_size < MINSIGSTKSZ)
576 		return (ENOMEM);
577 
578 	error = uvm_map_remap_as_stack(p, (vaddr_t)ss.ss_sp, ss.ss_size);
579 	if (error)
580 		return (error);
581 
582 	p->p_sigstk = ss;
583 	return (0);
584 }
585 
586 int
sys_kill(struct proc * cp,void * v,register_t * retval)587 sys_kill(struct proc *cp, void *v, register_t *retval)
588 {
589 	struct sys_kill_args /* {
590 		syscallarg(int) pid;
591 		syscallarg(int) signum;
592 	} */ *uap = v;
593 	struct process *pr;
594 	int pid = SCARG(uap, pid);
595 	int signum = SCARG(uap, signum);
596 	int error;
597 	int zombie = 0;
598 
599 	if ((error = pledge_kill(cp, pid)) != 0)
600 		return (error);
601 	if (((u_int)signum) >= NSIG)
602 		return (EINVAL);
603 	if (pid > 0) {
604 		if ((pr = prfind(pid)) == NULL) {
605 			if ((pr = zombiefind(pid)) == NULL)
606 				return (ESRCH);
607 			else
608 				zombie = 1;
609 		}
610 		if (!cansignal(cp, pr, signum))
611 			return (EPERM);
612 
613 		/* kill single process */
614 		if (signum && !zombie)
615 			prsignal(pr, signum);
616 		return (0);
617 	}
618 	switch (pid) {
619 	case -1:		/* broadcast signal */
620 		return (killpg1(cp, signum, 0, 1));
621 	case 0:			/* signal own process group */
622 		return (killpg1(cp, signum, 0, 0));
623 	default:		/* negative explicit process group */
624 		return (killpg1(cp, signum, -pid, 0));
625 	}
626 }
627 
628 int
sys_thrkill(struct proc * cp,void * v,register_t * retval)629 sys_thrkill(struct proc *cp, void *v, register_t *retval)
630 {
631 	struct sys_thrkill_args /* {
632 		syscallarg(pid_t) tid;
633 		syscallarg(int) signum;
634 		syscallarg(void *) tcb;
635 	} */ *uap = v;
636 	struct proc *p;
637 	int tid = SCARG(uap, tid);
638 	int signum = SCARG(uap, signum);
639 	void *tcb;
640 
641 	if (((u_int)signum) >= NSIG)
642 		return (EINVAL);
643 
644 	p = tid ? tfind_user(tid, cp->p_p) : cp;
645 	if (p == NULL)
646 		return (ESRCH);
647 
648 	/* optionally require the target thread to have the given tcb addr */
649 	tcb = SCARG(uap, tcb);
650 	if (tcb != NULL && tcb != TCB_GET(p))
651 		return (ESRCH);
652 
653 	if (signum)
654 		ptsignal(p, signum, STHREAD);
655 	return (0);
656 }
657 
658 /*
659  * Common code for kill process group/broadcast kill.
660  * cp is calling process.
661  */
662 int
killpg1(struct proc * cp,int signum,int pgid,int all)663 killpg1(struct proc *cp, int signum, int pgid, int all)
664 {
665 	struct process *pr;
666 	struct pgrp *pgrp;
667 	int nfound = 0;
668 
669 	if (all) {
670 		/*
671 		 * broadcast
672 		 */
673 		LIST_FOREACH(pr, &allprocess, ps_list) {
674 			if (pr->ps_pid <= 1 ||
675 			    pr->ps_flags & (PS_SYSTEM | PS_NOBROADCASTKILL) ||
676 			    pr == cp->p_p || !cansignal(cp, pr, signum))
677 				continue;
678 			nfound++;
679 			if (signum)
680 				prsignal(pr, signum);
681 		}
682 	} else {
683 		if (pgid == 0)
684 			/*
685 			 * zero pgid means send to my process group.
686 			 */
687 			pgrp = cp->p_p->ps_pgrp;
688 		else {
689 			pgrp = pgfind(pgid);
690 			if (pgrp == NULL)
691 				return (ESRCH);
692 		}
693 		LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) {
694 			if (pr->ps_pid <= 1 || pr->ps_flags & PS_SYSTEM ||
695 			    !cansignal(cp, pr, signum))
696 				continue;
697 			nfound++;
698 			if (signum)
699 				prsignal(pr, signum);
700 		}
701 	}
702 	return (nfound ? 0 : ESRCH);
703 }
704 
705 #define CANDELIVER(uid, euid, pr) \
706 	(euid == 0 || \
707 	(uid) == (pr)->ps_ucred->cr_ruid || \
708 	(uid) == (pr)->ps_ucred->cr_svuid || \
709 	(uid) == (pr)->ps_ucred->cr_uid || \
710 	(euid) == (pr)->ps_ucred->cr_ruid || \
711 	(euid) == (pr)->ps_ucred->cr_svuid || \
712 	(euid) == (pr)->ps_ucred->cr_uid)
713 
714 #define CANSIGIO(cr, pr) \
715 	CANDELIVER((cr)->cr_ruid, (cr)->cr_uid, (pr))
716 
717 /*
718  * Send a signal to a process group.  If checktty is 1,
719  * limit to members which have a controlling terminal.
720  */
721 void
pgsignal(struct pgrp * pgrp,int signum,int checkctty)722 pgsignal(struct pgrp *pgrp, int signum, int checkctty)
723 {
724 	struct process *pr;
725 
726 	if (pgrp)
727 		LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist)
728 			if (checkctty == 0 || pr->ps_flags & PS_CONTROLT)
729 				prsignal(pr, signum);
730 }
731 
732 /*
733  * Send a SIGIO or SIGURG signal to a process or process group using stored
734  * credentials rather than those of the current process.
735  */
736 void
pgsigio(struct sigio_ref * sir,int sig,int checkctty)737 pgsigio(struct sigio_ref *sir, int sig, int checkctty)
738 {
739 	struct process *pr;
740 	struct sigio *sigio;
741 
742 	if (sir->sir_sigio == NULL)
743 		return;
744 
745 	KERNEL_LOCK();
746 	mtx_enter(&sigio_lock);
747 	sigio = sir->sir_sigio;
748 	if (sigio == NULL)
749 		goto out;
750 	if (sigio->sio_pgid > 0) {
751 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc))
752 			prsignal(sigio->sio_proc, sig);
753 	} else if (sigio->sio_pgid < 0) {
754 		LIST_FOREACH(pr, &sigio->sio_pgrp->pg_members, ps_pglist) {
755 			if (CANSIGIO(sigio->sio_ucred, pr) &&
756 			    (checkctty == 0 || (pr->ps_flags & PS_CONTROLT)))
757 				prsignal(pr, sig);
758 		}
759 	}
760 out:
761 	mtx_leave(&sigio_lock);
762 	KERNEL_UNLOCK();
763 }
764 
765 /*
766  * Recalculate the signal mask and reset the signal disposition after
767  * usermode frame for delivery is formed.
768  */
769 void
postsig_done(struct proc * p,int signum,sigset_t catchmask,int reset)770 postsig_done(struct proc *p, int signum, sigset_t catchmask, int reset)
771 {
772 	p->p_ru.ru_nsignals++;
773 	SET(p->p_sigmask, catchmask);
774 	if (reset != 0) {
775 		sigset_t mask = sigmask(signum);
776 		struct sigacts *ps = p->p_p->ps_sigacts;
777 
778 		mtx_enter(&p->p_p->ps_mtx);
779 		ps->ps_sigcatch &= ~mask;
780 		if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
781 			ps->ps_sigignore |= mask;
782 		ps->ps_sigact[signum] = SIG_DFL;
783 		mtx_leave(&p->p_p->ps_mtx);
784 	}
785 }
786 
787 /*
788  * Send a signal caused by a trap to the current thread
789  * If it will be caught immediately, deliver it with correct code.
790  * Otherwise, post it normally.
791  */
792 void
trapsignal(struct proc * p,int signum,u_long trapno,int code,union sigval sigval)793 trapsignal(struct proc *p, int signum, u_long trapno, int code,
794     union sigval sigval)
795 {
796 	struct process *pr = p->p_p;
797 	struct sigctx ctx;
798 	int mask;
799 
800 	switch (signum) {
801 	case SIGILL:
802 		if (code == ILL_BTCFI) {
803 			pr->ps_acflag |= ABTCFI;
804 			break;
805 		}
806 		/* FALLTHROUGH */
807 	case SIGBUS:
808 	case SIGSEGV:
809 		pr->ps_acflag |= ATRAP;
810 		break;
811 	}
812 
813 	mask = sigmask(signum);
814 	setsigctx(p, signum, &ctx);
815 	if ((pr->ps_flags & PS_TRACED) == 0 && ctx.sig_catch != 0 &&
816 	    (p->p_sigmask & mask) == 0) {
817 		siginfo_t si;
818 
819 		initsiginfo(&si, signum, trapno, code, sigval);
820 #ifdef KTRACE
821 		if (KTRPOINT(p, KTR_PSIG)) {
822 			ktrpsig(p, signum, ctx.sig_action,
823 			    p->p_sigmask, code, &si);
824 		}
825 #endif
826 		if (sendsig(ctx.sig_action, signum, p->p_sigmask, &si,
827 		    ctx.sig_info, ctx.sig_onstack)) {
828 			KERNEL_LOCK();
829 			sigexit(p, SIGILL);
830 			/* NOTREACHED */
831 		}
832 		postsig_done(p, signum, ctx.sig_catchmask, ctx.sig_reset);
833 	} else {
834 		p->p_sisig = signum;
835 		p->p_sitrapno = trapno;	/* XXX for core dump/debugger */
836 		p->p_sicode = code;
837 		p->p_sigval = sigval;
838 
839 		/*
840 		 * If traced, stop if signal is masked, and stay stopped
841 		 * until released by the debugger.  If our parent process
842 		 * is waiting for us, don't hang as we could deadlock.
843 		 */
844 		if (((pr->ps_flags & (PS_TRACED | PS_PPWAIT)) == PS_TRACED) &&
845 		    signum != SIGKILL && (p->p_sigmask & mask) != 0) {
846 			single_thread_set(p, SINGLE_SUSPEND | SINGLE_NOWAIT);
847 			pr->ps_xsig = signum;
848 
849 			SCHED_LOCK();
850 			proc_stop(p, 1);
851 			SCHED_UNLOCK();
852 
853 			signum = pr->ps_xsig;
854 			single_thread_clear(p, 0);
855 
856 			/*
857 			 * If we are no longer being traced, or the parent
858 			 * didn't give us a signal, skip sending the signal.
859 			 */
860 			if ((pr->ps_flags & PS_TRACED) == 0 ||
861 			    signum == 0)
862 				return;
863 
864 			/* update signal info */
865 			p->p_sisig = signum;
866 			mask = sigmask(signum);
867 		}
868 
869 		/*
870 		 * Signals like SIGBUS and SIGSEGV should not, when
871 		 * generated by the kernel, be ignorable or blockable.
872 		 * If it is and we're not being traced, then just kill
873 		 * the process.
874 		 * After vfs_shutdown(9), init(8) cannot receive signals
875 		 * because new code pages of the signal handler cannot be
876 		 * mapped from halted storage.  init(8) may not die or the
877 		 * kernel panics.  Better loop between signal handler and
878 		 * page fault trap until the machine is halted.
879 		 */
880 		if ((pr->ps_flags & PS_TRACED) == 0 &&
881 		    (sigprop[signum] & SA_KILL) &&
882 		    ((p->p_sigmask & mask) || ctx.sig_ignore) &&
883 		    pr->ps_pid != 1) {
884 			KERNEL_LOCK();
885 			sigexit(p, signum);
886 			/* NOTREACHED */
887 		}
888 		KERNEL_LOCK();
889 		ptsignal(p, signum, STHREAD);
890 		KERNEL_UNLOCK();
891 	}
892 }
893 
894 /*
895  * Send the signal to the process.  If the signal has an action, the action
896  * is usually performed by the target process rather than the caller; we add
897  * the signal to the set of pending signals for the process.
898  *
899  * Exceptions:
900  *   o When a stop signal is sent to a sleeping process that takes the
901  *     default action, the process is stopped without awakening it.
902  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
903  *     regardless of the signal action (eg, blocked or ignored).
904  *
905  * Other ignored signals are discarded immediately.
906  */
907 void
psignal(struct proc * p,int signum)908 psignal(struct proc *p, int signum)
909 {
910 	ptsignal(p, signum, SPROCESS);
911 }
912 
913 /*
914  * type = SPROCESS	process signal, can be diverted (sigwait())
915  * type = STHREAD	thread signal, but should be propagated if unhandled
916  * type = SPROPAGATED	propagated to this thread, so don't propagate again
917  */
918 void
ptsignal(struct proc * p,int signum,enum signal_type type)919 ptsignal(struct proc *p, int signum, enum signal_type type)
920 {
921 	int prop;
922 	sig_t action, altaction = SIG_DFL;
923 	sigset_t mask, sigmask;
924 	int *siglist;
925 	struct process *pr = p->p_p;
926 	struct proc *q;
927 	int wakeparent = 0;
928 
929 	KERNEL_ASSERT_LOCKED();
930 
931 #ifdef DIAGNOSTIC
932 	if ((u_int)signum >= NSIG || signum == 0)
933 		panic("psignal signal number");
934 #endif
935 
936 	/* Ignore signal if the target process is exiting */
937 	if (pr->ps_flags & PS_EXITING)
938 		return;
939 
940 	mask = sigmask(signum);
941 	sigmask = READ_ONCE(p->p_sigmask);
942 
943 	if (type == SPROCESS) {
944 		sigset_t tmpmask;
945 
946 		/* Accept SIGKILL to coredumping processes */
947 		if (pr->ps_flags & PS_COREDUMP && signum == SIGKILL) {
948 			atomic_setbits_int(&pr->ps_siglist, mask);
949 			return;
950 		}
951 
952 		/*
953 		 * If the current thread can process the signal
954 		 * immediately (it's unblocked) then have it take it.
955 		 */
956 		q = curproc;
957 		tmpmask = READ_ONCE(q->p_sigmask);
958 		if (q->p_p == pr && (q->p_flag & P_WEXIT) == 0 &&
959 		    (tmpmask & mask) == 0) {
960 			p = q;
961 			sigmask = tmpmask;
962 		} else {
963 			/*
964 			 * A process-wide signal can be diverted to a
965 			 * different thread that's in sigwait() for this
966 			 * signal.  If there isn't such a thread, then
967 			 * pick a thread that doesn't have it blocked so
968 			 * that the stop/kill consideration isn't
969 			 * delayed.  Otherwise, mark it pending on the
970 			 * main thread.
971 			 */
972 			TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) {
973 
974 				/* ignore exiting threads */
975 				if (q->p_flag & P_WEXIT)
976 					continue;
977 
978 				/* skip threads that have the signal blocked */
979 				tmpmask = READ_ONCE(q->p_sigmask);
980 				if ((tmpmask & mask) != 0)
981 					continue;
982 
983 				/* okay, could send to this thread */
984 				p = q;
985 				sigmask = tmpmask;
986 
987 				/*
988 				 * sigsuspend, sigwait, ppoll/pselect, etc?
989 				 * Definitely go to this thread, as it's
990 				 * already blocked in the kernel.
991 				 */
992 				if (q->p_flag & P_SIGSUSPEND)
993 					break;
994 			}
995 		}
996 	}
997 
998 	if (type != SPROPAGATED)
999 		knote_locked(&pr->ps_klist, NOTE_SIGNAL | signum);
1000 
1001 	prop = sigprop[signum];
1002 
1003 	/*
1004 	 * If proc is traced, always give parent a chance.
1005 	 */
1006 	if (pr->ps_flags & PS_TRACED) {
1007 		action = SIG_DFL;
1008 	} else {
1009 		sigset_t sigcatch, sigignore;
1010 
1011 		/*
1012 		 * If the signal is being ignored,
1013 		 * then we forget about it immediately.
1014 		 * (Note: we don't set SIGCONT in ps_sigignore,
1015 		 * and if it is set to SIG_IGN,
1016 		 * action will be SIG_DFL here.)
1017 		 */
1018 		mtx_enter(&pr->ps_mtx);
1019 		sigignore = pr->ps_sigacts->ps_sigignore;
1020 		sigcatch = pr->ps_sigacts->ps_sigcatch;
1021 		mtx_leave(&pr->ps_mtx);
1022 
1023 		if (sigignore & mask)
1024 			return;
1025 		if (sigmask & mask) {
1026 			action = SIG_HOLD;
1027 			if (sigcatch & mask)
1028 				altaction = SIG_CATCH;
1029 		} else if (sigcatch & mask) {
1030 			action = SIG_CATCH;
1031 		} else {
1032 			action = SIG_DFL;
1033 
1034 			if (prop & SA_KILL && pr->ps_nice > NZERO)
1035 				 pr->ps_nice = NZERO;
1036 
1037 			/*
1038 			 * If sending a tty stop signal to a member of an
1039 			 * orphaned process group, discard the signal here if
1040 			 * the action is default; don't stop the process below
1041 			 * if sleeping, and don't clear any pending SIGCONT.
1042 			 */
1043 			if (prop & SA_TTYSTOP && pr->ps_pgrp->pg_jobc == 0)
1044 				return;
1045 		}
1046 	}
1047 	/*
1048 	 * If delivered to process, mark as pending there.  Continue and stop
1049 	 * signals will be propagated to all threads.  So they are always
1050 	 * marked at thread level.
1051 	 */
1052 	siglist = (type == SPROCESS) ? &pr->ps_siglist : &p->p_siglist;
1053 	if (prop & (SA_CONT | SA_STOP))
1054 		siglist = &p->p_siglist;
1055 
1056 	/*
1057 	 * XXX delay processing of SA_STOP signals unless action == SIG_DFL?
1058 	 */
1059 	if (prop & (SA_CONT | SA_STOP) && type != SPROPAGATED)
1060 		TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link)
1061 			if (q != p)
1062 				ptsignal(q, signum, SPROPAGATED);
1063 
1064 	SCHED_LOCK();
1065 
1066 	switch (p->p_stat) {
1067 
1068 	case SSLEEP:
1069 		/*
1070 		 * If process is sleeping uninterruptibly
1071 		 * we can't interrupt the sleep... the signal will
1072 		 * be noticed when the process returns through
1073 		 * trap() or syscall().
1074 		 */
1075 		if ((p->p_flag & P_SINTR) == 0)
1076 			goto out;
1077 		/*
1078 		 * Process is sleeping and traced... make it runnable
1079 		 * so it can discover the signal in cursig() and stop
1080 		 * for the parent.
1081 		 */
1082 		if (pr->ps_flags & PS_TRACED)
1083 			goto run;
1084 
1085 		/*
1086 		 * Recheck sigmask before waking up the process,
1087 		 * there is a chance that while sending the signal
1088 		 * the process changed sigmask and went to sleep.
1089 		 */
1090 		sigmask = READ_ONCE(p->p_sigmask);
1091 		if (sigmask & mask)
1092 			goto out;
1093 		else if (action == SIG_HOLD) {
1094 			/* signal got unmasked, get proper action */
1095 			action = altaction;
1096 
1097 			if (action == SIG_DFL) {
1098 				if (prop & SA_KILL && pr->ps_nice > NZERO)
1099 					 pr->ps_nice = NZERO;
1100 
1101 				/*
1102 				 * Discard tty stop signals sent to an
1103 				 * orphaned process group, see above.
1104 				 */
1105 				if (prop & SA_TTYSTOP &&
1106 				    pr->ps_pgrp->pg_jobc == 0) {
1107 					mask = 0;
1108 					prop = 0;
1109 					goto out;
1110 				}
1111 			}
1112 		}
1113 
1114 		/*
1115 		 * If SIGCONT is default (or ignored) and process is
1116 		 * asleep, we are finished; the process should not
1117 		 * be awakened.
1118 		 */
1119 		if ((prop & SA_CONT) && action == SIG_DFL) {
1120 			mask = 0;
1121 			goto out;
1122 		}
1123 		/*
1124 		 * When a sleeping process receives a stop
1125 		 * signal, process immediately if possible.
1126 		 */
1127 		if ((prop & SA_STOP) && action == SIG_DFL) {
1128 			/*
1129 			 * If a child holding parent blocked,
1130 			 * stopping could cause deadlock.
1131 			 */
1132 			if (pr->ps_flags & PS_PPWAIT)
1133 				goto out;
1134 			mask = 0;
1135 			pr->ps_xsig = signum;
1136 			proc_stop(p, 0);
1137 			goto out;
1138 		}
1139 		/*
1140 		 * All other (caught or default) signals
1141 		 * cause the process to run.
1142 		 */
1143 		goto runfast;
1144 		/* NOTREACHED */
1145 
1146 	case SSTOP:
1147 		/*
1148 		 * If traced process is already stopped,
1149 		 * then no further action is necessary.
1150 		 */
1151 		if (pr->ps_flags & PS_TRACED)
1152 			goto out;
1153 
1154 		/*
1155 		 * Kill signal always sets processes running.
1156 		 */
1157 		if (signum == SIGKILL) {
1158 			atomic_clearbits_int(&p->p_flag, P_SUSPSIG);
1159 			goto runfast;
1160 		}
1161 
1162 		if (prop & SA_CONT) {
1163 			/*
1164 			 * If SIGCONT is default (or ignored), we continue the
1165 			 * process but don't leave the signal in p_siglist, as
1166 			 * it has no further action.  If SIGCONT is held, we
1167 			 * continue the process and leave the signal in
1168 			 * p_siglist.  If the process catches SIGCONT, let it
1169 			 * handle the signal itself.  If it isn't waiting on
1170 			 * an event, then it goes back to run state.
1171 			 * Otherwise, process goes back to sleep state.
1172 			 */
1173 			atomic_setbits_int(&p->p_flag, P_CONTINUED);
1174 			atomic_clearbits_int(&p->p_flag, P_SUSPSIG);
1175 			wakeparent = 1;
1176 			if (action == SIG_DFL)
1177 				mask = 0;
1178 			if (action == SIG_CATCH)
1179 				goto runfast;
1180 			if (p->p_wchan == NULL)
1181 				goto run;
1182 			atomic_clearbits_int(&p->p_flag, P_WSLEEP);
1183 			p->p_stat = SSLEEP;
1184 			goto out;
1185 		}
1186 
1187 		/*
1188 		 * Defer further processing for signals which are held,
1189 		 * except that stopped processes must be continued by SIGCONT.
1190 		 */
1191 		if (action == SIG_HOLD)
1192 			goto out;
1193 
1194 		if (prop & SA_STOP) {
1195 			/*
1196 			 * Already stopped, don't need to stop again.
1197 			 * (If we did the shell could get confused.)
1198 			 */
1199 			mask = 0;
1200 			goto out;
1201 		}
1202 
1203 		/*
1204 		 * If process is sleeping interruptibly, then simulate a
1205 		 * wakeup so that when it is continued, it will be made
1206 		 * runnable and can look at the signal.  But don't make
1207 		 * the process runnable, leave it stopped.
1208 		 */
1209 		if (p->p_flag & P_SINTR)
1210 			unsleep(p);
1211 		goto out;
1212 
1213 	case SONPROC:
1214 		if (action == SIG_HOLD)
1215 			goto out;
1216 
1217 		/* set siglist before issuing the ast */
1218 		atomic_setbits_int(siglist, mask);
1219 		mask = 0;
1220 		signotify(p);
1221 		/* FALLTHROUGH */
1222 	default:
1223 		/*
1224 		 * SRUN, SIDL, SDEAD do nothing with the signal,
1225 		 * other than kicking ourselves if we are running.
1226 		 * It will either never be noticed, or noticed very soon.
1227 		 */
1228 		goto out;
1229 	}
1230 	/* NOTREACHED */
1231 
1232 runfast:
1233 	/*
1234 	 * Raise priority to at least PUSER.
1235 	 */
1236 	if (p->p_usrpri > PUSER)
1237 		p->p_usrpri = PUSER;
1238 run:
1239 	unsleep(p);
1240 	setrunnable(p);
1241 out:
1242 	/* finally adjust siglist */
1243 	if (mask)
1244 		atomic_setbits_int(siglist, mask);
1245 	if (prop & SA_CONT) {
1246 		atomic_clearbits_int(siglist, STOPSIGMASK);
1247 	}
1248 	if (prop & SA_STOP) {
1249 		atomic_clearbits_int(siglist, CONTSIGMASK);
1250 		atomic_clearbits_int(&p->p_flag, P_CONTINUED);
1251 	}
1252 
1253 	SCHED_UNLOCK();
1254 	if (wakeparent)
1255 		wakeup(pr->ps_pptr);
1256 }
1257 
1258 /* fill the signal context which should be used by postsig() and issignal() */
1259 void
setsigctx(struct proc * p,int signum,struct sigctx * sctx)1260 setsigctx(struct proc *p, int signum, struct sigctx *sctx)
1261 {
1262 	struct sigacts *ps = p->p_p->ps_sigacts;
1263 	sigset_t mask;
1264 
1265 	mtx_enter(&p->p_p->ps_mtx);
1266 	mask = sigmask(signum);
1267 	sctx->sig_action = ps->ps_sigact[signum];
1268 	sctx->sig_catchmask = ps->ps_catchmask[signum];
1269 	sctx->sig_reset = (ps->ps_sigreset & mask) != 0;
1270 	sctx->sig_info = (ps->ps_siginfo & mask) != 0;
1271 	sctx->sig_intr = (ps->ps_sigintr & mask) != 0;
1272 	sctx->sig_onstack = (ps->ps_sigonstack & mask) != 0;
1273 	sctx->sig_ignore = (ps->ps_sigignore & mask) != 0;
1274 	sctx->sig_catch = (ps->ps_sigcatch & mask) != 0;
1275 	mtx_leave(&p->p_p->ps_mtx);
1276 }
1277 
1278 /*
1279  * Determine signal that should be delivered to process p, the current
1280  * process, 0 if none.
1281  *
1282  * If the current process has received a signal (should be caught or cause
1283  * termination, should interrupt current syscall), return the signal number.
1284  * Stop signals with default action are processed immediately, then cleared;
1285  * they aren't returned.  This is checked after each entry to the system for
1286  * a syscall or trap. The normal call sequence is
1287  *
1288  *	while (signum = cursig(curproc, &ctx))
1289  *		postsig(signum, &ctx);
1290  *
1291  * Assumes that if the P_SINTR flag is set, we're holding both the
1292  * kernel and scheduler locks.
1293  */
1294 int
cursig(struct proc * p,struct sigctx * sctx)1295 cursig(struct proc *p, struct sigctx *sctx)
1296 {
1297 	struct process *pr = p->p_p;
1298 	int signum, mask, prop;
1299 	sigset_t ps_siglist;
1300 
1301 	KASSERT(p == curproc);
1302 
1303 	for (;;) {
1304 		ps_siglist = READ_ONCE(pr->ps_siglist);
1305 		membar_consumer();
1306 		mask = SIGPENDING(p);
1307 		if (pr->ps_flags & PS_PPWAIT)
1308 			mask &= ~STOPSIGMASK;
1309 		if (mask == 0)	 	/* no signal to send */
1310 			return (0);
1311 		signum = ffs((long)mask);
1312 		mask = sigmask(signum);
1313 
1314 		/* take the signal! */
1315 		if (atomic_cas_uint(&pr->ps_siglist, ps_siglist,
1316 		    ps_siglist & ~mask) != ps_siglist) {
1317 			/* lost race taking the process signal, restart */
1318 			continue;
1319 		}
1320 		atomic_clearbits_int(&p->p_siglist, mask);
1321 		setsigctx(p, signum, sctx);
1322 
1323 		/*
1324 		 * We should see pending but ignored signals
1325 		 * only if PS_TRACED was on when they were posted.
1326 		 */
1327 		if (sctx->sig_ignore && (pr->ps_flags & PS_TRACED) == 0)
1328 			continue;
1329 
1330 		/*
1331 		 * If traced, always stop, and stay stopped until released
1332 		 * by the debugger.  If our parent process is waiting for
1333 		 * us, don't hang as we could deadlock.
1334 		 */
1335 		if (((pr->ps_flags & (PS_TRACED | PS_PPWAIT)) == PS_TRACED) &&
1336 		    signum != SIGKILL) {
1337 			single_thread_set(p, SINGLE_SUSPEND | SINGLE_NOWAIT);
1338 			pr->ps_xsig = signum;
1339 
1340 			SCHED_LOCK();
1341 			proc_stop(p, 1);
1342 			SCHED_UNLOCK();
1343 
1344 			/*
1345 			 * re-take the signal before releasing
1346 			 * the other threads. Must check the continue
1347 			 * conditions below and only take the signal if
1348 			 * those are not true.
1349 			 */
1350 			signum = pr->ps_xsig;
1351 			mask = sigmask(signum);
1352 			setsigctx(p, signum, sctx);
1353 			if (!((pr->ps_flags & PS_TRACED) == 0 ||
1354 			    signum == 0 ||
1355 			    (p->p_sigmask & mask) != 0)) {
1356 				atomic_clearbits_int(&p->p_siglist, mask);
1357 				atomic_clearbits_int(&pr->ps_siglist, mask);
1358 			}
1359 
1360 			single_thread_clear(p, 0);
1361 
1362 			/*
1363 			 * If we are no longer being traced, or the parent
1364 			 * didn't give us a signal, look for more signals.
1365 			 */
1366 			if ((pr->ps_flags & PS_TRACED) == 0 ||
1367 			    signum == 0)
1368 				continue;
1369 
1370 			/*
1371 			 * If the new signal is being masked, look for other
1372 			 * signals.
1373 			 */
1374 			if ((p->p_sigmask & mask) != 0)
1375 				continue;
1376 
1377 		}
1378 
1379 		prop = sigprop[signum];
1380 
1381 		/*
1382 		 * Decide whether the signal should be returned.
1383 		 * Return the signal's number, or fall through
1384 		 * to clear it from the pending mask.
1385 		 */
1386 		switch ((long)sctx->sig_action) {
1387 		case (long)SIG_DFL:
1388 			/*
1389 			 * Don't take default actions on system processes.
1390 			 */
1391 			if (pr->ps_pid <= 1) {
1392 #ifdef DIAGNOSTIC
1393 				/*
1394 				 * Are you sure you want to ignore SIGSEGV
1395 				 * in init? XXX
1396 				 */
1397 				printf("Process (pid %d) got signal"
1398 				    " %d\n", pr->ps_pid, signum);
1399 #endif
1400 				break;		/* == ignore */
1401 			}
1402 			/*
1403 			 * If there is a pending stop signal to process
1404 			 * with default action, stop here,
1405 			 * then clear the signal.  However,
1406 			 * if process is member of an orphaned
1407 			 * process group, ignore tty stop signals.
1408 			 */
1409 			if (prop & SA_STOP) {
1410 				if (pr->ps_flags & PS_TRACED ||
1411 		    		    (pr->ps_pgrp->pg_jobc == 0 &&
1412 				    prop & SA_TTYSTOP))
1413 					break;	/* == ignore */
1414 				pr->ps_xsig = signum;
1415 				SCHED_LOCK();
1416 				proc_stop(p, 1);
1417 				SCHED_UNLOCK();
1418 				break;
1419 			} else if (prop & SA_IGNORE) {
1420 				/*
1421 				 * Except for SIGCONT, shouldn't get here.
1422 				 * Default action is to ignore; drop it.
1423 				 */
1424 				break;		/* == ignore */
1425 			} else
1426 				goto keep;
1427 			/* NOTREACHED */
1428 		case (long)SIG_IGN:
1429 			/*
1430 			 * Masking above should prevent us ever trying
1431 			 * to take action on an ignored signal other
1432 			 * than SIGCONT, unless process is traced.
1433 			 */
1434 			if ((prop & SA_CONT) == 0 &&
1435 			    (pr->ps_flags & PS_TRACED) == 0)
1436 				printf("%s\n", __func__);
1437 			break;		/* == ignore */
1438 		default:
1439 			/*
1440 			 * This signal has an action, let
1441 			 * postsig() process it.
1442 			 */
1443 			goto keep;
1444 		}
1445 	}
1446 	/* NOTREACHED */
1447 
1448 keep:
1449 	atomic_setbits_int(&p->p_siglist, mask); /*leave the signal for later */
1450 	return (signum);
1451 }
1452 
1453 /*
1454  * Put the argument process into the stopped state and notify the parent
1455  * via wakeup.  Signals are handled elsewhere.  The process must not be
1456  * on the run queue.
1457  */
1458 void
proc_stop(struct proc * p,int sw)1459 proc_stop(struct proc *p, int sw)
1460 {
1461 	struct process *pr = p->p_p;
1462 
1463 #ifdef MULTIPROCESSOR
1464 	SCHED_ASSERT_LOCKED();
1465 #endif
1466 	/* do not stop exiting procs */
1467 	if (ISSET(p->p_flag, P_WEXIT))
1468 		return;
1469 
1470 	p->p_stat = SSTOP;
1471 	atomic_clearbits_int(&pr->ps_flags, PS_WAITED);
1472 	atomic_setbits_int(&pr->ps_flags, PS_STOPPED);
1473 	atomic_setbits_int(&p->p_flag, P_SUSPSIG);
1474 	/*
1475 	 * We need this soft interrupt to be handled fast.
1476 	 * Extra calls to softclock don't hurt.
1477 	 */
1478 	softintr_schedule(proc_stop_si);
1479 	if (sw)
1480 		mi_switch();
1481 }
1482 
1483 /*
1484  * Called from a soft interrupt to send signals to the parents of stopped
1485  * processes.
1486  * We can't do this in proc_stop because it's called with nasty locks held
1487  * and we would need recursive scheduler lock to deal with that.
1488  */
1489 void
proc_stop_sweep(void * v)1490 proc_stop_sweep(void *v)
1491 {
1492 	struct process *pr;
1493 
1494 	LIST_FOREACH(pr, &allprocess, ps_list) {
1495 		if ((pr->ps_flags & PS_STOPPED) == 0)
1496 			continue;
1497 		atomic_clearbits_int(&pr->ps_flags, PS_STOPPED);
1498 
1499 		if ((pr->ps_pptr->ps_sigacts->ps_sigflags & SAS_NOCLDSTOP) == 0)
1500 			prsignal(pr->ps_pptr, SIGCHLD);
1501 		wakeup(pr->ps_pptr);
1502 	}
1503 }
1504 
1505 /*
1506  * Take the action for the specified signal
1507  * from the current set of pending signals.
1508  */
1509 void
postsig(struct proc * p,int signum,struct sigctx * sctx)1510 postsig(struct proc *p, int signum, struct sigctx *sctx)
1511 {
1512 	u_long trapno;
1513 	int mask, returnmask;
1514 	siginfo_t si;
1515 	union sigval sigval;
1516 	int code;
1517 
1518 	KASSERT(signum != 0);
1519 
1520 	mask = sigmask(signum);
1521 	atomic_clearbits_int(&p->p_siglist, mask);
1522 	sigval.sival_ptr = NULL;
1523 
1524 	if (p->p_sisig != signum) {
1525 		trapno = 0;
1526 		code = SI_USER;
1527 		sigval.sival_ptr = NULL;
1528 	} else {
1529 		trapno = p->p_sitrapno;
1530 		code = p->p_sicode;
1531 		sigval = p->p_sigval;
1532 	}
1533 	initsiginfo(&si, signum, trapno, code, sigval);
1534 
1535 #ifdef KTRACE
1536 	if (KTRPOINT(p, KTR_PSIG)) {
1537 		ktrpsig(p, signum, sctx->sig_action, p->p_flag & P_SIGSUSPEND ?
1538 		    p->p_oldmask : p->p_sigmask, code, &si);
1539 	}
1540 #endif
1541 	if (sctx->sig_action == SIG_DFL) {
1542 		/*
1543 		 * Default action, where the default is to kill
1544 		 * the process.  (Other cases were ignored above.)
1545 		 */
1546 		KERNEL_LOCK();
1547 		sigexit(p, signum);
1548 		/* NOTREACHED */
1549 	} else {
1550 		/*
1551 		 * If we get here, the signal must be caught.
1552 		 */
1553 #ifdef DIAGNOSTIC
1554 		if (sctx->sig_action == SIG_IGN || (p->p_sigmask & mask))
1555 			panic("postsig action");
1556 #endif
1557 		/*
1558 		 * Set the new mask value and also defer further
1559 		 * occurrences of this signal.
1560 		 *
1561 		 * Special case: user has done a sigpause.  Here the
1562 		 * current mask is not of interest, but rather the
1563 		 * mask from before the sigpause is what we want
1564 		 * restored after the signal processing is completed.
1565 		 */
1566 		if (p->p_flag & P_SIGSUSPEND) {
1567 			atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND);
1568 			returnmask = p->p_oldmask;
1569 		} else {
1570 			returnmask = p->p_sigmask;
1571 		}
1572 		if (p->p_sisig == signum) {
1573 			p->p_sisig = 0;
1574 			p->p_sitrapno = 0;
1575 			p->p_sicode = SI_USER;
1576 			p->p_sigval.sival_ptr = NULL;
1577 		}
1578 
1579 		if (sendsig(sctx->sig_action, signum, returnmask, &si,
1580 		    sctx->sig_info, sctx->sig_onstack)) {
1581 			KERNEL_LOCK();
1582 			sigexit(p, SIGILL);
1583 			/* NOTREACHED */
1584 		}
1585 		postsig_done(p, signum, sctx->sig_catchmask, sctx->sig_reset);
1586 	}
1587 }
1588 
1589 /*
1590  * Force the current process to exit with the specified signal, dumping core
1591  * if appropriate.  We bypass the normal tests for masked and caught signals,
1592  * allowing unrecoverable failures to terminate the process without changing
1593  * signal state.  Mark the accounting record with the signal termination.
1594  * If dumping core, save the signal number for the debugger.  Calls exit and
1595  * does not return.
1596  */
1597 void
sigexit(struct proc * p,int signum)1598 sigexit(struct proc *p, int signum)
1599 {
1600 	/* Mark process as going away */
1601 	atomic_setbits_int(&p->p_flag, P_WEXIT);
1602 
1603 	p->p_p->ps_acflag |= AXSIG;
1604 	if (sigprop[signum] & SA_CORE) {
1605 		p->p_sisig = signum;
1606 
1607 		/* if there are other threads, pause them */
1608 		if (P_HASSIBLING(p))
1609 			single_thread_set(p, SINGLE_UNWIND);
1610 
1611 		if (coredump(p) == 0)
1612 			signum |= WCOREFLAG;
1613 	}
1614 	exit1(p, 0, signum, EXIT_NORMAL);
1615 	/* NOTREACHED */
1616 }
1617 
1618 /*
1619  * Send uncatchable SIGABRT for coredump.
1620  */
1621 void
sigabort(struct proc * p)1622 sigabort(struct proc *p)
1623 {
1624 	struct sigaction sa;
1625 
1626 	KASSERT(p == curproc || panicstr || db_active);
1627 
1628 	memset(&sa, 0, sizeof sa);
1629 	sa.sa_handler = SIG_DFL;
1630 	setsigvec(p, SIGABRT, &sa);
1631 	CLR(p->p_sigmask, sigmask(SIGABRT));
1632 	psignal(p, SIGABRT);
1633 }
1634 
1635 /*
1636  * Return 1 if `sig', a given signal, is ignored or masked for `p', a given
1637  * thread, and 0 otherwise.
1638  */
1639 int
sigismasked(struct proc * p,int sig)1640 sigismasked(struct proc *p, int sig)
1641 {
1642 	struct process *pr = p->p_p;
1643 	int rv;
1644 
1645 	KASSERT(p == curproc);
1646 
1647 	mtx_enter(&pr->ps_mtx);
1648 	rv = (pr->ps_sigacts->ps_sigignore & sigmask(sig)) ||
1649 	    (p->p_sigmask & sigmask(sig));
1650 	mtx_leave(&pr->ps_mtx);
1651 
1652 	return !!rv;
1653 }
1654 
1655 struct coredump_iostate {
1656 	struct proc *io_proc;
1657 	struct vnode *io_vp;
1658 	struct ucred *io_cred;
1659 	off_t io_offset;
1660 };
1661 
1662 /*
1663  * Dump core, into a file named "progname.core", unless the process was
1664  * setuid/setgid.
1665  */
1666 int
coredump(struct proc * p)1667 coredump(struct proc *p)
1668 {
1669 #ifdef SMALL_KERNEL
1670 	return EPERM;
1671 #else
1672 	struct process *pr = p->p_p;
1673 	struct vnode *vp;
1674 	struct ucred *cred = p->p_ucred;
1675 	struct vmspace *vm = p->p_vmspace;
1676 	struct nameidata nd;
1677 	struct vattr vattr;
1678 	struct coredump_iostate	io;
1679 	int error, len, incrash = 0;
1680 	char *name;
1681 	const char *dir = "/var/crash";
1682 
1683 	atomic_setbits_int(&pr->ps_flags, PS_COREDUMP);
1684 
1685 #ifdef PMAP_CHECK_COPYIN
1686 	/* disable copyin checks, so we can write out text sections if needed */
1687 	p->p_vmspace->vm_map.check_copyin_count = 0;
1688 #endif
1689 
1690 	/* Don't dump if will exceed file size limit. */
1691 	if (USPACE + ptoa(vm->vm_dsize + vm->vm_ssize) >= lim_cur(RLIMIT_CORE))
1692 		return (EFBIG);
1693 
1694 	name = pool_get(&namei_pool, PR_WAITOK);
1695 
1696 	/*
1697 	 * If the process has inconsistent uids, nosuidcoredump
1698 	 * determines coredump placement policy.
1699 	 */
1700 	if (((pr->ps_flags & PS_SUGID) && (error = suser(p))) ||
1701 	   ((pr->ps_flags & PS_SUGID) && nosuidcoredump)) {
1702 		if (nosuidcoredump == 3) {
1703 			/*
1704 			 * If the program directory does not exist, dumps of
1705 			 * that core will silently fail.
1706 			 */
1707 			len = snprintf(name, MAXPATHLEN, "%s/%s/%u.core",
1708 			    dir, pr->ps_comm, pr->ps_pid);
1709 			incrash = KERNELPATH;
1710 		} else if (nosuidcoredump == 2) {
1711 			len = snprintf(name, MAXPATHLEN, "%s/%s.core",
1712 			    dir, pr->ps_comm);
1713 			incrash = KERNELPATH;
1714 		} else {
1715 			pool_put(&namei_pool, name);
1716 			return (EPERM);
1717 		}
1718 	} else
1719 		len = snprintf(name, MAXPATHLEN, "%s.core", pr->ps_comm);
1720 
1721 	if (len >= MAXPATHLEN) {
1722 		pool_put(&namei_pool, name);
1723 		return (EACCES);
1724 	}
1725 
1726 	/*
1727 	 * Control the UID used to write out.  The normal case uses
1728 	 * the real UID.  If the sugid case is going to write into the
1729 	 * controlled directory, we do so as root.
1730 	 */
1731 	if (incrash == 0) {
1732 		cred = crdup(cred);
1733 		cred->cr_uid = cred->cr_ruid;
1734 		cred->cr_gid = cred->cr_rgid;
1735 	} else {
1736 		if (p->p_fd->fd_rdir) {
1737 			vrele(p->p_fd->fd_rdir);
1738 			p->p_fd->fd_rdir = NULL;
1739 		}
1740 		p->p_ucred = crdup(p->p_ucred);
1741 		crfree(cred);
1742 		cred = p->p_ucred;
1743 		crhold(cred);
1744 		cred->cr_uid = 0;
1745 		cred->cr_gid = 0;
1746 	}
1747 
1748 	/* incrash should be 0 or KERNELPATH only */
1749 	NDINIT(&nd, 0, BYPASSUNVEIL | incrash, UIO_SYSSPACE, name, p);
1750 
1751 	error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW | O_NONBLOCK,
1752 	    S_IRUSR | S_IWUSR);
1753 
1754 	if (error)
1755 		goto out;
1756 
1757 	/*
1758 	 * Don't dump to non-regular files, files with links, or files
1759 	 * owned by someone else.
1760 	 */
1761 	vp = nd.ni_vp;
1762 	if ((error = VOP_GETATTR(vp, &vattr, cred, p)) != 0) {
1763 		VOP_UNLOCK(vp);
1764 		vn_close(vp, FWRITE, cred, p);
1765 		goto out;
1766 	}
1767 	if (vp->v_type != VREG || vattr.va_nlink != 1 ||
1768 	    vattr.va_mode & ((VREAD | VWRITE) >> 3 | (VREAD | VWRITE) >> 6) ||
1769 	    vattr.va_uid != cred->cr_uid) {
1770 		error = EACCES;
1771 		VOP_UNLOCK(vp);
1772 		vn_close(vp, FWRITE, cred, p);
1773 		goto out;
1774 	}
1775 	VATTR_NULL(&vattr);
1776 	vattr.va_size = 0;
1777 	VOP_SETATTR(vp, &vattr, cred, p);
1778 	pr->ps_acflag |= ACORE;
1779 
1780 	io.io_proc = p;
1781 	io.io_vp = vp;
1782 	io.io_cred = cred;
1783 	io.io_offset = 0;
1784 	VOP_UNLOCK(vp);
1785 	vref(vp);
1786 	error = vn_close(vp, FWRITE, cred, p);
1787 	if (error == 0)
1788 		error = coredump_elf(p, &io);
1789 	vrele(vp);
1790 out:
1791 	crfree(cred);
1792 	pool_put(&namei_pool, name);
1793 	return (error);
1794 #endif
1795 }
1796 
1797 #ifndef SMALL_KERNEL
1798 int
coredump_write(void * cookie,enum uio_seg segflg,const void * data,size_t len,int isvnode)1799 coredump_write(void *cookie, enum uio_seg segflg, const void *data, size_t len,
1800     int isvnode)
1801 {
1802 	struct coredump_iostate *io = cookie;
1803 	off_t coffset = 0;
1804 	size_t csize;
1805 	int chunk, error;
1806 
1807 	csize = len;
1808 	do {
1809 		if (sigmask(SIGKILL) &
1810 		    (io->io_proc->p_siglist | io->io_proc->p_p->ps_siglist))
1811 			return (EINTR);
1812 
1813 		/* Rest of the loop sleeps with lock held, so... */
1814 		yield();
1815 
1816 		chunk = MIN(csize, MAXPHYS);
1817 		error = vn_rdwr(UIO_WRITE, io->io_vp,
1818 		    (caddr_t)data + coffset, chunk,
1819 		    io->io_offset + coffset, segflg,
1820 		    IO_UNIT, io->io_cred, NULL, io->io_proc);
1821 		if (error && (error != EFAULT || !isvnode)) {
1822 			struct process *pr = io->io_proc->p_p;
1823 
1824 			if (error == ENOSPC)
1825 				log(LOG_ERR,
1826 				    "coredump of %s(%d) failed, filesystem full\n",
1827 				    pr->ps_comm, pr->ps_pid);
1828 			else
1829 				log(LOG_ERR,
1830 				    "coredump of %s(%d), write failed: errno %d\n",
1831 				    pr->ps_comm, pr->ps_pid, error);
1832 			return (error);
1833 		}
1834 
1835 		coffset += chunk;
1836 		csize -= chunk;
1837 	} while (csize > 0);
1838 
1839 	io->io_offset += len;
1840 	return (0);
1841 }
1842 
1843 void
coredump_unmap(void * cookie,vaddr_t start,vaddr_t end)1844 coredump_unmap(void *cookie, vaddr_t start, vaddr_t end)
1845 {
1846 	struct coredump_iostate *io = cookie;
1847 
1848 	uvm_unmap(&io->io_proc->p_vmspace->vm_map, start, end);
1849 }
1850 
1851 #endif	/* !SMALL_KERNEL */
1852 
1853 /*
1854  * Nonexistent system call-- signal process (may want to handle it).
1855  * Flag error in case process won't see signal immediately (blocked or ignored).
1856  */
1857 int
sys_nosys(struct proc * p,void * v,register_t * retval)1858 sys_nosys(struct proc *p, void *v, register_t *retval)
1859 {
1860 	ptsignal(p, SIGSYS, STHREAD);
1861 	return (ENOSYS);
1862 }
1863 
1864 int
sys___thrsigdivert(struct proc * p,void * v,register_t * retval)1865 sys___thrsigdivert(struct proc *p, void *v, register_t *retval)
1866 {
1867 	struct sys___thrsigdivert_args /* {
1868 		syscallarg(sigset_t) sigmask;
1869 		syscallarg(siginfo_t *) info;
1870 		syscallarg(const struct timespec *) timeout;
1871 	} */ *uap = v;
1872 	struct sigctx ctx;
1873 	sigset_t mask = SCARG(uap, sigmask) &~ sigcantmask;
1874 	siginfo_t si;
1875 	uint64_t nsecs = INFSLP;
1876 	int timeinvalid = 0;
1877 	int error = 0;
1878 
1879 	memset(&si, 0, sizeof(si));
1880 
1881 	if (SCARG(uap, timeout) != NULL) {
1882 		struct timespec ts;
1883 		if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))) != 0)
1884 			return (error);
1885 #ifdef KTRACE
1886 		if (KTRPOINT(p, KTR_STRUCT))
1887 			ktrreltimespec(p, &ts);
1888 #endif
1889 		if (!timespecisvalid(&ts))
1890 			timeinvalid = 1;
1891 		else
1892 			nsecs = TIMESPEC_TO_NSEC(&ts);
1893 	}
1894 
1895 	dosigsuspend(p, p->p_sigmask &~ mask);
1896 	for (;;) {
1897 		si.si_signo = cursig(p, &ctx);
1898 		if (si.si_signo != 0) {
1899 			sigset_t smask = sigmask(si.si_signo);
1900 			if (smask & mask) {
1901 				atomic_clearbits_int(&p->p_siglist, smask);
1902 				error = 0;
1903 				break;
1904 			}
1905 		}
1906 
1907 		/* per-POSIX, delay this error until after the above */
1908 		if (timeinvalid)
1909 			error = EINVAL;
1910 		/* per-POSIX, return immediately if timeout is zero-valued */
1911 		if (nsecs == 0)
1912 			error = EAGAIN;
1913 
1914 		if (error != 0)
1915 			break;
1916 
1917 		error = tsleep_nsec(&nowake, PPAUSE|PCATCH, "sigwait", nsecs);
1918 	}
1919 
1920 	if (error == 0) {
1921 		*retval = si.si_signo;
1922 		if (SCARG(uap, info) != NULL) {
1923 			error = copyout(&si, SCARG(uap, info), sizeof(si));
1924 #ifdef KTRACE
1925 			if (error == 0 && KTRPOINT(p, KTR_STRUCT))
1926 				ktrsiginfo(p, &si);
1927 #endif
1928 		}
1929 	} else if (error == ERESTART && SCARG(uap, timeout) != NULL) {
1930 		/*
1931 		 * Restarting is wrong if there's a timeout, as it'll be
1932 		 * for the same interval again
1933 		 */
1934 		error = EINTR;
1935 	}
1936 
1937 	return (error);
1938 }
1939 
1940 void
initsiginfo(siginfo_t * si,int sig,u_long trapno,int code,union sigval val)1941 initsiginfo(siginfo_t *si, int sig, u_long trapno, int code, union sigval val)
1942 {
1943 	memset(si, 0, sizeof(*si));
1944 
1945 	si->si_signo = sig;
1946 	si->si_code = code;
1947 	if (code == SI_USER) {
1948 		si->si_value = val;
1949 	} else {
1950 		switch (sig) {
1951 		case SIGSEGV:
1952 		case SIGILL:
1953 		case SIGBUS:
1954 		case SIGFPE:
1955 			si->si_addr = val.sival_ptr;
1956 			si->si_trapno = trapno;
1957 			break;
1958 		case SIGXFSZ:
1959 			break;
1960 		}
1961 	}
1962 }
1963 
1964 int
filt_sigattach(struct knote * kn)1965 filt_sigattach(struct knote *kn)
1966 {
1967 	struct process *pr = curproc->p_p;
1968 	int s;
1969 
1970 	if (kn->kn_id >= NSIG)
1971 		return EINVAL;
1972 
1973 	kn->kn_ptr.p_process = pr;
1974 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
1975 
1976 	s = splhigh();
1977 	klist_insert_locked(&pr->ps_klist, kn);
1978 	splx(s);
1979 
1980 	return (0);
1981 }
1982 
1983 void
filt_sigdetach(struct knote * kn)1984 filt_sigdetach(struct knote *kn)
1985 {
1986 	struct process *pr = kn->kn_ptr.p_process;
1987 	int s;
1988 
1989 	s = splhigh();
1990 	klist_remove_locked(&pr->ps_klist, kn);
1991 	splx(s);
1992 }
1993 
1994 /*
1995  * signal knotes are shared with proc knotes, so we apply a mask to
1996  * the hint in order to differentiate them from process hints.  This
1997  * could be avoided by using a signal-specific knote list, but probably
1998  * isn't worth the trouble.
1999  */
2000 int
filt_signal(struct knote * kn,long hint)2001 filt_signal(struct knote *kn, long hint)
2002 {
2003 
2004 	if (hint & NOTE_SIGNAL) {
2005 		hint &= ~NOTE_SIGNAL;
2006 
2007 		if (kn->kn_id == hint)
2008 			kn->kn_data++;
2009 	}
2010 	return (kn->kn_data != 0);
2011 }
2012 
2013 void
userret(struct proc * p)2014 userret(struct proc *p)
2015 {
2016 	struct sigctx ctx;
2017 	int signum;
2018 
2019 	if (p->p_flag & P_SUSPSINGLE)
2020 		single_thread_check(p, 0);
2021 
2022 	/* send SIGPROF or SIGVTALRM if their timers interrupted this thread */
2023 	if (p->p_flag & P_PROFPEND) {
2024 		atomic_clearbits_int(&p->p_flag, P_PROFPEND);
2025 		KERNEL_LOCK();
2026 		psignal(p, SIGPROF);
2027 		KERNEL_UNLOCK();
2028 	}
2029 	if (p->p_flag & P_ALRMPEND) {
2030 		atomic_clearbits_int(&p->p_flag, P_ALRMPEND);
2031 		KERNEL_LOCK();
2032 		psignal(p, SIGVTALRM);
2033 		KERNEL_UNLOCK();
2034 	}
2035 
2036 	if (SIGPENDING(p) != 0) {
2037 		while ((signum = cursig(p, &ctx)) != 0)
2038 			postsig(p, signum, &ctx);
2039 	}
2040 
2041 	/*
2042 	 * If P_SIGSUSPEND is still set here, then we still need to restore
2043 	 * the original sigmask before returning to userspace.  Also, this
2044 	 * might unmask some pending signals, so we need to check a second
2045 	 * time for signals to post.
2046 	 */
2047 	if (p->p_flag & P_SIGSUSPEND) {
2048 		p->p_sigmask = p->p_oldmask;
2049 		atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND);
2050 
2051 		while ((signum = cursig(p, &ctx)) != 0)
2052 			postsig(p, signum, &ctx);
2053 	}
2054 
2055 	WITNESS_WARN(WARN_PANIC, NULL, "userret: returning");
2056 
2057 	p->p_cpu->ci_schedstate.spc_curpriority = p->p_usrpri;
2058 }
2059 
2060 int
single_thread_check_locked(struct proc * p,int deep)2061 single_thread_check_locked(struct proc *p, int deep)
2062 {
2063 	struct process *pr = p->p_p;
2064 
2065 	MUTEX_ASSERT_LOCKED(&pr->ps_mtx);
2066 
2067 	if (pr->ps_single == NULL || pr->ps_single == p)
2068 		return (0);
2069 
2070 	do {
2071 		/* if we're in deep, we need to unwind to the edge */
2072 		if (deep) {
2073 			if (pr->ps_flags & PS_SINGLEUNWIND)
2074 				return (ERESTART);
2075 			if (pr->ps_flags & PS_SINGLEEXIT)
2076 				return (EINTR);
2077 		}
2078 
2079 		if (pr->ps_flags & PS_SINGLEEXIT) {
2080 			mtx_leave(&pr->ps_mtx);
2081 			KERNEL_LOCK();
2082 			exit1(p, 0, 0, EXIT_THREAD_NOCHECK);
2083 			/* NOTREACHED */
2084 		}
2085 
2086 		if (--pr->ps_singlecnt == 0)
2087 			wakeup(&pr->ps_singlecnt);
2088 
2089 		/* not exiting and don't need to unwind, so suspend */
2090 		mtx_leave(&pr->ps_mtx);
2091 
2092 		SCHED_LOCK();
2093 		p->p_stat = SSTOP;
2094 		mi_switch();
2095 		SCHED_UNLOCK();
2096 		mtx_enter(&pr->ps_mtx);
2097 	} while (pr->ps_single != NULL);
2098 
2099 	return (0);
2100 }
2101 
2102 int
single_thread_check(struct proc * p,int deep)2103 single_thread_check(struct proc *p, int deep)
2104 {
2105 	int error;
2106 
2107 	mtx_enter(&p->p_p->ps_mtx);
2108 	error = single_thread_check_locked(p, deep);
2109 	mtx_leave(&p->p_p->ps_mtx);
2110 
2111 	return error;
2112 }
2113 
2114 /*
2115  * Stop other threads in the process.  The mode controls how and
2116  * where the other threads should stop:
2117  *  - SINGLE_SUSPEND: stop wherever they are, will later be released (via
2118  *    single_thread_clear())
2119  *  - SINGLE_UNWIND: just unwind to kernel boundary, will be told to exit
2120  *    (by setting to SINGLE_EXIT) or released as with SINGLE_SUSPEND
2121  *  - SINGLE_EXIT: unwind to kernel boundary and exit
2122  */
2123 int
single_thread_set(struct proc * p,int flags)2124 single_thread_set(struct proc *p, int flags)
2125 {
2126 	struct process *pr = p->p_p;
2127 	struct proc *q;
2128 	int error, mode = flags & SINGLE_MASK;
2129 
2130 	KASSERT(curproc == p);
2131 
2132 	mtx_enter(&pr->ps_mtx);
2133 	error = single_thread_check_locked(p, flags & SINGLE_DEEP);
2134 	if (error) {
2135 		mtx_leave(&pr->ps_mtx);
2136 		return error;
2137 	}
2138 
2139 	switch (mode) {
2140 	case SINGLE_SUSPEND:
2141 		break;
2142 	case SINGLE_UNWIND:
2143 		atomic_setbits_int(&pr->ps_flags, PS_SINGLEUNWIND);
2144 		break;
2145 	case SINGLE_EXIT:
2146 		atomic_setbits_int(&pr->ps_flags, PS_SINGLEEXIT);
2147 		atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND);
2148 		break;
2149 #ifdef DIAGNOSTIC
2150 	default:
2151 		panic("single_thread_mode = %d", mode);
2152 #endif
2153 	}
2154 	pr->ps_single = p;
2155 	pr->ps_singlecnt = pr->ps_threadcnt;
2156 
2157 	TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) {
2158 		if (q == p)
2159 			continue;
2160 		SCHED_LOCK();
2161 		atomic_setbits_int(&q->p_flag, P_SUSPSINGLE);
2162 		switch (q->p_stat) {
2163 		case SIDL:
2164 		case SDEAD:
2165 			break;
2166 		case SSLEEP:
2167 			/* if it's not interruptible, then just have to wait */
2168 			if (q->p_flag & P_SINTR) {
2169 				/* merely need to suspend?  just stop it */
2170 				if (mode == SINGLE_SUSPEND) {
2171 					q->p_stat = SSTOP;
2172 					--pr->ps_singlecnt;
2173 					break;
2174 				}
2175 				/* need to unwind or exit, so wake it */
2176 				unsleep(q);
2177 				setrunnable(q);
2178 			}
2179 			break;
2180 		case SSTOP:
2181 			if (mode == SINGLE_EXIT) {
2182 				unsleep(q);
2183 				setrunnable(q);
2184 			} else
2185 				--pr->ps_singlecnt;
2186 			break;
2187 		case SONPROC:
2188 			signotify(q);
2189 			/* FALLTHROUGH */
2190 		case SRUN:
2191 			break;
2192 		}
2193 		SCHED_UNLOCK();
2194 	}
2195 
2196 	/* count ourselfs out */
2197 	--pr->ps_singlecnt;
2198 	mtx_leave(&pr->ps_mtx);
2199 
2200 	if ((flags & SINGLE_NOWAIT) == 0)
2201 		single_thread_wait(pr, 1);
2202 
2203 	return 0;
2204 }
2205 
2206 /*
2207  * Wait for other threads to stop. If recheck is false then the function
2208  * returns non-zero if the caller needs to restart the check else 0 is
2209  * returned. If recheck is true the return value is always 0.
2210  */
2211 int
single_thread_wait(struct process * pr,int recheck)2212 single_thread_wait(struct process *pr, int recheck)
2213 {
2214 	int wait;
2215 
2216 	/* wait until they're all suspended */
2217 	mtx_enter(&pr->ps_mtx);
2218 	while ((wait = pr->ps_singlecnt > 0)) {
2219 		msleep_nsec(&pr->ps_singlecnt, &pr->ps_mtx, PWAIT, "suspend",
2220 		    INFSLP);
2221 		if (!recheck)
2222 			break;
2223 	}
2224 	mtx_leave(&pr->ps_mtx);
2225 
2226 	return wait;
2227 }
2228 
2229 void
single_thread_clear(struct proc * p,int flag)2230 single_thread_clear(struct proc *p, int flag)
2231 {
2232 	struct process *pr = p->p_p;
2233 	struct proc *q;
2234 
2235 	KASSERT(pr->ps_single == p);
2236 	KASSERT(curproc == p);
2237 
2238 	mtx_enter(&pr->ps_mtx);
2239 	pr->ps_single = NULL;
2240 	atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND | PS_SINGLEEXIT);
2241 
2242 	TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) {
2243 		if (q == p || (q->p_flag & P_SUSPSINGLE) == 0)
2244 			continue;
2245 		atomic_clearbits_int(&q->p_flag, P_SUSPSINGLE);
2246 
2247 		/*
2248 		 * if the thread was only stopped for single threading
2249 		 * then clearing that either makes it runnable or puts
2250 		 * it back into some sleep queue
2251 		 */
2252 		SCHED_LOCK();
2253 		if (q->p_stat == SSTOP && (q->p_flag & flag) == 0) {
2254 			if (q->p_wchan == NULL)
2255 				setrunnable(q);
2256 			else {
2257 				atomic_clearbits_int(&q->p_flag, P_WSLEEP);
2258 				q->p_stat = SSLEEP;
2259 			}
2260 		}
2261 		SCHED_UNLOCK();
2262 	}
2263 	mtx_leave(&pr->ps_mtx);
2264 }
2265 
2266 void
sigio_del(struct sigiolst * rmlist)2267 sigio_del(struct sigiolst *rmlist)
2268 {
2269 	struct sigio *sigio;
2270 
2271 	while ((sigio = LIST_FIRST(rmlist)) != NULL) {
2272 		LIST_REMOVE(sigio, sio_pgsigio);
2273 		crfree(sigio->sio_ucred);
2274 		free(sigio, M_SIGIO, sizeof(*sigio));
2275 	}
2276 }
2277 
2278 void
sigio_unlink(struct sigio_ref * sir,struct sigiolst * rmlist)2279 sigio_unlink(struct sigio_ref *sir, struct sigiolst *rmlist)
2280 {
2281 	struct sigio *sigio;
2282 
2283 	MUTEX_ASSERT_LOCKED(&sigio_lock);
2284 
2285 	sigio = sir->sir_sigio;
2286 	if (sigio != NULL) {
2287 		KASSERT(sigio->sio_myref == sir);
2288 		sir->sir_sigio = NULL;
2289 
2290 		if (sigio->sio_pgid > 0)
2291 			sigio->sio_proc = NULL;
2292 		else
2293 			sigio->sio_pgrp = NULL;
2294 		LIST_REMOVE(sigio, sio_pgsigio);
2295 
2296 		LIST_INSERT_HEAD(rmlist, sigio, sio_pgsigio);
2297 	}
2298 }
2299 
2300 void
sigio_free(struct sigio_ref * sir)2301 sigio_free(struct sigio_ref *sir)
2302 {
2303 	struct sigiolst rmlist;
2304 
2305 	if (sir->sir_sigio == NULL)
2306 		return;
2307 
2308 	LIST_INIT(&rmlist);
2309 
2310 	mtx_enter(&sigio_lock);
2311 	sigio_unlink(sir, &rmlist);
2312 	mtx_leave(&sigio_lock);
2313 
2314 	sigio_del(&rmlist);
2315 }
2316 
2317 void
sigio_freelist(struct sigiolst * sigiolst)2318 sigio_freelist(struct sigiolst *sigiolst)
2319 {
2320 	struct sigiolst rmlist;
2321 	struct sigio *sigio;
2322 
2323 	if (LIST_EMPTY(sigiolst))
2324 		return;
2325 
2326 	LIST_INIT(&rmlist);
2327 
2328 	mtx_enter(&sigio_lock);
2329 	while ((sigio = LIST_FIRST(sigiolst)) != NULL)
2330 		sigio_unlink(sigio->sio_myref, &rmlist);
2331 	mtx_leave(&sigio_lock);
2332 
2333 	sigio_del(&rmlist);
2334 }
2335 
2336 int
sigio_setown(struct sigio_ref * sir,u_long cmd,caddr_t data)2337 sigio_setown(struct sigio_ref *sir, u_long cmd, caddr_t data)
2338 {
2339 	struct sigiolst rmlist;
2340 	struct proc *p = curproc;
2341 	struct pgrp *pgrp = NULL;
2342 	struct process *pr = NULL;
2343 	struct sigio *sigio;
2344 	int error;
2345 	pid_t pgid = *(int *)data;
2346 
2347 	if (pgid == 0) {
2348 		sigio_free(sir);
2349 		return (0);
2350 	}
2351 
2352 	if (cmd == TIOCSPGRP) {
2353 		if (pgid < 0)
2354 			return (EINVAL);
2355 		pgid = -pgid;
2356 	}
2357 
2358 	sigio = malloc(sizeof(*sigio), M_SIGIO, M_WAITOK);
2359 	sigio->sio_pgid = pgid;
2360 	sigio->sio_ucred = crhold(p->p_ucred);
2361 	sigio->sio_myref = sir;
2362 
2363 	LIST_INIT(&rmlist);
2364 
2365 	/*
2366 	 * The kernel lock, and not sleeping between prfind()/pgfind() and
2367 	 * linking of the sigio ensure that the process or process group does
2368 	 * not disappear unexpectedly.
2369 	 */
2370 	KERNEL_LOCK();
2371 	mtx_enter(&sigio_lock);
2372 
2373 	if (pgid > 0) {
2374 		pr = prfind(pgid);
2375 		if (pr == NULL) {
2376 			error = ESRCH;
2377 			goto fail;
2378 		}
2379 
2380 		/*
2381 		 * Policy - Don't allow a process to FSETOWN a process
2382 		 * in another session.
2383 		 *
2384 		 * Remove this test to allow maximum flexibility or
2385 		 * restrict FSETOWN to the current process or process
2386 		 * group for maximum safety.
2387 		 */
2388 		if (pr->ps_session != p->p_p->ps_session) {
2389 			error = EPERM;
2390 			goto fail;
2391 		}
2392 
2393 		if ((pr->ps_flags & PS_EXITING) != 0) {
2394 			error = ESRCH;
2395 			goto fail;
2396 		}
2397 	} else /* if (pgid < 0) */ {
2398 		pgrp = pgfind(-pgid);
2399 		if (pgrp == NULL) {
2400 			error = ESRCH;
2401 			goto fail;
2402 		}
2403 
2404 		/*
2405 		 * Policy - Don't allow a process to FSETOWN a process
2406 		 * in another session.
2407 		 *
2408 		 * Remove this test to allow maximum flexibility or
2409 		 * restrict FSETOWN to the current process or process
2410 		 * group for maximum safety.
2411 		 */
2412 		if (pgrp->pg_session != p->p_p->ps_session) {
2413 			error = EPERM;
2414 			goto fail;
2415 		}
2416 	}
2417 
2418 	if (pgid > 0) {
2419 		sigio->sio_proc = pr;
2420 		LIST_INSERT_HEAD(&pr->ps_sigiolst, sigio, sio_pgsigio);
2421 	} else {
2422 		sigio->sio_pgrp = pgrp;
2423 		LIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
2424 	}
2425 
2426 	sigio_unlink(sir, &rmlist);
2427 	sir->sir_sigio = sigio;
2428 
2429 	mtx_leave(&sigio_lock);
2430 	KERNEL_UNLOCK();
2431 
2432 	sigio_del(&rmlist);
2433 
2434 	return (0);
2435 
2436 fail:
2437 	mtx_leave(&sigio_lock);
2438 	KERNEL_UNLOCK();
2439 
2440 	crfree(sigio->sio_ucred);
2441 	free(sigio, M_SIGIO, sizeof(*sigio));
2442 
2443 	return (error);
2444 }
2445 
2446 void
sigio_getown(struct sigio_ref * sir,u_long cmd,caddr_t data)2447 sigio_getown(struct sigio_ref *sir, u_long cmd, caddr_t data)
2448 {
2449 	struct sigio *sigio;
2450 	pid_t pgid = 0;
2451 
2452 	mtx_enter(&sigio_lock);
2453 	sigio = sir->sir_sigio;
2454 	if (sigio != NULL)
2455 		pgid = sigio->sio_pgid;
2456 	mtx_leave(&sigio_lock);
2457 
2458 	if (cmd == TIOCGPGRP)
2459 		pgid = -pgid;
2460 
2461 	*(int *)data = pgid;
2462 }
2463 
2464 void
sigio_copy(struct sigio_ref * dst,struct sigio_ref * src)2465 sigio_copy(struct sigio_ref *dst, struct sigio_ref *src)
2466 {
2467 	struct sigiolst rmlist;
2468 	struct sigio *newsigio, *sigio;
2469 
2470 	sigio_free(dst);
2471 
2472 	if (src->sir_sigio == NULL)
2473 		return;
2474 
2475 	newsigio = malloc(sizeof(*newsigio), M_SIGIO, M_WAITOK);
2476 	LIST_INIT(&rmlist);
2477 
2478 	mtx_enter(&sigio_lock);
2479 
2480 	sigio = src->sir_sigio;
2481 	if (sigio == NULL) {
2482 		mtx_leave(&sigio_lock);
2483 		free(newsigio, M_SIGIO, sizeof(*newsigio));
2484 		return;
2485 	}
2486 
2487 	newsigio->sio_pgid = sigio->sio_pgid;
2488 	newsigio->sio_ucred = crhold(sigio->sio_ucred);
2489 	newsigio->sio_myref = dst;
2490 	if (newsigio->sio_pgid > 0) {
2491 		newsigio->sio_proc = sigio->sio_proc;
2492 		LIST_INSERT_HEAD(&newsigio->sio_proc->ps_sigiolst, newsigio,
2493 		    sio_pgsigio);
2494 	} else {
2495 		newsigio->sio_pgrp = sigio->sio_pgrp;
2496 		LIST_INSERT_HEAD(&newsigio->sio_pgrp->pg_sigiolst, newsigio,
2497 		    sio_pgsigio);
2498 	}
2499 
2500 	sigio_unlink(dst, &rmlist);
2501 	dst->sir_sigio = newsigio;
2502 
2503 	mtx_leave(&sigio_lock);
2504 
2505 	sigio_del(&rmlist);
2506 }
2507