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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD: releng/8.3/sys/kern/sys_process.c 219144 2011-03-01 20:44:14Z dchagin $");
34
35 #include "opt_compat.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/syscallsubr.h>
42 #include <sys/sysent.h>
43 #include <sys/sysproto.h>
44 #include <sys/proc.h>
45 #include <sys/vnode.h>
46 #include <sys/ptrace.h>
47 #include <sys/sx.h>
48 #include <sys/malloc.h>
49 #include <sys/signalvar.h>
50
51 #include <machine/reg.h>
52
53 #include <security/audit/audit.h>
54
55 #include <vm/vm.h>
56 #include <vm/pmap.h>
57 #include <vm/vm_extern.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_kern.h>
60 #include <vm/vm_object.h>
61 #include <vm/vm_page.h>
62 #include <vm/vm_param.h>
63
64 #ifdef COMPAT_FREEBSD32
65 #include <sys/procfs.h>
66 #include <compat/freebsd32/freebsd32_signal.h>
67
68 struct ptrace_io_desc32 {
69 int piod_op;
70 u_int32_t piod_offs;
71 u_int32_t piod_addr;
72 u_int32_t piod_len;
73 };
74
75 struct ptrace_vm_entry32 {
76 int pve_entry;
77 int pve_timestamp;
78 uint32_t pve_start;
79 uint32_t pve_end;
80 uint32_t pve_offset;
81 u_int pve_prot;
82 u_int pve_pathlen;
83 int32_t pve_fileid;
84 u_int pve_fsid;
85 uint32_t pve_path;
86 };
87
88 struct ptrace_lwpinfo32 {
89 lwpid_t pl_lwpid; /* LWP described. */
90 int pl_event; /* Event that stopped the LWP. */
91 int pl_flags; /* LWP flags. */
92 sigset_t pl_sigmask; /* LWP signal mask */
93 sigset_t pl_siglist; /* LWP pending signal */
94 struct siginfo32 pl_siginfo; /* siginfo for signal */
95 char pl_tdname[MAXCOMLEN + 1]; /* LWP name. */
96 int pl_child_pid; /* New child pid */
97 };
98
99 #endif
100
101 /*
102 * Functions implemented using PROC_ACTION():
103 *
104 * proc_read_regs(proc, regs)
105 * Get the current user-visible register set from the process
106 * and copy it into the regs structure (<machine/reg.h>).
107 * The process is stopped at the time read_regs is called.
108 *
109 * proc_write_regs(proc, regs)
110 * Update the current register set from the passed in regs
111 * structure. Take care to avoid clobbering special CPU
112 * registers or privileged bits in the PSL.
113 * Depending on the architecture this may have fix-up work to do,
114 * especially if the IAR or PCW are modified.
115 * The process is stopped at the time write_regs is called.
116 *
117 * proc_read_fpregs, proc_write_fpregs
118 * deal with the floating point register set, otherwise as above.
119 *
120 * proc_read_dbregs, proc_write_dbregs
121 * deal with the processor debug register set, otherwise as above.
122 *
123 * proc_sstep(proc)
124 * Arrange for the process to trap after executing a single instruction.
125 */
126
127 #define PROC_ACTION(action) do { \
128 int error; \
129 \
130 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); \
131 if ((td->td_proc->p_flag & P_INMEM) == 0) \
132 error = EIO; \
133 else \
134 error = (action); \
135 return (error); \
136 } while(0)
137
138 int
139 proc_read_regs(struct thread *td, struct reg *regs)
140 {
141
142 PROC_ACTION(fill_regs(td, regs));
143 }
144
145 int
146 proc_write_regs(struct thread *td, struct reg *regs)
147 {
148
149 PROC_ACTION(set_regs(td, regs));
150 }
151
152 int
153 proc_read_dbregs(struct thread *td, struct dbreg *dbregs)
154 {
155
156 PROC_ACTION(fill_dbregs(td, dbregs));
157 }
158
159 int
160 proc_write_dbregs(struct thread *td, struct dbreg *dbregs)
161 {
162
163 PROC_ACTION(set_dbregs(td, dbregs));
164 }
165
166 /*
167 * Ptrace doesn't support fpregs at all, and there are no security holes
168 * or translations for fpregs, so we can just copy them.
169 */
170 int
171 proc_read_fpregs(struct thread *td, struct fpreg *fpregs)
172 {
173
174 PROC_ACTION(fill_fpregs(td, fpregs));
175 }
176
177 int
178 proc_write_fpregs(struct thread *td, struct fpreg *fpregs)
179 {
180
181 PROC_ACTION(set_fpregs(td, fpregs));
182 }
183
184 #ifdef COMPAT_FREEBSD32
185 /* For 32 bit binaries, we need to expose the 32 bit regs layouts. */
186 int
187 proc_read_regs32(struct thread *td, struct reg32 *regs32)
188 {
189
190 PROC_ACTION(fill_regs32(td, regs32));
191 }
192
193 int
194 proc_write_regs32(struct thread *td, struct reg32 *regs32)
195 {
196
197 PROC_ACTION(set_regs32(td, regs32));
198 }
199
200 int
201 proc_read_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
202 {
203
204 PROC_ACTION(fill_dbregs32(td, dbregs32));
205 }
206
207 int
208 proc_write_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
209 {
210
211 PROC_ACTION(set_dbregs32(td, dbregs32));
212 }
213
214 int
215 proc_read_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
216 {
217
218 PROC_ACTION(fill_fpregs32(td, fpregs32));
219 }
220
221 int
222 proc_write_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
223 {
224
225 PROC_ACTION(set_fpregs32(td, fpregs32));
226 }
227 #endif
228
229 int
230 proc_sstep(struct thread *td)
231 {
232
233 PROC_ACTION(ptrace_single_step(td));
234 }
235
236 int
237 proc_rwmem(struct proc *p, struct uio *uio)
238 {
239 vm_map_t map;
240 vm_object_t backing_object, object = NULL;
241 vm_offset_t pageno = 0; /* page number */
242 vm_prot_t reqprot;
243 int error, fault_flags, writing;
244
245 /*
246 * Assert that someone has locked this vmspace. (Should be
247 * curthread but we can't assert that.) This keeps the process
248 * from exiting out from under us until this operation completes.
249 */
250 KASSERT(p->p_lock >= 1, ("%s: process %p (pid %d) not held", __func__,
251 p, p->p_pid));
252
253 /*
254 * The map we want...
255 */
256 map = &p->p_vmspace->vm_map;
257
258 writing = uio->uio_rw == UIO_WRITE;
259 reqprot = writing ? (VM_PROT_WRITE | VM_PROT_OVERRIDE_WRITE) :
260 VM_PROT_READ;
261 fault_flags = writing ? VM_FAULT_DIRTY : VM_FAULT_NORMAL;
262
263 /*
264 * Only map in one page at a time. We don't have to, but it
265 * makes things easier. This way is trivial - right?
266 */
267 do {
268 vm_map_t tmap;
269 vm_offset_t uva;
270 int page_offset; /* offset into page */
271 vm_map_entry_t out_entry;
272 vm_prot_t out_prot;
273 boolean_t wired;
274 vm_pindex_t pindex;
275 u_int len;
276 vm_page_t m;
277
278 object = NULL;
279
280 uva = (vm_offset_t)uio->uio_offset;
281
282 /*
283 * Get the page number of this segment.
284 */
285 pageno = trunc_page(uva);
286 page_offset = uva - pageno;
287
288 /*
289 * How many bytes to copy
290 */
291 len = min(PAGE_SIZE - page_offset, uio->uio_resid);
292
293 /*
294 * Fault the page on behalf of the process
295 */
296 error = vm_fault(map, pageno, reqprot, fault_flags);
297 if (error) {
298 if (error == KERN_RESOURCE_SHORTAGE)
299 error = ENOMEM;
300 else
301 error = EFAULT;
302 break;
303 }
304
305 /*
306 * Now we need to get the page. out_entry, out_prot, wired,
307 * and single_use aren't used. One would think the vm code
308 * would be a *bit* nicer... We use tmap because
309 * vm_map_lookup() can change the map argument.
310 */
311 tmap = map;
312 error = vm_map_lookup(&tmap, pageno, reqprot, &out_entry,
313 &object, &pindex, &out_prot, &wired);
314 if (error) {
315 error = EFAULT;
316 break;
317 }
318 VM_OBJECT_LOCK(object);
319 while ((m = vm_page_lookup(object, pindex)) == NULL &&
320 !writing &&
321 (backing_object = object->backing_object) != NULL) {
322 /*
323 * Allow fallback to backing objects if we are reading.
324 */
325 VM_OBJECT_LOCK(backing_object);
326 pindex += OFF_TO_IDX(object->backing_object_offset);
327 VM_OBJECT_UNLOCK(object);
328 object = backing_object;
329 }
330 VM_OBJECT_UNLOCK(object);
331 if (m == NULL) {
332 vm_map_lookup_done(tmap, out_entry);
333 error = EFAULT;
334 break;
335 }
336
337 /*
338 * Hold the page in memory.
339 */
340 vm_page_lock_queues();
341 vm_page_hold(m);
342 vm_page_unlock_queues();
343
344 /*
345 * We're done with tmap now.
346 */
347 vm_map_lookup_done(tmap, out_entry);
348
349 /*
350 * Now do the i/o move.
351 */
352 error = uiomove_fromphys(&m, page_offset, len, uio);
353
354 /* Make the I-cache coherent for breakpoints. */
355 if (!error && writing && (out_prot & VM_PROT_EXECUTE))
356 vm_sync_icache(map, uva, len);
357
358 /*
359 * Release the page.
360 */
361 vm_page_lock_queues();
362 vm_page_unhold(m);
363 vm_page_unlock_queues();
364
365 } while (error == 0 && uio->uio_resid > 0);
366
367 return (error);
368 }
369
370 static int
371 ptrace_vm_entry(struct thread *td, struct proc *p, struct ptrace_vm_entry *pve)
372 {
373 struct vattr vattr;
374 vm_map_t map;
375 vm_map_entry_t entry;
376 vm_object_t obj, tobj, lobj;
377 struct vmspace *vm;
378 struct vnode *vp;
379 char *freepath, *fullpath;
380 u_int pathlen;
381 int error, index, vfslocked;
382
383 error = 0;
384 obj = NULL;
385
386 vm = vmspace_acquire_ref(p);
387 map = &vm->vm_map;
388 vm_map_lock_read(map);
389
390 do {
391 entry = map->header.next;
392 index = 0;
393 while (index < pve->pve_entry && entry != &map->header) {
394 entry = entry->next;
395 index++;
396 }
397 if (index != pve->pve_entry) {
398 error = EINVAL;
399 break;
400 }
401 while (entry != &map->header &&
402 (entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) {
403 entry = entry->next;
404 index++;
405 }
406 if (entry == &map->header) {
407 error = ENOENT;
408 break;
409 }
410
411 /* We got an entry. */
412 pve->pve_entry = index + 1;
413 pve->pve_timestamp = map->timestamp;
414 pve->pve_start = entry->start;
415 pve->pve_end = entry->end - 1;
416 pve->pve_offset = entry->offset;
417 pve->pve_prot = entry->protection;
418
419 /* Backing object's path needed? */
420 if (pve->pve_pathlen == 0)
421 break;
422
423 pathlen = pve->pve_pathlen;
424 pve->pve_pathlen = 0;
425
426 obj = entry->object.vm_object;
427 if (obj != NULL)
428 VM_OBJECT_LOCK(obj);
429 } while (0);
430
431 vm_map_unlock_read(map);
432 vmspace_free(vm);
433
434 pve->pve_fsid = VNOVAL;
435 pve->pve_fileid = VNOVAL;
436
437 if (error == 0 && obj != NULL) {
438 lobj = obj;
439 for (tobj = obj; tobj != NULL; tobj = tobj->backing_object) {
440 if (tobj != obj)
441 VM_OBJECT_LOCK(tobj);
442 if (lobj != obj)
443 VM_OBJECT_UNLOCK(lobj);
444 lobj = tobj;
445 pve->pve_offset += tobj->backing_object_offset;
446 }
447 vp = (lobj->type == OBJT_VNODE) ? lobj->handle : NULL;
448 if (vp != NULL)
449 vref(vp);
450 if (lobj != obj)
451 VM_OBJECT_UNLOCK(lobj);
452 VM_OBJECT_UNLOCK(obj);
453
454 if (vp != NULL) {
455 freepath = NULL;
456 fullpath = NULL;
457 vn_fullpath(td, vp, &fullpath, &freepath);
458 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
459 vn_lock(vp, LK_SHARED | LK_RETRY);
460 if (VOP_GETATTR(vp, &vattr, td->td_ucred) == 0) {
461 pve->pve_fileid = vattr.va_fileid;
462 pve->pve_fsid = vattr.va_fsid;
463 }
464 vput(vp);
465 VFS_UNLOCK_GIANT(vfslocked);
466
467 if (fullpath != NULL) {
468 pve->pve_pathlen = strlen(fullpath) + 1;
469 if (pve->pve_pathlen <= pathlen) {
470 error = copyout(fullpath, pve->pve_path,
471 pve->pve_pathlen);
472 } else
473 error = ENAMETOOLONG;
474 }
475 if (freepath != NULL)
476 free(freepath, M_TEMP);
477 }
478 }
479
480 return (error);
481 }
482
483 #ifdef COMPAT_FREEBSD32
484 static int
485 ptrace_vm_entry32(struct thread *td, struct proc *p,
486 struct ptrace_vm_entry32 *pve32)
487 {
488 struct ptrace_vm_entry pve;
489 int error;
490
491 pve.pve_entry = pve32->pve_entry;
492 pve.pve_pathlen = pve32->pve_pathlen;
493 pve.pve_path = (void *)(uintptr_t)pve32->pve_path;
494
495 error = ptrace_vm_entry(td, p, &pve);
496 if (error == 0) {
497 pve32->pve_entry = pve.pve_entry;
498 pve32->pve_timestamp = pve.pve_timestamp;
499 pve32->pve_start = pve.pve_start;
500 pve32->pve_end = pve.pve_end;
501 pve32->pve_offset = pve.pve_offset;
502 pve32->pve_prot = pve.pve_prot;
503 pve32->pve_fileid = pve.pve_fileid;
504 pve32->pve_fsid = pve.pve_fsid;
505 }
506
507 pve32->pve_pathlen = pve.pve_pathlen;
508 return (error);
509 }
510
511 static void
512 ptrace_lwpinfo_to32(const struct ptrace_lwpinfo *pl,
513 struct ptrace_lwpinfo32 *pl32)
514 {
515
516 pl32->pl_lwpid = pl->pl_lwpid;
517 pl32->pl_event = pl->pl_event;
518 pl32->pl_flags = pl->pl_flags;
519 pl32->pl_sigmask = pl->pl_sigmask;
520 pl32->pl_siglist = pl->pl_siglist;
521 siginfo_to_siginfo32(&pl->pl_siginfo, &pl32->pl_siginfo);
522 strcpy(pl32->pl_tdname, pl->pl_tdname);
523 pl32->pl_child_pid = pl->pl_child_pid;
524 }
525 #endif /* COMPAT_FREEBSD32 */
526
527 /*
528 * Process debugging system call.
529 */
530 #ifndef _SYS_SYSPROTO_H_
531 struct ptrace_args {
532 int req;
533 pid_t pid;
534 caddr_t addr;
535 int data;
536 };
537 #endif
538
539 #ifdef COMPAT_FREEBSD32
540 /*
541 * This CPP subterfuge is to try and reduce the number of ifdefs in
542 * the body of the code.
543 * COPYIN(uap->addr, &r.reg, sizeof r.reg);
544 * becomes either:
545 * copyin(uap->addr, &r.reg, sizeof r.reg);
546 * or
547 * copyin(uap->addr, &r.reg32, sizeof r.reg32);
548 * .. except this is done at runtime.
549 */
550 #define COPYIN(u, k, s) wrap32 ? \
551 copyin(u, k ## 32, s ## 32) : \
552 copyin(u, k, s)
553 #define COPYOUT(k, u, s) wrap32 ? \
554 copyout(k ## 32, u, s ## 32) : \
555 copyout(k, u, s)
556 #else
557 #define COPYIN(u, k, s) copyin(u, k, s)
558 #define COPYOUT(k, u, s) copyout(k, u, s)
559 #endif
560 int
561 ptrace(struct thread *td, struct ptrace_args *uap)
562 {
563 /*
564 * XXX this obfuscation is to reduce stack usage, but the register
565 * structs may be too large to put on the stack anyway.
566 */
567 union {
568 struct ptrace_io_desc piod;
569 struct ptrace_lwpinfo pl;
570 struct ptrace_vm_entry pve;
571 struct dbreg dbreg;
572 struct fpreg fpreg;
573 struct reg reg;
574 #ifdef COMPAT_FREEBSD32
575 struct dbreg32 dbreg32;
576 struct fpreg32 fpreg32;
577 struct reg32 reg32;
578 struct ptrace_io_desc32 piod32;
579 struct ptrace_lwpinfo32 pl32;
580 struct ptrace_vm_entry32 pve32;
581 #endif
582 } r;
583 void *addr;
584 int error = 0;
585 #ifdef COMPAT_FREEBSD32
586 int wrap32 = 0;
587
588 if (SV_CURPROC_FLAG(SV_ILP32))
589 wrap32 = 1;
590 #endif
591 AUDIT_ARG_PID(uap->pid);
592 AUDIT_ARG_CMD(uap->req);
593 AUDIT_ARG_VALUE(uap->data);
594 addr = &r;
595 switch (uap->req) {
596 case PT_GETREGS:
597 case PT_GETFPREGS:
598 case PT_GETDBREGS:
599 case PT_LWPINFO:
600 break;
601 case PT_SETREGS:
602 error = COPYIN(uap->addr, &r.reg, sizeof r.reg);
603 break;
604 case PT_SETFPREGS:
605 error = COPYIN(uap->addr, &r.fpreg, sizeof r.fpreg);
606 break;
607 case PT_SETDBREGS:
608 error = COPYIN(uap->addr, &r.dbreg, sizeof r.dbreg);
609 break;
610 case PT_IO:
611 error = COPYIN(uap->addr, &r.piod, sizeof r.piod);
612 break;
613 case PT_VM_ENTRY:
614 error = COPYIN(uap->addr, &r.pve, sizeof r.pve);
615 break;
616 default:
617 addr = uap->addr;
618 break;
619 }
620 if (error)
621 return (error);
622
623 error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data);
624 if (error)
625 return (error);
626
627 switch (uap->req) {
628 case PT_VM_ENTRY:
629 error = COPYOUT(&r.pve, uap->addr, sizeof r.pve);
630 break;
631 case PT_IO:
632 error = COPYOUT(&r.piod, uap->addr, sizeof r.piod);
633 break;
634 case PT_GETREGS:
635 error = COPYOUT(&r.reg, uap->addr, sizeof r.reg);
636 break;
637 case PT_GETFPREGS:
638 error = COPYOUT(&r.fpreg, uap->addr, sizeof r.fpreg);
639 break;
640 case PT_GETDBREGS:
641 error = COPYOUT(&r.dbreg, uap->addr, sizeof r.dbreg);
642 break;
643 case PT_LWPINFO:
644 error = copyout(&r.pl, uap->addr, uap->data);
645 break;
646 }
647
648 return (error);
649 }
650 #undef COPYIN
651 #undef COPYOUT
652
653 #ifdef COMPAT_FREEBSD32
654 /*
655 * PROC_READ(regs, td2, addr);
656 * becomes either:
657 * proc_read_regs(td2, addr);
658 * or
659 * proc_read_regs32(td2, addr);
660 * .. except this is done at runtime. There is an additional
661 * complication in that PROC_WRITE disallows 32 bit consumers
662 * from writing to 64 bit address space targets.
663 */
664 #define PROC_READ(w, t, a) wrap32 ? \
665 proc_read_ ## w ## 32(t, a) : \
666 proc_read_ ## w (t, a)
667 #define PROC_WRITE(w, t, a) wrap32 ? \
668 (safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \
669 proc_write_ ## w (t, a)
670 #else
671 #define PROC_READ(w, t, a) proc_read_ ## w (t, a)
672 #define PROC_WRITE(w, t, a) proc_write_ ## w (t, a)
673 #endif
674
675 int
676 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
677 {
678 struct iovec iov;
679 struct uio uio;
680 struct proc *curp, *p, *pp;
681 struct thread *td2 = NULL;
682 struct ptrace_io_desc *piod = NULL;
683 struct ptrace_lwpinfo *pl;
684 int error, write, tmp, num;
685 int proctree_locked = 0;
686 lwpid_t tid = 0, *buf;
687 #ifdef COMPAT_FREEBSD32
688 int wrap32 = 0, safe = 0;
689 struct ptrace_io_desc32 *piod32 = NULL;
690 struct ptrace_lwpinfo32 *pl32 = NULL;
691 struct ptrace_lwpinfo plr;
692 #endif
693
694 curp = td->td_proc;
695
696 /* Lock proctree before locking the process. */
697 switch (req) {
698 case PT_TRACE_ME:
699 case PT_ATTACH:
700 case PT_STEP:
701 case PT_CONTINUE:
702 case PT_TO_SCE:
703 case PT_TO_SCX:
704 case PT_SYSCALL:
705 case PT_FOLLOW_FORK:
706 case PT_DETACH:
707 sx_xlock(&proctree_lock);
708 proctree_locked = 1;
709 break;
710 default:
711 break;
712 }
713
714 write = 0;
715 if (req == PT_TRACE_ME) {
716 p = td->td_proc;
717 PROC_LOCK(p);
718 } else {
719 if (pid <= PID_MAX) {
720 if ((p = pfind(pid)) == NULL) {
721 if (proctree_locked)
722 sx_xunlock(&proctree_lock);
723 return (ESRCH);
724 }
725 } else {
726 /* this is slow, should be optimized */
727 sx_slock(&allproc_lock);
728 FOREACH_PROC_IN_SYSTEM(p) {
729 PROC_LOCK(p);
730 FOREACH_THREAD_IN_PROC(p, td2) {
731 if (td2->td_tid == pid)
732 break;
733 }
734 if (td2 != NULL)
735 break; /* proc lock held */
736 PROC_UNLOCK(p);
737 }
738 sx_sunlock(&allproc_lock);
739 if (p == NULL) {
740 if (proctree_locked)
741 sx_xunlock(&proctree_lock);
742 return (ESRCH);
743 }
744 tid = pid;
745 pid = p->p_pid;
746 }
747 }
748 AUDIT_ARG_PROCESS(p);
749
750 if ((p->p_flag & P_WEXIT) != 0) {
751 error = ESRCH;
752 goto fail;
753 }
754 if ((error = p_cansee(td, p)) != 0)
755 goto fail;
756
757 if ((error = p_candebug(td, p)) != 0)
758 goto fail;
759
760 /*
761 * System processes can't be debugged.
762 */
763 if ((p->p_flag & P_SYSTEM) != 0) {
764 error = EINVAL;
765 goto fail;
766 }
767
768 if (tid == 0) {
769 if ((p->p_flag & P_STOPPED_TRACE) != 0) {
770 KASSERT(p->p_xthread != NULL, ("NULL p_xthread"));
771 td2 = p->p_xthread;
772 } else {
773 td2 = FIRST_THREAD_IN_PROC(p);
774 }
775 tid = td2->td_tid;
776 }
777
778 #ifdef COMPAT_FREEBSD32
779 /*
780 * Test if we're a 32 bit client and what the target is.
781 * Set the wrap controls accordingly.
782 */
783 if (SV_CURPROC_FLAG(SV_ILP32)) {
784 if (SV_PROC_FLAG(td2->td_proc, SV_ILP32))
785 safe = 1;
786 wrap32 = 1;
787 }
788 #endif
789 /*
790 * Permissions check
791 */
792 switch (req) {
793 case PT_TRACE_ME:
794 /* Always legal. */
795 break;
796
797 case PT_ATTACH:
798 /* Self */
799 if (p->p_pid == td->td_proc->p_pid) {
800 error = EINVAL;
801 goto fail;
802 }
803
804 /* Already traced */
805 if (p->p_flag & P_TRACED) {
806 error = EBUSY;
807 goto fail;
808 }
809
810 /* Can't trace an ancestor if you're being traced. */
811 if (curp->p_flag & P_TRACED) {
812 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) {
813 if (pp == p) {
814 error = EINVAL;
815 goto fail;
816 }
817 }
818 }
819
820
821 /* OK */
822 break;
823
824 case PT_CLEARSTEP:
825 /* Allow thread to clear single step for itself */
826 if (td->td_tid == tid)
827 break;
828
829 /* FALLTHROUGH */
830 default:
831 /* not being traced... */
832 if ((p->p_flag & P_TRACED) == 0) {
833 error = EPERM;
834 goto fail;
835 }
836
837 /* not being traced by YOU */
838 if (p->p_pptr != td->td_proc) {
839 error = EBUSY;
840 goto fail;
841 }
842
843 /* not currently stopped */
844 if ((p->p_flag & (P_STOPPED_SIG | P_STOPPED_TRACE)) == 0 ||
845 p->p_suspcount != p->p_numthreads ||
846 (p->p_flag & P_WAITED) == 0) {
847 error = EBUSY;
848 goto fail;
849 }
850
851 if ((p->p_flag & P_STOPPED_TRACE) == 0) {
852 static int count = 0;
853 if (count++ == 0)
854 printf("P_STOPPED_TRACE not set.\n");
855 }
856
857 /* OK */
858 break;
859 }
860
861 /* Keep this process around until we finish this request. */
862 _PHOLD(p);
863
864 #ifdef FIX_SSTEP
865 /*
866 * Single step fixup ala procfs
867 */
868 FIX_SSTEP(td2);
869 #endif
870
871 /*
872 * Actually do the requests
873 */
874
875 td->td_retval[0] = 0;
876
877 switch (req) {
878 case PT_TRACE_ME:
879 /* set my trace flag and "owner" so it can read/write me */
880 p->p_flag |= P_TRACED;
881 p->p_oppid = p->p_pptr->p_pid;
882 break;
883
884 case PT_ATTACH:
885 /* security check done above */
886 p->p_flag |= P_TRACED;
887 p->p_oppid = p->p_pptr->p_pid;
888 if (p->p_pptr != td->td_proc)
889 proc_reparent(p, td->td_proc);
890 data = SIGSTOP;
891 goto sendsig; /* in PT_CONTINUE below */
892
893 case PT_CLEARSTEP:
894 error = ptrace_clear_single_step(td2);
895 break;
896
897 case PT_SETSTEP:
898 error = ptrace_single_step(td2);
899 break;
900
901 case PT_SUSPEND:
902 td2->td_dbgflags |= TDB_SUSPEND;
903 thread_lock(td2);
904 td2->td_flags |= TDF_NEEDSUSPCHK;
905 thread_unlock(td2);
906 break;
907
908 case PT_RESUME:
909 td2->td_dbgflags &= ~TDB_SUSPEND;
910 break;
911
912 case PT_FOLLOW_FORK:
913 if (data)
914 p->p_flag |= P_FOLLOWFORK;
915 else
916 p->p_flag &= ~P_FOLLOWFORK;
917 break;
918
919 case PT_STEP:
920 case PT_CONTINUE:
921 case PT_TO_SCE:
922 case PT_TO_SCX:
923 case PT_SYSCALL:
924 case PT_DETACH:
925 /* Zero means do not send any signal */
926 if (data < 0 || data > _SIG_MAXSIG) {
927 error = EINVAL;
928 break;
929 }
930
931 switch (req) {
932 case PT_STEP:
933 error = ptrace_single_step(td2);
934 if (error)
935 goto out;
936 break;
937 case PT_CONTINUE:
938 case PT_TO_SCE:
939 case PT_TO_SCX:
940 case PT_SYSCALL:
941 if (addr != (void *)1) {
942 error = ptrace_set_pc(td2,
943 (u_long)(uintfptr_t)addr);
944 if (error)
945 goto out;
946 }
947 switch (req) {
948 case PT_TO_SCE:
949 p->p_stops |= S_PT_SCE;
950 break;
951 case PT_TO_SCX:
952 p->p_stops |= S_PT_SCX;
953 break;
954 case PT_SYSCALL:
955 p->p_stops |= S_PT_SCE | S_PT_SCX;
956 break;
957 }
958 break;
959 case PT_DETACH:
960 /* reset process parent */
961 if (p->p_oppid != p->p_pptr->p_pid) {
962 struct proc *pp;
963
964 PROC_LOCK(p->p_pptr);
965 sigqueue_take(p->p_ksi);
966 PROC_UNLOCK(p->p_pptr);
967
968 PROC_UNLOCK(p);
969 pp = pfind(p->p_oppid);
970 if (pp == NULL)
971 pp = initproc;
972 else
973 PROC_UNLOCK(pp);
974 PROC_LOCK(p);
975 proc_reparent(p, pp);
976 if (pp == initproc)
977 p->p_sigparent = SIGCHLD;
978 }
979 p->p_flag &= ~(P_TRACED | P_WAITED | P_FOLLOWFORK);
980 p->p_oppid = 0;
981
982 /* should we send SIGCHLD? */
983 /* childproc_continued(p); */
984 break;
985 }
986
987 sendsig:
988 if (proctree_locked) {
989 sx_xunlock(&proctree_lock);
990 proctree_locked = 0;
991 }
992 p->p_xstat = data;
993 p->p_xthread = NULL;
994 if ((p->p_flag & (P_STOPPED_SIG | P_STOPPED_TRACE)) != 0) {
995 /* deliver or queue signal */
996 td2->td_dbgflags &= ~TDB_XSIG;
997 td2->td_xsig = data;
998
999 if (req == PT_DETACH) {
1000 struct thread *td3;
1001 FOREACH_THREAD_IN_PROC(p, td3) {
1002 td3->td_dbgflags &= ~TDB_SUSPEND;
1003 }
1004 }
1005 /*
1006 * unsuspend all threads, to not let a thread run,
1007 * you should use PT_SUSPEND to suspend it before
1008 * continuing process.
1009 */
1010 PROC_SLOCK(p);
1011 p->p_flag &= ~(P_STOPPED_TRACE|P_STOPPED_SIG|P_WAITED);
1012 thread_unsuspend(p);
1013 PROC_SUNLOCK(p);
1014 } else {
1015 if (data)
1016 psignal(p, data);
1017 }
1018 break;
1019
1020 case PT_WRITE_I:
1021 case PT_WRITE_D:
1022 td2->td_dbgflags |= TDB_USERWR;
1023 write = 1;
1024 /* FALLTHROUGH */
1025 case PT_READ_I:
1026 case PT_READ_D:
1027 PROC_UNLOCK(p);
1028 tmp = 0;
1029 /* write = 0 set above */
1030 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
1031 iov.iov_len = sizeof(int);
1032 uio.uio_iov = &iov;
1033 uio.uio_iovcnt = 1;
1034 uio.uio_offset = (off_t)(uintptr_t)addr;
1035 uio.uio_resid = sizeof(int);
1036 uio.uio_segflg = UIO_SYSSPACE; /* i.e.: the uap */
1037 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
1038 uio.uio_td = td;
1039 error = proc_rwmem(p, &uio);
1040 if (uio.uio_resid != 0) {
1041 /*
1042 * XXX proc_rwmem() doesn't currently return ENOSPC,
1043 * so I think write() can bogusly return 0.
1044 * XXX what happens for short writes? We don't want
1045 * to write partial data.
1046 * XXX proc_rwmem() returns EPERM for other invalid
1047 * addresses. Convert this to EINVAL. Does this
1048 * clobber returns of EPERM for other reasons?
1049 */
1050 if (error == 0 || error == ENOSPC || error == EPERM)
1051 error = EINVAL; /* EOF */
1052 }
1053 if (!write)
1054 td->td_retval[0] = tmp;
1055 PROC_LOCK(p);
1056 break;
1057
1058 case PT_IO:
1059 #ifdef COMPAT_FREEBSD32
1060 if (wrap32) {
1061 piod32 = addr;
1062 iov.iov_base = (void *)(uintptr_t)piod32->piod_addr;
1063 iov.iov_len = piod32->piod_len;
1064 uio.uio_offset = (off_t)(uintptr_t)piod32->piod_offs;
1065 uio.uio_resid = piod32->piod_len;
1066 } else
1067 #endif
1068 {
1069 piod = addr;
1070 iov.iov_base = piod->piod_addr;
1071 iov.iov_len = piod->piod_len;
1072 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
1073 uio.uio_resid = piod->piod_len;
1074 }
1075 uio.uio_iov = &iov;
1076 uio.uio_iovcnt = 1;
1077 uio.uio_segflg = UIO_USERSPACE;
1078 uio.uio_td = td;
1079 #ifdef COMPAT_FREEBSD32
1080 tmp = wrap32 ? piod32->piod_op : piod->piod_op;
1081 #else
1082 tmp = piod->piod_op;
1083 #endif
1084 switch (tmp) {
1085 case PIOD_READ_D:
1086 case PIOD_READ_I:
1087 uio.uio_rw = UIO_READ;
1088 break;
1089 case PIOD_WRITE_D:
1090 case PIOD_WRITE_I:
1091 td2->td_dbgflags |= TDB_USERWR;
1092 uio.uio_rw = UIO_WRITE;
1093 break;
1094 default:
1095 error = EINVAL;
1096 goto out;
1097 }
1098 PROC_UNLOCK(p);
1099 error = proc_rwmem(p, &uio);
1100 #ifdef COMPAT_FREEBSD32
1101 if (wrap32)
1102 piod32->piod_len -= uio.uio_resid;
1103 else
1104 #endif
1105 piod->piod_len -= uio.uio_resid;
1106 PROC_LOCK(p);
1107 break;
1108
1109 case PT_KILL:
1110 data = SIGKILL;
1111 goto sendsig; /* in PT_CONTINUE above */
1112
1113 case PT_SETREGS:
1114 td2->td_dbgflags |= TDB_USERWR;
1115 error = PROC_WRITE(regs, td2, addr);
1116 break;
1117
1118 case PT_GETREGS:
1119 error = PROC_READ(regs, td2, addr);
1120 break;
1121
1122 case PT_SETFPREGS:
1123 td2->td_dbgflags |= TDB_USERWR;
1124 error = PROC_WRITE(fpregs, td2, addr);
1125 break;
1126
1127 case PT_GETFPREGS:
1128 error = PROC_READ(fpregs, td2, addr);
1129 break;
1130
1131 case PT_SETDBREGS:
1132 td2->td_dbgflags |= TDB_USERWR;
1133 error = PROC_WRITE(dbregs, td2, addr);
1134 break;
1135
1136 case PT_GETDBREGS:
1137 error = PROC_READ(dbregs, td2, addr);
1138 break;
1139
1140 case PT_LWPINFO:
1141 if (data <= 0 ||
1142 #ifdef COMPAT_FREEBSD32
1143 (!wrap32 && data > sizeof(*pl)) ||
1144 (wrap32 && data > sizeof(*pl32))) {
1145 #else
1146 data > sizeof(*pl)) {
1147 #endif
1148 error = EINVAL;
1149 break;
1150 }
1151 #ifdef COMPAT_FREEBSD32
1152 if (wrap32) {
1153 pl = &plr;
1154 pl32 = addr;
1155 } else
1156 #endif
1157 pl = addr;
1158 pl->pl_lwpid = td2->td_tid;
1159 pl->pl_flags = 0;
1160 if (td2->td_dbgflags & TDB_XSIG) {
1161 pl->pl_event = PL_EVENT_SIGNAL;
1162 if (td2->td_dbgksi.ksi_signo != 0 &&
1163 #ifdef COMPAT_FREEBSD32
1164 ((!wrap32 && data >= offsetof(struct ptrace_lwpinfo,
1165 pl_siginfo) + sizeof(pl->pl_siginfo)) ||
1166 (wrap32 && data >= offsetof(struct ptrace_lwpinfo32,
1167 pl_siginfo) + sizeof(struct siginfo32)))
1168 #else
1169 data >= offsetof(struct ptrace_lwpinfo, pl_siginfo)
1170 + sizeof(pl->pl_siginfo)
1171 #endif
1172 ){
1173 pl->pl_flags |= PL_FLAG_SI;
1174 pl->pl_siginfo = td2->td_dbgksi.ksi_info;
1175 }
1176 }
1177 if ((pl->pl_flags & PL_FLAG_SI) == 0)
1178 bzero(&pl->pl_siginfo, sizeof(pl->pl_siginfo));
1179 if (td2->td_dbgflags & TDB_SCE)
1180 pl->pl_flags |= PL_FLAG_SCE;
1181 else if (td2->td_dbgflags & TDB_SCX)
1182 pl->pl_flags |= PL_FLAG_SCX;
1183 if (td2->td_dbgflags & TDB_EXEC)
1184 pl->pl_flags |= PL_FLAG_EXEC;
1185 if (td2->td_dbgflags & TDB_FORK) {
1186 pl->pl_flags |= PL_FLAG_FORKED;
1187 pl->pl_child_pid = td2->td_dbg_forked;
1188 }
1189 pl->pl_sigmask = td2->td_sigmask;
1190 pl->pl_siglist = td2->td_siglist;
1191 strcpy(pl->pl_tdname, td2->td_name);
1192 #ifdef COMPAT_FREEBSD32
1193 if (wrap32)
1194 ptrace_lwpinfo_to32(pl, pl32);
1195 #endif
1196 break;
1197
1198 case PT_GETNUMLWPS:
1199 td->td_retval[0] = p->p_numthreads;
1200 break;
1201
1202 case PT_GETLWPLIST:
1203 if (data <= 0) {
1204 error = EINVAL;
1205 break;
1206 }
1207 num = imin(p->p_numthreads, data);
1208 PROC_UNLOCK(p);
1209 buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK);
1210 tmp = 0;
1211 PROC_LOCK(p);
1212 FOREACH_THREAD_IN_PROC(p, td2) {
1213 if (tmp >= num)
1214 break;
1215 buf[tmp++] = td2->td_tid;
1216 }
1217 PROC_UNLOCK(p);
1218 error = copyout(buf, addr, tmp * sizeof(lwpid_t));
1219 free(buf, M_TEMP);
1220 if (!error)
1221 td->td_retval[0] = tmp;
1222 PROC_LOCK(p);
1223 break;
1224
1225 case PT_VM_TIMESTAMP:
1226 td->td_retval[0] = p->p_vmspace->vm_map.timestamp;
1227 break;
1228
1229 case PT_VM_ENTRY:
1230 PROC_UNLOCK(p);
1231 #ifdef COMPAT_FREEBSD32
1232 if (wrap32)
1233 error = ptrace_vm_entry32(td, p, addr);
1234 else
1235 #endif
1236 error = ptrace_vm_entry(td, p, addr);
1237 PROC_LOCK(p);
1238 break;
1239
1240 default:
1241 #ifdef __HAVE_PTRACE_MACHDEP
1242 if (req >= PT_FIRSTMACH) {
1243 PROC_UNLOCK(p);
1244 error = cpu_ptrace(td2, req, addr, data);
1245 PROC_LOCK(p);
1246 } else
1247 #endif
1248 /* Unknown request. */
1249 error = EINVAL;
1250 break;
1251 }
1252
1253 out:
1254 /* Drop our hold on this process now that the request has completed. */
1255 _PRELE(p);
1256 fail:
1257 PROC_UNLOCK(p);
1258 if (proctree_locked)
1259 sx_xunlock(&proctree_lock);
1260 return (error);
1261 }
1262 #undef PROC_READ
1263 #undef PROC_WRITE
1264
1265 /*
1266 * Stop a process because of a debugging event;
1267 * stay stopped until p->p_step is cleared
1268 * (cleared by PIOCCONT in procfs).
1269 */
1270 void
1271 stopevent(struct proc *p, unsigned int event, unsigned int val)
1272 {
1273
1274 PROC_LOCK_ASSERT(p, MA_OWNED);
1275 p->p_step = 1;
1276 do {
1277 p->p_xstat = val;
1278 p->p_xthread = NULL;
1279 p->p_stype = event; /* Which event caused the stop? */
1280 wakeup(&p->p_stype); /* Wake up any PIOCWAIT'ing procs */
1281 msleep(&p->p_step, &p->p_mtx, PWAIT, "stopevent", 0);
1282 } while (p->p_step);
1283 }
Cache object: 07b3c15e8774e862fa1050603f3e4745
|