xref: /netbsd-src/sys/compat/common/kern_sig_16.c (revision 455d1eeffa0e876de95715b695ba88571afaaf4e)
1 /*	$NetBSD: kern_sig_16.c,v 1.10 2024/12/10 10:52:05 mlelstv Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
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 /*
33  * Copyright (c) 1982, 1986, 1989, 1991, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  * (c) UNIX System Laboratories, Inc.
36  * All or some portions of this file are derived from material licensed
37  * to the University of California by American Telephone and Telegraph
38  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39  * the permission of UNIX System Laboratories, Inc.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	@(#)kern_sig.c	8.14 (Berkeley) 5/14/95
66  */
67 
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: kern_sig_16.c,v 1.10 2024/12/10 10:52:05 mlelstv Exp $");
70 
71 #if defined(_KERNEL_OPT)
72 #include "opt_compat_netbsd.h"
73 #endif
74 
75 #include <sys/param.h>
76 #include <sys/exec.h>
77 #include <sys/kernel.h>
78 #include <sys/rwlock.h>
79 #include <sys/signalvar.h>
80 #include <sys/proc.h>
81 #include <sys/pool.h>
82 #include <sys/syscall.h>
83 #include <sys/syscallvar.h>
84 #include <sys/syscallargs.h>
85 #include <sys/kauth.h>
86 #include <sys/wait.h>
87 #include <sys/kmem.h>
88 #include <sys/compat_stub.h>
89 
90 #include <uvm/uvm_object.h>
91 #include <uvm/uvm_prot.h>
92 #include <uvm/uvm_pager.h>
93 
94 #include <compat/common/compat_mod.h>
95 
96 #ifdef COMPAT_NETBSD32
97 # ifndef __aarch64__
98 #  define COMPAT_SIGCONTEXT
99 # endif
100 #else /* COMPAT_NETBSD32 */
101 # if !defined(__amd64__) && !defined(__aarch64__)
102 #  define COMPAT_SIGCONTEXT
103 # endif
104 #endif /* COMPAT_NETBSD32 */
105 
106 #ifdef COMPAT_SIGCONTEXT
107 extern char sigcode[], esigcode[];
108 struct uvm_object *emul_netbsd_object;
109 #endif
110 
111 static const struct syscall_package kern_sig_16_syscalls[] = {
112 #ifdef COMPAT_SIGCONTEXT
113 	{ SYS_compat_16___sigaction14, 0,
114 	    (sy_call_t *)compat_16_sys___sigaction14 },
115 	/* compat_16_sigreturn14 is in MD code! */
116 	{ SYS_compat_16___sigreturn14, 0,
117 	    (sy_call_t *)compat_16_sys___sigreturn14 },
118 #endif
119 	{ 0, 0, NULL }
120 };
121 
122 int
123 compat_16_sys___sigaction14(struct lwp *l,
124     const struct compat_16_sys___sigaction14_args *uap, register_t *retval)
125 {
126 	/* {
127 		syscallarg(int)				signum;
128 		syscallarg(const struct sigaction *)	nsa;
129 		syscallarg(struct sigaction *)		osa;
130 	} */
131 	struct sigaction	nsa, osa;
132 	int			error;
133 
134 	if (SCARG(uap, nsa)) {
135 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
136 		if (error)
137 			return (error);
138 	}
139 	error = sigaction1(l, SCARG(uap, signum),
140 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
141 	    NULL, 0);
142 	if (error)
143 		return (error);
144 	if (SCARG(uap, osa)) {
145 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
146 		if (error)
147 			return (error);
148 	}
149 	return (0);
150 }
151 
152 int
153 kern_sig_16_init(void)
154 {
155 	int error;
156 
157 	error = syscall_establish(NULL, kern_sig_16_syscalls);
158 	if (error)
159 		return error;
160 #if defined(COMPAT_SIGCONTEXT)
161 	KASSERT(emul_netbsd.e_sigobject == NULL);
162 	rw_enter(&exec_lock, RW_WRITER);
163 	emul_netbsd.e_sigcode = sigcode;
164 	emul_netbsd.e_esigcode = esigcode;
165 	emul_netbsd.e_sigobject = &emul_netbsd_object;
166 	error = exec_sigcode_alloc(&emul_netbsd);
167 	if (error) {
168 		emul_netbsd.e_sigcode = NULL;
169 		emul_netbsd.e_esigcode = NULL;
170 		emul_netbsd.e_sigobject = NULL;
171 	}
172 	rw_exit(&exec_lock);
173 	if (error)
174 		return error;
175 	MODULE_HOOK_SET(sendsig_sigcontext_16_hook, sendsig_sigcontext);
176 #endif
177 
178 	return 0;
179 }
180 
181 int
182 kern_sig_16_fini(void)
183 {
184 	proc_t *p;
185 	int error;
186 
187 	error = syscall_disestablish(NULL, kern_sig_16_syscalls);
188 	if (error)
189 		return error;
190 	/*
191 	 * Ensure sendsig_sigcontext() is not being used.
192 	 * module_lock prevents the flag being set on any
193 	 * further processes while we are here.  See
194 	 * sigaction1() for the opposing half.
195 	 */
196 	mutex_enter(&proc_lock);
197 	PROCLIST_FOREACH(p, &allproc) {
198 		if ((p->p_lflag & PL_SIGCOMPAT) != 0) {
199 			break;
200 		}
201 	}
202 	mutex_exit(&proc_lock);
203 	if (p != NULL) {
204 		syscall_establish(NULL, kern_sig_16_syscalls);
205 		return EBUSY;
206 	}
207 
208 #if defined(COMPAT_SIGCONTEXT)
209 	/*
210 	 * The sigobject may persist if still in use, but
211 	 * is reference counted so will die eventually.
212 	 */
213 	rw_enter(&exec_lock, RW_WRITER);
214 	exec_sigcode_free(&emul_netbsd);
215 	emul_netbsd_object = NULL;
216 	emul_netbsd.e_sigcode = NULL;
217 	emul_netbsd.e_esigcode = NULL;
218 	emul_netbsd.e_sigobject = NULL;
219 	rw_exit(&exec_lock);
220 
221 	MODULE_HOOK_UNSET(sendsig_sigcontext_16_hook);
222 #endif
223 	return 0;
224 }
225 
226