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$"); 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/lock.h> 38 #include <sys/mutex.h> 39 #include <sys/syscallsubr.h> 40 #include <sys/sysproto.h> 41 #include <sys/proc.h> 42 #include <sys/vnode.h> 43 #include <sys/ptrace.h> 44 #include <sys/sx.h> 45 #include <sys/user.h> 46 47 #include <machine/reg.h> 48 49 #include <vm/vm.h> 50 #include <vm/pmap.h> 51 #include <vm/vm_extern.h> 52 #include <vm/vm_map.h> 53 #include <vm/vm_kern.h> 54 #include <vm/vm_object.h> 55 #include <vm/vm_page.h> 56 57 /* 58 * Functions implemented using PROC_ACTION(): 59 * 60 * proc_read_regs(proc, regs) 61 * Get the current user-visible register set from the process 62 * and copy it into the regs structure (<machine/reg.h>). 63 * The process is stopped at the time read_regs is called. 64 * 65 * proc_write_regs(proc, regs) 66 * Update the current register set from the passed in regs 67 * structure. Take care to avoid clobbering special CPU 68 * registers or privileged bits in the PSL. 69 * Depending on the architecture this may have fix-up work to do, 70 * especially if the IAR or PCW are modified. 71 * The process is stopped at the time write_regs is called. 72 * 73 * proc_read_fpregs, proc_write_fpregs 74 * deal with the floating point register set, otherwise as above. 75 * 76 * proc_read_dbregs, proc_write_dbregs 77 * deal with the processor debug register set, otherwise as above. 78 * 79 * proc_sstep(proc) 80 * Arrange for the process to trap after executing a single instruction. 81 */ 82 83 #define PROC_ACTION(action) do { \ 84 int error; \ 85 \ 86 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); \ 87 if ((td->td_proc->p_sflag & PS_INMEM) == 0) \ 88 error = EIO; \ 89 else \ 90 error = (action); \ 91 return (error); \ 92 } while(0) 93 94 int 95 proc_read_regs(struct thread *td, struct reg *regs) 96 { 97 98 PROC_ACTION(fill_regs(td, regs)); 99 } 100 101 int 102 proc_write_regs(struct thread *td, struct reg *regs) 103 { 104 105 PROC_ACTION(set_regs(td, regs)); 106 } 107 108 int 109 proc_read_dbregs(struct thread *td, struct dbreg *dbregs) 110 { 111 112 PROC_ACTION(fill_dbregs(td, dbregs)); 113 } 114 115 int 116 proc_write_dbregs(struct thread *td, struct dbreg *dbregs) 117 { 118 119 PROC_ACTION(set_dbregs(td, dbregs)); 120 } 121 122 /* 123 * Ptrace doesn't support fpregs at all, and there are no security holes 124 * or translations for fpregs, so we can just copy them. 125 */ 126 int 127 proc_read_fpregs(struct thread *td, struct fpreg *fpregs) 128 { 129 130 PROC_ACTION(fill_fpregs(td, fpregs)); 131 } 132 133 int 134 proc_write_fpregs(struct thread *td, struct fpreg *fpregs) 135 { 136 137 PROC_ACTION(set_fpregs(td, fpregs)); 138 } 139 140 int 141 proc_sstep(struct thread *td) 142 { 143 144 PROC_ACTION(ptrace_single_step(td)); 145 } 146 147 int 148 proc_rwmem(struct proc *p, struct uio *uio) 149 { 150 struct vmspace *vm; 151 vm_map_t map; 152 vm_object_t backing_object, object = NULL; 153 vm_offset_t pageno = 0; /* page number */ 154 vm_prot_t reqprot; 155 int error, writing; 156 157 mtx_lock(&Giant); 158 /* 159 * if the vmspace is in the midst of being deallocated or the 160 * process is exiting, don't try to grab anything. The page table 161 * usage in that process can be messed up. 162 */ 163 vm = p->p_vmspace; 164 if ((p->p_flag & P_WEXIT)) { 165 mtx_unlock(&Giant); 166 return (EFAULT); 167 } 168 if (vm->vm_refcnt < 1) { 169 mtx_unlock(&Giant); 170 return (EFAULT); 171 } 172 ++vm->vm_refcnt; 173 /* 174 * The map we want... 175 */ 176 map = &vm->vm_map; 177 178 writing = uio->uio_rw == UIO_WRITE; 179 reqprot = writing ? (VM_PROT_WRITE | VM_PROT_OVERRIDE_WRITE) : 180 VM_PROT_READ; 181 182 /* 183 * Only map in one page at a time. We don't have to, but it 184 * makes things easier. This way is trivial - right? 185 */ 186 do { 187 vm_map_t tmap; 188 vm_offset_t uva; 189 int page_offset; /* offset into page */ 190 vm_map_entry_t out_entry; 191 vm_prot_t out_prot; 192 boolean_t wired; 193 vm_pindex_t pindex; 194 u_int len; 195 vm_page_t m; 196 197 object = NULL; 198 199 uva = (vm_offset_t)uio->uio_offset; 200 201 /* 202 * Get the page number of this segment. 203 */ 204 pageno = trunc_page(uva); 205 page_offset = uva - pageno; 206 207 /* 208 * How many bytes to copy 209 */ 210 len = min(PAGE_SIZE - page_offset, uio->uio_resid); 211 212 /* 213 * Fault the page on behalf of the process 214 */ 215 error = vm_fault(map, pageno, reqprot, VM_FAULT_NORMAL); 216 if (error) { 217 error = EFAULT; 218 break; 219 } 220 221 /* 222 * Now we need to get the page. out_entry, out_prot, wired, 223 * and single_use aren't used. One would think the vm code 224 * would be a *bit* nicer... We use tmap because 225 * vm_map_lookup() can change the map argument. 226 */ 227 tmap = map; 228 error = vm_map_lookup(&tmap, pageno, reqprot, &out_entry, 229 &object, &pindex, &out_prot, &wired); 230 if (error) { 231 error = EFAULT; 232 break; 233 } 234 VM_OBJECT_LOCK(object); 235 while ((m = vm_page_lookup(object, pindex)) == NULL && 236 !writing && 237 (backing_object = object->backing_object) != NULL) { 238 /* 239 * Allow fallback to backing objects if we are reading. 240 */ 241 VM_OBJECT_LOCK(backing_object); 242 pindex += OFF_TO_IDX(object->backing_object_offset); 243 VM_OBJECT_UNLOCK(object); 244 object = backing_object; 245 } 246 VM_OBJECT_UNLOCK(object); 247 if (m == NULL) { 248 vm_map_lookup_done(tmap, out_entry); 249 error = EFAULT; 250 break; 251 } 252 253 /* 254 * Hold the page in memory. 255 */ 256 vm_page_lock_queues(); 257 vm_page_hold(m); 258 vm_page_unlock_queues(); 259 260 /* 261 * We're done with tmap now. 262 */ 263 vm_map_lookup_done(tmap, out_entry); 264 265 /* 266 * Now do the i/o move. 267 */ 268 error = uiomove_fromphys(&m, page_offset, len, uio); 269 270 /* 271 * Release the page. 272 */ 273 vm_page_lock_queues(); 274 vm_page_unhold(m); 275 vm_page_unlock_queues(); 276 277 } while (error == 0 && uio->uio_resid > 0); 278 279 vmspace_free(vm); 280 mtx_unlock(&Giant); 281 return (error); 282 } 283 284 /* 285 * Process debugging system call. 286 */ 287 #ifndef _SYS_SYSPROTO_H_ 288 struct ptrace_args { 289 int req; 290 pid_t pid; 291 caddr_t addr; 292 int data; 293 }; 294 #endif 295 296 /* 297 * MPSAFE 298 */ 299 int 300 ptrace(struct thread *td, struct ptrace_args *uap) 301 { 302 /* 303 * XXX this obfuscation is to reduce stack usage, but the register 304 * structs may be too large to put on the stack anyway. 305 */ 306 union { 307 struct ptrace_io_desc piod; 308 struct dbreg dbreg; 309 struct fpreg fpreg; 310 struct reg reg; 311 } r; 312 void *addr; 313 int error = 0; 314 315 addr = &r; 316 switch (uap->req) { 317 case PT_GETREGS: 318 case PT_GETFPREGS: 319 case PT_GETDBREGS: 320 break; 321 case PT_SETREGS: 322 error = copyin(uap->addr, &r.reg, sizeof r.reg); 323 break; 324 case PT_SETFPREGS: 325 error = copyin(uap->addr, &r.fpreg, sizeof r.fpreg); 326 break; 327 case PT_SETDBREGS: 328 error = copyin(uap->addr, &r.dbreg, sizeof r.dbreg); 329 break; 330 case PT_IO: 331 error = copyin(uap->addr, &r.piod, sizeof r.piod); 332 break; 333 default: 334 addr = uap->addr; 335 break; 336 } 337 if (error) 338 return (error); 339 340 error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data); 341 if (error) 342 return (error); 343 344 switch (uap->req) { 345 case PT_IO: 346 (void)copyout(&r.piod, uap->addr, sizeof r.piod); 347 break; 348 case PT_GETREGS: 349 error = copyout(&r.reg, uap->addr, sizeof r.reg); 350 break; 351 case PT_GETFPREGS: 352 error = copyout(&r.fpreg, uap->addr, sizeof r.fpreg); 353 break; 354 case PT_GETDBREGS: 355 error = copyout(&r.dbreg, uap->addr, sizeof r.dbreg); 356 break; 357 } 358 359 return (error); 360 } 361 362 int 363 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data) 364 { 365 struct iovec iov; 366 struct uio uio; 367 struct proc *curp, *p, *pp; 368 struct thread *td2; 369 struct ptrace_io_desc *piod; 370 int error, write, tmp; 371 int proctree_locked = 0; 372 373 curp = td->td_proc; 374 375 /* Lock proctree before locking the process. */ 376 switch (req) { 377 case PT_TRACE_ME: 378 case PT_ATTACH: 379 case PT_STEP: 380 case PT_CONTINUE: 381 case PT_TO_SCE: 382 case PT_TO_SCX: 383 case PT_DETACH: 384 sx_xlock(&proctree_lock); 385 proctree_locked = 1; 386 break; 387 default: 388 break; 389 } 390 391 write = 0; 392 if (req == PT_TRACE_ME) { 393 p = td->td_proc; 394 PROC_LOCK(p); 395 } else { 396 if ((p = pfind(pid)) == NULL) { 397 if (proctree_locked) 398 sx_xunlock(&proctree_lock); 399 return (ESRCH); 400 } 401 } 402 if ((error = p_cansee(td, p)) != 0) 403 goto fail; 404 405 if ((error = p_candebug(td, p)) != 0) 406 goto fail; 407 408 /* 409 * System processes can't be debugged. 410 */ 411 if ((p->p_flag & P_SYSTEM) != 0) { 412 error = EINVAL; 413 goto fail; 414 } 415 416 /* 417 * Permissions check 418 */ 419 switch (req) { 420 case PT_TRACE_ME: 421 /* Always legal. */ 422 break; 423 424 case PT_ATTACH: 425 /* Self */ 426 if (p->p_pid == td->td_proc->p_pid) { 427 error = EINVAL; 428 goto fail; 429 } 430 431 /* Already traced */ 432 if (p->p_flag & P_TRACED) { 433 error = EBUSY; 434 goto fail; 435 } 436 437 /* Can't trace an ancestor if you're being traced. */ 438 if (curp->p_flag & P_TRACED) { 439 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) { 440 if (pp == p) { 441 error = EINVAL; 442 goto fail; 443 } 444 } 445 } 446 447 448 /* OK */ 449 break; 450 451 default: 452 /* not being traced... */ 453 if ((p->p_flag & P_TRACED) == 0) { 454 error = EPERM; 455 goto fail; 456 } 457 458 /* not being traced by YOU */ 459 if (p->p_pptr != td->td_proc) { 460 error = EBUSY; 461 goto fail; 462 } 463 464 /* not currently stopped */ 465 if (!P_SHOULDSTOP(p) || (p->p_flag & P_WAITED) == 0) { 466 error = EBUSY; 467 goto fail; 468 } 469 470 /* OK */ 471 break; 472 } 473 474 td2 = FIRST_THREAD_IN_PROC(p); 475 #ifdef FIX_SSTEP 476 /* 477 * Single step fixup ala procfs 478 */ 479 FIX_SSTEP(td2); /* XXXKSE */ 480 #endif 481 482 /* 483 * Actually do the requests 484 */ 485 486 td->td_retval[0] = 0; 487 488 switch (req) { 489 case PT_TRACE_ME: 490 /* set my trace flag and "owner" so it can read/write me */ 491 p->p_flag |= P_TRACED; 492 p->p_oppid = p->p_pptr->p_pid; 493 PROC_UNLOCK(p); 494 sx_xunlock(&proctree_lock); 495 return (0); 496 497 case PT_ATTACH: 498 /* security check done above */ 499 p->p_flag |= P_TRACED; 500 p->p_oppid = p->p_pptr->p_pid; 501 if (p->p_pptr != td->td_proc) 502 proc_reparent(p, td->td_proc); 503 data = SIGSTOP; 504 goto sendsig; /* in PT_CONTINUE below */ 505 506 case PT_STEP: 507 case PT_CONTINUE: 508 case PT_TO_SCE: 509 case PT_TO_SCX: 510 case PT_DETACH: 511 /* Zero means do not send any signal */ 512 if (data < 0 || data > _SIG_MAXSIG) { 513 error = EINVAL; 514 goto fail; 515 } 516 517 _PHOLD(p); 518 519 switch (req) { 520 case PT_STEP: 521 PROC_UNLOCK(p); 522 error = ptrace_single_step(td2); 523 if (error) { 524 PRELE(p); 525 goto fail_noproc; 526 } 527 PROC_LOCK(p); 528 break; 529 case PT_TO_SCE: 530 p->p_stops |= S_PT_SCE; 531 break; 532 case PT_TO_SCX: 533 p->p_stops |= S_PT_SCX; 534 break; 535 case PT_SYSCALL: 536 p->p_stops |= S_PT_SCE | S_PT_SCX; 537 break; 538 } 539 540 if (addr != (void *)1) { 541 PROC_UNLOCK(p); 542 error = ptrace_set_pc(td2, (u_long)(uintfptr_t)addr); 543 if (error) { 544 PRELE(p); 545 goto fail_noproc; 546 } 547 PROC_LOCK(p); 548 } 549 _PRELE(p); 550 551 if (req == PT_DETACH) { 552 /* reset process parent */ 553 if (p->p_oppid != p->p_pptr->p_pid) { 554 struct proc *pp; 555 556 PROC_UNLOCK(p); 557 pp = pfind(p->p_oppid); 558 if (pp == NULL) 559 pp = initproc; 560 else 561 PROC_UNLOCK(pp); 562 PROC_LOCK(p); 563 proc_reparent(p, pp); 564 if (pp == initproc) 565 p->p_sigparent = SIGCHLD; 566 } 567 p->p_flag &= ~(P_TRACED | P_WAITED); 568 p->p_oppid = 0; 569 570 /* should we send SIGCHLD? */ 571 } 572 573 sendsig: 574 if (proctree_locked) 575 sx_xunlock(&proctree_lock); 576 /* deliver or queue signal */ 577 if (P_SHOULDSTOP(p)) { 578 p->p_xstat = data; 579 p->p_flag &= ~(P_STOPPED_TRACE|P_STOPPED_SIG); 580 mtx_lock_spin(&sched_lock); 581 thread_unsuspend(p); 582 setrunnable(td2); /* XXXKSE */ 583 /* Need foreach kse in proc, ... make_kse_queued(). */ 584 mtx_unlock_spin(&sched_lock); 585 } else if (data) 586 psignal(p, data); 587 PROC_UNLOCK(p); 588 589 return (0); 590 591 case PT_WRITE_I: 592 case PT_WRITE_D: 593 write = 1; 594 /* FALLTHROUGH */ 595 case PT_READ_I: 596 case PT_READ_D: 597 PROC_UNLOCK(p); 598 tmp = 0; 599 /* write = 0 set above */ 600 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp; 601 iov.iov_len = sizeof(int); 602 uio.uio_iov = &iov; 603 uio.uio_iovcnt = 1; 604 uio.uio_offset = (off_t)(uintptr_t)addr; 605 uio.uio_resid = sizeof(int); 606 uio.uio_segflg = UIO_SYSSPACE; /* i.e.: the uap */ 607 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 608 uio.uio_td = td; 609 error = proc_rwmem(p, &uio); 610 if (uio.uio_resid != 0) { 611 /* 612 * XXX proc_rwmem() doesn't currently return ENOSPC, 613 * so I think write() can bogusly return 0. 614 * XXX what happens for short writes? We don't want 615 * to write partial data. 616 * XXX proc_rwmem() returns EPERM for other invalid 617 * addresses. Convert this to EINVAL. Does this 618 * clobber returns of EPERM for other reasons? 619 */ 620 if (error == 0 || error == ENOSPC || error == EPERM) 621 error = EINVAL; /* EOF */ 622 } 623 if (!write) 624 td->td_retval[0] = tmp; 625 return (error); 626 627 case PT_IO: 628 PROC_UNLOCK(p); 629 piod = addr; 630 iov.iov_base = piod->piod_addr; 631 iov.iov_len = piod->piod_len; 632 uio.uio_iov = &iov; 633 uio.uio_iovcnt = 1; 634 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs; 635 uio.uio_resid = piod->piod_len; 636 uio.uio_segflg = UIO_USERSPACE; 637 uio.uio_td = td; 638 switch (piod->piod_op) { 639 case PIOD_READ_D: 640 case PIOD_READ_I: 641 uio.uio_rw = UIO_READ; 642 break; 643 case PIOD_WRITE_D: 644 case PIOD_WRITE_I: 645 uio.uio_rw = UIO_WRITE; 646 break; 647 default: 648 return (EINVAL); 649 } 650 error = proc_rwmem(p, &uio); 651 piod->piod_len -= uio.uio_resid; 652 return (error); 653 654 case PT_KILL: 655 data = SIGKILL; 656 goto sendsig; /* in PT_CONTINUE above */ 657 658 case PT_SETREGS: 659 _PHOLD(p); 660 error = proc_write_regs(td2, addr); 661 _PRELE(p); 662 PROC_UNLOCK(p); 663 return (error); 664 665 case PT_GETREGS: 666 _PHOLD(p); 667 error = proc_read_regs(td2, addr); 668 _PRELE(p); 669 PROC_UNLOCK(p); 670 return (error); 671 672 case PT_SETFPREGS: 673 _PHOLD(p); 674 error = proc_write_fpregs(td2, addr); 675 _PRELE(p); 676 PROC_UNLOCK(p); 677 return (error); 678 679 case PT_GETFPREGS: 680 _PHOLD(p); 681 error = proc_read_fpregs(td2, addr); 682 _PRELE(p); 683 PROC_UNLOCK(p); 684 return (error); 685 686 case PT_SETDBREGS: 687 _PHOLD(p); 688 error = proc_write_dbregs(td2, addr); 689 _PRELE(p); 690 PROC_UNLOCK(p); 691 return (error); 692 693 case PT_GETDBREGS: 694 _PHOLD(p); 695 error = proc_read_dbregs(td2, addr); 696 _PRELE(p); 697 PROC_UNLOCK(p); 698 return (error); 699 700 default: 701 #ifdef __HAVE_PTRACE_MACHDEP 702 if (req >= PT_FIRSTMACH) { 703 _PHOLD(p); 704 PROC_UNLOCK(p); 705 error = cpu_ptrace(td2, req, addr, data); 706 PRELE(p); 707 return (error); 708 } 709 #endif 710 break; 711 } 712 713 /* Unknown request. */ 714 error = EINVAL; 715 716 fail: 717 PROC_UNLOCK(p); 718 fail_noproc: 719 if (proctree_locked) 720 sx_xunlock(&proctree_lock); 721 return (error); 722 } 723 724 /* 725 * Stop a process because of a debugging event; 726 * stay stopped until p->p_step is cleared 727 * (cleared by PIOCCONT in procfs). 728 */ 729 void 730 stopevent(struct proc *p, unsigned int event, unsigned int val) 731 { 732 733 PROC_LOCK_ASSERT(p, MA_OWNED); 734 p->p_step = 1; 735 do { 736 p->p_xstat = val; 737 p->p_stype = event; /* Which event caused the stop? */ 738 wakeup(&p->p_stype); /* Wake up any PIOCWAIT'ing procs */ 739 msleep(&p->p_step, &p->p_mtx, PWAIT, "stopevent", 0); 740 } while (p->p_step); 741 } 742