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