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