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