xref: /freebsd/sys/kern/sys_process.c (revision 42c159fe388a3765f69860c84183700af37aca8a)
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  * $FreeBSD$
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/sysproto.h>
39 #include <sys/proc.h>
40 #include <sys/vnode.h>
41 #include <sys/ptrace.h>
42 #include <sys/sx.h>
43 #include <sys/user.h>
44 
45 #include <machine/reg.h>
46 
47 #include <vm/vm.h>
48 #include <vm/pmap.h>
49 #include <vm/vm_extern.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_kern.h>
52 #include <vm/vm_object.h>
53 #include <vm/vm_page.h>
54 
55 /*
56  * Functions implemented using PROC_ACTION():
57  *
58  * proc_read_regs(proc, regs)
59  *	Get the current user-visible register set from the process
60  *	and copy it into the regs structure (<machine/reg.h>).
61  *	The process is stopped at the time read_regs is called.
62  *
63  * proc_write_regs(proc, regs)
64  *	Update the current register set from the passed in regs
65  *	structure.  Take care to avoid clobbering special CPU
66  *	registers or privileged bits in the PSL.
67  *	Depending on the architecture this may have fix-up work to do,
68  *	especially if the IAR or PCW are modified.
69  *	The process is stopped at the time write_regs is called.
70  *
71  * proc_read_fpregs, proc_write_fpregs
72  *	deal with the floating point register set, otherwise as above.
73  *
74  * proc_read_dbregs, proc_write_dbregs
75  *	deal with the processor debug register set, otherwise as above.
76  *
77  * proc_sstep(proc)
78  *	Arrange for the process to trap after executing a single instruction.
79  */
80 
81 #define	PROC_ACTION(action) do {					\
82 	int error;							\
83 									\
84 	mtx_lock_spin(&sched_lock);					\
85 	if ((td->td_proc->p_sflag & PS_INMEM) == 0)			\
86 		error = EIO;						\
87 	else								\
88 		error = (action);					\
89 	mtx_unlock_spin(&sched_lock);					\
90 	return (error);							\
91 } while(0)
92 
93 int
94 proc_read_regs(struct thread *td, struct reg *regs)
95 {
96 
97 	PROC_ACTION(fill_regs(td, regs));
98 }
99 
100 int
101 proc_write_regs(struct thread *td, struct reg *regs)
102 {
103 
104 	PROC_ACTION(set_regs(td, regs));
105 }
106 
107 int
108 proc_read_dbregs(struct thread *td, struct dbreg *dbregs)
109 {
110 
111 	PROC_ACTION(fill_dbregs(td, dbregs));
112 }
113 
114 int
115 proc_write_dbregs(struct thread *td, struct dbreg *dbregs)
116 {
117 
118 	PROC_ACTION(set_dbregs(td, dbregs));
119 }
120 
121 /*
122  * Ptrace doesn't support fpregs at all, and there are no security holes
123  * or translations for fpregs, so we can just copy them.
124  */
125 int
126 proc_read_fpregs(struct thread *td, struct fpreg *fpregs)
127 {
128 
129 	PROC_ACTION(fill_fpregs(td, fpregs));
130 }
131 
132 int
133 proc_write_fpregs(struct thread *td, struct fpreg *fpregs)
134 {
135 
136 	PROC_ACTION(set_fpregs(td, fpregs));
137 }
138 
139 int
140 proc_sstep(struct thread *td)
141 {
142 
143 	PROC_ACTION(ptrace_single_step(td));
144 }
145 
146 int
147 proc_rwmem(struct proc *p, struct uio *uio)
148 {
149 	struct vmspace *vm;
150 	vm_map_t map;
151 	vm_object_t object = NULL;
152 	vm_offset_t pageno = 0;		/* page number */
153 	vm_prot_t reqprot;
154 	vm_offset_t kva;
155 	int error, writing;
156 
157 	GIANT_REQUIRED;
158 
159 	/*
160 	 * if the vmspace is in the midst of being deallocated or the
161 	 * process is exiting, don't try to grab anything.  The page table
162 	 * usage in that process can be messed up.
163 	 */
164 	vm = p->p_vmspace;
165 	if ((p->p_flag & P_WEXIT))
166 		return (EFAULT);
167 	if (vm->vm_refcnt < 1)
168 		return (EFAULT);
169 	++vm->vm_refcnt;
170 	/*
171 	 * The map we want...
172 	 */
173 	map = &vm->vm_map;
174 
175 	writing = uio->uio_rw == UIO_WRITE;
176 	reqprot = writing ? (VM_PROT_WRITE | VM_PROT_OVERRIDE_WRITE) :
177 	    VM_PROT_READ;
178 
179 	kva = kmem_alloc_pageable(kernel_map, PAGE_SIZE);
180 
181 	/*
182 	 * Only map in one page at a time.  We don't have to, but it
183 	 * makes things easier.  This way is trivial - right?
184 	 */
185 	do {
186 		vm_map_t tmap;
187 		vm_offset_t uva;
188 		int page_offset;		/* offset into page */
189 		vm_map_entry_t out_entry;
190 		vm_prot_t out_prot;
191 		boolean_t wired;
192 		vm_pindex_t pindex;
193 		u_int len;
194 		vm_page_t m;
195 
196 		object = NULL;
197 
198 		uva = (vm_offset_t)uio->uio_offset;
199 
200 		/*
201 		 * Get the page number of this segment.
202 		 */
203 		pageno = trunc_page(uva);
204 		page_offset = uva - pageno;
205 
206 		/*
207 		 * How many bytes to copy
208 		 */
209 		len = min(PAGE_SIZE - page_offset, uio->uio_resid);
210 
211 		/*
212 		 * Fault the page on behalf of the process
213 		 */
214 		error = vm_fault(map, pageno, reqprot, VM_FAULT_NORMAL);
215 		if (error) {
216 			error = EFAULT;
217 			break;
218 		}
219 
220 		/*
221 		 * Now we need to get the page.  out_entry, out_prot, wired,
222 		 * and single_use aren't used.  One would think the vm code
223 		 * would be a *bit* nicer...  We use tmap because
224 		 * vm_map_lookup() can change the map argument.
225 		 */
226 		tmap = map;
227 		error = vm_map_lookup(&tmap, pageno, reqprot, &out_entry,
228 		    &object, &pindex, &out_prot, &wired);
229 
230 		if (error) {
231 			error = EFAULT;
232 
233 			/*
234 			 * Make sure that there is no residue in 'object' from
235 			 * an error return on vm_map_lookup.
236 			 */
237 			object = NULL;
238 
239 			break;
240 		}
241 
242 		m = vm_page_lookup(object, pindex);
243 
244 		/* Allow fallback to backing objects if we are reading */
245 
246 		while (m == NULL && !writing && object->backing_object) {
247 
248 			pindex += OFF_TO_IDX(object->backing_object_offset);
249 			object = object->backing_object;
250 
251 			m = vm_page_lookup(object, pindex);
252 		}
253 
254 		if (m == NULL) {
255 			error = EFAULT;
256 
257 			/*
258 			 * Make sure that there is no residue in 'object' from
259 			 * an error return on vm_map_lookup.
260 			 */
261 			object = NULL;
262 
263 			vm_map_lookup_done(tmap, out_entry);
264 
265 			break;
266 		}
267 
268 		/*
269 		 * Wire the page into memory
270 		 */
271 		vm_page_wire(m);
272 
273 		/*
274 		 * We're done with tmap now.
275 		 * But reference the object first, so that we won't loose
276 		 * it.
277 		 */
278 		vm_object_reference(object);
279 		vm_map_lookup_done(tmap, out_entry);
280 
281 		pmap_qenter(kva, &m, 1);
282 
283 		/*
284 		 * Now do the i/o move.
285 		 */
286 		error = uiomove((caddr_t)(kva + page_offset), len, uio);
287 
288 		pmap_qremove(kva, 1);
289 
290 		/*
291 		 * release the page and the object
292 		 */
293 		vm_page_unwire(m, 1);
294 		vm_object_deallocate(object);
295 
296 		object = NULL;
297 
298 	} while (error == 0 && uio->uio_resid > 0);
299 
300 	if (object)
301 		vm_object_deallocate(object);
302 
303 	kmem_free(kernel_map, kva, PAGE_SIZE);
304 	vmspace_free(vm);
305 	return (error);
306 }
307 
308 /*
309  * Process debugging system call.
310  */
311 #ifndef _SYS_SYSPROTO_H_
312 struct ptrace_args {
313 	int	req;
314 	pid_t	pid;
315 	caddr_t	addr;
316 	int	data;
317 };
318 #endif
319 
320 int
321 ptrace(struct thread *td, struct ptrace_args *uap)
322 {
323 	struct iovec iov;
324 	struct uio uio;
325 	/*
326 	 * XXX this obfuscation is to reduce stack usage, but the register
327 	 * structs may be too large to put on the stack anyway.
328 	 */
329 	union {
330 		struct ptrace_io_desc piod;
331 		struct dbreg dbreg;
332 		struct fpreg fpreg;
333 		struct reg reg;
334 	} r;
335 	struct proc *curp, *p;
336 	struct thread *td2;
337 	int error, write;
338 
339 	curp = td->td_proc;
340 	error = 0;
341 	write = 0;
342 	if (uap->req == PT_TRACE_ME) {
343 		p = curp;
344 		PROC_LOCK(p);
345 	} else {
346 		if ((p = pfind(uap->pid)) == NULL)
347 			return (ESRCH);
348 	}
349 	if (p_cansee(curp, p)) {
350 		PROC_UNLOCK(p);
351 		return (ESRCH);
352 	}
353 	if ((error = p_candebug(curp, p)) != 0) {
354 		PROC_UNLOCK(p);
355 		return (error);
356 	}
357 
358 	/*
359 	 * System processes can't be debugged.
360 	 */
361 	if ((p->p_flag & P_SYSTEM) != 0) {
362 		PROC_UNLOCK(p);
363 		return (EINVAL);
364 	}
365 
366 	/*
367 	 * Permissions check
368 	 */
369 	switch (uap->req) {
370 	case PT_TRACE_ME:
371 		/* Always legal. */
372 		break;
373 
374 	case PT_ATTACH:
375 		/* Self */
376 		if (p->p_pid == curp->p_pid) {
377 			PROC_UNLOCK(p);
378 			return (EINVAL);
379 		}
380 
381 		/* Already traced */
382 		if (p->p_flag & P_TRACED) {
383 			PROC_UNLOCK(p);
384 			return (EBUSY);
385 		}
386 
387 		/* OK */
388 		break;
389 
390 	case PT_READ_I:
391 	case PT_READ_D:
392 	case PT_WRITE_I:
393 	case PT_WRITE_D:
394 	case PT_IO:
395 	case PT_CONTINUE:
396 	case PT_KILL:
397 	case PT_STEP:
398 	case PT_DETACH:
399 	case PT_GETREGS:
400 	case PT_SETREGS:
401 	case PT_GETFPREGS:
402 	case PT_SETFPREGS:
403 	case PT_GETDBREGS:
404 	case PT_SETDBREGS:
405 		/* not being traced... */
406 		if ((p->p_flag & P_TRACED) == 0) {
407 			PROC_UNLOCK(p);
408 			return (EPERM);
409 		}
410 
411 		/* not being traced by YOU */
412 		if (p->p_pptr != curp) {
413 			PROC_UNLOCK(p);
414 			return (EBUSY);
415 		}
416 
417 		/* not currently stopped */
418 		mtx_lock_spin(&sched_lock);
419 		if (p->p_stat != SSTOP || (p->p_flag & P_WAITED) == 0) {
420 			mtx_unlock_spin(&sched_lock);
421 			PROC_UNLOCK(p);
422 			return (EBUSY);
423 		}
424 		mtx_unlock_spin(&sched_lock);
425 
426 		/* OK */
427 		break;
428 
429 	default:
430 		PROC_UNLOCK(p);
431 		return (EINVAL);
432 	}
433 
434 	td2 = FIRST_THREAD_IN_PROC(p);
435 	PROC_UNLOCK(p);
436 #ifdef FIX_SSTEP
437 	/*
438 	 * Single step fixup ala procfs
439 	 */
440 	FIX_SSTEP(td2);			/* XXXKSE */
441 #endif
442 
443 	/*
444 	 * Actually do the requests
445 	 */
446 
447 	td->td_retval[0] = 0;
448 
449 	switch (uap->req) {
450 	case PT_TRACE_ME:
451 		/* set my trace flag and "owner" so it can read/write me */
452 		sx_xlock(&proctree_lock);
453 		PROC_LOCK(p);
454 		p->p_flag |= P_TRACED;
455 		p->p_oppid = p->p_pptr->p_pid;
456 		PROC_UNLOCK(p);
457 		sx_xunlock(&proctree_lock);
458 		return (0);
459 
460 	case PT_ATTACH:
461 		/* security check done above */
462 		sx_xlock(&proctree_lock);
463 		PROC_LOCK(p);
464 		p->p_flag |= P_TRACED;
465 		p->p_oppid = p->p_pptr->p_pid;
466 		if (p->p_pptr != curp)
467 			proc_reparent(p, curp);
468 		PROC_UNLOCK(p);
469 		sx_xunlock(&proctree_lock);
470 		uap->data = SIGSTOP;
471 		goto sendsig;	/* in PT_CONTINUE below */
472 
473 	case PT_STEP:
474 	case PT_CONTINUE:
475 	case PT_DETACH:
476 		/* XXX uap->data is used even in the PT_STEP case. */
477 		if (uap->req != PT_STEP && (unsigned)uap->data >= NSIG)
478 			return (EINVAL);
479 
480 		PHOLD(p);
481 
482 		if (uap->req == PT_STEP) {
483 			error = ptrace_single_step(td2);
484 			if (error) {
485 				PRELE(p);
486 				return (error);
487 			}
488 		}
489 
490 		if (uap->addr != (caddr_t)1) {
491 			fill_kinfo_proc(p, &p->p_uarea->u_kproc);
492 			error = ptrace_set_pc(td2,
493 			    (u_long)(uintfptr_t)uap->addr);
494 			if (error) {
495 				PRELE(p);
496 				return (error);
497 			}
498 		}
499 		PRELE(p);
500 
501 		if (uap->req == PT_DETACH) {
502 			/* reset process parent */
503 			sx_xlock(&proctree_lock);
504 			if (p->p_oppid != p->p_pptr->p_pid) {
505 				struct proc *pp;
506 
507 				pp = pfind(p->p_oppid);
508 				if (pp == NULL)
509 					pp = initproc;
510 				else
511 					PROC_UNLOCK(pp);
512 				PROC_LOCK(p);
513 				proc_reparent(p, pp);
514 			} else
515 				PROC_LOCK(p);
516 			p->p_flag &= ~(P_TRACED | P_WAITED);
517 			p->p_oppid = 0;
518 			PROC_UNLOCK(p);
519 			sx_xunlock(&proctree_lock);
520 
521 			/* should we send SIGCHLD? */
522 		}
523 
524 	sendsig:
525 		/* deliver or queue signal */
526 		PROC_LOCK(p);
527 		mtx_lock_spin(&sched_lock);
528 		if (p->p_stat == SSTOP) {
529 			p->p_xstat = uap->data;
530 			setrunnable(td2);	/* XXXKSE */
531 			mtx_unlock_spin(&sched_lock);
532 		} else {
533 			mtx_unlock_spin(&sched_lock);
534 			if (uap->data)
535 				psignal(p, uap->data);
536 		}
537 		PROC_UNLOCK(p);
538 		return (0);
539 
540 	case PT_WRITE_I:
541 	case PT_WRITE_D:
542 		write = 1;
543 		/* fallthrough */
544 	case PT_READ_I:
545 	case PT_READ_D:
546 		/* write = 0 set above */
547 		iov.iov_base = write ? (caddr_t)&uap->data :
548 		    (caddr_t)td->td_retval;
549 		iov.iov_len = sizeof(int);
550 		uio.uio_iov = &iov;
551 		uio.uio_iovcnt = 1;
552 		uio.uio_offset = (off_t)(uintptr_t)uap->addr;
553 		uio.uio_resid = sizeof(int);
554 		uio.uio_segflg = UIO_SYSSPACE;	/* i.e.: the uap */
555 		uio.uio_rw = write ? UIO_WRITE : UIO_READ;
556 		uio.uio_td = td;
557 		error = proc_rwmem(p, &uio);
558 		if (uio.uio_resid != 0) {
559 			/*
560 			 * XXX proc_rwmem() doesn't currently return ENOSPC,
561 			 * so I think write() can bogusly return 0.
562 			 * XXX what happens for short writes?  We don't want
563 			 * to write partial data.
564 			 * XXX proc_rwmem() returns EPERM for other invalid
565 			 * addresses.  Convert this to EINVAL.  Does this
566 			 * clobber returns of EPERM for other reasons?
567 			 */
568 			if (error == 0 || error == ENOSPC || error == EPERM)
569 				error = EINVAL;	/* EOF */
570 		}
571 		return (error);
572 
573 	case PT_IO:
574 		error = copyin(uap->addr, &r.piod, sizeof r.piod);
575 		if (error)
576 			return (error);
577 		iov.iov_base = r.piod.piod_addr;
578 		iov.iov_len = r.piod.piod_len;
579 		uio.uio_iov = &iov;
580 		uio.uio_iovcnt = 1;
581 		uio.uio_offset = (off_t)(uintptr_t)r.piod.piod_offs;
582 		uio.uio_resid = r.piod.piod_len;
583 		uio.uio_segflg = UIO_USERSPACE;
584 		uio.uio_td = td;
585 		switch (r.piod.piod_op) {
586 		case PIOD_READ_D:
587 		case PIOD_READ_I:
588 			uio.uio_rw = UIO_READ;
589 			break;
590 		case PIOD_WRITE_D:
591 		case PIOD_WRITE_I:
592 			uio.uio_rw = UIO_WRITE;
593 			break;
594 		default:
595 			return (EINVAL);
596 		}
597 		error = proc_rwmem(p, &uio);
598 		r.piod.piod_len -= uio.uio_resid;
599 		(void)copyout(&r.piod, uap->addr, sizeof r.piod);
600 		return (error);
601 
602 	case PT_KILL:
603 		uap->data = SIGKILL;
604 		goto sendsig;	/* in PT_CONTINUE above */
605 
606 	case PT_SETREGS:
607 		error = copyin(uap->addr, &r.reg, sizeof r.reg);
608 		if (error == 0) {
609 			PHOLD(p);
610 			error = proc_write_regs(td2, &r.reg);
611 			PRELE(p);
612 		}
613 		return (error);
614 
615 	case PT_GETREGS:
616 		PHOLD(p);
617 		error = proc_read_regs(td2, &r.reg);
618 		PRELE(p);
619 		if (error == 0)
620 			error = copyout(&r.reg, uap->addr, sizeof r.reg);
621 		return (error);
622 
623 	case PT_SETFPREGS:
624 		error = copyin(uap->addr, &r.fpreg, sizeof r.fpreg);
625 		if (error == 0) {
626 			PHOLD(p);
627 			error = proc_write_fpregs(td2, &r.fpreg);
628 			PRELE(p);
629 		}
630 		return (error);
631 
632 	case PT_GETFPREGS:
633 		PHOLD(p);
634 		error = proc_read_fpregs(td2, &r.fpreg);
635 		PRELE(p);
636 		if (error == 0)
637 			error = copyout(&r.fpreg, uap->addr, sizeof r.fpreg);
638 		return (error);
639 
640 	case PT_SETDBREGS:
641 		error = copyin(uap->addr, &r.dbreg, sizeof r.dbreg);
642 		if (error == 0) {
643 			PHOLD(p);
644 			error = proc_write_dbregs(td2, &r.dbreg);
645 			PRELE(p);
646 		}
647 		return (error);
648 
649 	case PT_GETDBREGS:
650 		PHOLD(p);
651 		error = proc_read_dbregs(td2, &r.dbreg);
652 		PRELE(p);
653 		if (error == 0)
654 			error = copyout(&r.dbreg, uap->addr, sizeof r.dbreg);
655 		return (error);
656 
657 	default:
658 		KASSERT(0, ("unreachable code\n"));
659 		break;
660 	}
661 
662 	KASSERT(0, ("unreachable code\n"));
663 	return (0);
664 }
665 
666 int
667 trace_req(struct proc *p)
668 {
669 
670 	return (1);
671 }
672 
673 /*
674  * Stop a process because of a debugging event;
675  * stay stopped until p->p_step is cleared
676  * (cleared by PIOCCONT in procfs).
677  */
678 void
679 stopevent(struct proc *p, unsigned int event, unsigned int val)
680 {
681 
682 	PROC_LOCK_ASSERT(p, MA_OWNED | MA_NOTRECURSED);
683 	p->p_step = 1;
684 
685 	do {
686 		p->p_xstat = val;
687 		p->p_stype = event;	/* Which event caused the stop? */
688 		wakeup(&p->p_stype);	/* Wake up any PIOCWAIT'ing procs */
689 		msleep(&p->p_step, &p->p_mtx, PWAIT, "stopevent", 0);
690 	} while (p->p_step);
691 }
692