xref: /netbsd-src/sys/compat/common/kern_sig_16.c (revision 455d1eeffa0e876de95715b695ba88571afaaf4e)
1*455d1eefSmlelstv /*	$NetBSD: kern_sig_16.c,v 1.10 2024/12/10 10:52:05 mlelstv Exp $	*/
257de28faSad 
357de28faSad /*-
457de28faSad  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
557de28faSad  * All rights reserved.
657de28faSad  *
757de28faSad  * This code is derived from software contributed to The NetBSD Foundation
857de28faSad  * by Andrew Doran.
957de28faSad  *
1057de28faSad  * Redistribution and use in source and binary forms, with or without
1157de28faSad  * modification, are permitted provided that the following conditions
1257de28faSad  * are met:
1357de28faSad  * 1. Redistributions of source code must retain the above copyright
1457de28faSad  *    notice, this list of conditions and the following disclaimer.
1557de28faSad  * 2. Redistributions in binary form must reproduce the above copyright
1657de28faSad  *    notice, this list of conditions and the following disclaimer in the
1757de28faSad  *    documentation and/or other materials provided with the distribution.
1857de28faSad  *
1957de28faSad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2057de28faSad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2157de28faSad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2257de28faSad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2357de28faSad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2457de28faSad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2557de28faSad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2657de28faSad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2757de28faSad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2857de28faSad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2957de28faSad  * POSSIBILITY OF SUCH DAMAGE.
3057de28faSad  */
3157de28faSad 
3257de28faSad /*
3357de28faSad  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3457de28faSad  *	The Regents of the University of California.  All rights reserved.
3557de28faSad  * (c) UNIX System Laboratories, Inc.
3657de28faSad  * All or some portions of this file are derived from material licensed
3757de28faSad  * to the University of California by American Telephone and Telegraph
3857de28faSad  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
3957de28faSad  * the permission of UNIX System Laboratories, Inc.
4057de28faSad  *
4157de28faSad  * Redistribution and use in source and binary forms, with or without
4257de28faSad  * modification, are permitted provided that the following conditions
4357de28faSad  * are met:
4457de28faSad  * 1. Redistributions of source code must retain the above copyright
4557de28faSad  *    notice, this list of conditions and the following disclaimer.
4657de28faSad  * 2. Redistributions in binary form must reproduce the above copyright
4757de28faSad  *    notice, this list of conditions and the following disclaimer in the
4857de28faSad  *    documentation and/or other materials provided with the distribution.
4957de28faSad  * 3. Neither the name of the University nor the names of its contributors
5057de28faSad  *    may be used to endorse or promote products derived from this software
5157de28faSad  *    without specific prior written permission.
5257de28faSad  *
5357de28faSad  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5457de28faSad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5557de28faSad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5657de28faSad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5757de28faSad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5857de28faSad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5957de28faSad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6057de28faSad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6157de28faSad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6257de28faSad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6357de28faSad  * SUCH DAMAGE.
6457de28faSad  *
6557de28faSad  *	@(#)kern_sig.c	8.14 (Berkeley) 5/14/95
6657de28faSad  */
6757de28faSad 
6857de28faSad #include <sys/cdefs.h>
69*455d1eefSmlelstv __KERNEL_RCSID(0, "$NetBSD: kern_sig_16.c,v 1.10 2024/12/10 10:52:05 mlelstv Exp $");
70d91f98a8Spgoyette 
71d91f98a8Spgoyette #if defined(_KERNEL_OPT)
72d91f98a8Spgoyette #include "opt_compat_netbsd.h"
73d91f98a8Spgoyette #endif
7457de28faSad 
7557de28faSad #include <sys/param.h>
76d9c43f29Sryo #include <sys/exec.h>
7757de28faSad #include <sys/kernel.h>
78d91f98a8Spgoyette #include <sys/rwlock.h>
7957de28faSad #include <sys/signalvar.h>
8057de28faSad #include <sys/proc.h>
8157de28faSad #include <sys/pool.h>
82d91f98a8Spgoyette #include <sys/syscall.h>
83d91f98a8Spgoyette #include <sys/syscallvar.h>
8457de28faSad #include <sys/syscallargs.h>
8557de28faSad #include <sys/kauth.h>
8657de28faSad #include <sys/wait.h>
8757de28faSad #include <sys/kmem.h>
887b0f5c9eSpgoyette #include <sys/compat_stub.h>
8957de28faSad 
90d91f98a8Spgoyette #include <uvm/uvm_object.h>
91d91f98a8Spgoyette #include <uvm/uvm_prot.h>
92d91f98a8Spgoyette #include <uvm/uvm_pager.h>
93d91f98a8Spgoyette 
94d91f98a8Spgoyette #include <compat/common/compat_mod.h>
95d91f98a8Spgoyette 
9657440f7bSmlelstv #ifdef COMPAT_NETBSD32
9757440f7bSmlelstv # ifndef __aarch64__
98d91f98a8Spgoyette #  define COMPAT_SIGCONTEXT
9957440f7bSmlelstv # endif
10057440f7bSmlelstv #else /* COMPAT_NETBSD32 */
101*455d1eefSmlelstv # if !defined(__amd64__) && !defined(__aarch64__)
10257440f7bSmlelstv #  define COMPAT_SIGCONTEXT
10357440f7bSmlelstv # endif
10457440f7bSmlelstv #endif /* COMPAT_NETBSD32 */
10557440f7bSmlelstv 
10657440f7bSmlelstv #ifdef COMPAT_SIGCONTEXT
107d91f98a8Spgoyette extern char sigcode[], esigcode[];
108d91f98a8Spgoyette struct uvm_object *emul_netbsd_object;
109d91f98a8Spgoyette #endif
110d91f98a8Spgoyette 
111d91f98a8Spgoyette static const struct syscall_package kern_sig_16_syscalls[] = {
112d91f98a8Spgoyette #ifdef COMPAT_SIGCONTEXT
113d91f98a8Spgoyette 	{ SYS_compat_16___sigaction14, 0,
114d91f98a8Spgoyette 	    (sy_call_t *)compat_16_sys___sigaction14 },
115d91f98a8Spgoyette 	/* compat_16_sigreturn14 is in MD code! */
116d91f98a8Spgoyette 	{ SYS_compat_16___sigreturn14, 0,
117d91f98a8Spgoyette 	    (sy_call_t *)compat_16_sys___sigreturn14 },
118d91f98a8Spgoyette #endif
119d91f98a8Spgoyette 	{ 0, 0, NULL }
120d91f98a8Spgoyette };
121d91f98a8Spgoyette 
12257de28faSad int
123ad12c770Srmind compat_16_sys___sigaction14(struct lwp *l,
124ad12c770Srmind     const struct compat_16_sys___sigaction14_args *uap, register_t *retval)
12557de28faSad {
12657de28faSad 	/* {
12757de28faSad 		syscallarg(int)				signum;
12857de28faSad 		syscallarg(const struct sigaction *)	nsa;
12957de28faSad 		syscallarg(struct sigaction *)		osa;
13057de28faSad 	} */
13157de28faSad 	struct sigaction	nsa, osa;
13257de28faSad 	int			error;
13357de28faSad 
13457de28faSad 	if (SCARG(uap, nsa)) {
13557de28faSad 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
13657de28faSad 		if (error)
13757de28faSad 			return (error);
13857de28faSad 	}
13957de28faSad 	error = sigaction1(l, SCARG(uap, signum),
14057de28faSad 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
14157de28faSad 	    NULL, 0);
14257de28faSad 	if (error)
14357de28faSad 		return (error);
14457de28faSad 	if (SCARG(uap, osa)) {
14557de28faSad 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
14657de28faSad 		if (error)
14757de28faSad 			return (error);
14857de28faSad 	}
14957de28faSad 	return (0);
15057de28faSad }
151d91f98a8Spgoyette 
152d91f98a8Spgoyette int
153d91f98a8Spgoyette kern_sig_16_init(void)
154d91f98a8Spgoyette {
155d91f98a8Spgoyette 	int error;
156d91f98a8Spgoyette 
157d91f98a8Spgoyette 	error = syscall_establish(NULL, kern_sig_16_syscalls);
158d91f98a8Spgoyette 	if (error)
159d91f98a8Spgoyette 		return error;
160d91f98a8Spgoyette #if defined(COMPAT_SIGCONTEXT)
161d91f98a8Spgoyette 	KASSERT(emul_netbsd.e_sigobject == NULL);
162d91f98a8Spgoyette 	rw_enter(&exec_lock, RW_WRITER);
163d91f98a8Spgoyette 	emul_netbsd.e_sigcode = sigcode;
164d91f98a8Spgoyette 	emul_netbsd.e_esigcode = esigcode;
165d91f98a8Spgoyette 	emul_netbsd.e_sigobject = &emul_netbsd_object;
166d9c43f29Sryo 	error = exec_sigcode_alloc(&emul_netbsd);
167d9c43f29Sryo 	if (error) {
168d9c43f29Sryo 		emul_netbsd.e_sigcode = NULL;
169d9c43f29Sryo 		emul_netbsd.e_esigcode = NULL;
170d9c43f29Sryo 		emul_netbsd.e_sigobject = NULL;
171d9c43f29Sryo 	}
172d91f98a8Spgoyette 	rw_exit(&exec_lock);
173d4593f40Sryo 	if (error)
174d4593f40Sryo 		return error;
1758a031a1dSpgoyette 	MODULE_HOOK_SET(sendsig_sigcontext_16_hook, sendsig_sigcontext);
176d91f98a8Spgoyette #endif
177d91f98a8Spgoyette 
178d4593f40Sryo 	return 0;
179d91f98a8Spgoyette }
180d91f98a8Spgoyette 
181d91f98a8Spgoyette int
182d91f98a8Spgoyette kern_sig_16_fini(void)
183d91f98a8Spgoyette {
184d91f98a8Spgoyette 	proc_t *p;
185d91f98a8Spgoyette 	int error;
186d91f98a8Spgoyette 
187d91f98a8Spgoyette 	error = syscall_disestablish(NULL, kern_sig_16_syscalls);
188d91f98a8Spgoyette 	if (error)
189d91f98a8Spgoyette 		return error;
190d91f98a8Spgoyette 	/*
191d91f98a8Spgoyette 	 * Ensure sendsig_sigcontext() is not being used.
192d91f98a8Spgoyette 	 * module_lock prevents the flag being set on any
193d91f98a8Spgoyette 	 * further processes while we are here.  See
194d91f98a8Spgoyette 	 * sigaction1() for the opposing half.
195d91f98a8Spgoyette 	 */
1960eaaa024Sad 	mutex_enter(&proc_lock);
197d91f98a8Spgoyette 	PROCLIST_FOREACH(p, &allproc) {
198d91f98a8Spgoyette 		if ((p->p_lflag & PL_SIGCOMPAT) != 0) {
199d91f98a8Spgoyette 			break;
200d91f98a8Spgoyette 		}
201d91f98a8Spgoyette 	}
2020eaaa024Sad 	mutex_exit(&proc_lock);
203d91f98a8Spgoyette 	if (p != NULL) {
204d91f98a8Spgoyette 		syscall_establish(NULL, kern_sig_16_syscalls);
205d91f98a8Spgoyette 		return EBUSY;
206d91f98a8Spgoyette 	}
207d91f98a8Spgoyette 
208d91f98a8Spgoyette #if defined(COMPAT_SIGCONTEXT)
209d91f98a8Spgoyette 	/*
210d91f98a8Spgoyette 	 * The sigobject may persist if still in use, but
211d91f98a8Spgoyette 	 * is reference counted so will die eventually.
212d91f98a8Spgoyette 	 */
213d91f98a8Spgoyette 	rw_enter(&exec_lock, RW_WRITER);
214d9c43f29Sryo 	exec_sigcode_free(&emul_netbsd);
215d91f98a8Spgoyette 	emul_netbsd_object = NULL;
216d91f98a8Spgoyette 	emul_netbsd.e_sigcode = NULL;
217d91f98a8Spgoyette 	emul_netbsd.e_esigcode = NULL;
218d91f98a8Spgoyette 	emul_netbsd.e_sigobject = NULL;
219d91f98a8Spgoyette 	rw_exit(&exec_lock);
2207b0f5c9eSpgoyette 
2217b0f5c9eSpgoyette 	MODULE_HOOK_UNSET(sendsig_sigcontext_16_hook);
222d91f98a8Spgoyette #endif
223d91f98a8Spgoyette 	return 0;
224d91f98a8Spgoyette }
225d91f98a8Spgoyette 
226