1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (c) 1994, Sean Eric Fagan 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Sean Eric Fagan. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/systm.h> 35 #include <sys/caprights.h> 36 #include <sys/filedesc.h> 37 #include <sys/imgact.h> 38 #include <sys/ktr.h> 39 #include <sys/limits.h> 40 #include <sys/lock.h> 41 #include <sys/malloc.h> 42 #include <sys/mman.h> 43 #include <sys/mutex.h> 44 #include <sys/priv.h> 45 #include <sys/proc.h> 46 #include <sys/ptrace.h> 47 #include <sys/reg.h> 48 #include <sys/rwlock.h> 49 #include <sys/signalvar.h> 50 #include <sys/sleepqueue.h> 51 #include <sys/sx.h> 52 #include <sys/syscallsubr.h> 53 #include <sys/sysent.h> 54 #include <sys/sysproto.h> 55 #include <sys/vnode.h> 56 57 #include <security/audit/audit.h> 58 59 #include <vm/vm.h> 60 #include <vm/pmap.h> 61 #include <vm/vm_extern.h> 62 #include <vm/vm_map.h> 63 #include <vm/vm_kern.h> 64 #include <vm/vm_object.h> 65 #include <vm/vm_page.h> 66 #include <vm/vm_param.h> 67 68 #ifdef COMPAT_FREEBSD32 69 #include <sys/procfs.h> 70 #endif 71 72 /* Assert it's safe to unlock a process, e.g. to allocate working memory */ 73 #define PROC_ASSERT_TRACEREQ(p) MPASS(((p)->p_flag2 & P2_PTRACEREQ) != 0) 74 75 /* 76 * Functions implemented below: 77 * 78 * proc_read_regs(proc, regs) 79 * Get the current user-visible register set from the process 80 * and copy it into the regs structure (<machine/reg.h>). 81 * The process is stopped at the time read_regs is called. 82 * 83 * proc_write_regs(proc, regs) 84 * Update the current register set from the passed in regs 85 * structure. Take care to avoid clobbering special CPU 86 * registers or privileged bits in the PSL. 87 * Depending on the architecture this may have fix-up work to do, 88 * especially if the IAR or PCW are modified. 89 * The process is stopped at the time write_regs is called. 90 * 91 * proc_read_fpregs, proc_write_fpregs 92 * deal with the floating point register set, otherwise as above. 93 * 94 * proc_read_dbregs, proc_write_dbregs 95 * deal with the processor debug register set, otherwise as above. 96 * 97 * proc_sstep(proc) 98 * Arrange for the process to trap after executing a single instruction. 99 */ 100 101 int 102 proc_read_regs(struct thread *td, struct reg *regs) 103 { 104 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 105 return (fill_regs(td, regs)); 106 } 107 108 int 109 proc_write_regs(struct thread *td, struct reg *regs) 110 { 111 int error; 112 113 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 114 error = priv_check(curthread, PRIV_PROC_MEM_WRITE); 115 if (error != 0) 116 return (error); 117 return (set_regs(td, regs)); 118 } 119 120 int 121 proc_read_dbregs(struct thread *td, struct dbreg *dbregs) 122 { 123 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 124 return (fill_dbregs(td, dbregs)); 125 } 126 127 int 128 proc_write_dbregs(struct thread *td, struct dbreg *dbregs) 129 { 130 int error; 131 132 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 133 error = priv_check(curthread, PRIV_PROC_MEM_WRITE); 134 if (error != 0) 135 return (error); 136 return (set_dbregs(td, dbregs)); 137 } 138 139 /* 140 * Ptrace doesn't support fpregs at all, and there are no security holes 141 * or translations for fpregs, so we can just copy them. 142 */ 143 int 144 proc_read_fpregs(struct thread *td, struct fpreg *fpregs) 145 { 146 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 147 return (fill_fpregs(td, fpregs)); 148 } 149 150 int 151 proc_write_fpregs(struct thread *td, struct fpreg *fpregs) 152 { 153 int error; 154 155 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 156 error = priv_check(curthread, PRIV_PROC_MEM_WRITE); 157 if (error != 0) 158 return (error); 159 return (set_fpregs(td, fpregs)); 160 } 161 162 static struct regset * 163 proc_find_regset(struct thread *td, int note) 164 { 165 struct regset **regsetp, **regset_end, *regset; 166 struct sysentvec *sv; 167 168 sv = td->td_proc->p_sysent; 169 regsetp = sv->sv_regset_begin; 170 if (regsetp == NULL) 171 return (NULL); 172 regset_end = sv->sv_regset_end; 173 MPASS(regset_end != NULL); 174 for (; regsetp < regset_end; regsetp++) { 175 regset = *regsetp; 176 if (regset->note != note) 177 continue; 178 179 return (regset); 180 } 181 182 return (NULL); 183 } 184 185 static int 186 proc_read_regset(struct thread *td, int note, struct iovec *iov) 187 { 188 struct regset *regset; 189 struct proc *p; 190 void *buf; 191 size_t size; 192 int error; 193 194 regset = proc_find_regset(td, note); 195 if (regset == NULL) 196 return (EINVAL); 197 198 if (regset->get == NULL) 199 return (EINVAL); 200 201 size = regset->size; 202 /* 203 * The regset is dynamically sized, e.g. the size could change 204 * depending on the hardware, or may have a per-thread size. 205 */ 206 if (size == 0) { 207 if (!regset->get(regset, td, NULL, &size)) 208 return (EINVAL); 209 } 210 211 if (iov->iov_base == NULL) { 212 iov->iov_len = size; 213 if (iov->iov_len == 0) 214 return (EINVAL); 215 216 return (0); 217 } 218 219 /* The length is wrong, return an error */ 220 if (iov->iov_len != size) 221 return (EINVAL); 222 223 error = 0; 224 p = td->td_proc; 225 226 /* Drop the proc lock while allocating the temp buffer */ 227 PROC_ASSERT_TRACEREQ(p); 228 PROC_UNLOCK(p); 229 buf = malloc(size, M_TEMP, M_WAITOK); 230 PROC_LOCK(p); 231 232 if (!regset->get(regset, td, buf, &size)) { 233 error = EINVAL; 234 } else { 235 KASSERT(size == regset->size || regset->size == 0, 236 ("%s: Getter function changed the size", __func__)); 237 238 iov->iov_len = size; 239 PROC_UNLOCK(p); 240 error = copyout(buf, iov->iov_base, size); 241 PROC_LOCK(p); 242 } 243 244 free(buf, M_TEMP); 245 246 return (error); 247 } 248 249 static int 250 proc_write_regset(struct thread *td, int note, struct iovec *iov) 251 { 252 struct regset *regset; 253 struct proc *p; 254 void *buf; 255 size_t size; 256 int error; 257 258 regset = proc_find_regset(td, note); 259 if (regset == NULL) 260 return (EINVAL); 261 262 size = regset->size; 263 /* 264 * The regset is dynamically sized, e.g. the size could change 265 * depending on the hardware, or may have a per-thread size. 266 */ 267 if (size == 0) { 268 if (!regset->get(regset, td, NULL, &size)) 269 return (EINVAL); 270 } 271 272 /* The length is wrong, return an error */ 273 if (iov->iov_len != size) 274 return (EINVAL); 275 276 if (regset->set == NULL) 277 return (EINVAL); 278 279 error = priv_check(curthread, PRIV_PROC_MEM_WRITE); 280 if (error != 0) 281 return (error); 282 283 p = td->td_proc; 284 285 /* Drop the proc lock while allocating the temp buffer */ 286 PROC_ASSERT_TRACEREQ(p); 287 PROC_UNLOCK(p); 288 buf = malloc(size, M_TEMP, M_WAITOK); 289 error = copyin(iov->iov_base, buf, size); 290 PROC_LOCK(p); 291 292 if (error == 0) { 293 if (!regset->set(regset, td, buf, size)) { 294 error = EINVAL; 295 } 296 } 297 298 free(buf, M_TEMP); 299 300 return (error); 301 } 302 303 #ifdef COMPAT_FREEBSD32 304 /* For 32 bit binaries, we need to expose the 32 bit regs layouts. */ 305 int 306 proc_read_regs32(struct thread *td, struct reg32 *regs32) 307 { 308 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 309 return (fill_regs32(td, regs32)); 310 } 311 312 int 313 proc_write_regs32(struct thread *td, struct reg32 *regs32) 314 { 315 int error; 316 317 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 318 error = priv_check(curthread, PRIV_PROC_MEM_WRITE); 319 if (error != 0) 320 return (error); 321 return (set_regs32(td, regs32)); 322 } 323 324 int 325 proc_read_dbregs32(struct thread *td, struct dbreg32 *dbregs32) 326 { 327 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 328 return (fill_dbregs32(td, dbregs32)); 329 } 330 331 int 332 proc_write_dbregs32(struct thread *td, struct dbreg32 *dbregs32) 333 { 334 int error; 335 336 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 337 error = priv_check(curthread, PRIV_PROC_MEM_WRITE); 338 if (error != 0) 339 return (error); 340 return (set_dbregs32(td, dbregs32)); 341 } 342 343 int 344 proc_read_fpregs32(struct thread *td, struct fpreg32 *fpregs32) 345 { 346 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 347 return (fill_fpregs32(td, fpregs32)); 348 } 349 350 int 351 proc_write_fpregs32(struct thread *td, struct fpreg32 *fpregs32) 352 { 353 int error; 354 355 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 356 error = priv_check(curthread, PRIV_PROC_MEM_WRITE); 357 if (error != 0) 358 return (error); 359 return (set_fpregs32(td, fpregs32)); 360 } 361 #endif 362 363 int 364 proc_sstep(struct thread *td) 365 { 366 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 367 return (ptrace_single_step(td)); 368 } 369 370 static int 371 proc_vmspace_check_access(struct thread *td, struct proc *p, int flags) 372 { 373 PROC_ASSERT_HELD(p); 374 if ((flags & PRVM_CHECK_DEBUG) != 0) 375 return (p_candebug(td, p)); 376 if ((flags & PRVM_CHECK_VISIBILITY) != 0) 377 return (p_cansee(td, p)); 378 return (0); 379 } 380 381 int 382 proc_vmspace_ref(struct thread *td, struct proc *p, int flags, 383 struct vmspace **vmp) 384 { 385 struct vmspace *vm; 386 int error; 387 388 MPASS((flags & ~(PRVM_BLOCK_EXEC | PRVM_CHECK_VISIBILITY | 389 PRVM_CHECK_DEBUG)) == 0); 390 MPASS((flags & (PRVM_CHECK_VISIBILITY | PRVM_CHECK_DEBUG)) != 391 (PRVM_CHECK_VISIBILITY | PRVM_CHECK_DEBUG)); 392 393 PROC_LOCK(p); 394 if (p != td->td_proc) { 395 PROC_ASSERT_HELD(p); 396 397 /* 398 * Make sure that the vmspace doesn't switch out from 399 * under us. 400 */ 401 if ((flags & PRVM_BLOCK_EXEC) != 0) { 402 for (;;) { 403 if (!execve_block(td, p)) { 404 PROC_LOCK(p); 405 continue; 406 } 407 error = proc_vmspace_check_access(td, p, flags); 408 if (error != 0) { 409 execve_unblock(td, p); 410 PROC_UNLOCK(p); 411 return (error); 412 } 413 break; 414 } 415 } else { 416 error = proc_vmspace_check_access(td, p, flags); 417 if (error != 0) { 418 PROC_UNLOCK(p); 419 return (error); 420 } 421 } 422 } 423 vm = vmspace_acquire_ref(p); 424 if (vm == NULL) { 425 if (p != td->td_proc && (flags & PRVM_BLOCK_EXEC) != 0) 426 execve_unblock(td, p); 427 PROC_UNLOCK(p); 428 return (ESRCH); 429 } 430 PROC_UNLOCK(p); 431 *vmp = vm; 432 return (0); 433 } 434 435 void 436 proc_vmspace_unref(struct thread *td, struct proc *p, int flags, 437 struct vmspace *vm) 438 { 439 vmspace_free(vm); 440 if (p != td->td_proc && (flags & PRVM_BLOCK_EXEC) != 0) { 441 PROC_LOCK(p); 442 PROC_ASSERT_HELD(p); 443 execve_unblock(td, p); 444 PROC_UNLOCK(p); 445 } 446 } 447 448 static int 449 vmspace_rwmem(struct vmspace *vm, struct uio *uio) 450 { 451 vm_map_t map; 452 vm_offset_t pageno; /* page number */ 453 vm_prot_t reqprot; 454 int error, fault_flags, page_offset, writing; 455 456 map = &vm->vm_map; 457 458 /* 459 * If we are writing, then we request vm_fault() to create a private 460 * copy of each page. Since these copies will not be writeable by the 461 * process, we must explicitly request that they be dirtied. 462 */ 463 writing = uio->uio_rw == UIO_WRITE; 464 reqprot = writing ? VM_PROT_COPY | VM_PROT_READ : VM_PROT_READ; 465 fault_flags = writing ? VM_FAULT_DIRTY : VM_FAULT_NORMAL; 466 467 if (writing) { 468 error = priv_check(curthread, PRIV_PROC_MEM_WRITE); 469 if (error != 0) 470 goto out; 471 } 472 473 /* 474 * Only map in one page at a time. We don't have to, but it 475 * makes things easier. This way is trivial - right? 476 */ 477 do { 478 vm_offset_t uva; 479 u_int len; 480 vm_page_t m; 481 482 uva = (vm_offset_t)uio->uio_offset; 483 484 /* 485 * Get the page number of this segment. 486 */ 487 pageno = trunc_page(uva); 488 page_offset = uva - pageno; 489 490 /* 491 * How many bytes to copy 492 */ 493 len = MIN(PAGE_SIZE - page_offset, uio->uio_resid); 494 495 /* 496 * Fault and hold the page on behalf of the process. 497 */ 498 error = vm_fault(map, pageno, reqprot, fault_flags, &m); 499 if (error != KERN_SUCCESS) { 500 if (error == KERN_RESOURCE_SHORTAGE) 501 error = ENOMEM; 502 else 503 error = EFAULT; 504 break; 505 } 506 507 /* 508 * Now do the i/o move. 509 */ 510 error = uiomove_fromphys(&m, page_offset, len, uio); 511 512 /* Make the I-cache coherent for breakpoints. */ 513 if (writing && error == 0) { 514 vm_map_lock_read(map); 515 if (vm_map_check_protection(map, pageno, pageno + 516 PAGE_SIZE, VM_PROT_EXECUTE)) 517 vm_sync_icache(map, uva, len); 518 vm_map_unlock_read(map); 519 } 520 521 /* 522 * Release the page. 523 */ 524 vm_page_unwire(m, PQ_ACTIVE); 525 526 } while (error == 0 && uio->uio_resid > 0); 527 528 out: 529 return (error); 530 } 531 532 int 533 proc_rwmem(struct proc *p, struct uio *uio, int flags) 534 { 535 struct vmspace *vm; 536 struct thread *td; 537 int error; 538 539 td = curthread; 540 error = proc_vmspace_ref(td, p, flags, &vm); 541 if (error != 0) 542 return (error); 543 error = vmspace_rwmem(vm, uio); 544 proc_vmspace_unref(td, p, flags, vm); 545 return (error); 546 } 547 548 ssize_t 549 vmspace_iop(struct thread *td, struct vmspace *vm, vm_offset_t va, void *buf, 550 size_t len, enum uio_rw rw) 551 { 552 struct iovec iov; 553 struct uio uio; 554 ssize_t slen; 555 int error; 556 557 MPASS(len < SSIZE_MAX); 558 slen = (ssize_t)len; 559 560 iov.iov_base = (caddr_t)buf; 561 iov.iov_len = len; 562 uio.uio_iov = &iov; 563 uio.uio_iovcnt = 1; 564 uio.uio_offset = va; 565 uio.uio_resid = slen; 566 uio.uio_segflg = UIO_SYSSPACE; 567 uio.uio_rw = rw; 568 uio.uio_td = td; 569 error = vmspace_rwmem(vm, &uio); 570 if (error != 0 || uio.uio_resid == slen) 571 return (-1); 572 return (slen - uio.uio_resid); 573 } 574 575 ssize_t 576 proc_readmem(struct thread *td, struct proc *p, vm_offset_t va, void *buf, 577 size_t len) 578 { 579 580 return (vmspace_iop(td, p->p_vmspace, va, buf, len, UIO_READ)); 581 } 582 583 ssize_t 584 proc_writemem(struct thread *td, struct proc *p, vm_offset_t va, void *buf, 585 size_t len) 586 { 587 588 return (vmspace_iop(td, p->p_vmspace, va, buf, len, UIO_WRITE)); 589 } 590 591 static int 592 ptrace_vm_entry(struct thread *td, struct proc *p, struct ptrace_vm_entry *pve) 593 { 594 struct vattr vattr; 595 vm_map_t map; 596 vm_map_entry_t entry; 597 vm_object_t obj, tobj, lobj; 598 struct vmspace *vm; 599 struct vnode *vp; 600 char *freepath, *fullpath; 601 u_int pathlen; 602 int error, index; 603 604 error = 0; 605 obj = NULL; 606 607 vm = vmspace_acquire_ref(p); 608 map = &vm->vm_map; 609 vm_map_lock_read(map); 610 611 do { 612 KASSERT((map->header.eflags & MAP_ENTRY_IS_SUB_MAP) == 0, 613 ("Submap in map header")); 614 index = 0; 615 VM_MAP_ENTRY_FOREACH(entry, map) { 616 if (index >= pve->pve_entry && 617 (entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) 618 break; 619 index++; 620 } 621 if (index < pve->pve_entry) { 622 error = EINVAL; 623 break; 624 } 625 if (entry == &map->header) { 626 error = ENOENT; 627 break; 628 } 629 630 /* We got an entry. */ 631 pve->pve_entry = index + 1; 632 pve->pve_timestamp = map->timestamp; 633 pve->pve_start = entry->start; 634 pve->pve_end = entry->end - 1; 635 pve->pve_offset = entry->offset; 636 pve->pve_prot = entry->protection | 637 PROT_MAX(entry->max_protection); 638 639 /* Backing object's path needed? */ 640 if (pve->pve_pathlen == 0) 641 break; 642 643 pathlen = pve->pve_pathlen; 644 pve->pve_pathlen = 0; 645 646 obj = entry->object.vm_object; 647 if (obj != NULL) 648 VM_OBJECT_RLOCK(obj); 649 } while (0); 650 651 vm_map_unlock_read(map); 652 653 pve->pve_fsid = VNOVAL; 654 pve->pve_fileid = VNOVAL; 655 656 if (error == 0 && obj != NULL) { 657 lobj = obj; 658 for (tobj = obj; tobj != NULL; tobj = tobj->backing_object) { 659 if (tobj != obj) 660 VM_OBJECT_RLOCK(tobj); 661 if (lobj != obj) 662 VM_OBJECT_RUNLOCK(lobj); 663 lobj = tobj; 664 pve->pve_offset += tobj->backing_object_offset; 665 } 666 vp = vm_object_vnode(lobj); 667 if (vp != NULL) 668 vref(vp); 669 if (lobj != obj) 670 VM_OBJECT_RUNLOCK(lobj); 671 VM_OBJECT_RUNLOCK(obj); 672 673 if (vp != NULL) { 674 freepath = NULL; 675 fullpath = NULL; 676 vn_fullpath(vp, &fullpath, &freepath); 677 vn_lock(vp, LK_SHARED | LK_RETRY); 678 if (VOP_GETATTR(vp, &vattr, td->td_ucred) == 0) { 679 pve->pve_fileid = vattr.va_fileid; 680 pve->pve_fsid = vattr.va_fsid; 681 } 682 vput(vp); 683 684 if (fullpath != NULL) { 685 pve->pve_pathlen = strlen(fullpath) + 1; 686 if (pve->pve_pathlen <= pathlen) { 687 error = copyout(fullpath, pve->pve_path, 688 pve->pve_pathlen); 689 } else 690 error = ENAMETOOLONG; 691 } 692 if (freepath != NULL) 693 free(freepath, M_TEMP); 694 } 695 } 696 vmspace_free(vm); 697 if (error == 0) 698 CTR3(KTR_PTRACE, "PT_VM_ENTRY: pid %d, entry %d, start %p", 699 p->p_pid, pve->pve_entry, pve->pve_start); 700 701 return (error); 702 } 703 704 /* 705 * Process debugging system call. 706 */ 707 #ifndef _SYS_SYSPROTO_H_ 708 struct ptrace_args { 709 int req; 710 pid_t pid; 711 caddr_t addr; 712 int data; 713 }; 714 #endif 715 716 int 717 sys_ptrace(struct thread *td, struct ptrace_args *uap) 718 { 719 /* 720 * XXX this obfuscation is to reduce stack usage, but the register 721 * structs may be too large to put on the stack anyway. 722 */ 723 union { 724 struct ptrace_io_desc piod; 725 struct ptrace_lwpinfo pl; 726 struct ptrace_vm_entry pve; 727 struct ptrace_coredump pc; 728 struct ptrace_sc_remote sr; 729 struct dbreg dbreg; 730 struct fpreg fpreg; 731 struct reg reg; 732 struct iovec vec; 733 syscallarg_t args[nitems(td->td_sa.args)]; 734 struct ptrace_sc_ret psr; 735 int ptevents; 736 } r; 737 syscallarg_t pscr_args[nitems(td->td_sa.args)]; 738 void *addr; 739 int error; 740 741 if (!allow_ptrace) 742 return (ENOSYS); 743 error = 0; 744 745 AUDIT_ARG_PID(uap->pid); 746 AUDIT_ARG_CMD(uap->req); 747 AUDIT_ARG_VALUE(uap->data); 748 addr = &r; 749 switch (uap->req) { 750 case PT_GET_EVENT_MASK: 751 case PT_LWPINFO: 752 case PT_GET_SC_ARGS: 753 case PT_GET_SC_RET: 754 break; 755 case PT_SET_SC_RET: 756 if (uap->data != sizeof(r.psr)) 757 error = EINVAL; 758 else 759 error = copyin(uap->addr, &r.psr, sizeof(r.psr)); 760 break; 761 case PT_GETREGS: 762 bzero(&r.reg, sizeof(r.reg)); 763 break; 764 case PT_GETFPREGS: 765 bzero(&r.fpreg, sizeof(r.fpreg)); 766 break; 767 case PT_GETDBREGS: 768 bzero(&r.dbreg, sizeof(r.dbreg)); 769 break; 770 case PT_GETREGSET: 771 case PT_SETREGSET: 772 error = copyin(uap->addr, &r.vec, sizeof(r.vec)); 773 break; 774 case PT_SETREGS: 775 error = copyin(uap->addr, &r.reg, sizeof(r.reg)); 776 break; 777 case PT_SETFPREGS: 778 error = copyin(uap->addr, &r.fpreg, sizeof(r.fpreg)); 779 break; 780 case PT_SETDBREGS: 781 error = copyin(uap->addr, &r.dbreg, sizeof(r.dbreg)); 782 break; 783 case PT_SET_EVENT_MASK: 784 if (uap->data != sizeof(r.ptevents)) 785 error = EINVAL; 786 else 787 error = copyin(uap->addr, &r.ptevents, uap->data); 788 break; 789 case PT_IO: 790 error = copyin(uap->addr, &r.piod, sizeof(r.piod)); 791 break; 792 case PT_VM_ENTRY: 793 error = copyin(uap->addr, &r.pve, sizeof(r.pve)); 794 break; 795 case PT_COREDUMP: 796 if (uap->data != sizeof(r.pc)) 797 error = EINVAL; 798 else 799 error = copyin(uap->addr, &r.pc, uap->data); 800 break; 801 case PT_SC_REMOTE: 802 if (uap->data != sizeof(r.sr)) { 803 error = EINVAL; 804 break; 805 } 806 error = copyin(uap->addr, &r.sr, uap->data); 807 if (error != 0) 808 break; 809 if (r.sr.pscr_nargs > nitems(td->td_sa.args)) { 810 error = EINVAL; 811 break; 812 } 813 error = copyin(r.sr.pscr_args, pscr_args, 814 sizeof(u_long) * r.sr.pscr_nargs); 815 if (error != 0) 816 break; 817 r.sr.pscr_args = pscr_args; 818 break; 819 case PTINTERNAL_FIRST ... PTINTERNAL_LAST: 820 error = EINVAL; 821 break; 822 default: 823 addr = uap->addr; 824 break; 825 } 826 if (error != 0) 827 return (error); 828 829 error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data); 830 if (error != 0) 831 return (error); 832 833 switch (uap->req) { 834 case PT_VM_ENTRY: 835 error = copyout(&r.pve, uap->addr, sizeof(r.pve)); 836 break; 837 case PT_IO: 838 error = copyout(&r.piod, uap->addr, sizeof(r.piod)); 839 break; 840 case PT_GETREGS: 841 error = copyout(&r.reg, uap->addr, sizeof(r.reg)); 842 break; 843 case PT_GETFPREGS: 844 error = copyout(&r.fpreg, uap->addr, sizeof(r.fpreg)); 845 break; 846 case PT_GETDBREGS: 847 error = copyout(&r.dbreg, uap->addr, sizeof(r.dbreg)); 848 break; 849 case PT_GETREGSET: 850 error = copyout(&r.vec, uap->addr, sizeof(r.vec)); 851 break; 852 case PT_GET_EVENT_MASK: 853 /* NB: The size in uap->data is validated in kern_ptrace(). */ 854 error = copyout(&r.ptevents, uap->addr, uap->data); 855 break; 856 case PT_LWPINFO: 857 /* NB: The size in uap->data is validated in kern_ptrace(). */ 858 error = copyout(&r.pl, uap->addr, uap->data); 859 break; 860 case PT_GET_SC_ARGS: 861 error = copyout(r.args, uap->addr, MIN(uap->data, 862 sizeof(r.args))); 863 break; 864 case PT_GET_SC_RET: 865 error = copyout(&r.psr, uap->addr, MIN(uap->data, 866 sizeof(r.psr))); 867 break; 868 case PT_SC_REMOTE: 869 error = copyout(&r.sr.pscr_ret, uap->addr + 870 offsetof(struct ptrace_sc_remote, pscr_ret), 871 sizeof(r.sr.pscr_ret)); 872 break; 873 } 874 875 return (error); 876 } 877 878 #ifdef COMPAT_FREEBSD32 879 /* 880 * PROC_READ(regs, td2, addr); 881 * becomes either: 882 * proc_read_regs(td2, addr); 883 * or 884 * proc_read_regs32(td2, addr); 885 * .. except this is done at runtime. There is an additional 886 * complication in that PROC_WRITE disallows 32 bit consumers 887 * from writing to 64 bit address space targets. 888 */ 889 #define PROC_READ(w, t, a) wrap32 ? \ 890 proc_read_ ## w ## 32(t, a) : \ 891 proc_read_ ## w (t, a) 892 #define PROC_WRITE(w, t, a) wrap32 ? \ 893 (safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \ 894 proc_write_ ## w (t, a) 895 #else 896 #define PROC_READ(w, t, a) proc_read_ ## w (t, a) 897 #define PROC_WRITE(w, t, a) proc_write_ ## w (t, a) 898 #endif 899 900 void 901 proc_set_traced(struct proc *p, bool stop) 902 { 903 904 sx_assert(&proctree_lock, SX_XLOCKED); 905 PROC_LOCK_ASSERT(p, MA_OWNED); 906 p->p_flag |= P_TRACED; 907 if (stop) 908 p->p_flag2 |= P2_PTRACE_FSTP; 909 p->p_ptevents = PTRACE_DEFAULT; 910 } 911 912 void 913 ptrace_unsuspend(struct proc *p) 914 { 915 PROC_LOCK_ASSERT(p, MA_OWNED); 916 917 PROC_SLOCK(p); 918 p->p_flag &= ~(P_STOPPED_TRACE | P_STOPPED_SIG | P_WAITED); 919 thread_unsuspend(p); 920 PROC_SUNLOCK(p); 921 itimer_proc_continue(p); 922 kqtimer_proc_continue(p); 923 } 924 925 static int 926 proc_can_ptrace(struct thread *td, struct proc *p) 927 { 928 int error; 929 930 PROC_LOCK_ASSERT(p, MA_OWNED); 931 932 if ((p->p_flag & P_WEXIT) != 0) 933 return (ESRCH); 934 935 if ((error = p_cansee(td, p)) != 0) 936 return (error); 937 if ((error = p_candebug(td, p)) != 0) 938 return (error); 939 940 /* not being traced... */ 941 if ((p->p_flag & P_TRACED) == 0) 942 return (EPERM); 943 944 /* not being traced by YOU */ 945 if (p->p_pptr != td->td_proc) 946 return (EBUSY); 947 948 /* not currently stopped */ 949 if ((p->p_flag & P_STOPPED_TRACE) == 0 || 950 p->p_suspcount != p->p_numthreads || 951 (p->p_flag & P_WAITED) == 0) 952 return (EBUSY); 953 954 return (0); 955 } 956 957 static struct thread * 958 ptrace_sel_coredump_thread(struct proc *p) 959 { 960 struct thread *td2; 961 962 PROC_LOCK_ASSERT(p, MA_OWNED); 963 MPASS((p->p_flag & P_STOPPED_TRACE) != 0); 964 965 FOREACH_THREAD_IN_PROC(p, td2) { 966 if ((td2->td_dbgflags & TDB_SSWITCH) != 0) 967 return (td2); 968 } 969 return (NULL); 970 } 971 972 int 973 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data) 974 { 975 struct iovec iov; 976 struct uio uio; 977 struct proc *curp, *p, *pp; 978 struct thread *td2 = NULL, *td3; 979 struct ptrace_io_desc *piod = NULL; 980 struct ptrace_lwpinfo *pl; 981 struct ptrace_sc_ret *psr; 982 struct ptrace_sc_remote *pscr; 983 struct file *fp; 984 struct ptrace_coredump *pc; 985 struct thr_coredump_req *tcq; 986 struct thr_syscall_req *tsr; 987 int error, num, tmp; 988 lwpid_t tid = 0, *buf; 989 #ifdef COMPAT_FREEBSD32 990 int wrap32 = 0, safe = 0; 991 #endif 992 bool proctree_locked, p2_req_set; 993 994 curp = td->td_proc; 995 proctree_locked = false; 996 p2_req_set = false; 997 998 /* Lock proctree before locking the process. */ 999 switch (req) { 1000 case PT_TRACE_ME: 1001 case PT_ATTACH: 1002 case PT_STEP: 1003 case PT_CONTINUE: 1004 case PT_TO_SCE: 1005 case PT_TO_SCX: 1006 case PT_SYSCALL: 1007 case PT_FOLLOW_FORK: 1008 case PT_LWP_EVENTS: 1009 case PT_GET_EVENT_MASK: 1010 case PT_SET_EVENT_MASK: 1011 case PT_DETACH: 1012 case PT_GET_SC_ARGS: 1013 sx_xlock(&proctree_lock); 1014 proctree_locked = true; 1015 break; 1016 default: 1017 break; 1018 } 1019 1020 if (req == PT_TRACE_ME) { 1021 p = td->td_proc; 1022 PROC_LOCK(p); 1023 } else { 1024 if (pid <= PID_MAX) { 1025 if ((p = pfind(pid)) == NULL) { 1026 if (proctree_locked) 1027 sx_xunlock(&proctree_lock); 1028 return (ESRCH); 1029 } 1030 } else { 1031 td2 = tdfind(pid, -1); 1032 if (td2 == NULL) { 1033 if (proctree_locked) 1034 sx_xunlock(&proctree_lock); 1035 return (ESRCH); 1036 } 1037 p = td2->td_proc; 1038 tid = pid; 1039 pid = p->p_pid; 1040 } 1041 } 1042 AUDIT_ARG_PROCESS(p); 1043 1044 if ((p->p_flag & P_WEXIT) != 0) { 1045 error = ESRCH; 1046 goto fail; 1047 } 1048 if ((error = p_cansee(td, p)) != 0) 1049 goto fail; 1050 1051 if ((error = p_candebug(td, p)) != 0) 1052 goto fail; 1053 1054 /* 1055 * System processes can't be debugged. 1056 */ 1057 if ((p->p_flag & P_SYSTEM) != 0) { 1058 error = EINVAL; 1059 goto fail; 1060 } 1061 1062 if (tid == 0) { 1063 if ((p->p_flag & P_STOPPED_TRACE) != 0) 1064 td2 = p->p_xthread; 1065 if (td2 == NULL) 1066 td2 = FIRST_THREAD_IN_PROC(p); 1067 tid = td2->td_tid; 1068 } 1069 1070 #ifdef COMPAT_FREEBSD32 1071 /* 1072 * Test if we're a 32 bit client and what the target is. 1073 * Set the wrap controls accordingly. 1074 */ 1075 if (SV_CURPROC_FLAG(SV_ILP32)) { 1076 if (SV_PROC_FLAG(td2->td_proc, SV_ILP32)) 1077 safe = 1; 1078 wrap32 = 1; 1079 } 1080 #endif 1081 /* 1082 * Permissions check 1083 */ 1084 switch (req) { 1085 case PT_TRACE_ME: 1086 /* 1087 * Always legal, when there is a parent process which 1088 * could trace us. Otherwise, reject. 1089 */ 1090 if ((p->p_flag & P_TRACED) != 0) { 1091 error = EBUSY; 1092 goto fail; 1093 } 1094 if (p->p_pptr == initproc) { 1095 error = EPERM; 1096 goto fail; 1097 } 1098 break; 1099 1100 case PT_ATTACH: 1101 /* Self */ 1102 if (p == td->td_proc) { 1103 error = EINVAL; 1104 goto fail; 1105 } 1106 1107 /* Already traced */ 1108 if (p->p_flag & P_TRACED) { 1109 error = EBUSY; 1110 goto fail; 1111 } 1112 1113 /* Can't trace an ancestor if you're being traced. */ 1114 if (curp->p_flag & P_TRACED) { 1115 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) { 1116 if (pp == p) { 1117 error = EINVAL; 1118 goto fail; 1119 } 1120 } 1121 } 1122 1123 /* OK */ 1124 break; 1125 1126 case PT_CLEARSTEP: 1127 /* Allow thread to clear single step for itself */ 1128 if (td->td_tid == tid) 1129 break; 1130 1131 /* FALLTHROUGH */ 1132 default: 1133 /* 1134 * Check for ptrace eligibility before waiting for 1135 * holds to drain. 1136 */ 1137 error = proc_can_ptrace(td, p); 1138 if (error != 0) 1139 goto fail; 1140 1141 /* 1142 * Block parallel ptrace requests. Most important, do 1143 * not allow other thread in debugger to continue the 1144 * debuggee until coredump finished. 1145 */ 1146 while ((p->p_flag2 & P2_PTRACEREQ) != 0) { 1147 if (proctree_locked) 1148 sx_xunlock(&proctree_lock); 1149 error = msleep(&p->p_flag2, &p->p_mtx, PPAUSE | PCATCH | 1150 (proctree_locked ? PDROP : 0), "pptrace", 0); 1151 if (proctree_locked) { 1152 sx_xlock(&proctree_lock); 1153 PROC_LOCK(p); 1154 } 1155 if (error == 0 && td2->td_proc != p) 1156 error = ESRCH; 1157 if (error == 0) 1158 error = proc_can_ptrace(td, p); 1159 if (error != 0) 1160 goto fail; 1161 } 1162 1163 /* Ok */ 1164 break; 1165 } 1166 1167 /* 1168 * Keep this process around and request parallel ptrace() 1169 * request to wait until we finish this request. 1170 */ 1171 MPASS((p->p_flag2 & P2_PTRACEREQ) == 0); 1172 p->p_flag2 |= P2_PTRACEREQ; 1173 p2_req_set = true; 1174 _PHOLD(p); 1175 1176 /* 1177 * Actually do the requests 1178 */ 1179 1180 td->td_retval[0] = 0; 1181 1182 switch (req) { 1183 case PT_TRACE_ME: 1184 /* set my trace flag and "owner" so it can read/write me */ 1185 proc_set_traced(p, false); 1186 if (p->p_flag & P_PPWAIT) 1187 p->p_flag |= P_PPTRACE; 1188 CTR1(KTR_PTRACE, "PT_TRACE_ME: pid %d", p->p_pid); 1189 break; 1190 1191 case PT_ATTACH: 1192 /* security check done above */ 1193 /* 1194 * It would be nice if the tracing relationship was separate 1195 * from the parent relationship but that would require 1196 * another set of links in the proc struct or for "wait" 1197 * to scan the entire proc table. To make life easier, 1198 * we just re-parent the process we're trying to trace. 1199 * The old parent is remembered so we can put things back 1200 * on a "detach". 1201 */ 1202 proc_set_traced(p, true); 1203 proc_reparent(p, td->td_proc, false); 1204 CTR2(KTR_PTRACE, "PT_ATTACH: pid %d, oppid %d", p->p_pid, 1205 p->p_oppid); 1206 1207 sx_xunlock(&proctree_lock); 1208 proctree_locked = false; 1209 MPASS(p->p_xthread == NULL); 1210 MPASS((p->p_flag & P_STOPPED_TRACE) == 0); 1211 1212 /* 1213 * If already stopped due to a stop signal, clear the 1214 * existing stop before triggering a traced SIGSTOP. 1215 */ 1216 if ((p->p_flag & P_STOPPED_SIG) != 0) { 1217 PROC_SLOCK(p); 1218 p->p_flag &= ~(P_STOPPED_SIG | P_WAITED); 1219 thread_unsuspend(p); 1220 PROC_SUNLOCK(p); 1221 } 1222 1223 kern_psignal(p, SIGSTOP); 1224 break; 1225 1226 case PT_CLEARSTEP: 1227 CTR2(KTR_PTRACE, "PT_CLEARSTEP: tid %d (pid %d)", td2->td_tid, 1228 p->p_pid); 1229 error = ptrace_clear_single_step(td2); 1230 break; 1231 1232 case PT_SETSTEP: 1233 CTR2(KTR_PTRACE, "PT_SETSTEP: tid %d (pid %d)", td2->td_tid, 1234 p->p_pid); 1235 error = ptrace_single_step(td2); 1236 break; 1237 1238 case PT_SUSPEND: 1239 CTR2(KTR_PTRACE, "PT_SUSPEND: tid %d (pid %d)", td2->td_tid, 1240 p->p_pid); 1241 td2->td_dbgflags |= TDB_SUSPEND; 1242 ast_sched(td2, TDA_SUSPEND); 1243 break; 1244 1245 case PT_RESUME: 1246 CTR2(KTR_PTRACE, "PT_RESUME: tid %d (pid %d)", td2->td_tid, 1247 p->p_pid); 1248 td2->td_dbgflags &= ~TDB_SUSPEND; 1249 break; 1250 1251 case PT_FOLLOW_FORK: 1252 CTR3(KTR_PTRACE, "PT_FOLLOW_FORK: pid %d %s -> %s", p->p_pid, 1253 p->p_ptevents & PTRACE_FORK ? "enabled" : "disabled", 1254 data ? "enabled" : "disabled"); 1255 if (data) 1256 p->p_ptevents |= PTRACE_FORK; 1257 else 1258 p->p_ptevents &= ~PTRACE_FORK; 1259 break; 1260 1261 case PT_LWP_EVENTS: 1262 CTR3(KTR_PTRACE, "PT_LWP_EVENTS: pid %d %s -> %s", p->p_pid, 1263 p->p_ptevents & PTRACE_LWP ? "enabled" : "disabled", 1264 data ? "enabled" : "disabled"); 1265 if (data) 1266 p->p_ptevents |= PTRACE_LWP; 1267 else 1268 p->p_ptevents &= ~PTRACE_LWP; 1269 break; 1270 1271 case PT_GET_EVENT_MASK: 1272 if (data != sizeof(p->p_ptevents)) { 1273 error = EINVAL; 1274 break; 1275 } 1276 CTR2(KTR_PTRACE, "PT_GET_EVENT_MASK: pid %d mask %#x", p->p_pid, 1277 p->p_ptevents); 1278 *(int *)addr = p->p_ptevents; 1279 break; 1280 1281 case PT_SET_EVENT_MASK: 1282 if (data != sizeof(p->p_ptevents)) { 1283 error = EINVAL; 1284 break; 1285 } 1286 tmp = *(int *)addr; 1287 if ((tmp & ~(PTRACE_EXEC | PTRACE_SCE | PTRACE_SCX | 1288 PTRACE_FORK | PTRACE_LWP | PTRACE_VFORK)) != 0) { 1289 error = EINVAL; 1290 break; 1291 } 1292 CTR3(KTR_PTRACE, "PT_SET_EVENT_MASK: pid %d mask %#x -> %#x", 1293 p->p_pid, p->p_ptevents, tmp); 1294 p->p_ptevents = tmp; 1295 break; 1296 1297 case PT_GET_SC_ARGS: 1298 case PTLINUX_GET_SC_ARGS: 1299 CTR2(KTR_PTRACE, "%s: pid %d", req == PT_GET_SC_ARGS ? 1300 "PT_GET_SC_ARGS" : "PT_LINUX_GET_SC_ARGS", p->p_pid); 1301 if (((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) == 0 && 1302 td2->td_sa.code == 0) 1303 #ifdef COMPAT_FREEBSD32 1304 || (wrap32 && !safe) 1305 #endif 1306 ) { 1307 error = EINVAL; 1308 break; 1309 } 1310 if (req == PT_GET_SC_ARGS) { 1311 bzero(addr, sizeof(td2->td_sa.args)); 1312 bcopy(td2->td_sa.args, addr, td2->td_sa.callp->sy_narg * 1313 sizeof(syscallarg_t)); 1314 } else { 1315 /* 1316 * Emulate a Linux bug which which strace(1) depends on: 1317 * at initialization it tests whether ptrace works by 1318 * calling close(2), or some other single-argument 1319 * syscall, _with six arguments_, and then verifies 1320 * whether it can fetch them all using this API; 1321 * otherwise it bails out. 1322 */ 1323 bcopy(td2->td_sa.args, addr, 6 * sizeof(syscallarg_t)); 1324 } 1325 break; 1326 1327 case PT_GET_SC_RET: 1328 if ((td2->td_dbgflags & TDB_SCX) == 0 1329 #ifdef COMPAT_FREEBSD32 1330 || (wrap32 && !safe) 1331 #endif 1332 ) { 1333 error = EINVAL; 1334 break; 1335 } 1336 psr = addr; 1337 bzero(psr, sizeof(*psr)); 1338 psr->sr_error = td2->td_errno; 1339 if (psr->sr_error == 0) { 1340 psr->sr_retval[0] = td2->td_retval[0]; 1341 psr->sr_retval[1] = td2->td_retval[1]; 1342 } 1343 CTR4(KTR_PTRACE, 1344 "PT_GET_SC_RET: pid %d error %d retval %#lx,%#lx", 1345 p->p_pid, psr->sr_error, psr->sr_retval[0], 1346 psr->sr_retval[1]); 1347 break; 1348 1349 case PT_SET_SC_RET: 1350 if ((td2->td_dbgflags & TDB_SCE) == 0 1351 #ifdef COMPAT_FREEBSD32 1352 || (wrap32 && !safe) 1353 #endif 1354 ) { 1355 error = EINVAL; 1356 break; 1357 } 1358 psr = addr; 1359 td2->td_errno = psr->sr_error; 1360 if (td2->td_errno == 0) { 1361 td2->td_retval[0] = psr->sr_retval[0]; 1362 td2->td_retval[1] = psr->sr_retval[1]; 1363 } 1364 td2->td_dbgflags |= TDB_SET_SC_RET; 1365 break; 1366 1367 case PT_STEP: 1368 case PT_CONTINUE: 1369 case PT_TO_SCE: 1370 case PT_TO_SCX: 1371 case PT_SYSCALL: 1372 case PT_DETACH: 1373 /* Zero means do not send any signal */ 1374 if (data < 0 || data > _SIG_MAXSIG) { 1375 error = EINVAL; 1376 break; 1377 } 1378 1379 switch (req) { 1380 case PT_STEP: 1381 CTR3(KTR_PTRACE, "PT_STEP: tid %d (pid %d), sig = %d", 1382 td2->td_tid, p->p_pid, data); 1383 error = ptrace_single_step(td2); 1384 if (error != 0) 1385 goto out; 1386 break; 1387 case PT_CONTINUE: 1388 case PT_TO_SCE: 1389 case PT_TO_SCX: 1390 case PT_SYSCALL: 1391 if (addr != (void *)1) { 1392 error = ptrace_set_pc(td2, 1393 (u_long)(uintfptr_t)addr); 1394 if (error != 0) 1395 goto out; 1396 td2->td_dbgflags |= TDB_USERWR; 1397 } 1398 switch (req) { 1399 case PT_TO_SCE: 1400 p->p_ptevents |= PTRACE_SCE; 1401 CTR4(KTR_PTRACE, 1402 "PT_TO_SCE: pid %d, events = %#x, PC = %#lx, sig = %d", 1403 p->p_pid, p->p_ptevents, 1404 (u_long)(uintfptr_t)addr, data); 1405 break; 1406 case PT_TO_SCX: 1407 p->p_ptevents |= PTRACE_SCX; 1408 CTR4(KTR_PTRACE, 1409 "PT_TO_SCX: pid %d, events = %#x, PC = %#lx, sig = %d", 1410 p->p_pid, p->p_ptevents, 1411 (u_long)(uintfptr_t)addr, data); 1412 break; 1413 case PT_SYSCALL: 1414 p->p_ptevents |= PTRACE_SYSCALL; 1415 CTR4(KTR_PTRACE, 1416 "PT_SYSCALL: pid %d, events = %#x, PC = %#lx, sig = %d", 1417 p->p_pid, p->p_ptevents, 1418 (u_long)(uintfptr_t)addr, data); 1419 break; 1420 case PT_CONTINUE: 1421 CTR3(KTR_PTRACE, 1422 "PT_CONTINUE: pid %d, PC = %#lx, sig = %d", 1423 p->p_pid, (u_long)(uintfptr_t)addr, data); 1424 break; 1425 } 1426 break; 1427 case PT_DETACH: 1428 /* 1429 * Clear P_TRACED before reparenting 1430 * a detached process back to its original 1431 * parent. Otherwise the debugee will be set 1432 * as an orphan of the debugger. 1433 */ 1434 p->p_flag &= ~(P_TRACED | P_WAITED); 1435 1436 /* 1437 * Reset the process parent. 1438 */ 1439 if (p->p_oppid != p->p_pptr->p_pid) { 1440 PROC_LOCK(p->p_pptr); 1441 sigqueue_take(p->p_ksi); 1442 PROC_UNLOCK(p->p_pptr); 1443 1444 pp = proc_realparent(p); 1445 proc_reparent(p, pp, false); 1446 if (pp == initproc) 1447 p->p_sigparent = SIGCHLD; 1448 CTR3(KTR_PTRACE, 1449 "PT_DETACH: pid %d reparented to pid %d, sig %d", 1450 p->p_pid, pp->p_pid, data); 1451 } else { 1452 CTR2(KTR_PTRACE, "PT_DETACH: pid %d, sig %d", 1453 p->p_pid, data); 1454 } 1455 1456 p->p_ptevents = 0; 1457 FOREACH_THREAD_IN_PROC(p, td3) { 1458 if ((td3->td_dbgflags & TDB_FSTP) != 0) { 1459 sigqueue_delete(&td3->td_sigqueue, 1460 SIGSTOP); 1461 } 1462 td3->td_dbgflags &= ~(TDB_XSIG | TDB_FSTP | 1463 TDB_SUSPEND | TDB_BORN); 1464 } 1465 1466 if ((p->p_flag2 & P2_PTRACE_FSTP) != 0) { 1467 sigqueue_delete(&p->p_sigqueue, SIGSTOP); 1468 p->p_flag2 &= ~P2_PTRACE_FSTP; 1469 } 1470 1471 /* 1472 * Send SIGCHLD and wakeup the parent as needed. It 1473 * may be the case that they had stopped the child 1474 * before it got ptraced, and now they're in the middle 1475 * of a wait(2) for it to continue. 1476 */ 1477 PROC_LOCK(p->p_pptr); 1478 childproc_continued(p); 1479 PROC_UNLOCK(p->p_pptr); 1480 break; 1481 } 1482 1483 sx_xunlock(&proctree_lock); 1484 proctree_locked = false; 1485 1486 sendsig: 1487 MPASS(!proctree_locked); 1488 1489 /* 1490 * Clear the pending event for the thread that just 1491 * reported its event (p_xthread), if any. This may 1492 * not be the thread passed to PT_CONTINUE, PT_STEP, 1493 * etc. if the debugger is resuming a different 1494 * thread. There might be no reporting thread if 1495 * the process was just attached. 1496 * 1497 * Deliver any pending signal via the reporting thread. 1498 */ 1499 if (p->p_xthread != NULL) { 1500 p->p_xthread->td_dbgflags &= ~TDB_XSIG; 1501 p->p_xthread->td_xsig = data; 1502 p->p_xthread = NULL; 1503 } 1504 p->p_xsig = data; 1505 1506 /* 1507 * P_WKILLED is insurance that a PT_KILL/SIGKILL 1508 * always works immediately, even if another thread is 1509 * unsuspended first and attempts to handle a 1510 * different signal or if the POSIX.1b style signal 1511 * queue cannot accommodate any new signals. 1512 */ 1513 if (data == SIGKILL) 1514 proc_wkilled(p); 1515 1516 /* 1517 * If the PT_CONTINUE-like operation is attempted on 1518 * the thread on sleepq, this is possible only after 1519 * the transparent PT_ATTACH. In this case, if the 1520 * caller modified the thread state, e.g. by writing 1521 * register file or specifying the pc, make the thread 1522 * xstopped by waking it up. 1523 */ 1524 if ((td2->td_dbgflags & TDB_USERWR) != 0 && 1525 pt_attach_transparent) { 1526 thread_lock(td2); 1527 if (TD_ON_SLEEPQ(td2) && 1528 (td2->td_flags & TDF_SINTR) != 0) { 1529 td2->td_dbgflags &= ~TDB_USERWR; 1530 sleepq_abort(td2, EINTR); 1531 } else { 1532 thread_unlock(td2); 1533 } 1534 } 1535 1536 /* 1537 * Unsuspend all threads. To leave a thread 1538 * suspended, use PT_SUSPEND to suspend it before 1539 * continuing the process. 1540 */ 1541 ptrace_unsuspend(p); 1542 break; 1543 1544 case PT_WRITE_I: 1545 case PT_WRITE_D: 1546 td2->td_dbgflags |= TDB_USERWR; 1547 PROC_UNLOCK(p); 1548 error = 0; 1549 if (proc_writemem(td, p, (off_t)(uintptr_t)addr, &data, 1550 sizeof(int)) != sizeof(int)) 1551 error = ENOMEM; 1552 else 1553 CTR3(KTR_PTRACE, "PT_WRITE: pid %d: %p <= %#x", 1554 p->p_pid, addr, data); 1555 PROC_LOCK(p); 1556 break; 1557 1558 case PT_READ_I: 1559 case PT_READ_D: 1560 PROC_UNLOCK(p); 1561 error = tmp = 0; 1562 if (proc_readmem(td, p, (off_t)(uintptr_t)addr, &tmp, 1563 sizeof(int)) != sizeof(int)) 1564 error = ENOMEM; 1565 else 1566 CTR3(KTR_PTRACE, "PT_READ: pid %d: %p >= %#x", 1567 p->p_pid, addr, tmp); 1568 td->td_retval[0] = tmp; 1569 PROC_LOCK(p); 1570 break; 1571 1572 case PT_IO: 1573 piod = addr; 1574 if (piod->piod_len > SSIZE_MAX) { 1575 error = EINVAL; 1576 goto out; 1577 } 1578 iov.iov_base = piod->piod_addr; 1579 iov.iov_len = piod->piod_len; 1580 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs; 1581 uio.uio_resid = piod->piod_len; 1582 uio.uio_iov = &iov; 1583 uio.uio_iovcnt = 1; 1584 uio.uio_segflg = UIO_USERSPACE; 1585 uio.uio_td = td; 1586 switch (piod->piod_op) { 1587 case PIOD_READ_D: 1588 case PIOD_READ_I: 1589 CTR3(KTR_PTRACE, "PT_IO: pid %d: READ (%p, %#x)", 1590 p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid); 1591 uio.uio_rw = UIO_READ; 1592 break; 1593 case PIOD_WRITE_D: 1594 case PIOD_WRITE_I: 1595 CTR3(KTR_PTRACE, "PT_IO: pid %d: WRITE (%p, %#x)", 1596 p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid); 1597 td2->td_dbgflags |= TDB_USERWR; 1598 uio.uio_rw = UIO_WRITE; 1599 break; 1600 default: 1601 error = EINVAL; 1602 goto out; 1603 } 1604 PROC_UNLOCK(p); 1605 error = proc_rwmem(p, &uio, 0); 1606 piod->piod_len -= uio.uio_resid; 1607 PROC_LOCK(p); 1608 break; 1609 1610 case PT_KILL: 1611 CTR1(KTR_PTRACE, "PT_KILL: pid %d", p->p_pid); 1612 data = SIGKILL; 1613 goto sendsig; /* in PT_CONTINUE above */ 1614 1615 case PT_SETREGS: 1616 CTR2(KTR_PTRACE, "PT_SETREGS: tid %d (pid %d)", td2->td_tid, 1617 p->p_pid); 1618 td2->td_dbgflags |= TDB_USERWR; 1619 error = PROC_WRITE(regs, td2, addr); 1620 break; 1621 1622 case PT_GETREGS: 1623 CTR2(KTR_PTRACE, "PT_GETREGS: tid %d (pid %d)", td2->td_tid, 1624 p->p_pid); 1625 error = PROC_READ(regs, td2, addr); 1626 break; 1627 1628 case PT_SETFPREGS: 1629 CTR2(KTR_PTRACE, "PT_SETFPREGS: tid %d (pid %d)", td2->td_tid, 1630 p->p_pid); 1631 td2->td_dbgflags |= TDB_USERWR; 1632 error = PROC_WRITE(fpregs, td2, addr); 1633 break; 1634 1635 case PT_GETFPREGS: 1636 CTR2(KTR_PTRACE, "PT_GETFPREGS: tid %d (pid %d)", td2->td_tid, 1637 p->p_pid); 1638 error = PROC_READ(fpregs, td2, addr); 1639 break; 1640 1641 case PT_SETDBREGS: 1642 CTR2(KTR_PTRACE, "PT_SETDBREGS: tid %d (pid %d)", td2->td_tid, 1643 p->p_pid); 1644 td2->td_dbgflags |= TDB_USERWR; 1645 error = PROC_WRITE(dbregs, td2, addr); 1646 break; 1647 1648 case PT_GETDBREGS: 1649 CTR2(KTR_PTRACE, "PT_GETDBREGS: tid %d (pid %d)", td2->td_tid, 1650 p->p_pid); 1651 error = PROC_READ(dbregs, td2, addr); 1652 break; 1653 1654 case PT_SETREGSET: 1655 CTR2(KTR_PTRACE, "PT_SETREGSET: tid %d (pid %d)", td2->td_tid, 1656 p->p_pid); 1657 error = proc_write_regset(td2, data, addr); 1658 break; 1659 1660 case PT_GETREGSET: 1661 CTR2(KTR_PTRACE, "PT_GETREGSET: tid %d (pid %d)", td2->td_tid, 1662 p->p_pid); 1663 error = proc_read_regset(td2, data, addr); 1664 break; 1665 1666 case PT_LWPINFO: 1667 if (data <= 0 || data > sizeof(*pl)) { 1668 error = EINVAL; 1669 break; 1670 } 1671 pl = addr; 1672 bzero(pl, sizeof(*pl)); 1673 pl->pl_lwpid = td2->td_tid; 1674 pl->pl_event = PL_EVENT_NONE; 1675 pl->pl_flags = 0; 1676 if (td2->td_dbgflags & TDB_XSIG) { 1677 pl->pl_event = PL_EVENT_SIGNAL; 1678 if (td2->td_si.si_signo != 0 && 1679 data >= offsetof(struct ptrace_lwpinfo, pl_siginfo) 1680 + sizeof(pl->pl_siginfo)){ 1681 pl->pl_flags |= PL_FLAG_SI; 1682 pl->pl_siginfo = td2->td_si; 1683 } 1684 } 1685 if (td2->td_dbgflags & TDB_SCE) 1686 pl->pl_flags |= PL_FLAG_SCE; 1687 else if (td2->td_dbgflags & TDB_SCX) 1688 pl->pl_flags |= PL_FLAG_SCX; 1689 if (td2->td_dbgflags & TDB_EXEC) 1690 pl->pl_flags |= PL_FLAG_EXEC; 1691 if (td2->td_dbgflags & TDB_FORK) { 1692 pl->pl_flags |= PL_FLAG_FORKED; 1693 pl->pl_child_pid = td2->td_dbg_forked; 1694 if (td2->td_dbgflags & TDB_VFORK) 1695 pl->pl_flags |= PL_FLAG_VFORKED; 1696 } else if ((td2->td_dbgflags & (TDB_SCX | TDB_VFORK)) == 1697 TDB_VFORK) 1698 pl->pl_flags |= PL_FLAG_VFORK_DONE; 1699 if (td2->td_dbgflags & TDB_CHILD) 1700 pl->pl_flags |= PL_FLAG_CHILD; 1701 if (td2->td_dbgflags & TDB_BORN) 1702 pl->pl_flags |= PL_FLAG_BORN; 1703 if (td2->td_dbgflags & TDB_EXIT) 1704 pl->pl_flags |= PL_FLAG_EXITED; 1705 pl->pl_sigmask = td2->td_sigmask; 1706 pl->pl_siglist = td2->td_siglist; 1707 strcpy(pl->pl_tdname, td2->td_name); 1708 if (td2->td_sa.code != 0) { 1709 pl->pl_syscall_code = td2->td_sa.code; 1710 pl->pl_syscall_narg = td2->td_sa.callp->sy_narg; 1711 } 1712 CTR6(KTR_PTRACE, 1713 "PT_LWPINFO: tid %d (pid %d) event %d flags %#x child pid %d syscall %d", 1714 td2->td_tid, p->p_pid, pl->pl_event, pl->pl_flags, 1715 pl->pl_child_pid, pl->pl_syscall_code); 1716 break; 1717 1718 case PT_GETNUMLWPS: 1719 CTR2(KTR_PTRACE, "PT_GETNUMLWPS: pid %d: %d threads", p->p_pid, 1720 p->p_numthreads); 1721 td->td_retval[0] = p->p_numthreads; 1722 break; 1723 1724 case PT_GETLWPLIST: 1725 CTR3(KTR_PTRACE, "PT_GETLWPLIST: pid %d: data %d, actual %d", 1726 p->p_pid, data, p->p_numthreads); 1727 if (data <= 0) { 1728 error = EINVAL; 1729 break; 1730 } 1731 num = imin(p->p_numthreads, data); 1732 PROC_UNLOCK(p); 1733 buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK); 1734 tmp = 0; 1735 PROC_LOCK(p); 1736 FOREACH_THREAD_IN_PROC(p, td2) { 1737 if (tmp >= num) 1738 break; 1739 buf[tmp++] = td2->td_tid; 1740 } 1741 PROC_UNLOCK(p); 1742 error = copyout(buf, addr, tmp * sizeof(lwpid_t)); 1743 free(buf, M_TEMP); 1744 if (!error) 1745 td->td_retval[0] = tmp; 1746 PROC_LOCK(p); 1747 break; 1748 1749 case PT_VM_TIMESTAMP: 1750 CTR2(KTR_PTRACE, "PT_VM_TIMESTAMP: pid %d: timestamp %d", 1751 p->p_pid, p->p_vmspace->vm_map.timestamp); 1752 td->td_retval[0] = p->p_vmspace->vm_map.timestamp; 1753 break; 1754 1755 case PT_VM_ENTRY: 1756 PROC_UNLOCK(p); 1757 error = ptrace_vm_entry(td, p, addr); 1758 PROC_LOCK(p); 1759 break; 1760 1761 case PT_COREDUMP: 1762 pc = addr; 1763 CTR2(KTR_PTRACE, "PT_COREDUMP: pid %d, fd %d", 1764 p->p_pid, pc->pc_fd); 1765 1766 if ((pc->pc_flags & ~(PC_COMPRESS | PC_ALL)) != 0) { 1767 error = EINVAL; 1768 break; 1769 } 1770 PROC_UNLOCK(p); 1771 1772 tcq = malloc(sizeof(*tcq), M_TEMP, M_WAITOK | M_ZERO); 1773 fp = NULL; 1774 error = fget_write(td, pc->pc_fd, &cap_write_rights, &fp); 1775 if (error != 0) 1776 goto coredump_cleanup_nofp; 1777 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VREG) { 1778 error = EPIPE; 1779 goto coredump_cleanup; 1780 } 1781 1782 PROC_LOCK(p); 1783 error = proc_can_ptrace(td, p); 1784 if (error != 0) 1785 goto coredump_cleanup_locked; 1786 1787 td2 = ptrace_sel_coredump_thread(p); 1788 if (td2 == NULL) { 1789 error = EBUSY; 1790 goto coredump_cleanup_locked; 1791 } 1792 KASSERT((td2->td_dbgflags & (TDB_COREDUMPREQ | 1793 TDB_SCREMOTEREQ)) == 0, 1794 ("proc %d tid %d req coredump", p->p_pid, td2->td_tid)); 1795 1796 tcq->tc_vp = fp->f_vnode; 1797 tcq->tc_limit = pc->pc_limit == 0 ? OFF_MAX : pc->pc_limit; 1798 tcq->tc_flags = SVC_PT_COREDUMP; 1799 if ((pc->pc_flags & PC_COMPRESS) == 0) 1800 tcq->tc_flags |= SVC_NOCOMPRESS; 1801 if ((pc->pc_flags & PC_ALL) != 0) 1802 tcq->tc_flags |= SVC_ALL; 1803 td2->td_remotereq = tcq; 1804 td2->td_dbgflags |= TDB_COREDUMPREQ; 1805 thread_run_flash(td2); 1806 while ((td2->td_dbgflags & TDB_COREDUMPREQ) != 0) 1807 msleep(p, &p->p_mtx, PPAUSE, "crdmp", 0); 1808 error = tcq->tc_error; 1809 coredump_cleanup_locked: 1810 PROC_UNLOCK(p); 1811 coredump_cleanup: 1812 fdrop(fp, td); 1813 coredump_cleanup_nofp: 1814 free(tcq, M_TEMP); 1815 PROC_LOCK(p); 1816 break; 1817 1818 case PT_SC_REMOTE: 1819 pscr = addr; 1820 CTR2(KTR_PTRACE, "PT_SC_REMOTE: pid %d, syscall %d", 1821 p->p_pid, pscr->pscr_syscall); 1822 if ((td2->td_dbgflags & TDB_BOUNDARY) == 0) { 1823 error = EBUSY; 1824 break; 1825 } 1826 PROC_UNLOCK(p); 1827 MPASS(pscr->pscr_nargs <= nitems(td->td_sa.args)); 1828 1829 tsr = malloc(sizeof(struct thr_syscall_req), M_TEMP, 1830 M_WAITOK | M_ZERO); 1831 1832 tsr->ts_sa.code = pscr->pscr_syscall; 1833 tsr->ts_nargs = pscr->pscr_nargs; 1834 memcpy(&tsr->ts_sa.args, pscr->pscr_args, 1835 sizeof(syscallarg_t) * tsr->ts_nargs); 1836 1837 PROC_LOCK(p); 1838 error = proc_can_ptrace(td, p); 1839 if (error != 0) { 1840 free(tsr, M_TEMP); 1841 break; 1842 } 1843 if (td2->td_proc != p) { 1844 free(tsr, M_TEMP); 1845 error = ESRCH; 1846 break; 1847 } 1848 KASSERT((td2->td_dbgflags & (TDB_COREDUMPREQ | 1849 TDB_SCREMOTEREQ)) == 0, 1850 ("proc %d tid %d req coredump", p->p_pid, td2->td_tid)); 1851 1852 td2->td_remotereq = tsr; 1853 td2->td_dbgflags |= TDB_SCREMOTEREQ; 1854 thread_run_flash(td2); 1855 while ((td2->td_dbgflags & TDB_SCREMOTEREQ) != 0) 1856 msleep(p, &p->p_mtx, PPAUSE, "pscrx", 0); 1857 error = 0; 1858 memcpy(&pscr->pscr_ret, &tsr->ts_ret, sizeof(tsr->ts_ret)); 1859 free(tsr, M_TEMP); 1860 break; 1861 1862 default: 1863 #ifdef __HAVE_PTRACE_MACHDEP 1864 if (req >= PT_FIRSTMACH) { 1865 PROC_UNLOCK(p); 1866 error = cpu_ptrace(td2, req, addr, data); 1867 PROC_LOCK(p); 1868 } else 1869 #endif 1870 /* Unknown request. */ 1871 error = EINVAL; 1872 break; 1873 } 1874 out: 1875 /* Drop our hold on this process now that the request has completed. */ 1876 _PRELE(p); 1877 fail: 1878 if (p2_req_set) { 1879 if ((p->p_flag2 & P2_PTRACEREQ) != 0) 1880 wakeup(&p->p_flag2); 1881 p->p_flag2 &= ~P2_PTRACEREQ; 1882 } 1883 PROC_UNLOCK(p); 1884 if (proctree_locked) 1885 sx_xunlock(&proctree_lock); 1886 return (error); 1887 } 1888 #undef PROC_READ 1889 #undef PROC_WRITE 1890