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