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