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_GETREGS: 756 bzero(&r.reg, sizeof(r.reg)); 757 break; 758 case PT_GETFPREGS: 759 bzero(&r.fpreg, sizeof(r.fpreg)); 760 break; 761 case PT_GETDBREGS: 762 bzero(&r.dbreg, sizeof(r.dbreg)); 763 break; 764 case PT_GETREGSET: 765 case PT_SETREGSET: 766 error = copyin(uap->addr, &r.vec, sizeof(r.vec)); 767 break; 768 case PT_SETREGS: 769 error = copyin(uap->addr, &r.reg, sizeof(r.reg)); 770 break; 771 case PT_SETFPREGS: 772 error = copyin(uap->addr, &r.fpreg, sizeof(r.fpreg)); 773 break; 774 case PT_SETDBREGS: 775 error = copyin(uap->addr, &r.dbreg, sizeof(r.dbreg)); 776 break; 777 case PT_SET_EVENT_MASK: 778 if (uap->data != sizeof(r.ptevents)) 779 error = EINVAL; 780 else 781 error = copyin(uap->addr, &r.ptevents, uap->data); 782 break; 783 case PT_IO: 784 error = copyin(uap->addr, &r.piod, sizeof(r.piod)); 785 break; 786 case PT_VM_ENTRY: 787 error = copyin(uap->addr, &r.pve, sizeof(r.pve)); 788 break; 789 case PT_COREDUMP: 790 if (uap->data != sizeof(r.pc)) 791 error = EINVAL; 792 else 793 error = copyin(uap->addr, &r.pc, uap->data); 794 break; 795 case PT_SC_REMOTE: 796 if (uap->data != sizeof(r.sr)) { 797 error = EINVAL; 798 break; 799 } 800 error = copyin(uap->addr, &r.sr, uap->data); 801 if (error != 0) 802 break; 803 if (r.sr.pscr_nargs > nitems(td->td_sa.args)) { 804 error = EINVAL; 805 break; 806 } 807 error = copyin(r.sr.pscr_args, pscr_args, 808 sizeof(u_long) * r.sr.pscr_nargs); 809 if (error != 0) 810 break; 811 r.sr.pscr_args = pscr_args; 812 break; 813 case PTINTERNAL_FIRST ... PTINTERNAL_LAST: 814 error = EINVAL; 815 break; 816 default: 817 addr = uap->addr; 818 break; 819 } 820 if (error != 0) 821 return (error); 822 823 error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data); 824 if (error != 0) 825 return (error); 826 827 switch (uap->req) { 828 case PT_VM_ENTRY: 829 error = copyout(&r.pve, uap->addr, sizeof(r.pve)); 830 break; 831 case PT_IO: 832 error = copyout(&r.piod, uap->addr, sizeof(r.piod)); 833 break; 834 case PT_GETREGS: 835 error = copyout(&r.reg, uap->addr, sizeof(r.reg)); 836 break; 837 case PT_GETFPREGS: 838 error = copyout(&r.fpreg, uap->addr, sizeof(r.fpreg)); 839 break; 840 case PT_GETDBREGS: 841 error = copyout(&r.dbreg, uap->addr, sizeof(r.dbreg)); 842 break; 843 case PT_GETREGSET: 844 error = copyout(&r.vec, uap->addr, sizeof(r.vec)); 845 break; 846 case PT_GET_EVENT_MASK: 847 /* NB: The size in uap->data is validated in kern_ptrace(). */ 848 error = copyout(&r.ptevents, uap->addr, uap->data); 849 break; 850 case PT_LWPINFO: 851 /* NB: The size in uap->data is validated in kern_ptrace(). */ 852 error = copyout(&r.pl, uap->addr, uap->data); 853 break; 854 case PT_GET_SC_ARGS: 855 error = copyout(r.args, uap->addr, MIN(uap->data, 856 sizeof(r.args))); 857 break; 858 case PT_GET_SC_RET: 859 error = copyout(&r.psr, uap->addr, MIN(uap->data, 860 sizeof(r.psr))); 861 break; 862 case PT_SC_REMOTE: 863 error = copyout(&r.sr.pscr_ret, uap->addr + 864 offsetof(struct ptrace_sc_remote, pscr_ret), 865 sizeof(r.sr.pscr_ret)); 866 break; 867 } 868 869 return (error); 870 } 871 872 #ifdef COMPAT_FREEBSD32 873 /* 874 * PROC_READ(regs, td2, addr); 875 * becomes either: 876 * proc_read_regs(td2, addr); 877 * or 878 * proc_read_regs32(td2, addr); 879 * .. except this is done at runtime. There is an additional 880 * complication in that PROC_WRITE disallows 32 bit consumers 881 * from writing to 64 bit address space targets. 882 */ 883 #define PROC_READ(w, t, a) wrap32 ? \ 884 proc_read_ ## w ## 32(t, a) : \ 885 proc_read_ ## w (t, a) 886 #define PROC_WRITE(w, t, a) wrap32 ? \ 887 (safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \ 888 proc_write_ ## w (t, a) 889 #else 890 #define PROC_READ(w, t, a) proc_read_ ## w (t, a) 891 #define PROC_WRITE(w, t, a) proc_write_ ## w (t, a) 892 #endif 893 894 void 895 proc_set_traced(struct proc *p, bool stop) 896 { 897 898 sx_assert(&proctree_lock, SX_XLOCKED); 899 PROC_LOCK_ASSERT(p, MA_OWNED); 900 p->p_flag |= P_TRACED; 901 if (stop) 902 p->p_flag2 |= P2_PTRACE_FSTP; 903 p->p_ptevents = PTRACE_DEFAULT; 904 } 905 906 void 907 ptrace_unsuspend(struct proc *p) 908 { 909 PROC_LOCK_ASSERT(p, MA_OWNED); 910 911 PROC_SLOCK(p); 912 p->p_flag &= ~(P_STOPPED_TRACE | P_STOPPED_SIG | P_WAITED); 913 thread_unsuspend(p); 914 PROC_SUNLOCK(p); 915 itimer_proc_continue(p); 916 kqtimer_proc_continue(p); 917 } 918 919 static int 920 proc_can_ptrace(struct thread *td, struct proc *p) 921 { 922 int error; 923 924 PROC_LOCK_ASSERT(p, MA_OWNED); 925 926 if ((p->p_flag & P_WEXIT) != 0) 927 return (ESRCH); 928 929 if ((error = p_cansee(td, p)) != 0) 930 return (error); 931 if ((error = p_candebug(td, p)) != 0) 932 return (error); 933 934 /* not being traced... */ 935 if ((p->p_flag & P_TRACED) == 0) 936 return (EPERM); 937 938 /* not being traced by YOU */ 939 if (p->p_pptr != td->td_proc) 940 return (EBUSY); 941 942 /* not currently stopped */ 943 if ((p->p_flag & P_STOPPED_TRACE) == 0 || 944 p->p_suspcount != p->p_numthreads || 945 (p->p_flag & P_WAITED) == 0) 946 return (EBUSY); 947 948 return (0); 949 } 950 951 static struct thread * 952 ptrace_sel_coredump_thread(struct proc *p) 953 { 954 struct thread *td2; 955 956 PROC_LOCK_ASSERT(p, MA_OWNED); 957 MPASS((p->p_flag & P_STOPPED_TRACE) != 0); 958 959 FOREACH_THREAD_IN_PROC(p, td2) { 960 if ((td2->td_dbgflags & TDB_SSWITCH) != 0) 961 return (td2); 962 } 963 return (NULL); 964 } 965 966 int 967 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data) 968 { 969 struct iovec iov; 970 struct uio uio; 971 struct proc *curp, *p, *pp; 972 struct thread *td2 = NULL, *td3; 973 struct ptrace_io_desc *piod = NULL; 974 struct ptrace_lwpinfo *pl; 975 struct ptrace_sc_ret *psr; 976 struct ptrace_sc_remote *pscr; 977 struct file *fp; 978 struct ptrace_coredump *pc; 979 struct thr_coredump_req *tcq; 980 struct thr_syscall_req *tsr; 981 int error, num, tmp; 982 lwpid_t tid = 0, *buf; 983 #ifdef COMPAT_FREEBSD32 984 int wrap32 = 0, safe = 0; 985 #endif 986 bool proctree_locked, p2_req_set; 987 988 curp = td->td_proc; 989 proctree_locked = false; 990 p2_req_set = false; 991 992 /* Lock proctree before locking the process. */ 993 switch (req) { 994 case PT_TRACE_ME: 995 case PT_ATTACH: 996 case PT_STEP: 997 case PT_CONTINUE: 998 case PT_TO_SCE: 999 case PT_TO_SCX: 1000 case PT_SYSCALL: 1001 case PT_FOLLOW_FORK: 1002 case PT_LWP_EVENTS: 1003 case PT_GET_EVENT_MASK: 1004 case PT_SET_EVENT_MASK: 1005 case PT_DETACH: 1006 case PT_GET_SC_ARGS: 1007 sx_xlock(&proctree_lock); 1008 proctree_locked = true; 1009 break; 1010 default: 1011 break; 1012 } 1013 1014 if (req == PT_TRACE_ME) { 1015 p = td->td_proc; 1016 PROC_LOCK(p); 1017 } else { 1018 if (pid <= PID_MAX) { 1019 if ((p = pfind(pid)) == NULL) { 1020 if (proctree_locked) 1021 sx_xunlock(&proctree_lock); 1022 return (ESRCH); 1023 } 1024 } else { 1025 td2 = tdfind(pid, -1); 1026 if (td2 == NULL) { 1027 if (proctree_locked) 1028 sx_xunlock(&proctree_lock); 1029 return (ESRCH); 1030 } 1031 p = td2->td_proc; 1032 tid = pid; 1033 pid = p->p_pid; 1034 } 1035 } 1036 AUDIT_ARG_PROCESS(p); 1037 1038 if ((p->p_flag & P_WEXIT) != 0) { 1039 error = ESRCH; 1040 goto fail; 1041 } 1042 if ((error = p_cansee(td, p)) != 0) 1043 goto fail; 1044 1045 if ((error = p_candebug(td, p)) != 0) 1046 goto fail; 1047 1048 /* 1049 * System processes can't be debugged. 1050 */ 1051 if ((p->p_flag & P_SYSTEM) != 0) { 1052 error = EINVAL; 1053 goto fail; 1054 } 1055 1056 if (tid == 0) { 1057 if ((p->p_flag & P_STOPPED_TRACE) != 0) 1058 td2 = p->p_xthread; 1059 if (td2 == NULL) 1060 td2 = FIRST_THREAD_IN_PROC(p); 1061 tid = td2->td_tid; 1062 } 1063 1064 #ifdef COMPAT_FREEBSD32 1065 /* 1066 * Test if we're a 32 bit client and what the target is. 1067 * Set the wrap controls accordingly. 1068 */ 1069 if (SV_CURPROC_FLAG(SV_ILP32)) { 1070 if (SV_PROC_FLAG(td2->td_proc, SV_ILP32)) 1071 safe = 1; 1072 wrap32 = 1; 1073 } 1074 #endif 1075 /* 1076 * Permissions check 1077 */ 1078 switch (req) { 1079 case PT_TRACE_ME: 1080 /* 1081 * Always legal, when there is a parent process which 1082 * could trace us. Otherwise, reject. 1083 */ 1084 if ((p->p_flag & P_TRACED) != 0) { 1085 error = EBUSY; 1086 goto fail; 1087 } 1088 if (p->p_pptr == initproc) { 1089 error = EPERM; 1090 goto fail; 1091 } 1092 break; 1093 1094 case PT_ATTACH: 1095 /* Self */ 1096 if (p == td->td_proc) { 1097 error = EINVAL; 1098 goto fail; 1099 } 1100 1101 /* Already traced */ 1102 if (p->p_flag & P_TRACED) { 1103 error = EBUSY; 1104 goto fail; 1105 } 1106 1107 /* Can't trace an ancestor if you're being traced. */ 1108 if (curp->p_flag & P_TRACED) { 1109 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) { 1110 if (pp == p) { 1111 error = EINVAL; 1112 goto fail; 1113 } 1114 } 1115 } 1116 1117 /* OK */ 1118 break; 1119 1120 case PT_CLEARSTEP: 1121 /* Allow thread to clear single step for itself */ 1122 if (td->td_tid == tid) 1123 break; 1124 1125 /* FALLTHROUGH */ 1126 default: 1127 /* 1128 * Check for ptrace eligibility before waiting for 1129 * holds to drain. 1130 */ 1131 error = proc_can_ptrace(td, p); 1132 if (error != 0) 1133 goto fail; 1134 1135 /* 1136 * Block parallel ptrace requests. Most important, do 1137 * not allow other thread in debugger to continue the 1138 * debuggee until coredump finished. 1139 */ 1140 while ((p->p_flag2 & P2_PTRACEREQ) != 0) { 1141 if (proctree_locked) 1142 sx_xunlock(&proctree_lock); 1143 error = msleep(&p->p_flag2, &p->p_mtx, PPAUSE | PCATCH | 1144 (proctree_locked ? PDROP : 0), "pptrace", 0); 1145 if (proctree_locked) { 1146 sx_xlock(&proctree_lock); 1147 PROC_LOCK(p); 1148 } 1149 if (error == 0 && td2->td_proc != p) 1150 error = ESRCH; 1151 if (error == 0) 1152 error = proc_can_ptrace(td, p); 1153 if (error != 0) 1154 goto fail; 1155 } 1156 1157 /* Ok */ 1158 break; 1159 } 1160 1161 /* 1162 * Keep this process around and request parallel ptrace() 1163 * request to wait until we finish this request. 1164 */ 1165 MPASS((p->p_flag2 & P2_PTRACEREQ) == 0); 1166 p->p_flag2 |= P2_PTRACEREQ; 1167 p2_req_set = true; 1168 _PHOLD(p); 1169 1170 /* 1171 * Actually do the requests 1172 */ 1173 1174 td->td_retval[0] = 0; 1175 1176 switch (req) { 1177 case PT_TRACE_ME: 1178 /* set my trace flag and "owner" so it can read/write me */ 1179 proc_set_traced(p, false); 1180 if (p->p_flag & P_PPWAIT) 1181 p->p_flag |= P_PPTRACE; 1182 CTR1(KTR_PTRACE, "PT_TRACE_ME: pid %d", p->p_pid); 1183 break; 1184 1185 case PT_ATTACH: 1186 /* security check done above */ 1187 /* 1188 * It would be nice if the tracing relationship was separate 1189 * from the parent relationship but that would require 1190 * another set of links in the proc struct or for "wait" 1191 * to scan the entire proc table. To make life easier, 1192 * we just re-parent the process we're trying to trace. 1193 * The old parent is remembered so we can put things back 1194 * on a "detach". 1195 */ 1196 proc_set_traced(p, true); 1197 proc_reparent(p, td->td_proc, false); 1198 CTR2(KTR_PTRACE, "PT_ATTACH: pid %d, oppid %d", p->p_pid, 1199 p->p_oppid); 1200 1201 sx_xunlock(&proctree_lock); 1202 proctree_locked = false; 1203 MPASS(p->p_xthread == NULL); 1204 MPASS((p->p_flag & P_STOPPED_TRACE) == 0); 1205 1206 /* 1207 * If already stopped due to a stop signal, clear the 1208 * existing stop before triggering a traced SIGSTOP. 1209 */ 1210 if ((p->p_flag & P_STOPPED_SIG) != 0) { 1211 PROC_SLOCK(p); 1212 p->p_flag &= ~(P_STOPPED_SIG | P_WAITED); 1213 thread_unsuspend(p); 1214 PROC_SUNLOCK(p); 1215 } 1216 1217 kern_psignal(p, SIGSTOP); 1218 break; 1219 1220 case PT_CLEARSTEP: 1221 CTR2(KTR_PTRACE, "PT_CLEARSTEP: tid %d (pid %d)", td2->td_tid, 1222 p->p_pid); 1223 error = ptrace_clear_single_step(td2); 1224 break; 1225 1226 case PT_SETSTEP: 1227 CTR2(KTR_PTRACE, "PT_SETSTEP: tid %d (pid %d)", td2->td_tid, 1228 p->p_pid); 1229 error = ptrace_single_step(td2); 1230 break; 1231 1232 case PT_SUSPEND: 1233 CTR2(KTR_PTRACE, "PT_SUSPEND: tid %d (pid %d)", td2->td_tid, 1234 p->p_pid); 1235 td2->td_dbgflags |= TDB_SUSPEND; 1236 ast_sched(td2, TDA_SUSPEND); 1237 break; 1238 1239 case PT_RESUME: 1240 CTR2(KTR_PTRACE, "PT_RESUME: tid %d (pid %d)", td2->td_tid, 1241 p->p_pid); 1242 td2->td_dbgflags &= ~TDB_SUSPEND; 1243 break; 1244 1245 case PT_FOLLOW_FORK: 1246 CTR3(KTR_PTRACE, "PT_FOLLOW_FORK: pid %d %s -> %s", p->p_pid, 1247 p->p_ptevents & PTRACE_FORK ? "enabled" : "disabled", 1248 data ? "enabled" : "disabled"); 1249 if (data) 1250 p->p_ptevents |= PTRACE_FORK; 1251 else 1252 p->p_ptevents &= ~PTRACE_FORK; 1253 break; 1254 1255 case PT_LWP_EVENTS: 1256 CTR3(KTR_PTRACE, "PT_LWP_EVENTS: pid %d %s -> %s", p->p_pid, 1257 p->p_ptevents & PTRACE_LWP ? "enabled" : "disabled", 1258 data ? "enabled" : "disabled"); 1259 if (data) 1260 p->p_ptevents |= PTRACE_LWP; 1261 else 1262 p->p_ptevents &= ~PTRACE_LWP; 1263 break; 1264 1265 case PT_GET_EVENT_MASK: 1266 if (data != sizeof(p->p_ptevents)) { 1267 error = EINVAL; 1268 break; 1269 } 1270 CTR2(KTR_PTRACE, "PT_GET_EVENT_MASK: pid %d mask %#x", p->p_pid, 1271 p->p_ptevents); 1272 *(int *)addr = p->p_ptevents; 1273 break; 1274 1275 case PT_SET_EVENT_MASK: 1276 if (data != sizeof(p->p_ptevents)) { 1277 error = EINVAL; 1278 break; 1279 } 1280 tmp = *(int *)addr; 1281 if ((tmp & ~(PTRACE_EXEC | PTRACE_SCE | PTRACE_SCX | 1282 PTRACE_FORK | PTRACE_LWP | PTRACE_VFORK)) != 0) { 1283 error = EINVAL; 1284 break; 1285 } 1286 CTR3(KTR_PTRACE, "PT_SET_EVENT_MASK: pid %d mask %#x -> %#x", 1287 p->p_pid, p->p_ptevents, tmp); 1288 p->p_ptevents = tmp; 1289 break; 1290 1291 case PT_GET_SC_ARGS: 1292 case PTLINUX_GET_SC_ARGS: 1293 CTR2(KTR_PTRACE, "%s: pid %d", req == PT_GET_SC_ARGS ? 1294 "PT_GET_SC_ARGS" : "PT_LINUX_GET_SC_ARGS", p->p_pid); 1295 if (((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) == 0 && 1296 td2->td_sa.code == 0) 1297 #ifdef COMPAT_FREEBSD32 1298 || (wrap32 && !safe) 1299 #endif 1300 ) { 1301 error = EINVAL; 1302 break; 1303 } 1304 if (req == PT_GET_SC_ARGS) { 1305 bzero(addr, sizeof(td2->td_sa.args)); 1306 bcopy(td2->td_sa.args, addr, td2->td_sa.callp->sy_narg * 1307 sizeof(syscallarg_t)); 1308 } else { 1309 /* 1310 * Emulate a Linux bug which which strace(1) depends on: 1311 * at initialization it tests whether ptrace works by 1312 * calling close(2), or some other single-argument 1313 * syscall, _with six arguments_, and then verifies 1314 * whether it can fetch them all using this API; 1315 * otherwise it bails out. 1316 */ 1317 bcopy(td2->td_sa.args, addr, 6 * sizeof(syscallarg_t)); 1318 } 1319 break; 1320 1321 case PT_GET_SC_RET: 1322 if ((td2->td_dbgflags & (TDB_SCX)) == 0 1323 #ifdef COMPAT_FREEBSD32 1324 || (wrap32 && !safe) 1325 #endif 1326 ) { 1327 error = EINVAL; 1328 break; 1329 } 1330 psr = addr; 1331 bzero(psr, sizeof(*psr)); 1332 psr->sr_error = td2->td_errno; 1333 if (psr->sr_error == 0) { 1334 psr->sr_retval[0] = td2->td_retval[0]; 1335 psr->sr_retval[1] = td2->td_retval[1]; 1336 } 1337 CTR4(KTR_PTRACE, 1338 "PT_GET_SC_RET: pid %d error %d retval %#lx,%#lx", 1339 p->p_pid, psr->sr_error, psr->sr_retval[0], 1340 psr->sr_retval[1]); 1341 break; 1342 1343 case PT_STEP: 1344 case PT_CONTINUE: 1345 case PT_TO_SCE: 1346 case PT_TO_SCX: 1347 case PT_SYSCALL: 1348 case PT_DETACH: 1349 /* Zero means do not send any signal */ 1350 if (data < 0 || data > _SIG_MAXSIG) { 1351 error = EINVAL; 1352 break; 1353 } 1354 1355 switch (req) { 1356 case PT_STEP: 1357 CTR3(KTR_PTRACE, "PT_STEP: tid %d (pid %d), sig = %d", 1358 td2->td_tid, p->p_pid, data); 1359 error = ptrace_single_step(td2); 1360 if (error != 0) 1361 goto out; 1362 break; 1363 case PT_CONTINUE: 1364 case PT_TO_SCE: 1365 case PT_TO_SCX: 1366 case PT_SYSCALL: 1367 if (addr != (void *)1) { 1368 error = ptrace_set_pc(td2, 1369 (u_long)(uintfptr_t)addr); 1370 if (error != 0) 1371 goto out; 1372 td2->td_dbgflags |= TDB_USERWR; 1373 } 1374 switch (req) { 1375 case PT_TO_SCE: 1376 p->p_ptevents |= PTRACE_SCE; 1377 CTR4(KTR_PTRACE, 1378 "PT_TO_SCE: pid %d, events = %#x, PC = %#lx, sig = %d", 1379 p->p_pid, p->p_ptevents, 1380 (u_long)(uintfptr_t)addr, data); 1381 break; 1382 case PT_TO_SCX: 1383 p->p_ptevents |= PTRACE_SCX; 1384 CTR4(KTR_PTRACE, 1385 "PT_TO_SCX: pid %d, events = %#x, PC = %#lx, sig = %d", 1386 p->p_pid, p->p_ptevents, 1387 (u_long)(uintfptr_t)addr, data); 1388 break; 1389 case PT_SYSCALL: 1390 p->p_ptevents |= PTRACE_SYSCALL; 1391 CTR4(KTR_PTRACE, 1392 "PT_SYSCALL: pid %d, events = %#x, PC = %#lx, sig = %d", 1393 p->p_pid, p->p_ptevents, 1394 (u_long)(uintfptr_t)addr, data); 1395 break; 1396 case PT_CONTINUE: 1397 CTR3(KTR_PTRACE, 1398 "PT_CONTINUE: pid %d, PC = %#lx, sig = %d", 1399 p->p_pid, (u_long)(uintfptr_t)addr, data); 1400 break; 1401 } 1402 break; 1403 case PT_DETACH: 1404 /* 1405 * Clear P_TRACED before reparenting 1406 * a detached process back to its original 1407 * parent. Otherwise the debugee will be set 1408 * as an orphan of the debugger. 1409 */ 1410 p->p_flag &= ~(P_TRACED | P_WAITED); 1411 1412 /* 1413 * Reset the process parent. 1414 */ 1415 if (p->p_oppid != p->p_pptr->p_pid) { 1416 PROC_LOCK(p->p_pptr); 1417 sigqueue_take(p->p_ksi); 1418 PROC_UNLOCK(p->p_pptr); 1419 1420 pp = proc_realparent(p); 1421 proc_reparent(p, pp, false); 1422 if (pp == initproc) 1423 p->p_sigparent = SIGCHLD; 1424 CTR3(KTR_PTRACE, 1425 "PT_DETACH: pid %d reparented to pid %d, sig %d", 1426 p->p_pid, pp->p_pid, data); 1427 } else { 1428 CTR2(KTR_PTRACE, "PT_DETACH: pid %d, sig %d", 1429 p->p_pid, data); 1430 } 1431 1432 p->p_ptevents = 0; 1433 FOREACH_THREAD_IN_PROC(p, td3) { 1434 if ((td3->td_dbgflags & TDB_FSTP) != 0) { 1435 sigqueue_delete(&td3->td_sigqueue, 1436 SIGSTOP); 1437 } 1438 td3->td_dbgflags &= ~(TDB_XSIG | TDB_FSTP | 1439 TDB_SUSPEND | TDB_BORN); 1440 } 1441 1442 if ((p->p_flag2 & P2_PTRACE_FSTP) != 0) { 1443 sigqueue_delete(&p->p_sigqueue, SIGSTOP); 1444 p->p_flag2 &= ~P2_PTRACE_FSTP; 1445 } 1446 1447 /* 1448 * Send SIGCHLD and wakeup the parent as needed. It 1449 * may be the case that they had stopped the child 1450 * before it got ptraced, and now they're in the middle 1451 * of a wait(2) for it to continue. 1452 */ 1453 PROC_LOCK(p->p_pptr); 1454 childproc_continued(p); 1455 PROC_UNLOCK(p->p_pptr); 1456 break; 1457 } 1458 1459 sx_xunlock(&proctree_lock); 1460 proctree_locked = false; 1461 1462 sendsig: 1463 MPASS(!proctree_locked); 1464 1465 /* 1466 * Clear the pending event for the thread that just 1467 * reported its event (p_xthread), if any. This may 1468 * not be the thread passed to PT_CONTINUE, PT_STEP, 1469 * etc. if the debugger is resuming a different 1470 * thread. There might be no reporting thread if 1471 * the process was just attached. 1472 * 1473 * Deliver any pending signal via the reporting thread. 1474 */ 1475 if (p->p_xthread != NULL) { 1476 p->p_xthread->td_dbgflags &= ~TDB_XSIG; 1477 p->p_xthread->td_xsig = data; 1478 p->p_xthread = NULL; 1479 } 1480 p->p_xsig = data; 1481 1482 /* 1483 * P_WKILLED is insurance that a PT_KILL/SIGKILL 1484 * always works immediately, even if another thread is 1485 * unsuspended first and attempts to handle a 1486 * different signal or if the POSIX.1b style signal 1487 * queue cannot accommodate any new signals. 1488 */ 1489 if (data == SIGKILL) 1490 proc_wkilled(p); 1491 1492 /* 1493 * If the PT_CONTINUE-like operation is attempted on 1494 * the thread on sleepq, this is possible only after 1495 * the transparent PT_ATTACH. In this case, if the 1496 * caller modified the thread state, e.g. by writing 1497 * register file or specifying the pc, make the thread 1498 * xstopped by waking it up. 1499 */ 1500 if ((td2->td_dbgflags & TDB_USERWR) != 0 && 1501 pt_attach_transparent) { 1502 thread_lock(td2); 1503 if (TD_ON_SLEEPQ(td2) && 1504 (td2->td_flags & TDF_SINTR) != 0) { 1505 td2->td_dbgflags &= ~TDB_USERWR; 1506 sleepq_abort(td2, EINTR); 1507 } else { 1508 thread_unlock(td2); 1509 } 1510 } 1511 1512 /* 1513 * Unsuspend all threads. To leave a thread 1514 * suspended, use PT_SUSPEND to suspend it before 1515 * continuing the process. 1516 */ 1517 ptrace_unsuspend(p); 1518 break; 1519 1520 case PT_WRITE_I: 1521 case PT_WRITE_D: 1522 td2->td_dbgflags |= TDB_USERWR; 1523 PROC_UNLOCK(p); 1524 error = 0; 1525 if (proc_writemem(td, p, (off_t)(uintptr_t)addr, &data, 1526 sizeof(int)) != sizeof(int)) 1527 error = ENOMEM; 1528 else 1529 CTR3(KTR_PTRACE, "PT_WRITE: pid %d: %p <= %#x", 1530 p->p_pid, addr, data); 1531 PROC_LOCK(p); 1532 break; 1533 1534 case PT_READ_I: 1535 case PT_READ_D: 1536 PROC_UNLOCK(p); 1537 error = tmp = 0; 1538 if (proc_readmem(td, p, (off_t)(uintptr_t)addr, &tmp, 1539 sizeof(int)) != sizeof(int)) 1540 error = ENOMEM; 1541 else 1542 CTR3(KTR_PTRACE, "PT_READ: pid %d: %p >= %#x", 1543 p->p_pid, addr, tmp); 1544 td->td_retval[0] = tmp; 1545 PROC_LOCK(p); 1546 break; 1547 1548 case PT_IO: 1549 piod = addr; 1550 if (piod->piod_len > SSIZE_MAX) { 1551 error = EINVAL; 1552 goto out; 1553 } 1554 iov.iov_base = piod->piod_addr; 1555 iov.iov_len = piod->piod_len; 1556 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs; 1557 uio.uio_resid = piod->piod_len; 1558 uio.uio_iov = &iov; 1559 uio.uio_iovcnt = 1; 1560 uio.uio_segflg = UIO_USERSPACE; 1561 uio.uio_td = td; 1562 switch (piod->piod_op) { 1563 case PIOD_READ_D: 1564 case PIOD_READ_I: 1565 CTR3(KTR_PTRACE, "PT_IO: pid %d: READ (%p, %#x)", 1566 p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid); 1567 uio.uio_rw = UIO_READ; 1568 break; 1569 case PIOD_WRITE_D: 1570 case PIOD_WRITE_I: 1571 CTR3(KTR_PTRACE, "PT_IO: pid %d: WRITE (%p, %#x)", 1572 p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid); 1573 td2->td_dbgflags |= TDB_USERWR; 1574 uio.uio_rw = UIO_WRITE; 1575 break; 1576 default: 1577 error = EINVAL; 1578 goto out; 1579 } 1580 PROC_UNLOCK(p); 1581 error = proc_rwmem(p, &uio, 0); 1582 piod->piod_len -= uio.uio_resid; 1583 PROC_LOCK(p); 1584 break; 1585 1586 case PT_KILL: 1587 CTR1(KTR_PTRACE, "PT_KILL: pid %d", p->p_pid); 1588 data = SIGKILL; 1589 goto sendsig; /* in PT_CONTINUE above */ 1590 1591 case PT_SETREGS: 1592 CTR2(KTR_PTRACE, "PT_SETREGS: tid %d (pid %d)", td2->td_tid, 1593 p->p_pid); 1594 td2->td_dbgflags |= TDB_USERWR; 1595 error = PROC_WRITE(regs, td2, addr); 1596 break; 1597 1598 case PT_GETREGS: 1599 CTR2(KTR_PTRACE, "PT_GETREGS: tid %d (pid %d)", td2->td_tid, 1600 p->p_pid); 1601 error = PROC_READ(regs, td2, addr); 1602 break; 1603 1604 case PT_SETFPREGS: 1605 CTR2(KTR_PTRACE, "PT_SETFPREGS: tid %d (pid %d)", td2->td_tid, 1606 p->p_pid); 1607 td2->td_dbgflags |= TDB_USERWR; 1608 error = PROC_WRITE(fpregs, td2, addr); 1609 break; 1610 1611 case PT_GETFPREGS: 1612 CTR2(KTR_PTRACE, "PT_GETFPREGS: tid %d (pid %d)", td2->td_tid, 1613 p->p_pid); 1614 error = PROC_READ(fpregs, td2, addr); 1615 break; 1616 1617 case PT_SETDBREGS: 1618 CTR2(KTR_PTRACE, "PT_SETDBREGS: tid %d (pid %d)", td2->td_tid, 1619 p->p_pid); 1620 td2->td_dbgflags |= TDB_USERWR; 1621 error = PROC_WRITE(dbregs, td2, addr); 1622 break; 1623 1624 case PT_GETDBREGS: 1625 CTR2(KTR_PTRACE, "PT_GETDBREGS: tid %d (pid %d)", td2->td_tid, 1626 p->p_pid); 1627 error = PROC_READ(dbregs, td2, addr); 1628 break; 1629 1630 case PT_SETREGSET: 1631 CTR2(KTR_PTRACE, "PT_SETREGSET: tid %d (pid %d)", td2->td_tid, 1632 p->p_pid); 1633 error = proc_write_regset(td2, data, addr); 1634 break; 1635 1636 case PT_GETREGSET: 1637 CTR2(KTR_PTRACE, "PT_GETREGSET: tid %d (pid %d)", td2->td_tid, 1638 p->p_pid); 1639 error = proc_read_regset(td2, data, addr); 1640 break; 1641 1642 case PT_LWPINFO: 1643 if (data <= 0 || data > sizeof(*pl)) { 1644 error = EINVAL; 1645 break; 1646 } 1647 pl = addr; 1648 bzero(pl, sizeof(*pl)); 1649 pl->pl_lwpid = td2->td_tid; 1650 pl->pl_event = PL_EVENT_NONE; 1651 pl->pl_flags = 0; 1652 if (td2->td_dbgflags & TDB_XSIG) { 1653 pl->pl_event = PL_EVENT_SIGNAL; 1654 if (td2->td_si.si_signo != 0 && 1655 data >= offsetof(struct ptrace_lwpinfo, pl_siginfo) 1656 + sizeof(pl->pl_siginfo)){ 1657 pl->pl_flags |= PL_FLAG_SI; 1658 pl->pl_siginfo = td2->td_si; 1659 } 1660 } 1661 if (td2->td_dbgflags & TDB_SCE) 1662 pl->pl_flags |= PL_FLAG_SCE; 1663 else if (td2->td_dbgflags & TDB_SCX) 1664 pl->pl_flags |= PL_FLAG_SCX; 1665 if (td2->td_dbgflags & TDB_EXEC) 1666 pl->pl_flags |= PL_FLAG_EXEC; 1667 if (td2->td_dbgflags & TDB_FORK) { 1668 pl->pl_flags |= PL_FLAG_FORKED; 1669 pl->pl_child_pid = td2->td_dbg_forked; 1670 if (td2->td_dbgflags & TDB_VFORK) 1671 pl->pl_flags |= PL_FLAG_VFORKED; 1672 } else if ((td2->td_dbgflags & (TDB_SCX | TDB_VFORK)) == 1673 TDB_VFORK) 1674 pl->pl_flags |= PL_FLAG_VFORK_DONE; 1675 if (td2->td_dbgflags & TDB_CHILD) 1676 pl->pl_flags |= PL_FLAG_CHILD; 1677 if (td2->td_dbgflags & TDB_BORN) 1678 pl->pl_flags |= PL_FLAG_BORN; 1679 if (td2->td_dbgflags & TDB_EXIT) 1680 pl->pl_flags |= PL_FLAG_EXITED; 1681 pl->pl_sigmask = td2->td_sigmask; 1682 pl->pl_siglist = td2->td_siglist; 1683 strcpy(pl->pl_tdname, td2->td_name); 1684 if (td2->td_sa.code != 0) { 1685 pl->pl_syscall_code = td2->td_sa.code; 1686 pl->pl_syscall_narg = td2->td_sa.callp->sy_narg; 1687 } 1688 CTR6(KTR_PTRACE, 1689 "PT_LWPINFO: tid %d (pid %d) event %d flags %#x child pid %d syscall %d", 1690 td2->td_tid, p->p_pid, pl->pl_event, pl->pl_flags, 1691 pl->pl_child_pid, pl->pl_syscall_code); 1692 break; 1693 1694 case PT_GETNUMLWPS: 1695 CTR2(KTR_PTRACE, "PT_GETNUMLWPS: pid %d: %d threads", p->p_pid, 1696 p->p_numthreads); 1697 td->td_retval[0] = p->p_numthreads; 1698 break; 1699 1700 case PT_GETLWPLIST: 1701 CTR3(KTR_PTRACE, "PT_GETLWPLIST: pid %d: data %d, actual %d", 1702 p->p_pid, data, p->p_numthreads); 1703 if (data <= 0) { 1704 error = EINVAL; 1705 break; 1706 } 1707 num = imin(p->p_numthreads, data); 1708 PROC_UNLOCK(p); 1709 buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK); 1710 tmp = 0; 1711 PROC_LOCK(p); 1712 FOREACH_THREAD_IN_PROC(p, td2) { 1713 if (tmp >= num) 1714 break; 1715 buf[tmp++] = td2->td_tid; 1716 } 1717 PROC_UNLOCK(p); 1718 error = copyout(buf, addr, tmp * sizeof(lwpid_t)); 1719 free(buf, M_TEMP); 1720 if (!error) 1721 td->td_retval[0] = tmp; 1722 PROC_LOCK(p); 1723 break; 1724 1725 case PT_VM_TIMESTAMP: 1726 CTR2(KTR_PTRACE, "PT_VM_TIMESTAMP: pid %d: timestamp %d", 1727 p->p_pid, p->p_vmspace->vm_map.timestamp); 1728 td->td_retval[0] = p->p_vmspace->vm_map.timestamp; 1729 break; 1730 1731 case PT_VM_ENTRY: 1732 PROC_UNLOCK(p); 1733 error = ptrace_vm_entry(td, p, addr); 1734 PROC_LOCK(p); 1735 break; 1736 1737 case PT_COREDUMP: 1738 pc = addr; 1739 CTR2(KTR_PTRACE, "PT_COREDUMP: pid %d, fd %d", 1740 p->p_pid, pc->pc_fd); 1741 1742 if ((pc->pc_flags & ~(PC_COMPRESS | PC_ALL)) != 0) { 1743 error = EINVAL; 1744 break; 1745 } 1746 PROC_UNLOCK(p); 1747 1748 tcq = malloc(sizeof(*tcq), M_TEMP, M_WAITOK | M_ZERO); 1749 fp = NULL; 1750 error = fget_write(td, pc->pc_fd, &cap_write_rights, &fp); 1751 if (error != 0) 1752 goto coredump_cleanup_nofp; 1753 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VREG) { 1754 error = EPIPE; 1755 goto coredump_cleanup; 1756 } 1757 1758 PROC_LOCK(p); 1759 error = proc_can_ptrace(td, p); 1760 if (error != 0) 1761 goto coredump_cleanup_locked; 1762 1763 td2 = ptrace_sel_coredump_thread(p); 1764 if (td2 == NULL) { 1765 error = EBUSY; 1766 goto coredump_cleanup_locked; 1767 } 1768 KASSERT((td2->td_dbgflags & (TDB_COREDUMPREQ | 1769 TDB_SCREMOTEREQ)) == 0, 1770 ("proc %d tid %d req coredump", p->p_pid, td2->td_tid)); 1771 1772 tcq->tc_vp = fp->f_vnode; 1773 tcq->tc_limit = pc->pc_limit == 0 ? OFF_MAX : pc->pc_limit; 1774 tcq->tc_flags = SVC_PT_COREDUMP; 1775 if ((pc->pc_flags & PC_COMPRESS) == 0) 1776 tcq->tc_flags |= SVC_NOCOMPRESS; 1777 if ((pc->pc_flags & PC_ALL) != 0) 1778 tcq->tc_flags |= SVC_ALL; 1779 td2->td_remotereq = tcq; 1780 td2->td_dbgflags |= TDB_COREDUMPREQ; 1781 thread_run_flash(td2); 1782 while ((td2->td_dbgflags & TDB_COREDUMPREQ) != 0) 1783 msleep(p, &p->p_mtx, PPAUSE, "crdmp", 0); 1784 error = tcq->tc_error; 1785 coredump_cleanup_locked: 1786 PROC_UNLOCK(p); 1787 coredump_cleanup: 1788 fdrop(fp, td); 1789 coredump_cleanup_nofp: 1790 free(tcq, M_TEMP); 1791 PROC_LOCK(p); 1792 break; 1793 1794 case PT_SC_REMOTE: 1795 pscr = addr; 1796 CTR2(KTR_PTRACE, "PT_SC_REMOTE: pid %d, syscall %d", 1797 p->p_pid, pscr->pscr_syscall); 1798 if ((td2->td_dbgflags & TDB_BOUNDARY) == 0) { 1799 error = EBUSY; 1800 break; 1801 } 1802 PROC_UNLOCK(p); 1803 MPASS(pscr->pscr_nargs <= nitems(td->td_sa.args)); 1804 1805 tsr = malloc(sizeof(struct thr_syscall_req), M_TEMP, 1806 M_WAITOK | M_ZERO); 1807 1808 tsr->ts_sa.code = pscr->pscr_syscall; 1809 tsr->ts_nargs = pscr->pscr_nargs; 1810 memcpy(&tsr->ts_sa.args, pscr->pscr_args, 1811 sizeof(syscallarg_t) * tsr->ts_nargs); 1812 1813 PROC_LOCK(p); 1814 error = proc_can_ptrace(td, p); 1815 if (error != 0) { 1816 free(tsr, M_TEMP); 1817 break; 1818 } 1819 if (td2->td_proc != p) { 1820 free(tsr, M_TEMP); 1821 error = ESRCH; 1822 break; 1823 } 1824 KASSERT((td2->td_dbgflags & (TDB_COREDUMPREQ | 1825 TDB_SCREMOTEREQ)) == 0, 1826 ("proc %d tid %d req coredump", p->p_pid, td2->td_tid)); 1827 1828 td2->td_remotereq = tsr; 1829 td2->td_dbgflags |= TDB_SCREMOTEREQ; 1830 thread_run_flash(td2); 1831 while ((td2->td_dbgflags & TDB_SCREMOTEREQ) != 0) 1832 msleep(p, &p->p_mtx, PPAUSE, "pscrx", 0); 1833 error = 0; 1834 memcpy(&pscr->pscr_ret, &tsr->ts_ret, sizeof(tsr->ts_ret)); 1835 free(tsr, M_TEMP); 1836 break; 1837 1838 default: 1839 #ifdef __HAVE_PTRACE_MACHDEP 1840 if (req >= PT_FIRSTMACH) { 1841 PROC_UNLOCK(p); 1842 error = cpu_ptrace(td2, req, addr, data); 1843 PROC_LOCK(p); 1844 } else 1845 #endif 1846 /* Unknown request. */ 1847 error = EINVAL; 1848 break; 1849 } 1850 out: 1851 /* Drop our hold on this process now that the request has completed. */ 1852 _PRELE(p); 1853 fail: 1854 if (p2_req_set) { 1855 if ((p->p_flag2 & P2_PTRACEREQ) != 0) 1856 wakeup(&p->p_flag2); 1857 p->p_flag2 &= ~P2_PTRACEREQ; 1858 } 1859 PROC_UNLOCK(p); 1860 if (proctree_locked) 1861 sx_xunlock(&proctree_lock); 1862 return (error); 1863 } 1864 #undef PROC_READ 1865 #undef PROC_WRITE 1866