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