xref: /freebsd/sys/kern/sys_process.c (revision ec0e626bafb335b30c499d06066997f54b10c092)
1 /*-
2  * Copyright (c) 1994, Sean Eric Fagan
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Sean Eric Fagan.
16  * 4. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_compat.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/syscallsubr.h>
42 #include <sys/sysent.h>
43 #include <sys/sysproto.h>
44 #include <sys/priv.h>
45 #include <sys/proc.h>
46 #include <sys/vnode.h>
47 #include <sys/ptrace.h>
48 #include <sys/rwlock.h>
49 #include <sys/sx.h>
50 #include <sys/malloc.h>
51 #include <sys/signalvar.h>
52 
53 #include <machine/reg.h>
54 
55 #include <security/audit/audit.h>
56 
57 #include <vm/vm.h>
58 #include <vm/pmap.h>
59 #include <vm/vm_extern.h>
60 #include <vm/vm_map.h>
61 #include <vm/vm_kern.h>
62 #include <vm/vm_object.h>
63 #include <vm/vm_page.h>
64 #include <vm/vm_param.h>
65 
66 #ifdef COMPAT_FREEBSD32
67 #include <sys/procfs.h>
68 #include <compat/freebsd32/freebsd32_signal.h>
69 
70 struct ptrace_io_desc32 {
71 	int		piod_op;
72 	uint32_t	piod_offs;
73 	uint32_t	piod_addr;
74 	uint32_t	piod_len;
75 };
76 
77 struct ptrace_vm_entry32 {
78 	int		pve_entry;
79 	int		pve_timestamp;
80 	uint32_t	pve_start;
81 	uint32_t	pve_end;
82 	uint32_t	pve_offset;
83 	u_int		pve_prot;
84 	u_int		pve_pathlen;
85 	int32_t		pve_fileid;
86 	u_int		pve_fsid;
87 	uint32_t	pve_path;
88 };
89 
90 struct ptrace_lwpinfo32 {
91 	lwpid_t	pl_lwpid;	/* LWP described. */
92 	int	pl_event;	/* Event that stopped the LWP. */
93 	int	pl_flags;	/* LWP flags. */
94 	sigset_t	pl_sigmask;	/* LWP signal mask */
95 	sigset_t	pl_siglist;	/* LWP pending signal */
96 	struct siginfo32 pl_siginfo;	/* siginfo for signal */
97 	char	pl_tdname[MAXCOMLEN + 1];	/* LWP name. */
98 	int	pl_child_pid;		/* New child pid */
99 };
100 
101 #endif
102 
103 /*
104  * Functions implemented using PROC_ACTION():
105  *
106  * proc_read_regs(proc, regs)
107  *	Get the current user-visible register set from the process
108  *	and copy it into the regs structure (<machine/reg.h>).
109  *	The process is stopped at the time read_regs is called.
110  *
111  * proc_write_regs(proc, regs)
112  *	Update the current register set from the passed in regs
113  *	structure.  Take care to avoid clobbering special CPU
114  *	registers or privileged bits in the PSL.
115  *	Depending on the architecture this may have fix-up work to do,
116  *	especially if the IAR or PCW are modified.
117  *	The process is stopped at the time write_regs is called.
118  *
119  * proc_read_fpregs, proc_write_fpregs
120  *	deal with the floating point register set, otherwise as above.
121  *
122  * proc_read_dbregs, proc_write_dbregs
123  *	deal with the processor debug register set, otherwise as above.
124  *
125  * proc_sstep(proc)
126  *	Arrange for the process to trap after executing a single instruction.
127  */
128 
129 #define	PROC_ACTION(action) do {					\
130 	int error;							\
131 									\
132 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);			\
133 	if ((td->td_proc->p_flag & P_INMEM) == 0)			\
134 		error = EIO;						\
135 	else								\
136 		error = (action);					\
137 	return (error);							\
138 } while(0)
139 
140 int
141 proc_read_regs(struct thread *td, struct reg *regs)
142 {
143 
144 	PROC_ACTION(fill_regs(td, regs));
145 }
146 
147 int
148 proc_write_regs(struct thread *td, struct reg *regs)
149 {
150 
151 	PROC_ACTION(set_regs(td, regs));
152 }
153 
154 int
155 proc_read_dbregs(struct thread *td, struct dbreg *dbregs)
156 {
157 
158 	PROC_ACTION(fill_dbregs(td, dbregs));
159 }
160 
161 int
162 proc_write_dbregs(struct thread *td, struct dbreg *dbregs)
163 {
164 
165 	PROC_ACTION(set_dbregs(td, dbregs));
166 }
167 
168 /*
169  * Ptrace doesn't support fpregs at all, and there are no security holes
170  * or translations for fpregs, so we can just copy them.
171  */
172 int
173 proc_read_fpregs(struct thread *td, struct fpreg *fpregs)
174 {
175 
176 	PROC_ACTION(fill_fpregs(td, fpregs));
177 }
178 
179 int
180 proc_write_fpregs(struct thread *td, struct fpreg *fpregs)
181 {
182 
183 	PROC_ACTION(set_fpregs(td, fpregs));
184 }
185 
186 #ifdef COMPAT_FREEBSD32
187 /* For 32 bit binaries, we need to expose the 32 bit regs layouts. */
188 int
189 proc_read_regs32(struct thread *td, struct reg32 *regs32)
190 {
191 
192 	PROC_ACTION(fill_regs32(td, regs32));
193 }
194 
195 int
196 proc_write_regs32(struct thread *td, struct reg32 *regs32)
197 {
198 
199 	PROC_ACTION(set_regs32(td, regs32));
200 }
201 
202 int
203 proc_read_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
204 {
205 
206 	PROC_ACTION(fill_dbregs32(td, dbregs32));
207 }
208 
209 int
210 proc_write_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
211 {
212 
213 	PROC_ACTION(set_dbregs32(td, dbregs32));
214 }
215 
216 int
217 proc_read_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
218 {
219 
220 	PROC_ACTION(fill_fpregs32(td, fpregs32));
221 }
222 
223 int
224 proc_write_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
225 {
226 
227 	PROC_ACTION(set_fpregs32(td, fpregs32));
228 }
229 #endif
230 
231 int
232 proc_sstep(struct thread *td)
233 {
234 
235 	PROC_ACTION(ptrace_single_step(td));
236 }
237 
238 int
239 proc_rwmem(struct proc *p, struct uio *uio)
240 {
241 	vm_map_t map;
242 	vm_offset_t pageno;		/* page number */
243 	vm_prot_t reqprot;
244 	int error, fault_flags, page_offset, writing;
245 
246 	/*
247 	 * Assert that someone has locked this vmspace.  (Should be
248 	 * curthread but we can't assert that.)  This keeps the process
249 	 * from exiting out from under us until this operation completes.
250 	 */
251 	KASSERT(p->p_lock >= 1, ("%s: process %p (pid %d) not held", __func__,
252 	    p, p->p_pid));
253 
254 	/*
255 	 * The map we want...
256 	 */
257 	map = &p->p_vmspace->vm_map;
258 
259 	/*
260 	 * If we are writing, then we request vm_fault() to create a private
261 	 * copy of each page.  Since these copies will not be writeable by the
262 	 * process, we must explicity request that they be dirtied.
263 	 */
264 	writing = uio->uio_rw == UIO_WRITE;
265 	reqprot = writing ? VM_PROT_COPY | VM_PROT_READ : VM_PROT_READ;
266 	fault_flags = writing ? VM_FAULT_DIRTY : VM_FAULT_NORMAL;
267 
268 	/*
269 	 * Only map in one page at a time.  We don't have to, but it
270 	 * makes things easier.  This way is trivial - right?
271 	 */
272 	do {
273 		vm_offset_t uva;
274 		u_int len;
275 		vm_page_t m;
276 
277 		uva = (vm_offset_t)uio->uio_offset;
278 
279 		/*
280 		 * Get the page number of this segment.
281 		 */
282 		pageno = trunc_page(uva);
283 		page_offset = uva - pageno;
284 
285 		/*
286 		 * How many bytes to copy
287 		 */
288 		len = min(PAGE_SIZE - page_offset, uio->uio_resid);
289 
290 		/*
291 		 * Fault and hold the page on behalf of the process.
292 		 */
293 		error = vm_fault_hold(map, pageno, reqprot, fault_flags, &m);
294 		if (error != KERN_SUCCESS) {
295 			if (error == KERN_RESOURCE_SHORTAGE)
296 				error = ENOMEM;
297 			else
298 				error = EFAULT;
299 			break;
300 		}
301 
302 		/*
303 		 * Now do the i/o move.
304 		 */
305 		error = uiomove_fromphys(&m, page_offset, len, uio);
306 
307 		/* Make the I-cache coherent for breakpoints. */
308 		if (writing && error == 0) {
309 			vm_map_lock_read(map);
310 			if (vm_map_check_protection(map, pageno, pageno +
311 			    PAGE_SIZE, VM_PROT_EXECUTE))
312 				vm_sync_icache(map, uva, len);
313 			vm_map_unlock_read(map);
314 		}
315 
316 		/*
317 		 * Release the page.
318 		 */
319 		vm_page_lock(m);
320 		vm_page_unhold(m);
321 		vm_page_unlock(m);
322 
323 	} while (error == 0 && uio->uio_resid > 0);
324 
325 	return (error);
326 }
327 
328 static int
329 ptrace_vm_entry(struct thread *td, struct proc *p, struct ptrace_vm_entry *pve)
330 {
331 	struct vattr vattr;
332 	vm_map_t map;
333 	vm_map_entry_t entry;
334 	vm_object_t obj, tobj, lobj;
335 	struct vmspace *vm;
336 	struct vnode *vp;
337 	char *freepath, *fullpath;
338 	u_int pathlen;
339 	int error, index;
340 
341 	error = 0;
342 	obj = NULL;
343 
344 	vm = vmspace_acquire_ref(p);
345 	map = &vm->vm_map;
346 	vm_map_lock_read(map);
347 
348 	do {
349 		entry = map->header.next;
350 		index = 0;
351 		while (index < pve->pve_entry && entry != &map->header) {
352 			entry = entry->next;
353 			index++;
354 		}
355 		if (index != pve->pve_entry) {
356 			error = EINVAL;
357 			break;
358 		}
359 		while (entry != &map->header &&
360 		    (entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) {
361 			entry = entry->next;
362 			index++;
363 		}
364 		if (entry == &map->header) {
365 			error = ENOENT;
366 			break;
367 		}
368 
369 		/* We got an entry. */
370 		pve->pve_entry = index + 1;
371 		pve->pve_timestamp = map->timestamp;
372 		pve->pve_start = entry->start;
373 		pve->pve_end = entry->end - 1;
374 		pve->pve_offset = entry->offset;
375 		pve->pve_prot = entry->protection;
376 
377 		/* Backing object's path needed? */
378 		if (pve->pve_pathlen == 0)
379 			break;
380 
381 		pathlen = pve->pve_pathlen;
382 		pve->pve_pathlen = 0;
383 
384 		obj = entry->object.vm_object;
385 		if (obj != NULL)
386 			VM_OBJECT_RLOCK(obj);
387 	} while (0);
388 
389 	vm_map_unlock_read(map);
390 	vmspace_free(vm);
391 
392 	pve->pve_fsid = VNOVAL;
393 	pve->pve_fileid = VNOVAL;
394 
395 	if (error == 0 && obj != NULL) {
396 		lobj = obj;
397 		for (tobj = obj; tobj != NULL; tobj = tobj->backing_object) {
398 			if (tobj != obj)
399 				VM_OBJECT_RLOCK(tobj);
400 			if (lobj != obj)
401 				VM_OBJECT_RUNLOCK(lobj);
402 			lobj = tobj;
403 			pve->pve_offset += tobj->backing_object_offset;
404 		}
405 		vp = (lobj->type == OBJT_VNODE) ? lobj->handle : NULL;
406 		if (vp != NULL)
407 			vref(vp);
408 		if (lobj != obj)
409 			VM_OBJECT_RUNLOCK(lobj);
410 		VM_OBJECT_RUNLOCK(obj);
411 
412 		if (vp != NULL) {
413 			freepath = NULL;
414 			fullpath = NULL;
415 			vn_fullpath(td, vp, &fullpath, &freepath);
416 			vn_lock(vp, LK_SHARED | LK_RETRY);
417 			if (VOP_GETATTR(vp, &vattr, td->td_ucred) == 0) {
418 				pve->pve_fileid = vattr.va_fileid;
419 				pve->pve_fsid = vattr.va_fsid;
420 			}
421 			vput(vp);
422 
423 			if (fullpath != NULL) {
424 				pve->pve_pathlen = strlen(fullpath) + 1;
425 				if (pve->pve_pathlen <= pathlen) {
426 					error = copyout(fullpath, pve->pve_path,
427 					    pve->pve_pathlen);
428 				} else
429 					error = ENAMETOOLONG;
430 			}
431 			if (freepath != NULL)
432 				free(freepath, M_TEMP);
433 		}
434 	}
435 
436 	return (error);
437 }
438 
439 #ifdef COMPAT_FREEBSD32
440 static int
441 ptrace_vm_entry32(struct thread *td, struct proc *p,
442     struct ptrace_vm_entry32 *pve32)
443 {
444 	struct ptrace_vm_entry pve;
445 	int error;
446 
447 	pve.pve_entry = pve32->pve_entry;
448 	pve.pve_pathlen = pve32->pve_pathlen;
449 	pve.pve_path = (void *)(uintptr_t)pve32->pve_path;
450 
451 	error = ptrace_vm_entry(td, p, &pve);
452 	if (error == 0) {
453 		pve32->pve_entry = pve.pve_entry;
454 		pve32->pve_timestamp = pve.pve_timestamp;
455 		pve32->pve_start = pve.pve_start;
456 		pve32->pve_end = pve.pve_end;
457 		pve32->pve_offset = pve.pve_offset;
458 		pve32->pve_prot = pve.pve_prot;
459 		pve32->pve_fileid = pve.pve_fileid;
460 		pve32->pve_fsid = pve.pve_fsid;
461 	}
462 
463 	pve32->pve_pathlen = pve.pve_pathlen;
464 	return (error);
465 }
466 
467 static void
468 ptrace_lwpinfo_to32(const struct ptrace_lwpinfo *pl,
469     struct ptrace_lwpinfo32 *pl32)
470 {
471 
472 	pl32->pl_lwpid = pl->pl_lwpid;
473 	pl32->pl_event = pl->pl_event;
474 	pl32->pl_flags = pl->pl_flags;
475 	pl32->pl_sigmask = pl->pl_sigmask;
476 	pl32->pl_siglist = pl->pl_siglist;
477 	siginfo_to_siginfo32(&pl->pl_siginfo, &pl32->pl_siginfo);
478 	strcpy(pl32->pl_tdname, pl->pl_tdname);
479 	pl32->pl_child_pid = pl->pl_child_pid;
480 }
481 #endif /* COMPAT_FREEBSD32 */
482 
483 /*
484  * Process debugging system call.
485  */
486 #ifndef _SYS_SYSPROTO_H_
487 struct ptrace_args {
488 	int	req;
489 	pid_t	pid;
490 	caddr_t	addr;
491 	int	data;
492 };
493 #endif
494 
495 #ifdef COMPAT_FREEBSD32
496 /*
497  * This CPP subterfuge is to try and reduce the number of ifdefs in
498  * the body of the code.
499  *   COPYIN(uap->addr, &r.reg, sizeof r.reg);
500  * becomes either:
501  *   copyin(uap->addr, &r.reg, sizeof r.reg);
502  * or
503  *   copyin(uap->addr, &r.reg32, sizeof r.reg32);
504  * .. except this is done at runtime.
505  */
506 #define	COPYIN(u, k, s)		wrap32 ? \
507 	copyin(u, k ## 32, s ## 32) : \
508 	copyin(u, k, s)
509 #define	COPYOUT(k, u, s)	wrap32 ? \
510 	copyout(k ## 32, u, s ## 32) : \
511 	copyout(k, u, s)
512 #else
513 #define	COPYIN(u, k, s)		copyin(u, k, s)
514 #define	COPYOUT(k, u, s)	copyout(k, u, s)
515 #endif
516 int
517 sys_ptrace(struct thread *td, struct ptrace_args *uap)
518 {
519 	/*
520 	 * XXX this obfuscation is to reduce stack usage, but the register
521 	 * structs may be too large to put on the stack anyway.
522 	 */
523 	union {
524 		struct ptrace_io_desc piod;
525 		struct ptrace_lwpinfo pl;
526 		struct ptrace_vm_entry pve;
527 		struct dbreg dbreg;
528 		struct fpreg fpreg;
529 		struct reg reg;
530 #ifdef COMPAT_FREEBSD32
531 		struct dbreg32 dbreg32;
532 		struct fpreg32 fpreg32;
533 		struct reg32 reg32;
534 		struct ptrace_io_desc32 piod32;
535 		struct ptrace_lwpinfo32 pl32;
536 		struct ptrace_vm_entry32 pve32;
537 #endif
538 	} r;
539 	void *addr;
540 	int error = 0;
541 #ifdef COMPAT_FREEBSD32
542 	int wrap32 = 0;
543 
544 	if (SV_CURPROC_FLAG(SV_ILP32))
545 		wrap32 = 1;
546 #endif
547 	AUDIT_ARG_PID(uap->pid);
548 	AUDIT_ARG_CMD(uap->req);
549 	AUDIT_ARG_VALUE(uap->data);
550 	addr = &r;
551 	switch (uap->req) {
552 	case PT_GETREGS:
553 	case PT_GETFPREGS:
554 	case PT_GETDBREGS:
555 	case PT_LWPINFO:
556 		break;
557 	case PT_SETREGS:
558 		error = COPYIN(uap->addr, &r.reg, sizeof r.reg);
559 		break;
560 	case PT_SETFPREGS:
561 		error = COPYIN(uap->addr, &r.fpreg, sizeof r.fpreg);
562 		break;
563 	case PT_SETDBREGS:
564 		error = COPYIN(uap->addr, &r.dbreg, sizeof r.dbreg);
565 		break;
566 	case PT_IO:
567 		error = COPYIN(uap->addr, &r.piod, sizeof r.piod);
568 		break;
569 	case PT_VM_ENTRY:
570 		error = COPYIN(uap->addr, &r.pve, sizeof r.pve);
571 		break;
572 	default:
573 		addr = uap->addr;
574 		break;
575 	}
576 	if (error)
577 		return (error);
578 
579 	error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data);
580 	if (error)
581 		return (error);
582 
583 	switch (uap->req) {
584 	case PT_VM_ENTRY:
585 		error = COPYOUT(&r.pve, uap->addr, sizeof r.pve);
586 		break;
587 	case PT_IO:
588 		error = COPYOUT(&r.piod, uap->addr, sizeof r.piod);
589 		break;
590 	case PT_GETREGS:
591 		error = COPYOUT(&r.reg, uap->addr, sizeof r.reg);
592 		break;
593 	case PT_GETFPREGS:
594 		error = COPYOUT(&r.fpreg, uap->addr, sizeof r.fpreg);
595 		break;
596 	case PT_GETDBREGS:
597 		error = COPYOUT(&r.dbreg, uap->addr, sizeof r.dbreg);
598 		break;
599 	case PT_LWPINFO:
600 		error = copyout(&r.pl, uap->addr, uap->data);
601 		break;
602 	}
603 
604 	return (error);
605 }
606 #undef COPYIN
607 #undef COPYOUT
608 
609 #ifdef COMPAT_FREEBSD32
610 /*
611  *   PROC_READ(regs, td2, addr);
612  * becomes either:
613  *   proc_read_regs(td2, addr);
614  * or
615  *   proc_read_regs32(td2, addr);
616  * .. except this is done at runtime.  There is an additional
617  * complication in that PROC_WRITE disallows 32 bit consumers
618  * from writing to 64 bit address space targets.
619  */
620 #define	PROC_READ(w, t, a)	wrap32 ? \
621 	proc_read_ ## w ## 32(t, a) : \
622 	proc_read_ ## w (t, a)
623 #define	PROC_WRITE(w, t, a)	wrap32 ? \
624 	(safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \
625 	proc_write_ ## w (t, a)
626 #else
627 #define	PROC_READ(w, t, a)	proc_read_ ## w (t, a)
628 #define	PROC_WRITE(w, t, a)	proc_write_ ## w (t, a)
629 #endif
630 
631 int
632 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
633 {
634 	struct iovec iov;
635 	struct uio uio;
636 	struct proc *curp, *p, *pp;
637 	struct thread *td2 = NULL, *td3;
638 	struct ptrace_io_desc *piod = NULL;
639 	struct ptrace_lwpinfo *pl;
640 	int error, write, tmp, num;
641 	int proctree_locked = 0;
642 	lwpid_t tid = 0, *buf;
643 #ifdef COMPAT_FREEBSD32
644 	int wrap32 = 0, safe = 0;
645 	struct ptrace_io_desc32 *piod32 = NULL;
646 	struct ptrace_lwpinfo32 *pl32 = NULL;
647 	struct ptrace_lwpinfo plr;
648 #endif
649 
650 	curp = td->td_proc;
651 
652 	/* Lock proctree before locking the process. */
653 	switch (req) {
654 	case PT_TRACE_ME:
655 	case PT_ATTACH:
656 	case PT_STEP:
657 	case PT_CONTINUE:
658 	case PT_TO_SCE:
659 	case PT_TO_SCX:
660 	case PT_SYSCALL:
661 	case PT_FOLLOW_FORK:
662 	case PT_DETACH:
663 		sx_xlock(&proctree_lock);
664 		proctree_locked = 1;
665 		break;
666 	default:
667 		break;
668 	}
669 
670 	write = 0;
671 	if (req == PT_TRACE_ME) {
672 		p = td->td_proc;
673 		PROC_LOCK(p);
674 	} else {
675 		if (pid <= PID_MAX) {
676 			if ((p = pfind(pid)) == NULL) {
677 				if (proctree_locked)
678 					sx_xunlock(&proctree_lock);
679 				return (ESRCH);
680 			}
681 		} else {
682 			td2 = tdfind(pid, -1);
683 			if (td2 == NULL) {
684 				if (proctree_locked)
685 					sx_xunlock(&proctree_lock);
686 				return (ESRCH);
687 			}
688 			p = td2->td_proc;
689 			tid = pid;
690 			pid = p->p_pid;
691 		}
692 	}
693 	AUDIT_ARG_PROCESS(p);
694 
695 	if ((p->p_flag & P_WEXIT) != 0) {
696 		error = ESRCH;
697 		goto fail;
698 	}
699 	if ((error = p_cansee(td, p)) != 0)
700 		goto fail;
701 
702 	if ((error = p_candebug(td, p)) != 0)
703 		goto fail;
704 
705 	/*
706 	 * System processes can't be debugged.
707 	 */
708 	if ((p->p_flag & P_SYSTEM) != 0) {
709 		error = EINVAL;
710 		goto fail;
711 	}
712 
713 	if (tid == 0) {
714 		if ((p->p_flag & P_STOPPED_TRACE) != 0) {
715 			KASSERT(p->p_xthread != NULL, ("NULL p_xthread"));
716 			td2 = p->p_xthread;
717 		} else {
718 			td2 = FIRST_THREAD_IN_PROC(p);
719 		}
720 		tid = td2->td_tid;
721 	}
722 
723 #ifdef COMPAT_FREEBSD32
724 	/*
725 	 * Test if we're a 32 bit client and what the target is.
726 	 * Set the wrap controls accordingly.
727 	 */
728 	if (SV_CURPROC_FLAG(SV_ILP32)) {
729 		if (SV_PROC_FLAG(td2->td_proc, SV_ILP32))
730 			safe = 1;
731 		wrap32 = 1;
732 	}
733 #endif
734 	/*
735 	 * Permissions check
736 	 */
737 	switch (req) {
738 	case PT_TRACE_ME:
739 		/* Always legal. */
740 		break;
741 
742 	case PT_ATTACH:
743 		/* Self */
744 		if (p->p_pid == td->td_proc->p_pid) {
745 			error = EINVAL;
746 			goto fail;
747 		}
748 
749 		/* Already traced */
750 		if (p->p_flag & P_TRACED) {
751 			error = EBUSY;
752 			goto fail;
753 		}
754 
755 		/* Can't trace an ancestor if you're being traced. */
756 		if (curp->p_flag & P_TRACED) {
757 			for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) {
758 				if (pp == p) {
759 					error = EINVAL;
760 					goto fail;
761 				}
762 			}
763 		}
764 
765 
766 		/* OK */
767 		break;
768 
769 	case PT_CLEARSTEP:
770 		/* Allow thread to clear single step for itself */
771 		if (td->td_tid == tid)
772 			break;
773 
774 		/* FALLTHROUGH */
775 	default:
776 		/* not being traced... */
777 		if ((p->p_flag & P_TRACED) == 0) {
778 			error = EPERM;
779 			goto fail;
780 		}
781 
782 		/* not being traced by YOU */
783 		if (p->p_pptr != td->td_proc) {
784 			error = EBUSY;
785 			goto fail;
786 		}
787 
788 		/* not currently stopped */
789 		if ((p->p_flag & (P_STOPPED_SIG | P_STOPPED_TRACE)) == 0 ||
790 		    p->p_suspcount != p->p_numthreads  ||
791 		    (p->p_flag & P_WAITED) == 0) {
792 			error = EBUSY;
793 			goto fail;
794 		}
795 
796 		if ((p->p_flag & P_STOPPED_TRACE) == 0) {
797 			static int count = 0;
798 			if (count++ == 0)
799 				printf("P_STOPPED_TRACE not set.\n");
800 		}
801 
802 		/* OK */
803 		break;
804 	}
805 
806 	/* Keep this process around until we finish this request. */
807 	_PHOLD(p);
808 
809 #ifdef FIX_SSTEP
810 	/*
811 	 * Single step fixup ala procfs
812 	 */
813 	FIX_SSTEP(td2);
814 #endif
815 
816 	/*
817 	 * Actually do the requests
818 	 */
819 
820 	td->td_retval[0] = 0;
821 
822 	switch (req) {
823 	case PT_TRACE_ME:
824 		/* set my trace flag and "owner" so it can read/write me */
825 		p->p_flag |= P_TRACED;
826 		if (p->p_flag & P_PPWAIT)
827 			p->p_flag |= P_PPTRACE;
828 		p->p_oppid = p->p_pptr->p_pid;
829 		break;
830 
831 	case PT_ATTACH:
832 		/* security check done above */
833 		/*
834 		 * It would be nice if the tracing relationship was separate
835 		 * from the parent relationship but that would require
836 		 * another set of links in the proc struct or for "wait"
837 		 * to scan the entire proc table.  To make life easier,
838 		 * we just re-parent the process we're trying to trace.
839 		 * The old parent is remembered so we can put things back
840 		 * on a "detach".
841 		 */
842 		p->p_flag |= P_TRACED;
843 		p->p_oppid = p->p_pptr->p_pid;
844 		if (p->p_pptr != td->td_proc) {
845 			proc_reparent(p, td->td_proc);
846 		}
847 		data = SIGSTOP;
848 		goto sendsig;	/* in PT_CONTINUE below */
849 
850 	case PT_CLEARSTEP:
851 		error = ptrace_clear_single_step(td2);
852 		break;
853 
854 	case PT_SETSTEP:
855 		error = ptrace_single_step(td2);
856 		break;
857 
858 	case PT_SUSPEND:
859 		td2->td_dbgflags |= TDB_SUSPEND;
860 		thread_lock(td2);
861 		td2->td_flags |= TDF_NEEDSUSPCHK;
862 		thread_unlock(td2);
863 		break;
864 
865 	case PT_RESUME:
866 		td2->td_dbgflags &= ~TDB_SUSPEND;
867 		break;
868 
869 	case PT_FOLLOW_FORK:
870 		if (data)
871 			p->p_flag |= P_FOLLOWFORK;
872 		else
873 			p->p_flag &= ~P_FOLLOWFORK;
874 		break;
875 
876 	case PT_STEP:
877 	case PT_CONTINUE:
878 	case PT_TO_SCE:
879 	case PT_TO_SCX:
880 	case PT_SYSCALL:
881 	case PT_DETACH:
882 		/* Zero means do not send any signal */
883 		if (data < 0 || data > _SIG_MAXSIG) {
884 			error = EINVAL;
885 			break;
886 		}
887 
888 		switch (req) {
889 		case PT_STEP:
890 			error = ptrace_single_step(td2);
891 			if (error)
892 				goto out;
893 			break;
894 		case PT_CONTINUE:
895 		case PT_TO_SCE:
896 		case PT_TO_SCX:
897 		case PT_SYSCALL:
898 			if (addr != (void *)1) {
899 				error = ptrace_set_pc(td2,
900 				    (u_long)(uintfptr_t)addr);
901 				if (error)
902 					goto out;
903 			}
904 			switch (req) {
905 			case PT_TO_SCE:
906 				p->p_stops |= S_PT_SCE;
907 				break;
908 			case PT_TO_SCX:
909 				p->p_stops |= S_PT_SCX;
910 				break;
911 			case PT_SYSCALL:
912 				p->p_stops |= S_PT_SCE | S_PT_SCX;
913 				break;
914 			}
915 			break;
916 		case PT_DETACH:
917 			/* reset process parent */
918 			if (p->p_oppid != p->p_pptr->p_pid) {
919 				PROC_LOCK(p->p_pptr);
920 				sigqueue_take(p->p_ksi);
921 				PROC_UNLOCK(p->p_pptr);
922 
923 				pp = proc_realparent(p);
924 				proc_reparent(p, pp);
925 				if (pp == initproc)
926 					p->p_sigparent = SIGCHLD;
927 			}
928 			p->p_oppid = 0;
929 			p->p_flag &= ~(P_TRACED | P_WAITED | P_FOLLOWFORK);
930 
931 			/* should we send SIGCHLD? */
932 			/* childproc_continued(p); */
933 			break;
934 		}
935 
936 	sendsig:
937 		if (proctree_locked) {
938 			sx_xunlock(&proctree_lock);
939 			proctree_locked = 0;
940 		}
941 		p->p_xstat = data;
942 		p->p_xthread = NULL;
943 		if ((p->p_flag & (P_STOPPED_SIG | P_STOPPED_TRACE)) != 0) {
944 			/* deliver or queue signal */
945 			td2->td_dbgflags &= ~TDB_XSIG;
946 			td2->td_xsig = data;
947 
948 			if (req == PT_DETACH) {
949 				FOREACH_THREAD_IN_PROC(p, td3)
950 					td3->td_dbgflags &= ~TDB_SUSPEND;
951 			}
952 			/*
953 			 * unsuspend all threads, to not let a thread run,
954 			 * you should use PT_SUSPEND to suspend it before
955 			 * continuing process.
956 			 */
957 			PROC_SLOCK(p);
958 			p->p_flag &= ~(P_STOPPED_TRACE|P_STOPPED_SIG|P_WAITED);
959 			thread_unsuspend(p);
960 			PROC_SUNLOCK(p);
961 			if (req == PT_ATTACH)
962 				kern_psignal(p, data);
963 		} else {
964 			if (data)
965 				kern_psignal(p, data);
966 		}
967 		break;
968 
969 	case PT_WRITE_I:
970 	case PT_WRITE_D:
971 		td2->td_dbgflags |= TDB_USERWR;
972 		write = 1;
973 		/* FALLTHROUGH */
974 	case PT_READ_I:
975 	case PT_READ_D:
976 		PROC_UNLOCK(p);
977 		tmp = 0;
978 		/* write = 0 set above */
979 		iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
980 		iov.iov_len = sizeof(int);
981 		uio.uio_iov = &iov;
982 		uio.uio_iovcnt = 1;
983 		uio.uio_offset = (off_t)(uintptr_t)addr;
984 		uio.uio_resid = sizeof(int);
985 		uio.uio_segflg = UIO_SYSSPACE;	/* i.e.: the uap */
986 		uio.uio_rw = write ? UIO_WRITE : UIO_READ;
987 		uio.uio_td = td;
988 		error = proc_rwmem(p, &uio);
989 		if (uio.uio_resid != 0) {
990 			/*
991 			 * XXX proc_rwmem() doesn't currently return ENOSPC,
992 			 * so I think write() can bogusly return 0.
993 			 * XXX what happens for short writes?  We don't want
994 			 * to write partial data.
995 			 * XXX proc_rwmem() returns EPERM for other invalid
996 			 * addresses.  Convert this to EINVAL.  Does this
997 			 * clobber returns of EPERM for other reasons?
998 			 */
999 			if (error == 0 || error == ENOSPC || error == EPERM)
1000 				error = EINVAL;	/* EOF */
1001 		}
1002 		if (!write)
1003 			td->td_retval[0] = tmp;
1004 		PROC_LOCK(p);
1005 		break;
1006 
1007 	case PT_IO:
1008 #ifdef COMPAT_FREEBSD32
1009 		if (wrap32) {
1010 			piod32 = addr;
1011 			iov.iov_base = (void *)(uintptr_t)piod32->piod_addr;
1012 			iov.iov_len = piod32->piod_len;
1013 			uio.uio_offset = (off_t)(uintptr_t)piod32->piod_offs;
1014 			uio.uio_resid = piod32->piod_len;
1015 		} else
1016 #endif
1017 		{
1018 			piod = addr;
1019 			iov.iov_base = piod->piod_addr;
1020 			iov.iov_len = piod->piod_len;
1021 			uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
1022 			uio.uio_resid = piod->piod_len;
1023 		}
1024 		uio.uio_iov = &iov;
1025 		uio.uio_iovcnt = 1;
1026 		uio.uio_segflg = UIO_USERSPACE;
1027 		uio.uio_td = td;
1028 #ifdef COMPAT_FREEBSD32
1029 		tmp = wrap32 ? piod32->piod_op : piod->piod_op;
1030 #else
1031 		tmp = piod->piod_op;
1032 #endif
1033 		switch (tmp) {
1034 		case PIOD_READ_D:
1035 		case PIOD_READ_I:
1036 			uio.uio_rw = UIO_READ;
1037 			break;
1038 		case PIOD_WRITE_D:
1039 		case PIOD_WRITE_I:
1040 			td2->td_dbgflags |= TDB_USERWR;
1041 			uio.uio_rw = UIO_WRITE;
1042 			break;
1043 		default:
1044 			error = EINVAL;
1045 			goto out;
1046 		}
1047 		PROC_UNLOCK(p);
1048 		error = proc_rwmem(p, &uio);
1049 #ifdef COMPAT_FREEBSD32
1050 		if (wrap32)
1051 			piod32->piod_len -= uio.uio_resid;
1052 		else
1053 #endif
1054 			piod->piod_len -= uio.uio_resid;
1055 		PROC_LOCK(p);
1056 		break;
1057 
1058 	case PT_KILL:
1059 		data = SIGKILL;
1060 		goto sendsig;	/* in PT_CONTINUE above */
1061 
1062 	case PT_SETREGS:
1063 		td2->td_dbgflags |= TDB_USERWR;
1064 		error = PROC_WRITE(regs, td2, addr);
1065 		break;
1066 
1067 	case PT_GETREGS:
1068 		error = PROC_READ(regs, td2, addr);
1069 		break;
1070 
1071 	case PT_SETFPREGS:
1072 		td2->td_dbgflags |= TDB_USERWR;
1073 		error = PROC_WRITE(fpregs, td2, addr);
1074 		break;
1075 
1076 	case PT_GETFPREGS:
1077 		error = PROC_READ(fpregs, td2, addr);
1078 		break;
1079 
1080 	case PT_SETDBREGS:
1081 		td2->td_dbgflags |= TDB_USERWR;
1082 		error = PROC_WRITE(dbregs, td2, addr);
1083 		break;
1084 
1085 	case PT_GETDBREGS:
1086 		error = PROC_READ(dbregs, td2, addr);
1087 		break;
1088 
1089 	case PT_LWPINFO:
1090 		if (data <= 0 ||
1091 #ifdef COMPAT_FREEBSD32
1092 		    (!wrap32 && data > sizeof(*pl)) ||
1093 		    (wrap32 && data > sizeof(*pl32))) {
1094 #else
1095 		    data > sizeof(*pl)) {
1096 #endif
1097 			error = EINVAL;
1098 			break;
1099 		}
1100 #ifdef COMPAT_FREEBSD32
1101 		if (wrap32) {
1102 			pl = &plr;
1103 			pl32 = addr;
1104 		} else
1105 #endif
1106 		pl = addr;
1107 		pl->pl_lwpid = td2->td_tid;
1108 		pl->pl_event = PL_EVENT_NONE;
1109 		pl->pl_flags = 0;
1110 		if (td2->td_dbgflags & TDB_XSIG) {
1111 			pl->pl_event = PL_EVENT_SIGNAL;
1112 			if (td2->td_dbgksi.ksi_signo != 0 &&
1113 #ifdef COMPAT_FREEBSD32
1114 			    ((!wrap32 && data >= offsetof(struct ptrace_lwpinfo,
1115 			    pl_siginfo) + sizeof(pl->pl_siginfo)) ||
1116 			    (wrap32 && data >= offsetof(struct ptrace_lwpinfo32,
1117 			    pl_siginfo) + sizeof(struct siginfo32)))
1118 #else
1119 			    data >= offsetof(struct ptrace_lwpinfo, pl_siginfo)
1120 			    + sizeof(pl->pl_siginfo)
1121 #endif
1122 			){
1123 				pl->pl_flags |= PL_FLAG_SI;
1124 				pl->pl_siginfo = td2->td_dbgksi.ksi_info;
1125 			}
1126 		}
1127 		if ((pl->pl_flags & PL_FLAG_SI) == 0)
1128 			bzero(&pl->pl_siginfo, sizeof(pl->pl_siginfo));
1129 		if (td2->td_dbgflags & TDB_SCE)
1130 			pl->pl_flags |= PL_FLAG_SCE;
1131 		else if (td2->td_dbgflags & TDB_SCX)
1132 			pl->pl_flags |= PL_FLAG_SCX;
1133 		if (td2->td_dbgflags & TDB_EXEC)
1134 			pl->pl_flags |= PL_FLAG_EXEC;
1135 		if (td2->td_dbgflags & TDB_FORK) {
1136 			pl->pl_flags |= PL_FLAG_FORKED;
1137 			pl->pl_child_pid = td2->td_dbg_forked;
1138 		}
1139 		if (td2->td_dbgflags & TDB_CHILD)
1140 			pl->pl_flags |= PL_FLAG_CHILD;
1141 		pl->pl_sigmask = td2->td_sigmask;
1142 		pl->pl_siglist = td2->td_siglist;
1143 		strcpy(pl->pl_tdname, td2->td_name);
1144 #ifdef COMPAT_FREEBSD32
1145 		if (wrap32)
1146 			ptrace_lwpinfo_to32(pl, pl32);
1147 #endif
1148 		break;
1149 
1150 	case PT_GETNUMLWPS:
1151 		td->td_retval[0] = p->p_numthreads;
1152 		break;
1153 
1154 	case PT_GETLWPLIST:
1155 		if (data <= 0) {
1156 			error = EINVAL;
1157 			break;
1158 		}
1159 		num = imin(p->p_numthreads, data);
1160 		PROC_UNLOCK(p);
1161 		buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK);
1162 		tmp = 0;
1163 		PROC_LOCK(p);
1164 		FOREACH_THREAD_IN_PROC(p, td2) {
1165 			if (tmp >= num)
1166 				break;
1167 			buf[tmp++] = td2->td_tid;
1168 		}
1169 		PROC_UNLOCK(p);
1170 		error = copyout(buf, addr, tmp * sizeof(lwpid_t));
1171 		free(buf, M_TEMP);
1172 		if (!error)
1173 			td->td_retval[0] = tmp;
1174 		PROC_LOCK(p);
1175 		break;
1176 
1177 	case PT_VM_TIMESTAMP:
1178 		td->td_retval[0] = p->p_vmspace->vm_map.timestamp;
1179 		break;
1180 
1181 	case PT_VM_ENTRY:
1182 		PROC_UNLOCK(p);
1183 #ifdef COMPAT_FREEBSD32
1184 		if (wrap32)
1185 			error = ptrace_vm_entry32(td, p, addr);
1186 		else
1187 #endif
1188 		error = ptrace_vm_entry(td, p, addr);
1189 		PROC_LOCK(p);
1190 		break;
1191 
1192 	default:
1193 #ifdef __HAVE_PTRACE_MACHDEP
1194 		if (req >= PT_FIRSTMACH) {
1195 			PROC_UNLOCK(p);
1196 			error = cpu_ptrace(td2, req, addr, data);
1197 			PROC_LOCK(p);
1198 		} else
1199 #endif
1200 			/* Unknown request. */
1201 			error = EINVAL;
1202 		break;
1203 	}
1204 
1205 out:
1206 	/* Drop our hold on this process now that the request has completed. */
1207 	_PRELE(p);
1208 fail:
1209 	PROC_UNLOCK(p);
1210 	if (proctree_locked)
1211 		sx_xunlock(&proctree_lock);
1212 	return (error);
1213 }
1214 #undef PROC_READ
1215 #undef PROC_WRITE
1216 
1217 /*
1218  * Stop a process because of a debugging event;
1219  * stay stopped until p->p_step is cleared
1220  * (cleared by PIOCCONT in procfs).
1221  */
1222 void
1223 stopevent(struct proc *p, unsigned int event, unsigned int val)
1224 {
1225 
1226 	PROC_LOCK_ASSERT(p, MA_OWNED);
1227 	p->p_step = 1;
1228 	do {
1229 		p->p_xstat = val;
1230 		p->p_xthread = NULL;
1231 		p->p_stype = event;	/* Which event caused the stop? */
1232 		wakeup(&p->p_stype);	/* Wake up any PIOCWAIT'ing procs */
1233 		msleep(&p->p_step, &p->p_mtx, PWAIT, "stopevent", 0);
1234 	} while (p->p_step);
1235 }
1236