1 /* $NetBSD: kern_sig_16.c,v 1.3 2019/01/27 02:08:39 pgoyette 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.3 2019/01/27 02:08:39 pgoyette Exp $"); 70 71 #if defined(_KERNEL_OPT) 72 #include "opt_compat_netbsd.h" 73 #endif 74 75 #include <sys/param.h> 76 #include <sys/kernel.h> 77 #include <sys/rwlock.h> 78 #include <sys/signalvar.h> 79 #include <sys/proc.h> 80 #include <sys/pool.h> 81 #include <sys/syscall.h> 82 #include <sys/syscallvar.h> 83 #include <sys/syscallargs.h> 84 #include <sys/kauth.h> 85 #include <sys/wait.h> 86 #include <sys/kmem.h> 87 88 #include <uvm/uvm_object.h> 89 #include <uvm/uvm_prot.h> 90 #include <uvm/uvm_pager.h> 91 92 #include <compat/common/compat_mod.h> 93 94 extern krwlock_t exec_lock; 95 96 #if !defined(__amd64__) || defined(COMPAT_NETBSD32) 97 #define COMPAT_SIGCONTEXT 98 extern char sigcode[], esigcode[]; 99 struct uvm_object *emul_netbsd_object; 100 #endif 101 102 static const struct syscall_package kern_sig_16_syscalls[] = { 103 #ifdef COMPAT_SIGCONTEXT 104 { SYS_compat_16___sigaction14, 0, 105 (sy_call_t *)compat_16_sys___sigaction14 }, 106 /* compat_16_sigreturn14 is in MD code! */ 107 { SYS_compat_16___sigreturn14, 0, 108 (sy_call_t *)compat_16_sys___sigreturn14 }, 109 #endif 110 { 0, 0, NULL } 111 }; 112 113 int 114 compat_16_sys___sigaction14(struct lwp *l, 115 const struct compat_16_sys___sigaction14_args *uap, register_t *retval) 116 { 117 /* { 118 syscallarg(int) signum; 119 syscallarg(const struct sigaction *) nsa; 120 syscallarg(struct sigaction *) osa; 121 } */ 122 struct sigaction nsa, osa; 123 int error; 124 125 if (SCARG(uap, nsa)) { 126 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa)); 127 if (error) 128 return (error); 129 } 130 error = sigaction1(l, SCARG(uap, signum), 131 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0, 132 NULL, 0); 133 if (error) 134 return (error); 135 if (SCARG(uap, osa)) { 136 error = copyout(&osa, SCARG(uap, osa), sizeof(osa)); 137 if (error) 138 return (error); 139 } 140 return (0); 141 } 142 143 int 144 kern_sig_16_init(void) 145 { 146 int error; 147 148 error = syscall_establish(NULL, kern_sig_16_syscalls); 149 if (error) 150 return error; 151 #if defined(COMPAT_SIGCONTEXT) 152 KASSERT(emul_netbsd.e_sigobject == NULL); 153 rw_enter(&exec_lock, RW_WRITER); 154 emul_netbsd.e_sigcode = sigcode; 155 emul_netbsd.e_esigcode = esigcode; 156 emul_netbsd.e_sigobject = &emul_netbsd_object; 157 rw_exit(&exec_lock); 158 KASSERT(sendsig_sigcontext_vec == NULL); 159 sendsig_sigcontext_vec = sendsig_sigcontext; 160 #endif 161 162 return 0; 163 } 164 165 int 166 kern_sig_16_fini(void) 167 { 168 proc_t *p; 169 int error; 170 171 error = syscall_disestablish(NULL, kern_sig_16_syscalls); 172 if (error) 173 return error; 174 /* 175 * Ensure sendsig_sigcontext() is not being used. 176 * module_lock prevents the flag being set on any 177 * further processes while we are here. See 178 * sigaction1() for the opposing half. 179 */ 180 mutex_enter(proc_lock); 181 PROCLIST_FOREACH(p, &allproc) { 182 if ((p->p_lflag & PL_SIGCOMPAT) != 0) { 183 break; 184 } 185 } 186 mutex_exit(proc_lock); 187 if (p != NULL) { 188 syscall_establish(NULL, kern_sig_16_syscalls); 189 return EBUSY; 190 } 191 sendsig_sigcontext_vec = NULL; 192 193 #if defined(COMPAT_SIGCONTEXT) 194 /* 195 * The sigobject may persist if still in use, but 196 * is reference counted so will die eventually. 197 */ 198 rw_enter(&exec_lock, RW_WRITER); 199 if (emul_netbsd_object != NULL) { 200 (*emul_netbsd_object->pgops->pgo_detach)(emul_netbsd_object); 201 } 202 emul_netbsd_object = NULL; 203 emul_netbsd.e_sigcode = NULL; 204 emul_netbsd.e_esigcode = NULL; 205 emul_netbsd.e_sigobject = NULL; 206 rw_exit(&exec_lock); 207 #endif 208 return 0; 209 } 210 211