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