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