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