xref: /dragonfly/sys/kern/sys_process.c (revision 851dc90d)
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: src/sys/kern/sys_process.c,v 1.51.2.6 2003/01/08 03:06:45 kan Exp $
32  * $DragonFly: src/sys/kern/sys_process.c,v 1.14 2003/10/15 21:52:38 dillon Exp $
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/sysproto.h>
38 #include <sys/proc.h>
39 #include <sys/vnode.h>
40 #include <sys/ptrace.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 <vfs/procfs/procfs.h>
51 #include <sys/thread2.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, 0);
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_wire (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, 0);
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_wire (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 int
195 ptrace(struct ptrace_args *uap)
196 {
197 	struct proc *p = curproc;
198 
199 	/*
200 	 * XXX this obfuscation is to reduce stack usage, but the register
201 	 * structs may be too large to put on the stack anyway.
202 	 */
203 	union {
204 		struct ptrace_io_desc piod;
205 		struct dbreg dbreg;
206 		struct fpreg fpreg;
207 		struct reg reg;
208 	} r;
209 	void *addr;
210 	int error = 0;
211 
212 	addr = &r;
213 	switch (uap->req) {
214 	case PT_GETREGS:
215 	case PT_GETFPREGS:
216 #ifdef PT_GETDBREGS
217 	case PT_GETDBREGS:
218 #endif
219 		break;
220 	case PT_SETREGS:
221 		error = copyin(uap->addr, &r.reg, sizeof r.reg);
222 		break;
223 	case PT_SETFPREGS:
224 		error = copyin(uap->addr, &r.fpreg, sizeof r.fpreg);
225 		break;
226 #ifdef PT_SETDBREGS
227 	case PT_SETDBREGS:
228 		error = copyin(uap->addr, &r.dbreg, sizeof r.dbreg);
229 		break;
230 #endif
231 	case PT_IO:
232 		error = copyin(uap->addr, &r.piod, sizeof r.piod);
233 		break;
234 	default:
235 		addr = uap->addr;
236 	}
237 	if (error)
238 		return (error);
239 
240 	error = kern_ptrace(p, uap->req, uap->pid, addr, uap->data,
241 			&uap->sysmsg_result);
242 	if (error)
243 		return (error);
244 
245 	switch (uap->req) {
246 	case PT_IO:
247 		(void)copyout(&r.piod, uap->addr, sizeof r.piod);
248 		break;
249 	case PT_GETREGS:
250 		error = copyout(&r.reg, uap->addr, sizeof r.reg);
251 		break;
252 	case PT_GETFPREGS:
253 		error = copyout(&r.fpreg, uap->addr, sizeof r.fpreg);
254 		break;
255 #ifdef PT_GETDBREGS
256 	case PT_GETDBREGS:
257 		error = copyout(&r.dbreg, uap->addr, sizeof r.dbreg);
258 		break;
259 #endif
260 	}
261 
262 	return (error);
263 }
264 
265 int
266 kern_ptrace(struct proc *curp, int req, pid_t pid, void *addr, int data, int *res)
267 {
268 	struct proc *p, *pp;
269 	struct iovec iov;
270 	struct uio uio;
271 	struct ptrace_io_desc *piod;
272 	int error = 0;
273 	int write, tmp, s;
274 
275 	write = 0;
276 	if (req == PT_TRACE_ME) {
277 		p = curp;
278 	} else {
279 		if ((p = pfind(pid)) == NULL)
280 			return ESRCH;
281 	}
282 	if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
283 		return (ESRCH);
284 
285 	/* Can't trace a process that's currently exec'ing. */
286 	if ((p->p_flag & P_INEXEC) != 0)
287 		return EAGAIN;
288 
289 	/*
290 	 * Permissions check
291 	 */
292 	switch (req) {
293 	case PT_TRACE_ME:
294 		/* Always legal. */
295 		break;
296 
297 	case PT_ATTACH:
298 		/* Self */
299 		if (p->p_pid == curp->p_pid)
300 			return EINVAL;
301 
302 		/* Already traced */
303 		if (p->p_flag & P_TRACED)
304 			return EBUSY;
305 
306 		if (curp->p_flag & P_TRACED)
307 			for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr)
308 				if (pp == p)
309 					return (EINVAL);
310 
311 		/* not owned by you, has done setuid (unless you're root) */
312 		if ((p->p_ucred->cr_ruid != curp->p_ucred->cr_ruid) ||
313 		     (p->p_flag & P_SUGID)) {
314 			if ((error = suser(curp->p_thread)) != 0)
315 				return error;
316 		}
317 
318 		/* can't trace init when securelevel > 0 */
319 		if (securelevel > 0 && p->p_pid == 1)
320 			return EPERM;
321 
322 		/* OK */
323 		break;
324 
325 	case PT_READ_I:
326 	case PT_READ_D:
327 	case PT_WRITE_I:
328 	case PT_WRITE_D:
329 	case PT_IO:
330 	case PT_CONTINUE:
331 	case PT_KILL:
332 	case PT_STEP:
333 	case PT_DETACH:
334 #ifdef PT_GETREGS
335 	case PT_GETREGS:
336 #endif
337 #ifdef PT_SETREGS
338 	case PT_SETREGS:
339 #endif
340 #ifdef PT_GETFPREGS
341 	case PT_GETFPREGS:
342 #endif
343 #ifdef PT_SETFPREGS
344 	case PT_SETFPREGS:
345 #endif
346 #ifdef PT_GETDBREGS
347 	case PT_GETDBREGS:
348 #endif
349 #ifdef PT_SETDBREGS
350 	case PT_SETDBREGS:
351 #endif
352 		/* not being traced... */
353 		if ((p->p_flag & P_TRACED) == 0)
354 			return EPERM;
355 
356 		/* not being traced by YOU */
357 		if (p->p_pptr != curp)
358 			return EBUSY;
359 
360 		/* not currently stopped */
361 		if (p->p_stat != SSTOP || (p->p_flag & P_WAITED) == 0)
362 			return EBUSY;
363 
364 		/* OK */
365 		break;
366 
367 	default:
368 		return EINVAL;
369 	}
370 
371 #ifdef FIX_SSTEP
372 	/*
373 	 * Single step fixup ala procfs
374 	 */
375 	FIX_SSTEP(p);
376 #endif
377 
378 	/*
379 	 * Actually do the requests
380 	 */
381 
382 	*res = 0;
383 
384 	switch (req) {
385 	case PT_TRACE_ME:
386 		/* set my trace flag and "owner" so it can read/write me */
387 		p->p_flag |= P_TRACED;
388 		p->p_oppid = p->p_pptr->p_pid;
389 		return 0;
390 
391 	case PT_ATTACH:
392 		/* security check done above */
393 		p->p_flag |= P_TRACED;
394 		p->p_oppid = p->p_pptr->p_pid;
395 		if (p->p_pptr != curp)
396 			proc_reparent(p, curp);
397 		data = SIGSTOP;
398 		goto sendsig;	/* in PT_CONTINUE below */
399 
400 	case PT_STEP:
401 	case PT_CONTINUE:
402 	case PT_DETACH:
403 		/* Zero means do not send any signal */
404 		if (data < 0 || data > _SIG_MAXSIG)
405 			return EINVAL;
406 
407 		PHOLD(p);
408 
409 		if (req == PT_STEP) {
410 			if ((error = ptrace_single_step (p))) {
411 				PRELE(p);
412 				return error;
413 			}
414 		}
415 
416 		if (addr != (void *)1) {
417 			if ((error = ptrace_set_pc (p,
418 			    (u_long)(uintfptr_t)addr))) {
419 				PRELE(p);
420 				return error;
421 			}
422 		}
423 		PRELE(p);
424 
425 		if (req == PT_DETACH) {
426 			/* reset process parent */
427 			if (p->p_oppid != p->p_pptr->p_pid) {
428 				struct proc *pp;
429 
430 				pp = pfind(p->p_oppid);
431 				proc_reparent(p, pp ? pp : initproc);
432 			}
433 
434 			p->p_flag &= ~(P_TRACED | P_WAITED);
435 			p->p_oppid = 0;
436 
437 			/* should we send SIGCHLD? */
438 		}
439 
440 	sendsig:
441 		/* deliver or queue signal */
442 		s = splhigh();
443 		if (p->p_stat == SSTOP) {
444 			p->p_xstat = data;
445 			setrunnable(p);
446 		} else if (data) {
447 			psignal(p, data);
448 		}
449 		splx(s);
450 		return 0;
451 
452 	case PT_WRITE_I:
453 	case PT_WRITE_D:
454 		write = 1;
455 		/* fallthrough */
456 	case PT_READ_I:
457 	case PT_READ_D:
458 		/*
459 		 * NOTE! uio_offset represents the offset in the target
460 		 * process.  The iov is in the current process (the guy
461 		 * making the ptrace call) so uio_td must be the current
462 		 * process (though for a SYSSPACE transfer it doesn't
463 		 * really matter).
464 		 */
465 		tmp = 0;
466 		/* write = 0 set above */
467 		iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
468 		iov.iov_len = sizeof(int);
469 		uio.uio_iov = &iov;
470 		uio.uio_iovcnt = 1;
471 		uio.uio_offset = (off_t)(uintptr_t)addr;
472 		uio.uio_resid = sizeof(int);
473 		uio.uio_segflg = UIO_SYSSPACE;
474 		uio.uio_rw = write ? UIO_WRITE : UIO_READ;
475 		uio.uio_td = curp->p_thread;
476 		error = procfs_domem(curp, p, NULL, &uio);
477 		if (uio.uio_resid != 0) {
478 			/*
479 			 * XXX procfs_domem() doesn't currently return ENOSPC,
480 			 * so I think write() can bogusly return 0.
481 			 * XXX what happens for short writes?  We don't want
482 			 * to write partial data.
483 			 * XXX procfs_domem() returns EPERM for other invalid
484 			 * addresses.  Convert this to EINVAL.  Does this
485 			 * clobber returns of EPERM for other reasons?
486 			 */
487 			if (error == 0 || error == ENOSPC || error == EPERM)
488 				error = EINVAL;	/* EOF */
489 		}
490 		if (!write)
491 			*res = tmp;
492 		return (error);
493 
494 	case PT_IO:
495 		/*
496 		 * NOTE! uio_offset represents the offset in the target
497 		 * process.  The iov is in the current process (the guy
498 		 * making the ptrace call) so uio_td must be the current
499 		 * process.
500 		 */
501 		piod = addr;
502 		iov.iov_base = piod->piod_addr;
503 		iov.iov_len = piod->piod_len;
504 		uio.uio_iov = &iov;
505 		uio.uio_iovcnt = 1;
506 		uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
507 		uio.uio_resid = piod->piod_len;
508 		uio.uio_segflg = UIO_USERSPACE;
509 		uio.uio_td = curp->p_thread;
510 		switch (piod->piod_op) {
511 		case PIOD_READ_D:
512 		case PIOD_READ_I:
513 			uio.uio_rw = UIO_READ;
514 			break;
515 		case PIOD_WRITE_D:
516 		case PIOD_WRITE_I:
517 			uio.uio_rw = UIO_WRITE;
518 			break;
519 		default:
520 			return (EINVAL);
521 		}
522 		error = procfs_domem(curp, p, NULL, &uio);
523 		piod->piod_len -= uio.uio_resid;
524 		return (error);
525 
526 	case PT_KILL:
527 		data = SIGKILL;
528 		goto sendsig;	/* in PT_CONTINUE above */
529 
530 #ifdef PT_SETREGS
531 	case PT_SETREGS:
532 		write = 1;
533 		/* fallthrough */
534 #endif /* PT_SETREGS */
535 #ifdef PT_GETREGS
536 	case PT_GETREGS:
537 		/* write = 0 above */
538 #endif /* PT_SETREGS */
539 #if defined(PT_SETREGS) || defined(PT_GETREGS)
540 		if (!procfs_validregs(p))	/* no P_SYSTEM procs please */
541 			return EINVAL;
542 		else {
543 			iov.iov_base = addr;
544 			iov.iov_len = sizeof(struct reg);
545 			uio.uio_iov = &iov;
546 			uio.uio_iovcnt = 1;
547 			uio.uio_offset = 0;
548 			uio.uio_resid = sizeof(struct reg);
549 			uio.uio_segflg = UIO_SYSSPACE;
550 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
551 			uio.uio_td = curp->p_thread;
552 			return (procfs_doregs(curp, p, NULL, &uio));
553 		}
554 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
555 
556 #ifdef PT_SETFPREGS
557 	case PT_SETFPREGS:
558 		write = 1;
559 		/* fallthrough */
560 #endif /* PT_SETFPREGS */
561 #ifdef PT_GETFPREGS
562 	case PT_GETFPREGS:
563 		/* write = 0 above */
564 #endif /* PT_SETFPREGS */
565 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
566 		if (!procfs_validfpregs(p))	/* no P_SYSTEM procs please */
567 			return EINVAL;
568 		else {
569 			iov.iov_base = addr;
570 			iov.iov_len = sizeof(struct fpreg);
571 			uio.uio_iov = &iov;
572 			uio.uio_iovcnt = 1;
573 			uio.uio_offset = 0;
574 			uio.uio_resid = sizeof(struct fpreg);
575 			uio.uio_segflg = UIO_SYSSPACE;
576 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
577 			uio.uio_td = curp->p_thread;
578 			return (procfs_dofpregs(curp, p, NULL, &uio));
579 		}
580 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
581 
582 #ifdef PT_SETDBREGS
583 	case PT_SETDBREGS:
584 		write = 1;
585 		/* fallthrough */
586 #endif /* PT_SETDBREGS */
587 #ifdef PT_GETDBREGS
588 	case PT_GETDBREGS:
589 		/* write = 0 above */
590 #endif /* PT_SETDBREGS */
591 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
592 		if (!procfs_validdbregs(p))	/* no P_SYSTEM procs please */
593 			return EINVAL;
594 		else {
595 			iov.iov_base = addr;
596 			iov.iov_len = sizeof(struct dbreg);
597 			uio.uio_iov = &iov;
598 			uio.uio_iovcnt = 1;
599 			uio.uio_offset = 0;
600 			uio.uio_resid = sizeof(struct dbreg);
601 			uio.uio_segflg = UIO_SYSSPACE;
602 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
603 			uio.uio_td = curp->p_thread;
604 			return (procfs_dodbregs(curp, p, NULL, &uio));
605 		}
606 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
607 
608 	default:
609 		break;
610 	}
611 
612 	return 0;
613 }
614 
615 int
616 trace_req(p)
617 	struct proc *p;
618 {
619 	return 1;
620 }
621 
622 /*
623  * stopevent()
624  * Stop a process because of a procfs event;
625  * stay stopped until p->p_step is cleared
626  * (cleared by PIOCCONT in procfs).
627  */
628 
629 void
630 stopevent(struct proc *p, unsigned int event, unsigned int val)
631 {
632 	p->p_step = 1;
633 
634 	do {
635 		crit_enter();
636 		wakeup(&p->p_stype);	/* Wake up any PIOCWAIT'ing procs */
637 		p->p_xstat = val;
638 		p->p_stype = event;	/* Which event caused the stop? */
639 		tsleep(&p->p_step, 0, "stopevent", 0);
640 		crit_exit();
641 	} while (p->p_step);
642 }
643 
644