xref: /freebsd/sys/kern/sys_process.c (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
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  *	$Id: sys_process.c,v 1.44 1999/04/27 11:16:13 phk Exp $
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 	if (uap->req == PT_TRACE_ME)
216 		p = curp;
217 	else {
218 		if ((p = pfind(uap->pid)) == NULL)
219 			return ESRCH;
220 	}
221 	if (!PRISON_CHECK(curp, p))
222 		return (ESRCH);
223 
224 	/*
225 	 * Permissions check
226 	 */
227 	switch (uap->req) {
228 	case PT_TRACE_ME:
229 		/* Always legal. */
230 		break;
231 
232 	case PT_ATTACH:
233 		/* Self */
234 		if (p->p_pid == curp->p_pid)
235 			return EINVAL;
236 
237 		/* Already traced */
238 		if (p->p_flag & P_TRACED)
239 			return EBUSY;
240 
241 		/* not owned by you, has done setuid (unless you're root) */
242 		if ((p->p_cred->p_ruid != curp->p_cred->p_ruid) ||
243 		     (p->p_flag & P_SUGID)) {
244 			if ((error = suser(curp)) != 0)
245 				return error;
246 		}
247 
248 		/* can't trace init when securelevel > 0 */
249 		if (securelevel > 0 && p->p_pid == 1)
250 			return EPERM;
251 
252 		/* OK */
253 		break;
254 
255 	case PT_READ_I:
256 	case PT_READ_D:
257 	case PT_READ_U:
258 	case PT_WRITE_I:
259 	case PT_WRITE_D:
260 	case PT_WRITE_U:
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 		/* not being traced... */
278 		if ((p->p_flag & P_TRACED) == 0)
279 			return EPERM;
280 
281 		/* not being traced by YOU */
282 		if (p->p_pptr != curp)
283 			return EBUSY;
284 
285 		/* not currently stopped */
286 		if (p->p_stat != SSTOP || (p->p_flag & P_WAITED) == 0)
287 			return EBUSY;
288 
289 		/* OK */
290 		break;
291 
292 	default:
293 		return EINVAL;
294 	}
295 
296 #ifdef FIX_SSTEP
297 	/*
298 	 * Single step fixup ala procfs
299 	 */
300 	FIX_SSTEP(p);
301 #endif
302 
303 	/*
304 	 * Actually do the requests
305 	 */
306 
307 	write = 0;
308 	curp->p_retval[0] = 0;
309 
310 	switch (uap->req) {
311 	case PT_TRACE_ME:
312 		/* set my trace flag and "owner" so it can read/write me */
313 		p->p_flag |= P_TRACED;
314 		p->p_oppid = p->p_pptr->p_pid;
315 		return 0;
316 
317 	case PT_ATTACH:
318 		/* security check done above */
319 		p->p_flag |= P_TRACED;
320 		p->p_oppid = p->p_pptr->p_pid;
321 		if (p->p_pptr != curp)
322 			proc_reparent(p, curp);
323 		uap->data = SIGSTOP;
324 		goto sendsig;	/* in PT_CONTINUE below */
325 
326 	case PT_STEP:
327 	case PT_CONTINUE:
328 	case PT_DETACH:
329 		if ((unsigned)uap->data >= NSIG)
330 			return EINVAL;
331 
332 		PHOLD(p);
333 
334 		if (uap->req == PT_STEP) {
335 			if ((error = ptrace_single_step (p))) {
336 				PRELE(p);
337 				return error;
338 			}
339 		}
340 
341 		if (uap->addr != (caddr_t)1) {
342 			fill_eproc (p, &p->p_addr->u_kproc.kp_eproc);
343 			if ((error = ptrace_set_pc (p,
344 			    (u_long)(uintfptr_t)uap->addr))) {
345 				PRELE(p);
346 				return error;
347 			}
348 		}
349 		PRELE(p);
350 
351 		if (uap->req == PT_DETACH) {
352 			/* reset process parent */
353 			if (p->p_oppid != p->p_pptr->p_pid) {
354 				struct proc *pp;
355 
356 				pp = pfind(p->p_oppid);
357 				proc_reparent(p, pp ? pp : initproc);
358 			}
359 
360 			p->p_flag &= ~(P_TRACED | P_WAITED);
361 			p->p_oppid = 0;
362 
363 			/* should we send SIGCHLD? */
364 
365 		}
366 
367 	sendsig:
368 		/* deliver or queue signal */
369 		s = splhigh();
370 		if (p->p_stat == SSTOP) {
371 			p->p_xstat = uap->data;
372 			setrunnable(p);
373 		} else if (uap->data) {
374 			psignal(p, uap->data);
375 		}
376 		splx(s);
377 		return 0;
378 
379 	case PT_WRITE_I:
380 	case PT_WRITE_D:
381 		write = 1;
382 		/* fallthrough */
383 	case PT_READ_I:
384 	case PT_READ_D:
385 		/* write = 0 set above */
386 		iov.iov_base = write ? (caddr_t)&uap->data : (caddr_t)curp->p_retval;
387 		iov.iov_len = sizeof(int);
388 		uio.uio_iov = &iov;
389 		uio.uio_iovcnt = 1;
390 		uio.uio_offset = (off_t)(uintptr_t)uap->addr;
391 		uio.uio_resid = sizeof(int);
392 		uio.uio_segflg = UIO_SYSSPACE;	/* ie: the uap */
393 		uio.uio_rw = write ? UIO_WRITE : UIO_READ;
394 		uio.uio_procp = p;
395 		error = procfs_domem(curp, p, NULL, &uio);
396 		if (uio.uio_resid != 0) {
397 			/*
398 			 * XXX procfs_domem() doesn't currently return ENOSPC,
399 			 * so I think write() can bogusly return 0.
400 			 * XXX what happens for short writes?  We don't want
401 			 * to write partial data.
402 			 * XXX procfs_domem() returns EPERM for other invalid
403 			 * addresses.  Convert this to EINVAL.  Does this
404 			 * clobber returns of EPERM for other reasons?
405 			 */
406 			if (error == 0 || error == ENOSPC || error == EPERM)
407 				error = EINVAL;	/* EOF */
408 		}
409 		return (error);
410 
411 	case PT_READ_U:
412 		if ((uintptr_t)uap->addr > UPAGES * PAGE_SIZE - sizeof(int)) {
413 			return EFAULT;
414 		}
415 		if ((uintptr_t)uap->addr & (sizeof(int) - 1)) {
416 			return EFAULT;
417 		}
418 		if (ptrace_read_u_check(p,(vm_offset_t) uap->addr,
419 					sizeof(int)) &&
420 		    !procfs_kmemaccess(curp)) {
421 			return EFAULT;
422 		}
423 		error = 0;
424 		PHOLD(p);	/* user had damn well better be incore! */
425 		if (p->p_flag & P_INMEM) {
426 			p->p_addr->u_kproc.kp_proc = *p;
427 			fill_eproc (p, &p->p_addr->u_kproc.kp_eproc);
428 			curp->p_retval[0] = *(int *)
429 			    ((uintptr_t)p->p_addr + (uintptr_t)uap->addr);
430 		} else {
431 			curp->p_retval[0] = 0;
432 			error = EFAULT;
433 		}
434 		PRELE(p);
435 		return error;
436 
437 	case PT_WRITE_U:
438 		PHOLD(p);	/* user had damn well better be incore! */
439 		if (p->p_flag & P_INMEM) {
440 			p->p_addr->u_kproc.kp_proc = *p;
441 			fill_eproc (p, &p->p_addr->u_kproc.kp_eproc);
442 			error = ptrace_write_u(p, (vm_offset_t)uap->addr, uap->data);
443 		} else {
444 			error = EFAULT;
445 		}
446 		PRELE(p);
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 	default:
506 		break;
507 	}
508 
509 	return 0;
510 }
511 
512 int
513 trace_req(p)
514 	struct proc *p;
515 {
516 	return 1;
517 }
518 
519 /*
520  * stopevent()
521  * Stop a process because of a procfs event;
522  * stay stopped until p->p_step is cleared
523  * (cleared by PIOCCONT in procfs).
524  */
525 
526 void
527 stopevent(struct proc *p, unsigned int event, unsigned int val) {
528 	p->p_step = 1;
529 
530 	do {
531 		p->p_xstat = val;
532 		p->p_stype = event;	/* Which event caused the stop? */
533 		wakeup(&p->p_stype);	/* Wake up any PIOCWAIT'ing procs */
534 		tsleep(&p->p_step, PWAIT, "stopevent", 0);
535 	} while (p->p_step);
536 }
537