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