xref: /freebsd/sys/compat/linux/linux_signal.c (revision fdea83a5f63b881ade91c6cc9dfbf173137512c5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1994-1995 Søren Schmidt
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include "opt_ktrace.h"
30 
31 #include <sys/param.h>
32 #include <sys/ktr.h>
33 #include <sys/lock.h>
34 #include <sys/mutex.h>
35 #include <sys/proc.h>
36 #include <sys/signalvar.h>
37 #include <sys/sx.h>
38 #include <sys/syscallsubr.h>
39 #include <sys/sysproto.h>
40 #ifdef KTRACE
41 #include <sys/ktrace.h>
42 #endif
43 
44 #include <security/audit/audit.h>
45 
46 #ifdef COMPAT_LINUX32
47 #include <machine/../linux32/linux.h>
48 #include <machine/../linux32/linux32_proto.h>
49 #else
50 #include <machine/../linux/linux.h>
51 #include <machine/../linux/linux_proto.h>
52 #endif
53 #include <compat/linux/linux_mib.h>
54 #include <compat/linux/linux_signal.h>
55 #include <compat/linux/linux_time.h>
56 #include <compat/linux/linux_util.h>
57 #include <compat/linux/linux_emul.h>
58 #include <compat/linux/linux_misc.h>
59 
60 static int	linux_pksignal(struct thread *td, int pid, int sig,
61 		    ksiginfo_t *ksi);
62 static int	linux_psignal(struct thread *td, int pid, int sig);
63 static int	linux_tdksignal(struct thread *td, lwpid_t tid,
64 		    int tgid, int sig, ksiginfo_t *ksi);
65 static int	linux_tdsignal(struct thread *td, lwpid_t tid,
66 		    int tgid, int sig);
67 static void	sicode_to_lsicode(int sig, int si_code, int *lsi_code);
68 static int	linux_common_rt_sigtimedwait(struct thread *,
69 		    l_sigset_t *, struct timespec *, l_siginfo_t *,
70 		    l_size_t);
71 
72 static void
linux_to_bsd_sigaction(l_sigaction_t * lsa,struct sigaction * bsa)73 linux_to_bsd_sigaction(l_sigaction_t *lsa, struct sigaction *bsa)
74 {
75 	unsigned long flags;
76 
77 	linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
78 	bsa->sa_handler = PTRIN(lsa->lsa_handler);
79 	bsa->sa_flags = 0;
80 
81 	flags = lsa->lsa_flags;
82 	if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP) {
83 		flags &= ~LINUX_SA_NOCLDSTOP;
84 		bsa->sa_flags |= SA_NOCLDSTOP;
85 	}
86 	if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT) {
87 		flags &= ~LINUX_SA_NOCLDWAIT;
88 		bsa->sa_flags |= SA_NOCLDWAIT;
89 	}
90 	if (lsa->lsa_flags & LINUX_SA_SIGINFO) {
91 		flags &= ~LINUX_SA_SIGINFO;
92 		bsa->sa_flags |= SA_SIGINFO;
93 #ifdef notyet
94 		/*
95 		 * XXX: We seem to be missing code to convert
96 		 *      some of the fields in ucontext_t.
97 		 */
98 		linux_msg(curthread,
99 		    "partially unsupported sigaction flag SA_SIGINFO");
100 #endif
101 	}
102 	if (lsa->lsa_flags & LINUX_SA_RESTORER) {
103 		flags &= ~LINUX_SA_RESTORER;
104 		/*
105 		 * We ignore the lsa_restorer and always use our own signal
106 		 * trampoline instead.  It looks like SA_RESTORER is obsolete
107 		 * in Linux too - it doesn't seem to be used at all on arm64.
108 		 * In any case: see Linux sigreturn(2).
109 		 */
110 	}
111 	if (lsa->lsa_flags & LINUX_SA_ONSTACK) {
112 		flags &= ~LINUX_SA_ONSTACK;
113 		bsa->sa_flags |= SA_ONSTACK;
114 	}
115 	if (lsa->lsa_flags & LINUX_SA_RESTART) {
116 		flags &= ~LINUX_SA_RESTART;
117 		bsa->sa_flags |= SA_RESTART;
118 	}
119 	if (lsa->lsa_flags & LINUX_SA_INTERRUPT) {
120 		flags &= ~LINUX_SA_INTERRUPT;
121 		/* Documented to be a "historical no-op". */
122 	}
123 	if (lsa->lsa_flags & LINUX_SA_ONESHOT) {
124 		flags &= ~LINUX_SA_ONESHOT;
125 		bsa->sa_flags |= SA_RESETHAND;
126 	}
127 	if (lsa->lsa_flags & LINUX_SA_NOMASK) {
128 		flags &= ~LINUX_SA_NOMASK;
129 		bsa->sa_flags |= SA_NODEFER;
130 	}
131 
132 	/*
133 	 * SA_UNSUPPORTED was introduced in Linux 5.11 to probe support for
134 	 * other flags such as SA_EXPOSE_TAGBITS, introduced at the same time.
135 	 * Ignore both.
136 	 */
137 	if (lsa->lsa_flags & (LINUX_SA_UNSUPPORTED | LINUX_SA_EXPOSE_TAGBITS))
138 		flags &= ~(LINUX_SA_UNSUPPORTED | LINUX_SA_EXPOSE_TAGBITS);
139 
140 	if (flags != 0)
141 		linux_msg(curthread, "unsupported sigaction flag %#lx", flags);
142 }
143 
144 static void
bsd_to_linux_sigaction(struct sigaction * bsa,l_sigaction_t * lsa)145 bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa)
146 {
147 
148 	bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
149 #ifdef COMPAT_LINUX32
150 	lsa->lsa_handler = (uintptr_t)bsa->sa_handler;
151 #else
152 	lsa->lsa_handler = bsa->sa_handler;
153 #endif
154 	lsa->lsa_restorer = 0;		/* unsupported */
155 	lsa->lsa_flags = 0;
156 	if (bsa->sa_flags & SA_NOCLDSTOP)
157 		lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
158 	if (bsa->sa_flags & SA_NOCLDWAIT)
159 		lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
160 	if (bsa->sa_flags & SA_SIGINFO)
161 		lsa->lsa_flags |= LINUX_SA_SIGINFO;
162 	if (bsa->sa_flags & SA_ONSTACK)
163 		lsa->lsa_flags |= LINUX_SA_ONSTACK;
164 	if (bsa->sa_flags & SA_RESTART)
165 		lsa->lsa_flags |= LINUX_SA_RESTART;
166 	if (bsa->sa_flags & SA_RESETHAND)
167 		lsa->lsa_flags |= LINUX_SA_ONESHOT;
168 	if (bsa->sa_flags & SA_NODEFER)
169 		lsa->lsa_flags |= LINUX_SA_NOMASK;
170 }
171 
172 int
linux_do_sigaction(struct thread * td,int linux_sig,l_sigaction_t * linux_nsa,l_sigaction_t * linux_osa)173 linux_do_sigaction(struct thread *td, int linux_sig, l_sigaction_t *linux_nsa,
174 		   l_sigaction_t *linux_osa)
175 {
176 	struct sigaction act, oact, *nsa, *osa;
177 	int error, sig;
178 
179 	if (!LINUX_SIG_VALID(linux_sig))
180 		return (EINVAL);
181 	sig = linux_to_bsd_signal(linux_sig);
182 
183 	osa = (linux_osa != NULL) ? &oact : NULL;
184 	if (linux_nsa != NULL) {
185 		nsa = &act;
186 		linux_to_bsd_sigaction(linux_nsa, nsa);
187 #ifdef KTRACE
188 		if (KTRPOINT(td, KTR_STRUCT))
189 			linux_ktrsigset(&linux_nsa->lsa_mask,
190 			    sizeof(linux_nsa->lsa_mask));
191 #endif
192 		if ((sig == SIGKILL || sig == SIGSTOP) &&
193 		    nsa->sa_handler == SIG_DFL)
194 			return (EINVAL);
195 	} else
196 		nsa = NULL;
197 
198 	error = kern_sigaction(td, sig, nsa, osa, 0);
199 	if (error != 0)
200 		return (error);
201 
202 	if (linux_osa != NULL) {
203 		bsd_to_linux_sigaction(osa, linux_osa);
204 #ifdef KTRACE
205 		if (KTRPOINT(td, KTR_STRUCT))
206 			linux_ktrsigset(&linux_osa->lsa_mask,
207 			    sizeof(linux_osa->lsa_mask));
208 #endif
209 	}
210 	return (0);
211 }
212 
213 int
linux_sigaltstack(struct thread * td,struct linux_sigaltstack_args * uap)214 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
215 {
216 	stack_t ss, oss;
217 	l_stack_t lss;
218 	int error;
219 
220 	memset(&lss, 0, sizeof(lss));
221 	LINUX_CTR2(sigaltstack, "%p, %p", uap->uss, uap->uoss);
222 
223 	if (uap->uss != NULL) {
224 		error = copyin(uap->uss, &lss, sizeof(lss));
225 		if (error != 0)
226 			return (error);
227 
228 		ss.ss_sp = PTRIN(lss.ss_sp);
229 		ss.ss_size = lss.ss_size;
230 		ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
231 	}
232 	error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
233 	    (uap->uoss != NULL) ? &oss : NULL);
234 	if (error == 0 && uap->uoss != NULL) {
235 		lss.ss_sp = PTROUT(oss.ss_sp);
236 		lss.ss_size = oss.ss_size;
237 		lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
238 		error = copyout(&lss, uap->uoss, sizeof(lss));
239 	}
240 
241 	return (error);
242 }
243 
244 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
245 int
linux_signal(struct thread * td,struct linux_signal_args * args)246 linux_signal(struct thread *td, struct linux_signal_args *args)
247 {
248 	l_sigaction_t nsa, osa;
249 	int error;
250 
251 	nsa.lsa_handler = args->handler;
252 	nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
253 	LINUX_SIGEMPTYSET(nsa.lsa_mask);
254 
255 	error = linux_do_sigaction(td, args->sig, &nsa, &osa);
256 	td->td_retval[0] = (int)(intptr_t)osa.lsa_handler;
257 
258 	return (error);
259 }
260 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
261 
262 int
linux_rt_sigaction(struct thread * td,struct linux_rt_sigaction_args * args)263 linux_rt_sigaction(struct thread *td, struct linux_rt_sigaction_args *args)
264 {
265 	l_sigaction_t nsa, osa;
266 	int error;
267 
268 	if (args->sigsetsize != sizeof(l_sigset_t))
269 		return (EINVAL);
270 
271 	if (args->act != NULL) {
272 		error = copyin(args->act, &nsa, sizeof(nsa));
273 		if (error != 0)
274 			return (error);
275 	}
276 
277 	error = linux_do_sigaction(td, args->sig,
278 				   args->act ? &nsa : NULL,
279 				   args->oact ? &osa : NULL);
280 
281 	if (args->oact != NULL && error == 0)
282 		error = copyout(&osa, args->oact, sizeof(osa));
283 
284 	return (error);
285 }
286 
287 static int
linux_do_sigprocmask(struct thread * td,int how,sigset_t * new,l_sigset_t * old)288 linux_do_sigprocmask(struct thread *td, int how, sigset_t *new,
289 		     l_sigset_t *old)
290 {
291 	sigset_t omask;
292 	int error;
293 
294 	td->td_retval[0] = 0;
295 
296 	switch (how) {
297 	case LINUX_SIG_BLOCK:
298 		how = SIG_BLOCK;
299 		break;
300 	case LINUX_SIG_UNBLOCK:
301 		how = SIG_UNBLOCK;
302 		break;
303 	case LINUX_SIG_SETMASK:
304 		how = SIG_SETMASK;
305 		break;
306 	default:
307 		return (EINVAL);
308 	}
309 	error = kern_sigprocmask(td, how, new, &omask, 0);
310 	if (error == 0 && old != NULL)
311 		bsd_to_linux_sigset(&omask, old);
312 
313 	return (error);
314 }
315 
316 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
317 int
linux_sigprocmask(struct thread * td,struct linux_sigprocmask_args * args)318 linux_sigprocmask(struct thread *td, struct linux_sigprocmask_args *args)
319 {
320 	l_osigset_t mask;
321 	l_sigset_t lset, oset;
322 	sigset_t set;
323 	int error;
324 
325 	if (args->mask != NULL) {
326 		error = copyin(args->mask, &mask, sizeof(mask));
327 		if (error != 0)
328 			return (error);
329 		LINUX_SIGEMPTYSET(lset);
330 		lset.__mask = mask;
331 #ifdef KTRACE
332 		if (KTRPOINT(td, KTR_STRUCT))
333 			linux_ktrsigset(&lset, sizeof(lset));
334 #endif
335 		linux_to_bsd_sigset(&lset, &set);
336 	}
337 
338 	error = linux_do_sigprocmask(td, args->how,
339 				     args->mask ? &set : NULL,
340 				     args->omask ? &oset : NULL);
341 
342 	if (args->omask != NULL && error == 0) {
343 #ifdef KTRACE
344 		if (KTRPOINT(td, KTR_STRUCT))
345 			linux_ktrsigset(&oset, sizeof(oset));
346 #endif
347 		mask = oset.__mask;
348 		error = copyout(&mask, args->omask, sizeof(mask));
349 	}
350 
351 	return (error);
352 }
353 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
354 
355 int
linux_rt_sigprocmask(struct thread * td,struct linux_rt_sigprocmask_args * args)356 linux_rt_sigprocmask(struct thread *td, struct linux_rt_sigprocmask_args *args)
357 {
358 	l_sigset_t oset;
359 	sigset_t set, *pset;
360 	int error;
361 
362 	error = linux_copyin_sigset(td, args->mask, args->sigsetsize,
363 	    &set, &pset);
364 	if (error != 0)
365 		return (EINVAL);
366 
367 	error = linux_do_sigprocmask(td, args->how, pset,
368 				     args->omask ? &oset : NULL);
369 
370 	if (args->omask != NULL && error == 0) {
371 #ifdef KTRACE
372 		if (KTRPOINT(td, KTR_STRUCT))
373 			linux_ktrsigset(&oset, sizeof(oset));
374 #endif
375 		error = copyout(&oset, args->omask, sizeof(oset));
376 	}
377 
378 	return (error);
379 }
380 
381 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
382 int
linux_sgetmask(struct thread * td,struct linux_sgetmask_args * args)383 linux_sgetmask(struct thread *td, struct linux_sgetmask_args *args)
384 {
385 	struct proc *p = td->td_proc;
386 	l_sigset_t mask;
387 
388 	PROC_LOCK(p);
389 	bsd_to_linux_sigset(&td->td_sigmask, &mask);
390 	PROC_UNLOCK(p);
391 	td->td_retval[0] = mask.__mask;
392 #ifdef KTRACE
393 	if (KTRPOINT(td, KTR_STRUCT))
394 		linux_ktrsigset(&mask, sizeof(mask));
395 #endif
396 	return (0);
397 }
398 
399 int
linux_ssetmask(struct thread * td,struct linux_ssetmask_args * args)400 linux_ssetmask(struct thread *td, struct linux_ssetmask_args *args)
401 {
402 	struct proc *p = td->td_proc;
403 	l_sigset_t lset;
404 	sigset_t bset;
405 
406 	PROC_LOCK(p);
407 	bsd_to_linux_sigset(&td->td_sigmask, &lset);
408 	td->td_retval[0] = lset.__mask;
409 	LINUX_SIGEMPTYSET(lset);
410 	lset.__mask = args->mask;
411 	linux_to_bsd_sigset(&lset, &bset);
412 #ifdef KTRACE
413 	if (KTRPOINT(td, KTR_STRUCT))
414 		linux_ktrsigset(&lset, sizeof(lset));
415 #endif
416 	td->td_sigmask = bset;
417 	SIG_CANTMASK(td->td_sigmask);
418 	signotify(td);
419 	PROC_UNLOCK(p);
420 	return (0);
421 }
422 
423 int
linux_sigpending(struct thread * td,struct linux_sigpending_args * args)424 linux_sigpending(struct thread *td, struct linux_sigpending_args *args)
425 {
426 	struct proc *p = td->td_proc;
427 	sigset_t bset;
428 	l_sigset_t lset;
429 	l_osigset_t mask;
430 
431 	PROC_LOCK(p);
432 	bset = p->p_siglist;
433 	SIGSETOR(bset, td->td_siglist);
434 	SIGSETAND(bset, td->td_sigmask);
435 	PROC_UNLOCK(p);
436 	bsd_to_linux_sigset(&bset, &lset);
437 #ifdef KTRACE
438 	if (KTRPOINT(td, KTR_STRUCT))
439 		linux_ktrsigset(&lset, sizeof(lset));
440 #endif
441 	mask = lset.__mask;
442 	return (copyout(&mask, args->mask, sizeof(mask)));
443 }
444 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
445 
446 /*
447  * MPSAFE
448  */
449 int
linux_rt_sigpending(struct thread * td,struct linux_rt_sigpending_args * args)450 linux_rt_sigpending(struct thread *td, struct linux_rt_sigpending_args *args)
451 {
452 	struct proc *p = td->td_proc;
453 	sigset_t bset;
454 	l_sigset_t lset;
455 
456 	if (args->sigsetsize > sizeof(lset))
457 		return (EINVAL);
458 		/* NOT REACHED */
459 
460 	PROC_LOCK(p);
461 	bset = p->p_siglist;
462 	SIGSETOR(bset, td->td_siglist);
463 	SIGSETAND(bset, td->td_sigmask);
464 	PROC_UNLOCK(p);
465 	bsd_to_linux_sigset(&bset, &lset);
466 #ifdef KTRACE
467 	if (KTRPOINT(td, KTR_STRUCT))
468 		linux_ktrsigset(&lset, sizeof(lset));
469 #endif
470 	return (copyout(&lset, args->set, args->sigsetsize));
471 }
472 
473 int
linux_rt_sigtimedwait(struct thread * td,struct linux_rt_sigtimedwait_args * args)474 linux_rt_sigtimedwait(struct thread *td,
475 	struct linux_rt_sigtimedwait_args *args)
476 {
477 	struct timespec ts, *tsa;
478 	int error;
479 
480 	if (args->timeout) {
481 		error = linux_get_timespec(&ts, args->timeout);
482 		if (error != 0)
483 			return (error);
484 		tsa = &ts;
485 	} else
486 		tsa = NULL;
487 
488 	return (linux_common_rt_sigtimedwait(td, args->mask, tsa,
489 	    args->ptr, args->sigsetsize));
490 }
491 
492 static int
linux_common_rt_sigtimedwait(struct thread * td,l_sigset_t * mask,struct timespec * tsa,l_siginfo_t * ptr,l_size_t sigsetsize)493 linux_common_rt_sigtimedwait(struct thread *td, l_sigset_t *mask,
494     struct timespec *tsa, l_siginfo_t *ptr, l_size_t sigsetsize)
495 {
496 	int error, sig;
497 	sigset_t bset;
498 	l_siginfo_t lsi;
499 	ksiginfo_t ksi;
500 
501 	error = linux_copyin_sigset(td, mask, sigsetsize, &bset, NULL);
502 	if (error != 0)
503 		return (error);
504 
505 	ksiginfo_init(&ksi);
506 	error = kern_sigtimedwait(td, bset, &ksi, tsa);
507 	if (error != 0)
508 		return (error);
509 
510 	sig = bsd_to_linux_signal(ksi.ksi_signo);
511 
512 	if (ptr) {
513 		memset(&lsi, 0, sizeof(lsi));
514 		siginfo_to_lsiginfo(&ksi.ksi_info, &lsi, sig);
515 		error = copyout(&lsi, ptr, sizeof(lsi));
516 	}
517 	if (error == 0)
518 		td->td_retval[0] = sig;
519 
520 	return (error);
521 }
522 
523 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
524 int
linux_rt_sigtimedwait_time64(struct thread * td,struct linux_rt_sigtimedwait_time64_args * args)525 linux_rt_sigtimedwait_time64(struct thread *td,
526 	struct linux_rt_sigtimedwait_time64_args *args)
527 {
528 	struct timespec ts, *tsa;
529 	int error;
530 
531 	if (args->timeout) {
532 		error = linux_get_timespec64(&ts, args->timeout);
533 		if (error != 0)
534 			return (error);
535 		tsa = &ts;
536 	} else
537 		tsa = NULL;
538 
539 	return (linux_common_rt_sigtimedwait(td, args->mask, tsa,
540 	    args->ptr, args->sigsetsize));
541 }
542 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
543 
544 int
linux_kill(struct thread * td,struct linux_kill_args * args)545 linux_kill(struct thread *td, struct linux_kill_args *args)
546 {
547 	int sig;
548 
549 	/*
550 	 * Allow signal 0 as a means to check for privileges
551 	 */
552 	if (!LINUX_SIG_VALID(args->signum) && args->signum != 0)
553 		return (EINVAL);
554 
555 	if (args->signum > 0)
556 		sig = linux_to_bsd_signal(args->signum);
557 	else
558 		sig = 0;
559 
560 	if (args->pid > PID_MAX)
561 		return (linux_psignal(td, args->pid, sig));
562 	else
563 		return (kern_kill(td, args->pid, sig));
564 }
565 
566 int
linux_tgkill(struct thread * td,struct linux_tgkill_args * args)567 linux_tgkill(struct thread *td, struct linux_tgkill_args *args)
568 {
569 	int sig;
570 
571 	if (args->pid <= 0 || args->tgid <=0)
572 		return (EINVAL);
573 
574 	/*
575 	 * Allow signal 0 as a means to check for privileges
576 	 */
577 	if (!LINUX_SIG_VALID(args->sig) && args->sig != 0)
578 		return (EINVAL);
579 
580 	if (args->sig > 0)
581 		sig = linux_to_bsd_signal(args->sig);
582 	else
583 		sig = 0;
584 
585 	return (linux_tdsignal(td, args->pid, args->tgid, sig));
586 }
587 
588 /*
589  * Deprecated since 2.5.75. Replaced by tgkill().
590  */
591 int
linux_tkill(struct thread * td,struct linux_tkill_args * args)592 linux_tkill(struct thread *td, struct linux_tkill_args *args)
593 {
594 	int sig;
595 
596 	if (args->tid <= 0)
597 		return (EINVAL);
598 
599 	if (!LINUX_SIG_VALID(args->sig))
600 		return (EINVAL);
601 
602 	sig = linux_to_bsd_signal(args->sig);
603 
604 	return (linux_tdsignal(td, args->tid, -1, sig));
605 }
606 
607 static int
sigfpe_sicode2lsicode(int si_code)608 sigfpe_sicode2lsicode(int si_code)
609 {
610 
611 	switch (si_code) {
612 	case FPE_INTOVF:
613 		return (LINUX_FPE_INTOVF);
614 	case FPE_INTDIV:
615 		return (LINUX_FPE_INTDIV);
616 	case FPE_FLTIDO:
617 		return (LINUX_FPE_FLTUNK);
618 	default:
619 		return (si_code);
620 	}
621 }
622 
623 static int
sigbus_sicode2lsicode(int si_code)624 sigbus_sicode2lsicode(int si_code)
625 {
626 
627 	switch (si_code) {
628 	case BUS_OOMERR:
629 		return (LINUX_BUS_MCEERR_AR);
630 	default:
631 		return (si_code);
632 	}
633 }
634 
635 static int
sigsegv_sicode2lsicode(int si_code)636 sigsegv_sicode2lsicode(int si_code)
637 {
638 
639 	switch (si_code) {
640 	case SEGV_PKUERR:
641 		return (LINUX_SEGV_PKUERR);
642 	default:
643 		return (si_code);
644 	}
645 }
646 
647 static int
sigtrap_sicode2lsicode(int si_code)648 sigtrap_sicode2lsicode(int si_code)
649 {
650 
651 	switch (si_code) {
652 	case TRAP_DTRACE:
653 		return (LINUX_TRAP_TRACE);
654 	case TRAP_CAP:
655 		return (LINUX_TRAP_UNK);
656 	default:
657 		return (si_code);
658 	}
659 }
660 
661 static void
sicode_to_lsicode(int sig,int si_code,int * lsi_code)662 sicode_to_lsicode(int sig, int si_code, int *lsi_code)
663 {
664 
665 	switch (si_code) {
666 	case SI_USER:
667 		*lsi_code = LINUX_SI_USER;
668 		break;
669 	case SI_KERNEL:
670 		*lsi_code = LINUX_SI_KERNEL;
671 		break;
672 	case SI_QUEUE:
673 		*lsi_code = LINUX_SI_QUEUE;
674 		break;
675 	case SI_TIMER:
676 		*lsi_code = LINUX_SI_TIMER;
677 		break;
678 	case SI_MESGQ:
679 		*lsi_code = LINUX_SI_MESGQ;
680 		break;
681 	case SI_ASYNCIO:
682 		*lsi_code = LINUX_SI_ASYNCIO;
683 		break;
684 	case SI_LWP:
685 		*lsi_code = LINUX_SI_TKILL;
686 		break;
687 	default:
688 		switch (sig) {
689 		case LINUX_SIGFPE:
690 			*lsi_code = sigfpe_sicode2lsicode(si_code);
691 			break;
692 		case LINUX_SIGBUS:
693 			*lsi_code = sigbus_sicode2lsicode(si_code);
694 			break;
695 		case LINUX_SIGSEGV:
696 			*lsi_code = sigsegv_sicode2lsicode(si_code);
697 			break;
698 		case LINUX_SIGTRAP:
699 			*lsi_code = sigtrap_sicode2lsicode(si_code);
700 			break;
701 		default:
702 			*lsi_code = si_code;
703 			break;
704 		}
705 		break;
706 	}
707 }
708 
709 void
siginfo_to_lsiginfo(const siginfo_t * si,l_siginfo_t * lsi,l_int sig)710 siginfo_to_lsiginfo(const siginfo_t *si, l_siginfo_t *lsi, l_int sig)
711 {
712 
713 	/* sig already converted */
714 	lsi->lsi_signo = sig;
715 	sicode_to_lsicode(sig, si->si_code, &lsi->lsi_code);
716 
717 	switch (si->si_code) {
718 	case SI_LWP:
719 		lsi->lsi_pid = si->si_pid;
720 		lsi->lsi_uid = si->si_uid;
721 		break;
722 
723 	case SI_TIMER:
724 		lsi->lsi_int = si->si_value.sival_int;
725 		lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
726 		lsi->lsi_tid = si->si_timerid;
727 		break;
728 
729 	case SI_QUEUE:
730 		lsi->lsi_pid = si->si_pid;
731 		lsi->lsi_uid = si->si_uid;
732 		lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
733 		break;
734 
735 	case SI_ASYNCIO:
736 		lsi->lsi_int = si->si_value.sival_int;
737 		lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
738 		break;
739 
740 	default:
741 		switch (sig) {
742 		case LINUX_SIGPOLL:
743 			/* XXX si_fd? */
744 			lsi->lsi_band = si->si_band;
745 			break;
746 
747 		case LINUX_SIGCHLD:
748 			lsi->lsi_errno = 0;
749 			lsi->lsi_pid = si->si_pid;
750 			lsi->lsi_uid = si->si_uid;
751 
752 			if (si->si_code == CLD_STOPPED || si->si_code == CLD_KILLED)
753 				lsi->lsi_status = bsd_to_linux_signal(si->si_status);
754 			else if (si->si_code == CLD_CONTINUED)
755 				lsi->lsi_status = bsd_to_linux_signal(SIGCONT);
756 			else
757 				lsi->lsi_status = si->si_status;
758 			break;
759 
760 		case LINUX_SIGBUS:
761 		case LINUX_SIGILL:
762 		case LINUX_SIGFPE:
763 		case LINUX_SIGSEGV:
764 			lsi->lsi_addr = PTROUT(si->si_addr);
765 			break;
766 
767 		default:
768 			lsi->lsi_pid = si->si_pid;
769 			lsi->lsi_uid = si->si_uid;
770 			if (sig >= LINUX_SIGRTMIN) {
771 				lsi->lsi_int = si->si_value.sival_int;
772 				lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
773 			}
774 			break;
775 		}
776 		break;
777 	}
778 }
779 
780 static int
lsiginfo_to_siginfo(struct thread * td,const l_siginfo_t * lsi,siginfo_t * si,int sig)781 lsiginfo_to_siginfo(struct thread *td, const l_siginfo_t *lsi,
782     siginfo_t *si, int sig)
783 {
784 
785 	switch (lsi->lsi_code) {
786 	case LINUX_SI_TKILL:
787 		if (linux_kernver(td) >= LINUX_KERNVER(2,6,39)) {
788 			linux_msg(td, "SI_TKILL forbidden since 2.6.39");
789 			return (EPERM);
790 		}
791 		si->si_code = SI_LWP;
792 	case LINUX_SI_QUEUE:
793 		si->si_code = SI_QUEUE;
794 		break;
795 	case LINUX_SI_TIMER:
796 		si->si_code = SI_TIMER;
797 		break;
798 	case LINUX_SI_MESGQ:
799 		si->si_code = SI_MESGQ;
800 		break;
801 	case LINUX_SI_ASYNCIO:
802 		si->si_code = SI_ASYNCIO;
803 		break;
804 	default:
805 		si->si_code = lsi->lsi_code;
806 		break;
807 	}
808 
809 	si->si_signo = sig;
810 	si->si_pid = td->td_proc->p_pid;
811 	si->si_uid = td->td_ucred->cr_ruid;
812 	si->si_value.sival_ptr = PTRIN(lsi->lsi_value.sival_ptr);
813 	return (0);
814 }
815 
816 int
linux_rt_sigqueueinfo(struct thread * td,struct linux_rt_sigqueueinfo_args * args)817 linux_rt_sigqueueinfo(struct thread *td, struct linux_rt_sigqueueinfo_args *args)
818 {
819 	l_siginfo_t linfo;
820 	ksiginfo_t ksi;
821 	int error;
822 	int sig;
823 
824 	if (!LINUX_SIG_VALID(args->sig))
825 		return (EINVAL);
826 
827 	error = copyin(args->info, &linfo, sizeof(linfo));
828 	if (error != 0)
829 		return (error);
830 
831 	if (linfo.lsi_code >= 0)
832 		/* SI_USER, SI_KERNEL */
833 		return (EPERM);
834 
835 	sig = linux_to_bsd_signal(args->sig);
836 	ksiginfo_init(&ksi);
837 	error = lsiginfo_to_siginfo(td, &linfo, &ksi.ksi_info, sig);
838 	if (error != 0)
839 		return (error);
840 
841 	return (linux_pksignal(td, args->pid, sig, &ksi));
842 }
843 
844 int
linux_rt_tgsigqueueinfo(struct thread * td,struct linux_rt_tgsigqueueinfo_args * args)845 linux_rt_tgsigqueueinfo(struct thread *td, struct linux_rt_tgsigqueueinfo_args *args)
846 {
847 	l_siginfo_t linfo;
848 	ksiginfo_t ksi;
849 	int error;
850 	int sig;
851 
852 	if (!LINUX_SIG_VALID(args->sig))
853 		return (EINVAL);
854 
855 	error = copyin(args->uinfo, &linfo, sizeof(linfo));
856 	if (error != 0)
857 		return (error);
858 
859 	if (linfo.lsi_code >= 0)
860 		return (EPERM);
861 
862 	sig = linux_to_bsd_signal(args->sig);
863 	ksiginfo_init(&ksi);
864 	error = lsiginfo_to_siginfo(td, &linfo, &ksi.ksi_info, sig);
865 	if (error != 0)
866 		return (error);
867 
868 	return (linux_tdksignal(td, args->tid, args->tgid, sig, &ksi));
869 }
870 
871 int
linux_rt_sigsuspend(struct thread * td,struct linux_rt_sigsuspend_args * uap)872 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
873 {
874 	sigset_t sigmask;
875 	int error;
876 
877 	error = linux_copyin_sigset(td, uap->newset, uap->sigsetsize,
878 	    &sigmask, NULL);
879 	if (error != 0)
880 		return (error);
881 
882 	return (kern_sigsuspend(td, sigmask));
883 }
884 
885 static int
linux_tdksignal(struct thread * td,lwpid_t tid,int tgid,int sig,ksiginfo_t * ksi)886 linux_tdksignal(struct thread *td, lwpid_t tid, int tgid, int sig,
887     ksiginfo_t *ksi)
888 {
889 	struct thread *tdt;
890 	struct proc *p;
891 	int error;
892 
893 	tdt = linux_tdfind(td, tid, tgid);
894 	if (tdt == NULL)
895 		return (ESRCH);
896 
897 	p = tdt->td_proc;
898 	AUDIT_ARG_SIGNUM(sig);
899 	AUDIT_ARG_PID(p->p_pid);
900 	AUDIT_ARG_PROCESS(p);
901 
902 	error = p_cansignal(td, p, sig);
903 	if (error != 0 || sig == 0)
904 		goto out;
905 
906 	tdksignal(tdt, sig, ksi);
907 
908 out:
909 	PROC_UNLOCK(p);
910 	return (error);
911 }
912 
913 static int
linux_tdsignal(struct thread * td,lwpid_t tid,int tgid,int sig)914 linux_tdsignal(struct thread *td, lwpid_t tid, int tgid, int sig)
915 {
916 	ksiginfo_t ksi;
917 
918 	ksiginfo_init(&ksi);
919 	ksi.ksi_signo = sig;
920 	ksi.ksi_code = SI_LWP;
921 	ksi.ksi_pid = td->td_proc->p_pid;
922 	ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
923 	return (linux_tdksignal(td, tid, tgid, sig, &ksi));
924 }
925 
926 static int
linux_pksignal(struct thread * td,int pid,int sig,ksiginfo_t * ksi)927 linux_pksignal(struct thread *td, int pid, int sig, ksiginfo_t *ksi)
928 {
929 	struct thread *tdt;
930 	struct proc *p;
931 	int error;
932 
933 	tdt = linux_tdfind(td, pid, -1);
934 	if (tdt == NULL)
935 		return (ESRCH);
936 
937 	p = tdt->td_proc;
938 	AUDIT_ARG_SIGNUM(sig);
939 	AUDIT_ARG_PID(p->p_pid);
940 	AUDIT_ARG_PROCESS(p);
941 
942 	error = p_cansignal(td, p, sig);
943 	if (error != 0 || sig == 0)
944 		goto out;
945 
946 	pksignal(p, sig, ksi);
947 
948 out:
949 	PROC_UNLOCK(p);
950 	return (error);
951 }
952 
953 static int
linux_psignal(struct thread * td,int pid,int sig)954 linux_psignal(struct thread *td, int pid, int sig)
955 {
956 	ksiginfo_t ksi;
957 
958 	ksiginfo_init(&ksi);
959 	ksi.ksi_signo = sig;
960 	ksi.ksi_code = SI_LWP;
961 	ksi.ksi_pid = td->td_proc->p_pid;
962 	ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
963 	return (linux_pksignal(td, pid, sig, &ksi));
964 }
965 
966 int
linux_copyin_sigset(struct thread * td,l_sigset_t * lset,l_size_t sigsetsize,sigset_t * set,sigset_t ** pset)967 linux_copyin_sigset(struct thread *td, l_sigset_t *lset,
968     l_size_t sigsetsize, sigset_t *set, sigset_t **pset)
969 {
970 	l_sigset_t lmask;
971 	int error;
972 
973 	if (sigsetsize != sizeof(l_sigset_t))
974 		return (EINVAL);
975 	if (lset != NULL) {
976 		error = copyin(lset, &lmask, sizeof(lmask));
977 		if (error != 0)
978 			return (error);
979 		linux_to_bsd_sigset(&lmask, set);
980 		if (pset != NULL)
981 			*pset = set;
982 #ifdef KTRACE
983 		if (KTRPOINT(td, KTR_STRUCT))
984 			linux_ktrsigset(&lmask, sizeof(lmask));
985 #endif
986 	} else if (pset != NULL)
987 		*pset = NULL;
988 	return (0);
989 }
990