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