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