xref: /freebsd/sys/kern/sys_process.c (revision 25d0cc3b4dcc3d9e3929615a9e9b5be4801fbaac)
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/sysproto.h>
37 #include <sys/proc.h>
38 #include <sys/vnode.h>
39 #include <sys/ptrace.h>
40 #include <sys/sx.h>
41 
42 #include <machine/reg.h>
43 #include <vm/vm.h>
44 #include <sys/lock.h>
45 #include <vm/pmap.h>
46 #include <vm/vm_map.h>
47 #include <vm/vm_page.h>
48 
49 #include <sys/user.h>
50 #include <miscfs/procfs/procfs.h>
51 
52 /* use the equivalent procfs code */
53 #if 0
54 static int
55 pread (struct proc *procp, unsigned int addr, unsigned int *retval) {
56 	int		rv;
57 	vm_map_t	map, tmap;
58 	vm_object_t	object;
59 	vm_offset_t	kva = 0;
60 	int		page_offset;	/* offset into page */
61 	vm_offset_t	pageno;		/* page number */
62 	vm_map_entry_t	out_entry;
63 	vm_prot_t	out_prot;
64 	boolean_t	wired;
65 	vm_pindex_t	pindex;
66 
67 	/* Map page into kernel space */
68 
69 	map = &procp->p_vmspace->vm_map;
70 
71 	page_offset = addr - trunc_page(addr);
72 	pageno = trunc_page(addr);
73 
74 	tmap = map;
75 	rv = vm_map_lookup (&tmap, pageno, VM_PROT_READ, &out_entry,
76 		&object, &pindex, &out_prot, &wired);
77 
78 	if (rv != KERN_SUCCESS)
79 		return EINVAL;
80 
81 	vm_map_lookup_done (tmap, out_entry);
82 
83 	/* Find space in kernel_map for the page we're interested in */
84 	rv = vm_map_find (kernel_map, object, IDX_TO_OFF(pindex),
85 		&kva, PAGE_SIZE, 0, VM_PROT_ALL, VM_PROT_ALL, 0);
86 
87 	if (!rv) {
88 		vm_object_reference (object);
89 
90 		rv = vm_map_pageable (kernel_map, kva, kva + PAGE_SIZE, 0);
91 		if (!rv) {
92 			*retval = 0;
93 			bcopy ((caddr_t)kva + page_offset,
94 			       retval, sizeof *retval);
95 		}
96 		vm_map_remove (kernel_map, kva, kva + PAGE_SIZE);
97 	}
98 
99 	return rv;
100 }
101 
102 static int
103 pwrite (struct proc *procp, unsigned int addr, unsigned int datum) {
104 	int		rv;
105 	vm_map_t	map, tmap;
106 	vm_object_t	object;
107 	vm_offset_t	kva = 0;
108 	int		page_offset;	/* offset into page */
109 	vm_offset_t	pageno;		/* page number */
110 	vm_map_entry_t	out_entry;
111 	vm_prot_t	out_prot;
112 	boolean_t	wired;
113 	vm_pindex_t	pindex;
114 	boolean_t	fix_prot = 0;
115 
116 	/* Map page into kernel space */
117 
118 	map = &procp->p_vmspace->vm_map;
119 
120 	page_offset = addr - trunc_page(addr);
121 	pageno = trunc_page(addr);
122 
123 	/*
124 	 * Check the permissions for the area we're interested in.
125 	 */
126 
127 	if (vm_map_check_protection (map, pageno, pageno + PAGE_SIZE,
128 		VM_PROT_WRITE) == FALSE) {
129 		/*
130 		 * If the page was not writable, we make it so.
131 		 * XXX It is possible a page may *not* be read/executable,
132 		 * if a process changes that!
133 		 */
134 		fix_prot = 1;
135 		/* The page isn't writable, so let's try making it so... */
136 		if ((rv = vm_map_protect (map, pageno, pageno + PAGE_SIZE,
137 			VM_PROT_ALL, 0)) != KERN_SUCCESS)
138 		  return EFAULT;	/* I guess... */
139 	}
140 
141 	/*
142 	 * Now we need to get the page.  out_entry, out_prot, wired, and
143 	 * single_use aren't used.  One would think the vm code would be
144 	 * a *bit* nicer...  We use tmap because vm_map_lookup() can
145 	 * change the map argument.
146 	 */
147 
148 	tmap = map;
149 	rv = vm_map_lookup (&tmap, pageno, VM_PROT_WRITE, &out_entry,
150 		&object, &pindex, &out_prot, &wired);
151 	if (rv != KERN_SUCCESS) {
152 		return EINVAL;
153 	}
154 
155 	/*
156 	 * Okay, we've got the page.  Let's release tmap.
157 	 */
158 
159 	vm_map_lookup_done (tmap, out_entry);
160 
161 	/*
162 	 * Fault the page in...
163 	 */
164 
165 	rv = vm_fault(map, pageno, VM_PROT_WRITE|VM_PROT_READ, FALSE);
166 	if (rv != KERN_SUCCESS)
167 		return EFAULT;
168 
169 	/* Find space in kernel_map for the page we're interested in */
170 	rv = vm_map_find (kernel_map, object, IDX_TO_OFF(pindex),
171 		&kva, PAGE_SIZE, 0,
172 		VM_PROT_ALL, VM_PROT_ALL, 0);
173 	if (!rv) {
174 		vm_object_reference (object);
175 
176 		rv = vm_map_pageable (kernel_map, kva, kva + PAGE_SIZE, 0);
177 		if (!rv) {
178 		  bcopy (&datum, (caddr_t)kva + page_offset, sizeof datum);
179 		}
180 		vm_map_remove (kernel_map, kva, kva + PAGE_SIZE);
181 	}
182 
183 	if (fix_prot)
184 		vm_map_protect (map, pageno, pageno + PAGE_SIZE,
185 			VM_PROT_READ|VM_PROT_EXECUTE, 0);
186 	return rv;
187 }
188 #endif
189 
190 /*
191  * Process debugging system call.
192  */
193 #ifndef _SYS_SYSPROTO_H_
194 struct ptrace_args {
195 	int	req;
196 	pid_t	pid;
197 	caddr_t	addr;
198 	int	data;
199 };
200 #endif
201 
202 int
203 ptrace(curp, uap)
204 	struct proc *curp;
205 	struct ptrace_args *uap;
206 {
207 	struct proc *p;
208 	struct iovec iov;
209 	struct uio uio;
210 	int error = 0;
211 	int write;
212 
213 	write = 0;
214 	if (uap->req == PT_TRACE_ME)
215 		p = curp;
216 	else {
217 		if ((p = pfind(uap->pid)) == NULL)
218 			return ESRCH;
219 	}
220 	if (p_can(curp, p, P_CAN_SEE, NULL))
221 		return (ESRCH);
222 
223 	/*
224 	 * Permissions check
225 	 */
226 	switch (uap->req) {
227 	case PT_TRACE_ME:
228 		/* Always legal. */
229 		break;
230 
231 	case PT_ATTACH:
232 		/* Self */
233 		if (p->p_pid == curp->p_pid)
234 			return EINVAL;
235 
236 		/* Already traced */
237 		PROC_LOCK(p);
238 		if (p->p_flag & P_TRACED) {
239 			PROC_UNLOCK(p);
240 			return EBUSY;
241 		}
242 		PROC_UNLOCK(p);
243 
244 		if ((error = p_can(curp, p, P_CAN_DEBUG, NULL)))
245 			return error;
246 
247 		/* OK */
248 		break;
249 
250 	case PT_READ_I:
251 	case PT_READ_D:
252 	case PT_READ_U:
253 	case PT_WRITE_I:
254 	case PT_WRITE_D:
255 	case PT_WRITE_U:
256 	case PT_CONTINUE:
257 	case PT_KILL:
258 	case PT_STEP:
259 	case PT_DETACH:
260 #ifdef PT_GETREGS
261 	case PT_GETREGS:
262 #endif
263 #ifdef PT_SETREGS
264 	case PT_SETREGS:
265 #endif
266 #ifdef PT_GETFPREGS
267 	case PT_GETFPREGS:
268 #endif
269 #ifdef PT_SETFPREGS
270 	case PT_SETFPREGS:
271 #endif
272 #ifdef PT_GETDBREGS
273 	case PT_GETDBREGS:
274 #endif
275 #ifdef PT_SETDBREGS
276 	case PT_SETDBREGS:
277 #endif
278 		/* not being traced... */
279 		PROC_LOCK(p);
280 		if ((p->p_flag & P_TRACED) == 0) {
281 			PROC_UNLOCK(p);
282 			return EPERM;
283 		}
284 
285 		/* not being traced by YOU */
286 		if (p->p_pptr != curp) {
287 			PROC_UNLOCK(p);
288 			return EBUSY;
289 		}
290 
291 		/* not currently stopped */
292 		mtx_lock_spin(&sched_lock);
293 		if (p->p_stat != SSTOP || (p->p_flag & P_WAITED) == 0) {
294 			mtx_unlock_spin(&sched_lock);
295 			PROC_UNLOCK(p);
296 			return EBUSY;
297 		}
298 		mtx_unlock_spin(&sched_lock);
299 		PROC_UNLOCK(p);
300 
301 		/* OK */
302 		break;
303 
304 	default:
305 		return EINVAL;
306 	}
307 
308 #ifdef FIX_SSTEP
309 	/*
310 	 * Single step fixup ala procfs
311 	 */
312 	FIX_SSTEP(p);
313 #endif
314 
315 	/*
316 	 * Actually do the requests
317 	 */
318 
319 	curp->p_retval[0] = 0;
320 
321 	switch (uap->req) {
322 	case PT_TRACE_ME:
323 		/* set my trace flag and "owner" so it can read/write me */
324 		sx_xlock(&proctree_lock);
325 		PROC_LOCK(p);
326 		p->p_flag |= P_TRACED;
327 		p->p_oppid = p->p_pptr->p_pid;
328 		PROC_UNLOCK(p);
329 		sx_xunlock(&proctree_lock);
330 		return 0;
331 
332 	case PT_ATTACH:
333 		/* security check done above */
334 		sx_xlock(&proctree_lock);
335 		PROC_LOCK(p);
336 		p->p_flag |= P_TRACED;
337 		p->p_oppid = p->p_pptr->p_pid;
338 		if (p->p_pptr != curp)
339 			proc_reparent(p, curp);
340 		PROC_UNLOCK(p);
341 		sx_xunlock(&proctree_lock);
342 		uap->data = SIGSTOP;
343 		goto sendsig;	/* in PT_CONTINUE below */
344 
345 	case PT_STEP:
346 	case PT_CONTINUE:
347 	case PT_DETACH:
348 		if ((uap->req != PT_STEP) && ((unsigned)uap->data >= NSIG))
349 			return EINVAL;
350 
351 		PHOLD(p);
352 
353 		if (uap->req == PT_STEP) {
354 			if ((error = ptrace_single_step (p))) {
355 				PRELE(p);
356 				return error;
357 			}
358 		}
359 
360 		if (uap->addr != (caddr_t)1) {
361 			fill_kinfo_proc (p, &p->p_addr->u_kproc);
362 			if ((error = ptrace_set_pc (p,
363 			    (u_long)(uintfptr_t)uap->addr))) {
364 				PRELE(p);
365 				return error;
366 			}
367 		}
368 		PRELE(p);
369 
370 		if (uap->req == PT_DETACH) {
371 			/* reset process parent */
372 			sx_xlock(&proctree_lock);
373 			if (p->p_oppid != p->p_pptr->p_pid) {
374 				struct proc *pp;
375 
376 				pp = pfind(p->p_oppid);
377 				PROC_LOCK(p);
378 				proc_reparent(p, pp ? pp : initproc);
379 			} else
380 				PROC_LOCK(p);
381 			p->p_flag &= ~(P_TRACED | P_WAITED);
382 			p->p_oppid = 0;
383 
384 			PROC_UNLOCK(p);
385 			sx_xunlock(&proctree_lock);
386 
387 			/* should we send SIGCHLD? */
388 
389 		}
390 
391 	sendsig:
392 		/* deliver or queue signal */
393 		PROC_LOCK(p);
394 		mtx_lock_spin(&sched_lock);
395 		if (p->p_stat == SSTOP) {
396 			p->p_xstat = uap->data;
397 			setrunnable(p);
398 			mtx_unlock_spin(&sched_lock);
399 		} else {
400 			mtx_unlock_spin(&sched_lock);
401 			if (uap->data)
402 				psignal(p, uap->data);
403 
404 		}
405 		PROC_UNLOCK(p);
406 		return 0;
407 
408 	case PT_WRITE_I:
409 	case PT_WRITE_D:
410 		write = 1;
411 		/* fallthrough */
412 	case PT_READ_I:
413 	case PT_READ_D:
414 		/* write = 0 set above */
415 		iov.iov_base = write ? (caddr_t)&uap->data : (caddr_t)curp->p_retval;
416 		iov.iov_len = sizeof(int);
417 		uio.uio_iov = &iov;
418 		uio.uio_iovcnt = 1;
419 		uio.uio_offset = (off_t)(uintptr_t)uap->addr;
420 		uio.uio_resid = sizeof(int);
421 		uio.uio_segflg = UIO_SYSSPACE;	/* ie: the uap */
422 		uio.uio_rw = write ? UIO_WRITE : UIO_READ;
423 		uio.uio_procp = p;
424 		error = procfs_domem(curp, p, NULL, &uio);
425 		if (uio.uio_resid != 0) {
426 			/*
427 			 * XXX procfs_domem() doesn't currently return ENOSPC,
428 			 * so I think write() can bogusly return 0.
429 			 * XXX what happens for short writes?  We don't want
430 			 * to write partial data.
431 			 * XXX procfs_domem() returns EPERM for other invalid
432 			 * addresses.  Convert this to EINVAL.  Does this
433 			 * clobber returns of EPERM for other reasons?
434 			 */
435 			if (error == 0 || error == ENOSPC || error == EPERM)
436 				error = EINVAL;	/* EOF */
437 		}
438 		return (error);
439 
440 	case PT_READ_U:
441 		if ((uintptr_t)uap->addr > UPAGES * PAGE_SIZE - sizeof(int)) {
442 			return EFAULT;
443 		}
444 		if ((uintptr_t)uap->addr & (sizeof(int) - 1)) {
445 			return EFAULT;
446 		}
447 		if (ptrace_read_u_check(p,(vm_offset_t) uap->addr,
448 					sizeof(int))) {
449 			return EFAULT;
450 		}
451 		error = 0;
452 		PHOLD(p);	/* user had damn well better be incore! */
453 		mtx_lock_spin(&sched_lock);
454 		if (p->p_sflag & PS_INMEM) {
455 			mtx_unlock_spin(&sched_lock);
456 			fill_kinfo_proc (p, &p->p_addr->u_kproc);
457 			curp->p_retval[0] = *(int *)
458 			    ((uintptr_t)p->p_addr + (uintptr_t)uap->addr);
459 		} else {
460 			mtx_unlock_spin(&sched_lock);
461 			curp->p_retval[0] = 0;
462 			error = EFAULT;
463 		}
464 		PRELE(p);
465 		return error;
466 
467 	case PT_WRITE_U:
468 		PHOLD(p);	/* user had damn well better be incore! */
469 		mtx_lock_spin(&sched_lock);
470 		if (p->p_sflag & PS_INMEM) {
471 			mtx_unlock_spin(&sched_lock);
472 			fill_kinfo_proc (p, &p->p_addr->u_kproc);
473 			error = ptrace_write_u(p, (vm_offset_t)uap->addr, uap->data);
474 		} else {
475 			mtx_unlock_spin(&sched_lock);
476 			error = EFAULT;
477 		}
478 		PRELE(p);
479 		return error;
480 
481 	case PT_KILL:
482 		uap->data = SIGKILL;
483 		goto sendsig;	/* in PT_CONTINUE above */
484 
485 #ifdef PT_SETREGS
486 	case PT_SETREGS:
487 		write = 1;
488 		/* fallthrough */
489 #endif /* PT_SETREGS */
490 #ifdef PT_GETREGS
491 	case PT_GETREGS:
492 		/* write = 0 above */
493 #endif /* PT_SETREGS */
494 #if defined(PT_SETREGS) || defined(PT_GETREGS)
495 		if (!procfs_validregs(p))	/* no P_SYSTEM procs please */
496 			return EINVAL;
497 		else {
498 			iov.iov_base = uap->addr;
499 			iov.iov_len = sizeof(struct reg);
500 			uio.uio_iov = &iov;
501 			uio.uio_iovcnt = 1;
502 			uio.uio_offset = 0;
503 			uio.uio_resid = sizeof(struct reg);
504 			uio.uio_segflg = UIO_USERSPACE;
505 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
506 			uio.uio_procp = curp;
507 			return (procfs_doregs(curp, p, NULL, &uio));
508 		}
509 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
510 
511 #ifdef PT_SETFPREGS
512 	case PT_SETFPREGS:
513 		write = 1;
514 		/* fallthrough */
515 #endif /* PT_SETFPREGS */
516 #ifdef PT_GETFPREGS
517 	case PT_GETFPREGS:
518 		/* write = 0 above */
519 #endif /* PT_SETFPREGS */
520 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
521 		if (!procfs_validfpregs(p))	/* no P_SYSTEM procs please */
522 			return EINVAL;
523 		else {
524 			iov.iov_base = uap->addr;
525 			iov.iov_len = sizeof(struct fpreg);
526 			uio.uio_iov = &iov;
527 			uio.uio_iovcnt = 1;
528 			uio.uio_offset = 0;
529 			uio.uio_resid = sizeof(struct fpreg);
530 			uio.uio_segflg = UIO_USERSPACE;
531 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
532 			uio.uio_procp = curp;
533 			return (procfs_dofpregs(curp, p, NULL, &uio));
534 		}
535 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
536 
537 #ifdef PT_SETDBREGS
538 	case PT_SETDBREGS:
539 		write = 1;
540 		/* fallthrough */
541 #endif /* PT_SETDBREGS */
542 #ifdef PT_GETDBREGS
543 	case PT_GETDBREGS:
544 		/* write = 0 above */
545 #endif /* PT_SETDBREGS */
546 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
547 		if (!procfs_validdbregs(p))	/* no P_SYSTEM procs please */
548 			return EINVAL;
549 		else {
550 			iov.iov_base = uap->addr;
551 			iov.iov_len = sizeof(struct dbreg);
552 			uio.uio_iov = &iov;
553 			uio.uio_iovcnt = 1;
554 			uio.uio_offset = 0;
555 			uio.uio_resid = sizeof(struct dbreg);
556 			uio.uio_segflg = UIO_USERSPACE;
557 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
558 			uio.uio_procp = curp;
559 			return (procfs_dodbregs(curp, p, NULL, &uio));
560 		}
561 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
562 
563 	default:
564 		break;
565 	}
566 
567 	return 0;
568 }
569 
570 int
571 trace_req(p)
572 	struct proc *p;
573 {
574 	return 1;
575 }
576 
577 /*
578  * stopevent()
579  * Stop a process because of a procfs event;
580  * stay stopped until p->p_step is cleared
581  * (cleared by PIOCCONT in procfs).
582  *
583  * Must be called with the proc struct mutex held.
584  */
585 
586 void
587 stopevent(p, event, val)
588 	struct proc *p;
589 	unsigned int event;
590 	unsigned int val;
591 {
592 
593 	PROC_LOCK_ASSERT(p, MA_OWNED | MA_NOTRECURSED);
594 	p->p_step = 1;
595 
596 	do {
597 		p->p_xstat = val;
598 		p->p_stype = event;	/* Which event caused the stop? */
599 		wakeup(&p->p_stype);	/* Wake up any PIOCWAIT'ing procs */
600 		msleep(&p->p_step, &p->p_mtx, PWAIT, "stopevent", 0);
601 	} while (p->p_step);
602 }
603