Home | History | Annotate | Line # | Download | only in kern
sys_ptrace_common.c revision 1.85
      1 /*	$NetBSD: sys_ptrace_common.c,v 1.85 2020/10/19 14:47:01 kamil Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1982, 1986, 1989, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  * (c) UNIX System Laboratories, Inc.
     36  * All or some portions of this file are derived from material licensed
     37  * to the University of California by American Telephone and Telegraph
     38  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     39  * the permission of UNIX System Laboratories, Inc.
     40  *
     41  * This code is derived from software contributed to Berkeley by
     42  * Jan-Simon Pendry.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. Neither the name of the University nor the names of its contributors
     53  *    may be used to endorse or promote products derived from this software
     54  *    without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     66  * SUCH DAMAGE.
     67  *
     68  *	from: @(#)sys_process.c	8.1 (Berkeley) 6/10/93
     69  */
     70 
     71 /*-
     72  * Copyright (c) 1993 Jan-Simon Pendry.
     73  * Copyright (c) 1994 Christopher G. Demetriou.  All rights reserved.
     74  *
     75  * This code is derived from software contributed to Berkeley by
     76  * Jan-Simon Pendry.
     77  *
     78  * Redistribution and use in source and binary forms, with or without
     79  * modification, are permitted provided that the following conditions
     80  * are met:
     81  * 1. Redistributions of source code must retain the above copyright
     82  *    notice, this list of conditions and the following disclaimer.
     83  * 2. Redistributions in binary form must reproduce the above copyright
     84  *    notice, this list of conditions and the following disclaimer in the
     85  *    documentation and/or other materials provided with the distribution.
     86  * 3. All advertising materials mentioning features or use of this software
     87  *    must display the following acknowledgement:
     88  *	This product includes software developed by the University of
     89  *	California, Berkeley and its contributors.
     90  * 4. Neither the name of the University nor the names of its contributors
     91  *    may be used to endorse or promote products derived from this software
     92  *    without specific prior written permission.
     93  *
     94  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     95  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     96  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     97  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     98  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     99  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    100  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    101  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    102  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    103  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    104  * SUCH DAMAGE.
    105  *
    106  *	from: @(#)sys_process.c	8.1 (Berkeley) 6/10/93
    107  */
    108 
    109 
    110 #include <sys/cdefs.h>
    111 __KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.85 2020/10/19 14:47:01 kamil Exp $");
    112 
    113 #ifdef _KERNEL_OPT
    114 #include "opt_ptrace.h"
    115 #include "opt_ktrace.h"
    116 #include "opt_pax.h"
    117 #include "opt_compat_netbsd32.h"
    118 #endif
    119 
    120 #if defined(__HAVE_COMPAT_NETBSD32) && !defined(COMPAT_NETBSD32) \
    121     && !defined(_RUMPKERNEL)
    122 #define COMPAT_NETBSD32
    123 #endif
    124 
    125 #include <sys/param.h>
    126 #include <sys/systm.h>
    127 #include <sys/proc.h>
    128 #include <sys/errno.h>
    129 #include <sys/exec.h>
    130 #include <sys/pax.h>
    131 #include <sys/ptrace.h>
    132 #include <sys/uio.h>
    133 #include <sys/ras.h>
    134 #include <sys/kmem.h>
    135 #include <sys/kauth.h>
    136 #include <sys/mount.h>
    137 #include <sys/syscallargs.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 #if defined(PT_SETREGS) || defined(PT_GETREGS) || \
    201     defined(PT_SETFPREGS) || defined(PT_GETFPREGS) || \
    202     defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
    203 # define PT_REGISTERS
    204 #endif
    205 
    206 #ifndef PTRACE_REGS_ALIGN
    207 #define PTRACE_REGS_ALIGN /* nothing */
    208 #endif
    209 
    210 static int
    211 ptrace_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    212     void *arg0, void *arg1, void *arg2, void *arg3)
    213 {
    214 	struct proc *p;
    215 	int result;
    216 #ifdef PT_SETDBREGS
    217 	extern int user_set_dbregs;
    218 #endif
    219 
    220 	result = KAUTH_RESULT_DEFER;
    221 	p = arg0;
    222 
    223 #if 0
    224 	mutex_enter(&ptrace_mtx);
    225 	ptrace_cbref++;
    226 	mutex_exit(&ptrace_mtx);
    227 #endif
    228 	if (action != KAUTH_PROCESS_PTRACE)
    229 		goto out;
    230 
    231 	switch ((u_long)arg1) {
    232 #ifdef PT_SETDBREGS
    233 	case_PT_SETDBREGS
    234 		if (kauth_cred_getuid(cred) != 0 && user_set_dbregs == 0) {
    235 			result = KAUTH_RESULT_DENY;
    236 			break;
    237 		}
    238 #endif
    239 		/* FALLTHROUGH */
    240 	case PT_TRACE_ME:
    241 	case PT_ATTACH:
    242 	case PT_WRITE_I:
    243 	case PT_WRITE_D:
    244 	case PT_READ_I:
    245 	case PT_READ_D:
    246 	case PT_IO:
    247 	case_PT_GETREGS
    248 	case_PT_SETREGS
    249 	case_PT_GETFPREGS
    250 	case_PT_SETFPREGS
    251 	case_PT_GETDBREGS
    252 	case PT_SET_EVENT_MASK:
    253 	case PT_GET_EVENT_MASK:
    254 	case PT_GET_PROCESS_STATE:
    255 	case PT_SET_SIGINFO:
    256 	case PT_GET_SIGINFO:
    257 #ifdef __HAVE_PTRACE_MACHDEP
    258 	PTRACE_MACHDEP_REQUEST_CASES
    259 #endif
    260 		if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) ||
    261 		    ISSET(p->p_flag, PK_SUGID)) {
    262 			break;
    263 		}
    264 
    265 		result = KAUTH_RESULT_ALLOW;
    266 
    267 	break;
    268 
    269 #ifdef PT_STEP
    270 	case PT_STEP:
    271 	case PT_SETSTEP:
    272 	case PT_CLEARSTEP:
    273 #endif
    274 	case PT_CONTINUE:
    275 	case PT_KILL:
    276 	case PT_DETACH:
    277 	case PT_LWPINFO:
    278 	case PT_SYSCALL:
    279 	case PT_SYSCALLEMU:
    280 	case PT_DUMPCORE:
    281 	case PT_RESUME:
    282 	case PT_SUSPEND:
    283 	case PT_STOP:
    284 	case PT_LWPSTATUS:
    285 	case PT_LWPNEXT:
    286 	case PT_SET_SIGPASS:
    287 	case PT_GET_SIGPASS:
    288 		result = KAUTH_RESULT_ALLOW;
    289 		break;
    290 
    291 	default:
    292 		break;
    293 	}
    294 
    295  out:
    296 #if 0
    297 	mutex_enter(&ptrace_mtx);
    298 	if (--ptrace_cbref == 0)
    299 		cv_broadcast(&ptrace_cv);
    300 	mutex_exit(&ptrace_mtx);
    301 #endif
    302 
    303 	return result;
    304 }
    305 
    306 int
    307 ptrace_init(void)
    308 {
    309 
    310 #if 0
    311 	mutex_init(&ptrace_mtx, MUTEX_DEFAULT, IPL_NONE);
    312 	cv_init(&ptrace_cv, "ptracecb");
    313 	ptrace_cbref = 0;
    314 #endif
    315 	ptrace_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
    316 	    ptrace_listener_cb, NULL);
    317 	return 0;
    318 }
    319 
    320 int
    321 ptrace_fini(void)
    322 {
    323 
    324 	kauth_unlisten_scope(ptrace_listener);
    325 
    326 #if 0
    327 	/* Make sure no-one is executing our kauth listener */
    328 
    329 	mutex_enter(&ptrace_mtx);
    330 	while (ptrace_cbref != 0)
    331 		cv_wait(&ptrace_cv, &ptrace_mtx);
    332 	mutex_exit(&ptrace_mtx);
    333 	mutex_destroy(&ptrace_mtx);
    334 	cv_destroy(&ptrace_cv);
    335 #endif
    336 
    337 	return 0;
    338 }
    339 
    340 static struct proc *
    341 ptrace_find(struct lwp *l, int req, pid_t pid)
    342 {
    343 	struct proc *t;
    344 
    345 	/* "A foolish consistency..." XXX */
    346 	if (req == PT_TRACE_ME) {
    347 		t = l->l_proc;
    348 		mutex_enter(t->p_lock);
    349 		return t;
    350 	}
    351 
    352 	/* Find the process we're supposed to be operating on. */
    353 	t = proc_find(pid);
    354 	if (t == NULL)
    355 		return NULL;
    356 
    357 	/* XXX-elad */
    358 	mutex_enter(t->p_lock);
    359 	int error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
    360 	    t, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
    361 	if (error) {
    362 		mutex_exit(t->p_lock);
    363 		return NULL;
    364 	}
    365 	return t;
    366 }
    367 
    368 static int
    369 ptrace_allowed(struct lwp *l, int req, struct proc *t, struct proc *p,
    370     bool *locked)
    371 {
    372 	*locked = false;
    373 
    374 	/*
    375 	 * Grab a reference on the process to prevent it from execing or
    376 	 * exiting.
    377 	 */
    378 	if (!rw_tryenter(&t->p_reflock, RW_READER))
    379 		return EBUSY;
    380 
    381 	*locked = true;
    382 
    383 	/* Make sure we can operate on it. */
    384 	switch (req) {
    385 	case PT_TRACE_ME:
    386 		/*
    387 		 * You can't say to the parent of a process to start tracing if:
    388 		 *	(1) the parent is initproc,
    389 		 */
    390 		if (p->p_pptr == initproc)
    391 			return EPERM;
    392 
    393 		/*
    394 		 *	(2) the process is initproc, or
    395 		 */
    396 		if (p == initproc)
    397 			return EPERM;
    398 
    399 		/*
    400 		 *	(3) the child is already traced.
    401 		 */
    402 		if (ISSET(p->p_slflag, PSL_TRACED))
    403 			return EBUSY;
    404 
    405 		return 0;
    406 
    407 	case PT_ATTACH:
    408 		/*
    409 		 * You can't attach to a process if:
    410 		 *	(1) it's the process that's doing the attaching,
    411 		 */
    412 		if (t == p)
    413 			return EINVAL;
    414 
    415 		/*
    416 		 *	(2) it's a system process,
    417 		 */
    418 		if (t->p_flag & PK_SYSTEM)
    419 			return EPERM;
    420 
    421 		/*
    422 		 *	(3) the tracer is initproc,
    423 		 */
    424 		if (p == initproc)
    425 			return EPERM;
    426 
    427 		/*
    428 		 *	(4) it's already being traced,
    429 		 */
    430 		if (ISSET(t->p_slflag, PSL_TRACED))
    431 			return EBUSY;
    432 
    433 		/*
    434 		 *	(5) it's a vfork(2)ed parent of the current process, or
    435 		 */
    436 		if (ISSET(p->p_lflag, PL_PPWAIT) && p->p_pptr == t)
    437 			return EPERM;
    438 
    439 		/*
    440 		 * 	(6) the tracer is chrooted, and its root directory is
    441 		 * 	    not at or above the root directory of the tracee
    442 		 */
    443 		mutex_exit(t->p_lock);	/* XXXSMP */
    444 		int tmp = proc_isunder(t, l);
    445 		mutex_enter(t->p_lock);	/* XXXSMP */
    446 		if (!tmp)
    447 			return EPERM;
    448 		return 0;
    449 
    450 	case PT_READ_I:
    451 	case PT_READ_D:
    452 	case PT_WRITE_I:
    453 	case PT_WRITE_D:
    454 	case PT_IO:
    455 	case PT_SET_SIGINFO:
    456 	case PT_GET_SIGINFO:
    457 	case_PT_GETREGS
    458 	case_PT_SETREGS
    459 	case_PT_GETFPREGS
    460 	case_PT_SETFPREGS
    461 	case_PT_GETDBREGS
    462 	case_PT_SETDBREGS
    463 #ifdef __HAVE_PTRACE_MACHDEP
    464 	PTRACE_MACHDEP_REQUEST_CASES
    465 #endif
    466 		/*
    467 		 * You can't read/write the memory or registers of a process
    468 		 * if the tracer is chrooted, and its root directory is not at
    469 		 * or above the root directory of the tracee.
    470 		 */
    471 		mutex_exit(t->p_lock);	/* XXXSMP */
    472 		tmp = proc_isunder(t, l);
    473 		mutex_enter(t->p_lock);	/* XXXSMP */
    474 		if (!tmp)
    475 			return EPERM;
    476 		/*FALLTHROUGH*/
    477 
    478 	case PT_CONTINUE:
    479 	case PT_KILL:
    480 	case PT_DETACH:
    481 	case PT_LWPINFO:
    482 	case PT_SYSCALL:
    483 	case PT_SYSCALLEMU:
    484 	case PT_DUMPCORE:
    485 #ifdef PT_STEP
    486 	case PT_STEP:
    487 	case PT_SETSTEP:
    488 	case PT_CLEARSTEP:
    489 #endif
    490 	case PT_SET_EVENT_MASK:
    491 	case PT_GET_EVENT_MASK:
    492 	case PT_GET_PROCESS_STATE:
    493 	case PT_RESUME:
    494 	case PT_SUSPEND:
    495 	case PT_STOP:
    496 	case PT_LWPSTATUS:
    497 	case PT_LWPNEXT:
    498 	case PT_SET_SIGPASS:
    499 	case PT_GET_SIGPASS:
    500 		/*
    501 		 * You can't do what you want to the process if:
    502 		 *	(1) It's not being traced at all,
    503 		 */
    504 		if (!ISSET(t->p_slflag, PSL_TRACED))
    505 			return EPERM;
    506 
    507 		/*
    508 		 *	(2) it's not being traced by _you_, or
    509 		 */
    510 		if (t->p_pptr != p) {
    511 			DPRINTF(("parent %d != %d\n", t->p_pptr->p_pid,
    512 			    p->p_pid));
    513 			return EBUSY;
    514 		}
    515 
    516 		/*
    517 		 *	(3) it's not currently stopped.
    518 		 *
    519 		 *	As an exception allow PT_KILL and PT_STOP here.
    520 		 */
    521 		if (req != PT_KILL && req != PT_STOP &&
    522 		    (t->p_stat != SSTOP || !t->p_waited /* XXXSMP */)) {
    523 			DPRINTF(("stat %d flag %d\n", t->p_stat,
    524 			    !t->p_waited));
    525 			return EBUSY;
    526 		}
    527 		return 0;
    528 
    529 	default:			/* It was not a legal request. */
    530 		return EINVAL;
    531 	}
    532 }
    533 
    534 static int
    535 ptrace_needs_hold(int req)
    536 {
    537 	switch (req) {
    538 #ifdef PT_STEP
    539 	case PT_STEP:
    540 #endif
    541 	case PT_CONTINUE:
    542 	case PT_DETACH:
    543 	case PT_KILL:
    544 	case PT_SYSCALL:
    545 	case PT_SYSCALLEMU:
    546 	case PT_ATTACH:
    547 	case PT_TRACE_ME:
    548 	case PT_GET_SIGINFO:
    549 	case PT_SET_SIGINFO:
    550 	case PT_STOP:
    551 		return 1;
    552 	default:
    553 		return 0;
    554 	}
    555 }
    556 
    557 int
    558 ptrace_update_lwp(struct proc *t, struct lwp **lt, lwpid_t lid)
    559 {
    560 	if (lid == 0 || lid == (*lt)->l_lid || t->p_nlwps == 1)
    561 		return 0;
    562 
    563 	mutex_enter(t->p_lock);
    564 	lwp_delref2(*lt);
    565 
    566 	*lt = lwp_find(t, lid);
    567 	if (*lt == NULL) {
    568 		mutex_exit(t->p_lock);
    569 		return ESRCH;
    570 	}
    571 
    572 	if ((*lt)->l_flag & LW_SYSTEM) {
    573 		mutex_exit(t->p_lock);
    574 		*lt = NULL;
    575 		return EINVAL;
    576 	}
    577 
    578 	lwp_addref(*lt);
    579 	mutex_exit(t->p_lock);
    580 
    581 	return 0;
    582 }
    583 
    584 static int
    585 ptrace_get_siginfo(struct proc *t, struct ptrace_methods *ptm, void *addr,
    586     size_t data)
    587 {
    588 	struct ptrace_siginfo psi;
    589 
    590 	memset(&psi, 0, sizeof(psi));
    591 	psi.psi_siginfo._info = t->p_sigctx.ps_info;
    592 	psi.psi_lwpid = t->p_sigctx.ps_lwp;
    593 	DPRINTF(("%s: lwp=%d signal=%d\n", __func__, psi.psi_lwpid,
    594 	    psi.psi_siginfo.si_signo));
    595 
    596 	return ptm->ptm_copyout_siginfo(&psi, addr, data);
    597 }
    598 
    599 static int
    600 ptrace_set_siginfo(struct proc *t, struct lwp **lt, struct ptrace_methods *ptm,
    601     void *addr, size_t data)
    602 {
    603 	struct ptrace_siginfo psi;
    604 
    605 	int error = ptm->ptm_copyin_siginfo(&psi, addr, data);
    606 	if (error)
    607 		return error;
    608 
    609 	/* Check that the data is a valid signal number or zero. */
    610 	if (psi.psi_siginfo.si_signo < 0 || psi.psi_siginfo.si_signo >= NSIG)
    611 		return EINVAL;
    612 
    613 	t->p_sigctx.ps_faked = true;
    614 	t->p_sigctx.ps_info = psi.psi_siginfo._info;
    615 	t->p_sigctx.ps_lwp = psi.psi_lwpid;
    616 	DPRINTF(("%s: lwp=%d signal=%d\n", __func__, psi.psi_lwpid,
    617 	    psi.psi_siginfo.si_signo));
    618 	return 0;
    619 }
    620 
    621 static int
    622 ptrace_get_sigpass(struct proc *t, void *addr, size_t data)
    623 {
    624 	sigset_t set;
    625 
    626 	if (data > sizeof(set) || data <= 0) {
    627 		DPRINTF(("%s: invalid data: %zu < %zu <= 0\n",
    628 		        __func__, sizeof(set), data));
    629 		return EINVAL;
    630 	}
    631 
    632 	set = t->p_sigctx.ps_sigpass;
    633 
    634 	return copyout(&set, addr, data);
    635 }
    636 
    637 static int
    638 ptrace_set_sigpass(struct proc *t, void *addr, size_t data)
    639 {
    640 	sigset_t set;
    641 	int error;
    642 
    643 	if (data > sizeof(set) || data <= 0) {
    644 		DPRINTF(("%s: invalid data: %zu < %zu <= 0\n",
    645 		        __func__, sizeof(set), data));
    646 		return EINVAL;
    647 	}
    648 
    649 	memset(&set, 0, sizeof(set));
    650 
    651 	if ((error = copyin(addr, &set, data)))
    652 		return error;
    653 
    654 	/* We catch SIGSTOP and cannot intercept SIGKILL. */
    655 	sigminusset(&sigcantmask, &set);
    656 
    657 	t->p_sigctx.ps_sigpass = set;
    658 
    659 	return 0;
    660 }
    661 
    662 static int
    663 ptrace_get_event_mask(struct proc *t, void *addr, size_t data)
    664 {
    665 	struct ptrace_event pe;
    666 
    667 	if (data != sizeof(pe)) {
    668 		DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(pe)));
    669 		return EINVAL;
    670 	}
    671 	memset(&pe, 0, sizeof(pe));
    672 	pe.pe_set_event = ISSET(t->p_slflag, PSL_TRACEFORK) ?
    673 	    PTRACE_FORK : 0;
    674 	pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK) ?
    675 	    PTRACE_VFORK : 0;
    676 	pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK_DONE) ?
    677 	    PTRACE_VFORK_DONE : 0;
    678 	pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_CREATE) ?
    679 	    PTRACE_LWP_CREATE : 0;
    680 	pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_EXIT) ?
    681 	    PTRACE_LWP_EXIT : 0;
    682 	pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEPOSIX_SPAWN) ?
    683 	    PTRACE_POSIX_SPAWN : 0;
    684 	DPRINTF(("%s: lwp=%d event=%#x\n", __func__,
    685 	    t->p_sigctx.ps_lwp, pe.pe_set_event));
    686 	return copyout(&pe, addr, sizeof(pe));
    687 }
    688 
    689 static int
    690 ptrace_set_event_mask(struct proc *t, void *addr, size_t data)
    691 {
    692 	struct ptrace_event pe;
    693 	int error;
    694 
    695 	if (data != sizeof(pe)) {
    696 		DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(pe)));
    697 		return EINVAL;
    698 	}
    699 	if ((error = copyin(addr, &pe, sizeof(pe))) != 0)
    700 		return error;
    701 
    702 	DPRINTF(("%s: lwp=%d event=%#x\n", __func__,
    703 	    t->p_sigctx.ps_lwp, pe.pe_set_event));
    704 	if (pe.pe_set_event & PTRACE_FORK)
    705 		SET(t->p_slflag, PSL_TRACEFORK);
    706 	else
    707 		CLR(t->p_slflag, PSL_TRACEFORK);
    708 
    709 	if (pe.pe_set_event & PTRACE_VFORK)
    710 		SET(t->p_slflag, PSL_TRACEVFORK);
    711 	else
    712 		CLR(t->p_slflag, PSL_TRACEVFORK);
    713 
    714 	if (pe.pe_set_event & PTRACE_VFORK_DONE)
    715 		SET(t->p_slflag, PSL_TRACEVFORK_DONE);
    716 	else
    717 		CLR(t->p_slflag, PSL_TRACEVFORK_DONE);
    718 
    719 	if (pe.pe_set_event & PTRACE_LWP_CREATE)
    720 		SET(t->p_slflag, PSL_TRACELWP_CREATE);
    721 	else
    722 		CLR(t->p_slflag, PSL_TRACELWP_CREATE);
    723 
    724 	if (pe.pe_set_event & PTRACE_LWP_EXIT)
    725 		SET(t->p_slflag, PSL_TRACELWP_EXIT);
    726 	else
    727 		CLR(t->p_slflag, PSL_TRACELWP_EXIT);
    728 
    729 	if (pe.pe_set_event & PTRACE_POSIX_SPAWN)
    730 		SET(t->p_slflag, PSL_TRACEPOSIX_SPAWN);
    731 	else
    732 		CLR(t->p_slflag, PSL_TRACEPOSIX_SPAWN);
    733 
    734 	return 0;
    735 }
    736 
    737 static int
    738 ptrace_get_process_state(struct proc *t, void *addr, size_t data)
    739 {
    740 	struct _ksiginfo *si;
    741 	struct ptrace_state ps;
    742 
    743 	if (data != sizeof(ps)) {
    744 		DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(ps)));
    745 		return EINVAL;
    746 	}
    747 
    748 	if (t->p_sigctx.ps_info._signo != SIGTRAP ||
    749 	    (t->p_sigctx.ps_info._code != TRAP_CHLD &&
    750 	        t->p_sigctx.ps_info._code != TRAP_LWP)) {
    751 		memset(&ps, 0, sizeof(ps));
    752 	} else {
    753 		si = &t->p_sigctx.ps_info;
    754 
    755 		KASSERT(si->_reason._ptrace_state._pe_report_event > 0);
    756 		KASSERT(si->_reason._ptrace_state._option._pe_other_pid > 0);
    757 
    758 		ps.pe_report_event = si->_reason._ptrace_state._pe_report_event;
    759 
    760 		CTASSERT(sizeof(ps.pe_other_pid) == sizeof(ps.pe_lwp));
    761 		ps.pe_other_pid =
    762 			si->_reason._ptrace_state._option._pe_other_pid;
    763 	}
    764 
    765 	DPRINTF(("%s: lwp=%d event=%#x pid=%d lwp=%d\n", __func__,
    766 	    t->p_sigctx.ps_lwp, ps.pe_report_event,
    767 	    ps.pe_other_pid, ps.pe_lwp));
    768 	return copyout(&ps, addr, sizeof(ps));
    769 }
    770 
    771 static int
    772 ptrace_lwpinfo(struct proc *t, struct lwp **lt, void *addr, size_t data)
    773 {
    774 	struct ptrace_lwpinfo pl;
    775 
    776 	if (data != sizeof(pl)) {
    777 		DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(pl)));
    778 		return EINVAL;
    779 	}
    780 	int error = copyin(addr, &pl, sizeof(pl));
    781 	if (error)
    782 		return error;
    783 
    784 	lwpid_t tmp = pl.pl_lwpid;
    785 	lwp_delref(*lt);
    786 	mutex_enter(t->p_lock);
    787 	if (tmp == 0)
    788 		*lt = lwp_find_first(t);
    789 	else {
    790 		*lt = lwp_find(t, tmp);
    791 		if (*lt == NULL) {
    792 			mutex_exit(t->p_lock);
    793 			return ESRCH;
    794 		}
    795 		*lt = LIST_NEXT(*lt, l_sibling);
    796 	}
    797 
    798 	while (*lt != NULL && (!lwp_alive(*lt) ||
    799 	       ((*lt)->l_flag & LW_SYSTEM) != 0))
    800 		*lt = LIST_NEXT(*lt, l_sibling);
    801 
    802 	pl.pl_lwpid = 0;
    803 	pl.pl_event = 0;
    804 	if (*lt) {
    805 		lwp_addref(*lt);
    806 		pl.pl_lwpid = (*lt)->l_lid;
    807 
    808 		if ((*lt)->l_flag & LW_WSUSPEND)
    809 			pl.pl_event = PL_EVENT_SUSPENDED;
    810 		/*
    811 		 * If we match the lwp, or it was sent to every lwp,
    812 		 * we set PL_EVENT_SIGNAL.
    813 		 * XXX: ps_lwp == 0 means everyone and noone, so
    814 		 * check ps_signo too.
    815 		 */
    816 		else if ((*lt)->l_lid == t->p_sigctx.ps_lwp
    817 			 || (t->p_sigctx.ps_lwp == 0 &&
    818 			     t->p_sigctx.ps_info._signo)) {
    819 			DPRINTF(("%s: lwp=%d siglwp=%d signo %d\n", __func__,
    820 			    pl.pl_lwpid, t->p_sigctx.ps_lwp,
    821 			    t->p_sigctx.ps_info._signo));
    822 			pl.pl_event = PL_EVENT_SIGNAL;
    823 		}
    824 	}
    825 	mutex_exit(t->p_lock);
    826 	DPRINTF(("%s: lwp=%d event=%#x\n", __func__,
    827 	    pl.pl_lwpid, pl.pl_event));
    828 
    829 	return copyout(&pl, addr, sizeof(pl));
    830 }
    831 
    832 static int
    833 ptrace_lwpstatus(struct proc *t, struct ptrace_methods *ptm, struct lwp **lt,
    834     void *addr, size_t data, bool next)
    835 {
    836 	struct ptrace_lwpstatus pls;
    837 	struct lwp *l;
    838 	int error;
    839 
    840 	if (data > sizeof(pls) || data < sizeof(lwpid_t)) {
    841 		DPRINTF(("%s: invalid data: %zu < %zu < %zu\n",
    842 		        __func__, sizeof(lwpid_t), data, sizeof(pls)));
    843 		return EINVAL;
    844 	}
    845 	error = copyin(addr, &pls.pl_lwpid, sizeof(lwpid_t));
    846 	if (error)
    847 		return error;
    848 
    849 	if (next) {
    850 		lwp_delref(*lt);
    851 		lwpid_t tmp = pls.pl_lwpid;
    852 		mutex_enter(t->p_lock);
    853 		if (tmp == 0)
    854 			*lt = lwp_find_first(t);
    855 		else {
    856 			*lt = lwp_find(t, tmp);
    857 			if (*lt == NULL) {
    858 				mutex_exit(t->p_lock);
    859 				return ESRCH;
    860 			}
    861 			*lt = LIST_NEXT(*lt, l_sibling);
    862 		}
    863 
    864 		while (*lt != NULL && (!lwp_alive(*lt) ||
    865 		       ((*lt)->l_flag & LW_SYSTEM) != 0))
    866 			*lt = LIST_NEXT(*lt, l_sibling);
    867 
    868 		if (*lt == NULL) {
    869 			memset(&pls, 0, sizeof(pls));
    870 			mutex_exit(t->p_lock);
    871 			goto out;
    872 		}
    873 		lwp_addref(*lt);
    874 		mutex_exit(t->p_lock);
    875 
    876 		pls.pl_lwpid = (*lt)->l_lid;
    877 	} else {
    878 		if ((error = ptrace_update_lwp(t, lt, pls.pl_lwpid)) != 0)
    879 			return error;
    880 	}
    881 
    882 	l = *lt;
    883 
    884 	ptrace_read_lwpstatus(l, &pls);
    885 
    886 out:
    887 	DPRINTF(("%s: lwp=%d sigpend=%02x%02x%02x%02x sigmask=%02x%02x%02x%02x "
    888 	   "name='%s' private=%p\n", __func__, pls.pl_lwpid,
    889 	    pls.pl_sigpend.__bits[0], pls.pl_sigpend.__bits[1],
    890 	    pls.pl_sigpend.__bits[2], pls.pl_sigpend.__bits[3],
    891 	    pls.pl_sigmask.__bits[0], pls.pl_sigmask.__bits[1],
    892 	    pls.pl_sigmask.__bits[2], pls.pl_sigmask.__bits[3],
    893 	    pls.pl_name, pls.pl_private));
    894 
    895 	return ptm->ptm_copyout_lwpstatus(&pls, addr, data);
    896 }
    897 
    898 static int
    899 ptrace_startstop(struct proc *t, struct lwp **lt, int rq, void *addr,
    900     size_t data)
    901 {
    902 	int error;
    903 
    904 	if ((error = ptrace_update_lwp(t, lt, data)) != 0)
    905 		return error;
    906 
    907 	DPRINTF(("%s: lwp=%d request=%d\n", __func__, (*lt)->l_lid, rq));
    908 	lwp_lock(*lt);
    909 	if (rq == PT_SUSPEND)
    910 		(*lt)->l_flag |= LW_DBGSUSPEND;
    911 	else {
    912 		(*lt)->l_flag &= ~LW_DBGSUSPEND;
    913 		if ((*lt)->l_flag != LSSUSPENDED)
    914 			(*lt)->l_stat = LSSTOP;
    915 	}
    916 	lwp_unlock(*lt);
    917 	return 0;
    918 }
    919 
    920 #ifdef PT_REGISTERS
    921 static int
    922 ptrace_uio_dir(int req)
    923 {
    924 	switch (req) {
    925 	case_PT_GETREGS
    926 	case_PT_GETFPREGS
    927 	case_PT_GETDBREGS
    928 		return UIO_READ;
    929 	case_PT_SETREGS
    930 	case_PT_SETFPREGS
    931 	case_PT_SETDBREGS
    932 		return UIO_WRITE;
    933 	default:
    934 		return -1;
    935 	}
    936 }
    937 
    938 static int
    939 ptrace_regs(struct lwp *l, struct lwp **lt, int rq, struct ptrace_methods *ptm,
    940     void *addr, size_t data)
    941 {
    942 	int error;
    943 	struct proc *p, *t;
    944 	struct vmspace *vm;
    945 
    946 	p = l->l_proc;		/* tracer */
    947 	t = (*lt)->l_proc;	/* traced */
    948 
    949 	if ((error = ptrace_update_lwp(t, lt, data)) != 0)
    950 		return error;
    951 
    952 	int dir = ptrace_uio_dir(rq);
    953 	size_t size;
    954 	int (*func)(struct lwp *, struct lwp *, struct uio *);
    955 
    956 	DPRINTF(("%s: lwp=%d request=%d\n", __func__, l->l_lid, rq));
    957 
    958 	switch (rq) {
    959 #if defined(PT_SETREGS) || defined(PT_GETREGS)
    960 	case_PT_GETREGS
    961 	case_PT_SETREGS
    962 		if (!process_validregs(*lt))
    963 			return EINVAL;
    964 		size = PROC_REGSZ(p);
    965 		func = ptm->ptm_doregs;
    966 		break;
    967 #endif
    968 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
    969 	case_PT_GETFPREGS
    970 	case_PT_SETFPREGS
    971 		if (!process_validfpregs(*lt))
    972 			return EINVAL;
    973 		size = PROC_FPREGSZ(p);
    974 		func = ptm->ptm_dofpregs;
    975 		break;
    976 #endif
    977 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
    978 	case_PT_GETDBREGS
    979 	case_PT_SETDBREGS
    980 		if (!process_validdbregs(*lt))
    981 			return EINVAL;
    982 		size = PROC_DBREGSZ(p);
    983 		func = ptm->ptm_dodbregs;
    984 		break;
    985 #endif
    986 	default:
    987 		return EINVAL;
    988 	}
    989 
    990 	error = proc_vmspace_getref(l->l_proc, &vm);
    991 	if (error)
    992 		return error;
    993 
    994 	struct uio uio;
    995 	struct iovec iov;
    996 
    997 	iov.iov_base = addr;
    998 	iov.iov_len = size;
    999 	uio.uio_iov = &iov;
   1000 	uio.uio_iovcnt = 1;
   1001 	uio.uio_offset = 0;
   1002 	uio.uio_resid = iov.iov_len;
   1003 	uio.uio_rw = dir;
   1004 	uio.uio_vmspace = vm;
   1005 
   1006 	error = (*func)(l, *lt, &uio);
   1007 	uvmspace_free(vm);
   1008 	return error;
   1009 }
   1010 #endif
   1011 
   1012 static int
   1013 ptrace_sendsig(struct lwp *l, int req, struct proc *t, struct lwp *lt, int signo, int resume_all)
   1014 {
   1015 	ksiginfo_t ksi;
   1016 
   1017 	/* Finally, deliver the requested signal (or none). */
   1018 	if (t->p_stat == SSTOP) {
   1019 		/*
   1020 		 * Unstop the process.  If it needs to take a
   1021 		 * signal, make all efforts to ensure that at
   1022 		 * an LWP runs to see it.
   1023 		 */
   1024 		t->p_xsig = signo;
   1025 
   1026 		/*
   1027 		 * signo > 0 check prevents a potential panic, as
   1028 		 * sigismember(&...,0) is invalid check and signo
   1029 		 * can be equal to 0 as a special case of no-signal.
   1030 		 */
   1031 		if (signo > 0 && sigismember(&stopsigmask, signo)) {
   1032 			t->p_waited = 0;
   1033 			child_psignal(t, 0);
   1034 		} else if (resume_all)
   1035 			proc_unstop(t);
   1036 		else
   1037 			lwp_unstop(lt);
   1038 		return 0;
   1039 	}
   1040 
   1041 	KASSERT(req == PT_KILL || req == PT_STOP || req == PT_ATTACH);
   1042 
   1043 	KSI_INIT(&ksi);
   1044 	ksi.ksi_signo = signo;
   1045 	ksi.ksi_code = SI_USER;
   1046 	ksi.ksi_pid = l->l_proc->p_pid;
   1047 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
   1048 
   1049 	t->p_sigctx.ps_faked = false;
   1050 
   1051 	DPRINTF(("%s: pid=%d.%d signal=%d resume_all=%d\n", __func__, t->p_pid,
   1052 	    lt->l_lid, signo, resume_all));
   1053 
   1054 	return kpsignal2(t, &ksi);
   1055 }
   1056 
   1057 static int
   1058 ptrace_dumpcore(struct lwp *lt, char *path, size_t len)
   1059 {
   1060 	int error;
   1061 	if (path != NULL) {
   1062 
   1063 		if (len >= MAXPATHLEN)
   1064 			return EINVAL;
   1065 
   1066 		char *src = path;
   1067 		path = kmem_alloc(len + 1, KM_SLEEP);
   1068 		error = copyin(src, path, len);
   1069 		if (error)
   1070 			goto out;
   1071 		path[len] = '\0';
   1072 	}
   1073 	DPRINTF(("%s: lwp=%d\n", __func__, lt->l_lid));
   1074 	MODULE_HOOK_CALL(coredump_hook, (lt, path), 0, error);
   1075 out:
   1076 	if (path)
   1077 		kmem_free(path, len + 1);
   1078 	return error;
   1079 }
   1080 
   1081 static int
   1082 ptrace_doio(struct lwp *l, struct proc *t, struct lwp *lt,
   1083     struct ptrace_io_desc *piod, void *addr, bool sysspace)
   1084 {
   1085 	struct uio uio;
   1086 	struct iovec iov;
   1087 	int error, tmp;
   1088 
   1089 	error = 0;
   1090 	iov.iov_base = piod->piod_addr;
   1091 	iov.iov_len = piod->piod_len;
   1092 	uio.uio_iov = &iov;
   1093 	uio.uio_iovcnt = 1;
   1094 	uio.uio_offset = (off_t)(unsigned long)piod->piod_offs;
   1095 	uio.uio_resid = piod->piod_len;
   1096 
   1097 	DPRINTF(("%s: lwp=%d request=%d\n", __func__, l->l_lid, piod->piod_op));
   1098 
   1099 	switch (piod->piod_op) {
   1100 	case PIOD_READ_D:
   1101 	case PIOD_READ_I:
   1102 		uio.uio_rw = UIO_READ;
   1103 		break;
   1104 	case PIOD_WRITE_D:
   1105 	case PIOD_WRITE_I:
   1106 		/*
   1107 		 * Can't write to a RAS
   1108 		 */
   1109 		if (ras_lookup(t, addr) != (void *)-1) {
   1110 			return EACCES;
   1111 		}
   1112 		uio.uio_rw = UIO_WRITE;
   1113 		break;
   1114 	case PIOD_READ_AUXV:
   1115 		uio.uio_rw = UIO_READ;
   1116 		tmp = t->p_execsw->es_arglen;
   1117 		if (uio.uio_offset > tmp)
   1118 			return EIO;
   1119 		if (uio.uio_resid > tmp - uio.uio_offset)
   1120 			uio.uio_resid = tmp - uio.uio_offset;
   1121 		piod->piod_len = iov.iov_len = uio.uio_resid;
   1122 		error = process_auxv_offset(t, &uio);
   1123 		break;
   1124 	default:
   1125 		error = EINVAL;
   1126 		break;
   1127 	}
   1128 
   1129 	if (error)
   1130 		return error;
   1131 
   1132 	if (sysspace) {
   1133 		uio.uio_vmspace = vmspace_kernel();
   1134 	} else {
   1135 		error = proc_vmspace_getref(l->l_proc, &uio.uio_vmspace);
   1136 		if (error)
   1137 			return error;
   1138 	}
   1139 
   1140 	error = process_domem(l, lt, &uio);
   1141 	if (!sysspace)
   1142 		uvmspace_free(uio.uio_vmspace);
   1143 	if (error)
   1144 		return error;
   1145 	piod->piod_len -= uio.uio_resid;
   1146 	return 0;
   1147 }
   1148 
   1149 int
   1150 do_ptrace(struct ptrace_methods *ptm, struct lwp *l, int req, pid_t pid,
   1151     void *addr, int data, register_t *retval)
   1152 {
   1153 	struct proc *p = l->l_proc;
   1154 	struct lwp *lt = NULL;
   1155 	struct lwp *lt2;
   1156 	struct proc *t;				/* target process */
   1157 	struct ptrace_io_desc piod;
   1158 	int error, write, tmp, pheld;
   1159 	int signo = 0;
   1160 	int resume_all;
   1161 	bool locked;
   1162 	error = 0;
   1163 
   1164 	/*
   1165 	 * If attaching or detaching, we need to get a write hold on the
   1166 	 * proclist lock so that we can re-parent the target process.
   1167 	 */
   1168 	mutex_enter(&proc_lock);
   1169 
   1170 	t = ptrace_find(l, req, pid);
   1171 	if (t == NULL) {
   1172 		mutex_exit(&proc_lock);
   1173 		return ESRCH;
   1174 	}
   1175 
   1176 	pheld = 1;
   1177 	if ((error = ptrace_allowed(l, req, t, p, &locked)) != 0)
   1178 		goto out;
   1179 
   1180 	if ((error = kauth_authorize_process(l->l_cred,
   1181 	    KAUTH_PROCESS_PTRACE, t, KAUTH_ARG(req), NULL, NULL)) != 0)
   1182 		goto out;
   1183 
   1184 	if ((lt = lwp_find_first(t)) == NULL) {
   1185 	    error = ESRCH;
   1186 	    goto out;
   1187 	}
   1188 
   1189 	/* Do single-step fixup if needed. */
   1190 	FIX_SSTEP(t);
   1191 	KASSERT(lt != NULL);
   1192 	lwp_addref(lt);
   1193 
   1194 	/*
   1195 	 * Which locks do we need held? XXX Ugly.
   1196 	 */
   1197 	if ((pheld = ptrace_needs_hold(req)) == 0) {
   1198 		mutex_exit(t->p_lock);
   1199 		mutex_exit(&proc_lock);
   1200 	}
   1201 
   1202 	/* Now do the operation. */
   1203 	write = 0;
   1204 	*retval = 0;
   1205 	tmp = 0;
   1206 	resume_all = 1;
   1207 
   1208 	switch (req) {
   1209 	case PT_TRACE_ME:
   1210 		/* Just set the trace flag. */
   1211 		SET(t->p_slflag, PSL_TRACED);
   1212 		t->p_opptr = t->p_pptr;
   1213 		break;
   1214 
   1215 	/*
   1216 	 * The I and D separate address space has been inherited from PDP-11.
   1217 	 * The 16-bit UNIX started with a single address space per program,
   1218 	 * but was extended to two 16-bit (2 x 64kb) address spaces.
   1219 	 *
   1220 	 * We no longer maintain this feature in maintained architectures, but
   1221 	 * we keep the API for backward compatiblity. Currently the I and D
   1222 	 * operations are exactly the same and not distinguished in debuggers.
   1223 	 */
   1224 	case PT_WRITE_I:
   1225 	case PT_WRITE_D:
   1226 		write = 1;
   1227 		tmp = data;
   1228 		/* FALLTHROUGH */
   1229 	case PT_READ_I:
   1230 	case PT_READ_D:
   1231 		piod.piod_addr = &tmp;
   1232 		piod.piod_len = sizeof(tmp);
   1233 		piod.piod_offs = addr;
   1234 		piod.piod_op = write ? PIOD_WRITE_D : PIOD_READ_D;
   1235 		if ((error = ptrace_doio(l, t, lt, &piod, addr, true)) != 0)
   1236 			break;
   1237 		/*
   1238 		 * For legacy reasons we treat here two results as success:
   1239 		 *  - incomplete transfer  piod.piod_len < sizeof(tmp)
   1240 		 *  - no transfer          piod.piod_len == 0
   1241 		 *
   1242 		 * This means that there is no way to determine whether
   1243 		 * transfer operation was performed in PT_WRITE and PT_READ
   1244 		 * calls.
   1245 		 */
   1246 		if (!write)
   1247 			*retval = tmp;
   1248 		break;
   1249 
   1250 	case PT_IO:
   1251 		if ((error = ptm->ptm_copyin_piod(&piod, addr, data)) != 0)
   1252 			break;
   1253 		if (piod.piod_len < 1) {
   1254 			error = EINVAL;
   1255 			break;
   1256 		}
   1257 		if ((error = ptrace_doio(l, t, lt, &piod, addr, false)) != 0)
   1258 			break;
   1259 		/*
   1260 		 * For legacy reasons we treat here two results as success:
   1261 		 *  - incomplete transfer  piod.piod_len < sizeof(tmp)
   1262 		 *  - no transfer          piod.piod_len == 0
   1263 		 */
   1264 		error = ptm->ptm_copyout_piod(&piod, addr, data);
   1265 		break;
   1266 
   1267 	case PT_DUMPCORE:
   1268 		error = ptrace_dumpcore(lt, addr, data);
   1269 		break;
   1270 
   1271 #ifdef PT_STEP
   1272 	case PT_STEP:
   1273 		/*
   1274 		 * From the 4.4BSD PRM:
   1275 		 * "Execution continues as in request PT_CONTINUE; however
   1276 		 * as soon as possible after execution of at least one
   1277 		 * instruction, execution stops again. [ ... ]"
   1278 		 */
   1279 #endif
   1280 	case PT_CONTINUE:
   1281 	case PT_SYSCALL:
   1282 	case PT_DETACH:
   1283 		if (req == PT_SYSCALL) {
   1284 			if (!ISSET(t->p_slflag, PSL_SYSCALL)) {
   1285 				SET(t->p_slflag, PSL_SYSCALL);
   1286 #ifdef __HAVE_SYSCALL_INTERN
   1287 				(*t->p_emul->e_syscall_intern)(t);
   1288 #endif
   1289 			}
   1290 		} else {
   1291 			if (ISSET(t->p_slflag, PSL_SYSCALL)) {
   1292 				CLR(t->p_slflag, PSL_SYSCALL);
   1293 #ifdef __HAVE_SYSCALL_INTERN
   1294 				(*t->p_emul->e_syscall_intern)(t);
   1295 #endif
   1296 			}
   1297 		}
   1298 		t->p_trace_enabled = trace_is_enabled(t);
   1299 
   1300 		/*
   1301 		 * Pick up the LWPID, if supplied.  There are two cases:
   1302 		 * data < 0 : step or continue single thread, lwp = -data
   1303 		 * data > 0 in PT_STEP : step this thread, continue others
   1304 		 * For operations other than PT_STEP, data > 0 means
   1305 		 * data is the signo to deliver to the process.
   1306 		 */
   1307 		tmp = data;
   1308 		if (tmp >= 0) {
   1309 #ifdef PT_STEP
   1310 			if (req == PT_STEP)
   1311 				signo = 0;
   1312 			else
   1313 #endif
   1314 			{
   1315 				signo = tmp;
   1316 				tmp = 0;	/* don't search for LWP */
   1317 			}
   1318 		} else if (tmp == INT_MIN) {
   1319 			error = ESRCH;
   1320 			break;
   1321 		} else {
   1322 			tmp = -tmp;
   1323 		}
   1324 
   1325 		if (tmp > 0) {
   1326 			if (req == PT_DETACH) {
   1327 				error = EINVAL;
   1328 				break;
   1329 			}
   1330 			lwp_delref2 (lt);
   1331 			lt = lwp_find(t, tmp);
   1332 			if (lt == NULL) {
   1333 				error = ESRCH;
   1334 				break;
   1335 			}
   1336 			lwp_addref(lt);
   1337 			resume_all = 0;
   1338 			signo = 0;
   1339 		}
   1340 
   1341 		/*
   1342 		 * From the 4.4BSD PRM:
   1343 		 * "The data argument is taken as a signal number and the
   1344 		 * child's execution continues at location addr as if it
   1345 		 * incurred that signal.  Normally the signal number will
   1346 		 * be either 0 to indicate that the signal that caused the
   1347 		 * stop should be ignored, or that value fetched out of
   1348 		 * the process's image indicating which signal caused
   1349 		 * the stop.  If addr is (int *)1 then execution continues
   1350 		 * from where it stopped."
   1351 		 */
   1352 
   1353 		/* Check that the data is a valid signal number or zero. */
   1354 		if (signo < 0 || signo >= NSIG) {
   1355 			error = EINVAL;
   1356 			break;
   1357 		}
   1358 
   1359 		/* Prevent process deadlock */
   1360 		if (resume_all) {
   1361 #ifdef PT_STEP
   1362 			if (req == PT_STEP) {
   1363 				if (lt->l_flag &
   1364 				    (LW_WSUSPEND | LW_DBGSUSPEND)) {
   1365 					error = EDEADLK;
   1366 					break;
   1367 				}
   1368 			} else
   1369 #endif
   1370 			{
   1371 				error = EDEADLK;
   1372 				LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
   1373 					if ((lt2->l_flag &
   1374 					    (LW_WSUSPEND | LW_DBGSUSPEND)) == 0
   1375 					    ) {
   1376 						error = 0;
   1377 						break;
   1378 					}
   1379 				}
   1380 				if (error != 0)
   1381 					break;
   1382 			}
   1383 		} else {
   1384 			if (lt->l_flag & (LW_WSUSPEND | LW_DBGSUSPEND)) {
   1385 				error = EDEADLK;
   1386 				break;
   1387 			}
   1388 		}
   1389 
   1390 		/*
   1391 		 * Reject setting program counter to 0x0 if VA0 is disabled.
   1392 		 *
   1393 		 * Not all kernels implement this feature to set Program
   1394 		 * Counter in one go in PT_CONTINUE and similar operations.
   1395 		 * This causes portability issues as passing address 0x0
   1396 		 * on these kernels is no-operation, but can cause failure
   1397 		 * in most cases on NetBSD.
   1398 		 */
   1399 		if (user_va0_disable && addr == 0) {
   1400 			error = EINVAL;
   1401 			break;
   1402 		}
   1403 
   1404 		/* If the address parameter is not (int *)1, set the pc. */
   1405 		if ((int *)addr != (int *)1) {
   1406 			error = process_set_pc(lt, addr);
   1407 			if (error != 0)
   1408 				break;
   1409 		}
   1410 #ifdef PT_STEP
   1411 		/*
   1412 		 * Arrange for a single-step, if that's requested and possible.
   1413 		 * More precisely, set the single step status as requested for
   1414 		 * the requested thread, and clear it for other threads.
   1415 		 */
   1416 		LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
   1417 			error = process_sstep(lt2,
   1418 			    ISSET(lt2->l_pflag, LP_SINGLESTEP));
   1419 			if (error)
   1420 				break;
   1421 		}
   1422 		if (error)
   1423 			break;
   1424 		error = process_sstep(lt,
   1425 		    ISSET(lt->l_pflag, LP_SINGLESTEP) || req == PT_STEP);
   1426 		if (error)
   1427 			break;
   1428 #endif
   1429 		if (req == PT_DETACH) {
   1430 			CLR(t->p_slflag,
   1431 			    PSL_TRACED|PSL_TRACEDCHILD|PSL_SYSCALL);
   1432 
   1433 			/* clear sigpass mask */
   1434 			sigemptyset(&t->p_sigctx.ps_sigpass);
   1435 
   1436 			/* give process back to original parent or init */
   1437 			if (t->p_opptr != t->p_pptr) {
   1438 				struct proc *pp = t->p_opptr;
   1439 				proc_reparent(t, pp ? pp : initproc);
   1440 			}
   1441 
   1442 			/* not being traced any more */
   1443 			t->p_opptr = NULL;
   1444 
   1445 			/* clear single step */
   1446 			LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
   1447 				CLR(lt2->l_pflag, LP_SINGLESTEP);
   1448 			}
   1449 			CLR(lt->l_pflag, LP_SINGLESTEP);
   1450 		}
   1451 	sendsig:
   1452 		error = ptrace_sendsig(l, req, t, lt, signo, resume_all);
   1453 		break;
   1454 
   1455 	case PT_SYSCALLEMU:
   1456 		if (!ISSET(t->p_slflag, PSL_SYSCALL) || t->p_stat != SSTOP) {
   1457 			error = EINVAL;
   1458 			break;
   1459 		}
   1460 		SET(t->p_slflag, PSL_SYSCALLEMU);
   1461 		break;
   1462 
   1463 #ifdef PT_STEP
   1464 	case PT_SETSTEP:
   1465 		write = 1;
   1466 
   1467 		/* FALLTHROUGH */
   1468 	case PT_CLEARSTEP:
   1469 		/* write = 0 done above. */
   1470 		if ((error = ptrace_update_lwp(t, &lt, data)) != 0)
   1471 			break;
   1472 
   1473 		if (write)
   1474 			SET(lt->l_pflag, LP_SINGLESTEP);
   1475 		else
   1476 			CLR(lt->l_pflag, LP_SINGLESTEP);
   1477 		break;
   1478 #endif
   1479 
   1480 	case PT_KILL:
   1481 		/* just send the process a KILL signal. */
   1482 		signo = SIGKILL;
   1483 		goto sendsig;	/* in PT_CONTINUE, above. */
   1484 
   1485 	case PT_STOP:
   1486 		/* just send the process a STOP signal. */
   1487 		signo = SIGSTOP;
   1488 		goto sendsig;	/* in PT_CONTINUE, above. */
   1489 
   1490 	case PT_ATTACH:
   1491 		/*
   1492 		 * Go ahead and set the trace flag.
   1493 		 * Save the old parent (it's reset in
   1494 		 *   _DETACH, and also in kern_exit.c:wait4()
   1495 		 * Reparent the process so that the tracing
   1496 		 *   proc gets to see all the action.
   1497 		 * Stop the target.
   1498 		 */
   1499 		proc_changeparent(t, p);
   1500 		signo = SIGSTOP;
   1501 		goto sendsig;
   1502 
   1503 	case PT_GET_EVENT_MASK:
   1504 		error = ptrace_get_event_mask(t, addr, data);
   1505 		break;
   1506 
   1507 	case PT_SET_EVENT_MASK:
   1508 		error = ptrace_set_event_mask(t, addr, data);
   1509 		break;
   1510 
   1511 	case PT_GET_PROCESS_STATE:
   1512 		error = ptrace_get_process_state(t, addr, data);
   1513 		break;
   1514 
   1515 	case PT_LWPINFO:
   1516 		error = ptrace_lwpinfo(t, &lt, addr, data);
   1517 		break;
   1518 
   1519 	case PT_SET_SIGINFO:
   1520 		error = ptrace_set_siginfo(t, &lt, ptm, addr, data);
   1521 		break;
   1522 
   1523 	case PT_GET_SIGINFO:
   1524 		error = ptrace_get_siginfo(t, ptm, addr, data);
   1525 		break;
   1526 
   1527 	case PT_RESUME:
   1528 	case PT_SUSPEND:
   1529 		error = ptrace_startstop(t, &lt, req, addr, data);
   1530 		break;
   1531 
   1532 	case PT_LWPSTATUS:
   1533 		error = ptrace_lwpstatus(t, ptm, &lt, addr, data, false);
   1534 		break;
   1535 
   1536 	case PT_LWPNEXT:
   1537 		error = ptrace_lwpstatus(t, ptm, &lt, addr, data, true);
   1538 		break;
   1539 
   1540 	case PT_SET_SIGPASS:
   1541 		error = ptrace_set_sigpass(t, addr, data);
   1542 		break;
   1543 
   1544 	case PT_GET_SIGPASS:
   1545 		error = ptrace_get_sigpass(t, addr, data);
   1546 		break;
   1547 
   1548 #ifdef PT_REGISTERS
   1549 	case_PT_SETREGS
   1550 	case_PT_GETREGS
   1551 	case_PT_SETFPREGS
   1552 	case_PT_GETFPREGS
   1553 	case_PT_SETDBREGS
   1554 	case_PT_GETDBREGS
   1555 		error = ptrace_regs(l, &lt, req, ptm, addr, data);
   1556 		break;
   1557 #endif
   1558 
   1559 #ifdef __HAVE_PTRACE_MACHDEP
   1560 	PTRACE_MACHDEP_REQUEST_CASES
   1561 		error = ptrace_machdep_dorequest(l, &lt, req, addr, data);
   1562 		break;
   1563 #endif
   1564 	}
   1565 
   1566 out:
   1567 	if (pheld) {
   1568 		mutex_exit(t->p_lock);
   1569 		mutex_exit(&proc_lock);
   1570 	}
   1571 	if (lt != NULL)
   1572 		lwp_delref(lt);
   1573 	if (locked)
   1574 		rw_exit(&t->p_reflock);
   1575 
   1576 	return error;
   1577 }
   1578 
   1579 typedef int (*regrfunc_t)(struct lwp *, void *, size_t *);
   1580 typedef int (*regwfunc_t)(struct lwp *, void *, size_t);
   1581 
   1582 #ifdef PT_REGISTERS
   1583 static int
   1584 proc_regio(struct lwp *l, struct uio *uio, size_t ks, regrfunc_t r,
   1585     regwfunc_t w)
   1586 {
   1587 	char buf[1024] PTRACE_REGS_ALIGN;
   1588 	int error;
   1589 	char *kv;
   1590 	size_t kl;
   1591 
   1592 	if (ks > sizeof(buf))
   1593 		return E2BIG;
   1594 
   1595 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)ks)
   1596 		return EINVAL;
   1597 
   1598 	kv = buf + uio->uio_offset;
   1599 	kl = ks - uio->uio_offset;
   1600 
   1601 	if (kl > uio->uio_resid)
   1602 		kl = uio->uio_resid;
   1603 
   1604 	error = (*r)(l, buf, &ks);
   1605 	if (error == 0)
   1606 		error = uiomove(kv, kl, uio);
   1607 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
   1608 		if (l->l_stat != LSSTOP)
   1609 			error = EBUSY;
   1610 		else
   1611 			error = (*w)(l, buf, ks);
   1612 	}
   1613 
   1614 	uio->uio_offset = 0;
   1615 	return error;
   1616 }
   1617 #endif
   1618 
   1619 int
   1620 process_doregs(struct lwp *curl /*tracer*/,
   1621     struct lwp *l /*traced*/,
   1622     struct uio *uio)
   1623 {
   1624 #if defined(PT_GETREGS) || defined(PT_SETREGS)
   1625 	size_t s;
   1626 	regrfunc_t r;
   1627 	regwfunc_t w;
   1628 
   1629 #ifdef COMPAT_NETBSD32
   1630 	const bool pk32 = (curl->l_proc->p_flag & PK_32) != 0;
   1631 
   1632 	if (__predict_false(pk32)) {
   1633 		if ((l->l_proc->p_flag & PK_32) == 0) {
   1634 			// 32 bit tracer can't trace 64 bit process
   1635 			return EINVAL;
   1636 		}
   1637 		s = sizeof(process_reg32);
   1638 		r = __FPTRCAST(regrfunc_t, process_read_regs32);
   1639 		w = __FPTRCAST(regwfunc_t, process_write_regs32);
   1640 	} else
   1641 #endif
   1642 	{
   1643 		s = sizeof(struct reg);
   1644 		r = __FPTRCAST(regrfunc_t, process_read_regs);
   1645 		w = __FPTRCAST(regwfunc_t, process_write_regs);
   1646 	}
   1647 	return proc_regio(l, uio, s, r, w);
   1648 #else
   1649 	return EINVAL;
   1650 #endif
   1651 }
   1652 
   1653 int
   1654 process_validregs(struct lwp *l)
   1655 {
   1656 
   1657 #if defined(PT_SETREGS) || defined(PT_GETREGS)
   1658 	return (l->l_flag & LW_SYSTEM) == 0;
   1659 #else
   1660 	return 0;
   1661 #endif
   1662 }
   1663 
   1664 int
   1665 process_dofpregs(struct lwp *curl /*tracer*/,
   1666     struct lwp *l /*traced*/,
   1667     struct uio *uio)
   1668 {
   1669 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
   1670 	size_t s;
   1671 	regrfunc_t r;
   1672 	regwfunc_t w;
   1673 
   1674 #ifdef COMPAT_NETBSD32
   1675 	const bool pk32 = (curl->l_proc->p_flag & PK_32) != 0;
   1676 
   1677 	if (__predict_false(pk32)) {
   1678 		if ((l->l_proc->p_flag & PK_32) == 0) {
   1679 			// 32 bit tracer can't trace 64 bit process
   1680 			return EINVAL;
   1681 		}
   1682 		s = sizeof(process_fpreg32);
   1683 		r = (regrfunc_t)process_read_fpregs32;
   1684 		w = (regwfunc_t)process_write_fpregs32;
   1685 	} else
   1686 #endif
   1687 	{
   1688 		s = sizeof(struct fpreg);
   1689 		r = (regrfunc_t)process_read_fpregs;
   1690 		w = (regwfunc_t)process_write_fpregs;
   1691 	}
   1692 	return proc_regio(l, uio, s, r, w);
   1693 #else
   1694 	return EINVAL;
   1695 #endif
   1696 }
   1697 
   1698 int
   1699 process_validfpregs(struct lwp *l)
   1700 {
   1701 
   1702 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
   1703 	return (l->l_flag & LW_SYSTEM) == 0;
   1704 #else
   1705 	return 0;
   1706 #endif
   1707 }
   1708 
   1709 int
   1710 process_dodbregs(struct lwp *curl /*tracer*/,
   1711     struct lwp *l /*traced*/,
   1712     struct uio *uio)
   1713 {
   1714 #if defined(PT_GETDBREGS) || defined(PT_SETDBREGS)
   1715 	size_t s;
   1716 	regrfunc_t r;
   1717 	regwfunc_t w;
   1718 
   1719 #ifdef COMPAT_NETBSD32
   1720 	const bool pk32 = (curl->l_proc->p_flag & PK_32) != 0;
   1721 
   1722 	if (__predict_false(pk32)) {
   1723 		if ((l->l_proc->p_flag & PK_32) == 0) {
   1724 			// 32 bit tracer can't trace 64 bit process
   1725 			return EINVAL;
   1726 		}
   1727 		s = sizeof(process_dbreg32);
   1728 		r = (regrfunc_t)process_read_dbregs32;
   1729 		w = (regwfunc_t)process_write_dbregs32;
   1730 	} else
   1731 #endif
   1732 	{
   1733 		s = sizeof(struct dbreg);
   1734 		r = (regrfunc_t)process_read_dbregs;
   1735 		w = (regwfunc_t)process_write_dbregs;
   1736 	}
   1737 	return proc_regio(l, uio, s, r, w);
   1738 #else
   1739 	return EINVAL;
   1740 #endif
   1741 }
   1742 
   1743 int
   1744 process_validdbregs(struct lwp *l)
   1745 {
   1746 
   1747 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
   1748 	return (l->l_flag & LW_SYSTEM) == 0;
   1749 #else
   1750 	return 0;
   1751 #endif
   1752 }
   1753 
   1754 static int
   1755 process_auxv_offset(struct proc *p, struct uio *uio)
   1756 {
   1757 	struct ps_strings pss;
   1758 	int error;
   1759 	off_t off = (off_t)p->p_psstrp;
   1760 
   1761 	if ((error = copyin_psstrings(p, &pss)) != 0)
   1762 		return error;
   1763 
   1764 	if (pss.ps_envstr == NULL)
   1765 		return EIO;
   1766 
   1767 #ifdef COMPAT_NETBSD32
   1768 	if (p->p_flag & PK_32)
   1769 		uio->uio_offset += (off_t)((vaddr_t)pss.ps_envstr +
   1770 		    sizeof(uint32_t) * (pss.ps_nenvstr + 1));
   1771 	else
   1772 #endif
   1773 		uio->uio_offset += (off_t)(vaddr_t)(pss.ps_envstr +
   1774 		    pss.ps_nenvstr + 1);
   1775 
   1776 #ifdef __MACHINE_STACK_GROWS_UP
   1777 	if (uio->uio_offset < off)
   1778 		return EIO;
   1779 #else
   1780 	if (uio->uio_offset > off)
   1781 		return EIO;
   1782 	if ((uio->uio_offset + uio->uio_resid) > off)
   1783 		uio->uio_resid = off - uio->uio_offset;
   1784 #endif
   1785 	return 0;
   1786 }
   1787 #endif /* PTRACE */
   1788