xref: /netbsd-src/sys/compat/common/kern_sig_43.c (revision 404ee5b9334f618040b6cdef96a0ff35a6fc4636)
1 /*	$NetBSD: kern_sig_43.c,v 1.35 2019/01/27 02:08:39 pgoyette Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: kern_sig_43.c,v 1.35 2019/01/27 02:08:39 pgoyette Exp $");
34 
35 #if defined(_KERNEL_OPT)
36 #include "opt_compat_netbsd.h"
37 #endif
38 
39 #include <sys/param.h>
40 #include <sys/signalvar.h>
41 #include <sys/resourcevar.h>
42 #include <sys/namei.h>
43 #include <sys/vnode.h>
44 #include <sys/proc.h>
45 #include <sys/systm.h>
46 #include <sys/timeb.h>
47 #include <sys/times.h>
48 #include <sys/buf.h>
49 #include <sys/acct.h>
50 #include <sys/file.h>
51 #include <sys/kernel.h>
52 #include <sys/wait.h>
53 #include <sys/ktrace.h>
54 #include <sys/syslog.h>
55 #include <sys/stat.h>
56 #include <sys/core.h>
57 #include <sys/kauth.h>
58 
59 #include <sys/syscall.h>
60 #include <sys/syscallvar.h>
61 #include <sys/syscallargs.h>
62 #include <sys/compat_stub.h>
63 
64 #include <sys/cpu.h>
65 
66 #include <compat/sys/signal.h>
67 #include <compat/common/compat_mod.h>
68 
69 void compat_43_sigmask_to_sigset(const int *, sigset_t *);
70 void compat_43_sigset_to_sigmask(const sigset_t *, int *);
71 void compat_43_sigvec_to_sigaction(const struct sigvec *, struct sigaction *);
72 void compat_43_sigaction_to_sigvec(const struct sigaction *, struct sigvec *);
73 void compat_43_sigstack_to_sigaltstack(const struct sigstack *, struct sigaltstack *);
74 void compat_43_sigaltstack_to_sigstack(const struct sigaltstack *, struct sigstack *);
75 
76 static struct syscall_package kern_sig_43_syscalls[] = {
77 	{ SYS_compat_43_osigblock, 0, (sy_call_t *)compat_43_sys_sigblock },
78 	{ SYS_compat_43_osigsetmask, 0, (sy_call_t *)compat_43_sys_sigsetmask },
79 	{ SYS_compat_43_osigstack, 0, (sy_call_t *)compat_43_sys_sigstack },
80 	{ SYS_compat_43_osigvec, 0, (sy_call_t *)compat_43_sys_sigvec },
81 	{ SYS_compat_43_okillpg, 0, (sy_call_t *)compat_43_sys_killpg },
82 	{ 0, 0, NULL }
83 };
84 
85 void
86 compat_43_sigmask_to_sigset(const int *sm, sigset_t *ss)
87 {
88 
89 	ss->__bits[0] = *sm;
90 	ss->__bits[1] = 0;
91 	ss->__bits[2] = 0;
92 	ss->__bits[3] = 0;
93 }
94 
95 void
96 compat_43_sigset_to_sigmask(const sigset_t *ss, int *sm)
97 {
98 
99 	*sm = ss->__bits[0];
100 }
101 
102 void
103 compat_43_sigvec_to_sigaction(const struct sigvec *sv, struct sigaction *sa)
104 {
105 	sa->sa_handler = sv->sv_handler;
106 	compat_43_sigmask_to_sigset(&sv->sv_mask, &sa->sa_mask);
107 	sa->sa_flags = sv->sv_flags ^ SA_RESTART;
108 }
109 
110 void
111 compat_43_sigaction_to_sigvec(const struct sigaction *sa, struct sigvec *sv)
112 {
113 	sv->sv_handler = sa->sa_handler;
114 	compat_43_sigset_to_sigmask(&sa->sa_mask, &sv->sv_mask);
115 	sv->sv_flags = sa->sa_flags ^ SA_RESTART;
116 }
117 
118 void
119 compat_43_sigstack_to_sigaltstack(const struct sigstack *ss, struct sigaltstack *sa)
120 {
121 	sa->ss_sp = ss->ss_sp;
122 	sa->ss_size = SIGSTKSZ;	/* Use the recommended size */
123 	sa->ss_flags = 0;
124 	if (ss->ss_onstack)
125 		sa->ss_flags |= SS_ONSTACK;
126 }
127 
128 void
129 compat_43_sigaltstack_to_sigstack(const struct sigaltstack *sa, struct sigstack *ss)
130 {
131 	ss->ss_sp = sa->ss_sp;
132 	if (sa->ss_flags & SS_ONSTACK)
133 		ss->ss_onstack = 1;
134 	else
135 		ss->ss_onstack = 0;
136 }
137 
138 int
139 compat_43_sys_sigblock(struct lwp *l, const struct compat_43_sys_sigblock_args *uap, register_t *retval)
140 {
141 	/* {
142 		syscallarg(int) mask;
143 	} */
144 	struct proc *p = l->l_proc;
145 	int nsm, osm;
146 	sigset_t nss, oss;
147 	int error;
148 
149 	nsm = SCARG(uap, mask);
150 	compat_43_sigmask_to_sigset(&nsm, &nss);
151 	mutex_enter(p->p_lock);
152 	error = sigprocmask1(l, SIG_BLOCK, &nss, &oss);
153 	mutex_exit(p->p_lock);
154 	if (error)
155 		return (error);
156 	compat_43_sigset_to_sigmask(&oss, &osm);
157 	*retval = osm;
158 	return (0);
159 }
160 
161 int
162 compat_43_sys_sigsetmask(struct lwp *l, const struct compat_43_sys_sigsetmask_args *uap, register_t *retval)
163 {
164 	/* {
165 		syscallarg(int) mask;
166 	} */
167 	struct proc *p = l->l_proc;
168 	int nsm, osm;
169 	sigset_t nss, oss;
170 	int error;
171 
172 	nsm = SCARG(uap, mask);
173 	compat_43_sigmask_to_sigset(&nsm, &nss);
174 	mutex_enter(p->p_lock);
175 	error = sigprocmask1(l, SIG_SETMASK, &nss, &oss);
176 	mutex_exit(p->p_lock);
177 	if (error)
178 		return (error);
179 	compat_43_sigset_to_sigmask(&oss, &osm);
180 	*retval = osm;
181 	return (0);
182 }
183 
184 /* ARGSUSED */
185 int
186 compat_43_sys_sigstack(struct lwp *l, const struct compat_43_sys_sigstack_args *uap, register_t *retval)
187 {
188 	/* {
189 		syscallarg(struct sigstack *) nss;
190 		syscallarg(struct sigstack *) oss;
191 	} */
192 	struct sigstack nss, oss;
193 	struct sigaltstack nsa, osa;
194 	int error;
195 
196 	if (SCARG(uap, nss)) {
197 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
198 		if (error)
199 			return (error);
200 		compat_43_sigstack_to_sigaltstack(&nss, &nsa);
201 	}
202 	error = sigaltstack1(l,
203 	    SCARG(uap, nss) ? &nsa : 0, SCARG(uap, oss) ? &osa : 0);
204 	if (error)
205 		return (error);
206 	if (SCARG(uap, oss)) {
207 		compat_43_sigaltstack_to_sigstack(&osa, &oss);
208 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
209 		if (error)
210 			return (error);
211 	}
212 	return (0);
213 }
214 
215 /*
216  * Generalized interface signal handler, 4.3-compatible.
217  */
218 /* ARGSUSED */
219 int
220 compat_43_sys_sigvec(struct lwp *l, const struct compat_43_sys_sigvec_args *uap, register_t *retval)
221 {
222 	/* {
223 		syscallarg(int) signum;
224 		syscallarg(const struct sigvec *) nsv;
225 		syscallarg(struct sigvec *) osv;
226 	} */
227 	struct sigvec nsv, osv;
228 	struct sigaction nsa, osa;
229 	int error;
230 
231 	if (SCARG(uap, nsv)) {
232 		error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv));
233 		if (error)
234 			return (error);
235 		compat_43_sigvec_to_sigaction(&nsv, &nsa);
236 	}
237 	error = sigaction1(l, SCARG(uap, signum),
238 	    SCARG(uap, nsv) ? &nsa : 0, SCARG(uap, osv) ? &osa : 0,
239 	    NULL, 0);
240 	if (error)
241 		return (error);
242 	if (SCARG(uap, osv)) {
243 		compat_43_sigaction_to_sigvec(&osa, &osv);
244 		error = copyout(&osv, SCARG(uap, osv), sizeof(osv));
245 		if (error)
246 			return (error);
247 	}
248 	return (0);
249 }
250 
251 
252 /* ARGSUSED */
253 int
254 compat_43_sys_killpg(struct lwp *l, const struct compat_43_sys_killpg_args *uap, register_t *retval)
255 {
256 	/* {
257 		syscallarg(int) pgid;
258 		syscallarg(int) signum;
259 	} */
260 	ksiginfo_t ksi;
261 	int pgid = SCARG(uap, pgid);
262 
263 	pgid &= kern_sig_43_pgid_mask;
264 
265 	if ((u_int)SCARG(uap, signum) >= NSIG)
266 		return (EINVAL);
267 	memset(&ksi, 0, sizeof(ksi));
268 	ksi.ksi_signo = SCARG(uap, signum);
269 	ksi.ksi_code = SI_USER;
270 	ksi.ksi_pid = l->l_proc->p_pid;
271 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
272 	return killpg1(l, &ksi, pgid, 0);
273 }
274 
275 int
276 kern_sig_43_init(void)
277 {
278 
279 	return syscall_establish(NULL, kern_sig_43_syscalls);
280 }
281 
282 int
283 kern_sig_43_fini(void)
284 {
285 
286 	return syscall_disestablish(NULL, kern_sig_43_syscalls);
287 }
288