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