1 /* $NetBSD: sys_ptrace.c,v 1.11 2020/11/04 19:16:34 pgoyette Exp $ */ 2 3 /*- 4 * Copyright (c) 2008, 2009 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, 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 * This code is derived from software contributed to Berkeley by 42 * Jan-Simon Pendry. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. Neither the name of the University nor the names of its contributors 53 * may be used to endorse or promote products derived from this software 54 * without specific prior written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 66 * SUCH DAMAGE. 67 * 68 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93 69 */ 70 71 #include <sys/cdefs.h> 72 __KERNEL_RCSID(0, "$NetBSD: sys_ptrace.c,v 1.11 2020/11/04 19:16:34 pgoyette Exp $"); 73 74 #ifdef _KERNEL_OPT 75 #include "opt_ptrace.h" 76 #endif 77 78 #include <sys/param.h> 79 #include <sys/systm.h> 80 #include <sys/proc.h> 81 #include <sys/errno.h> 82 #include <sys/exec.h> 83 #include <sys/pax.h> 84 #include <sys/ptrace.h> 85 #include <sys/uio.h> 86 #include <sys/ras.h> 87 #include <sys/kmem.h> 88 #include <sys/kauth.h> 89 #include <sys/mount.h> 90 #include <sys/syscallargs.h> 91 #include <sys/syscallvar.h> 92 #include <sys/syscall.h> 93 #include <sys/module.h> 94 95 #include <uvm/uvm_extern.h> 96 97 #include <machine/reg.h> 98 99 /* 100 * PTRACE methods 101 */ 102 103 static int 104 ptrace_copyin_piod(struct ptrace_io_desc *piod, const void *addr, size_t len) 105 { 106 if (len != 0 && sizeof(*piod) != len) 107 return EINVAL; 108 109 return copyin(addr, piod, sizeof(*piod)); 110 } 111 112 static int 113 ptrace_copyout_piod(const struct ptrace_io_desc *piod, void *addr, size_t len) 114 { 115 if (len != 0 && sizeof(*piod) != len) 116 return EINVAL; 117 118 return copyout(piod, addr, sizeof(*piod)); 119 } 120 121 static int 122 ptrace_copyin_siginfo(struct ptrace_siginfo *psi, const void *addr, size_t len) 123 { 124 if (sizeof(*psi) != len) 125 return EINVAL; 126 127 return copyin(addr, psi, sizeof(*psi)); 128 } 129 130 static int 131 ptrace_copyout_siginfo(const struct ptrace_siginfo *psi, void *addr, size_t len) 132 { 133 if (sizeof(*psi) != len) 134 return EINVAL; 135 136 return copyout(psi, addr, sizeof(*psi)); 137 } 138 139 static int 140 ptrace_copyout_lwpstatus(const struct ptrace_lwpstatus *pls, void *addr, 141 size_t len) 142 { 143 144 return copyout(pls, addr, len); 145 } 146 147 static struct ptrace_methods native_ptm = { 148 .ptm_copyin_piod = ptrace_copyin_piod, 149 .ptm_copyout_piod = ptrace_copyout_piod, 150 .ptm_copyin_siginfo = ptrace_copyin_siginfo, 151 .ptm_copyout_siginfo = ptrace_copyout_siginfo, 152 .ptm_copyout_lwpstatus = ptrace_copyout_lwpstatus, 153 .ptm_doregs = process_doregs, 154 .ptm_dofpregs = process_dofpregs, 155 .ptm_dodbregs = process_dodbregs, 156 }; 157 158 static const struct syscall_package ptrace_syscalls[] = { 159 { SYS_ptrace, 0, (sy_call_t *)sys_ptrace }, 160 { 0, 0, NULL }, 161 }; 162 163 /* 164 * Process debugging system call. 165 */ 166 int 167 sys_ptrace(struct lwp *l, const struct sys_ptrace_args *uap, register_t *retval) 168 { 169 /* { 170 syscallarg(int) req; 171 syscallarg(pid_t) pid; 172 syscallarg(void *) addr; 173 syscallarg(int) data; 174 } */ 175 176 return do_ptrace(&native_ptm, l, SCARG(uap, req), SCARG(uap, pid), 177 SCARG(uap, addr), SCARG(uap, data), retval); 178 } 179 180 #define DEPS "ptrace_common" 181 182 MODULE(MODULE_CLASS_EXEC, ptrace, DEPS); 183 184 static int 185 ptrace_init(void) 186 { 187 int error; 188 189 error = syscall_establish(&emul_netbsd, ptrace_syscalls); 190 return error; 191 } 192 193 static int 194 ptrace_fini(void) 195 { 196 int error; 197 198 error = syscall_disestablish(&emul_netbsd, ptrace_syscalls); 199 return error; 200 } 201 202 203 static int 204 ptrace_modcmd(modcmd_t cmd, void *arg) 205 { 206 int error; 207 208 switch (cmd) { 209 case MODULE_CMD_INIT: 210 error = ptrace_init(); 211 break; 212 case MODULE_CMD_FINI: 213 error = ptrace_fini(); 214 break; 215 default: 216 error = ENOTTY; 217 break; 218 } 219 return error; 220 } 221