sys_ptrace_common.c revision 1.22.2.6 1 1.22.2.6 martin /* $NetBSD: sys_ptrace_common.c,v 1.22.2.6 2019/08/04 11:05:29 martin Exp $ */
2 1.1 pgoyette
3 1.1 pgoyette /*-
4 1.1 pgoyette * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 1.1 pgoyette * All rights reserved.
6 1.1 pgoyette *
7 1.1 pgoyette * This code is derived from software contributed to The NetBSD Foundation
8 1.1 pgoyette * by Andrew Doran.
9 1.1 pgoyette *
10 1.1 pgoyette * Redistribution and use in source and binary forms, with or without
11 1.1 pgoyette * modification, are permitted provided that the following conditions
12 1.1 pgoyette * are met:
13 1.1 pgoyette * 1. Redistributions of source code must retain the above copyright
14 1.1 pgoyette * notice, this list of conditions and the following disclaimer.
15 1.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 pgoyette * notice, this list of conditions and the following disclaimer in the
17 1.1 pgoyette * documentation and/or other materials provided with the distribution.
18 1.1 pgoyette *
19 1.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 pgoyette * POSSIBILITY OF SUCH DAMAGE.
30 1.1 pgoyette */
31 1.1 pgoyette
32 1.1 pgoyette /*-
33 1.1 pgoyette * Copyright (c) 1982, 1986, 1989, 1993
34 1.1 pgoyette * The Regents of the University of California. All rights reserved.
35 1.1 pgoyette * (c) UNIX System Laboratories, Inc.
36 1.1 pgoyette * All or some portions of this file are derived from material licensed
37 1.1 pgoyette * to the University of California by American Telephone and Telegraph
38 1.1 pgoyette * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39 1.1 pgoyette * the permission of UNIX System Laboratories, Inc.
40 1.1 pgoyette *
41 1.1 pgoyette * This code is derived from software contributed to Berkeley by
42 1.1 pgoyette * Jan-Simon Pendry.
43 1.1 pgoyette *
44 1.1 pgoyette * Redistribution and use in source and binary forms, with or without
45 1.1 pgoyette * modification, are permitted provided that the following conditions
46 1.1 pgoyette * are met:
47 1.1 pgoyette * 1. Redistributions of source code must retain the above copyright
48 1.1 pgoyette * notice, this list of conditions and the following disclaimer.
49 1.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
50 1.1 pgoyette * notice, this list of conditions and the following disclaimer in the
51 1.1 pgoyette * documentation and/or other materials provided with the distribution.
52 1.1 pgoyette * 3. Neither the name of the University nor the names of its contributors
53 1.1 pgoyette * may be used to endorse or promote products derived from this software
54 1.1 pgoyette * without specific prior written permission.
55 1.1 pgoyette *
56 1.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 1.1 pgoyette * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 1.1 pgoyette * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 1.1 pgoyette * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 1.1 pgoyette * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 1.1 pgoyette * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 1.1 pgoyette * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 1.1 pgoyette * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 1.1 pgoyette * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 1.1 pgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 1.1 pgoyette * SUCH DAMAGE.
67 1.1 pgoyette *
68 1.1 pgoyette * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
69 1.1 pgoyette */
70 1.1 pgoyette
71 1.1 pgoyette /*-
72 1.1 pgoyette * Copyright (c) 1993 Jan-Simon Pendry.
73 1.1 pgoyette * Copyright (c) 1994 Christopher G. Demetriou. All rights reserved.
74 1.1 pgoyette *
75 1.1 pgoyette * This code is derived from software contributed to Berkeley by
76 1.1 pgoyette * Jan-Simon Pendry.
77 1.1 pgoyette *
78 1.1 pgoyette * Redistribution and use in source and binary forms, with or without
79 1.1 pgoyette * modification, are permitted provided that the following conditions
80 1.1 pgoyette * are met:
81 1.1 pgoyette * 1. Redistributions of source code must retain the above copyright
82 1.1 pgoyette * notice, this list of conditions and the following disclaimer.
83 1.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
84 1.1 pgoyette * notice, this list of conditions and the following disclaimer in the
85 1.1 pgoyette * documentation and/or other materials provided with the distribution.
86 1.1 pgoyette * 3. All advertising materials mentioning features or use of this software
87 1.1 pgoyette * must display the following acknowledgement:
88 1.1 pgoyette * This product includes software developed by the University of
89 1.1 pgoyette * California, Berkeley and its contributors.
90 1.1 pgoyette * 4. Neither the name of the University nor the names of its contributors
91 1.1 pgoyette * may be used to endorse or promote products derived from this software
92 1.1 pgoyette * without specific prior written permission.
93 1.1 pgoyette *
94 1.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
95 1.1 pgoyette * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96 1.1 pgoyette * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
97 1.1 pgoyette * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
98 1.1 pgoyette * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
99 1.1 pgoyette * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
100 1.1 pgoyette * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
101 1.1 pgoyette * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
102 1.1 pgoyette * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
103 1.1 pgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
104 1.1 pgoyette * SUCH DAMAGE.
105 1.1 pgoyette *
106 1.1 pgoyette * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
107 1.1 pgoyette */
108 1.1 pgoyette
109 1.1 pgoyette /*
110 1.1 pgoyette * References:
111 1.1 pgoyette * (1) Bach's "The Design of the UNIX Operating System",
112 1.1 pgoyette * (2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution,
113 1.1 pgoyette * (3) the "4.4BSD Programmer's Reference Manual" published
114 1.1 pgoyette * by USENIX and O'Reilly & Associates.
115 1.1 pgoyette * The 4.4BSD PRM does a reasonably good job of documenting what the various
116 1.1 pgoyette * ptrace() requests should actually do, and its text is quoted several times
117 1.1 pgoyette * in this file.
118 1.1 pgoyette */
119 1.1 pgoyette
120 1.1 pgoyette #include <sys/cdefs.h>
121 1.22.2.6 martin __KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.22.2.6 2019/08/04 11:05:29 martin Exp $");
122 1.1 pgoyette
123 1.1 pgoyette #ifdef _KERNEL_OPT
124 1.1 pgoyette #include "opt_ptrace.h"
125 1.1 pgoyette #include "opt_ktrace.h"
126 1.1 pgoyette #include "opt_pax.h"
127 1.22.2.2 martin #include "opt_compat_netbsd32.h"
128 1.22.2.2 martin #endif
129 1.22.2.2 martin
130 1.22.2.2 martin #if defined(__HAVE_COMPAT_NETBSD32) && !defined(COMPAT_NETBSD32) \
131 1.22.2.2 martin && !defined(_RUMPKERNEL)
132 1.22.2.2 martin #define COMPAT_NETBSD32
133 1.1 pgoyette #endif
134 1.1 pgoyette
135 1.1 pgoyette #include <sys/param.h>
136 1.1 pgoyette #include <sys/systm.h>
137 1.1 pgoyette #include <sys/proc.h>
138 1.1 pgoyette #include <sys/errno.h>
139 1.1 pgoyette #include <sys/exec.h>
140 1.1 pgoyette #include <sys/pax.h>
141 1.1 pgoyette #include <sys/ptrace.h>
142 1.1 pgoyette #include <sys/uio.h>
143 1.1 pgoyette #include <sys/ras.h>
144 1.1 pgoyette #include <sys/kmem.h>
145 1.1 pgoyette #include <sys/kauth.h>
146 1.1 pgoyette #include <sys/mount.h>
147 1.1 pgoyette #include <sys/syscallargs.h>
148 1.1 pgoyette #include <sys/module.h>
149 1.1 pgoyette #include <sys/condvar.h>
150 1.1 pgoyette #include <sys/mutex.h>
151 1.1 pgoyette
152 1.1 pgoyette #include <uvm/uvm_extern.h>
153 1.1 pgoyette
154 1.1 pgoyette #include <machine/reg.h>
155 1.1 pgoyette
156 1.1 pgoyette #ifdef PTRACE
157 1.1 pgoyette
158 1.1 pgoyette # ifdef DEBUG
159 1.1 pgoyette # define DPRINTF(a) uprintf a
160 1.1 pgoyette # else
161 1.1 pgoyette # define DPRINTF(a)
162 1.1 pgoyette # endif
163 1.1 pgoyette
164 1.1 pgoyette static kauth_listener_t ptrace_listener;
165 1.1 pgoyette static int process_auxv_offset(struct proc *, struct uio *);
166 1.1 pgoyette
167 1.1 pgoyette #if 0
168 1.1 pgoyette static int ptrace_cbref;
169 1.1 pgoyette static kmutex_t ptrace_mtx;
170 1.1 pgoyette static kcondvar_t ptrace_cv;
171 1.1 pgoyette #endif
172 1.1 pgoyette
173 1.22.2.2 martin #ifdef PT_GETREGS
174 1.22.2.2 martin # define case_PT_GETREGS case PT_GETREGS:
175 1.22.2.2 martin #else
176 1.22.2.2 martin # define case_PT_GETREGS
177 1.22.2.2 martin #endif
178 1.22.2.2 martin
179 1.22.2.2 martin #ifdef PT_SETREGS
180 1.22.2.2 martin # define case_PT_SETREGS case PT_SETREGS:
181 1.22.2.2 martin #else
182 1.22.2.2 martin # define case_PT_SETREGS
183 1.22.2.2 martin #endif
184 1.22.2.2 martin
185 1.22.2.2 martin #ifdef PT_GETFPREGS
186 1.22.2.2 martin # define case_PT_GETFPREGS case PT_GETFPREGS:
187 1.22.2.2 martin #else
188 1.22.2.2 martin # define case_PT_GETFPREGS
189 1.22.2.2 martin #endif
190 1.22.2.2 martin
191 1.22.2.2 martin #ifdef PT_SETFPREGS
192 1.22.2.2 martin # define case_PT_SETFPREGS case PT_SETFPREGS:
193 1.22.2.2 martin #else
194 1.22.2.2 martin # define case_PT_SETFPREGS
195 1.22.2.2 martin #endif
196 1.22.2.2 martin
197 1.22.2.2 martin #ifdef PT_GETDBREGS
198 1.22.2.2 martin # define case_PT_GETDBREGS case PT_GETDBREGS:
199 1.22.2.2 martin #else
200 1.22.2.2 martin # define case_PT_GETDBREGS
201 1.22.2.2 martin #endif
202 1.22.2.2 martin
203 1.22.2.2 martin #ifdef PT_SETDBREGS
204 1.22.2.2 martin # define case_PT_SETDBREGS case PT_SETDBREGS:
205 1.22.2.2 martin #else
206 1.22.2.2 martin # define case_PT_SETDBREGS
207 1.22.2.2 martin #endif
208 1.22.2.2 martin
209 1.22.2.2 martin #if defined(PT_SETREGS) || defined(PT_GETREGS) || \
210 1.22.2.2 martin defined(PT_SETFPREGS) || defined(PT_GETFOREGS) || \
211 1.22.2.2 martin defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
212 1.22.2.2 martin # define PT_REGISTERS
213 1.22.2.2 martin #endif
214 1.22.2.2 martin
215 1.1 pgoyette static int
216 1.1 pgoyette ptrace_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
217 1.1 pgoyette void *arg0, void *arg1, void *arg2, void *arg3)
218 1.1 pgoyette {
219 1.1 pgoyette struct proc *p;
220 1.1 pgoyette int result;
221 1.22.2.2 martin #ifdef PT_SETDBREGS
222 1.22.2.2 martin extern int user_set_dbregs;
223 1.22.2.2 martin #endif
224 1.1 pgoyette
225 1.1 pgoyette result = KAUTH_RESULT_DEFER;
226 1.1 pgoyette p = arg0;
227 1.1 pgoyette
228 1.1 pgoyette #if 0
229 1.1 pgoyette mutex_enter(&ptrace_mtx);
230 1.1 pgoyette ptrace_cbref++;
231 1.1 pgoyette mutex_exit(&ptrace_mtx);
232 1.1 pgoyette #endif
233 1.1 pgoyette if (action != KAUTH_PROCESS_PTRACE)
234 1.1 pgoyette goto out;
235 1.1 pgoyette
236 1.1 pgoyette switch ((u_long)arg1) {
237 1.22.2.2 martin #ifdef PT_SETDBREGS
238 1.22.2.2 martin case_PT_SETDBREGS
239 1.22.2.2 martin if (kauth_cred_getuid(cred) != 0 && user_set_dbregs == 0) {
240 1.22.2.2 martin result = KAUTH_RESULT_DENY;
241 1.22.2.2 martin break;
242 1.22.2.2 martin }
243 1.22.2.2 martin #endif
244 1.1 pgoyette case PT_TRACE_ME:
245 1.1 pgoyette case PT_ATTACH:
246 1.1 pgoyette case PT_WRITE_I:
247 1.1 pgoyette case PT_WRITE_D:
248 1.1 pgoyette case PT_READ_I:
249 1.1 pgoyette case PT_READ_D:
250 1.1 pgoyette case PT_IO:
251 1.22.2.2 martin case_PT_GETREGS
252 1.22.2.2 martin case_PT_SETREGS
253 1.22.2.2 martin case_PT_GETFPREGS
254 1.22.2.2 martin case_PT_SETFPREGS
255 1.22.2.2 martin case_PT_GETDBREGS
256 1.1 pgoyette case PT_SET_EVENT_MASK:
257 1.1 pgoyette case PT_GET_EVENT_MASK:
258 1.1 pgoyette case PT_GET_PROCESS_STATE:
259 1.8 kamil case PT_SET_SIGINFO:
260 1.8 kamil case PT_GET_SIGINFO:
261 1.1 pgoyette #ifdef __HAVE_PTRACE_MACHDEP
262 1.1 pgoyette PTRACE_MACHDEP_REQUEST_CASES
263 1.1 pgoyette #endif
264 1.1 pgoyette if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) ||
265 1.1 pgoyette ISSET(p->p_flag, PK_SUGID)) {
266 1.1 pgoyette break;
267 1.1 pgoyette }
268 1.1 pgoyette
269 1.1 pgoyette result = KAUTH_RESULT_ALLOW;
270 1.1 pgoyette
271 1.1 pgoyette break;
272 1.1 pgoyette
273 1.1 pgoyette #ifdef PT_STEP
274 1.1 pgoyette case PT_STEP:
275 1.21 kamil case PT_SETSTEP:
276 1.21 kamil case PT_CLEARSTEP:
277 1.1 pgoyette #endif
278 1.1 pgoyette case PT_CONTINUE:
279 1.1 pgoyette case PT_KILL:
280 1.1 pgoyette case PT_DETACH:
281 1.1 pgoyette case PT_LWPINFO:
282 1.1 pgoyette case PT_SYSCALL:
283 1.1 pgoyette case PT_SYSCALLEMU:
284 1.1 pgoyette case PT_DUMPCORE:
285 1.15 kamil case PT_RESUME:
286 1.15 kamil case PT_SUSPEND:
287 1.1 pgoyette result = KAUTH_RESULT_ALLOW;
288 1.1 pgoyette break;
289 1.1 pgoyette
290 1.1 pgoyette default:
291 1.1 pgoyette break;
292 1.1 pgoyette }
293 1.1 pgoyette
294 1.1 pgoyette out:
295 1.1 pgoyette #if 0
296 1.1 pgoyette mutex_enter(&ptrace_mtx);
297 1.1 pgoyette if (--ptrace_cbref == 0)
298 1.1 pgoyette cv_broadcast(&ptrace_cv);
299 1.1 pgoyette mutex_exit(&ptrace_mtx);
300 1.1 pgoyette #endif
301 1.1 pgoyette
302 1.1 pgoyette return result;
303 1.1 pgoyette }
304 1.1 pgoyette
305 1.1 pgoyette int
306 1.1 pgoyette ptrace_init(void)
307 1.1 pgoyette {
308 1.1 pgoyette
309 1.1 pgoyette #if 0
310 1.1 pgoyette mutex_init(&ptrace_mtx, MUTEX_DEFAULT, IPL_NONE);
311 1.1 pgoyette cv_init(&ptrace_cv, "ptracecb");
312 1.1 pgoyette ptrace_cbref = 0;
313 1.1 pgoyette #endif
314 1.1 pgoyette ptrace_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
315 1.1 pgoyette ptrace_listener_cb, NULL);
316 1.1 pgoyette return 0;
317 1.1 pgoyette }
318 1.1 pgoyette
319 1.1 pgoyette int
320 1.1 pgoyette ptrace_fini(void)
321 1.1 pgoyette {
322 1.1 pgoyette
323 1.1 pgoyette kauth_unlisten_scope(ptrace_listener);
324 1.1 pgoyette
325 1.1 pgoyette #if 0
326 1.1 pgoyette /* Make sure no-one is executing our kauth listener */
327 1.1 pgoyette
328 1.1 pgoyette mutex_enter(&ptrace_mtx);
329 1.1 pgoyette while (ptrace_cbref != 0)
330 1.1 pgoyette cv_wait(&ptrace_cv, &ptrace_mtx);
331 1.1 pgoyette mutex_exit(&ptrace_mtx);
332 1.1 pgoyette mutex_destroy(&ptrace_mtx);
333 1.1 pgoyette cv_destroy(&ptrace_cv);
334 1.1 pgoyette #endif
335 1.1 pgoyette
336 1.1 pgoyette return 0;
337 1.1 pgoyette }
338 1.1 pgoyette
339 1.22.2.2 martin static struct proc *
340 1.22.2.2 martin ptrace_find(struct lwp *l, int req, pid_t pid)
341 1.1 pgoyette {
342 1.22.2.2 martin struct proc *t;
343 1.1 pgoyette
344 1.1 pgoyette /* "A foolish consistency..." XXX */
345 1.1 pgoyette if (req == PT_TRACE_ME) {
346 1.22.2.2 martin t = l->l_proc;
347 1.1 pgoyette mutex_enter(t->p_lock);
348 1.22.2.2 martin return t;
349 1.22.2.2 martin }
350 1.1 pgoyette
351 1.22.2.2 martin /* Find the process we're supposed to be operating on. */
352 1.22.2.2 martin t = proc_find(pid);
353 1.22.2.2 martin if (t == NULL)
354 1.22.2.2 martin return NULL;
355 1.22.2.2 martin
356 1.22.2.2 martin /* XXX-elad */
357 1.22.2.2 martin mutex_enter(t->p_lock);
358 1.22.2.2 martin int error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
359 1.22.2.2 martin t, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
360 1.22.2.2 martin if (error) {
361 1.22.2.2 martin mutex_exit(t->p_lock);
362 1.22.2.2 martin return NULL;
363 1.1 pgoyette }
364 1.22.2.2 martin return t;
365 1.22.2.2 martin }
366 1.1 pgoyette
367 1.22.2.2 martin static int
368 1.22.2.6 martin ptrace_allowed(struct lwp *l, int req, struct proc *t, struct proc *p,
369 1.22.2.6 martin bool *locked)
370 1.22.2.2 martin {
371 1.22.2.6 martin *locked = false;
372 1.22.2.6 martin
373 1.1 pgoyette /*
374 1.1 pgoyette * Grab a reference on the process to prevent it from execing or
375 1.1 pgoyette * exiting.
376 1.1 pgoyette */
377 1.22.2.2 martin if (!rw_tryenter(&t->p_reflock, RW_READER))
378 1.1 pgoyette return EBUSY;
379 1.1 pgoyette
380 1.22.2.6 martin *locked = true;
381 1.22.2.6 martin
382 1.1 pgoyette /* Make sure we can operate on it. */
383 1.1 pgoyette switch (req) {
384 1.22.2.2 martin case PT_TRACE_ME:
385 1.22.2.3 martin /*
386 1.22.2.3 martin * You can't say to the parent of a process to start tracing if:
387 1.22.2.3 martin * (1) the parent is initproc,
388 1.22.2.3 martin */
389 1.22.2.3 martin if (p->p_pptr == initproc)
390 1.22.2.3 martin return EPERM;
391 1.22.2.3 martin
392 1.22.2.3 martin /*
393 1.22.2.3 martin * (2) the child is already traced.
394 1.22.2.3 martin */
395 1.22.2.3 martin if (ISSET(p->p_slflag, PSL_TRACED))
396 1.22.2.3 martin return EBUSY;
397 1.22.2.3 martin
398 1.22.2.2 martin return 0;
399 1.1 pgoyette
400 1.22.2.2 martin case PT_ATTACH:
401 1.1 pgoyette /*
402 1.1 pgoyette * You can't attach to a process if:
403 1.1 pgoyette * (1) it's the process that's doing the attaching,
404 1.1 pgoyette */
405 1.22.2.2 martin if (t->p_pid == p->p_pid)
406 1.22.2.2 martin return EINVAL;
407 1.1 pgoyette
408 1.1 pgoyette /*
409 1.22.2.3 martin * (2) it's a system process
410 1.1 pgoyette */
411 1.22.2.2 martin if (t->p_flag & PK_SYSTEM)
412 1.22.2.2 martin return EPERM;
413 1.1 pgoyette
414 1.1 pgoyette /*
415 1.1 pgoyette * (3) it's already being traced, or
416 1.1 pgoyette */
417 1.22.2.2 martin if (ISSET(t->p_slflag, PSL_TRACED))
418 1.22.2.2 martin return EBUSY;
419 1.1 pgoyette
420 1.1 pgoyette /*
421 1.1 pgoyette * (4) the tracer is chrooted, and its root directory is
422 1.1 pgoyette * not at or above the root directory of the tracee
423 1.1 pgoyette */
424 1.1 pgoyette mutex_exit(t->p_lock); /* XXXSMP */
425 1.22.2.2 martin int tmp = proc_isunder(t, l);
426 1.1 pgoyette mutex_enter(t->p_lock); /* XXXSMP */
427 1.22.2.2 martin if (!tmp)
428 1.22.2.2 martin return EPERM;
429 1.22.2.2 martin return 0;
430 1.1 pgoyette
431 1.22.2.2 martin case PT_READ_I:
432 1.22.2.2 martin case PT_READ_D:
433 1.22.2.2 martin case PT_WRITE_I:
434 1.22.2.2 martin case PT_WRITE_D:
435 1.22.2.2 martin case PT_IO:
436 1.22.2.2 martin case PT_SET_SIGINFO:
437 1.22.2.2 martin case PT_GET_SIGINFO:
438 1.22.2.2 martin case_PT_GETREGS
439 1.22.2.2 martin case_PT_SETREGS
440 1.22.2.2 martin case_PT_GETFPREGS
441 1.22.2.2 martin case_PT_SETFPREGS
442 1.22.2.2 martin case_PT_GETDBREGS
443 1.22.2.2 martin case_PT_SETDBREGS
444 1.1 pgoyette #ifdef __HAVE_PTRACE_MACHDEP
445 1.1 pgoyette PTRACE_MACHDEP_REQUEST_CASES
446 1.1 pgoyette #endif
447 1.1 pgoyette /*
448 1.1 pgoyette * You can't read/write the memory or registers of a process
449 1.1 pgoyette * if the tracer is chrooted, and its root directory is not at
450 1.1 pgoyette * or above the root directory of the tracee.
451 1.1 pgoyette */
452 1.1 pgoyette mutex_exit(t->p_lock); /* XXXSMP */
453 1.1 pgoyette tmp = proc_isunder(t, l);
454 1.1 pgoyette mutex_enter(t->p_lock); /* XXXSMP */
455 1.22.2.2 martin if (!tmp)
456 1.22.2.2 martin return EPERM;
457 1.1 pgoyette /*FALLTHROUGH*/
458 1.1 pgoyette
459 1.22.2.2 martin case PT_CONTINUE:
460 1.22.2.2 martin case PT_KILL:
461 1.22.2.2 martin case PT_DETACH:
462 1.22.2.2 martin case PT_LWPINFO:
463 1.22.2.2 martin case PT_SYSCALL:
464 1.22.2.2 martin case PT_SYSCALLEMU:
465 1.22.2.2 martin case PT_DUMPCORE:
466 1.1 pgoyette #ifdef PT_STEP
467 1.22.2.2 martin case PT_STEP:
468 1.22.2.2 martin case PT_SETSTEP:
469 1.22.2.2 martin case PT_CLEARSTEP:
470 1.22.2.2 martin #endif
471 1.22.2.2 martin case PT_SET_EVENT_MASK:
472 1.22.2.2 martin case PT_GET_EVENT_MASK:
473 1.22.2.2 martin case PT_GET_PROCESS_STATE:
474 1.22.2.2 martin case PT_RESUME:
475 1.22.2.2 martin case PT_SUSPEND:
476 1.1 pgoyette /*
477 1.1 pgoyette * You can't do what you want to the process if:
478 1.1 pgoyette * (1) It's not being traced at all,
479 1.1 pgoyette */
480 1.22.2.2 martin if (!ISSET(t->p_slflag, PSL_TRACED))
481 1.22.2.2 martin return EPERM;
482 1.1 pgoyette
483 1.1 pgoyette /*
484 1.22.2.2 martin * (2) it's not being traced by _you_, or
485 1.1 pgoyette */
486 1.1 pgoyette if (t->p_pptr != p) {
487 1.1 pgoyette DPRINTF(("parent %d != %d\n", t->p_pptr->p_pid,
488 1.1 pgoyette p->p_pid));
489 1.22.2.2 martin return EBUSY;
490 1.1 pgoyette }
491 1.1 pgoyette
492 1.1 pgoyette /*
493 1.22.2.2 martin * (3) it's not currently stopped.
494 1.1 pgoyette */
495 1.1 pgoyette if (t->p_stat != SSTOP || !t->p_waited /* XXXSMP */) {
496 1.1 pgoyette DPRINTF(("stat %d flag %d\n", t->p_stat,
497 1.1 pgoyette !t->p_waited));
498 1.22.2.2 martin return EBUSY;
499 1.1 pgoyette }
500 1.22.2.2 martin return 0;
501 1.1 pgoyette
502 1.1 pgoyette default: /* It was not a legal request. */
503 1.22.2.2 martin return EINVAL;
504 1.22.2.2 martin }
505 1.22.2.2 martin }
506 1.22.2.2 martin
507 1.22.2.2 martin static int
508 1.22.2.2 martin ptrace_needs_hold(int req)
509 1.22.2.2 martin {
510 1.22.2.2 martin switch (req) {
511 1.22.2.2 martin #ifdef PT_STEP
512 1.22.2.2 martin case PT_STEP:
513 1.22.2.2 martin #endif
514 1.22.2.2 martin case PT_CONTINUE:
515 1.22.2.2 martin case PT_DETACH:
516 1.22.2.2 martin case PT_KILL:
517 1.22.2.2 martin case PT_SYSCALL:
518 1.22.2.2 martin case PT_SYSCALLEMU:
519 1.22.2.2 martin case PT_ATTACH:
520 1.22.2.2 martin case PT_TRACE_ME:
521 1.22.2.2 martin case PT_GET_SIGINFO:
522 1.22.2.2 martin case PT_SET_SIGINFO:
523 1.22.2.2 martin return 1;
524 1.22.2.2 martin default:
525 1.22.2.2 martin return 0;
526 1.22.2.2 martin }
527 1.22.2.2 martin }
528 1.22.2.2 martin
529 1.22.2.2 martin static int
530 1.22.2.2 martin ptrace_update_lwp(struct proc *t, struct lwp **lt, lwpid_t lid)
531 1.22.2.2 martin {
532 1.22.2.2 martin if (lid == 0 || lid == (*lt)->l_lid || t->p_nlwps == 1)
533 1.22.2.2 martin return 0;
534 1.22.2.2 martin
535 1.22.2.2 martin mutex_enter(t->p_lock);
536 1.22.2.5 martin lwp_delref2(*lt);
537 1.22.2.5 martin
538 1.22.2.2 martin *lt = lwp_find(t, lid);
539 1.22.2.2 martin if (*lt == NULL) {
540 1.22.2.2 martin mutex_exit(t->p_lock);
541 1.22.2.2 martin return ESRCH;
542 1.22.2.2 martin }
543 1.22.2.2 martin
544 1.22.2.2 martin if ((*lt)->l_flag & LW_SYSTEM) {
545 1.22.2.5 martin mutex_exit(t->p_lock);
546 1.22.2.2 martin *lt = NULL;
547 1.22.2.2 martin return EINVAL;
548 1.22.2.2 martin }
549 1.22.2.2 martin
550 1.22.2.2 martin lwp_addref(*lt);
551 1.22.2.2 martin mutex_exit(t->p_lock);
552 1.22.2.2 martin
553 1.22.2.2 martin return 0;
554 1.22.2.2 martin }
555 1.22.2.2 martin
556 1.22.2.2 martin static int
557 1.22.2.2 martin ptrace_get_siginfo(struct proc *t, struct ptrace_methods *ptm, void *addr,
558 1.22.2.2 martin size_t data)
559 1.22.2.2 martin {
560 1.22.2.2 martin struct ptrace_siginfo psi;
561 1.22.2.2 martin
562 1.22.2.4 martin memset(&psi, 0, sizeof(psi));
563 1.22.2.2 martin psi.psi_siginfo._info = t->p_sigctx.ps_info;
564 1.22.2.2 martin psi.psi_lwpid = t->p_sigctx.ps_lwp;
565 1.22.2.2 martin
566 1.22.2.2 martin return ptm->ptm_copyout_siginfo(&psi, addr, data);
567 1.22.2.2 martin }
568 1.22.2.2 martin
569 1.22.2.2 martin static int
570 1.22.2.2 martin ptrace_set_siginfo(struct proc *t, struct lwp **lt, struct ptrace_methods *ptm,
571 1.22.2.2 martin void *addr, size_t data)
572 1.22.2.2 martin {
573 1.22.2.2 martin struct ptrace_siginfo psi;
574 1.22.2.2 martin
575 1.22.2.2 martin int error = ptm->ptm_copyin_siginfo(&psi, addr, data);
576 1.22.2.2 martin if (error)
577 1.22.2.2 martin return error;
578 1.22.2.2 martin
579 1.22.2.2 martin /* Check that the data is a valid signal number or zero. */
580 1.22.2.2 martin if (psi.psi_siginfo.si_signo < 0 || psi.psi_siginfo.si_signo >= NSIG)
581 1.22.2.2 martin return EINVAL;
582 1.22.2.2 martin
583 1.22.2.2 martin t->p_sigctx.ps_faked = true;
584 1.22.2.2 martin t->p_sigctx.ps_info = psi.psi_siginfo._info;
585 1.22.2.2 martin t->p_sigctx.ps_lwp = psi.psi_lwpid;
586 1.22.2.2 martin return 0;
587 1.22.2.2 martin }
588 1.22.2.2 martin
589 1.22.2.2 martin static int
590 1.22.2.2 martin ptrace_get_event_mask(struct proc *t, void *addr, size_t data)
591 1.22.2.2 martin {
592 1.22.2.2 martin struct ptrace_event pe;
593 1.22.2.2 martin
594 1.22.2.2 martin if (data != sizeof(pe)) {
595 1.22.2.2 martin DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(pe)));
596 1.22.2.2 martin return EINVAL;
597 1.22.2.2 martin }
598 1.22.2.2 martin memset(&pe, 0, sizeof(pe));
599 1.22.2.2 martin pe.pe_set_event = ISSET(t->p_slflag, PSL_TRACEFORK) ?
600 1.22.2.2 martin PTRACE_FORK : 0;
601 1.22.2.2 martin pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK) ?
602 1.22.2.2 martin PTRACE_VFORK : 0;
603 1.22.2.2 martin pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK_DONE) ?
604 1.22.2.2 martin PTRACE_VFORK_DONE : 0;
605 1.22.2.2 martin pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_CREATE) ?
606 1.22.2.2 martin PTRACE_LWP_CREATE : 0;
607 1.22.2.2 martin pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_EXIT) ?
608 1.22.2.2 martin PTRACE_LWP_EXIT : 0;
609 1.22.2.2 martin return copyout(&pe, addr, sizeof(pe));
610 1.22.2.2 martin }
611 1.22.2.2 martin
612 1.22.2.2 martin static int
613 1.22.2.2 martin ptrace_set_event_mask(struct proc *t, void *addr, size_t data)
614 1.22.2.2 martin {
615 1.22.2.2 martin struct ptrace_event pe;
616 1.22.2.2 martin int error;
617 1.22.2.2 martin
618 1.22.2.2 martin if (data != sizeof(pe)) {
619 1.22.2.2 martin DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(pe)));
620 1.22.2.2 martin return EINVAL;
621 1.22.2.2 martin }
622 1.22.2.2 martin if ((error = copyin(addr, &pe, sizeof(pe))) != 0)
623 1.22.2.2 martin return error;
624 1.22.2.2 martin
625 1.22.2.2 martin if (pe.pe_set_event & PTRACE_FORK)
626 1.22.2.2 martin SET(t->p_slflag, PSL_TRACEFORK);
627 1.22.2.2 martin else
628 1.22.2.2 martin CLR(t->p_slflag, PSL_TRACEFORK);
629 1.22.2.2 martin #if notyet
630 1.22.2.2 martin if (pe.pe_set_event & PTRACE_VFORK)
631 1.22.2.2 martin SET(t->p_slflag, PSL_TRACEVFORK);
632 1.22.2.2 martin else
633 1.22.2.2 martin CLR(t->p_slflag, PSL_TRACEVFORK);
634 1.22.2.2 martin #else
635 1.22.2.2 martin if (pe.pe_set_event & PTRACE_VFORK)
636 1.22.2.2 martin return ENOTSUP;
637 1.22.2.2 martin #endif
638 1.22.2.2 martin if (pe.pe_set_event & PTRACE_VFORK_DONE)
639 1.22.2.2 martin SET(t->p_slflag, PSL_TRACEVFORK_DONE);
640 1.22.2.2 martin else
641 1.22.2.2 martin CLR(t->p_slflag, PSL_TRACEVFORK_DONE);
642 1.22.2.2 martin if (pe.pe_set_event & PTRACE_LWP_CREATE)
643 1.22.2.2 martin SET(t->p_slflag, PSL_TRACELWP_CREATE);
644 1.22.2.2 martin else
645 1.22.2.2 martin CLR(t->p_slflag, PSL_TRACELWP_CREATE);
646 1.22.2.2 martin if (pe.pe_set_event & PTRACE_LWP_EXIT)
647 1.22.2.2 martin SET(t->p_slflag, PSL_TRACELWP_EXIT);
648 1.22.2.2 martin else
649 1.22.2.2 martin CLR(t->p_slflag, PSL_TRACELWP_EXIT);
650 1.22.2.2 martin return 0;
651 1.22.2.2 martin }
652 1.22.2.2 martin
653 1.22.2.2 martin static int
654 1.22.2.2 martin ptrace_get_process_state(struct proc *t, void *addr, size_t data)
655 1.22.2.2 martin {
656 1.22.2.2 martin struct ptrace_state ps;
657 1.22.2.2 martin
658 1.22.2.2 martin if (data != sizeof(ps)) {
659 1.22.2.2 martin DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(ps)));
660 1.22.2.2 martin return EINVAL;
661 1.22.2.2 martin }
662 1.22.2.2 martin memset(&ps, 0, sizeof(ps));
663 1.22.2.2 martin
664 1.22.2.2 martin if (t->p_fpid) {
665 1.22.2.2 martin ps.pe_report_event = PTRACE_FORK;
666 1.22.2.2 martin ps.pe_other_pid = t->p_fpid;
667 1.22.2.2 martin } else if (t->p_vfpid) {
668 1.22.2.2 martin ps.pe_report_event = PTRACE_VFORK;
669 1.22.2.2 martin ps.pe_other_pid = t->p_vfpid;
670 1.22.2.2 martin } else if (t->p_vfpid_done) {
671 1.22.2.2 martin ps.pe_report_event = PTRACE_VFORK_DONE;
672 1.22.2.2 martin ps.pe_other_pid = t->p_vfpid_done;
673 1.22.2.2 martin } else if (t->p_lwp_created) {
674 1.22.2.2 martin ps.pe_report_event = PTRACE_LWP_CREATE;
675 1.22.2.2 martin ps.pe_lwp = t->p_lwp_created;
676 1.22.2.2 martin } else if (t->p_lwp_exited) {
677 1.22.2.2 martin ps.pe_report_event = PTRACE_LWP_EXIT;
678 1.22.2.2 martin ps.pe_lwp = t->p_lwp_exited;
679 1.22.2.2 martin }
680 1.22.2.2 martin return copyout(&ps, addr, sizeof(ps));
681 1.22.2.2 martin }
682 1.22.2.2 martin
683 1.22.2.2 martin static int
684 1.22.2.2 martin ptrace_lwpinfo(struct proc *t, struct lwp **lt, void *addr, size_t data)
685 1.22.2.2 martin {
686 1.22.2.2 martin struct ptrace_lwpinfo pl;
687 1.22.2.2 martin
688 1.22.2.2 martin if (data != sizeof(pl)) {
689 1.22.2.2 martin DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(pl)));
690 1.22.2.2 martin return EINVAL;
691 1.22.2.2 martin }
692 1.22.2.2 martin int error = copyin(addr, &pl, sizeof(pl));
693 1.22.2.2 martin if (error)
694 1.22.2.2 martin return error;
695 1.22.2.2 martin
696 1.22.2.2 martin lwpid_t tmp = pl.pl_lwpid;
697 1.22.2.2 martin lwp_delref(*lt);
698 1.22.2.2 martin mutex_enter(t->p_lock);
699 1.22.2.2 martin if (tmp == 0)
700 1.22.2.2 martin *lt = lwp_find_first(t);
701 1.22.2.2 martin else {
702 1.22.2.2 martin *lt = lwp_find(t, tmp);
703 1.22.2.2 martin if (*lt == NULL) {
704 1.22.2.2 martin mutex_exit(t->p_lock);
705 1.22.2.2 martin return ESRCH;
706 1.22.2.2 martin }
707 1.22.2.2 martin *lt = LIST_NEXT(*lt, l_sibling);
708 1.22.2.2 martin }
709 1.22.2.2 martin
710 1.22.2.2 martin while (*lt != NULL && !lwp_alive(*lt))
711 1.22.2.2 martin *lt = LIST_NEXT(*lt, l_sibling);
712 1.22.2.2 martin
713 1.22.2.2 martin pl.pl_lwpid = 0;
714 1.22.2.2 martin pl.pl_event = 0;
715 1.22.2.2 martin if (*lt) {
716 1.22.2.2 martin lwp_addref(*lt);
717 1.22.2.2 martin pl.pl_lwpid = (*lt)->l_lid;
718 1.22.2.2 martin
719 1.22.2.2 martin if ((*lt)->l_flag & LW_WSUSPEND)
720 1.22.2.2 martin pl.pl_event = PL_EVENT_SUSPENDED;
721 1.22.2.2 martin /*
722 1.22.2.2 martin * If we match the lwp, or it was sent to every lwp,
723 1.22.2.2 martin * we set PL_EVENT_SIGNAL.
724 1.22.2.2 martin * XXX: ps_lwp == 0 means everyone and noone, so
725 1.22.2.2 martin * check ps_signo too.
726 1.22.2.2 martin */
727 1.22.2.2 martin else if ((*lt)->l_lid == t->p_sigctx.ps_lwp
728 1.22.2.2 martin || (t->p_sigctx.ps_lwp == 0 &&
729 1.22.2.2 martin t->p_sigctx.ps_info._signo))
730 1.22.2.2 martin pl.pl_event = PL_EVENT_SIGNAL;
731 1.22.2.2 martin }
732 1.22.2.2 martin mutex_exit(t->p_lock);
733 1.22.2.2 martin
734 1.22.2.2 martin return copyout(&pl, addr, sizeof(pl));
735 1.22.2.2 martin }
736 1.22.2.2 martin
737 1.22.2.2 martin static int
738 1.22.2.2 martin ptrace_startstop(struct proc *t, struct lwp **lt, int rq, void *addr,
739 1.22.2.2 martin size_t data)
740 1.22.2.2 martin {
741 1.22.2.2 martin int error;
742 1.22.2.2 martin
743 1.22.2.2 martin if ((error = ptrace_update_lwp(t, lt, data)) != 0)
744 1.22.2.2 martin return error;
745 1.22.2.2 martin
746 1.22.2.2 martin lwp_lock(*lt);
747 1.22.2.2 martin if (rq == PT_SUSPEND)
748 1.22.2.2 martin (*lt)->l_flag |= LW_WSUSPEND;
749 1.22.2.2 martin else
750 1.22.2.2 martin (*lt)->l_flag &= ~LW_WSUSPEND;
751 1.22.2.2 martin lwp_unlock(*lt);
752 1.22.2.2 martin return 0;
753 1.22.2.2 martin }
754 1.22.2.2 martin
755 1.22.2.2 martin #ifdef PT_REGISTERS
756 1.22.2.2 martin static int
757 1.22.2.2 martin ptrace_uio_dir(int req)
758 1.22.2.2 martin {
759 1.22.2.2 martin switch (req) {
760 1.22.2.2 martin case_PT_GETREGS
761 1.22.2.2 martin case_PT_GETFPREGS
762 1.22.2.2 martin case_PT_GETDBREGS
763 1.22.2.2 martin return UIO_READ;
764 1.22.2.2 martin case_PT_SETREGS
765 1.22.2.2 martin case_PT_SETFPREGS
766 1.22.2.2 martin case_PT_SETDBREGS
767 1.22.2.2 martin return UIO_WRITE;
768 1.22.2.2 martin default:
769 1.22.2.2 martin return -1;
770 1.22.2.2 martin }
771 1.22.2.2 martin }
772 1.22.2.2 martin
773 1.22.2.2 martin static int
774 1.22.2.2 martin ptrace_regs(struct lwp *l, struct lwp **lt, int rq, struct ptrace_methods *ptm,
775 1.22.2.2 martin void *addr, size_t data)
776 1.22.2.2 martin {
777 1.22.2.2 martin int error;
778 1.22.2.2 martin struct proc *t = (*lt)->l_proc;
779 1.22.2.2 martin struct vmspace *vm;
780 1.22.2.2 martin
781 1.22.2.2 martin if ((error = ptrace_update_lwp(t, lt, data)) != 0)
782 1.22.2.2 martin return error;
783 1.22.2.2 martin
784 1.22.2.2 martin int dir = ptrace_uio_dir(rq);
785 1.22.2.2 martin size_t size;
786 1.22.2.2 martin int (*func)(struct lwp *, struct lwp *, struct uio *);
787 1.22.2.2 martin
788 1.22.2.2 martin switch (rq) {
789 1.22.2.2 martin #if defined(PT_SETREGS) || defined(PT_GETREGS)
790 1.22.2.2 martin case_PT_GETREGS
791 1.22.2.2 martin case_PT_SETREGS
792 1.22.2.2 martin if (!process_validregs(*lt))
793 1.22.2.2 martin return EINVAL;
794 1.22.2.2 martin size = PROC_REGSZ(t);
795 1.22.2.2 martin func = ptm->ptm_doregs;
796 1.22.2.2 martin break;
797 1.22.2.2 martin #endif
798 1.22.2.2 martin #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
799 1.22.2.2 martin case_PT_GETFPREGS
800 1.22.2.2 martin case_PT_SETFPREGS
801 1.22.2.2 martin if (!process_validfpregs(*lt))
802 1.22.2.2 martin return EINVAL;
803 1.22.2.2 martin size = PROC_FPREGSZ(t);
804 1.22.2.2 martin func = ptm->ptm_dofpregs;
805 1.22.2.2 martin break;
806 1.22.2.2 martin #endif
807 1.22.2.2 martin #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
808 1.22.2.2 martin case_PT_GETDBREGS
809 1.22.2.2 martin case_PT_SETDBREGS
810 1.22.2.2 martin if (!process_validdbregs(*lt))
811 1.22.2.2 martin return EINVAL;
812 1.22.2.2 martin size = PROC_DBREGSZ(t);
813 1.22.2.2 martin func = ptm->ptm_dodbregs;
814 1.22.2.2 martin break;
815 1.22.2.2 martin #endif
816 1.22.2.2 martin default:
817 1.22.2.2 martin return EINVAL;
818 1.22.2.2 martin }
819 1.22.2.2 martin
820 1.22.2.2 martin error = proc_vmspace_getref(l->l_proc, &vm);
821 1.22.2.2 martin if (error)
822 1.22.2.2 martin return error;
823 1.22.2.2 martin
824 1.22.2.2 martin struct uio uio;
825 1.22.2.2 martin struct iovec iov;
826 1.22.2.2 martin
827 1.22.2.2 martin iov.iov_base = addr;
828 1.22.2.2 martin iov.iov_len = size;
829 1.22.2.2 martin uio.uio_iov = &iov;
830 1.22.2.2 martin uio.uio_iovcnt = 1;
831 1.22.2.2 martin uio.uio_offset = 0;
832 1.22.2.2 martin uio.uio_resid = iov.iov_len;
833 1.22.2.2 martin uio.uio_rw = dir;
834 1.22.2.2 martin uio.uio_vmspace = vm;
835 1.22.2.2 martin
836 1.22.2.2 martin error = (*func)(l, *lt, &uio);
837 1.22.2.2 martin uvmspace_free(vm);
838 1.22.2.2 martin return error;
839 1.22.2.2 martin }
840 1.22.2.2 martin #endif
841 1.22.2.2 martin
842 1.22.2.2 martin static int
843 1.22.2.2 martin ptrace_sendsig(struct proc *t, struct lwp *lt, int signo, int resume_all)
844 1.22.2.2 martin {
845 1.22.2.2 martin ksiginfo_t ksi;
846 1.22.2.2 martin
847 1.22.2.2 martin t->p_fpid = 0;
848 1.22.2.2 martin t->p_vfpid = 0;
849 1.22.2.2 martin t->p_vfpid_done = 0;
850 1.22.2.2 martin t->p_lwp_created = 0;
851 1.22.2.2 martin t->p_lwp_exited = 0;
852 1.22.2.2 martin
853 1.22.2.2 martin /* Finally, deliver the requested signal (or none). */
854 1.22.2.2 martin if (t->p_stat == SSTOP) {
855 1.22.2.2 martin /*
856 1.22.2.2 martin * Unstop the process. If it needs to take a
857 1.22.2.2 martin * signal, make all efforts to ensure that at
858 1.22.2.2 martin * an LWP runs to see it.
859 1.22.2.2 martin */
860 1.22.2.2 martin t->p_xsig = signo;
861 1.22.2.2 martin if (resume_all)
862 1.22.2.2 martin proc_unstop(t);
863 1.22.2.2 martin else
864 1.22.2.2 martin lwp_unstop(lt);
865 1.22.2.2 martin return 0;
866 1.22.2.2 martin }
867 1.22.2.2 martin
868 1.22.2.2 martin KSI_INIT_EMPTY(&ksi);
869 1.22.2.2 martin if (t->p_sigctx.ps_faked) {
870 1.22.2.2 martin if (signo != t->p_sigctx.ps_info._signo)
871 1.22.2.2 martin return EINVAL;
872 1.22.2.2 martin t->p_sigctx.ps_faked = false;
873 1.22.2.2 martin ksi.ksi_info = t->p_sigctx.ps_info;
874 1.22.2.2 martin ksi.ksi_lid = t->p_sigctx.ps_lwp;
875 1.22.2.2 martin } else if (signo == 0) {
876 1.22.2.2 martin return 0;
877 1.22.2.2 martin } else {
878 1.22.2.2 martin ksi.ksi_signo = signo;
879 1.22.2.2 martin }
880 1.22.2.2 martin
881 1.22.2.2 martin kpsignal2(t, &ksi);
882 1.22.2.2 martin return 0;
883 1.22.2.2 martin }
884 1.22.2.2 martin
885 1.22.2.2 martin static int
886 1.22.2.2 martin ptrace_dumpcore(struct lwp *lt, char *path, size_t len)
887 1.22.2.2 martin {
888 1.22.2.2 martin int error;
889 1.22.2.2 martin if (path != NULL) {
890 1.22.2.2 martin
891 1.22.2.2 martin if (len >= MAXPATHLEN)
892 1.22.2.2 martin return EINVAL;
893 1.22.2.2 martin
894 1.22.2.2 martin char *src = path;
895 1.22.2.2 martin path = kmem_alloc(len + 1, KM_SLEEP);
896 1.22.2.2 martin error = copyin(src, path, len);
897 1.22.2.2 martin if (error)
898 1.22.2.2 martin goto out;
899 1.22.2.2 martin path[len] = '\0';
900 1.22.2.2 martin }
901 1.22.2.2 martin error = (*coredump_vec)(lt, path);
902 1.22.2.2 martin out:
903 1.22.2.2 martin if (path)
904 1.22.2.2 martin kmem_free(path, len + 1);
905 1.22.2.2 martin return error;
906 1.22.2.2 martin }
907 1.22.2.2 martin
908 1.22.2.2 martin static int
909 1.22.2.2 martin ptrace_doio(struct lwp *l, struct proc *t, struct lwp *lt,
910 1.22.2.2 martin struct ptrace_io_desc *piod, void *addr, bool sysspace)
911 1.22.2.2 martin {
912 1.22.2.2 martin struct uio uio;
913 1.22.2.2 martin struct iovec iov;
914 1.22.2.2 martin int error, tmp;
915 1.22.2.2 martin
916 1.22.2.2 martin error = 0;
917 1.22.2.2 martin iov.iov_base = piod->piod_addr;
918 1.22.2.2 martin iov.iov_len = piod->piod_len;
919 1.22.2.2 martin uio.uio_iov = &iov;
920 1.22.2.2 martin uio.uio_iovcnt = 1;
921 1.22.2.2 martin uio.uio_offset = (off_t)(unsigned long)piod->piod_offs;
922 1.22.2.2 martin uio.uio_resid = piod->piod_len;
923 1.22.2.2 martin
924 1.22.2.2 martin switch (piod->piod_op) {
925 1.22.2.2 martin case PIOD_READ_D:
926 1.22.2.2 martin case PIOD_READ_I:
927 1.22.2.2 martin uio.uio_rw = UIO_READ;
928 1.22.2.2 martin break;
929 1.22.2.2 martin case PIOD_WRITE_D:
930 1.22.2.2 martin case PIOD_WRITE_I:
931 1.22.2.2 martin /*
932 1.22.2.2 martin * Can't write to a RAS
933 1.22.2.2 martin */
934 1.22.2.2 martin if (ras_lookup(t, addr) != (void *)-1) {
935 1.22.2.2 martin return EACCES;
936 1.22.2.2 martin }
937 1.22.2.2 martin uio.uio_rw = UIO_WRITE;
938 1.22.2.2 martin break;
939 1.22.2.2 martin case PIOD_READ_AUXV:
940 1.22.2.2 martin uio.uio_rw = UIO_READ;
941 1.22.2.2 martin tmp = t->p_execsw->es_arglen;
942 1.22.2.2 martin if (uio.uio_offset > tmp)
943 1.22.2.2 martin return EIO;
944 1.22.2.2 martin if (uio.uio_resid > tmp - uio.uio_offset)
945 1.22.2.2 martin uio.uio_resid = tmp - uio.uio_offset;
946 1.22.2.2 martin piod->piod_len = iov.iov_len = uio.uio_resid;
947 1.22.2.2 martin error = process_auxv_offset(t, &uio);
948 1.22.2.2 martin break;
949 1.22.2.2 martin default:
950 1.1 pgoyette error = EINVAL;
951 1.1 pgoyette break;
952 1.1 pgoyette }
953 1.1 pgoyette
954 1.22.2.2 martin if (error)
955 1.22.2.2 martin return error;
956 1.22.2.2 martin
957 1.22.2.2 martin if (sysspace) {
958 1.22.2.2 martin uio.uio_vmspace = vmspace_kernel();
959 1.22.2.2 martin } else {
960 1.22.2.2 martin error = proc_vmspace_getref(l->l_proc, &uio.uio_vmspace);
961 1.22.2.2 martin if (error)
962 1.22.2.2 martin return error;
963 1.1 pgoyette }
964 1.1 pgoyette
965 1.22.2.2 martin error = process_domem(l, lt, &uio);
966 1.22.2.2 martin if (!sysspace)
967 1.22.2.2 martin uvmspace_free(uio.uio_vmspace);
968 1.22.2.2 martin if (error)
969 1.1 pgoyette return error;
970 1.22.2.2 martin piod->piod_len -= uio.uio_resid;
971 1.22.2.2 martin return 0;
972 1.22.2.2 martin }
973 1.22.2.2 martin
974 1.22.2.2 martin int
975 1.22.2.2 martin do_ptrace(struct ptrace_methods *ptm, struct lwp *l, int req, pid_t pid,
976 1.22.2.2 martin void *addr, int data, register_t *retval)
977 1.22.2.2 martin {
978 1.22.2.2 martin struct proc *p = l->l_proc;
979 1.22.2.2 martin struct lwp *lt = NULL;
980 1.22.2.2 martin struct lwp *lt2;
981 1.22.2.2 martin struct proc *t; /* target process */
982 1.22.2.2 martin struct ptrace_io_desc piod;
983 1.22.2.2 martin int error, write, tmp, pheld;
984 1.22.2.2 martin int signo = 0;
985 1.22.2.2 martin int resume_all;
986 1.22.2.6 martin bool locked;
987 1.22.2.2 martin error = 0;
988 1.22.2.2 martin
989 1.22.2.2 martin /*
990 1.22.2.2 martin * If attaching or detaching, we need to get a write hold on the
991 1.22.2.2 martin * proclist lock so that we can re-parent the target process.
992 1.22.2.2 martin */
993 1.22.2.2 martin mutex_enter(proc_lock);
994 1.22.2.2 martin
995 1.22.2.2 martin t = ptrace_find(l, req, pid);
996 1.22.2.2 martin if (t == NULL) {
997 1.22.2.2 martin mutex_exit(proc_lock);
998 1.22.2.2 martin return ESRCH;
999 1.22.2.2 martin }
1000 1.22.2.2 martin
1001 1.22.2.2 martin pheld = 1;
1002 1.22.2.6 martin if ((error = ptrace_allowed(l, req, t, p, &locked)) != 0)
1003 1.22.2.2 martin goto out;
1004 1.22.2.2 martin
1005 1.22.2.2 martin if ((error = kauth_authorize_process(l->l_cred,
1006 1.22.2.2 martin KAUTH_PROCESS_PTRACE, t, KAUTH_ARG(req), NULL, NULL)) != 0)
1007 1.22.2.2 martin goto out;
1008 1.22.2.2 martin
1009 1.22.2.2 martin if ((lt = lwp_find_first(t)) == NULL) {
1010 1.22.2.2 martin error = ESRCH;
1011 1.22.2.2 martin goto out;
1012 1.1 pgoyette }
1013 1.1 pgoyette
1014 1.1 pgoyette /* Do single-step fixup if needed. */
1015 1.1 pgoyette FIX_SSTEP(t);
1016 1.1 pgoyette KASSERT(lt != NULL);
1017 1.1 pgoyette lwp_addref(lt);
1018 1.1 pgoyette
1019 1.1 pgoyette /*
1020 1.1 pgoyette * Which locks do we need held? XXX Ugly.
1021 1.1 pgoyette */
1022 1.22.2.2 martin if ((pheld = ptrace_needs_hold(req)) == 0) {
1023 1.1 pgoyette mutex_exit(t->p_lock);
1024 1.22.2.2 martin mutex_exit(proc_lock);
1025 1.1 pgoyette }
1026 1.1 pgoyette
1027 1.1 pgoyette /* Now do the operation. */
1028 1.1 pgoyette write = 0;
1029 1.1 pgoyette *retval = 0;
1030 1.1 pgoyette tmp = 0;
1031 1.1 pgoyette resume_all = 1;
1032 1.1 pgoyette
1033 1.1 pgoyette switch (req) {
1034 1.22.2.2 martin case PT_TRACE_ME:
1035 1.1 pgoyette /* Just set the trace flag. */
1036 1.1 pgoyette SET(t->p_slflag, PSL_TRACED);
1037 1.1 pgoyette t->p_opptr = t->p_pptr;
1038 1.1 pgoyette break;
1039 1.1 pgoyette
1040 1.22.2.2 martin case PT_WRITE_I: /* XXX no separate I and D spaces */
1041 1.22.2.2 martin case PT_WRITE_D:
1042 1.1 pgoyette write = 1;
1043 1.1 pgoyette tmp = data;
1044 1.1 pgoyette /* FALLTHROUGH */
1045 1.22.2.2 martin case PT_READ_I: /* XXX no separate I and D spaces */
1046 1.22.2.2 martin case PT_READ_D:
1047 1.22.2.2 martin piod.piod_addr = &tmp;
1048 1.22.2.2 martin piod.piod_len = sizeof(tmp);
1049 1.22.2.2 martin piod.piod_offs = addr;
1050 1.22.2.2 martin piod.piod_op = write ? PIOD_WRITE_D : PIOD_READ_D;
1051 1.22.2.2 martin if ((error = ptrace_doio(l, t, lt, &piod, addr, true)) != 0)
1052 1.22.2.2 martin break;
1053 1.1 pgoyette if (!write)
1054 1.1 pgoyette *retval = tmp;
1055 1.1 pgoyette break;
1056 1.1 pgoyette
1057 1.22.2.2 martin case PT_IO:
1058 1.22.2.2 martin if ((error = ptm->ptm_copyin_piod(&piod, addr, data)) != 0)
1059 1.1 pgoyette break;
1060 1.22.2.2 martin if ((error = ptrace_doio(l, t, lt, &piod, addr, false)) != 0)
1061 1.1 pgoyette break;
1062 1.22.2.2 martin error = ptm->ptm_copyout_piod(&piod, addr, data);
1063 1.1 pgoyette break;
1064 1.1 pgoyette
1065 1.22.2.2 martin case PT_DUMPCORE:
1066 1.22.2.2 martin error = ptrace_dumpcore(lt, addr, data);
1067 1.1 pgoyette break;
1068 1.1 pgoyette
1069 1.1 pgoyette #ifdef PT_STEP
1070 1.22.2.2 martin case PT_STEP:
1071 1.1 pgoyette /*
1072 1.1 pgoyette * From the 4.4BSD PRM:
1073 1.1 pgoyette * "Execution continues as in request PT_CONTINUE; however
1074 1.1 pgoyette * as soon as possible after execution of at least one
1075 1.1 pgoyette * instruction, execution stops again. [ ... ]"
1076 1.1 pgoyette */
1077 1.1 pgoyette #endif
1078 1.22.2.2 martin case PT_CONTINUE:
1079 1.22.2.2 martin case PT_SYSCALL:
1080 1.22.2.2 martin case PT_DETACH:
1081 1.1 pgoyette if (req == PT_SYSCALL) {
1082 1.1 pgoyette if (!ISSET(t->p_slflag, PSL_SYSCALL)) {
1083 1.1 pgoyette SET(t->p_slflag, PSL_SYSCALL);
1084 1.1 pgoyette #ifdef __HAVE_SYSCALL_INTERN
1085 1.1 pgoyette (*t->p_emul->e_syscall_intern)(t);
1086 1.1 pgoyette #endif
1087 1.1 pgoyette }
1088 1.1 pgoyette } else {
1089 1.1 pgoyette if (ISSET(t->p_slflag, PSL_SYSCALL)) {
1090 1.1 pgoyette CLR(t->p_slflag, PSL_SYSCALL);
1091 1.1 pgoyette #ifdef __HAVE_SYSCALL_INTERN
1092 1.1 pgoyette (*t->p_emul->e_syscall_intern)(t);
1093 1.1 pgoyette #endif
1094 1.1 pgoyette }
1095 1.1 pgoyette }
1096 1.1 pgoyette t->p_trace_enabled = trace_is_enabled(t);
1097 1.1 pgoyette
1098 1.1 pgoyette /*
1099 1.1 pgoyette * Pick up the LWPID, if supplied. There are two cases:
1100 1.1 pgoyette * data < 0 : step or continue single thread, lwp = -data
1101 1.1 pgoyette * data > 0 in PT_STEP : step this thread, continue others
1102 1.1 pgoyette * For operations other than PT_STEP, data > 0 means
1103 1.1 pgoyette * data is the signo to deliver to the process.
1104 1.1 pgoyette */
1105 1.1 pgoyette tmp = data;
1106 1.1 pgoyette if (tmp >= 0) {
1107 1.1 pgoyette #ifdef PT_STEP
1108 1.1 pgoyette if (req == PT_STEP)
1109 1.1 pgoyette signo = 0;
1110 1.1 pgoyette else
1111 1.1 pgoyette #endif
1112 1.1 pgoyette {
1113 1.1 pgoyette signo = tmp;
1114 1.1 pgoyette tmp = 0; /* don't search for LWP */
1115 1.1 pgoyette }
1116 1.1 pgoyette } else
1117 1.1 pgoyette tmp = -tmp;
1118 1.1 pgoyette
1119 1.1 pgoyette if (tmp > 0) {
1120 1.1 pgoyette if (req == PT_DETACH) {
1121 1.1 pgoyette error = EINVAL;
1122 1.1 pgoyette break;
1123 1.1 pgoyette }
1124 1.1 pgoyette lwp_delref2 (lt);
1125 1.1 pgoyette lt = lwp_find(t, tmp);
1126 1.1 pgoyette if (lt == NULL) {
1127 1.1 pgoyette error = ESRCH;
1128 1.1 pgoyette break;
1129 1.1 pgoyette }
1130 1.1 pgoyette lwp_addref(lt);
1131 1.1 pgoyette resume_all = 0;
1132 1.1 pgoyette signo = 0;
1133 1.1 pgoyette }
1134 1.1 pgoyette
1135 1.1 pgoyette /*
1136 1.1 pgoyette * From the 4.4BSD PRM:
1137 1.1 pgoyette * "The data argument is taken as a signal number and the
1138 1.1 pgoyette * child's execution continues at location addr as if it
1139 1.1 pgoyette * incurred that signal. Normally the signal number will
1140 1.1 pgoyette * be either 0 to indicate that the signal that caused the
1141 1.1 pgoyette * stop should be ignored, or that value fetched out of
1142 1.1 pgoyette * the process's image indicating which signal caused
1143 1.1 pgoyette * the stop. If addr is (int *)1 then execution continues
1144 1.1 pgoyette * from where it stopped."
1145 1.1 pgoyette */
1146 1.1 pgoyette
1147 1.1 pgoyette /* Check that the data is a valid signal number or zero. */
1148 1.1 pgoyette if (signo < 0 || signo >= NSIG) {
1149 1.1 pgoyette error = EINVAL;
1150 1.1 pgoyette break;
1151 1.1 pgoyette }
1152 1.1 pgoyette
1153 1.15 kamil /* Prevent process deadlock */
1154 1.15 kamil if (resume_all) {
1155 1.15 kamil #ifdef PT_STEP
1156 1.15 kamil if (req == PT_STEP) {
1157 1.15 kamil if (lt->l_flag & LW_WSUSPEND) {
1158 1.15 kamil error = EDEADLK;
1159 1.15 kamil break;
1160 1.15 kamil }
1161 1.15 kamil } else
1162 1.15 kamil #endif
1163 1.15 kamil {
1164 1.15 kamil error = EDEADLK;
1165 1.15 kamil LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
1166 1.15 kamil if ((lt2->l_flag & LW_WSUSPEND) == 0) {
1167 1.15 kamil error = 0;
1168 1.15 kamil break;
1169 1.15 kamil }
1170 1.15 kamil }
1171 1.15 kamil if (error != 0)
1172 1.15 kamil break;
1173 1.15 kamil }
1174 1.15 kamil } else {
1175 1.15 kamil if (lt->l_flag & LW_WSUSPEND) {
1176 1.15 kamil error = EDEADLK;
1177 1.15 kamil break;
1178 1.15 kamil }
1179 1.15 kamil }
1180 1.15 kamil
1181 1.1 pgoyette /* If the address parameter is not (int *)1, set the pc. */
1182 1.1 pgoyette if ((int *)addr != (int *)1) {
1183 1.1 pgoyette error = process_set_pc(lt, addr);
1184 1.1 pgoyette if (error != 0)
1185 1.1 pgoyette break;
1186 1.1 pgoyette }
1187 1.1 pgoyette #ifdef PT_STEP
1188 1.1 pgoyette /*
1189 1.1 pgoyette * Arrange for a single-step, if that's requested and possible.
1190 1.1 pgoyette * More precisely, set the single step status as requested for
1191 1.1 pgoyette * the requested thread, and clear it for other threads.
1192 1.1 pgoyette */
1193 1.1 pgoyette LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
1194 1.21 kamil if (ISSET(lt2->l_pflag, LP_SINGLESTEP)) {
1195 1.21 kamil lwp_lock(lt2);
1196 1.21 kamil process_sstep(lt2, 1);
1197 1.21 kamil lwp_unlock(lt2);
1198 1.21 kamil } else if (lt != lt2) {
1199 1.1 pgoyette lwp_lock(lt2);
1200 1.1 pgoyette process_sstep(lt2, 0);
1201 1.1 pgoyette lwp_unlock(lt2);
1202 1.1 pgoyette }
1203 1.1 pgoyette }
1204 1.21 kamil error = process_sstep(lt,
1205 1.21 kamil ISSET(lt->l_pflag, LP_SINGLESTEP) || req == PT_STEP);
1206 1.1 pgoyette if (error)
1207 1.1 pgoyette break;
1208 1.1 pgoyette #endif
1209 1.1 pgoyette if (req == PT_DETACH) {
1210 1.22.2.2 martin CLR(t->p_slflag, PSL_TRACED|PSL_SYSCALL);
1211 1.1 pgoyette
1212 1.1 pgoyette /* give process back to original parent or init */
1213 1.1 pgoyette if (t->p_opptr != t->p_pptr) {
1214 1.1 pgoyette struct proc *pp = t->p_opptr;
1215 1.1 pgoyette proc_reparent(t, pp ? pp : initproc);
1216 1.1 pgoyette }
1217 1.1 pgoyette
1218 1.1 pgoyette /* not being traced any more */
1219 1.1 pgoyette t->p_opptr = NULL;
1220 1.21 kamil
1221 1.21 kamil /* clear single step */
1222 1.21 kamil LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
1223 1.21 kamil CLR(lt2->l_pflag, LP_SINGLESTEP);
1224 1.21 kamil }
1225 1.21 kamil CLR(lt->l_pflag, LP_SINGLESTEP);
1226 1.1 pgoyette }
1227 1.1 pgoyette sendsig:
1228 1.22.2.2 martin error = ptrace_sendsig(t, lt, signo, resume_all);
1229 1.1 pgoyette break;
1230 1.1 pgoyette
1231 1.22.2.2 martin case PT_SYSCALLEMU:
1232 1.1 pgoyette if (!ISSET(t->p_slflag, PSL_SYSCALL) || t->p_stat != SSTOP) {
1233 1.1 pgoyette error = EINVAL;
1234 1.1 pgoyette break;
1235 1.1 pgoyette }
1236 1.1 pgoyette SET(t->p_slflag, PSL_SYSCALLEMU);
1237 1.1 pgoyette break;
1238 1.1 pgoyette
1239 1.21 kamil #ifdef PT_STEP
1240 1.22.2.2 martin case PT_SETSTEP:
1241 1.21 kamil write = 1;
1242 1.21 kamil
1243 1.22.2.2 martin case PT_CLEARSTEP:
1244 1.21 kamil /* write = 0 done above. */
1245 1.22.2.2 martin if ((error = ptrace_update_lwp(t, <, data)) != 0)
1246 1.22.2.2 martin break;
1247 1.21 kamil
1248 1.22.2.2 martin if (write)
1249 1.21 kamil SET(lt->l_pflag, LP_SINGLESTEP);
1250 1.21 kamil else
1251 1.21 kamil CLR(lt->l_pflag, LP_SINGLESTEP);
1252 1.21 kamil break;
1253 1.21 kamil #endif
1254 1.21 kamil
1255 1.22.2.2 martin case PT_KILL:
1256 1.1 pgoyette /* just send the process a KILL signal. */
1257 1.1 pgoyette signo = SIGKILL;
1258 1.1 pgoyette goto sendsig; /* in PT_CONTINUE, above. */
1259 1.1 pgoyette
1260 1.22.2.2 martin case PT_ATTACH:
1261 1.1 pgoyette /*
1262 1.1 pgoyette * Go ahead and set the trace flag.
1263 1.1 pgoyette * Save the old parent (it's reset in
1264 1.1 pgoyette * _DETACH, and also in kern_exit.c:wait4()
1265 1.1 pgoyette * Reparent the process so that the tracing
1266 1.1 pgoyette * proc gets to see all the action.
1267 1.1 pgoyette * Stop the target.
1268 1.1 pgoyette */
1269 1.2 christos proc_changeparent(t, p);
1270 1.4 christos signo = SIGSTOP;
1271 1.1 pgoyette goto sendsig;
1272 1.1 pgoyette
1273 1.22.2.2 martin case PT_GET_EVENT_MASK:
1274 1.22.2.2 martin error = ptrace_get_event_mask(t, addr, data);
1275 1.1 pgoyette break;
1276 1.1 pgoyette
1277 1.22.2.2 martin case PT_SET_EVENT_MASK:
1278 1.22.2.2 martin error = ptrace_set_event_mask(t, addr, data);
1279 1.1 pgoyette break;
1280 1.1 pgoyette
1281 1.22.2.2 martin case PT_GET_PROCESS_STATE:
1282 1.22.2.2 martin error = ptrace_get_process_state(t, addr, data);
1283 1.8 kamil break;
1284 1.8 kamil
1285 1.22.2.2 martin case PT_LWPINFO:
1286 1.22.2.2 martin error = ptrace_lwpinfo(t, <, addr, data);
1287 1.8 kamil break;
1288 1.8 kamil
1289 1.22.2.2 martin case PT_SET_SIGINFO:
1290 1.22.2.2 martin error = ptrace_set_siginfo(t, <, ptm, addr, data);
1291 1.15 kamil break;
1292 1.15 kamil
1293 1.22.2.2 martin case PT_GET_SIGINFO:
1294 1.22.2.2 martin error = ptrace_get_siginfo(t, ptm, addr, data);
1295 1.1 pgoyette break;
1296 1.1 pgoyette
1297 1.22.2.2 martin case PT_RESUME:
1298 1.22.2.2 martin case PT_SUSPEND:
1299 1.22.2.2 martin error = ptrace_startstop(t, <, req, addr, data);
1300 1.1 pgoyette break;
1301 1.17 kamil
1302 1.22.2.2 martin #ifdef PT_REGISTERS
1303 1.22.2.2 martin case_PT_SETREGS
1304 1.22.2.2 martin case_PT_GETREGS
1305 1.22.2.2 martin case_PT_SETFPREGS
1306 1.22.2.2 martin case_PT_GETFPREGS
1307 1.22.2.2 martin case_PT_SETDBREGS
1308 1.22.2.2 martin case_PT_GETDBREGS
1309 1.22.2.2 martin error = ptrace_regs(l, <, req, ptm, addr, data);
1310 1.7 kamil break;
1311 1.7 kamil #endif
1312 1.7 kamil
1313 1.1 pgoyette #ifdef __HAVE_PTRACE_MACHDEP
1314 1.1 pgoyette PTRACE_MACHDEP_REQUEST_CASES
1315 1.1 pgoyette error = ptrace_machdep_dorequest(l, lt, req, addr, data);
1316 1.1 pgoyette break;
1317 1.1 pgoyette #endif
1318 1.1 pgoyette }
1319 1.1 pgoyette
1320 1.22.2.2 martin out:
1321 1.1 pgoyette if (pheld) {
1322 1.1 pgoyette mutex_exit(t->p_lock);
1323 1.1 pgoyette mutex_exit(proc_lock);
1324 1.1 pgoyette }
1325 1.1 pgoyette if (lt != NULL)
1326 1.1 pgoyette lwp_delref(lt);
1327 1.22.2.6 martin if (locked)
1328 1.22.2.6 martin rw_exit(&t->p_reflock);
1329 1.1 pgoyette
1330 1.1 pgoyette return error;
1331 1.1 pgoyette }
1332 1.1 pgoyette
1333 1.22.2.2 martin typedef int (*regrfunc_t)(struct lwp *, void *, size_t *);
1334 1.22.2.2 martin typedef int (*regwfunc_t)(struct lwp *, void *, size_t);
1335 1.22.2.2 martin
1336 1.22.2.2 martin #ifdef PT_REGISTERS
1337 1.22.2.2 martin static int
1338 1.22.2.2 martin proc_regio(struct lwp *l, struct uio *uio, size_t ks, regrfunc_t r,
1339 1.22.2.2 martin regwfunc_t w)
1340 1.1 pgoyette {
1341 1.22.2.2 martin char buf[1024];
1342 1.1 pgoyette int error;
1343 1.1 pgoyette char *kv;
1344 1.22.2.2 martin size_t kl;
1345 1.1 pgoyette
1346 1.22.2.2 martin if (ks > sizeof(buf))
1347 1.22.2.2 martin return E2BIG;
1348 1.22.2.2 martin
1349 1.22.2.2 martin if (uio->uio_offset < 0 || uio->uio_offset > (off_t)ks)
1350 1.1 pgoyette return EINVAL;
1351 1.1 pgoyette
1352 1.22.2.2 martin kv = buf + uio->uio_offset;
1353 1.22.2.2 martin kl = ks - uio->uio_offset;
1354 1.1 pgoyette
1355 1.22.2.2 martin if (kl > uio->uio_resid)
1356 1.1 pgoyette kl = uio->uio_resid;
1357 1.1 pgoyette
1358 1.22.2.2 martin error = (*r)(l, buf, &ks);
1359 1.1 pgoyette if (error == 0)
1360 1.1 pgoyette error = uiomove(kv, kl, uio);
1361 1.1 pgoyette if (error == 0 && uio->uio_rw == UIO_WRITE) {
1362 1.1 pgoyette if (l->l_stat != LSSTOP)
1363 1.1 pgoyette error = EBUSY;
1364 1.1 pgoyette else
1365 1.22.2.2 martin error = (*w)(l, buf, ks);
1366 1.1 pgoyette }
1367 1.1 pgoyette
1368 1.1 pgoyette uio->uio_offset = 0;
1369 1.1 pgoyette return error;
1370 1.22.2.2 martin }
1371 1.22.2.2 martin #endif
1372 1.22.2.2 martin
1373 1.22.2.2 martin int
1374 1.22.2.2 martin process_doregs(struct lwp *curl /*tracer*/,
1375 1.22.2.2 martin struct lwp *l /*traced*/,
1376 1.22.2.2 martin struct uio *uio)
1377 1.22.2.2 martin {
1378 1.22.2.2 martin #if defined(PT_GETREGS) || defined(PT_SETREGS)
1379 1.22.2.2 martin size_t s;
1380 1.22.2.2 martin regrfunc_t r;
1381 1.22.2.2 martin regwfunc_t w;
1382 1.22.2.2 martin
1383 1.22.2.2 martin #ifdef COMPAT_NETBSD32
1384 1.22.2.2 martin const bool pk32 = (l->l_proc->p_flag & PK_32) != 0;
1385 1.22.2.2 martin
1386 1.22.2.2 martin if (__predict_false(pk32)) {
1387 1.22.2.2 martin s = sizeof(process_reg32);
1388 1.22.2.2 martin r = (regrfunc_t)process_read_regs32;
1389 1.22.2.2 martin w = (regwfunc_t)process_write_regs32;
1390 1.22.2.2 martin } else
1391 1.22.2.2 martin #endif
1392 1.22.2.2 martin {
1393 1.22.2.2 martin s = sizeof(struct reg);
1394 1.22.2.2 martin r = (regrfunc_t)process_read_regs;
1395 1.22.2.2 martin w = (regwfunc_t)process_write_regs;
1396 1.22.2.2 martin }
1397 1.22.2.2 martin return proc_regio(l, uio, s, r, w);
1398 1.1 pgoyette #else
1399 1.1 pgoyette return EINVAL;
1400 1.1 pgoyette #endif
1401 1.1 pgoyette }
1402 1.1 pgoyette
1403 1.1 pgoyette int
1404 1.1 pgoyette process_validregs(struct lwp *l)
1405 1.1 pgoyette {
1406 1.1 pgoyette
1407 1.1 pgoyette #if defined(PT_SETREGS) || defined(PT_GETREGS)
1408 1.1 pgoyette return (l->l_flag & LW_SYSTEM) == 0;
1409 1.1 pgoyette #else
1410 1.1 pgoyette return 0;
1411 1.1 pgoyette #endif
1412 1.1 pgoyette }
1413 1.1 pgoyette
1414 1.1 pgoyette int
1415 1.1 pgoyette process_dofpregs(struct lwp *curl /*tracer*/,
1416 1.1 pgoyette struct lwp *l /*traced*/,
1417 1.1 pgoyette struct uio *uio)
1418 1.1 pgoyette {
1419 1.1 pgoyette #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
1420 1.22.2.2 martin size_t s;
1421 1.22.2.2 martin regrfunc_t r;
1422 1.22.2.2 martin regwfunc_t w;
1423 1.22.2.2 martin
1424 1.22.2.2 martin #ifdef COMPAT_NETBSD32
1425 1.22.2.2 martin const bool pk32 = (l->l_proc->p_flag & PK_32) != 0;
1426 1.22.2.2 martin
1427 1.22.2.2 martin if (__predict_false(pk32)) {
1428 1.22.2.2 martin s = sizeof(process_fpreg32);
1429 1.22.2.2 martin r = (regrfunc_t)process_read_fpregs32;
1430 1.22.2.2 martin w = (regwfunc_t)process_write_fpregs32;
1431 1.22.2.2 martin } else
1432 1.22.2.2 martin #endif
1433 1.22.2.2 martin {
1434 1.22.2.2 martin s = sizeof(struct fpreg);
1435 1.22.2.2 martin r = (regrfunc_t)process_read_fpregs;
1436 1.22.2.2 martin w = (regwfunc_t)process_write_fpregs;
1437 1.1 pgoyette }
1438 1.22.2.2 martin return proc_regio(l, uio, s, r, w);
1439 1.1 pgoyette #else
1440 1.1 pgoyette return EINVAL;
1441 1.1 pgoyette #endif
1442 1.1 pgoyette }
1443 1.1 pgoyette
1444 1.1 pgoyette int
1445 1.1 pgoyette process_validfpregs(struct lwp *l)
1446 1.1 pgoyette {
1447 1.1 pgoyette
1448 1.1 pgoyette #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
1449 1.1 pgoyette return (l->l_flag & LW_SYSTEM) == 0;
1450 1.1 pgoyette #else
1451 1.1 pgoyette return 0;
1452 1.1 pgoyette #endif
1453 1.1 pgoyette }
1454 1.1 pgoyette
1455 1.17 kamil int
1456 1.17 kamil process_dodbregs(struct lwp *curl /*tracer*/,
1457 1.17 kamil struct lwp *l /*traced*/,
1458 1.17 kamil struct uio *uio)
1459 1.17 kamil {
1460 1.17 kamil #if defined(PT_GETDBREGS) || defined(PT_SETDBREGS)
1461 1.22.2.2 martin size_t s;
1462 1.22.2.2 martin regrfunc_t r;
1463 1.22.2.2 martin regwfunc_t w;
1464 1.22.2.2 martin
1465 1.22.2.2 martin #ifdef COMPAT_NETBSD32
1466 1.22.2.2 martin const bool pk32 = (l->l_proc->p_flag & PK_32) != 0;
1467 1.22.2.2 martin
1468 1.22.2.2 martin if (__predict_false(pk32)) {
1469 1.22.2.2 martin s = sizeof(process_dbreg32);
1470 1.22.2.2 martin r = (regrfunc_t)process_read_dbregs32;
1471 1.22.2.2 martin w = (regwfunc_t)process_write_dbregs32;
1472 1.22.2.2 martin } else
1473 1.22.2.2 martin #endif
1474 1.22.2.2 martin {
1475 1.22.2.2 martin s = sizeof(struct dbreg);
1476 1.22.2.2 martin r = (regrfunc_t)process_read_dbregs;
1477 1.22.2.2 martin w = (regwfunc_t)process_write_dbregs;
1478 1.17 kamil }
1479 1.22.2.2 martin return proc_regio(l, uio, s, r, w);
1480 1.17 kamil #else
1481 1.17 kamil return EINVAL;
1482 1.17 kamil #endif
1483 1.17 kamil }
1484 1.17 kamil
1485 1.17 kamil int
1486 1.17 kamil process_validdbregs(struct lwp *l)
1487 1.17 kamil {
1488 1.17 kamil
1489 1.17 kamil #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
1490 1.17 kamil return (l->l_flag & LW_SYSTEM) == 0;
1491 1.17 kamil #else
1492 1.17 kamil return 0;
1493 1.17 kamil #endif
1494 1.17 kamil }
1495 1.17 kamil
1496 1.1 pgoyette static int
1497 1.1 pgoyette process_auxv_offset(struct proc *p, struct uio *uio)
1498 1.1 pgoyette {
1499 1.1 pgoyette struct ps_strings pss;
1500 1.1 pgoyette int error;
1501 1.1 pgoyette off_t off = (off_t)p->p_psstrp;
1502 1.1 pgoyette
1503 1.1 pgoyette if ((error = copyin_psstrings(p, &pss)) != 0)
1504 1.1 pgoyette return error;
1505 1.1 pgoyette
1506 1.1 pgoyette if (pss.ps_envstr == NULL)
1507 1.1 pgoyette return EIO;
1508 1.1 pgoyette
1509 1.1 pgoyette uio->uio_offset += (off_t)(vaddr_t)(pss.ps_envstr + pss.ps_nenvstr + 1);
1510 1.1 pgoyette #ifdef __MACHINE_STACK_GROWS_UP
1511 1.1 pgoyette if (uio->uio_offset < off)
1512 1.1 pgoyette return EIO;
1513 1.1 pgoyette #else
1514 1.1 pgoyette if (uio->uio_offset > off)
1515 1.1 pgoyette return EIO;
1516 1.1 pgoyette if ((uio->uio_offset + uio->uio_resid) > off)
1517 1.1 pgoyette uio->uio_resid = off - uio->uio_offset;
1518 1.1 pgoyette #endif
1519 1.1 pgoyette return 0;
1520 1.1 pgoyette }
1521 1.1 pgoyette #endif /* PTRACE */
1522 1.1 pgoyette
1523 1.1 pgoyette MODULE(MODULE_CLASS_EXEC, ptrace_common, "");
1524 1.1 pgoyette
1525 1.1 pgoyette static int
1526 1.1 pgoyette ptrace_common_modcmd(modcmd_t cmd, void *arg)
1527 1.1 pgoyette {
1528 1.1 pgoyette int error;
1529 1.1 pgoyette
1530 1.1 pgoyette switch (cmd) {
1531 1.1 pgoyette case MODULE_CMD_INIT:
1532 1.1 pgoyette error = ptrace_init();
1533 1.1 pgoyette break;
1534 1.1 pgoyette case MODULE_CMD_FINI:
1535 1.1 pgoyette error = ptrace_fini();
1536 1.1 pgoyette break;
1537 1.1 pgoyette default:
1538 1.1 pgoyette ptrace_hooks();
1539 1.1 pgoyette error = ENOTTY;
1540 1.1 pgoyette break;
1541 1.1 pgoyette }
1542 1.1 pgoyette return error;
1543 1.1 pgoyette }
1544