1 /* $NetBSD: sys_ptrace_common.c,v 1.69 2019/10/16 18:29:49 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright (c) 1982, 1986, 1989, 1993 34 * The Regents of the University of California. All rights reserved. 35 * (c) UNIX System Laboratories, Inc. 36 * All or some portions of this file are derived from material licensed 37 * to the University of California by American Telephone and Telegraph 38 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 39 * the permission of UNIX System Laboratories, Inc. 40 * 41 * This code is derived from software contributed to Berkeley by 42 * Jan-Simon Pendry. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. Neither the name of the University nor the names of its contributors 53 * may be used to endorse or promote products derived from this software 54 * without specific prior written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 66 * SUCH DAMAGE. 67 * 68 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93 69 */ 70 71 /*- 72 * Copyright (c) 1993 Jan-Simon Pendry. 73 * Copyright (c) 1994 Christopher G. Demetriou. All rights reserved. 74 * 75 * This code is derived from software contributed to Berkeley by 76 * Jan-Simon Pendry. 77 * 78 * Redistribution and use in source and binary forms, with or without 79 * modification, are permitted provided that the following conditions 80 * are met: 81 * 1. Redistributions of source code must retain the above copyright 82 * notice, this list of conditions and the following disclaimer. 83 * 2. Redistributions in binary form must reproduce the above copyright 84 * notice, this list of conditions and the following disclaimer in the 85 * documentation and/or other materials provided with the distribution. 86 * 3. All advertising materials mentioning features or use of this software 87 * must display the following acknowledgement: 88 * This product includes software developed by the University of 89 * California, Berkeley and its contributors. 90 * 4. Neither the name of the University nor the names of its contributors 91 * may be used to endorse or promote products derived from this software 92 * without specific prior written permission. 93 * 94 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 95 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 96 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 97 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 98 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 99 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 100 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 101 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 102 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 103 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 104 * SUCH DAMAGE. 105 * 106 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93 107 */ 108 109 /* 110 * References: 111 * (1) Bach's "The Design of the UNIX Operating System", 112 * (2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution, 113 * (3) the "4.4BSD Programmer's Reference Manual" published 114 * by USENIX and O'Reilly & Associates. 115 * The 4.4BSD PRM does a reasonably good job of documenting what the various 116 * ptrace() requests should actually do, and its text is quoted several times 117 * in this file. 118 */ 119 120 #include <sys/cdefs.h> 121 __KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.69 2019/10/16 18:29:49 christos Exp $"); 122 123 #ifdef _KERNEL_OPT 124 #include "opt_ptrace.h" 125 #include "opt_ktrace.h" 126 #include "opt_pax.h" 127 #include "opt_compat_netbsd32.h" 128 #endif 129 130 #if defined(__HAVE_COMPAT_NETBSD32) && !defined(COMPAT_NETBSD32) \ 131 && !defined(_RUMPKERNEL) 132 #define COMPAT_NETBSD32 133 #endif 134 135 #include <sys/param.h> 136 #include <sys/systm.h> 137 #include <sys/proc.h> 138 #include <sys/errno.h> 139 #include <sys/exec.h> 140 #include <sys/pax.h> 141 #include <sys/ptrace.h> 142 #include <sys/uio.h> 143 #include <sys/ras.h> 144 #include <sys/kmem.h> 145 #include <sys/kauth.h> 146 #include <sys/mount.h> 147 #include <sys/syscallargs.h> 148 #include <sys/module.h> 149 #include <sys/condvar.h> 150 #include <sys/mutex.h> 151 152 #include <uvm/uvm_extern.h> 153 154 #include <machine/reg.h> 155 156 #ifdef PTRACE 157 # ifdef PTRACE_DEBUG 158 # define DPRINTF(a) uprintf a 159 # else 160 # define DPRINTF(a) 161 # endif 162 163 static kauth_listener_t ptrace_listener; 164 static int process_auxv_offset(struct proc *, struct uio *); 165 166 extern int user_va0_disable; 167 168 #if 0 169 static int ptrace_cbref; 170 static kmutex_t ptrace_mtx; 171 static kcondvar_t ptrace_cv; 172 #endif 173 174 #ifdef PT_GETREGS 175 # define case_PT_GETREGS case PT_GETREGS: 176 #else 177 # define case_PT_GETREGS 178 #endif 179 180 #ifdef PT_SETREGS 181 # define case_PT_SETREGS case PT_SETREGS: 182 #else 183 # define case_PT_SETREGS 184 #endif 185 186 #ifdef PT_GETFPREGS 187 # define case_PT_GETFPREGS case PT_GETFPREGS: 188 #else 189 # define case_PT_GETFPREGS 190 #endif 191 192 #ifdef PT_SETFPREGS 193 # define case_PT_SETFPREGS case PT_SETFPREGS: 194 #else 195 # define case_PT_SETFPREGS 196 #endif 197 198 #ifdef PT_GETDBREGS 199 # define case_PT_GETDBREGS case PT_GETDBREGS: 200 #else 201 # define case_PT_GETDBREGS 202 #endif 203 204 #ifdef PT_SETDBREGS 205 # define case_PT_SETDBREGS case PT_SETDBREGS: 206 #else 207 # define case_PT_SETDBREGS 208 #endif 209 210 #if defined(PT_SETREGS) || defined(PT_GETREGS) || \ 211 defined(PT_SETFPREGS) || defined(PT_GETFPREGS) || \ 212 defined(PT_SETDBREGS) || defined(PT_GETDBREGS) 213 # define PT_REGISTERS 214 #endif 215 216 static int 217 ptrace_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie, 218 void *arg0, void *arg1, void *arg2, void *arg3) 219 { 220 struct proc *p; 221 int result; 222 #ifdef PT_SETDBREGS 223 extern int user_set_dbregs; 224 #endif 225 226 result = KAUTH_RESULT_DEFER; 227 p = arg0; 228 229 #if 0 230 mutex_enter(&ptrace_mtx); 231 ptrace_cbref++; 232 mutex_exit(&ptrace_mtx); 233 #endif 234 if (action != KAUTH_PROCESS_PTRACE) 235 goto out; 236 237 switch ((u_long)arg1) { 238 #ifdef PT_SETDBREGS 239 case_PT_SETDBREGS 240 if (kauth_cred_getuid(cred) != 0 && user_set_dbregs == 0) { 241 result = KAUTH_RESULT_DENY; 242 break; 243 } 244 #endif 245 /* FALLTHROUGH */ 246 case PT_TRACE_ME: 247 case PT_ATTACH: 248 case PT_WRITE_I: 249 case PT_WRITE_D: 250 case PT_READ_I: 251 case PT_READ_D: 252 case PT_IO: 253 case_PT_GETREGS 254 case_PT_SETREGS 255 case_PT_GETFPREGS 256 case_PT_SETFPREGS 257 case_PT_GETDBREGS 258 case PT_SET_EVENT_MASK: 259 case PT_GET_EVENT_MASK: 260 case PT_GET_PROCESS_STATE: 261 case PT_SET_SIGINFO: 262 case PT_GET_SIGINFO: 263 #ifdef __HAVE_PTRACE_MACHDEP 264 PTRACE_MACHDEP_REQUEST_CASES 265 #endif 266 if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) || 267 ISSET(p->p_flag, PK_SUGID)) { 268 break; 269 } 270 271 result = KAUTH_RESULT_ALLOW; 272 273 break; 274 275 #ifdef PT_STEP 276 case PT_STEP: 277 case PT_SETSTEP: 278 case PT_CLEARSTEP: 279 #endif 280 case PT_CONTINUE: 281 case PT_KILL: 282 case PT_DETACH: 283 case PT_LWPINFO: 284 case PT_SYSCALL: 285 case PT_SYSCALLEMU: 286 case PT_DUMPCORE: 287 case PT_RESUME: 288 case PT_SUSPEND: 289 case PT_STOP: 290 result = KAUTH_RESULT_ALLOW; 291 break; 292 293 default: 294 break; 295 } 296 297 out: 298 #if 0 299 mutex_enter(&ptrace_mtx); 300 if (--ptrace_cbref == 0) 301 cv_broadcast(&ptrace_cv); 302 mutex_exit(&ptrace_mtx); 303 #endif 304 305 return result; 306 } 307 308 int 309 ptrace_init(void) 310 { 311 312 #if 0 313 mutex_init(&ptrace_mtx, MUTEX_DEFAULT, IPL_NONE); 314 cv_init(&ptrace_cv, "ptracecb"); 315 ptrace_cbref = 0; 316 #endif 317 ptrace_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS, 318 ptrace_listener_cb, NULL); 319 return 0; 320 } 321 322 int 323 ptrace_fini(void) 324 { 325 326 kauth_unlisten_scope(ptrace_listener); 327 328 #if 0 329 /* Make sure no-one is executing our kauth listener */ 330 331 mutex_enter(&ptrace_mtx); 332 while (ptrace_cbref != 0) 333 cv_wait(&ptrace_cv, &ptrace_mtx); 334 mutex_exit(&ptrace_mtx); 335 mutex_destroy(&ptrace_mtx); 336 cv_destroy(&ptrace_cv); 337 #endif 338 339 return 0; 340 } 341 342 static struct proc * 343 ptrace_find(struct lwp *l, int req, pid_t pid) 344 { 345 struct proc *t; 346 347 /* "A foolish consistency..." XXX */ 348 if (req == PT_TRACE_ME) { 349 t = l->l_proc; 350 mutex_enter(t->p_lock); 351 return t; 352 } 353 354 /* Find the process we're supposed to be operating on. */ 355 t = proc_find(pid); 356 if (t == NULL) 357 return NULL; 358 359 /* XXX-elad */ 360 mutex_enter(t->p_lock); 361 int error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, 362 t, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL); 363 if (error) { 364 mutex_exit(t->p_lock); 365 return NULL; 366 } 367 return t; 368 } 369 370 static int 371 ptrace_allowed(struct lwp *l, int req, struct proc *t, struct proc *p, 372 bool *locked) 373 { 374 *locked = false; 375 376 /* 377 * Grab a reference on the process to prevent it from execing or 378 * exiting. 379 */ 380 if (!rw_tryenter(&t->p_reflock, RW_READER)) 381 return EBUSY; 382 383 *locked = true; 384 385 /* Make sure we can operate on it. */ 386 switch (req) { 387 case PT_TRACE_ME: 388 /* 389 * You can't say to the parent of a process to start tracing if: 390 * (1) the parent is initproc, 391 */ 392 if (p->p_pptr == initproc) 393 return EPERM; 394 395 /* 396 * (2) the process is initproc, or 397 */ 398 if (p == initproc) 399 return EPERM; 400 401 /* 402 * (3) the child is already traced. 403 */ 404 if (ISSET(p->p_slflag, PSL_TRACED)) 405 return EBUSY; 406 407 return 0; 408 409 case PT_ATTACH: 410 /* 411 * You can't attach to a process if: 412 * (1) it's the process that's doing the attaching, 413 */ 414 if (t == p) 415 return EINVAL; 416 417 /* 418 * (2) it's a system process, 419 */ 420 if (t->p_flag & PK_SYSTEM) 421 return EPERM; 422 423 /* 424 * (3) the tracer is initproc, 425 */ 426 if (p == initproc) 427 return EPERM; 428 429 /* 430 * (4) it's already being traced, 431 */ 432 if (ISSET(t->p_slflag, PSL_TRACED)) 433 return EBUSY; 434 435 /* 436 * (5) it's a vfork(2)ed parent of the current process, or 437 */ 438 if (ISSET(p->p_lflag, PL_PPWAIT) && p->p_pptr == t) 439 return EPERM; 440 441 /* 442 * (6) the tracer is chrooted, and its root directory is 443 * not at or above the root directory of the tracee 444 */ 445 mutex_exit(t->p_lock); /* XXXSMP */ 446 int tmp = proc_isunder(t, l); 447 mutex_enter(t->p_lock); /* XXXSMP */ 448 if (!tmp) 449 return EPERM; 450 return 0; 451 452 case PT_READ_I: 453 case PT_READ_D: 454 case PT_WRITE_I: 455 case PT_WRITE_D: 456 case PT_IO: 457 case PT_SET_SIGINFO: 458 case PT_GET_SIGINFO: 459 case_PT_GETREGS 460 case_PT_SETREGS 461 case_PT_GETFPREGS 462 case_PT_SETFPREGS 463 case_PT_GETDBREGS 464 case_PT_SETDBREGS 465 #ifdef __HAVE_PTRACE_MACHDEP 466 PTRACE_MACHDEP_REQUEST_CASES 467 #endif 468 /* 469 * You can't read/write the memory or registers of a process 470 * if the tracer is chrooted, and its root directory is not at 471 * or above the root directory of the tracee. 472 */ 473 mutex_exit(t->p_lock); /* XXXSMP */ 474 tmp = proc_isunder(t, l); 475 mutex_enter(t->p_lock); /* XXXSMP */ 476 if (!tmp) 477 return EPERM; 478 /*FALLTHROUGH*/ 479 480 case PT_CONTINUE: 481 case PT_KILL: 482 case PT_DETACH: 483 case PT_LWPINFO: 484 case PT_SYSCALL: 485 case PT_SYSCALLEMU: 486 case PT_DUMPCORE: 487 #ifdef PT_STEP 488 case PT_STEP: 489 case PT_SETSTEP: 490 case PT_CLEARSTEP: 491 #endif 492 case PT_SET_EVENT_MASK: 493 case PT_GET_EVENT_MASK: 494 case PT_GET_PROCESS_STATE: 495 case PT_RESUME: 496 case PT_SUSPEND: 497 case PT_STOP: 498 /* 499 * You can't do what you want to the process if: 500 * (1) It's not being traced at all, 501 */ 502 if (!ISSET(t->p_slflag, PSL_TRACED)) 503 return EPERM; 504 505 /* 506 * (2) it's not being traced by _you_, or 507 */ 508 if (t->p_pptr != p) { 509 DPRINTF(("parent %d != %d\n", t->p_pptr->p_pid, 510 p->p_pid)); 511 return EBUSY; 512 } 513 514 /* 515 * (3) it's not currently stopped. 516 * 517 * As an exception allow PT_KILL and PT_STOP here. 518 */ 519 if (req != PT_KILL && req != PT_STOP && 520 (t->p_stat != SSTOP || !t->p_waited /* XXXSMP */)) { 521 DPRINTF(("stat %d flag %d\n", t->p_stat, 522 !t->p_waited)); 523 return EBUSY; 524 } 525 return 0; 526 527 default: /* It was not a legal request. */ 528 return EINVAL; 529 } 530 } 531 532 static int 533 ptrace_needs_hold(int req) 534 { 535 switch (req) { 536 #ifdef PT_STEP 537 case PT_STEP: 538 #endif 539 case PT_CONTINUE: 540 case PT_DETACH: 541 case PT_KILL: 542 case PT_SYSCALL: 543 case PT_SYSCALLEMU: 544 case PT_ATTACH: 545 case PT_TRACE_ME: 546 case PT_GET_SIGINFO: 547 case PT_SET_SIGINFO: 548 case PT_STOP: 549 return 1; 550 default: 551 return 0; 552 } 553 } 554 555 static int 556 ptrace_update_lwp(struct proc *t, struct lwp **lt, lwpid_t lid) 557 { 558 if (lid == 0 || lid == (*lt)->l_lid || t->p_nlwps == 1) 559 return 0; 560 561 mutex_enter(t->p_lock); 562 lwp_delref2(*lt); 563 564 *lt = lwp_find(t, lid); 565 if (*lt == NULL) { 566 mutex_exit(t->p_lock); 567 return ESRCH; 568 } 569 570 if ((*lt)->l_flag & LW_SYSTEM) { 571 mutex_exit(t->p_lock); 572 *lt = NULL; 573 return EINVAL; 574 } 575 576 lwp_addref(*lt); 577 mutex_exit(t->p_lock); 578 579 return 0; 580 } 581 582 static int 583 ptrace_get_siginfo(struct proc *t, struct ptrace_methods *ptm, void *addr, 584 size_t data) 585 { 586 struct ptrace_siginfo psi; 587 588 memset(&psi, 0, sizeof(psi)); 589 psi.psi_siginfo._info = t->p_sigctx.ps_info; 590 psi.psi_lwpid = t->p_sigctx.ps_lwp; 591 DPRINTF(("%s: lwp=%d signal=%d\n", __func__, psi.psi_lwpid, 592 psi.psi_siginfo.si_signo)); 593 594 return ptm->ptm_copyout_siginfo(&psi, addr, data); 595 } 596 597 static int 598 ptrace_set_siginfo(struct proc *t, struct lwp **lt, struct ptrace_methods *ptm, 599 void *addr, size_t data) 600 { 601 struct ptrace_siginfo psi; 602 603 int error = ptm->ptm_copyin_siginfo(&psi, addr, data); 604 if (error) 605 return error; 606 607 /* Check that the data is a valid signal number or zero. */ 608 if (psi.psi_siginfo.si_signo < 0 || psi.psi_siginfo.si_signo >= NSIG) 609 return EINVAL; 610 611 t->p_sigctx.ps_faked = true; 612 t->p_sigctx.ps_info = psi.psi_siginfo._info; 613 t->p_sigctx.ps_lwp = psi.psi_lwpid; 614 DPRINTF(("%s: lwp=%d signal=%d\n", __func__, psi.psi_lwpid, 615 psi.psi_siginfo.si_signo)); 616 return 0; 617 } 618 619 static int 620 ptrace_get_event_mask(struct proc *t, void *addr, size_t data) 621 { 622 struct ptrace_event pe; 623 624 if (data != sizeof(pe)) { 625 DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(pe))); 626 return EINVAL; 627 } 628 memset(&pe, 0, sizeof(pe)); 629 pe.pe_set_event = ISSET(t->p_slflag, PSL_TRACEFORK) ? 630 PTRACE_FORK : 0; 631 pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK) ? 632 PTRACE_VFORK : 0; 633 pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK_DONE) ? 634 PTRACE_VFORK_DONE : 0; 635 pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_CREATE) ? 636 PTRACE_LWP_CREATE : 0; 637 pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_EXIT) ? 638 PTRACE_LWP_EXIT : 0; 639 pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEPOSIX_SPAWN) ? 640 PTRACE_POSIX_SPAWN : 0; 641 DPRINTF(("%s: lwp=%d event=%#x\n", __func__, 642 t->p_sigctx.ps_lwp, pe.pe_set_event)); 643 return copyout(&pe, addr, sizeof(pe)); 644 } 645 646 static int 647 ptrace_set_event_mask(struct proc *t, void *addr, size_t data) 648 { 649 struct ptrace_event pe; 650 int error; 651 652 if (data != sizeof(pe)) { 653 DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(pe))); 654 return EINVAL; 655 } 656 if ((error = copyin(addr, &pe, sizeof(pe))) != 0) 657 return error; 658 659 DPRINTF(("%s: lwp=%d event=%#x\n", __func__, 660 t->p_sigctx.ps_lwp, pe.pe_set_event)); 661 if (pe.pe_set_event & PTRACE_FORK) 662 SET(t->p_slflag, PSL_TRACEFORK); 663 else 664 CLR(t->p_slflag, PSL_TRACEFORK); 665 666 if (pe.pe_set_event & PTRACE_VFORK) 667 SET(t->p_slflag, PSL_TRACEVFORK); 668 else 669 CLR(t->p_slflag, PSL_TRACEVFORK); 670 671 if (pe.pe_set_event & PTRACE_VFORK_DONE) 672 SET(t->p_slflag, PSL_TRACEVFORK_DONE); 673 else 674 CLR(t->p_slflag, PSL_TRACEVFORK_DONE); 675 676 if (pe.pe_set_event & PTRACE_LWP_CREATE) 677 SET(t->p_slflag, PSL_TRACELWP_CREATE); 678 else 679 CLR(t->p_slflag, PSL_TRACELWP_CREATE); 680 681 if (pe.pe_set_event & PTRACE_LWP_EXIT) 682 SET(t->p_slflag, PSL_TRACELWP_EXIT); 683 else 684 CLR(t->p_slflag, PSL_TRACELWP_EXIT); 685 686 if (pe.pe_set_event & PTRACE_POSIX_SPAWN) 687 SET(t->p_slflag, PSL_TRACEPOSIX_SPAWN); 688 else 689 CLR(t->p_slflag, PSL_TRACEPOSIX_SPAWN); 690 691 return 0; 692 } 693 694 static int 695 ptrace_get_process_state(struct proc *t, void *addr, size_t data) 696 { 697 struct _ksiginfo *si; 698 struct ptrace_state ps; 699 700 if (data != sizeof(ps)) { 701 DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(ps))); 702 return EINVAL; 703 } 704 705 if (t->p_sigctx.ps_info._signo != SIGTRAP || 706 (t->p_sigctx.ps_info._code != TRAP_CHLD && 707 t->p_sigctx.ps_info._code != TRAP_LWP)) { 708 memset(&ps, 0, sizeof(ps)); 709 } else { 710 si = &t->p_sigctx.ps_info; 711 712 KASSERT(si->_reason._ptrace_state._pe_report_event > 0); 713 KASSERT(si->_reason._ptrace_state._option._pe_other_pid > 0); 714 715 ps.pe_report_event = si->_reason._ptrace_state._pe_report_event; 716 717 CTASSERT(sizeof(ps.pe_other_pid) == sizeof(ps.pe_lwp)); 718 ps.pe_other_pid = 719 si->_reason._ptrace_state._option._pe_other_pid; 720 } 721 722 DPRINTF(("%s: lwp=%d event=%#x pid=%d lwp=%d\n", __func__, 723 t->p_sigctx.ps_lwp, ps.pe_report_event, 724 ps.pe_other_pid, ps.pe_lwp)); 725 return copyout(&ps, addr, sizeof(ps)); 726 } 727 728 static int 729 ptrace_lwpinfo(struct proc *t, struct lwp **lt, void *addr, size_t data) 730 { 731 struct ptrace_lwpinfo pl; 732 733 if (data != sizeof(pl)) { 734 DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(pl))); 735 return EINVAL; 736 } 737 int error = copyin(addr, &pl, sizeof(pl)); 738 if (error) 739 return error; 740 741 lwpid_t tmp = pl.pl_lwpid; 742 lwp_delref(*lt); 743 mutex_enter(t->p_lock); 744 if (tmp == 0) 745 *lt = lwp_find_first(t); 746 else { 747 *lt = lwp_find(t, tmp); 748 if (*lt == NULL) { 749 mutex_exit(t->p_lock); 750 return ESRCH; 751 } 752 *lt = LIST_NEXT(*lt, l_sibling); 753 } 754 755 while (*lt != NULL && !lwp_alive(*lt)) 756 *lt = LIST_NEXT(*lt, l_sibling); 757 758 pl.pl_lwpid = 0; 759 pl.pl_event = 0; 760 if (*lt) { 761 lwp_addref(*lt); 762 pl.pl_lwpid = (*lt)->l_lid; 763 764 if ((*lt)->l_flag & LW_WSUSPEND) 765 pl.pl_event = PL_EVENT_SUSPENDED; 766 /* 767 * If we match the lwp, or it was sent to every lwp, 768 * we set PL_EVENT_SIGNAL. 769 * XXX: ps_lwp == 0 means everyone and noone, so 770 * check ps_signo too. 771 */ 772 else if ((*lt)->l_lid == t->p_sigctx.ps_lwp 773 || (t->p_sigctx.ps_lwp == 0 && 774 t->p_sigctx.ps_info._signo)) { 775 DPRINTF(("%s: lwp=%d siglwp=%d signo %d\n", __func__, 776 pl.pl_lwpid, t->p_sigctx.ps_lwp, 777 t->p_sigctx.ps_info._signo)); 778 pl.pl_event = PL_EVENT_SIGNAL; 779 } 780 } 781 mutex_exit(t->p_lock); 782 DPRINTF(("%s: lwp=%d event=%#x\n", __func__, 783 pl.pl_lwpid, pl.pl_event)); 784 785 return copyout(&pl, addr, sizeof(pl)); 786 } 787 788 static int 789 ptrace_startstop(struct proc *t, struct lwp **lt, int rq, void *addr, 790 size_t data) 791 { 792 int error; 793 794 if ((error = ptrace_update_lwp(t, lt, data)) != 0) 795 return error; 796 797 DPRINTF(("%s: lwp=%d request=%d\n", __func__, (*lt)->l_lid, rq)); 798 lwp_lock(*lt); 799 if (rq == PT_SUSPEND) 800 (*lt)->l_flag |= LW_DBGSUSPEND; 801 else { 802 (*lt)->l_flag &= ~LW_DBGSUSPEND; 803 if ((*lt)->l_flag != LSSUSPENDED) 804 (*lt)->l_stat = LSSTOP; 805 } 806 lwp_unlock(*lt); 807 return 0; 808 } 809 810 #ifdef PT_REGISTERS 811 static int 812 ptrace_uio_dir(int req) 813 { 814 switch (req) { 815 case_PT_GETREGS 816 case_PT_GETFPREGS 817 case_PT_GETDBREGS 818 return UIO_READ; 819 case_PT_SETREGS 820 case_PT_SETFPREGS 821 case_PT_SETDBREGS 822 return UIO_WRITE; 823 default: 824 return -1; 825 } 826 } 827 828 static int 829 ptrace_regs(struct lwp *l, struct lwp **lt, int rq, struct ptrace_methods *ptm, 830 void *addr, size_t data) 831 { 832 int error; 833 struct proc *t = (*lt)->l_proc; 834 struct vmspace *vm; 835 836 if ((error = ptrace_update_lwp(t, lt, data)) != 0) 837 return error; 838 839 int dir = ptrace_uio_dir(rq); 840 size_t size; 841 int (*func)(struct lwp *, struct lwp *, struct uio *); 842 843 DPRINTF(("%s: lwp=%d request=%d\n", __func__, l->l_lid, rq)); 844 845 switch (rq) { 846 #if defined(PT_SETREGS) || defined(PT_GETREGS) 847 case_PT_GETREGS 848 case_PT_SETREGS 849 if (!process_validregs(*lt)) 850 return EINVAL; 851 size = PROC_REGSZ(t); 852 func = ptm->ptm_doregs; 853 break; 854 #endif 855 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS) 856 case_PT_GETFPREGS 857 case_PT_SETFPREGS 858 if (!process_validfpregs(*lt)) 859 return EINVAL; 860 size = PROC_FPREGSZ(t); 861 func = ptm->ptm_dofpregs; 862 break; 863 #endif 864 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS) 865 case_PT_GETDBREGS 866 case_PT_SETDBREGS 867 if (!process_validdbregs(*lt)) 868 return EINVAL; 869 size = PROC_DBREGSZ(t); 870 func = ptm->ptm_dodbregs; 871 break; 872 #endif 873 default: 874 return EINVAL; 875 } 876 877 error = proc_vmspace_getref(l->l_proc, &vm); 878 if (error) 879 return error; 880 881 struct uio uio; 882 struct iovec iov; 883 884 iov.iov_base = addr; 885 iov.iov_len = size; 886 uio.uio_iov = &iov; 887 uio.uio_iovcnt = 1; 888 uio.uio_offset = 0; 889 uio.uio_resid = iov.iov_len; 890 uio.uio_rw = dir; 891 uio.uio_vmspace = vm; 892 893 error = (*func)(l, *lt, &uio); 894 uvmspace_free(vm); 895 return error; 896 } 897 #endif 898 899 static int 900 ptrace_sendsig(struct lwp *l, int req, struct proc *t, struct lwp *lt, int signo, int resume_all) 901 { 902 ksiginfo_t ksi; 903 904 /* Finally, deliver the requested signal (or none). */ 905 if (t->p_stat == SSTOP) { 906 /* 907 * Unstop the process. If it needs to take a 908 * signal, make all efforts to ensure that at 909 * an LWP runs to see it. 910 */ 911 t->p_xsig = signo; 912 913 /* 914 * signo > 0 check prevents a potential panic, as 915 * sigismember(&...,0) is invalid check and signo 916 * can be equal to 0 as a special case of no-signal. 917 */ 918 if (signo > 0 && sigismember(&stopsigmask, signo)) { 919 t->p_waited = 0; 920 child_psignal(t, 0); 921 } else if (resume_all) 922 proc_unstop(t); 923 else 924 lwp_unstop(lt); 925 return 0; 926 } 927 928 KASSERT(req == PT_KILL || req == PT_STOP || req == PT_ATTACH); 929 930 KSI_INIT(&ksi); 931 ksi.ksi_signo = signo; 932 ksi.ksi_code = SI_USER; 933 ksi.ksi_pid = l->l_proc->p_pid; 934 ksi.ksi_uid = kauth_cred_geteuid(l->l_cred); 935 936 t->p_sigctx.ps_faked = false; 937 938 DPRINTF(("%s: pid=%d.%d signal=%d resume_all=%d\n", __func__, t->p_pid, 939 lt->l_lid, signo, resume_all)); 940 941 return kpsignal2(t, &ksi); 942 } 943 944 static int 945 ptrace_dumpcore(struct lwp *lt, char *path, size_t len) 946 { 947 int error; 948 if (path != NULL) { 949 950 if (len >= MAXPATHLEN) 951 return EINVAL; 952 953 char *src = path; 954 path = kmem_alloc(len + 1, KM_SLEEP); 955 error = copyin(src, path, len); 956 if (error) 957 goto out; 958 path[len] = '\0'; 959 } 960 DPRINTF(("%s: lwp=%d\n", __func__, lt->l_lid)); 961 error = (*coredump_vec)(lt, path); 962 out: 963 if (path) 964 kmem_free(path, len + 1); 965 return error; 966 } 967 968 static int 969 ptrace_doio(struct lwp *l, struct proc *t, struct lwp *lt, 970 struct ptrace_io_desc *piod, void *addr, bool sysspace) 971 { 972 struct uio uio; 973 struct iovec iov; 974 int error, tmp; 975 976 error = 0; 977 iov.iov_base = piod->piod_addr; 978 iov.iov_len = piod->piod_len; 979 uio.uio_iov = &iov; 980 uio.uio_iovcnt = 1; 981 uio.uio_offset = (off_t)(unsigned long)piod->piod_offs; 982 uio.uio_resid = piod->piod_len; 983 984 DPRINTF(("%s: lwp=%d request=%d\n", __func__, l->l_lid, piod->piod_op)); 985 986 switch (piod->piod_op) { 987 case PIOD_READ_D: 988 case PIOD_READ_I: 989 uio.uio_rw = UIO_READ; 990 break; 991 case PIOD_WRITE_D: 992 case PIOD_WRITE_I: 993 /* 994 * Can't write to a RAS 995 */ 996 if (ras_lookup(t, addr) != (void *)-1) { 997 return EACCES; 998 } 999 uio.uio_rw = UIO_WRITE; 1000 break; 1001 case PIOD_READ_AUXV: 1002 uio.uio_rw = UIO_READ; 1003 tmp = t->p_execsw->es_arglen; 1004 if (uio.uio_offset > tmp) 1005 return EIO; 1006 if (uio.uio_resid > tmp - uio.uio_offset) 1007 uio.uio_resid = tmp - uio.uio_offset; 1008 piod->piod_len = iov.iov_len = uio.uio_resid; 1009 error = process_auxv_offset(t, &uio); 1010 break; 1011 default: 1012 error = EINVAL; 1013 break; 1014 } 1015 1016 if (error) 1017 return error; 1018 1019 if (sysspace) { 1020 uio.uio_vmspace = vmspace_kernel(); 1021 } else { 1022 error = proc_vmspace_getref(l->l_proc, &uio.uio_vmspace); 1023 if (error) 1024 return error; 1025 } 1026 1027 error = process_domem(l, lt, &uio); 1028 if (!sysspace) 1029 uvmspace_free(uio.uio_vmspace); 1030 if (error) 1031 return error; 1032 piod->piod_len -= uio.uio_resid; 1033 return 0; 1034 } 1035 1036 int 1037 do_ptrace(struct ptrace_methods *ptm, struct lwp *l, int req, pid_t pid, 1038 void *addr, int data, register_t *retval) 1039 { 1040 struct proc *p = l->l_proc; 1041 struct lwp *lt = NULL; 1042 struct lwp *lt2; 1043 struct proc *t; /* target process */ 1044 struct ptrace_io_desc piod; 1045 int error, write, tmp, pheld; 1046 int signo = 0; 1047 int resume_all; 1048 bool locked; 1049 error = 0; 1050 1051 /* 1052 * If attaching or detaching, we need to get a write hold on the 1053 * proclist lock so that we can re-parent the target process. 1054 */ 1055 mutex_enter(proc_lock); 1056 1057 t = ptrace_find(l, req, pid); 1058 if (t == NULL) { 1059 mutex_exit(proc_lock); 1060 return ESRCH; 1061 } 1062 1063 pheld = 1; 1064 if ((error = ptrace_allowed(l, req, t, p, &locked)) != 0) 1065 goto out; 1066 1067 if ((error = kauth_authorize_process(l->l_cred, 1068 KAUTH_PROCESS_PTRACE, t, KAUTH_ARG(req), NULL, NULL)) != 0) 1069 goto out; 1070 1071 if ((lt = lwp_find_first(t)) == NULL) { 1072 error = ESRCH; 1073 goto out; 1074 } 1075 1076 /* Do single-step fixup if needed. */ 1077 FIX_SSTEP(t); 1078 KASSERT(lt != NULL); 1079 lwp_addref(lt); 1080 1081 /* 1082 * Which locks do we need held? XXX Ugly. 1083 */ 1084 if ((pheld = ptrace_needs_hold(req)) == 0) { 1085 mutex_exit(t->p_lock); 1086 mutex_exit(proc_lock); 1087 } 1088 1089 /* Now do the operation. */ 1090 write = 0; 1091 *retval = 0; 1092 tmp = 0; 1093 resume_all = 1; 1094 1095 switch (req) { 1096 case PT_TRACE_ME: 1097 /* Just set the trace flag. */ 1098 SET(t->p_slflag, PSL_TRACED); 1099 t->p_opptr = t->p_pptr; 1100 break; 1101 1102 /* 1103 * The I and D separate address space has been inherited from PDP-11. 1104 * The 16-bit UNIX started with a single address space per program, 1105 * but was extended to two 16-bit (2 x 64kb) address spaces. 1106 * 1107 * We no longer maintain this feature in maintained architectures, but 1108 * we keep the API for backward compatiblity. Currently the I and D 1109 * operations are exactly the same and not distinguished in debuggers. 1110 */ 1111 case PT_WRITE_I: 1112 case PT_WRITE_D: 1113 write = 1; 1114 tmp = data; 1115 /* FALLTHROUGH */ 1116 case PT_READ_I: 1117 case PT_READ_D: 1118 piod.piod_addr = &tmp; 1119 piod.piod_len = sizeof(tmp); 1120 piod.piod_offs = addr; 1121 piod.piod_op = write ? PIOD_WRITE_D : PIOD_READ_D; 1122 if ((error = ptrace_doio(l, t, lt, &piod, addr, true)) != 0) 1123 break; 1124 /* 1125 * For legacy reasons we treat here two results as success: 1126 * - incomplete transfer piod.piod_len < sizeof(tmp) 1127 * - no transfer piod.piod_len == 0 1128 * 1129 * This means that there is no way to determine whether 1130 * transfer operation was performed in PT_WRITE and PT_READ 1131 * calls. 1132 */ 1133 if (!write) 1134 *retval = tmp; 1135 break; 1136 1137 case PT_IO: 1138 if ((error = ptm->ptm_copyin_piod(&piod, addr, data)) != 0) 1139 break; 1140 if (piod.piod_len < 1) { 1141 error = EINVAL; 1142 break; 1143 } 1144 if ((error = ptrace_doio(l, t, lt, &piod, addr, false)) != 0) 1145 break; 1146 /* 1147 * For legacy reasons we treat here two results as success: 1148 * - incomplete transfer piod.piod_len < sizeof(tmp) 1149 * - no transfer piod.piod_len == 0 1150 */ 1151 error = ptm->ptm_copyout_piod(&piod, addr, data); 1152 break; 1153 1154 case PT_DUMPCORE: 1155 error = ptrace_dumpcore(lt, addr, data); 1156 break; 1157 1158 #ifdef PT_STEP 1159 case PT_STEP: 1160 /* 1161 * From the 4.4BSD PRM: 1162 * "Execution continues as in request PT_CONTINUE; however 1163 * as soon as possible after execution of at least one 1164 * instruction, execution stops again. [ ... ]" 1165 */ 1166 #endif 1167 case PT_CONTINUE: 1168 case PT_SYSCALL: 1169 case PT_DETACH: 1170 if (req == PT_SYSCALL) { 1171 if (!ISSET(t->p_slflag, PSL_SYSCALL)) { 1172 SET(t->p_slflag, PSL_SYSCALL); 1173 #ifdef __HAVE_SYSCALL_INTERN 1174 (*t->p_emul->e_syscall_intern)(t); 1175 #endif 1176 } 1177 } else { 1178 if (ISSET(t->p_slflag, PSL_SYSCALL)) { 1179 CLR(t->p_slflag, PSL_SYSCALL); 1180 #ifdef __HAVE_SYSCALL_INTERN 1181 (*t->p_emul->e_syscall_intern)(t); 1182 #endif 1183 } 1184 } 1185 t->p_trace_enabled = trace_is_enabled(t); 1186 1187 /* 1188 * Pick up the LWPID, if supplied. There are two cases: 1189 * data < 0 : step or continue single thread, lwp = -data 1190 * data > 0 in PT_STEP : step this thread, continue others 1191 * For operations other than PT_STEP, data > 0 means 1192 * data is the signo to deliver to the process. 1193 */ 1194 tmp = data; 1195 if (tmp >= 0) { 1196 #ifdef PT_STEP 1197 if (req == PT_STEP) 1198 signo = 0; 1199 else 1200 #endif 1201 { 1202 signo = tmp; 1203 tmp = 0; /* don't search for LWP */ 1204 } 1205 } else if (tmp == INT_MIN) { 1206 error = ESRCH; 1207 break; 1208 } else { 1209 tmp = -tmp; 1210 } 1211 1212 if (tmp > 0) { 1213 if (req == PT_DETACH) { 1214 error = EINVAL; 1215 break; 1216 } 1217 lwp_delref2 (lt); 1218 lt = lwp_find(t, tmp); 1219 if (lt == NULL) { 1220 error = ESRCH; 1221 break; 1222 } 1223 lwp_addref(lt); 1224 resume_all = 0; 1225 signo = 0; 1226 } 1227 1228 /* 1229 * From the 4.4BSD PRM: 1230 * "The data argument is taken as a signal number and the 1231 * child's execution continues at location addr as if it 1232 * incurred that signal. Normally the signal number will 1233 * be either 0 to indicate that the signal that caused the 1234 * stop should be ignored, or that value fetched out of 1235 * the process's image indicating which signal caused 1236 * the stop. If addr is (int *)1 then execution continues 1237 * from where it stopped." 1238 */ 1239 1240 /* Check that the data is a valid signal number or zero. */ 1241 if (signo < 0 || signo >= NSIG) { 1242 error = EINVAL; 1243 break; 1244 } 1245 1246 /* Prevent process deadlock */ 1247 if (resume_all) { 1248 #ifdef PT_STEP 1249 if (req == PT_STEP) { 1250 if (lt->l_flag & 1251 (LW_WSUSPEND | LW_DBGSUSPEND)) { 1252 error = EDEADLK; 1253 break; 1254 } 1255 } else 1256 #endif 1257 { 1258 error = EDEADLK; 1259 LIST_FOREACH(lt2, &t->p_lwps, l_sibling) { 1260 if ((lt2->l_flag & 1261 (LW_WSUSPEND | LW_DBGSUSPEND)) == 0 1262 ) { 1263 error = 0; 1264 break; 1265 } 1266 } 1267 if (error != 0) 1268 break; 1269 } 1270 } else { 1271 if (lt->l_flag & (LW_WSUSPEND | LW_DBGSUSPEND)) { 1272 error = EDEADLK; 1273 break; 1274 } 1275 } 1276 1277 /* 1278 * Reject setting program counter to 0x0 if VA0 is disabled. 1279 * 1280 * Not all kernels implement this feature to set Program 1281 * Counter in one go in PT_CONTINUE and similar operations. 1282 * This causes portability issues as passing address 0x0 1283 * on these kernels is no-operation, but can cause failure 1284 * in most cases on NetBSD. 1285 */ 1286 if (user_va0_disable && addr == 0) { 1287 error = EINVAL; 1288 break; 1289 } 1290 1291 /* If the address parameter is not (int *)1, set the pc. */ 1292 if ((int *)addr != (int *)1) { 1293 error = process_set_pc(lt, addr); 1294 if (error != 0) 1295 break; 1296 } 1297 #ifdef PT_STEP 1298 /* 1299 * Arrange for a single-step, if that's requested and possible. 1300 * More precisely, set the single step status as requested for 1301 * the requested thread, and clear it for other threads. 1302 */ 1303 LIST_FOREACH(lt2, &t->p_lwps, l_sibling) { 1304 if (ISSET(lt2->l_pflag, LP_SINGLESTEP)) { 1305 lwp_lock(lt2); 1306 process_sstep(lt2, 1); 1307 lwp_unlock(lt2); 1308 } else if (lt != lt2) { 1309 lwp_lock(lt2); 1310 process_sstep(lt2, 0); 1311 lwp_unlock(lt2); 1312 } 1313 } 1314 error = process_sstep(lt, 1315 ISSET(lt->l_pflag, LP_SINGLESTEP) || req == PT_STEP); 1316 if (error) 1317 break; 1318 #endif 1319 if (req == PT_DETACH) { 1320 CLR(t->p_slflag, PSL_TRACED|PSL_SYSCALL); 1321 1322 /* give process back to original parent or init */ 1323 if (t->p_opptr != t->p_pptr) { 1324 struct proc *pp = t->p_opptr; 1325 proc_reparent(t, pp ? pp : initproc); 1326 } 1327 1328 /* not being traced any more */ 1329 t->p_opptr = NULL; 1330 1331 /* clear single step */ 1332 LIST_FOREACH(lt2, &t->p_lwps, l_sibling) { 1333 CLR(lt2->l_pflag, LP_SINGLESTEP); 1334 } 1335 CLR(lt->l_pflag, LP_SINGLESTEP); 1336 } 1337 sendsig: 1338 error = ptrace_sendsig(l, req, t, lt, signo, resume_all); 1339 break; 1340 1341 case PT_SYSCALLEMU: 1342 if (!ISSET(t->p_slflag, PSL_SYSCALL) || t->p_stat != SSTOP) { 1343 error = EINVAL; 1344 break; 1345 } 1346 SET(t->p_slflag, PSL_SYSCALLEMU); 1347 break; 1348 1349 #ifdef PT_STEP 1350 case PT_SETSTEP: 1351 write = 1; 1352 1353 /* FALLTHROUGH */ 1354 case PT_CLEARSTEP: 1355 /* write = 0 done above. */ 1356 if ((error = ptrace_update_lwp(t, <, data)) != 0) 1357 break; 1358 1359 if (write) 1360 SET(lt->l_pflag, LP_SINGLESTEP); 1361 else 1362 CLR(lt->l_pflag, LP_SINGLESTEP); 1363 break; 1364 #endif 1365 1366 case PT_KILL: 1367 /* just send the process a KILL signal. */ 1368 signo = SIGKILL; 1369 goto sendsig; /* in PT_CONTINUE, above. */ 1370 1371 case PT_STOP: 1372 /* just send the process a STOP signal. */ 1373 signo = SIGSTOP; 1374 goto sendsig; /* in PT_CONTINUE, above. */ 1375 1376 case PT_ATTACH: 1377 /* 1378 * Go ahead and set the trace flag. 1379 * Save the old parent (it's reset in 1380 * _DETACH, and also in kern_exit.c:wait4() 1381 * Reparent the process so that the tracing 1382 * proc gets to see all the action. 1383 * Stop the target. 1384 */ 1385 proc_changeparent(t, p); 1386 signo = SIGSTOP; 1387 goto sendsig; 1388 1389 case PT_GET_EVENT_MASK: 1390 error = ptrace_get_event_mask(t, addr, data); 1391 break; 1392 1393 case PT_SET_EVENT_MASK: 1394 error = ptrace_set_event_mask(t, addr, data); 1395 break; 1396 1397 case PT_GET_PROCESS_STATE: 1398 error = ptrace_get_process_state(t, addr, data); 1399 break; 1400 1401 case PT_LWPINFO: 1402 error = ptrace_lwpinfo(t, <, addr, data); 1403 break; 1404 1405 case PT_SET_SIGINFO: 1406 error = ptrace_set_siginfo(t, <, ptm, addr, data); 1407 break; 1408 1409 case PT_GET_SIGINFO: 1410 error = ptrace_get_siginfo(t, ptm, addr, data); 1411 break; 1412 1413 case PT_RESUME: 1414 case PT_SUSPEND: 1415 error = ptrace_startstop(t, <, req, addr, data); 1416 break; 1417 1418 #ifdef PT_REGISTERS 1419 case_PT_SETREGS 1420 case_PT_GETREGS 1421 case_PT_SETFPREGS 1422 case_PT_GETFPREGS 1423 case_PT_SETDBREGS 1424 case_PT_GETDBREGS 1425 error = ptrace_regs(l, <, req, ptm, addr, data); 1426 break; 1427 #endif 1428 1429 #ifdef __HAVE_PTRACE_MACHDEP 1430 PTRACE_MACHDEP_REQUEST_CASES 1431 error = ptrace_machdep_dorequest(l, lt, req, addr, data); 1432 break; 1433 #endif 1434 } 1435 1436 out: 1437 if (pheld) { 1438 mutex_exit(t->p_lock); 1439 mutex_exit(proc_lock); 1440 } 1441 if (lt != NULL) 1442 lwp_delref(lt); 1443 if (locked) 1444 rw_exit(&t->p_reflock); 1445 1446 return error; 1447 } 1448 1449 typedef int (*regrfunc_t)(struct lwp *, void *, size_t *); 1450 typedef int (*regwfunc_t)(struct lwp *, void *, size_t); 1451 1452 #ifdef PT_REGISTERS 1453 static int 1454 proc_regio(struct lwp *l, struct uio *uio, size_t ks, regrfunc_t r, 1455 regwfunc_t w) 1456 { 1457 char buf[1024]; 1458 int error; 1459 char *kv; 1460 size_t kl; 1461 1462 if (ks > sizeof(buf)) 1463 return E2BIG; 1464 1465 if (uio->uio_offset < 0 || uio->uio_offset > (off_t)ks) 1466 return EINVAL; 1467 1468 kv = buf + uio->uio_offset; 1469 kl = ks - uio->uio_offset; 1470 1471 if (kl > uio->uio_resid) 1472 kl = uio->uio_resid; 1473 1474 error = (*r)(l, buf, &ks); 1475 if (error == 0) 1476 error = uiomove(kv, kl, uio); 1477 if (error == 0 && uio->uio_rw == UIO_WRITE) { 1478 if (l->l_stat != LSSTOP) 1479 error = EBUSY; 1480 else 1481 error = (*w)(l, buf, ks); 1482 } 1483 1484 uio->uio_offset = 0; 1485 return error; 1486 } 1487 #endif 1488 1489 int 1490 process_doregs(struct lwp *curl /*tracer*/, 1491 struct lwp *l /*traced*/, 1492 struct uio *uio) 1493 { 1494 #if defined(PT_GETREGS) || defined(PT_SETREGS) 1495 size_t s; 1496 regrfunc_t r; 1497 regwfunc_t w; 1498 1499 #ifdef COMPAT_NETBSD32 1500 const bool pk32 = (curl->l_proc->p_flag & PK_32) != 0; 1501 1502 if (__predict_false(pk32)) { 1503 if ((l->l_proc->p_flag & PK_32) == 0) { 1504 // 32 bit tracer can't trace 64 bit process 1505 return EINVAL; 1506 } 1507 s = sizeof(process_reg32); 1508 r = __FPTRCAST(regrfunc_t, process_read_regs32); 1509 w = __FPTRCAST(regwfunc_t, process_write_regs32); 1510 } else 1511 #endif 1512 { 1513 s = sizeof(struct reg); 1514 r = __FPTRCAST(regrfunc_t, process_read_regs); 1515 w = __FPTRCAST(regwfunc_t, process_write_regs); 1516 } 1517 return proc_regio(l, uio, s, r, w); 1518 #else 1519 return EINVAL; 1520 #endif 1521 } 1522 1523 int 1524 process_validregs(struct lwp *l) 1525 { 1526 1527 #if defined(PT_SETREGS) || defined(PT_GETREGS) 1528 return (l->l_flag & LW_SYSTEM) == 0; 1529 #else 1530 return 0; 1531 #endif 1532 } 1533 1534 int 1535 process_dofpregs(struct lwp *curl /*tracer*/, 1536 struct lwp *l /*traced*/, 1537 struct uio *uio) 1538 { 1539 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS) 1540 size_t s; 1541 regrfunc_t r; 1542 regwfunc_t w; 1543 1544 #ifdef COMPAT_NETBSD32 1545 const bool pk32 = (curl->l_proc->p_flag & PK_32) != 0; 1546 1547 if (__predict_false(pk32)) { 1548 if ((l->l_proc->p_flag & PK_32) == 0) { 1549 // 32 bit tracer can't trace 64 bit process 1550 return EINVAL; 1551 } 1552 s = sizeof(process_fpreg32); 1553 r = (regrfunc_t)process_read_fpregs32; 1554 w = (regwfunc_t)process_write_fpregs32; 1555 } else 1556 #endif 1557 { 1558 s = sizeof(struct fpreg); 1559 r = (regrfunc_t)process_read_fpregs; 1560 w = (regwfunc_t)process_write_fpregs; 1561 } 1562 return proc_regio(l, uio, s, r, w); 1563 #else 1564 return EINVAL; 1565 #endif 1566 } 1567 1568 int 1569 process_validfpregs(struct lwp *l) 1570 { 1571 1572 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS) 1573 return (l->l_flag & LW_SYSTEM) == 0; 1574 #else 1575 return 0; 1576 #endif 1577 } 1578 1579 int 1580 process_dodbregs(struct lwp *curl /*tracer*/, 1581 struct lwp *l /*traced*/, 1582 struct uio *uio) 1583 { 1584 #if defined(PT_GETDBREGS) || defined(PT_SETDBREGS) 1585 size_t s; 1586 regrfunc_t r; 1587 regwfunc_t w; 1588 1589 #ifdef COMPAT_NETBSD32 1590 const bool pk32 = (curl->l_proc->p_flag & PK_32) != 0; 1591 1592 if (__predict_false(pk32)) { 1593 if ((l->l_proc->p_flag & PK_32) == 0) { 1594 // 32 bit tracer can't trace 64 bit process 1595 return EINVAL; 1596 } 1597 s = sizeof(process_dbreg32); 1598 r = (regrfunc_t)process_read_dbregs32; 1599 w = (regwfunc_t)process_write_dbregs32; 1600 } else 1601 #endif 1602 { 1603 s = sizeof(struct dbreg); 1604 r = (regrfunc_t)process_read_dbregs; 1605 w = (regwfunc_t)process_write_dbregs; 1606 } 1607 return proc_regio(l, uio, s, r, w); 1608 #else 1609 return EINVAL; 1610 #endif 1611 } 1612 1613 int 1614 process_validdbregs(struct lwp *l) 1615 { 1616 1617 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS) 1618 return (l->l_flag & LW_SYSTEM) == 0; 1619 #else 1620 return 0; 1621 #endif 1622 } 1623 1624 static int 1625 process_auxv_offset(struct proc *p, struct uio *uio) 1626 { 1627 struct ps_strings pss; 1628 int error; 1629 off_t off = (off_t)p->p_psstrp; 1630 1631 if ((error = copyin_psstrings(p, &pss)) != 0) 1632 return error; 1633 1634 if (pss.ps_envstr == NULL) 1635 return EIO; 1636 1637 uio->uio_offset += (off_t)(vaddr_t)(pss.ps_envstr + pss.ps_nenvstr + 1); 1638 #ifdef __MACHINE_STACK_GROWS_UP 1639 if (uio->uio_offset < off) 1640 return EIO; 1641 #else 1642 if (uio->uio_offset > off) 1643 return EIO; 1644 if ((uio->uio_offset + uio->uio_resid) > off) 1645 uio->uio_resid = off - uio->uio_offset; 1646 #endif 1647 return 0; 1648 } 1649 #endif /* PTRACE */ 1650 1651 MODULE(MODULE_CLASS_EXEC, ptrace_common, NULL); 1652 1653 static int 1654 ptrace_common_modcmd(modcmd_t cmd, void *arg) 1655 { 1656 int error; 1657 1658 switch (cmd) { 1659 case MODULE_CMD_INIT: 1660 error = ptrace_init(); 1661 break; 1662 case MODULE_CMD_FINI: 1663 error = ptrace_fini(); 1664 break; 1665 default: 1666 ptrace_hooks(); 1667 error = ENOTTY; 1668 break; 1669 } 1670 return error; 1671 } 1672