xref: /freebsd-14.2/sys/kern/sys_process.c (revision 32b3b010)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1994, Sean Eric Fagan
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Sean Eric Fagan.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/ktr.h>
38 #include <sys/limits.h>
39 #include <sys/lock.h>
40 #include <sys/mman.h>
41 #include <sys/mutex.h>
42 #include <sys/reg.h>
43 #include <sys/syscallsubr.h>
44 #include <sys/sysent.h>
45 #include <sys/sysproto.h>
46 #include <sys/priv.h>
47 #include <sys/proc.h>
48 #include <sys/vnode.h>
49 #include <sys/ptrace.h>
50 #include <sys/rwlock.h>
51 #include <sys/sx.h>
52 #include <sys/malloc.h>
53 #include <sys/signalvar.h>
54 #include <sys/caprights.h>
55 #include <sys/filedesc.h>
56 
57 #include <security/audit/audit.h>
58 
59 #include <vm/vm.h>
60 #include <vm/pmap.h>
61 #include <vm/vm_extern.h>
62 #include <vm/vm_map.h>
63 #include <vm/vm_kern.h>
64 #include <vm/vm_object.h>
65 #include <vm/vm_page.h>
66 #include <vm/vm_param.h>
67 
68 #ifdef COMPAT_FREEBSD32
69 #include <sys/procfs.h>
70 #endif
71 
72 /* Assert it's safe to unlock a process, e.g. to allocate working memory */
73 #define	PROC_ASSERT_TRACEREQ(p)	MPASS(((p)->p_flag2 & P2_PTRACEREQ) != 0)
74 
75 /*
76  * Functions implemented using PROC_ACTION():
77  *
78  * proc_read_regs(proc, regs)
79  *	Get the current user-visible register set from the process
80  *	and copy it into the regs structure (<machine/reg.h>).
81  *	The process is stopped at the time read_regs is called.
82  *
83  * proc_write_regs(proc, regs)
84  *	Update the current register set from the passed in regs
85  *	structure.  Take care to avoid clobbering special CPU
86  *	registers or privileged bits in the PSL.
87  *	Depending on the architecture this may have fix-up work to do,
88  *	especially if the IAR or PCW are modified.
89  *	The process is stopped at the time write_regs is called.
90  *
91  * proc_read_fpregs, proc_write_fpregs
92  *	deal with the floating point register set, otherwise as above.
93  *
94  * proc_read_dbregs, proc_write_dbregs
95  *	deal with the processor debug register set, otherwise as above.
96  *
97  * proc_sstep(proc)
98  *	Arrange for the process to trap after executing a single instruction.
99  */
100 
101 #define	PROC_ACTION(action) do {					\
102 	int error;							\
103 									\
104 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);			\
105 	if ((td->td_proc->p_flag & P_INMEM) == 0)			\
106 		error = EIO;						\
107 	else								\
108 		error = (action);					\
109 	return (error);							\
110 } while (0)
111 
112 int
proc_read_regs(struct thread * td,struct reg * regs)113 proc_read_regs(struct thread *td, struct reg *regs)
114 {
115 
116 	PROC_ACTION(fill_regs(td, regs));
117 }
118 
119 int
proc_write_regs(struct thread * td,struct reg * regs)120 proc_write_regs(struct thread *td, struct reg *regs)
121 {
122 
123 	PROC_ACTION(set_regs(td, regs));
124 }
125 
126 int
proc_read_dbregs(struct thread * td,struct dbreg * dbregs)127 proc_read_dbregs(struct thread *td, struct dbreg *dbregs)
128 {
129 
130 	PROC_ACTION(fill_dbregs(td, dbregs));
131 }
132 
133 int
proc_write_dbregs(struct thread * td,struct dbreg * dbregs)134 proc_write_dbregs(struct thread *td, struct dbreg *dbregs)
135 {
136 
137 	PROC_ACTION(set_dbregs(td, dbregs));
138 }
139 
140 /*
141  * Ptrace doesn't support fpregs at all, and there are no security holes
142  * or translations for fpregs, so we can just copy them.
143  */
144 int
proc_read_fpregs(struct thread * td,struct fpreg * fpregs)145 proc_read_fpregs(struct thread *td, struct fpreg *fpregs)
146 {
147 
148 	PROC_ACTION(fill_fpregs(td, fpregs));
149 }
150 
151 int
proc_write_fpregs(struct thread * td,struct fpreg * fpregs)152 proc_write_fpregs(struct thread *td, struct fpreg *fpregs)
153 {
154 
155 	PROC_ACTION(set_fpregs(td, fpregs));
156 }
157 
158 static struct regset *
proc_find_regset(struct thread * td,int note)159 proc_find_regset(struct thread *td, int note)
160 {
161 	struct regset **regsetp, **regset_end, *regset;
162 	struct sysentvec *sv;
163 
164 	sv = td->td_proc->p_sysent;
165 	regsetp = sv->sv_regset_begin;
166 	if (regsetp == NULL)
167 		return (NULL);
168 	regset_end = sv->sv_regset_end;
169 	MPASS(regset_end != NULL);
170 	for (; regsetp < regset_end; regsetp++) {
171 		regset = *regsetp;
172 		if (regset->note != note)
173 			continue;
174 
175 		return (regset);
176 	}
177 
178 	return (NULL);
179 }
180 
181 static int
proc_read_regset(struct thread * td,int note,struct iovec * iov)182 proc_read_regset(struct thread *td, int note, struct iovec *iov)
183 {
184 	struct regset *regset;
185 	struct proc *p;
186 	void *buf;
187 	size_t size;
188 	int error;
189 
190 	regset = proc_find_regset(td, note);
191 	if (regset == NULL)
192 		return (EINVAL);
193 
194 	if (regset->get == NULL)
195 		return (EINVAL);
196 
197 	size = regset->size;
198 	/*
199 	 * The regset is dynamically sized, e.g. the size could change
200 	 * depending on the hardware, or may have a per-thread size.
201 	 */
202 	if (size == 0) {
203 		if (!regset->get(regset, td, NULL, &size))
204 			return (EINVAL);
205 	}
206 
207 	if (iov->iov_base == NULL) {
208 		iov->iov_len = size;
209 		if (iov->iov_len == 0)
210 			return (EINVAL);
211 
212 		return (0);
213 	}
214 
215 	/* The length is wrong, return an error */
216 	if (iov->iov_len != size)
217 		return (EINVAL);
218 
219 	error = 0;
220 	p = td->td_proc;
221 
222 	/* Drop the proc lock while allocating the temp buffer */
223 	PROC_ASSERT_TRACEREQ(p);
224 	PROC_UNLOCK(p);
225 	buf = malloc(size, M_TEMP, M_WAITOK);
226 	PROC_LOCK(p);
227 
228 	if (!regset->get(regset, td, buf, &size)) {
229 		error = EINVAL;
230 	} else {
231 		KASSERT(size == regset->size || regset->size == 0,
232 		    ("%s: Getter function changed the size", __func__));
233 
234 		iov->iov_len = size;
235 		PROC_UNLOCK(p);
236 		error = copyout(buf, iov->iov_base, size);
237 		PROC_LOCK(p);
238 	}
239 
240 	free(buf, M_TEMP);
241 
242 	return (error);
243 }
244 
245 static int
proc_write_regset(struct thread * td,int note,struct iovec * iov)246 proc_write_regset(struct thread *td, int note, struct iovec *iov)
247 {
248 	struct regset *regset;
249 	struct proc *p;
250 	void *buf;
251 	size_t size;
252 	int error;
253 
254 	regset = proc_find_regset(td, note);
255 	if (regset == NULL)
256 		return (EINVAL);
257 
258 	size = regset->size;
259 	/*
260 	 * The regset is dynamically sized, e.g. the size could change
261 	 * depending on the hardware, or may have a per-thread size.
262 	 */
263 	if (size == 0) {
264 		if (!regset->get(regset, td, NULL, &size))
265 			return (EINVAL);
266 	}
267 
268 	/* The length is wrong, return an error */
269 	if (iov->iov_len != size)
270 		return (EINVAL);
271 
272 	if (regset->set == NULL)
273 		return (EINVAL);
274 
275 	p = td->td_proc;
276 
277 	/* Drop the proc lock while allocating the temp buffer */
278 	PROC_ASSERT_TRACEREQ(p);
279 	PROC_UNLOCK(p);
280 	buf = malloc(size, M_TEMP, M_WAITOK);
281 	error = copyin(iov->iov_base, buf, size);
282 	PROC_LOCK(p);
283 
284 	if (error == 0) {
285 		if (!regset->set(regset, td, buf, size)) {
286 			error = EINVAL;
287 		}
288 	}
289 
290 	free(buf, M_TEMP);
291 
292 	return (error);
293 }
294 
295 #ifdef COMPAT_FREEBSD32
296 /* For 32 bit binaries, we need to expose the 32 bit regs layouts. */
297 int
proc_read_regs32(struct thread * td,struct reg32 * regs32)298 proc_read_regs32(struct thread *td, struct reg32 *regs32)
299 {
300 
301 	PROC_ACTION(fill_regs32(td, regs32));
302 }
303 
304 int
proc_write_regs32(struct thread * td,struct reg32 * regs32)305 proc_write_regs32(struct thread *td, struct reg32 *regs32)
306 {
307 
308 	PROC_ACTION(set_regs32(td, regs32));
309 }
310 
311 int
proc_read_dbregs32(struct thread * td,struct dbreg32 * dbregs32)312 proc_read_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
313 {
314 
315 	PROC_ACTION(fill_dbregs32(td, dbregs32));
316 }
317 
318 int
proc_write_dbregs32(struct thread * td,struct dbreg32 * dbregs32)319 proc_write_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
320 {
321 
322 	PROC_ACTION(set_dbregs32(td, dbregs32));
323 }
324 
325 int
proc_read_fpregs32(struct thread * td,struct fpreg32 * fpregs32)326 proc_read_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
327 {
328 
329 	PROC_ACTION(fill_fpregs32(td, fpregs32));
330 }
331 
332 int
proc_write_fpregs32(struct thread * td,struct fpreg32 * fpregs32)333 proc_write_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
334 {
335 
336 	PROC_ACTION(set_fpregs32(td, fpregs32));
337 }
338 #endif
339 
340 int
proc_sstep(struct thread * td)341 proc_sstep(struct thread *td)
342 {
343 
344 	PROC_ACTION(ptrace_single_step(td));
345 }
346 
347 int
proc_rwmem(struct proc * p,struct uio * uio)348 proc_rwmem(struct proc *p, struct uio *uio)
349 {
350 	vm_map_t map;
351 	vm_offset_t pageno;		/* page number */
352 	vm_prot_t reqprot;
353 	int error, fault_flags, page_offset, writing;
354 
355 	/*
356 	 * Make sure that the process' vmspace remains live.
357 	 */
358 	if (p != curproc)
359 		PROC_ASSERT_HELD(p);
360 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
361 
362 	/*
363 	 * The map we want...
364 	 */
365 	map = &p->p_vmspace->vm_map;
366 
367 	/*
368 	 * If we are writing, then we request vm_fault() to create a private
369 	 * copy of each page.  Since these copies will not be writeable by the
370 	 * process, we must explicity request that they be dirtied.
371 	 */
372 	writing = uio->uio_rw == UIO_WRITE;
373 	reqprot = writing ? VM_PROT_COPY | VM_PROT_READ : VM_PROT_READ;
374 	fault_flags = writing ? VM_FAULT_DIRTY : VM_FAULT_NORMAL;
375 
376 	/*
377 	 * Only map in one page at a time.  We don't have to, but it
378 	 * makes things easier.  This way is trivial - right?
379 	 */
380 	do {
381 		vm_offset_t uva;
382 		u_int len;
383 		vm_page_t m;
384 
385 		uva = (vm_offset_t)uio->uio_offset;
386 
387 		/*
388 		 * Get the page number of this segment.
389 		 */
390 		pageno = trunc_page(uva);
391 		page_offset = uva - pageno;
392 
393 		/*
394 		 * How many bytes to copy
395 		 */
396 		len = min(PAGE_SIZE - page_offset, uio->uio_resid);
397 
398 		/*
399 		 * Fault and hold the page on behalf of the process.
400 		 */
401 		error = vm_fault(map, pageno, reqprot, fault_flags, &m);
402 		if (error != KERN_SUCCESS) {
403 			if (error == KERN_RESOURCE_SHORTAGE)
404 				error = ENOMEM;
405 			else
406 				error = EFAULT;
407 			break;
408 		}
409 
410 		/*
411 		 * Now do the i/o move.
412 		 */
413 		error = uiomove_fromphys(&m, page_offset, len, uio);
414 
415 		/* Make the I-cache coherent for breakpoints. */
416 		if (writing && error == 0) {
417 			vm_map_lock_read(map);
418 			if (vm_map_check_protection(map, pageno, pageno +
419 			    PAGE_SIZE, VM_PROT_EXECUTE))
420 				vm_sync_icache(map, uva, len);
421 			vm_map_unlock_read(map);
422 		}
423 
424 		/*
425 		 * Release the page.
426 		 */
427 		vm_page_unwire(m, PQ_ACTIVE);
428 
429 	} while (error == 0 && uio->uio_resid > 0);
430 
431 	return (error);
432 }
433 
434 static ssize_t
proc_iop(struct thread * td,struct proc * p,vm_offset_t va,void * buf,size_t len,enum uio_rw rw)435 proc_iop(struct thread *td, struct proc *p, vm_offset_t va, void *buf,
436     size_t len, enum uio_rw rw)
437 {
438 	struct iovec iov;
439 	struct uio uio;
440 	ssize_t slen;
441 
442 	MPASS(len < SSIZE_MAX);
443 	slen = (ssize_t)len;
444 
445 	iov.iov_base = (caddr_t)buf;
446 	iov.iov_len = len;
447 	uio.uio_iov = &iov;
448 	uio.uio_iovcnt = 1;
449 	uio.uio_offset = va;
450 	uio.uio_resid = slen;
451 	uio.uio_segflg = UIO_SYSSPACE;
452 	uio.uio_rw = rw;
453 	uio.uio_td = td;
454 	proc_rwmem(p, &uio);
455 	if (uio.uio_resid == slen)
456 		return (-1);
457 	return (slen - uio.uio_resid);
458 }
459 
460 ssize_t
proc_readmem(struct thread * td,struct proc * p,vm_offset_t va,void * buf,size_t len)461 proc_readmem(struct thread *td, struct proc *p, vm_offset_t va, void *buf,
462     size_t len)
463 {
464 
465 	return (proc_iop(td, p, va, buf, len, UIO_READ));
466 }
467 
468 ssize_t
proc_writemem(struct thread * td,struct proc * p,vm_offset_t va,void * buf,size_t len)469 proc_writemem(struct thread *td, struct proc *p, vm_offset_t va, void *buf,
470     size_t len)
471 {
472 
473 	return (proc_iop(td, p, va, buf, len, UIO_WRITE));
474 }
475 
476 static int
ptrace_vm_entry(struct thread * td,struct proc * p,struct ptrace_vm_entry * pve)477 ptrace_vm_entry(struct thread *td, struct proc *p, struct ptrace_vm_entry *pve)
478 {
479 	struct vattr vattr;
480 	vm_map_t map;
481 	vm_map_entry_t entry;
482 	vm_object_t obj, tobj, lobj;
483 	struct vmspace *vm;
484 	struct vnode *vp;
485 	char *freepath, *fullpath;
486 	u_int pathlen;
487 	int error, index;
488 
489 	error = 0;
490 	obj = NULL;
491 
492 	vm = vmspace_acquire_ref(p);
493 	map = &vm->vm_map;
494 	vm_map_lock_read(map);
495 
496 	do {
497 		KASSERT((map->header.eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
498 		    ("Submap in map header"));
499 		index = 0;
500 		VM_MAP_ENTRY_FOREACH(entry, map) {
501 			if (index >= pve->pve_entry &&
502 			    (entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
503 				break;
504 			index++;
505 		}
506 		if (index < pve->pve_entry) {
507 			error = EINVAL;
508 			break;
509 		}
510 		if (entry == &map->header) {
511 			error = ENOENT;
512 			break;
513 		}
514 
515 		/* We got an entry. */
516 		pve->pve_entry = index + 1;
517 		pve->pve_timestamp = map->timestamp;
518 		pve->pve_start = entry->start;
519 		pve->pve_end = entry->end - 1;
520 		pve->pve_offset = entry->offset;
521 		pve->pve_prot = entry->protection |
522 		    PROT_MAX(entry->max_protection);
523 
524 		/* Backing object's path needed? */
525 		if (pve->pve_pathlen == 0)
526 			break;
527 
528 		pathlen = pve->pve_pathlen;
529 		pve->pve_pathlen = 0;
530 
531 		obj = entry->object.vm_object;
532 		if (obj != NULL)
533 			VM_OBJECT_RLOCK(obj);
534 	} while (0);
535 
536 	vm_map_unlock_read(map);
537 
538 	pve->pve_fsid = VNOVAL;
539 	pve->pve_fileid = VNOVAL;
540 
541 	if (error == 0 && obj != NULL) {
542 		lobj = obj;
543 		for (tobj = obj; tobj != NULL; tobj = tobj->backing_object) {
544 			if (tobj != obj)
545 				VM_OBJECT_RLOCK(tobj);
546 			if (lobj != obj)
547 				VM_OBJECT_RUNLOCK(lobj);
548 			lobj = tobj;
549 			pve->pve_offset += tobj->backing_object_offset;
550 		}
551 		vp = vm_object_vnode(lobj);
552 		if (vp != NULL)
553 			vref(vp);
554 		if (lobj != obj)
555 			VM_OBJECT_RUNLOCK(lobj);
556 		VM_OBJECT_RUNLOCK(obj);
557 
558 		if (vp != NULL) {
559 			freepath = NULL;
560 			fullpath = NULL;
561 			vn_fullpath(vp, &fullpath, &freepath);
562 			vn_lock(vp, LK_SHARED | LK_RETRY);
563 			if (VOP_GETATTR(vp, &vattr, td->td_ucred) == 0) {
564 				pve->pve_fileid = vattr.va_fileid;
565 				pve->pve_fsid = vattr.va_fsid;
566 			}
567 			vput(vp);
568 
569 			if (fullpath != NULL) {
570 				pve->pve_pathlen = strlen(fullpath) + 1;
571 				if (pve->pve_pathlen <= pathlen) {
572 					error = copyout(fullpath, pve->pve_path,
573 					    pve->pve_pathlen);
574 				} else
575 					error = ENAMETOOLONG;
576 			}
577 			if (freepath != NULL)
578 				free(freepath, M_TEMP);
579 		}
580 	}
581 	vmspace_free(vm);
582 	if (error == 0)
583 		CTR3(KTR_PTRACE, "PT_VM_ENTRY: pid %d, entry %d, start %p",
584 		    p->p_pid, pve->pve_entry, pve->pve_start);
585 
586 	return (error);
587 }
588 
589 /*
590  * Process debugging system call.
591  */
592 #ifndef _SYS_SYSPROTO_H_
593 struct ptrace_args {
594 	int	req;
595 	pid_t	pid;
596 	caddr_t	addr;
597 	int	data;
598 };
599 #endif
600 
601 int
sys_ptrace(struct thread * td,struct ptrace_args * uap)602 sys_ptrace(struct thread *td, struct ptrace_args *uap)
603 {
604 	/*
605 	 * XXX this obfuscation is to reduce stack usage, but the register
606 	 * structs may be too large to put on the stack anyway.
607 	 */
608 	union {
609 		struct ptrace_io_desc piod;
610 		struct ptrace_lwpinfo pl;
611 		struct ptrace_vm_entry pve;
612 		struct ptrace_coredump pc;
613 		struct ptrace_sc_remote sr;
614 		struct dbreg dbreg;
615 		struct fpreg fpreg;
616 		struct reg reg;
617 		struct iovec vec;
618 		syscallarg_t args[nitems(td->td_sa.args)];
619 		struct ptrace_sc_ret psr;
620 		int ptevents;
621 	} r;
622 	syscallarg_t pscr_args[nitems(td->td_sa.args)];
623 	void *addr;
624 	int error;
625 
626 	if (!allow_ptrace)
627 		return (ENOSYS);
628 	error = 0;
629 
630 	AUDIT_ARG_PID(uap->pid);
631 	AUDIT_ARG_CMD(uap->req);
632 	AUDIT_ARG_VALUE(uap->data);
633 	addr = &r;
634 	switch (uap->req) {
635 	case PT_GET_EVENT_MASK:
636 	case PT_LWPINFO:
637 	case PT_GET_SC_ARGS:
638 	case PT_GET_SC_RET:
639 		break;
640 	case PT_GETREGS:
641 		bzero(&r.reg, sizeof(r.reg));
642 		break;
643 	case PT_GETFPREGS:
644 		bzero(&r.fpreg, sizeof(r.fpreg));
645 		break;
646 	case PT_GETDBREGS:
647 		bzero(&r.dbreg, sizeof(r.dbreg));
648 		break;
649 	case PT_GETREGSET:
650 	case PT_SETREGSET:
651 		error = copyin(uap->addr, &r.vec, sizeof(r.vec));
652 		break;
653 	case PT_SETREGS:
654 		error = copyin(uap->addr, &r.reg, sizeof(r.reg));
655 		break;
656 	case PT_SETFPREGS:
657 		error = copyin(uap->addr, &r.fpreg, sizeof(r.fpreg));
658 		break;
659 	case PT_SETDBREGS:
660 		error = copyin(uap->addr, &r.dbreg, sizeof(r.dbreg));
661 		break;
662 	case PT_SET_EVENT_MASK:
663 		if (uap->data != sizeof(r.ptevents))
664 			error = EINVAL;
665 		else
666 			error = copyin(uap->addr, &r.ptevents, uap->data);
667 		break;
668 	case PT_IO:
669 		error = copyin(uap->addr, &r.piod, sizeof(r.piod));
670 		break;
671 	case PT_VM_ENTRY:
672 		error = copyin(uap->addr, &r.pve, sizeof(r.pve));
673 		break;
674 	case PT_COREDUMP:
675 		if (uap->data != sizeof(r.pc))
676 			error = EINVAL;
677 		else
678 			error = copyin(uap->addr, &r.pc, uap->data);
679 		break;
680 	case PT_SC_REMOTE:
681 		if (uap->data != sizeof(r.sr)) {
682 			error = EINVAL;
683 			break;
684 		}
685 		error = copyin(uap->addr, &r.sr, uap->data);
686 		if (error != 0)
687 			break;
688 		if (r.sr.pscr_nargs > nitems(td->td_sa.args)) {
689 			error = EINVAL;
690 			break;
691 		}
692 		error = copyin(r.sr.pscr_args, pscr_args,
693 		    sizeof(u_long) * r.sr.pscr_nargs);
694 		if (error != 0)
695 			break;
696 		r.sr.pscr_args = pscr_args;
697 		break;
698 	default:
699 		addr = uap->addr;
700 		break;
701 	}
702 	if (error)
703 		return (error);
704 
705 	error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data);
706 	if (error)
707 		return (error);
708 
709 	switch (uap->req) {
710 	case PT_VM_ENTRY:
711 		error = copyout(&r.pve, uap->addr, sizeof(r.pve));
712 		break;
713 	case PT_IO:
714 		error = copyout(&r.piod, uap->addr, sizeof(r.piod));
715 		break;
716 	case PT_GETREGS:
717 		error = copyout(&r.reg, uap->addr, sizeof(r.reg));
718 		break;
719 	case PT_GETFPREGS:
720 		error = copyout(&r.fpreg, uap->addr, sizeof(r.fpreg));
721 		break;
722 	case PT_GETDBREGS:
723 		error = copyout(&r.dbreg, uap->addr, sizeof(r.dbreg));
724 		break;
725 	case PT_GETREGSET:
726 		error = copyout(&r.vec, uap->addr, sizeof(r.vec));
727 		break;
728 	case PT_GET_EVENT_MASK:
729 		/* NB: The size in uap->data is validated in kern_ptrace(). */
730 		error = copyout(&r.ptevents, uap->addr, uap->data);
731 		break;
732 	case PT_LWPINFO:
733 		/* NB: The size in uap->data is validated in kern_ptrace(). */
734 		error = copyout(&r.pl, uap->addr, uap->data);
735 		break;
736 	case PT_GET_SC_ARGS:
737 		error = copyout(r.args, uap->addr, MIN(uap->data,
738 		    sizeof(r.args)));
739 		break;
740 	case PT_GET_SC_RET:
741 		error = copyout(&r.psr, uap->addr, MIN(uap->data,
742 		    sizeof(r.psr)));
743 		break;
744 	case PT_SC_REMOTE:
745 		error = copyout(&r.sr.pscr_ret, uap->addr +
746 		    offsetof(struct ptrace_sc_remote, pscr_ret),
747 		    sizeof(r.sr.pscr_ret));
748 		break;
749 	}
750 
751 	return (error);
752 }
753 
754 #ifdef COMPAT_FREEBSD32
755 /*
756  *   PROC_READ(regs, td2, addr);
757  * becomes either:
758  *   proc_read_regs(td2, addr);
759  * or
760  *   proc_read_regs32(td2, addr);
761  * .. except this is done at runtime.  There is an additional
762  * complication in that PROC_WRITE disallows 32 bit consumers
763  * from writing to 64 bit address space targets.
764  */
765 #define	PROC_READ(w, t, a)	wrap32 ? \
766 	proc_read_ ## w ## 32(t, a) : \
767 	proc_read_ ## w (t, a)
768 #define	PROC_WRITE(w, t, a)	wrap32 ? \
769 	(safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \
770 	proc_write_ ## w (t, a)
771 #else
772 #define	PROC_READ(w, t, a)	proc_read_ ## w (t, a)
773 #define	PROC_WRITE(w, t, a)	proc_write_ ## w (t, a)
774 #endif
775 
776 void
proc_set_traced(struct proc * p,bool stop)777 proc_set_traced(struct proc *p, bool stop)
778 {
779 
780 	sx_assert(&proctree_lock, SX_XLOCKED);
781 	PROC_LOCK_ASSERT(p, MA_OWNED);
782 	p->p_flag |= P_TRACED;
783 	if (stop)
784 		p->p_flag2 |= P2_PTRACE_FSTP;
785 	p->p_ptevents = PTRACE_DEFAULT;
786 }
787 
788 void
ptrace_unsuspend(struct proc * p)789 ptrace_unsuspend(struct proc *p)
790 {
791 	PROC_LOCK_ASSERT(p, MA_OWNED);
792 
793 	PROC_SLOCK(p);
794 	p->p_flag &= ~(P_STOPPED_TRACE | P_STOPPED_SIG | P_WAITED);
795 	thread_unsuspend(p);
796 	PROC_SUNLOCK(p);
797 	itimer_proc_continue(p);
798 	kqtimer_proc_continue(p);
799 }
800 
801 static int
proc_can_ptrace(struct thread * td,struct proc * p)802 proc_can_ptrace(struct thread *td, struct proc *p)
803 {
804 	int error;
805 
806 	PROC_LOCK_ASSERT(p, MA_OWNED);
807 
808 	if ((p->p_flag & P_WEXIT) != 0)
809 		return (ESRCH);
810 
811 	if ((error = p_cansee(td, p)) != 0)
812 		return (error);
813 	if ((error = p_candebug(td, p)) != 0)
814 		return (error);
815 
816 	/* not being traced... */
817 	if ((p->p_flag & P_TRACED) == 0)
818 		return (EPERM);
819 
820 	/* not being traced by YOU */
821 	if (p->p_pptr != td->td_proc)
822 		return (EBUSY);
823 
824 	/* not currently stopped */
825 	if ((p->p_flag & P_STOPPED_TRACE) == 0 ||
826 	    p->p_suspcount != p->p_numthreads  ||
827 	    (p->p_flag & P_WAITED) == 0)
828 		return (EBUSY);
829 
830 	return (0);
831 }
832 
833 static struct thread *
ptrace_sel_coredump_thread(struct proc * p)834 ptrace_sel_coredump_thread(struct proc *p)
835 {
836 	struct thread *td2;
837 
838 	PROC_LOCK_ASSERT(p, MA_OWNED);
839 	MPASS((p->p_flag & P_STOPPED_TRACE) != 0);
840 
841 	FOREACH_THREAD_IN_PROC(p, td2) {
842 		if ((td2->td_dbgflags & TDB_SSWITCH) != 0)
843 			return (td2);
844 	}
845 	return (NULL);
846 }
847 
848 int
kern_ptrace(struct thread * td,int req,pid_t pid,void * addr,int data)849 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
850 {
851 	struct iovec iov;
852 	struct uio uio;
853 	struct proc *curp, *p, *pp;
854 	struct thread *td2 = NULL, *td3;
855 	struct ptrace_io_desc *piod = NULL;
856 	struct ptrace_lwpinfo *pl;
857 	struct ptrace_sc_ret *psr;
858 	struct ptrace_sc_remote *pscr;
859 	struct file *fp;
860 	struct ptrace_coredump *pc;
861 	struct thr_coredump_req *tcq;
862 	struct thr_syscall_req *tsr;
863 	int error, num, tmp;
864 	lwpid_t tid = 0, *buf;
865 #ifdef COMPAT_FREEBSD32
866 	int wrap32 = 0, safe = 0;
867 #endif
868 	bool proctree_locked, p2_req_set;
869 
870 	curp = td->td_proc;
871 	proctree_locked = false;
872 	p2_req_set = false;
873 
874 	/* Lock proctree before locking the process. */
875 	switch (req) {
876 	case PT_TRACE_ME:
877 	case PT_ATTACH:
878 	case PT_STEP:
879 	case PT_CONTINUE:
880 	case PT_TO_SCE:
881 	case PT_TO_SCX:
882 	case PT_SYSCALL:
883 	case PT_FOLLOW_FORK:
884 	case PT_LWP_EVENTS:
885 	case PT_GET_EVENT_MASK:
886 	case PT_SET_EVENT_MASK:
887 	case PT_DETACH:
888 	case PT_GET_SC_ARGS:
889 		sx_xlock(&proctree_lock);
890 		proctree_locked = true;
891 		break;
892 	default:
893 		break;
894 	}
895 
896 	if (req == PT_TRACE_ME) {
897 		p = td->td_proc;
898 		PROC_LOCK(p);
899 	} else {
900 		if (pid <= PID_MAX) {
901 			if ((p = pfind(pid)) == NULL) {
902 				if (proctree_locked)
903 					sx_xunlock(&proctree_lock);
904 				return (ESRCH);
905 			}
906 		} else {
907 			td2 = tdfind(pid, -1);
908 			if (td2 == NULL) {
909 				if (proctree_locked)
910 					sx_xunlock(&proctree_lock);
911 				return (ESRCH);
912 			}
913 			p = td2->td_proc;
914 			tid = pid;
915 			pid = p->p_pid;
916 		}
917 	}
918 	AUDIT_ARG_PROCESS(p);
919 
920 	if ((p->p_flag & P_WEXIT) != 0) {
921 		error = ESRCH;
922 		goto fail;
923 	}
924 	if ((error = p_cansee(td, p)) != 0)
925 		goto fail;
926 
927 	if ((error = p_candebug(td, p)) != 0)
928 		goto fail;
929 
930 	/*
931 	 * System processes can't be debugged.
932 	 */
933 	if ((p->p_flag & P_SYSTEM) != 0) {
934 		error = EINVAL;
935 		goto fail;
936 	}
937 
938 	if (tid == 0) {
939 		if ((p->p_flag & P_STOPPED_TRACE) != 0) {
940 			KASSERT(p->p_xthread != NULL, ("NULL p_xthread"));
941 			td2 = p->p_xthread;
942 		} else {
943 			td2 = FIRST_THREAD_IN_PROC(p);
944 		}
945 		tid = td2->td_tid;
946 	}
947 
948 #ifdef COMPAT_FREEBSD32
949 	/*
950 	 * Test if we're a 32 bit client and what the target is.
951 	 * Set the wrap controls accordingly.
952 	 */
953 	if (SV_CURPROC_FLAG(SV_ILP32)) {
954 		if (SV_PROC_FLAG(td2->td_proc, SV_ILP32))
955 			safe = 1;
956 		wrap32 = 1;
957 	}
958 #endif
959 	/*
960 	 * Permissions check
961 	 */
962 	switch (req) {
963 	case PT_TRACE_ME:
964 		/*
965 		 * Always legal, when there is a parent process which
966 		 * could trace us.  Otherwise, reject.
967 		 */
968 		if ((p->p_flag & P_TRACED) != 0) {
969 			error = EBUSY;
970 			goto fail;
971 		}
972 		if (p->p_pptr == initproc) {
973 			error = EPERM;
974 			goto fail;
975 		}
976 		break;
977 
978 	case PT_ATTACH:
979 		/* Self */
980 		if (p == td->td_proc) {
981 			error = EINVAL;
982 			goto fail;
983 		}
984 
985 		/* Already traced */
986 		if (p->p_flag & P_TRACED) {
987 			error = EBUSY;
988 			goto fail;
989 		}
990 
991 		/* Can't trace an ancestor if you're being traced. */
992 		if (curp->p_flag & P_TRACED) {
993 			for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) {
994 				if (pp == p) {
995 					error = EINVAL;
996 					goto fail;
997 				}
998 			}
999 		}
1000 
1001 		/* OK */
1002 		break;
1003 
1004 	case PT_CLEARSTEP:
1005 		/* Allow thread to clear single step for itself */
1006 		if (td->td_tid == tid)
1007 			break;
1008 
1009 		/* FALLTHROUGH */
1010 	default:
1011 		/*
1012 		 * Check for ptrace eligibility before waiting for
1013 		 * holds to drain.
1014 		 */
1015 		error = proc_can_ptrace(td, p);
1016 		if (error != 0)
1017 			goto fail;
1018 
1019 		/*
1020 		 * Block parallel ptrace requests.  Most important, do
1021 		 * not allow other thread in debugger to continue the
1022 		 * debuggee until coredump finished.
1023 		 */
1024 		while ((p->p_flag2 & P2_PTRACEREQ) != 0) {
1025 			if (proctree_locked)
1026 				sx_xunlock(&proctree_lock);
1027 			error = msleep(&p->p_flag2, &p->p_mtx, PPAUSE | PCATCH |
1028 			    (proctree_locked ? PDROP : 0), "pptrace", 0);
1029 			if (proctree_locked) {
1030 				sx_xlock(&proctree_lock);
1031 				PROC_LOCK(p);
1032 			}
1033 			if (error == 0 && td2->td_proc != p)
1034 				error = ESRCH;
1035 			if (error == 0)
1036 				error = proc_can_ptrace(td, p);
1037 			if (error != 0)
1038 				goto fail;
1039 		}
1040 
1041 		/* Ok */
1042 		break;
1043 	}
1044 
1045 	/*
1046 	 * Keep this process around and request parallel ptrace()
1047 	 * request to wait until we finish this request.
1048 	 */
1049 	MPASS((p->p_flag2 & P2_PTRACEREQ) == 0);
1050 	p->p_flag2 |= P2_PTRACEREQ;
1051 	p2_req_set = true;
1052 	_PHOLD(p);
1053 
1054 	/*
1055 	 * Actually do the requests
1056 	 */
1057 
1058 	td->td_retval[0] = 0;
1059 
1060 	switch (req) {
1061 	case PT_TRACE_ME:
1062 		/* set my trace flag and "owner" so it can read/write me */
1063 		proc_set_traced(p, false);
1064 		if (p->p_flag & P_PPWAIT)
1065 			p->p_flag |= P_PPTRACE;
1066 		CTR1(KTR_PTRACE, "PT_TRACE_ME: pid %d", p->p_pid);
1067 		break;
1068 
1069 	case PT_ATTACH:
1070 		/* security check done above */
1071 		/*
1072 		 * It would be nice if the tracing relationship was separate
1073 		 * from the parent relationship but that would require
1074 		 * another set of links in the proc struct or for "wait"
1075 		 * to scan the entire proc table.  To make life easier,
1076 		 * we just re-parent the process we're trying to trace.
1077 		 * The old parent is remembered so we can put things back
1078 		 * on a "detach".
1079 		 */
1080 		proc_set_traced(p, true);
1081 		proc_reparent(p, td->td_proc, false);
1082 		CTR2(KTR_PTRACE, "PT_ATTACH: pid %d, oppid %d", p->p_pid,
1083 		    p->p_oppid);
1084 
1085 		sx_xunlock(&proctree_lock);
1086 		proctree_locked = false;
1087 		MPASS(p->p_xthread == NULL);
1088 		MPASS((p->p_flag & P_STOPPED_TRACE) == 0);
1089 
1090 		/*
1091 		 * If already stopped due to a stop signal, clear the
1092 		 * existing stop before triggering a traced SIGSTOP.
1093 		 */
1094 		if ((p->p_flag & P_STOPPED_SIG) != 0) {
1095 			PROC_SLOCK(p);
1096 			p->p_flag &= ~(P_STOPPED_SIG | P_WAITED);
1097 			thread_unsuspend(p);
1098 			PROC_SUNLOCK(p);
1099 		}
1100 
1101 		kern_psignal(p, SIGSTOP);
1102 		break;
1103 
1104 	case PT_CLEARSTEP:
1105 		CTR2(KTR_PTRACE, "PT_CLEARSTEP: tid %d (pid %d)", td2->td_tid,
1106 		    p->p_pid);
1107 		error = ptrace_clear_single_step(td2);
1108 		break;
1109 
1110 	case PT_SETSTEP:
1111 		CTR2(KTR_PTRACE, "PT_SETSTEP: tid %d (pid %d)", td2->td_tid,
1112 		    p->p_pid);
1113 		error = ptrace_single_step(td2);
1114 		break;
1115 
1116 	case PT_SUSPEND:
1117 		CTR2(KTR_PTRACE, "PT_SUSPEND: tid %d (pid %d)", td2->td_tid,
1118 		    p->p_pid);
1119 		td2->td_dbgflags |= TDB_SUSPEND;
1120 		ast_sched(td2, TDA_SUSPEND);
1121 		break;
1122 
1123 	case PT_RESUME:
1124 		CTR2(KTR_PTRACE, "PT_RESUME: tid %d (pid %d)", td2->td_tid,
1125 		    p->p_pid);
1126 		td2->td_dbgflags &= ~TDB_SUSPEND;
1127 		break;
1128 
1129 	case PT_FOLLOW_FORK:
1130 		CTR3(KTR_PTRACE, "PT_FOLLOW_FORK: pid %d %s -> %s", p->p_pid,
1131 		    p->p_ptevents & PTRACE_FORK ? "enabled" : "disabled",
1132 		    data ? "enabled" : "disabled");
1133 		if (data)
1134 			p->p_ptevents |= PTRACE_FORK;
1135 		else
1136 			p->p_ptevents &= ~PTRACE_FORK;
1137 		break;
1138 
1139 	case PT_LWP_EVENTS:
1140 		CTR3(KTR_PTRACE, "PT_LWP_EVENTS: pid %d %s -> %s", p->p_pid,
1141 		    p->p_ptevents & PTRACE_LWP ? "enabled" : "disabled",
1142 		    data ? "enabled" : "disabled");
1143 		if (data)
1144 			p->p_ptevents |= PTRACE_LWP;
1145 		else
1146 			p->p_ptevents &= ~PTRACE_LWP;
1147 		break;
1148 
1149 	case PT_GET_EVENT_MASK:
1150 		if (data != sizeof(p->p_ptevents)) {
1151 			error = EINVAL;
1152 			break;
1153 		}
1154 		CTR2(KTR_PTRACE, "PT_GET_EVENT_MASK: pid %d mask %#x", p->p_pid,
1155 		    p->p_ptevents);
1156 		*(int *)addr = p->p_ptevents;
1157 		break;
1158 
1159 	case PT_SET_EVENT_MASK:
1160 		if (data != sizeof(p->p_ptevents)) {
1161 			error = EINVAL;
1162 			break;
1163 		}
1164 		tmp = *(int *)addr;
1165 		if ((tmp & ~(PTRACE_EXEC | PTRACE_SCE | PTRACE_SCX |
1166 		    PTRACE_FORK | PTRACE_LWP | PTRACE_VFORK)) != 0) {
1167 			error = EINVAL;
1168 			break;
1169 		}
1170 		CTR3(KTR_PTRACE, "PT_SET_EVENT_MASK: pid %d mask %#x -> %#x",
1171 		    p->p_pid, p->p_ptevents, tmp);
1172 		p->p_ptevents = tmp;
1173 		break;
1174 
1175 	case PT_GET_SC_ARGS:
1176 		CTR1(KTR_PTRACE, "PT_GET_SC_ARGS: pid %d", p->p_pid);
1177 		if ((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) == 0
1178 #ifdef COMPAT_FREEBSD32
1179 		    || (wrap32 && !safe)
1180 #endif
1181 		    ) {
1182 			error = EINVAL;
1183 			break;
1184 		}
1185 		bzero(addr, sizeof(td2->td_sa.args));
1186 		/* See the explanation in linux_ptrace_get_syscall_info(). */
1187 		bcopy(td2->td_sa.args, addr, SV_PROC_ABI(td->td_proc) ==
1188 		    SV_ABI_LINUX ? sizeof(td2->td_sa.args) :
1189 		    td2->td_sa.callp->sy_narg * sizeof(syscallarg_t));
1190 		break;
1191 
1192 	case PT_GET_SC_RET:
1193 		if ((td2->td_dbgflags & (TDB_SCX)) == 0
1194 #ifdef COMPAT_FREEBSD32
1195 		    || (wrap32 && !safe)
1196 #endif
1197 		    ) {
1198 			error = EINVAL;
1199 			break;
1200 		}
1201 		psr = addr;
1202 		bzero(psr, sizeof(*psr));
1203 		psr->sr_error = td2->td_errno;
1204 		if (psr->sr_error == 0) {
1205 			psr->sr_retval[0] = td2->td_retval[0];
1206 			psr->sr_retval[1] = td2->td_retval[1];
1207 		}
1208 		CTR4(KTR_PTRACE,
1209 		    "PT_GET_SC_RET: pid %d error %d retval %#lx,%#lx",
1210 		    p->p_pid, psr->sr_error, psr->sr_retval[0],
1211 		    psr->sr_retval[1]);
1212 		break;
1213 
1214 	case PT_STEP:
1215 	case PT_CONTINUE:
1216 	case PT_TO_SCE:
1217 	case PT_TO_SCX:
1218 	case PT_SYSCALL:
1219 	case PT_DETACH:
1220 		/* Zero means do not send any signal */
1221 		if (data < 0 || data > _SIG_MAXSIG) {
1222 			error = EINVAL;
1223 			break;
1224 		}
1225 
1226 		switch (req) {
1227 		case PT_STEP:
1228 			CTR3(KTR_PTRACE, "PT_STEP: tid %d (pid %d), sig = %d",
1229 			    td2->td_tid, p->p_pid, data);
1230 			error = ptrace_single_step(td2);
1231 			if (error)
1232 				goto out;
1233 			break;
1234 		case PT_CONTINUE:
1235 		case PT_TO_SCE:
1236 		case PT_TO_SCX:
1237 		case PT_SYSCALL:
1238 			if (addr != (void *)1) {
1239 				error = ptrace_set_pc(td2,
1240 				    (u_long)(uintfptr_t)addr);
1241 				if (error)
1242 					goto out;
1243 			}
1244 			switch (req) {
1245 			case PT_TO_SCE:
1246 				p->p_ptevents |= PTRACE_SCE;
1247 				CTR4(KTR_PTRACE,
1248 		    "PT_TO_SCE: pid %d, events = %#x, PC = %#lx, sig = %d",
1249 				    p->p_pid, p->p_ptevents,
1250 				    (u_long)(uintfptr_t)addr, data);
1251 				break;
1252 			case PT_TO_SCX:
1253 				p->p_ptevents |= PTRACE_SCX;
1254 				CTR4(KTR_PTRACE,
1255 		    "PT_TO_SCX: pid %d, events = %#x, PC = %#lx, sig = %d",
1256 				    p->p_pid, p->p_ptevents,
1257 				    (u_long)(uintfptr_t)addr, data);
1258 				break;
1259 			case PT_SYSCALL:
1260 				p->p_ptevents |= PTRACE_SYSCALL;
1261 				CTR4(KTR_PTRACE,
1262 		    "PT_SYSCALL: pid %d, events = %#x, PC = %#lx, sig = %d",
1263 				    p->p_pid, p->p_ptevents,
1264 				    (u_long)(uintfptr_t)addr, data);
1265 				break;
1266 			case PT_CONTINUE:
1267 				CTR3(KTR_PTRACE,
1268 				    "PT_CONTINUE: pid %d, PC = %#lx, sig = %d",
1269 				    p->p_pid, (u_long)(uintfptr_t)addr, data);
1270 				break;
1271 			}
1272 			break;
1273 		case PT_DETACH:
1274 			/*
1275 			 * Clear P_TRACED before reparenting
1276 			 * a detached process back to its original
1277 			 * parent.  Otherwise the debugee will be set
1278 			 * as an orphan of the debugger.
1279 			 */
1280 			p->p_flag &= ~(P_TRACED | P_WAITED);
1281 
1282 			/*
1283 			 * Reset the process parent.
1284 			 */
1285 			if (p->p_oppid != p->p_pptr->p_pid) {
1286 				PROC_LOCK(p->p_pptr);
1287 				sigqueue_take(p->p_ksi);
1288 				PROC_UNLOCK(p->p_pptr);
1289 
1290 				pp = proc_realparent(p);
1291 				proc_reparent(p, pp, false);
1292 				if (pp == initproc)
1293 					p->p_sigparent = SIGCHLD;
1294 				CTR3(KTR_PTRACE,
1295 			    "PT_DETACH: pid %d reparented to pid %d, sig %d",
1296 				    p->p_pid, pp->p_pid, data);
1297 			} else {
1298 				CTR2(KTR_PTRACE, "PT_DETACH: pid %d, sig %d",
1299 				    p->p_pid, data);
1300 			}
1301 
1302 			p->p_ptevents = 0;
1303 			FOREACH_THREAD_IN_PROC(p, td3) {
1304 				if ((td3->td_dbgflags & TDB_FSTP) != 0) {
1305 					sigqueue_delete(&td3->td_sigqueue,
1306 					    SIGSTOP);
1307 				}
1308 				td3->td_dbgflags &= ~(TDB_XSIG | TDB_FSTP |
1309 				    TDB_SUSPEND | TDB_BORN);
1310 			}
1311 
1312 			if ((p->p_flag2 & P2_PTRACE_FSTP) != 0) {
1313 				sigqueue_delete(&p->p_sigqueue, SIGSTOP);
1314 				p->p_flag2 &= ~P2_PTRACE_FSTP;
1315 			}
1316 
1317 			/* should we send SIGCHLD? */
1318 			/* childproc_continued(p); */
1319 			break;
1320 		}
1321 
1322 		sx_xunlock(&proctree_lock);
1323 		proctree_locked = false;
1324 
1325 	sendsig:
1326 		MPASS(!proctree_locked);
1327 
1328 		/*
1329 		 * Clear the pending event for the thread that just
1330 		 * reported its event (p_xthread).  This may not be
1331 		 * the thread passed to PT_CONTINUE, PT_STEP, etc. if
1332 		 * the debugger is resuming a different thread.
1333 		 *
1334 		 * Deliver any pending signal via the reporting thread.
1335 		 */
1336 		MPASS(p->p_xthread != NULL);
1337 		p->p_xthread->td_dbgflags &= ~TDB_XSIG;
1338 		p->p_xthread->td_xsig = data;
1339 		p->p_xthread = NULL;
1340 		p->p_xsig = data;
1341 
1342 		/*
1343 		 * P_WKILLED is insurance that a PT_KILL/SIGKILL
1344 		 * always works immediately, even if another thread is
1345 		 * unsuspended first and attempts to handle a
1346 		 * different signal or if the POSIX.1b style signal
1347 		 * queue cannot accommodate any new signals.
1348 		 */
1349 		if (data == SIGKILL)
1350 			proc_wkilled(p);
1351 
1352 		/*
1353 		 * Unsuspend all threads.  To leave a thread
1354 		 * suspended, use PT_SUSPEND to suspend it before
1355 		 * continuing the process.
1356 		 */
1357 		ptrace_unsuspend(p);
1358 		break;
1359 
1360 	case PT_WRITE_I:
1361 	case PT_WRITE_D:
1362 		td2->td_dbgflags |= TDB_USERWR;
1363 		PROC_UNLOCK(p);
1364 		error = 0;
1365 		if (proc_writemem(td, p, (off_t)(uintptr_t)addr, &data,
1366 		    sizeof(int)) != sizeof(int))
1367 			error = ENOMEM;
1368 		else
1369 			CTR3(KTR_PTRACE, "PT_WRITE: pid %d: %p <= %#x",
1370 			    p->p_pid, addr, data);
1371 		PROC_LOCK(p);
1372 		break;
1373 
1374 	case PT_READ_I:
1375 	case PT_READ_D:
1376 		PROC_UNLOCK(p);
1377 		error = tmp = 0;
1378 		if (proc_readmem(td, p, (off_t)(uintptr_t)addr, &tmp,
1379 		    sizeof(int)) != sizeof(int))
1380 			error = ENOMEM;
1381 		else
1382 			CTR3(KTR_PTRACE, "PT_READ: pid %d: %p >= %#x",
1383 			    p->p_pid, addr, tmp);
1384 		td->td_retval[0] = tmp;
1385 		PROC_LOCK(p);
1386 		break;
1387 
1388 	case PT_IO:
1389 		piod = addr;
1390 		iov.iov_base = piod->piod_addr;
1391 		iov.iov_len = piod->piod_len;
1392 		uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
1393 		uio.uio_resid = piod->piod_len;
1394 		uio.uio_iov = &iov;
1395 		uio.uio_iovcnt = 1;
1396 		uio.uio_segflg = UIO_USERSPACE;
1397 		uio.uio_td = td;
1398 		switch (piod->piod_op) {
1399 		case PIOD_READ_D:
1400 		case PIOD_READ_I:
1401 			CTR3(KTR_PTRACE, "PT_IO: pid %d: READ (%p, %#x)",
1402 			    p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid);
1403 			uio.uio_rw = UIO_READ;
1404 			break;
1405 		case PIOD_WRITE_D:
1406 		case PIOD_WRITE_I:
1407 			CTR3(KTR_PTRACE, "PT_IO: pid %d: WRITE (%p, %#x)",
1408 			    p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid);
1409 			td2->td_dbgflags |= TDB_USERWR;
1410 			uio.uio_rw = UIO_WRITE;
1411 			break;
1412 		default:
1413 			error = EINVAL;
1414 			goto out;
1415 		}
1416 		PROC_UNLOCK(p);
1417 		error = proc_rwmem(p, &uio);
1418 		piod->piod_len -= uio.uio_resid;
1419 		PROC_LOCK(p);
1420 		break;
1421 
1422 	case PT_KILL:
1423 		CTR1(KTR_PTRACE, "PT_KILL: pid %d", p->p_pid);
1424 		data = SIGKILL;
1425 		goto sendsig;	/* in PT_CONTINUE above */
1426 
1427 	case PT_SETREGS:
1428 		CTR2(KTR_PTRACE, "PT_SETREGS: tid %d (pid %d)", td2->td_tid,
1429 		    p->p_pid);
1430 		td2->td_dbgflags |= TDB_USERWR;
1431 		error = PROC_WRITE(regs, td2, addr);
1432 		break;
1433 
1434 	case PT_GETREGS:
1435 		CTR2(KTR_PTRACE, "PT_GETREGS: tid %d (pid %d)", td2->td_tid,
1436 		    p->p_pid);
1437 		error = PROC_READ(regs, td2, addr);
1438 		break;
1439 
1440 	case PT_SETFPREGS:
1441 		CTR2(KTR_PTRACE, "PT_SETFPREGS: tid %d (pid %d)", td2->td_tid,
1442 		    p->p_pid);
1443 		td2->td_dbgflags |= TDB_USERWR;
1444 		error = PROC_WRITE(fpregs, td2, addr);
1445 		break;
1446 
1447 	case PT_GETFPREGS:
1448 		CTR2(KTR_PTRACE, "PT_GETFPREGS: tid %d (pid %d)", td2->td_tid,
1449 		    p->p_pid);
1450 		error = PROC_READ(fpregs, td2, addr);
1451 		break;
1452 
1453 	case PT_SETDBREGS:
1454 		CTR2(KTR_PTRACE, "PT_SETDBREGS: tid %d (pid %d)", td2->td_tid,
1455 		    p->p_pid);
1456 		td2->td_dbgflags |= TDB_USERWR;
1457 		error = PROC_WRITE(dbregs, td2, addr);
1458 		break;
1459 
1460 	case PT_GETDBREGS:
1461 		CTR2(KTR_PTRACE, "PT_GETDBREGS: tid %d (pid %d)", td2->td_tid,
1462 		    p->p_pid);
1463 		error = PROC_READ(dbregs, td2, addr);
1464 		break;
1465 
1466 	case PT_SETREGSET:
1467 		CTR2(KTR_PTRACE, "PT_SETREGSET: tid %d (pid %d)", td2->td_tid,
1468 		    p->p_pid);
1469 		error = proc_write_regset(td2, data, addr);
1470 		break;
1471 
1472 	case PT_GETREGSET:
1473 		CTR2(KTR_PTRACE, "PT_GETREGSET: tid %d (pid %d)", td2->td_tid,
1474 		    p->p_pid);
1475 		error = proc_read_regset(td2, data, addr);
1476 		break;
1477 
1478 	case PT_LWPINFO:
1479 		if (data <= 0 || data > sizeof(*pl)) {
1480 			error = EINVAL;
1481 			break;
1482 		}
1483 		pl = addr;
1484 		bzero(pl, sizeof(*pl));
1485 		pl->pl_lwpid = td2->td_tid;
1486 		pl->pl_event = PL_EVENT_NONE;
1487 		pl->pl_flags = 0;
1488 		if (td2->td_dbgflags & TDB_XSIG) {
1489 			pl->pl_event = PL_EVENT_SIGNAL;
1490 			if (td2->td_si.si_signo != 0 &&
1491 			    data >= offsetof(struct ptrace_lwpinfo, pl_siginfo)
1492 			    + sizeof(pl->pl_siginfo)){
1493 				pl->pl_flags |= PL_FLAG_SI;
1494 				pl->pl_siginfo = td2->td_si;
1495 			}
1496 		}
1497 		if (td2->td_dbgflags & TDB_SCE)
1498 			pl->pl_flags |= PL_FLAG_SCE;
1499 		else if (td2->td_dbgflags & TDB_SCX)
1500 			pl->pl_flags |= PL_FLAG_SCX;
1501 		if (td2->td_dbgflags & TDB_EXEC)
1502 			pl->pl_flags |= PL_FLAG_EXEC;
1503 		if (td2->td_dbgflags & TDB_FORK) {
1504 			pl->pl_flags |= PL_FLAG_FORKED;
1505 			pl->pl_child_pid = td2->td_dbg_forked;
1506 			if (td2->td_dbgflags & TDB_VFORK)
1507 				pl->pl_flags |= PL_FLAG_VFORKED;
1508 		} else if ((td2->td_dbgflags & (TDB_SCX | TDB_VFORK)) ==
1509 		    TDB_VFORK)
1510 			pl->pl_flags |= PL_FLAG_VFORK_DONE;
1511 		if (td2->td_dbgflags & TDB_CHILD)
1512 			pl->pl_flags |= PL_FLAG_CHILD;
1513 		if (td2->td_dbgflags & TDB_BORN)
1514 			pl->pl_flags |= PL_FLAG_BORN;
1515 		if (td2->td_dbgflags & TDB_EXIT)
1516 			pl->pl_flags |= PL_FLAG_EXITED;
1517 		pl->pl_sigmask = td2->td_sigmask;
1518 		pl->pl_siglist = td2->td_siglist;
1519 		strcpy(pl->pl_tdname, td2->td_name);
1520 		if ((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) != 0) {
1521 			pl->pl_syscall_code = td2->td_sa.code;
1522 			pl->pl_syscall_narg = td2->td_sa.callp->sy_narg;
1523 		} else {
1524 			pl->pl_syscall_code = 0;
1525 			pl->pl_syscall_narg = 0;
1526 		}
1527 		CTR6(KTR_PTRACE,
1528     "PT_LWPINFO: tid %d (pid %d) event %d flags %#x child pid %d syscall %d",
1529 		    td2->td_tid, p->p_pid, pl->pl_event, pl->pl_flags,
1530 		    pl->pl_child_pid, pl->pl_syscall_code);
1531 		break;
1532 
1533 	case PT_GETNUMLWPS:
1534 		CTR2(KTR_PTRACE, "PT_GETNUMLWPS: pid %d: %d threads", p->p_pid,
1535 		    p->p_numthreads);
1536 		td->td_retval[0] = p->p_numthreads;
1537 		break;
1538 
1539 	case PT_GETLWPLIST:
1540 		CTR3(KTR_PTRACE, "PT_GETLWPLIST: pid %d: data %d, actual %d",
1541 		    p->p_pid, data, p->p_numthreads);
1542 		if (data <= 0) {
1543 			error = EINVAL;
1544 			break;
1545 		}
1546 		num = imin(p->p_numthreads, data);
1547 		PROC_UNLOCK(p);
1548 		buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK);
1549 		tmp = 0;
1550 		PROC_LOCK(p);
1551 		FOREACH_THREAD_IN_PROC(p, td2) {
1552 			if (tmp >= num)
1553 				break;
1554 			buf[tmp++] = td2->td_tid;
1555 		}
1556 		PROC_UNLOCK(p);
1557 		error = copyout(buf, addr, tmp * sizeof(lwpid_t));
1558 		free(buf, M_TEMP);
1559 		if (!error)
1560 			td->td_retval[0] = tmp;
1561 		PROC_LOCK(p);
1562 		break;
1563 
1564 	case PT_VM_TIMESTAMP:
1565 		CTR2(KTR_PTRACE, "PT_VM_TIMESTAMP: pid %d: timestamp %d",
1566 		    p->p_pid, p->p_vmspace->vm_map.timestamp);
1567 		td->td_retval[0] = p->p_vmspace->vm_map.timestamp;
1568 		break;
1569 
1570 	case PT_VM_ENTRY:
1571 		PROC_UNLOCK(p);
1572 		error = ptrace_vm_entry(td, p, addr);
1573 		PROC_LOCK(p);
1574 		break;
1575 
1576 	case PT_COREDUMP:
1577 		pc = addr;
1578 		CTR2(KTR_PTRACE, "PT_COREDUMP: pid %d, fd %d",
1579 		    p->p_pid, pc->pc_fd);
1580 
1581 		if ((pc->pc_flags & ~(PC_COMPRESS | PC_ALL)) != 0) {
1582 			error = EINVAL;
1583 			break;
1584 		}
1585 		PROC_UNLOCK(p);
1586 
1587 		tcq = malloc(sizeof(*tcq), M_TEMP, M_WAITOK | M_ZERO);
1588 		fp = NULL;
1589 		error = fget_write(td, pc->pc_fd, &cap_write_rights, &fp);
1590 		if (error != 0)
1591 			goto coredump_cleanup_nofp;
1592 		if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VREG) {
1593 			error = EPIPE;
1594 			goto coredump_cleanup;
1595 		}
1596 
1597 		PROC_LOCK(p);
1598 		error = proc_can_ptrace(td, p);
1599 		if (error != 0)
1600 			goto coredump_cleanup_locked;
1601 
1602 		td2 = ptrace_sel_coredump_thread(p);
1603 		if (td2 == NULL) {
1604 			error = EBUSY;
1605 			goto coredump_cleanup_locked;
1606 		}
1607 		KASSERT((td2->td_dbgflags & (TDB_COREDUMPREQ |
1608 		    TDB_SCREMOTEREQ)) == 0,
1609 		    ("proc %d tid %d req coredump", p->p_pid, td2->td_tid));
1610 
1611 		tcq->tc_vp = fp->f_vnode;
1612 		tcq->tc_limit = pc->pc_limit == 0 ? OFF_MAX : pc->pc_limit;
1613 		tcq->tc_flags = SVC_PT_COREDUMP;
1614 		if ((pc->pc_flags & PC_COMPRESS) == 0)
1615 			tcq->tc_flags |= SVC_NOCOMPRESS;
1616 		if ((pc->pc_flags & PC_ALL) != 0)
1617 			tcq->tc_flags |= SVC_ALL;
1618 		td2->td_remotereq = tcq;
1619 		td2->td_dbgflags |= TDB_COREDUMPREQ;
1620 		thread_run_flash(td2);
1621 		while ((td2->td_dbgflags & TDB_COREDUMPREQ) != 0)
1622 			msleep(p, &p->p_mtx, PPAUSE, "crdmp", 0);
1623 		error = tcq->tc_error;
1624 coredump_cleanup_locked:
1625 		PROC_UNLOCK(p);
1626 coredump_cleanup:
1627 		fdrop(fp, td);
1628 coredump_cleanup_nofp:
1629 		free(tcq, M_TEMP);
1630 		PROC_LOCK(p);
1631 		break;
1632 
1633 	case PT_SC_REMOTE:
1634 		pscr = addr;
1635 		CTR2(KTR_PTRACE, "PT_SC_REMOTE: pid %d, syscall %d",
1636 		    p->p_pid, pscr->pscr_syscall);
1637 		if ((td2->td_dbgflags & TDB_BOUNDARY) == 0) {
1638 			error = EBUSY;
1639 			break;
1640 		}
1641 		PROC_UNLOCK(p);
1642 		MPASS(pscr->pscr_nargs <= nitems(td->td_sa.args));
1643 
1644 		tsr = malloc(sizeof(struct thr_syscall_req), M_TEMP,
1645 		    M_WAITOK | M_ZERO);
1646 
1647 		tsr->ts_sa.code = pscr->pscr_syscall;
1648 		tsr->ts_nargs = pscr->pscr_nargs;
1649 		memcpy(&tsr->ts_sa.args, pscr->pscr_args,
1650 		    sizeof(syscallarg_t) * tsr->ts_nargs);
1651 
1652 		PROC_LOCK(p);
1653 		error = proc_can_ptrace(td, p);
1654 		if (error != 0) {
1655 			free(tsr, M_TEMP);
1656 			break;
1657 		}
1658 		if (td2->td_proc != p) {
1659 			free(tsr, M_TEMP);
1660 			error = ESRCH;
1661 			break;
1662 		}
1663 		KASSERT((td2->td_dbgflags & (TDB_COREDUMPREQ |
1664 		    TDB_SCREMOTEREQ)) == 0,
1665 		    ("proc %d tid %d req coredump", p->p_pid, td2->td_tid));
1666 
1667 		td2->td_remotereq = tsr;
1668 		td2->td_dbgflags |= TDB_SCREMOTEREQ;
1669 		thread_run_flash(td2);
1670 		while ((td2->td_dbgflags & TDB_SCREMOTEREQ) != 0)
1671 			msleep(p, &p->p_mtx, PPAUSE, "pscrx", 0);
1672 		error = 0;
1673 		memcpy(&pscr->pscr_ret, &tsr->ts_ret, sizeof(tsr->ts_ret));
1674 		free(tsr, M_TEMP);
1675 		break;
1676 
1677 	default:
1678 #ifdef __HAVE_PTRACE_MACHDEP
1679 		if (req >= PT_FIRSTMACH) {
1680 			PROC_UNLOCK(p);
1681 			error = cpu_ptrace(td2, req, addr, data);
1682 			PROC_LOCK(p);
1683 		} else
1684 #endif
1685 			/* Unknown request. */
1686 			error = EINVAL;
1687 		break;
1688 	}
1689 out:
1690 	/* Drop our hold on this process now that the request has completed. */
1691 	_PRELE(p);
1692 fail:
1693 	if (p2_req_set) {
1694 		if ((p->p_flag2 & P2_PTRACEREQ) != 0)
1695 			wakeup(&p->p_flag2);
1696 		p->p_flag2 &= ~P2_PTRACEREQ;
1697 	}
1698 	PROC_UNLOCK(p);
1699 	if (proctree_locked)
1700 		sx_xunlock(&proctree_lock);
1701 	return (error);
1702 }
1703 #undef PROC_READ
1704 #undef PROC_WRITE
1705