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