2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 1994-1995 Søren Schmidt
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include "opt_ktrace.h"
31 #include <sys/param.h>
34 #include <sys/mutex.h>
36 #include <sys/signalvar.h>
38 #include <sys/syscallsubr.h>
39 #include <sys/sysproto.h>
41 #include <sys/ktrace.h>
44 #include <security/audit/audit.h>
47 #include <machine/../linux32/linux.h>
48 #include <machine/../linux32/linux32_proto.h>
50 #include <machine/../linux/linux.h>
51 #include <machine/../linux/linux_proto.h>
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>
60 static int linux_pksignal(struct thread *td, int pid, int sig,
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,
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 *,
73 linux_to_bsd_sigaction(l_sigaction_t *lsa, struct sigaction *bsa)
77 linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
78 bsa->sa_handler = PTRIN(lsa->lsa_handler);
81 flags = lsa->lsa_flags;
82 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP) {
83 flags &= ~LINUX_SA_NOCLDSTOP;
84 bsa->sa_flags |= SA_NOCLDSTOP;
86 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT) {
87 flags &= ~LINUX_SA_NOCLDWAIT;
88 bsa->sa_flags |= SA_NOCLDWAIT;
90 if (lsa->lsa_flags & LINUX_SA_SIGINFO) {
91 flags &= ~LINUX_SA_SIGINFO;
92 bsa->sa_flags |= SA_SIGINFO;
95 * XXX: We seem to be missing code to convert
96 * some of the fields in ucontext_t.
99 "partially unsupported sigaction flag SA_SIGINFO");
102 if (lsa->lsa_flags & LINUX_SA_RESTORER) {
103 flags &= ~LINUX_SA_RESTORER;
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).
111 if (lsa->lsa_flags & LINUX_SA_ONSTACK) {
112 flags &= ~LINUX_SA_ONSTACK;
113 bsa->sa_flags |= SA_ONSTACK;
115 if (lsa->lsa_flags & LINUX_SA_RESTART) {
116 flags &= ~LINUX_SA_RESTART;
117 bsa->sa_flags |= SA_RESTART;
119 if (lsa->lsa_flags & LINUX_SA_INTERRUPT) {
120 flags &= ~LINUX_SA_INTERRUPT;
121 /* Documented to be a "historical no-op". */
123 if (lsa->lsa_flags & LINUX_SA_ONESHOT) {
124 flags &= ~LINUX_SA_ONESHOT;
125 bsa->sa_flags |= SA_RESETHAND;
127 if (lsa->lsa_flags & LINUX_SA_NOMASK) {
128 flags &= ~LINUX_SA_NOMASK;
129 bsa->sa_flags |= SA_NODEFER;
133 linux_msg(curthread, "unsupported sigaction flag %#lx", flags);
137 bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa)
140 bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
141 #ifdef COMPAT_LINUX32
142 lsa->lsa_handler = (uintptr_t)bsa->sa_handler;
144 lsa->lsa_handler = bsa->sa_handler;
146 lsa->lsa_restorer = 0; /* unsupported */
148 if (bsa->sa_flags & SA_NOCLDSTOP)
149 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
150 if (bsa->sa_flags & SA_NOCLDWAIT)
151 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
152 if (bsa->sa_flags & SA_SIGINFO)
153 lsa->lsa_flags |= LINUX_SA_SIGINFO;
154 if (bsa->sa_flags & SA_ONSTACK)
155 lsa->lsa_flags |= LINUX_SA_ONSTACK;
156 if (bsa->sa_flags & SA_RESTART)
157 lsa->lsa_flags |= LINUX_SA_RESTART;
158 if (bsa->sa_flags & SA_RESETHAND)
159 lsa->lsa_flags |= LINUX_SA_ONESHOT;
160 if (bsa->sa_flags & SA_NODEFER)
161 lsa->lsa_flags |= LINUX_SA_NOMASK;
165 linux_do_sigaction(struct thread *td, int linux_sig, l_sigaction_t *linux_nsa,
166 l_sigaction_t *linux_osa)
168 struct sigaction act, oact, *nsa, *osa;
171 if (!LINUX_SIG_VALID(linux_sig))
173 sig = linux_to_bsd_signal(linux_sig);
175 osa = (linux_osa != NULL) ? &oact : NULL;
176 if (linux_nsa != NULL) {
178 linux_to_bsd_sigaction(linux_nsa, nsa);
180 if (KTRPOINT(td, KTR_STRUCT))
181 linux_ktrsigset(&linux_nsa->lsa_mask,
182 sizeof(linux_nsa->lsa_mask));
184 if ((sig == SIGKILL || sig == SIGSTOP) &&
185 nsa->sa_handler == SIG_DFL)
190 error = kern_sigaction(td, sig, nsa, osa, 0);
194 if (linux_osa != NULL) {
195 bsd_to_linux_sigaction(osa, linux_osa);
197 if (KTRPOINT(td, KTR_STRUCT))
198 linux_ktrsigset(&linux_osa->lsa_mask,
199 sizeof(linux_osa->lsa_mask));
206 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
212 memset(&lss, 0, sizeof(lss));
213 LINUX_CTR2(sigaltstack, "%p, %p", uap->uss, uap->uoss);
215 if (uap->uss != NULL) {
216 error = copyin(uap->uss, &lss, sizeof(lss));
220 ss.ss_sp = PTRIN(lss.ss_sp);
221 ss.ss_size = lss.ss_size;
222 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
224 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
225 (uap->uoss != NULL) ? &oss : NULL);
226 if (error == 0 && uap->uoss != NULL) {
227 lss.ss_sp = PTROUT(oss.ss_sp);
228 lss.ss_size = oss.ss_size;
229 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
230 error = copyout(&lss, uap->uoss, sizeof(lss));
236 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
238 linux_signal(struct thread *td, struct linux_signal_args *args)
240 l_sigaction_t nsa, osa;
243 nsa.lsa_handler = args->handler;
244 nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
245 LINUX_SIGEMPTYSET(nsa.lsa_mask);
247 error = linux_do_sigaction(td, args->sig, &nsa, &osa);
248 td->td_retval[0] = (int)(intptr_t)osa.lsa_handler;
252 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
255 linux_rt_sigaction(struct thread *td, struct linux_rt_sigaction_args *args)
257 l_sigaction_t nsa, osa;
260 if (args->sigsetsize != sizeof(l_sigset_t))
263 if (args->act != NULL) {
264 error = copyin(args->act, &nsa, sizeof(nsa));
269 error = linux_do_sigaction(td, args->sig,
270 args->act ? &nsa : NULL,
271 args->oact ? &osa : NULL);
273 if (args->oact != NULL && error == 0)
274 error = copyout(&osa, args->oact, sizeof(osa));
280 linux_do_sigprocmask(struct thread *td, int how, sigset_t *new,
286 td->td_retval[0] = 0;
289 case LINUX_SIG_BLOCK:
292 case LINUX_SIG_UNBLOCK:
295 case LINUX_SIG_SETMASK:
301 error = kern_sigprocmask(td, how, new, &omask, 0);
302 if (error == 0 && old != NULL)
303 bsd_to_linux_sigset(&omask, old);
308 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
310 linux_sigprocmask(struct thread *td, struct linux_sigprocmask_args *args)
313 l_sigset_t lset, oset;
317 if (args->mask != NULL) {
318 error = copyin(args->mask, &mask, sizeof(mask));
321 LINUX_SIGEMPTYSET(lset);
324 if (KTRPOINT(td, KTR_STRUCT))
325 linux_ktrsigset(&lset, sizeof(lset));
327 linux_to_bsd_sigset(&lset, &set);
330 error = linux_do_sigprocmask(td, args->how,
331 args->mask ? &set : NULL,
332 args->omask ? &oset : NULL);
334 if (args->omask != NULL && error == 0) {
336 if (KTRPOINT(td, KTR_STRUCT))
337 linux_ktrsigset(&oset, sizeof(oset));
340 error = copyout(&mask, args->omask, sizeof(mask));
345 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
348 linux_rt_sigprocmask(struct thread *td, struct linux_rt_sigprocmask_args *args)
354 error = linux_copyin_sigset(td, args->mask, args->sigsetsize,
359 error = linux_do_sigprocmask(td, args->how, pset,
360 args->omask ? &oset : NULL);
362 if (args->omask != NULL && error == 0) {
364 if (KTRPOINT(td, KTR_STRUCT))
365 linux_ktrsigset(&oset, sizeof(oset));
367 error = copyout(&oset, args->omask, sizeof(oset));
373 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
375 linux_sgetmask(struct thread *td, struct linux_sgetmask_args *args)
377 struct proc *p = td->td_proc;
381 bsd_to_linux_sigset(&td->td_sigmask, &mask);
383 td->td_retval[0] = mask.__mask;
385 if (KTRPOINT(td, KTR_STRUCT))
386 linux_ktrsigset(&mask, sizeof(mask));
392 linux_ssetmask(struct thread *td, struct linux_ssetmask_args *args)
394 struct proc *p = td->td_proc;
399 bsd_to_linux_sigset(&td->td_sigmask, &lset);
400 td->td_retval[0] = lset.__mask;
401 LINUX_SIGEMPTYSET(lset);
402 lset.__mask = args->mask;
403 linux_to_bsd_sigset(&lset, &bset);
405 if (KTRPOINT(td, KTR_STRUCT))
406 linux_ktrsigset(&lset, sizeof(lset));
408 td->td_sigmask = bset;
409 SIG_CANTMASK(td->td_sigmask);
416 linux_sigpending(struct thread *td, struct linux_sigpending_args *args)
418 struct proc *p = td->td_proc;
425 SIGSETOR(bset, td->td_siglist);
426 SIGSETAND(bset, td->td_sigmask);
428 bsd_to_linux_sigset(&bset, &lset);
430 if (KTRPOINT(td, KTR_STRUCT))
431 linux_ktrsigset(&lset, sizeof(lset));
434 return (copyout(&mask, args->mask, sizeof(mask)));
436 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
442 linux_rt_sigpending(struct thread *td, struct linux_rt_sigpending_args *args)
444 struct proc *p = td->td_proc;
448 if (args->sigsetsize > sizeof(lset))
454 SIGSETOR(bset, td->td_siglist);
455 SIGSETAND(bset, td->td_sigmask);
457 bsd_to_linux_sigset(&bset, &lset);
459 if (KTRPOINT(td, KTR_STRUCT))
460 linux_ktrsigset(&lset, sizeof(lset));
462 return (copyout(&lset, args->set, args->sigsetsize));
466 linux_rt_sigtimedwait(struct thread *td,
467 struct linux_rt_sigtimedwait_args *args)
469 struct timespec ts, *tsa;
473 error = linux_get_timespec(&ts, args->timeout);
480 return (linux_common_rt_sigtimedwait(td, args->mask, tsa,
481 args->ptr, args->sigsetsize));
485 linux_common_rt_sigtimedwait(struct thread *td, l_sigset_t *mask,
486 struct timespec *tsa, l_siginfo_t *ptr, l_size_t sigsetsize)
493 error = linux_copyin_sigset(td, mask, sigsetsize, &bset, NULL);
498 error = kern_sigtimedwait(td, bset, &ksi, tsa);
502 sig = bsd_to_linux_signal(ksi.ksi_signo);
505 memset(&lsi, 0, sizeof(lsi));
506 siginfo_to_lsiginfo(&ksi.ksi_info, &lsi, sig);
507 error = copyout(&lsi, ptr, sizeof(lsi));
510 td->td_retval[0] = sig;
515 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
517 linux_rt_sigtimedwait_time64(struct thread *td,
518 struct linux_rt_sigtimedwait_time64_args *args)
520 struct timespec ts, *tsa;
524 error = linux_get_timespec64(&ts, args->timeout);
531 return (linux_common_rt_sigtimedwait(td, args->mask, tsa,
532 args->ptr, args->sigsetsize));
534 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
537 linux_kill(struct thread *td, struct linux_kill_args *args)
542 * Allow signal 0 as a means to check for privileges
544 if (!LINUX_SIG_VALID(args->signum) && args->signum != 0)
547 if (args->signum > 0)
548 sig = linux_to_bsd_signal(args->signum);
552 if (args->pid > PID_MAX)
553 return (linux_psignal(td, args->pid, sig));
555 return (kern_kill(td, args->pid, sig));
559 linux_tgkill(struct thread *td, struct linux_tgkill_args *args)
563 if (args->pid <= 0 || args->tgid <=0)
567 * Allow signal 0 as a means to check for privileges
569 if (!LINUX_SIG_VALID(args->sig) && args->sig != 0)
573 sig = linux_to_bsd_signal(args->sig);
577 return (linux_tdsignal(td, args->pid, args->tgid, sig));
581 * Deprecated since 2.5.75. Replaced by tgkill().
584 linux_tkill(struct thread *td, struct linux_tkill_args *args)
591 if (!LINUX_SIG_VALID(args->sig))
594 sig = linux_to_bsd_signal(args->sig);
596 return (linux_tdsignal(td, args->tid, -1, sig));
600 sigfpe_sicode2lsicode(int si_code)
605 return (LINUX_FPE_INTOVF);
607 return (LINUX_FPE_INTDIV);
609 return (LINUX_FPE_FLTUNK);
616 sigbus_sicode2lsicode(int si_code)
621 return (LINUX_BUS_MCEERR_AR);
628 sigsegv_sicode2lsicode(int si_code)
633 return (LINUX_SEGV_PKUERR);
640 sigtrap_sicode2lsicode(int si_code)
645 return (LINUX_TRAP_TRACE);
647 return (LINUX_TRAP_UNK);
654 sicode_to_lsicode(int sig, int si_code, int *lsi_code)
659 *lsi_code = LINUX_SI_USER;
662 *lsi_code = LINUX_SI_KERNEL;
665 *lsi_code = LINUX_SI_QUEUE;
668 *lsi_code = LINUX_SI_TIMER;
671 *lsi_code = LINUX_SI_MESGQ;
674 *lsi_code = LINUX_SI_ASYNCIO;
677 *lsi_code = LINUX_SI_TKILL;
682 *lsi_code = sigfpe_sicode2lsicode(si_code);
685 *lsi_code = sigbus_sicode2lsicode(si_code);
688 *lsi_code = sigsegv_sicode2lsicode(si_code);
691 *lsi_code = sigtrap_sicode2lsicode(si_code);
702 siginfo_to_lsiginfo(const siginfo_t *si, l_siginfo_t *lsi, l_int sig)
705 /* sig already converted */
706 lsi->lsi_signo = sig;
707 sicode_to_lsicode(sig, si->si_code, &lsi->lsi_code);
709 switch (si->si_code) {
711 lsi->lsi_pid = si->si_pid;
712 lsi->lsi_uid = si->si_uid;
716 lsi->lsi_int = si->si_value.sival_int;
717 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
718 lsi->lsi_tid = si->si_timerid;
722 lsi->lsi_pid = si->si_pid;
723 lsi->lsi_uid = si->si_uid;
724 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
728 lsi->lsi_int = si->si_value.sival_int;
729 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
736 lsi->lsi_band = si->si_band;
741 lsi->lsi_pid = si->si_pid;
742 lsi->lsi_uid = si->si_uid;
744 if (si->si_code == CLD_STOPPED || si->si_code == CLD_KILLED)
745 lsi->lsi_status = bsd_to_linux_signal(si->si_status);
746 else if (si->si_code == CLD_CONTINUED)
747 lsi->lsi_status = bsd_to_linux_signal(SIGCONT);
749 lsi->lsi_status = si->si_status;
756 lsi->lsi_addr = PTROUT(si->si_addr);
760 lsi->lsi_pid = si->si_pid;
761 lsi->lsi_uid = si->si_uid;
762 if (sig >= LINUX_SIGRTMIN) {
763 lsi->lsi_int = si->si_value.sival_int;
764 lsi->lsi_ptr = PTROUT(si->si_value.sival_ptr);
773 lsiginfo_to_siginfo(struct thread *td, const l_siginfo_t *lsi,
774 siginfo_t *si, int sig)
777 switch (lsi->lsi_code) {
779 if (linux_kernver(td) >= LINUX_KERNVER(2,6,39)) {
780 linux_msg(td, "SI_TKILL forbidden since 2.6.39");
783 si->si_code = SI_LWP;
785 si->si_code = SI_QUEUE;
788 si->si_code = SI_TIMER;
791 si->si_code = SI_MESGQ;
793 case LINUX_SI_ASYNCIO:
794 si->si_code = SI_ASYNCIO;
797 si->si_code = lsi->lsi_code;
802 si->si_pid = td->td_proc->p_pid;
803 si->si_uid = td->td_ucred->cr_ruid;
804 si->si_value.sival_ptr = PTRIN(lsi->lsi_value.sival_ptr);
809 linux_rt_sigqueueinfo(struct thread *td, struct linux_rt_sigqueueinfo_args *args)
816 if (!LINUX_SIG_VALID(args->sig))
819 error = copyin(args->info, &linfo, sizeof(linfo));
823 if (linfo.lsi_code >= 0)
824 /* SI_USER, SI_KERNEL */
827 sig = linux_to_bsd_signal(args->sig);
829 error = lsiginfo_to_siginfo(td, &linfo, &ksi.ksi_info, sig);
833 return (linux_pksignal(td, args->pid, sig, &ksi));
837 linux_rt_tgsigqueueinfo(struct thread *td, struct linux_rt_tgsigqueueinfo_args *args)
844 if (!LINUX_SIG_VALID(args->sig))
847 error = copyin(args->uinfo, &linfo, sizeof(linfo));
851 if (linfo.lsi_code >= 0)
854 sig = linux_to_bsd_signal(args->sig);
856 error = lsiginfo_to_siginfo(td, &linfo, &ksi.ksi_info, sig);
860 return (linux_tdksignal(td, args->tid, args->tgid, sig, &ksi));
864 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
869 error = linux_copyin_sigset(td, uap->newset, uap->sigsetsize,
874 return (kern_sigsuspend(td, sigmask));
878 linux_tdksignal(struct thread *td, lwpid_t tid, int tgid, int sig,
885 tdt = linux_tdfind(td, tid, tgid);
890 AUDIT_ARG_SIGNUM(sig);
891 AUDIT_ARG_PID(p->p_pid);
892 AUDIT_ARG_PROCESS(p);
894 error = p_cansignal(td, p, sig);
895 if (error != 0 || sig == 0)
898 tdksignal(tdt, sig, ksi);
906 linux_tdsignal(struct thread *td, lwpid_t tid, int tgid, int sig)
912 ksi.ksi_code = SI_LWP;
913 ksi.ksi_pid = td->td_proc->p_pid;
914 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
915 return (linux_tdksignal(td, tid, tgid, sig, &ksi));
919 linux_pksignal(struct thread *td, int pid, int sig, ksiginfo_t *ksi)
925 tdt = linux_tdfind(td, pid, -1);
930 AUDIT_ARG_SIGNUM(sig);
931 AUDIT_ARG_PID(p->p_pid);
932 AUDIT_ARG_PROCESS(p);
934 error = p_cansignal(td, p, sig);
935 if (error != 0 || sig == 0)
938 pksignal(p, sig, ksi);
946 linux_psignal(struct thread *td, int pid, int sig)
952 ksi.ksi_code = SI_LWP;
953 ksi.ksi_pid = td->td_proc->p_pid;
954 ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid;
955 return (linux_pksignal(td, pid, sig, &ksi));
959 linux_copyin_sigset(struct thread *td, l_sigset_t *lset,
960 l_size_t sigsetsize, sigset_t *set, sigset_t **pset)
965 if (sigsetsize != sizeof(l_sigset_t))
968 error = copyin(lset, &lmask, sizeof(lmask));
971 linux_to_bsd_sigset(&lmask, set);
975 if (KTRPOINT(td, KTR_STRUCT))
976 linux_ktrsigset(&lmask, sizeof(lmask));
978 } else if (pset != NULL)