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