1 /* $OpenBSD: sys_process.c,v 1.78 2017/10/14 10:17:08 guenther Exp $ */ 2 /* $NetBSD: sys_process.c,v 1.55 1996/05/15 06:17:47 tls Exp $ */ 3 4 /*- 5 * Copyright (c) 1994 Christopher G. Demetriou. All rights reserved. 6 * Copyright (c) 1982, 1986, 1989, 1993 7 * The Regents of the University of California. All rights reserved. 8 * (c) UNIX System Laboratories, Inc. 9 * All or some portions of this file are derived from material licensed 10 * to the University of California by American Telephone and Telegraph 11 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 12 * the permission of UNIX System Laboratories, Inc. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93 39 */ 40 41 /* 42 * References: 43 * (1) Bach's "The Design of the UNIX Operating System", 44 * (2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution, 45 * (3) the "4.4BSD Programmer's Reference Manual" published 46 * by USENIX and O'Reilly & Associates. 47 * The 4.4BSD PRM does a reasonably good job of documenting what the various 48 * ptrace() requests should actually do, and its text is quoted several times 49 * in this file. 50 */ 51 52 #include <sys/param.h> 53 #include <sys/systm.h> 54 #include <sys/exec.h> 55 #include <sys/proc.h> 56 #include <sys/signalvar.h> 57 #include <sys/errno.h> 58 #include <sys/malloc.h> 59 #include <sys/ptrace.h> 60 #include <sys/uio.h> 61 #include <sys/sched.h> 62 63 #include <sys/mount.h> 64 #include <sys/syscallargs.h> 65 66 #include <uvm/uvm_extern.h> 67 68 #include <machine/reg.h> 69 70 #ifdef PTRACE 71 72 static inline int process_checktracestate(struct process *_curpr, 73 struct process *_tr, struct proc *_t); 74 static inline struct process *process_tprfind(pid_t _tpid, struct proc **_tp); 75 76 int ptrace_ctrl(struct proc *, int, pid_t, caddr_t, int); 77 int ptrace_ustate(struct proc *, int, pid_t, void *, int, register_t *); 78 int ptrace_kstate(struct proc *, int, pid_t, void *); 79 int process_auxv_offset(struct proc *, struct process *, struct uio *); 80 81 int global_ptrace; /* permit tracing of not children */ 82 83 84 /* 85 * Process debugging system call. 86 */ 87 int 88 sys_ptrace(struct proc *p, void *v, register_t *retval) 89 { 90 struct sys_ptrace_args /* { 91 syscallarg(int) req; 92 syscallarg(pid_t) pid; 93 syscallarg(caddr_t) addr; 94 syscallarg(int) data; 95 } */ *uap = v; 96 int req = SCARG(uap, req); 97 pid_t pid = SCARG(uap, pid); 98 caddr_t uaddr = SCARG(uap, addr); /* userspace */ 99 void *kaddr = NULL; /* kernelspace */ 100 int data = SCARG(uap, data); 101 union { 102 struct ptrace_thread_state u_pts; 103 struct ptrace_io_desc u_piod; 104 struct ptrace_event u_pe; 105 struct ptrace_state u_ps; 106 register_t u_wcookie; 107 } u; 108 int size = 0; 109 enum { NONE, IN, IN_ALLOC, OUT, OUT_ALLOC, IN_OUT } mode; 110 int kstate = 0; 111 int error; 112 113 *retval = 0; 114 115 /* Figure out what sort of copyin/out operations we'll do */ 116 switch (req) { 117 case PT_TRACE_ME: 118 case PT_CONTINUE: 119 case PT_KILL: 120 case PT_ATTACH: 121 case PT_DETACH: 122 #ifdef PT_STEP 123 case PT_STEP: 124 #endif 125 /* control operations do no copyin/out; dispatch directly */ 126 return ptrace_ctrl(p, req, pid, uaddr, data); 127 128 case PT_READ_I: 129 case PT_READ_D: 130 case PT_WRITE_I: 131 case PT_WRITE_D: 132 mode = NONE; 133 break; 134 case PT_IO: 135 mode = IN_OUT; 136 size = sizeof u.u_piod; 137 data = size; /* suppress the data == size check */ 138 break; 139 case PT_GET_THREAD_FIRST: 140 mode = OUT; 141 size = sizeof u.u_pts; 142 kstate = 1; 143 break; 144 case PT_GET_THREAD_NEXT: 145 mode = IN_OUT; 146 size = sizeof u.u_pts; 147 kstate = 1; 148 break; 149 case PT_GET_EVENT_MASK: 150 mode = OUT; 151 size = sizeof u.u_pe; 152 kstate = 1; 153 break; 154 case PT_SET_EVENT_MASK: 155 mode = IN; 156 size = sizeof u.u_pe; 157 kstate = 1; 158 break; 159 case PT_GET_PROCESS_STATE: 160 mode = OUT; 161 size = sizeof u.u_ps; 162 kstate = 1; 163 break; 164 case PT_GETREGS: 165 mode = OUT_ALLOC; 166 size = sizeof(struct reg); 167 break; 168 case PT_SETREGS: 169 mode = IN_ALLOC; 170 size = sizeof(struct reg); 171 break; 172 #ifdef PT_GETFPREGS 173 case PT_GETFPREGS: 174 mode = OUT_ALLOC; 175 size = sizeof(struct fpreg); 176 break; 177 #endif 178 #ifdef PT_SETFPREGS 179 case PT_SETFPREGS: 180 mode = IN_ALLOC; 181 size = sizeof(struct fpreg); 182 break; 183 #endif 184 #ifdef PT_GETXMMREGS 185 case PT_GETXMMREGS: 186 mode = OUT_ALLOC; 187 size = sizeof(struct xmmregs); 188 break; 189 #endif 190 #ifdef PT_SETXMMREGS 191 case PT_SETXMMREGS: 192 mode = IN_ALLOC; 193 size = sizeof(struct xmmregs); 194 break; 195 #endif 196 #ifdef PT_WCOOKIE 197 case PT_WCOOKIE: 198 mode = OUT; 199 size = sizeof u.u_wcookie; 200 data = size; /* suppress the data == size check */ 201 break; 202 #endif 203 default: 204 return EINVAL; 205 } 206 207 208 /* Now do any copyin()s and allocations in a consistent manner */ 209 switch (mode) { 210 case NONE: 211 kaddr = uaddr; 212 break; 213 case IN: 214 case IN_OUT: 215 case OUT: 216 KASSERT(size <= sizeof u); 217 if (data != size) 218 return EINVAL; 219 if (mode == OUT) 220 memset(&u, 0, size); 221 else { /* IN or IN_OUT */ 222 if ((error = copyin(uaddr, &u, size))) 223 return error; 224 } 225 kaddr = &u; 226 break; 227 case IN_ALLOC: 228 kaddr = malloc(size, M_TEMP, M_WAITOK); 229 if ((error = copyin(uaddr, kaddr, size))) { 230 free(kaddr, M_TEMP, size); 231 return error; 232 } 233 break; 234 case OUT_ALLOC: 235 kaddr = malloc(size, M_TEMP, M_WAITOK | M_ZERO); 236 break; 237 } 238 239 if (kstate) 240 error = ptrace_kstate(p, req, pid, kaddr); 241 else 242 error = ptrace_ustate(p, req, pid, kaddr, data, retval); 243 244 /* Do any copyout()s and frees */ 245 if (error == 0) { 246 switch (mode) { 247 case NONE: 248 case IN: 249 case IN_ALLOC: 250 break; 251 case IN_OUT: 252 case OUT: 253 error = copyout(&u, uaddr, size); 254 if (req == PT_IO) { 255 /* historically, errors here are ignored */ 256 error = 0; 257 } 258 break; 259 case OUT_ALLOC: 260 error = copyout(kaddr, uaddr, size); 261 break; 262 } 263 } 264 265 if (mode == IN_ALLOC || mode == OUT_ALLOC) 266 free(kaddr, M_TEMP, size); 267 return error; 268 } 269 270 /* 271 * ptrace control requests: attach, detach, continue, kill, single-step, etc 272 */ 273 int 274 ptrace_ctrl(struct proc *p, int req, pid_t pid, caddr_t addr, int data) 275 { 276 struct proc *t; /* target thread */ 277 struct process *tr; /* target process */ 278 int error = 0; 279 int s; 280 281 switch (req) { 282 case PT_TRACE_ME: 283 /* Just set the trace flag. */ 284 tr = p->p_p; 285 atomic_setbits_int(&tr->ps_flags, PS_TRACED); 286 tr->ps_oppid = tr->ps_pptr->ps_pid; 287 if (tr->ps_ptstat == NULL) 288 tr->ps_ptstat = malloc(sizeof(*tr->ps_ptstat), 289 M_SUBPROC, M_WAITOK); 290 memset(tr->ps_ptstat, 0, sizeof(*tr->ps_ptstat)); 291 return 0; 292 293 /* calls that only operate on the PID */ 294 case PT_KILL: 295 case PT_ATTACH: 296 case PT_DETACH: 297 /* Find the process we're supposed to be operating on. */ 298 if ((tr = prfind(pid)) == NULL) 299 return (ESRCH); 300 t = TAILQ_FIRST(&tr->ps_threads); 301 break; 302 303 /* calls that accept a PID or a thread ID */ 304 case PT_CONTINUE: 305 #ifdef PT_STEP 306 case PT_STEP: 307 #endif 308 if ((tr = process_tprfind(pid, &t)) == NULL) 309 return ESRCH; 310 break; 311 } 312 313 /* Check permissions/state */ 314 if (req != PT_ATTACH) { 315 /* Check that the data is a valid signal number or zero. */ 316 if (req != PT_KILL && (data < 0 || data >= NSIG)) 317 return EINVAL; 318 319 /* Most operations require the target to already be traced */ 320 if ((error = process_checktracestate(p->p_p, tr, t))) 321 return error; 322 323 /* Do single-step fixup if needed. */ 324 FIX_SSTEP(t); 325 } else { 326 /* 327 * PT_ATTACH is the opposite; you can't attach to a process if: 328 * (1) it's the process that's doing the attaching, 329 */ 330 if (tr == p->p_p) 331 return (EINVAL); 332 333 /* 334 * (2) it's a system process 335 */ 336 if (ISSET(tr->ps_flags, PS_SYSTEM)) 337 return (EPERM); 338 339 /* 340 * (3) it's already being traced, or 341 */ 342 if (ISSET(tr->ps_flags, PS_TRACED)) 343 return (EBUSY); 344 345 /* 346 * (4) it's in the middle of execve(2) 347 */ 348 if (ISSET(tr->ps_flags, PS_INEXEC)) 349 return (EAGAIN); 350 351 /* 352 * (5) it's not owned by you, or the last exec 353 * gave us setuid/setgid privs (unless 354 * you're root), or... 355 * 356 * [Note: once PS_SUGID or PS_SUGIDEXEC gets set in 357 * execve(), they stay set until the process does 358 * another execve(). Hence this prevents a setuid 359 * process which revokes its special privileges using 360 * setuid() from being traced. This is good security.] 361 */ 362 if ((tr->ps_ucred->cr_ruid != p->p_ucred->cr_ruid || 363 ISSET(tr->ps_flags, PS_SUGIDEXEC | PS_SUGID)) && 364 (error = suser(p, 0)) != 0) 365 return (error); 366 367 /* 368 * (5.5) it's not a child of the tracing process. 369 */ 370 if (global_ptrace == 0 && !inferior(tr, p->p_p) && 371 (error = suser(p, 0)) != 0) 372 return (error); 373 374 /* 375 * (6) ...it's init, which controls the security level 376 * of the entire system, and the system was not 377 * compiled with permanently insecure mode turned 378 * on. 379 */ 380 if ((tr->ps_pid == 1) && (securelevel > -1)) 381 return (EPERM); 382 383 /* 384 * (7) it's an ancestor of the current process and 385 * not init (because that would create a loop in 386 * the process graph). 387 */ 388 if (tr->ps_pid != 1 && inferior(p->p_p, tr)) 389 return (EINVAL); 390 } 391 392 switch (req) { 393 394 #ifdef PT_STEP 395 case PT_STEP: 396 /* 397 * From the 4.4BSD PRM: 398 * "Execution continues as in request PT_CONTINUE; however 399 * as soon as possible after execution of at least one 400 * instruction, execution stops again. [ ... ]" 401 */ 402 #endif 403 case PT_CONTINUE: 404 /* 405 * From the 4.4BSD PRM: 406 * "The data argument is taken as a signal number and the 407 * child's execution continues at location addr as if it 408 * incurred that signal. Normally the signal number will 409 * be either 0 to indicate that the signal that caused the 410 * stop should be ignored, or that value fetched out of 411 * the process's image indicating which signal caused 412 * the stop. If addr is (int *)1 then execution continues 413 * from where it stopped." 414 */ 415 416 if (pid < THREAD_PID_OFFSET && tr->ps_single) 417 t = tr->ps_single; 418 419 /* If the address parameter is not (int *)1, set the pc. */ 420 if ((int *)addr != (int *)1) 421 if ((error = process_set_pc(t, addr)) != 0) 422 return error; 423 424 #ifdef PT_STEP 425 /* 426 * Arrange for a single-step, if that's requested and possible. 427 */ 428 error = process_sstep(t, req == PT_STEP); 429 if (error) 430 return error; 431 #endif 432 goto sendsig; 433 434 case PT_DETACH: 435 /* 436 * From the 4.4BSD PRM: 437 * "The data argument is taken as a signal number and the 438 * child's execution continues at location addr as if it 439 * incurred that signal. Normally the signal number will 440 * be either 0 to indicate that the signal that caused the 441 * stop should be ignored, or that value fetched out of 442 * the process's image indicating which signal caused 443 * the stop. If addr is (int *)1 then execution continues 444 * from where it stopped." 445 */ 446 447 if (pid < THREAD_PID_OFFSET && tr->ps_single) 448 t = tr->ps_single; 449 450 #ifdef PT_STEP 451 /* 452 * Stop single stepping. 453 */ 454 error = process_sstep(t, 0); 455 if (error) 456 return error; 457 #endif 458 459 /* give process back to original parent or init */ 460 if (tr->ps_oppid != tr->ps_pptr->ps_pid) { 461 struct process *ppr; 462 463 ppr = prfind(tr->ps_oppid); 464 proc_reparent(tr, ppr ? ppr : initprocess); 465 } 466 467 /* not being traced any more */ 468 tr->ps_oppid = 0; 469 atomic_clearbits_int(&tr->ps_flags, PS_TRACED|PS_WAITED); 470 471 sendsig: 472 memset(tr->ps_ptstat, 0, sizeof(*tr->ps_ptstat)); 473 474 /* Finally, deliver the requested signal (or none). */ 475 if (t->p_stat == SSTOP) { 476 t->p_xstat = data; 477 SCHED_LOCK(s); 478 setrunnable(t); 479 SCHED_UNLOCK(s); 480 } else { 481 if (data != 0) 482 psignal(t, data); 483 } 484 break; 485 486 case PT_KILL: 487 if (pid < THREAD_PID_OFFSET && tr->ps_single) 488 t = tr->ps_single; 489 490 /* just send the process a KILL signal. */ 491 data = SIGKILL; 492 goto sendsig; /* in PT_CONTINUE, above. */ 493 494 case PT_ATTACH: 495 /* 496 * As was done in procfs: 497 * Go ahead and set the trace flag. 498 * Save the old parent (it's reset in 499 * _DETACH, and also in kern_exit.c:wait4() 500 * Reparent the process so that the tracing 501 * proc gets to see all the action. 502 * Stop the target. 503 */ 504 atomic_setbits_int(&tr->ps_flags, PS_TRACED); 505 tr->ps_oppid = tr->ps_pptr->ps_pid; 506 if (tr->ps_pptr != p->p_p) 507 proc_reparent(tr, p->p_p); 508 if (tr->ps_ptstat == NULL) 509 tr->ps_ptstat = malloc(sizeof(*tr->ps_ptstat), 510 M_SUBPROC, M_WAITOK); 511 data = SIGSTOP; 512 goto sendsig; 513 default: 514 KASSERTMSG(0, "%s: unhandled request %d", __func__, req); 515 break; 516 } 517 518 return error; 519 } 520 521 /* 522 * ptrace kernel-state requests: thread list, event mask, process state 523 */ 524 int 525 ptrace_kstate(struct proc *p, int req, pid_t pid, void *addr) 526 { 527 struct process *tr; /* target process */ 528 struct ptrace_event *pe = addr; 529 int error; 530 531 KASSERT((p->p_flag & P_SYSTEM) == 0); 532 533 /* Find the process we're supposed to be operating on. */ 534 if ((tr = prfind(pid)) == NULL) 535 return ESRCH; 536 537 if ((error = process_checktracestate(p->p_p, tr, NULL))) 538 return error; 539 540 switch (req) { 541 case PT_GET_THREAD_FIRST: 542 case PT_GET_THREAD_NEXT: 543 { 544 struct ptrace_thread_state *pts = addr; 545 struct proc *t; 546 547 if (req == PT_GET_THREAD_NEXT) { 548 t = tfind(pts->pts_tid - THREAD_PID_OFFSET); 549 if (t == NULL || ISSET(t->p_flag, P_WEXIT)) 550 return ESRCH; 551 if (t->p_p != tr) 552 return EINVAL; 553 t = TAILQ_NEXT(t, p_thr_link); 554 } else { 555 t = TAILQ_FIRST(&tr->ps_threads); 556 } 557 558 if (t == NULL) 559 pts->pts_tid = -1; 560 else 561 pts->pts_tid = t->p_tid + THREAD_PID_OFFSET; 562 return 0; 563 } 564 } 565 566 switch (req) { 567 case PT_GET_EVENT_MASK: 568 pe->pe_set_event = tr->ps_ptmask; 569 break; 570 case PT_SET_EVENT_MASK: 571 tr->ps_ptmask = pe->pe_set_event; 572 break; 573 case PT_GET_PROCESS_STATE: 574 if (tr->ps_single) 575 tr->ps_ptstat->pe_tid = 576 tr->ps_single->p_tid + THREAD_PID_OFFSET; 577 memcpy(addr, tr->ps_ptstat, sizeof *tr->ps_ptstat); 578 break; 579 default: 580 KASSERTMSG(0, "%s: unhandled request %d", __func__, req); 581 break; 582 } 583 584 return 0; 585 } 586 587 /* 588 * ptrace user-state requests: memory access, registers, stack cookie 589 */ 590 int 591 ptrace_ustate(struct proc *p, int req, pid_t pid, void *addr, int data, 592 register_t *retval) 593 { 594 struct proc *t; /* target thread */ 595 struct process *tr; /* target process */ 596 struct uio uio; 597 struct iovec iov; 598 int error, write; 599 int temp = 0; 600 601 KASSERT((p->p_flag & P_SYSTEM) == 0); 602 603 /* Accept either PID or TID */ 604 if ((tr = process_tprfind(pid, &t)) == NULL) 605 return ESRCH; 606 607 if ((error = process_checktracestate(p->p_p, tr, t))) 608 return error; 609 610 FIX_SSTEP(t); 611 612 /* Now do the operation. */ 613 write = 0; 614 615 if ((error = process_checkioperm(p, tr)) != 0) 616 return error; 617 618 switch (req) { 619 case PT_WRITE_I: /* XXX no separate I and D spaces */ 620 case PT_WRITE_D: 621 write = 1; 622 temp = data; 623 case PT_READ_I: /* XXX no separate I and D spaces */ 624 case PT_READ_D: 625 /* write = 0 done above. */ 626 iov.iov_base = (caddr_t)&temp; 627 iov.iov_len = sizeof(int); 628 uio.uio_iov = &iov; 629 uio.uio_iovcnt = 1; 630 uio.uio_offset = (off_t)(vaddr_t)addr; 631 uio.uio_resid = sizeof(int); 632 uio.uio_segflg = UIO_SYSSPACE; 633 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 634 uio.uio_procp = p; 635 error = process_domem(p, tr, &uio, write ? PT_WRITE_I : 636 PT_READ_I); 637 if (write == 0) 638 *retval = temp; 639 return error; 640 641 case PT_IO: 642 { 643 struct ptrace_io_desc *piod = addr; 644 645 iov.iov_base = piod->piod_addr; 646 iov.iov_len = piod->piod_len; 647 uio.uio_iov = &iov; 648 uio.uio_iovcnt = 1; 649 uio.uio_offset = (off_t)(vaddr_t)piod->piod_offs; 650 uio.uio_resid = piod->piod_len; 651 uio.uio_segflg = UIO_USERSPACE; 652 uio.uio_procp = p; 653 switch (piod->piod_op) { 654 case PIOD_READ_I: 655 req = PT_READ_I; 656 uio.uio_rw = UIO_READ; 657 break; 658 case PIOD_READ_D: 659 req = PT_READ_D; 660 uio.uio_rw = UIO_READ; 661 break; 662 case PIOD_WRITE_I: 663 req = PT_WRITE_I; 664 uio.uio_rw = UIO_WRITE; 665 break; 666 case PIOD_WRITE_D: 667 req = PT_WRITE_D; 668 uio.uio_rw = UIO_WRITE; 669 break; 670 case PIOD_READ_AUXV: 671 req = PT_READ_D; 672 uio.uio_rw = UIO_READ; 673 temp = tr->ps_emul->e_arglen * sizeof(char *); 674 if (uio.uio_offset > temp) 675 return EIO; 676 if (uio.uio_resid > temp - uio.uio_offset) 677 uio.uio_resid = temp - uio.uio_offset; 678 piod->piod_len = iov.iov_len = uio.uio_resid; 679 error = process_auxv_offset(p, tr, &uio); 680 if (error) 681 return error; 682 break; 683 default: 684 return EINVAL; 685 } 686 error = process_domem(p, tr, &uio, req); 687 piod->piod_len -= uio.uio_resid; 688 return error; 689 } 690 691 case PT_SETREGS: 692 return process_write_regs(t, addr); 693 case PT_GETREGS: 694 return process_read_regs(t, addr); 695 696 #ifdef PT_SETFPREGS 697 case PT_SETFPREGS: 698 return process_write_fpregs(t, addr); 699 #endif 700 #ifdef PT_SETFPREGS 701 case PT_GETFPREGS: 702 return process_read_fpregs(t, addr); 703 #endif 704 #ifdef PT_SETXMMREGS 705 case PT_SETXMMREGS: 706 return process_write_xmmregs(t, addr); 707 #endif 708 #ifdef PT_SETXMMREGS 709 case PT_GETXMMREGS: 710 return process_read_xmmregs(t, addr); 711 #endif 712 #ifdef PT_WCOOKIE 713 case PT_WCOOKIE: 714 *(register_t *)addr = process_get_wcookie(t); 715 return 0; 716 #endif 717 default: 718 KASSERTMSG(0, "%s: unhandled request %d", __func__, req); 719 break; 720 } 721 722 return 0; 723 } 724 725 726 /* 727 * Helper for doing "it could be a PID or TID" lookup. On failure 728 * returns NULL; on success returns the selected process and sets *tp 729 * to an appropriate thread in that process. 730 */ 731 static inline struct process * 732 process_tprfind(pid_t tpid, struct proc **tp) 733 { 734 if (tpid > THREAD_PID_OFFSET) { 735 struct proc *t = tfind(tpid - THREAD_PID_OFFSET); 736 737 if (t == NULL) 738 return NULL; 739 *tp = t; 740 return t->p_p; 741 } else { 742 struct process *tr = prfind(tpid); 743 744 if (tr == NULL) 745 return NULL; 746 *tp = TAILQ_FIRST(&tr->ps_threads); 747 return tr; 748 } 749 } 750 751 752 /* 753 * Check whether 'tr' is currently traced by 'curpr' and in a state 754 * to be manipulated. If 't' is supplied then it must be stopped and 755 * waited for. 756 */ 757 static inline int 758 process_checktracestate(struct process *curpr, struct process *tr, 759 struct proc *t) 760 { 761 /* 762 * You can't do what you want to the process if: 763 * (1) It's not being traced at all, 764 */ 765 if (!ISSET(tr->ps_flags, PS_TRACED)) 766 return EPERM; 767 768 /* 769 * (2) it's not being traced by _you_, or 770 */ 771 if (tr->ps_pptr != curpr) 772 return EBUSY; 773 774 /* 775 * (3) it's in the middle of execve(2) 776 */ 777 if (ISSET(tr->ps_flags, PS_INEXEC)) 778 return EAGAIN; 779 780 /* 781 * (4) if a thread was specified and it's not currently stopped. 782 */ 783 if (t != NULL && 784 (t->p_stat != SSTOP || !ISSET(tr->ps_flags, PS_WAITED))) 785 return EBUSY; 786 787 return 0; 788 } 789 790 791 /* 792 * Check if a process is allowed to fiddle with the memory of another. 793 * 794 * p = tracer 795 * tr = tracee 796 * 797 * 1. You can't attach to a process not owned by you or one that has raised 798 * its privileges. 799 * 1a. ...unless you are root. 800 * 801 * 2. init is always off-limits because it can control the securelevel. 802 * 2a. ...unless securelevel is permanently set to insecure. 803 * 804 * 3. Processes that are in the process of doing an exec() are always 805 * off-limits because of the can of worms they are. Just wait a 806 * second. 807 */ 808 int 809 process_checkioperm(struct proc *p, struct process *tr) 810 { 811 int error; 812 813 if ((tr->ps_ucred->cr_ruid != p->p_ucred->cr_ruid || 814 ISSET(tr->ps_flags, PS_SUGIDEXEC | PS_SUGID)) && 815 (error = suser(p, 0)) != 0) 816 return (error); 817 818 if ((tr->ps_pid == 1) && (securelevel > -1)) 819 return (EPERM); 820 821 if (ISSET(tr->ps_flags, PS_INEXEC)) 822 return (EAGAIN); 823 824 return (0); 825 } 826 827 int 828 process_domem(struct proc *curp, struct process *tr, struct uio *uio, int req) 829 { 830 struct vmspace *vm; 831 int error; 832 vaddr_t addr; 833 vsize_t len; 834 835 len = uio->uio_resid; 836 if (len == 0) 837 return 0; 838 839 if ((error = process_checkioperm(curp, tr)) != 0) 840 return error; 841 842 /* XXXCDC: how should locking work here? */ 843 vm = tr->ps_vmspace; 844 if ((tr->ps_flags & PS_EXITING) || (vm->vm_refcnt < 1)) 845 return EFAULT; 846 addr = uio->uio_offset; 847 848 vm->vm_refcnt++; 849 850 error = uvm_io(&vm->vm_map, uio, 851 (uio->uio_rw == UIO_WRITE) ? UVM_IO_FIXPROT : 0); 852 853 uvmspace_free(vm); 854 855 if (error == 0 && req == PT_WRITE_I) 856 pmap_proc_iflush(tr, addr, len); 857 858 return error; 859 } 860 861 int 862 process_auxv_offset(struct proc *curp, struct process *tr, struct uio *uiop) 863 { 864 struct vmspace *vm; 865 struct ps_strings pss; 866 struct iovec iov; 867 struct uio uio; 868 int error; 869 870 iov.iov_base = &pss; 871 iov.iov_len = sizeof(pss); 872 uio.uio_iov = &iov; 873 uio.uio_iovcnt = 1; 874 uio.uio_offset = (off_t)tr->ps_strings; 875 uio.uio_resid = sizeof(pss); 876 uio.uio_segflg = UIO_SYSSPACE; 877 uio.uio_rw = UIO_READ; 878 uio.uio_procp = curp; 879 880 vm = tr->ps_vmspace; 881 if ((tr->ps_flags & PS_EXITING) || (vm->vm_refcnt < 1)) 882 return EFAULT; 883 884 vm->vm_refcnt++; 885 error = uvm_io(&vm->vm_map, &uio, 0); 886 uvmspace_free(vm); 887 888 if (error != 0) 889 return error; 890 891 if (pss.ps_envstr == NULL) 892 return EIO; 893 894 uiop->uio_offset += (off_t)(vaddr_t)(pss.ps_envstr + pss.ps_nenvstr + 1); 895 #ifdef MACHINE_STACK_GROWS_UP 896 if (uiop->uio_offset < (off_t)tr->ps_strings) 897 return EIO; 898 #else 899 if (uiop->uio_offset > (off_t)tr->ps_strings) 900 return EIO; 901 if ((uiop->uio_offset + uiop->uio_resid) > (off_t)tr->ps_strings) 902 uiop->uio_resid = (off_t)tr->ps_strings - uiop->uio_offset; 903 #endif 904 905 return 0; 906 } 907 #endif 908