xref: /freebsd-14-stable/sys/kern/sys_process.c (revision a86e21e2caaa9796308282a235bdb42bd3c71292)
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 explicitly 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 			td2 = p->p_xthread;
941 		if (td2 == NULL)
942 			td2 = FIRST_THREAD_IN_PROC(p);
943 		tid = td2->td_tid;
944 	}
945 
946 #ifdef COMPAT_FREEBSD32
947 	/*
948 	 * Test if we're a 32 bit client and what the target is.
949 	 * Set the wrap controls accordingly.
950 	 */
951 	if (SV_CURPROC_FLAG(SV_ILP32)) {
952 		if (SV_PROC_FLAG(td2->td_proc, SV_ILP32))
953 			safe = 1;
954 		wrap32 = 1;
955 	}
956 #endif
957 	/*
958 	 * Permissions check
959 	 */
960 	switch (req) {
961 	case PT_TRACE_ME:
962 		/*
963 		 * Always legal, when there is a parent process which
964 		 * could trace us.  Otherwise, reject.
965 		 */
966 		if ((p->p_flag & P_TRACED) != 0) {
967 			error = EBUSY;
968 			goto fail;
969 		}
970 		if (p->p_pptr == initproc) {
971 			error = EPERM;
972 			goto fail;
973 		}
974 		break;
975 
976 	case PT_ATTACH:
977 		/* Self */
978 		if (p == td->td_proc) {
979 			error = EINVAL;
980 			goto fail;
981 		}
982 
983 		/* Already traced */
984 		if (p->p_flag & P_TRACED) {
985 			error = EBUSY;
986 			goto fail;
987 		}
988 
989 		/* Can't trace an ancestor if you're being traced. */
990 		if (curp->p_flag & P_TRACED) {
991 			for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) {
992 				if (pp == p) {
993 					error = EINVAL;
994 					goto fail;
995 				}
996 			}
997 		}
998 
999 		/* OK */
1000 		break;
1001 
1002 	case PT_CLEARSTEP:
1003 		/* Allow thread to clear single step for itself */
1004 		if (td->td_tid == tid)
1005 			break;
1006 
1007 		/* FALLTHROUGH */
1008 	default:
1009 		/*
1010 		 * Check for ptrace eligibility before waiting for
1011 		 * holds to drain.
1012 		 */
1013 		error = proc_can_ptrace(td, p);
1014 		if (error != 0)
1015 			goto fail;
1016 
1017 		/*
1018 		 * Block parallel ptrace requests.  Most important, do
1019 		 * not allow other thread in debugger to continue the
1020 		 * debuggee until coredump finished.
1021 		 */
1022 		while ((p->p_flag2 & P2_PTRACEREQ) != 0) {
1023 			if (proctree_locked)
1024 				sx_xunlock(&proctree_lock);
1025 			error = msleep(&p->p_flag2, &p->p_mtx, PPAUSE | PCATCH |
1026 			    (proctree_locked ? PDROP : 0), "pptrace", 0);
1027 			if (proctree_locked) {
1028 				sx_xlock(&proctree_lock);
1029 				PROC_LOCK(p);
1030 			}
1031 			if (error == 0 && td2->td_proc != p)
1032 				error = ESRCH;
1033 			if (error == 0)
1034 				error = proc_can_ptrace(td, p);
1035 			if (error != 0)
1036 				goto fail;
1037 		}
1038 
1039 		/* Ok */
1040 		break;
1041 	}
1042 
1043 	/*
1044 	 * Keep this process around and request parallel ptrace()
1045 	 * request to wait until we finish this request.
1046 	 */
1047 	MPASS((p->p_flag2 & P2_PTRACEREQ) == 0);
1048 	p->p_flag2 |= P2_PTRACEREQ;
1049 	p2_req_set = true;
1050 	_PHOLD(p);
1051 
1052 	/*
1053 	 * Actually do the requests
1054 	 */
1055 
1056 	td->td_retval[0] = 0;
1057 
1058 	switch (req) {
1059 	case PT_TRACE_ME:
1060 		/* set my trace flag and "owner" so it can read/write me */
1061 		proc_set_traced(p, false);
1062 		if (p->p_flag & P_PPWAIT)
1063 			p->p_flag |= P_PPTRACE;
1064 		CTR1(KTR_PTRACE, "PT_TRACE_ME: pid %d", p->p_pid);
1065 		break;
1066 
1067 	case PT_ATTACH:
1068 		/* security check done above */
1069 		/*
1070 		 * It would be nice if the tracing relationship was separate
1071 		 * from the parent relationship but that would require
1072 		 * another set of links in the proc struct or for "wait"
1073 		 * to scan the entire proc table.  To make life easier,
1074 		 * we just re-parent the process we're trying to trace.
1075 		 * The old parent is remembered so we can put things back
1076 		 * on a "detach".
1077 		 */
1078 		proc_set_traced(p, true);
1079 		proc_reparent(p, td->td_proc, false);
1080 		CTR2(KTR_PTRACE, "PT_ATTACH: pid %d, oppid %d", p->p_pid,
1081 		    p->p_oppid);
1082 
1083 		sx_xunlock(&proctree_lock);
1084 		proctree_locked = false;
1085 		MPASS(p->p_xthread == NULL);
1086 		MPASS((p->p_flag & P_STOPPED_TRACE) == 0);
1087 
1088 		/*
1089 		 * If already stopped due to a stop signal, clear the
1090 		 * existing stop before triggering a traced SIGSTOP.
1091 		 */
1092 		if ((p->p_flag & P_STOPPED_SIG) != 0) {
1093 			PROC_SLOCK(p);
1094 			p->p_flag &= ~(P_STOPPED_SIG | P_WAITED);
1095 			thread_unsuspend(p);
1096 			PROC_SUNLOCK(p);
1097 		}
1098 
1099 		kern_psignal(p, SIGSTOP);
1100 		break;
1101 
1102 	case PT_CLEARSTEP:
1103 		CTR2(KTR_PTRACE, "PT_CLEARSTEP: tid %d (pid %d)", td2->td_tid,
1104 		    p->p_pid);
1105 		error = ptrace_clear_single_step(td2);
1106 		break;
1107 
1108 	case PT_SETSTEP:
1109 		CTR2(KTR_PTRACE, "PT_SETSTEP: tid %d (pid %d)", td2->td_tid,
1110 		    p->p_pid);
1111 		error = ptrace_single_step(td2);
1112 		break;
1113 
1114 	case PT_SUSPEND:
1115 		CTR2(KTR_PTRACE, "PT_SUSPEND: tid %d (pid %d)", td2->td_tid,
1116 		    p->p_pid);
1117 		td2->td_dbgflags |= TDB_SUSPEND;
1118 		ast_sched(td2, TDA_SUSPEND);
1119 		break;
1120 
1121 	case PT_RESUME:
1122 		CTR2(KTR_PTRACE, "PT_RESUME: tid %d (pid %d)", td2->td_tid,
1123 		    p->p_pid);
1124 		td2->td_dbgflags &= ~TDB_SUSPEND;
1125 		break;
1126 
1127 	case PT_FOLLOW_FORK:
1128 		CTR3(KTR_PTRACE, "PT_FOLLOW_FORK: pid %d %s -> %s", p->p_pid,
1129 		    p->p_ptevents & PTRACE_FORK ? "enabled" : "disabled",
1130 		    data ? "enabled" : "disabled");
1131 		if (data)
1132 			p->p_ptevents |= PTRACE_FORK;
1133 		else
1134 			p->p_ptevents &= ~PTRACE_FORK;
1135 		break;
1136 
1137 	case PT_LWP_EVENTS:
1138 		CTR3(KTR_PTRACE, "PT_LWP_EVENTS: pid %d %s -> %s", p->p_pid,
1139 		    p->p_ptevents & PTRACE_LWP ? "enabled" : "disabled",
1140 		    data ? "enabled" : "disabled");
1141 		if (data)
1142 			p->p_ptevents |= PTRACE_LWP;
1143 		else
1144 			p->p_ptevents &= ~PTRACE_LWP;
1145 		break;
1146 
1147 	case PT_GET_EVENT_MASK:
1148 		if (data != sizeof(p->p_ptevents)) {
1149 			error = EINVAL;
1150 			break;
1151 		}
1152 		CTR2(KTR_PTRACE, "PT_GET_EVENT_MASK: pid %d mask %#x", p->p_pid,
1153 		    p->p_ptevents);
1154 		*(int *)addr = p->p_ptevents;
1155 		break;
1156 
1157 	case PT_SET_EVENT_MASK:
1158 		if (data != sizeof(p->p_ptevents)) {
1159 			error = EINVAL;
1160 			break;
1161 		}
1162 		tmp = *(int *)addr;
1163 		if ((tmp & ~(PTRACE_EXEC | PTRACE_SCE | PTRACE_SCX |
1164 		    PTRACE_FORK | PTRACE_LWP | PTRACE_VFORK)) != 0) {
1165 			error = EINVAL;
1166 			break;
1167 		}
1168 		CTR3(KTR_PTRACE, "PT_SET_EVENT_MASK: pid %d mask %#x -> %#x",
1169 		    p->p_pid, p->p_ptevents, tmp);
1170 		p->p_ptevents = tmp;
1171 		break;
1172 
1173 	case PT_GET_SC_ARGS:
1174 		CTR1(KTR_PTRACE, "PT_GET_SC_ARGS: pid %d", p->p_pid);
1175 		if (((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) == 0 &&
1176 		     td2->td_sa.code == 0)
1177 #ifdef COMPAT_FREEBSD32
1178 		    || (wrap32 && !safe)
1179 #endif
1180 		    ) {
1181 			error = EINVAL;
1182 			break;
1183 		}
1184 		bzero(addr, sizeof(td2->td_sa.args));
1185 		/* See the explanation in linux_ptrace_get_syscall_info(). */
1186 		bcopy(td2->td_sa.args, addr, SV_PROC_ABI(td->td_proc) ==
1187 		    SV_ABI_LINUX ? sizeof(td2->td_sa.args) :
1188 		    td2->td_sa.callp->sy_narg * sizeof(syscallarg_t));
1189 		break;
1190 
1191 	case PT_GET_SC_RET:
1192 		if ((td2->td_dbgflags & (TDB_SCX)) == 0
1193 #ifdef COMPAT_FREEBSD32
1194 		    || (wrap32 && !safe)
1195 #endif
1196 		    ) {
1197 			error = EINVAL;
1198 			break;
1199 		}
1200 		psr = addr;
1201 		bzero(psr, sizeof(*psr));
1202 		psr->sr_error = td2->td_errno;
1203 		if (psr->sr_error == 0) {
1204 			psr->sr_retval[0] = td2->td_retval[0];
1205 			psr->sr_retval[1] = td2->td_retval[1];
1206 		}
1207 		CTR4(KTR_PTRACE,
1208 		    "PT_GET_SC_RET: pid %d error %d retval %#lx,%#lx",
1209 		    p->p_pid, psr->sr_error, psr->sr_retval[0],
1210 		    psr->sr_retval[1]);
1211 		break;
1212 
1213 	case PT_STEP:
1214 	case PT_CONTINUE:
1215 	case PT_TO_SCE:
1216 	case PT_TO_SCX:
1217 	case PT_SYSCALL:
1218 	case PT_DETACH:
1219 		/* Zero means do not send any signal */
1220 		if (data < 0 || data > _SIG_MAXSIG) {
1221 			error = EINVAL;
1222 			break;
1223 		}
1224 
1225 		switch (req) {
1226 		case PT_STEP:
1227 			CTR3(KTR_PTRACE, "PT_STEP: tid %d (pid %d), sig = %d",
1228 			    td2->td_tid, p->p_pid, data);
1229 			error = ptrace_single_step(td2);
1230 			if (error)
1231 				goto out;
1232 			break;
1233 		case PT_CONTINUE:
1234 		case PT_TO_SCE:
1235 		case PT_TO_SCX:
1236 		case PT_SYSCALL:
1237 			if (addr != (void *)1) {
1238 				error = ptrace_set_pc(td2,
1239 				    (u_long)(uintfptr_t)addr);
1240 				if (error)
1241 					goto out;
1242 			}
1243 			switch (req) {
1244 			case PT_TO_SCE:
1245 				p->p_ptevents |= PTRACE_SCE;
1246 				CTR4(KTR_PTRACE,
1247 		    "PT_TO_SCE: pid %d, events = %#x, PC = %#lx, sig = %d",
1248 				    p->p_pid, p->p_ptevents,
1249 				    (u_long)(uintfptr_t)addr, data);
1250 				break;
1251 			case PT_TO_SCX:
1252 				p->p_ptevents |= PTRACE_SCX;
1253 				CTR4(KTR_PTRACE,
1254 		    "PT_TO_SCX: pid %d, events = %#x, PC = %#lx, sig = %d",
1255 				    p->p_pid, p->p_ptevents,
1256 				    (u_long)(uintfptr_t)addr, data);
1257 				break;
1258 			case PT_SYSCALL:
1259 				p->p_ptevents |= PTRACE_SYSCALL;
1260 				CTR4(KTR_PTRACE,
1261 		    "PT_SYSCALL: pid %d, events = %#x, PC = %#lx, sig = %d",
1262 				    p->p_pid, p->p_ptevents,
1263 				    (u_long)(uintfptr_t)addr, data);
1264 				break;
1265 			case PT_CONTINUE:
1266 				CTR3(KTR_PTRACE,
1267 				    "PT_CONTINUE: pid %d, PC = %#lx, sig = %d",
1268 				    p->p_pid, (u_long)(uintfptr_t)addr, data);
1269 				break;
1270 			}
1271 			break;
1272 		case PT_DETACH:
1273 			/*
1274 			 * Clear P_TRACED before reparenting
1275 			 * a detached process back to its original
1276 			 * parent.  Otherwise the debugee will be set
1277 			 * as an orphan of the debugger.
1278 			 */
1279 			p->p_flag &= ~(P_TRACED | P_WAITED);
1280 
1281 			/*
1282 			 * Reset the process parent.
1283 			 */
1284 			if (p->p_oppid != p->p_pptr->p_pid) {
1285 				PROC_LOCK(p->p_pptr);
1286 				sigqueue_take(p->p_ksi);
1287 				PROC_UNLOCK(p->p_pptr);
1288 
1289 				pp = proc_realparent(p);
1290 				proc_reparent(p, pp, false);
1291 				if (pp == initproc)
1292 					p->p_sigparent = SIGCHLD;
1293 				CTR3(KTR_PTRACE,
1294 			    "PT_DETACH: pid %d reparented to pid %d, sig %d",
1295 				    p->p_pid, pp->p_pid, data);
1296 			} else {
1297 				CTR2(KTR_PTRACE, "PT_DETACH: pid %d, sig %d",
1298 				    p->p_pid, data);
1299 			}
1300 
1301 			p->p_ptevents = 0;
1302 			FOREACH_THREAD_IN_PROC(p, td3) {
1303 				if ((td3->td_dbgflags & TDB_FSTP) != 0) {
1304 					sigqueue_delete(&td3->td_sigqueue,
1305 					    SIGSTOP);
1306 				}
1307 				td3->td_dbgflags &= ~(TDB_XSIG | TDB_FSTP |
1308 				    TDB_SUSPEND | TDB_BORN);
1309 			}
1310 
1311 			if ((p->p_flag2 & P2_PTRACE_FSTP) != 0) {
1312 				sigqueue_delete(&p->p_sigqueue, SIGSTOP);
1313 				p->p_flag2 &= ~P2_PTRACE_FSTP;
1314 			}
1315 
1316 			/* should we send SIGCHLD? */
1317 			/* childproc_continued(p); */
1318 			break;
1319 		}
1320 
1321 		sx_xunlock(&proctree_lock);
1322 		proctree_locked = false;
1323 
1324 	sendsig:
1325 		MPASS(!proctree_locked);
1326 
1327 		/*
1328 		 * Clear the pending event for the thread that just
1329 		 * reported its event (p_xthread), if any.  This may
1330 		 * not be the thread passed to PT_CONTINUE, PT_STEP,
1331 		 * etc. if the debugger is resuming a different
1332 		 * thread.  There might be no reporting thread if
1333 		 * the process was just attached.
1334 		 *
1335 		 * Deliver any pending signal via the reporting thread.
1336 		 */
1337 		if (p->p_xthread != NULL) {
1338 			p->p_xthread->td_dbgflags &= ~TDB_XSIG;
1339 			p->p_xthread->td_xsig = data;
1340 			p->p_xthread = NULL;
1341 		}
1342 		p->p_xsig = data;
1343 
1344 		/*
1345 		 * P_WKILLED is insurance that a PT_KILL/SIGKILL
1346 		 * always works immediately, even if another thread is
1347 		 * unsuspended first and attempts to handle a
1348 		 * different signal or if the POSIX.1b style signal
1349 		 * queue cannot accommodate any new signals.
1350 		 */
1351 		if (data == SIGKILL)
1352 			proc_wkilled(p);
1353 
1354 		/*
1355 		 * Unsuspend all threads.  To leave a thread
1356 		 * suspended, use PT_SUSPEND to suspend it before
1357 		 * continuing the process.
1358 		 */
1359 		ptrace_unsuspend(p);
1360 		break;
1361 
1362 	case PT_WRITE_I:
1363 	case PT_WRITE_D:
1364 		td2->td_dbgflags |= TDB_USERWR;
1365 		PROC_UNLOCK(p);
1366 		error = 0;
1367 		if (proc_writemem(td, p, (off_t)(uintptr_t)addr, &data,
1368 		    sizeof(int)) != sizeof(int))
1369 			error = ENOMEM;
1370 		else
1371 			CTR3(KTR_PTRACE, "PT_WRITE: pid %d: %p <= %#x",
1372 			    p->p_pid, addr, data);
1373 		PROC_LOCK(p);
1374 		break;
1375 
1376 	case PT_READ_I:
1377 	case PT_READ_D:
1378 		PROC_UNLOCK(p);
1379 		error = tmp = 0;
1380 		if (proc_readmem(td, p, (off_t)(uintptr_t)addr, &tmp,
1381 		    sizeof(int)) != sizeof(int))
1382 			error = ENOMEM;
1383 		else
1384 			CTR3(KTR_PTRACE, "PT_READ: pid %d: %p >= %#x",
1385 			    p->p_pid, addr, tmp);
1386 		td->td_retval[0] = tmp;
1387 		PROC_LOCK(p);
1388 		break;
1389 
1390 	case PT_IO:
1391 		piod = addr;
1392 		if (piod->piod_len > SSIZE_MAX) {
1393 			error = EINVAL;
1394 			goto out;
1395 		}
1396 		iov.iov_base = piod->piod_addr;
1397 		iov.iov_len = piod->piod_len;
1398 		uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
1399 		uio.uio_resid = piod->piod_len;
1400 		uio.uio_iov = &iov;
1401 		uio.uio_iovcnt = 1;
1402 		uio.uio_segflg = UIO_USERSPACE;
1403 		uio.uio_td = td;
1404 		switch (piod->piod_op) {
1405 		case PIOD_READ_D:
1406 		case PIOD_READ_I:
1407 			CTR3(KTR_PTRACE, "PT_IO: pid %d: READ (%p, %#x)",
1408 			    p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid);
1409 			uio.uio_rw = UIO_READ;
1410 			break;
1411 		case PIOD_WRITE_D:
1412 		case PIOD_WRITE_I:
1413 			CTR3(KTR_PTRACE, "PT_IO: pid %d: WRITE (%p, %#x)",
1414 			    p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid);
1415 			td2->td_dbgflags |= TDB_USERWR;
1416 			uio.uio_rw = UIO_WRITE;
1417 			break;
1418 		default:
1419 			error = EINVAL;
1420 			goto out;
1421 		}
1422 		PROC_UNLOCK(p);
1423 		error = proc_rwmem(p, &uio);
1424 		piod->piod_len -= uio.uio_resid;
1425 		PROC_LOCK(p);
1426 		break;
1427 
1428 	case PT_KILL:
1429 		CTR1(KTR_PTRACE, "PT_KILL: pid %d", p->p_pid);
1430 		data = SIGKILL;
1431 		goto sendsig;	/* in PT_CONTINUE above */
1432 
1433 	case PT_SETREGS:
1434 		CTR2(KTR_PTRACE, "PT_SETREGS: tid %d (pid %d)", td2->td_tid,
1435 		    p->p_pid);
1436 		td2->td_dbgflags |= TDB_USERWR;
1437 		error = PROC_WRITE(regs, td2, addr);
1438 		break;
1439 
1440 	case PT_GETREGS:
1441 		CTR2(KTR_PTRACE, "PT_GETREGS: tid %d (pid %d)", td2->td_tid,
1442 		    p->p_pid);
1443 		error = PROC_READ(regs, td2, addr);
1444 		break;
1445 
1446 	case PT_SETFPREGS:
1447 		CTR2(KTR_PTRACE, "PT_SETFPREGS: tid %d (pid %d)", td2->td_tid,
1448 		    p->p_pid);
1449 		td2->td_dbgflags |= TDB_USERWR;
1450 		error = PROC_WRITE(fpregs, td2, addr);
1451 		break;
1452 
1453 	case PT_GETFPREGS:
1454 		CTR2(KTR_PTRACE, "PT_GETFPREGS: tid %d (pid %d)", td2->td_tid,
1455 		    p->p_pid);
1456 		error = PROC_READ(fpregs, td2, addr);
1457 		break;
1458 
1459 	case PT_SETDBREGS:
1460 		CTR2(KTR_PTRACE, "PT_SETDBREGS: tid %d (pid %d)", td2->td_tid,
1461 		    p->p_pid);
1462 		td2->td_dbgflags |= TDB_USERWR;
1463 		error = PROC_WRITE(dbregs, td2, addr);
1464 		break;
1465 
1466 	case PT_GETDBREGS:
1467 		CTR2(KTR_PTRACE, "PT_GETDBREGS: tid %d (pid %d)", td2->td_tid,
1468 		    p->p_pid);
1469 		error = PROC_READ(dbregs, td2, addr);
1470 		break;
1471 
1472 	case PT_SETREGSET:
1473 		CTR2(KTR_PTRACE, "PT_SETREGSET: tid %d (pid %d)", td2->td_tid,
1474 		    p->p_pid);
1475 		error = proc_write_regset(td2, data, addr);
1476 		break;
1477 
1478 	case PT_GETREGSET:
1479 		CTR2(KTR_PTRACE, "PT_GETREGSET: tid %d (pid %d)", td2->td_tid,
1480 		    p->p_pid);
1481 		error = proc_read_regset(td2, data, addr);
1482 		break;
1483 
1484 	case PT_LWPINFO:
1485 		if (data <= 0 || data > sizeof(*pl)) {
1486 			error = EINVAL;
1487 			break;
1488 		}
1489 		pl = addr;
1490 		bzero(pl, sizeof(*pl));
1491 		pl->pl_lwpid = td2->td_tid;
1492 		pl->pl_event = PL_EVENT_NONE;
1493 		pl->pl_flags = 0;
1494 		if (td2->td_dbgflags & TDB_XSIG) {
1495 			pl->pl_event = PL_EVENT_SIGNAL;
1496 			if (td2->td_si.si_signo != 0 &&
1497 			    data >= offsetof(struct ptrace_lwpinfo, pl_siginfo)
1498 			    + sizeof(pl->pl_siginfo)){
1499 				pl->pl_flags |= PL_FLAG_SI;
1500 				pl->pl_siginfo = td2->td_si;
1501 			}
1502 		}
1503 		if (td2->td_dbgflags & TDB_SCE)
1504 			pl->pl_flags |= PL_FLAG_SCE;
1505 		else if (td2->td_dbgflags & TDB_SCX)
1506 			pl->pl_flags |= PL_FLAG_SCX;
1507 		if (td2->td_dbgflags & TDB_EXEC)
1508 			pl->pl_flags |= PL_FLAG_EXEC;
1509 		if (td2->td_dbgflags & TDB_FORK) {
1510 			pl->pl_flags |= PL_FLAG_FORKED;
1511 			pl->pl_child_pid = td2->td_dbg_forked;
1512 			if (td2->td_dbgflags & TDB_VFORK)
1513 				pl->pl_flags |= PL_FLAG_VFORKED;
1514 		} else if ((td2->td_dbgflags & (TDB_SCX | TDB_VFORK)) ==
1515 		    TDB_VFORK)
1516 			pl->pl_flags |= PL_FLAG_VFORK_DONE;
1517 		if (td2->td_dbgflags & TDB_CHILD)
1518 			pl->pl_flags |= PL_FLAG_CHILD;
1519 		if (td2->td_dbgflags & TDB_BORN)
1520 			pl->pl_flags |= PL_FLAG_BORN;
1521 		if (td2->td_dbgflags & TDB_EXIT)
1522 			pl->pl_flags |= PL_FLAG_EXITED;
1523 		pl->pl_sigmask = td2->td_sigmask;
1524 		pl->pl_siglist = td2->td_siglist;
1525 		strcpy(pl->pl_tdname, td2->td_name);
1526 		if (td2->td_sa.code != 0) {
1527 			pl->pl_syscall_code = td2->td_sa.code;
1528 			pl->pl_syscall_narg = td2->td_sa.callp->sy_narg;
1529 		}
1530 		CTR6(KTR_PTRACE,
1531     "PT_LWPINFO: tid %d (pid %d) event %d flags %#x child pid %d syscall %d",
1532 		    td2->td_tid, p->p_pid, pl->pl_event, pl->pl_flags,
1533 		    pl->pl_child_pid, pl->pl_syscall_code);
1534 		break;
1535 
1536 	case PT_GETNUMLWPS:
1537 		CTR2(KTR_PTRACE, "PT_GETNUMLWPS: pid %d: %d threads", p->p_pid,
1538 		    p->p_numthreads);
1539 		td->td_retval[0] = p->p_numthreads;
1540 		break;
1541 
1542 	case PT_GETLWPLIST:
1543 		CTR3(KTR_PTRACE, "PT_GETLWPLIST: pid %d: data %d, actual %d",
1544 		    p->p_pid, data, p->p_numthreads);
1545 		if (data <= 0) {
1546 			error = EINVAL;
1547 			break;
1548 		}
1549 		num = imin(p->p_numthreads, data);
1550 		PROC_UNLOCK(p);
1551 		buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK);
1552 		tmp = 0;
1553 		PROC_LOCK(p);
1554 		FOREACH_THREAD_IN_PROC(p, td2) {
1555 			if (tmp >= num)
1556 				break;
1557 			buf[tmp++] = td2->td_tid;
1558 		}
1559 		PROC_UNLOCK(p);
1560 		error = copyout(buf, addr, tmp * sizeof(lwpid_t));
1561 		free(buf, M_TEMP);
1562 		if (!error)
1563 			td->td_retval[0] = tmp;
1564 		PROC_LOCK(p);
1565 		break;
1566 
1567 	case PT_VM_TIMESTAMP:
1568 		CTR2(KTR_PTRACE, "PT_VM_TIMESTAMP: pid %d: timestamp %d",
1569 		    p->p_pid, p->p_vmspace->vm_map.timestamp);
1570 		td->td_retval[0] = p->p_vmspace->vm_map.timestamp;
1571 		break;
1572 
1573 	case PT_VM_ENTRY:
1574 		PROC_UNLOCK(p);
1575 		error = ptrace_vm_entry(td, p, addr);
1576 		PROC_LOCK(p);
1577 		break;
1578 
1579 	case PT_COREDUMP:
1580 		pc = addr;
1581 		CTR2(KTR_PTRACE, "PT_COREDUMP: pid %d, fd %d",
1582 		    p->p_pid, pc->pc_fd);
1583 
1584 		if ((pc->pc_flags & ~(PC_COMPRESS | PC_ALL)) != 0) {
1585 			error = EINVAL;
1586 			break;
1587 		}
1588 		PROC_UNLOCK(p);
1589 
1590 		tcq = malloc(sizeof(*tcq), M_TEMP, M_WAITOK | M_ZERO);
1591 		fp = NULL;
1592 		error = fget_write(td, pc->pc_fd, &cap_write_rights, &fp);
1593 		if (error != 0)
1594 			goto coredump_cleanup_nofp;
1595 		if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VREG) {
1596 			error = EPIPE;
1597 			goto coredump_cleanup;
1598 		}
1599 
1600 		PROC_LOCK(p);
1601 		error = proc_can_ptrace(td, p);
1602 		if (error != 0)
1603 			goto coredump_cleanup_locked;
1604 
1605 		td2 = ptrace_sel_coredump_thread(p);
1606 		if (td2 == NULL) {
1607 			error = EBUSY;
1608 			goto coredump_cleanup_locked;
1609 		}
1610 		KASSERT((td2->td_dbgflags & (TDB_COREDUMPREQ |
1611 		    TDB_SCREMOTEREQ)) == 0,
1612 		    ("proc %d tid %d req coredump", p->p_pid, td2->td_tid));
1613 
1614 		tcq->tc_vp = fp->f_vnode;
1615 		tcq->tc_limit = pc->pc_limit == 0 ? OFF_MAX : pc->pc_limit;
1616 		tcq->tc_flags = SVC_PT_COREDUMP;
1617 		if ((pc->pc_flags & PC_COMPRESS) == 0)
1618 			tcq->tc_flags |= SVC_NOCOMPRESS;
1619 		if ((pc->pc_flags & PC_ALL) != 0)
1620 			tcq->tc_flags |= SVC_ALL;
1621 		td2->td_remotereq = tcq;
1622 		td2->td_dbgflags |= TDB_COREDUMPREQ;
1623 		thread_run_flash(td2);
1624 		while ((td2->td_dbgflags & TDB_COREDUMPREQ) != 0)
1625 			msleep(p, &p->p_mtx, PPAUSE, "crdmp", 0);
1626 		error = tcq->tc_error;
1627 coredump_cleanup_locked:
1628 		PROC_UNLOCK(p);
1629 coredump_cleanup:
1630 		fdrop(fp, td);
1631 coredump_cleanup_nofp:
1632 		free(tcq, M_TEMP);
1633 		PROC_LOCK(p);
1634 		break;
1635 
1636 	case PT_SC_REMOTE:
1637 		pscr = addr;
1638 		CTR2(KTR_PTRACE, "PT_SC_REMOTE: pid %d, syscall %d",
1639 		    p->p_pid, pscr->pscr_syscall);
1640 		if ((td2->td_dbgflags & TDB_BOUNDARY) == 0) {
1641 			error = EBUSY;
1642 			break;
1643 		}
1644 		PROC_UNLOCK(p);
1645 		MPASS(pscr->pscr_nargs <= nitems(td->td_sa.args));
1646 
1647 		tsr = malloc(sizeof(struct thr_syscall_req), M_TEMP,
1648 		    M_WAITOK | M_ZERO);
1649 
1650 		tsr->ts_sa.code = pscr->pscr_syscall;
1651 		tsr->ts_nargs = pscr->pscr_nargs;
1652 		memcpy(&tsr->ts_sa.args, pscr->pscr_args,
1653 		    sizeof(syscallarg_t) * tsr->ts_nargs);
1654 
1655 		PROC_LOCK(p);
1656 		error = proc_can_ptrace(td, p);
1657 		if (error != 0) {
1658 			free(tsr, M_TEMP);
1659 			break;
1660 		}
1661 		if (td2->td_proc != p) {
1662 			free(tsr, M_TEMP);
1663 			error = ESRCH;
1664 			break;
1665 		}
1666 		KASSERT((td2->td_dbgflags & (TDB_COREDUMPREQ |
1667 		    TDB_SCREMOTEREQ)) == 0,
1668 		    ("proc %d tid %d req coredump", p->p_pid, td2->td_tid));
1669 
1670 		td2->td_remotereq = tsr;
1671 		td2->td_dbgflags |= TDB_SCREMOTEREQ;
1672 		thread_run_flash(td2);
1673 		while ((td2->td_dbgflags & TDB_SCREMOTEREQ) != 0)
1674 			msleep(p, &p->p_mtx, PPAUSE, "pscrx", 0);
1675 		error = 0;
1676 		memcpy(&pscr->pscr_ret, &tsr->ts_ret, sizeof(tsr->ts_ret));
1677 		free(tsr, M_TEMP);
1678 		break;
1679 
1680 	default:
1681 #ifdef __HAVE_PTRACE_MACHDEP
1682 		if (req >= PT_FIRSTMACH) {
1683 			PROC_UNLOCK(p);
1684 			error = cpu_ptrace(td2, req, addr, data);
1685 			PROC_LOCK(p);
1686 		} else
1687 #endif
1688 			/* Unknown request. */
1689 			error = EINVAL;
1690 		break;
1691 	}
1692 out:
1693 	/* Drop our hold on this process now that the request has completed. */
1694 	_PRELE(p);
1695 fail:
1696 	if (p2_req_set) {
1697 		if ((p->p_flag2 & P2_PTRACEREQ) != 0)
1698 			wakeup(&p->p_flag2);
1699 		p->p_flag2 &= ~P2_PTRACEREQ;
1700 	}
1701 	PROC_UNLOCK(p);
1702 	if (proctree_locked)
1703 		sx_xunlock(&proctree_lock);
1704 	return (error);
1705 }
1706 #undef PROC_READ
1707 #undef PROC_WRITE
1708