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