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