1 /* $NetBSD: sys_ptrace_common.c,v 1.36 2018/04/08 14:46:32 kamil 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.36 2018/04/08 14:46:32 kamil 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 158 # ifdef DEBUG 159 # define DPRINTF(a) uprintf a 160 # else 161 # define DPRINTF(a) 162 # endif 163 164 static kauth_listener_t ptrace_listener; 165 static int process_auxv_offset(struct proc *, struct uio *); 166 167 #if 0 168 static int ptrace_cbref; 169 static kmutex_t ptrace_mtx; 170 static kcondvar_t ptrace_cv; 171 #endif 172 173 #ifdef PT_GETREGS 174 # define case_PT_GETREGS case PT_GETREGS: 175 #else 176 # define case_PT_GETREGS 177 #endif 178 179 #ifdef PT_SETREGS 180 # define case_PT_SETREGS case PT_SETREGS: 181 #else 182 # define case_PT_SETREGS 183 #endif 184 185 #ifdef PT_GETFPREGS 186 # define case_PT_GETFPREGS case PT_GETFPREGS: 187 #else 188 # define case_PT_GETFPREGS 189 #endif 190 191 #ifdef PT_SETFPREGS 192 # define case_PT_SETFPREGS case PT_SETFPREGS: 193 #else 194 # define case_PT_SETFPREGS 195 #endif 196 197 #ifdef PT_GETDBREGS 198 # define case_PT_GETDBREGS case PT_GETDBREGS: 199 #else 200 # define case_PT_GETDBREGS 201 #endif 202 203 #ifdef PT_SETDBREGS 204 # define case_PT_SETDBREGS case PT_SETDBREGS: 205 #else 206 # define case_PT_SETDBREGS 207 #endif 208 209 #if defined(PT_SETREGS) || defined(PT_GETREGS) || \ 210 defined(PT_SETFPREGS) || defined(PT_GETFOREGS) || \ 211 defined(PT_SETDBREGS) || defined(PT_GETDBREGS) 212 # define PT_REGISTERS 213 #endif 214 215 static int 216 ptrace_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie, 217 void *arg0, void *arg1, void *arg2, void *arg3) 218 { 219 struct proc *p; 220 int result; 221 #ifdef PT_SETDBREGS 222 extern int user_set_dbregs; 223 #endif 224 225 result = KAUTH_RESULT_DEFER; 226 p = arg0; 227 228 #if 0 229 mutex_enter(&ptrace_mtx); 230 ptrace_cbref++; 231 mutex_exit(&ptrace_mtx); 232 #endif 233 if (action != KAUTH_PROCESS_PTRACE) 234 goto out; 235 236 switch ((u_long)arg1) { 237 #ifdef PT_SETDBREGS 238 case_PT_SETDBREGS 239 if (kauth_cred_getuid(cred) != 0 && user_set_dbregs == 0) { 240 result = KAUTH_RESULT_DENY; 241 break; 242 } 243 #endif 244 case PT_TRACE_ME: 245 case PT_ATTACH: 246 case PT_WRITE_I: 247 case PT_WRITE_D: 248 case PT_READ_I: 249 case PT_READ_D: 250 case PT_IO: 251 case_PT_GETREGS 252 case_PT_SETREGS 253 case_PT_GETFPREGS 254 case_PT_SETFPREGS 255 case_PT_GETDBREGS 256 case PT_SET_EVENT_MASK: 257 case PT_GET_EVENT_MASK: 258 case PT_GET_PROCESS_STATE: 259 case PT_SET_SIGINFO: 260 case PT_GET_SIGINFO: 261 #ifdef __HAVE_PTRACE_MACHDEP 262 PTRACE_MACHDEP_REQUEST_CASES 263 #endif 264 if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) || 265 ISSET(p->p_flag, PK_SUGID)) { 266 break; 267 } 268 269 result = KAUTH_RESULT_ALLOW; 270 271 break; 272 273 #ifdef PT_STEP 274 case PT_STEP: 275 case PT_SETSTEP: 276 case PT_CLEARSTEP: 277 #endif 278 case PT_CONTINUE: 279 case PT_KILL: 280 case PT_DETACH: 281 case PT_LWPINFO: 282 case PT_SYSCALL: 283 case PT_SYSCALLEMU: 284 case PT_DUMPCORE: 285 case PT_RESUME: 286 case PT_SUSPEND: 287 result = KAUTH_RESULT_ALLOW; 288 break; 289 290 default: 291 break; 292 } 293 294 out: 295 #if 0 296 mutex_enter(&ptrace_mtx); 297 if (--ptrace_cbref == 0) 298 cv_broadcast(&ptrace_cv); 299 mutex_exit(&ptrace_mtx); 300 #endif 301 302 return result; 303 } 304 305 int 306 ptrace_init(void) 307 { 308 309 #if 0 310 mutex_init(&ptrace_mtx, MUTEX_DEFAULT, IPL_NONE); 311 cv_init(&ptrace_cv, "ptracecb"); 312 ptrace_cbref = 0; 313 #endif 314 ptrace_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS, 315 ptrace_listener_cb, NULL); 316 return 0; 317 } 318 319 int 320 ptrace_fini(void) 321 { 322 323 kauth_unlisten_scope(ptrace_listener); 324 325 #if 0 326 /* Make sure no-one is executing our kauth listener */ 327 328 mutex_enter(&ptrace_mtx); 329 while (ptrace_cbref != 0) 330 cv_wait(&ptrace_cv, &ptrace_mtx); 331 mutex_exit(&ptrace_mtx); 332 mutex_destroy(&ptrace_mtx); 333 cv_destroy(&ptrace_cv); 334 #endif 335 336 return 0; 337 } 338 339 static struct proc * 340 ptrace_find(struct lwp *l, int req, pid_t pid) 341 { 342 struct proc *t; 343 344 /* "A foolish consistency..." XXX */ 345 if (req == PT_TRACE_ME) { 346 t = l->l_proc; 347 mutex_enter(t->p_lock); 348 return t; 349 } 350 351 /* Find the process we're supposed to be operating on. */ 352 t = proc_find(pid); 353 if (t == NULL) 354 return NULL; 355 356 /* XXX-elad */ 357 mutex_enter(t->p_lock); 358 int error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, 359 t, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL); 360 if (error) { 361 mutex_exit(t->p_lock); 362 return NULL; 363 } 364 return t; 365 } 366 367 static int 368 ptrace_allowed(struct lwp *l, int req, struct proc *t, struct proc *p) 369 { 370 /* 371 * Grab a reference on the process to prevent it from execing or 372 * exiting. 373 */ 374 if (!rw_tryenter(&t->p_reflock, RW_READER)) 375 return EBUSY; 376 377 /* Make sure we can operate on it. */ 378 switch (req) { 379 case PT_TRACE_ME: 380 /* Saying that you're being traced is always legal. */ 381 return 0; 382 383 case PT_ATTACH: 384 /* 385 * You can't attach to a process if: 386 * (1) it's the process that's doing the attaching, 387 */ 388 if (t->p_pid == p->p_pid) 389 return EINVAL; 390 391 /* 392 * (2) it's a system process 393 */ 394 if (t->p_flag & PK_SYSTEM) 395 return EPERM; 396 397 /* 398 * (3) it's already being traced, or 399 */ 400 if (ISSET(t->p_slflag, PSL_TRACED)) 401 return EBUSY; 402 403 /* 404 * (4) the tracer is chrooted, and its root directory is 405 * not at or above the root directory of the tracee 406 */ 407 mutex_exit(t->p_lock); /* XXXSMP */ 408 int tmp = proc_isunder(t, l); 409 mutex_enter(t->p_lock); /* XXXSMP */ 410 if (!tmp) 411 return EPERM; 412 return 0; 413 414 case PT_READ_I: 415 case PT_READ_D: 416 case PT_WRITE_I: 417 case PT_WRITE_D: 418 case PT_IO: 419 case PT_SET_SIGINFO: 420 case PT_GET_SIGINFO: 421 case_PT_GETREGS 422 case_PT_SETREGS 423 case_PT_GETFPREGS 424 case_PT_SETFPREGS 425 case_PT_GETDBREGS 426 case_PT_SETDBREGS 427 #ifdef __HAVE_PTRACE_MACHDEP 428 PTRACE_MACHDEP_REQUEST_CASES 429 #endif 430 /* 431 * You can't read/write the memory or registers of a process 432 * if the tracer is chrooted, and its root directory is not at 433 * or above the root directory of the tracee. 434 */ 435 mutex_exit(t->p_lock); /* XXXSMP */ 436 tmp = proc_isunder(t, l); 437 mutex_enter(t->p_lock); /* XXXSMP */ 438 if (!tmp) 439 return EPERM; 440 /*FALLTHROUGH*/ 441 442 case PT_CONTINUE: 443 case PT_KILL: 444 case PT_DETACH: 445 case PT_LWPINFO: 446 case PT_SYSCALL: 447 case PT_SYSCALLEMU: 448 case PT_DUMPCORE: 449 #ifdef PT_STEP 450 case PT_STEP: 451 case PT_SETSTEP: 452 case PT_CLEARSTEP: 453 #endif 454 case PT_SET_EVENT_MASK: 455 case PT_GET_EVENT_MASK: 456 case PT_GET_PROCESS_STATE: 457 case PT_RESUME: 458 case PT_SUSPEND: 459 /* 460 * You can't do what you want to the process if: 461 * (1) It's not being traced at all, 462 */ 463 if (!ISSET(t->p_slflag, PSL_TRACED)) 464 return EPERM; 465 466 /* 467 * (2) it's not being traced by _you_, or 468 */ 469 if (t->p_pptr != p) { 470 DPRINTF(("parent %d != %d\n", t->p_pptr->p_pid, 471 p->p_pid)); 472 return EBUSY; 473 } 474 475 /* 476 * (3) it's not currently stopped. 477 */ 478 if (t->p_stat != SSTOP || !t->p_waited /* XXXSMP */) { 479 DPRINTF(("stat %d flag %d\n", t->p_stat, 480 !t->p_waited)); 481 return EBUSY; 482 } 483 return 0; 484 485 default: /* It was not a legal request. */ 486 return EINVAL; 487 } 488 } 489 490 static int 491 ptrace_needs_hold(int req) 492 { 493 switch (req) { 494 #ifdef PT_STEP 495 case PT_STEP: 496 #endif 497 case PT_CONTINUE: 498 case PT_DETACH: 499 case PT_KILL: 500 case PT_SYSCALL: 501 case PT_SYSCALLEMU: 502 case PT_ATTACH: 503 case PT_TRACE_ME: 504 case PT_GET_SIGINFO: 505 case PT_SET_SIGINFO: 506 return 1; 507 default: 508 return 0; 509 } 510 } 511 512 static int 513 ptrace_update_lwp(struct proc *t, struct lwp **lt, lwpid_t lid) 514 { 515 if (lid == 0 || lid == (*lt)->l_lid || t->p_nlwps == 1) 516 return 0; 517 518 lwp_delref(*lt); 519 520 mutex_enter(t->p_lock); 521 *lt = lwp_find(t, lid); 522 if (*lt == NULL) { 523 mutex_exit(t->p_lock); 524 return ESRCH; 525 } 526 527 if ((*lt)->l_flag & LW_SYSTEM) { 528 *lt = NULL; 529 return EINVAL; 530 } 531 532 lwp_addref(*lt); 533 mutex_exit(t->p_lock); 534 535 return 0; 536 } 537 538 static int 539 ptrace_get_siginfo(struct proc *t, struct ptrace_methods *ptm, void *addr, 540 size_t data) 541 { 542 struct ptrace_siginfo psi; 543 544 psi.psi_siginfo._info = t->p_sigctx.ps_info; 545 psi.psi_lwpid = t->p_sigctx.ps_lwp; 546 547 return ptm->ptm_copyout_siginfo(&psi, addr, data); 548 } 549 550 static int 551 ptrace_set_siginfo(struct proc *t, struct lwp **lt, struct ptrace_methods *ptm, 552 void *addr, size_t data) 553 { 554 struct ptrace_siginfo psi; 555 556 int error = ptm->ptm_copyin_siginfo(&psi, addr, data); 557 if (error) 558 return error; 559 560 /* Check that the data is a valid signal number or zero. */ 561 if (psi.psi_siginfo.si_signo < 0 || psi.psi_siginfo.si_signo >= NSIG) 562 return EINVAL; 563 564 if ((error = ptrace_update_lwp(t, lt, psi.psi_lwpid)) != 0) 565 return error; 566 567 t->p_sigctx.ps_faked = true; 568 t->p_sigctx.ps_info = psi.psi_siginfo._info; 569 t->p_sigctx.ps_lwp = psi.psi_lwpid; 570 return 0; 571 } 572 573 static int 574 ptrace_get_event_mask(struct proc *t, void *addr, size_t data) 575 { 576 struct ptrace_event pe; 577 578 if (data != sizeof(pe)) { 579 DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(pe))); 580 return EINVAL; 581 } 582 memset(&pe, 0, sizeof(pe)); 583 pe.pe_set_event = ISSET(t->p_slflag, PSL_TRACEFORK) ? 584 PTRACE_FORK : 0; 585 pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK) ? 586 PTRACE_VFORK : 0; 587 pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK_DONE) ? 588 PTRACE_VFORK_DONE : 0; 589 pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_CREATE) ? 590 PTRACE_LWP_CREATE : 0; 591 pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_EXIT) ? 592 PTRACE_LWP_EXIT : 0; 593 return copyout(&pe, addr, sizeof(pe)); 594 } 595 596 static int 597 ptrace_set_event_mask(struct proc *t, void *addr, size_t data) 598 { 599 struct ptrace_event pe; 600 int error; 601 602 if (data != sizeof(pe)) { 603 DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(pe))); 604 return EINVAL; 605 } 606 if ((error = copyin(addr, &pe, sizeof(pe))) != 0) 607 return error; 608 609 if (pe.pe_set_event & PTRACE_FORK) 610 SET(t->p_slflag, PSL_TRACEFORK); 611 else 612 CLR(t->p_slflag, PSL_TRACEFORK); 613 #if notyet 614 if (pe.pe_set_event & PTRACE_VFORK) 615 SET(t->p_slflag, PSL_TRACEVFORK); 616 else 617 CLR(t->p_slflag, PSL_TRACEVFORK); 618 #else 619 if (pe.pe_set_event & PTRACE_VFORK) 620 return ENOTSUP; 621 #endif 622 if (pe.pe_set_event & PTRACE_VFORK_DONE) 623 SET(t->p_slflag, PSL_TRACEVFORK_DONE); 624 else 625 CLR(t->p_slflag, PSL_TRACEVFORK_DONE); 626 if (pe.pe_set_event & PTRACE_LWP_CREATE) 627 SET(t->p_slflag, PSL_TRACELWP_CREATE); 628 else 629 CLR(t->p_slflag, PSL_TRACELWP_CREATE); 630 if (pe.pe_set_event & PTRACE_LWP_EXIT) 631 SET(t->p_slflag, PSL_TRACELWP_EXIT); 632 else 633 CLR(t->p_slflag, PSL_TRACELWP_EXIT); 634 return 0; 635 } 636 637 static int 638 ptrace_get_process_state(struct proc *t, void *addr, size_t data) 639 { 640 struct ptrace_state ps; 641 642 if (data != sizeof(ps)) { 643 DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(ps))); 644 return EINVAL; 645 } 646 memset(&ps, 0, sizeof(ps)); 647 648 if (t->p_fpid) { 649 ps.pe_report_event = PTRACE_FORK; 650 ps.pe_other_pid = t->p_fpid; 651 } else if (t->p_vfpid) { 652 ps.pe_report_event = PTRACE_VFORK; 653 ps.pe_other_pid = t->p_vfpid; 654 } else if (t->p_vfpid_done) { 655 ps.pe_report_event = PTRACE_VFORK_DONE; 656 ps.pe_other_pid = t->p_vfpid_done; 657 } else if (t->p_lwp_created) { 658 ps.pe_report_event = PTRACE_LWP_CREATE; 659 ps.pe_lwp = t->p_lwp_created; 660 } else if (t->p_lwp_exited) { 661 ps.pe_report_event = PTRACE_LWP_EXIT; 662 ps.pe_lwp = t->p_lwp_exited; 663 } 664 return copyout(&ps, addr, sizeof(ps)); 665 } 666 667 static int 668 ptrace_lwpinfo(struct proc *t, struct lwp **lt, void *addr, size_t data) 669 { 670 struct ptrace_lwpinfo pl; 671 672 if (data != sizeof(pl)) { 673 DPRINTF(("%s: %zu != %zu\n", __func__, data, sizeof(pl))); 674 return EINVAL; 675 } 676 int error = copyin(addr, &pl, sizeof(pl)); 677 if (error) 678 return error; 679 680 lwpid_t tmp = pl.pl_lwpid; 681 lwp_delref(*lt); 682 mutex_enter(t->p_lock); 683 if (tmp == 0) 684 *lt = lwp_find_first(t); 685 else { 686 *lt = lwp_find(t, tmp); 687 if (*lt == NULL) { 688 mutex_exit(t->p_lock); 689 return ESRCH; 690 } 691 *lt = LIST_NEXT(*lt, l_sibling); 692 } 693 694 while (*lt != NULL && !lwp_alive(*lt)) 695 *lt = LIST_NEXT(*lt, l_sibling); 696 697 pl.pl_lwpid = 0; 698 pl.pl_event = 0; 699 if (*lt) { 700 lwp_addref(*lt); 701 pl.pl_lwpid = (*lt)->l_lid; 702 703 if ((*lt)->l_flag & LW_WSUSPEND) 704 pl.pl_event = PL_EVENT_SUSPENDED; 705 /* 706 * If we match the lwp, or it was sent to every lwp, 707 * we set PL_EVENT_SIGNAL. 708 * XXX: ps_lwp == 0 means everyone and noone, so 709 * check ps_signo too. 710 */ 711 else if ((*lt)->l_lid == t->p_sigctx.ps_lwp 712 || (t->p_sigctx.ps_lwp == 0 && 713 t->p_sigctx.ps_info._signo)) 714 pl.pl_event = PL_EVENT_SIGNAL; 715 } 716 mutex_exit(t->p_lock); 717 718 return copyout(&pl, addr, sizeof(pl)); 719 } 720 721 static int 722 ptrace_startstop(struct proc *t, struct lwp **lt, int rq, void *addr, 723 size_t data) 724 { 725 int error; 726 727 if ((error = ptrace_update_lwp(t, lt, data)) != 0) 728 return error; 729 730 lwp_lock(*lt); 731 if (rq == PT_SUSPEND) 732 (*lt)->l_flag |= LW_WSUSPEND; 733 else 734 (*lt)->l_flag &= ~LW_WSUSPEND; 735 lwp_unlock(*lt); 736 return 0; 737 } 738 739 #ifdef PT_REGISTERS 740 static int 741 ptrace_uio_dir(int req) 742 { 743 switch (req) { 744 case_PT_GETREGS 745 case_PT_GETFPREGS 746 case_PT_GETDBREGS 747 return UIO_READ; 748 case_PT_SETREGS 749 case_PT_SETFPREGS 750 case_PT_SETDBREGS 751 return UIO_WRITE; 752 default: 753 return -1; 754 } 755 } 756 757 static int 758 ptrace_regs(struct lwp *l, struct lwp **lt, int rq, struct ptrace_methods *ptm, 759 void *addr, size_t data) 760 { 761 int error; 762 struct proc *t = (*lt)->l_proc; 763 struct vmspace *vm; 764 765 if ((error = ptrace_update_lwp(t, lt, data)) != 0) 766 return error; 767 768 int dir = ptrace_uio_dir(rq); 769 size_t size; 770 int (*func)(struct lwp *, struct lwp *, struct uio *); 771 772 switch (rq) { 773 #if defined(PT_SETREGS) || defined(PT_GETREGS) 774 case_PT_GETREGS 775 case_PT_SETREGS 776 if (!process_validregs(*lt)) 777 return EINVAL; 778 size = PROC_REGSZ(t); 779 func = ptm->ptm_doregs; 780 break; 781 #endif 782 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS) 783 case_PT_GETFPREGS 784 case_PT_SETFPREGS 785 if (!process_validfpregs(*lt)) 786 return EINVAL; 787 size = PROC_FPREGSZ(t); 788 func = ptm->ptm_dofpregs; 789 break; 790 #endif 791 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS) 792 case_PT_GETDBREGS 793 case_PT_SETDBREGS 794 if (!process_validdbregs(*lt)) 795 return EINVAL; 796 size = PROC_DBREGSZ(t); 797 func = ptm->ptm_dodbregs; 798 break; 799 #endif 800 default: 801 return EINVAL; 802 } 803 804 error = proc_vmspace_getref(l->l_proc, &vm); 805 if (error) 806 return error; 807 808 struct uio uio; 809 struct iovec iov; 810 811 iov.iov_base = addr; 812 iov.iov_len = size; 813 uio.uio_iov = &iov; 814 uio.uio_iovcnt = 1; 815 uio.uio_offset = 0; 816 uio.uio_resid = iov.iov_len; 817 uio.uio_rw = dir; 818 uio.uio_vmspace = vm; 819 820 error = (*func)(l, *lt, &uio); 821 uvmspace_free(vm); 822 return error; 823 } 824 #endif 825 826 static int 827 ptrace_sendsig(struct proc *t, struct lwp *lt, int signo, int resume_all) 828 { 829 ksiginfo_t ksi; 830 831 t->p_fpid = 0; 832 t->p_vfpid = 0; 833 t->p_vfpid_done = 0; 834 t->p_lwp_created = 0; 835 t->p_lwp_exited = 0; 836 837 /* Finally, deliver the requested signal (or none). */ 838 if (t->p_stat == SSTOP) { 839 /* 840 * Unstop the process. If it needs to take a 841 * signal, make all efforts to ensure that at 842 * an LWP runs to see it. 843 */ 844 t->p_xsig = signo; 845 if (resume_all) 846 proc_unstop(t); 847 else 848 lwp_unstop(lt); 849 return 0; 850 } 851 852 KSI_INIT_EMPTY(&ksi); 853 if (t->p_sigctx.ps_faked) { 854 if (signo != t->p_sigctx.ps_info._signo) 855 return EINVAL; 856 t->p_sigctx.ps_faked = false; 857 ksi.ksi_info = t->p_sigctx.ps_info; 858 ksi.ksi_lid = t->p_sigctx.ps_lwp; 859 } else if (signo == 0) { 860 return 0; 861 } else { 862 ksi.ksi_signo = signo; 863 } 864 865 kpsignal2(t, &ksi); 866 return 0; 867 } 868 869 static int 870 ptrace_dumpcore(struct lwp *lt, char *path, size_t len) 871 { 872 int error; 873 if (path != NULL) { 874 875 if (len >= MAXPATHLEN) 876 return EINVAL; 877 878 char *src = path; 879 path = kmem_alloc(len + 1, KM_SLEEP); 880 error = copyin(src, path, len); 881 if (error) 882 goto out; 883 path[len] = '\0'; 884 } 885 error = (*coredump_vec)(lt, path); 886 out: 887 if (path) 888 kmem_free(path, len + 1); 889 return error; 890 } 891 892 static int 893 ptrace_doio(struct lwp *l, struct proc *t, struct lwp *lt, 894 struct ptrace_io_desc *piod, void *addr, bool sysspace) 895 { 896 struct uio uio; 897 struct iovec iov; 898 int error, tmp; 899 900 error = 0; 901 iov.iov_base = piod->piod_addr; 902 iov.iov_len = piod->piod_len; 903 uio.uio_iov = &iov; 904 uio.uio_iovcnt = 1; 905 uio.uio_offset = (off_t)(unsigned long)piod->piod_offs; 906 uio.uio_resid = piod->piod_len; 907 908 switch (piod->piod_op) { 909 case PIOD_READ_D: 910 case PIOD_READ_I: 911 uio.uio_rw = UIO_READ; 912 break; 913 case PIOD_WRITE_D: 914 case PIOD_WRITE_I: 915 /* 916 * Can't write to a RAS 917 */ 918 if (ras_lookup(t, addr) != (void *)-1) { 919 return EACCES; 920 } 921 uio.uio_rw = UIO_WRITE; 922 break; 923 case PIOD_READ_AUXV: 924 uio.uio_rw = UIO_READ; 925 tmp = t->p_execsw->es_arglen; 926 if (uio.uio_offset > tmp) 927 return EIO; 928 if (uio.uio_resid > tmp - uio.uio_offset) 929 uio.uio_resid = tmp - uio.uio_offset; 930 piod->piod_len = iov.iov_len = uio.uio_resid; 931 error = process_auxv_offset(t, &uio); 932 break; 933 default: 934 error = EINVAL; 935 break; 936 } 937 938 if (error) 939 return error; 940 941 if (sysspace) { 942 uio.uio_vmspace = vmspace_kernel(); 943 } else { 944 error = proc_vmspace_getref(l->l_proc, &uio.uio_vmspace); 945 if (error) 946 return error; 947 } 948 949 error = process_domem(l, lt, &uio); 950 if (!sysspace) 951 uvmspace_free(uio.uio_vmspace); 952 if (error) 953 return error; 954 piod->piod_len -= uio.uio_resid; 955 return 0; 956 } 957 958 int 959 do_ptrace(struct ptrace_methods *ptm, struct lwp *l, int req, pid_t pid, 960 void *addr, int data, register_t *retval) 961 { 962 struct proc *p = l->l_proc; 963 struct lwp *lt = NULL; 964 struct lwp *lt2; 965 struct proc *t; /* target process */ 966 struct ptrace_io_desc piod; 967 int error, write, tmp, pheld; 968 int signo = 0; 969 int resume_all; 970 error = 0; 971 972 /* 973 * If attaching or detaching, we need to get a write hold on the 974 * proclist lock so that we can re-parent the target process. 975 */ 976 mutex_enter(proc_lock); 977 978 t = ptrace_find(l, req, pid); 979 if (t == NULL) { 980 mutex_exit(proc_lock); 981 return ESRCH; 982 } 983 984 pheld = 1; 985 if ((error = ptrace_allowed(l, req, t, p)) != 0) 986 goto out; 987 988 if ((error = kauth_authorize_process(l->l_cred, 989 KAUTH_PROCESS_PTRACE, t, KAUTH_ARG(req), NULL, NULL)) != 0) 990 goto out; 991 992 if ((lt = lwp_find_first(t)) == NULL) { 993 error = ESRCH; 994 goto out; 995 } 996 997 /* Do single-step fixup if needed. */ 998 FIX_SSTEP(t); 999 KASSERT(lt != NULL); 1000 lwp_addref(lt); 1001 1002 /* 1003 * Which locks do we need held? XXX Ugly. 1004 */ 1005 if ((pheld = ptrace_needs_hold(req)) == 0) { 1006 mutex_exit(t->p_lock); 1007 mutex_exit(proc_lock); 1008 } 1009 1010 /* Now do the operation. */ 1011 write = 0; 1012 *retval = 0; 1013 tmp = 0; 1014 resume_all = 1; 1015 1016 switch (req) { 1017 case PT_TRACE_ME: 1018 /* Just set the trace flag. */ 1019 SET(t->p_slflag, PSL_TRACED); 1020 t->p_opptr = t->p_pptr; 1021 break; 1022 1023 case PT_WRITE_I: /* XXX no separate I and D spaces */ 1024 case PT_WRITE_D: 1025 write = 1; 1026 tmp = data; 1027 /* FALLTHROUGH */ 1028 case PT_READ_I: /* XXX no separate I and D spaces */ 1029 case PT_READ_D: 1030 piod.piod_addr = &tmp; 1031 piod.piod_len = sizeof(tmp); 1032 piod.piod_offs = addr; 1033 piod.piod_op = write ? PIOD_WRITE_D : PIOD_READ_D; 1034 if ((error = ptrace_doio(l, t, lt, &piod, addr, true)) != 0) 1035 break; 1036 if (!write) 1037 *retval = tmp; 1038 break; 1039 1040 case PT_IO: 1041 if ((error = ptm->ptm_copyin_piod(&piod, addr, data)) != 0) 1042 break; 1043 if ((error = ptrace_doio(l, t, lt, &piod, addr, false)) != 0) 1044 break; 1045 error = ptm->ptm_copyout_piod(&piod, addr, data); 1046 break; 1047 1048 case PT_DUMPCORE: 1049 error = ptrace_dumpcore(lt, addr, data); 1050 break; 1051 1052 #ifdef PT_STEP 1053 case PT_STEP: 1054 /* 1055 * From the 4.4BSD PRM: 1056 * "Execution continues as in request PT_CONTINUE; however 1057 * as soon as possible after execution of at least one 1058 * instruction, execution stops again. [ ... ]" 1059 */ 1060 #endif 1061 case PT_CONTINUE: 1062 case PT_SYSCALL: 1063 case PT_DETACH: 1064 if (req == PT_SYSCALL) { 1065 if (!ISSET(t->p_slflag, PSL_SYSCALL)) { 1066 SET(t->p_slflag, PSL_SYSCALL); 1067 #ifdef __HAVE_SYSCALL_INTERN 1068 (*t->p_emul->e_syscall_intern)(t); 1069 #endif 1070 } 1071 } else { 1072 if (ISSET(t->p_slflag, PSL_SYSCALL)) { 1073 CLR(t->p_slflag, PSL_SYSCALL); 1074 #ifdef __HAVE_SYSCALL_INTERN 1075 (*t->p_emul->e_syscall_intern)(t); 1076 #endif 1077 } 1078 } 1079 t->p_trace_enabled = trace_is_enabled(t); 1080 1081 /* 1082 * Pick up the LWPID, if supplied. There are two cases: 1083 * data < 0 : step or continue single thread, lwp = -data 1084 * data > 0 in PT_STEP : step this thread, continue others 1085 * For operations other than PT_STEP, data > 0 means 1086 * data is the signo to deliver to the process. 1087 */ 1088 tmp = data; 1089 if (tmp >= 0) { 1090 #ifdef PT_STEP 1091 if (req == PT_STEP) 1092 signo = 0; 1093 else 1094 #endif 1095 { 1096 signo = tmp; 1097 tmp = 0; /* don't search for LWP */ 1098 } 1099 } else 1100 tmp = -tmp; 1101 1102 if (tmp > 0) { 1103 if (req == PT_DETACH) { 1104 error = EINVAL; 1105 break; 1106 } 1107 lwp_delref2 (lt); 1108 lt = lwp_find(t, tmp); 1109 if (lt == NULL) { 1110 error = ESRCH; 1111 break; 1112 } 1113 lwp_addref(lt); 1114 resume_all = 0; 1115 signo = 0; 1116 } 1117 1118 /* 1119 * From the 4.4BSD PRM: 1120 * "The data argument is taken as a signal number and the 1121 * child's execution continues at location addr as if it 1122 * incurred that signal. Normally the signal number will 1123 * be either 0 to indicate that the signal that caused the 1124 * stop should be ignored, or that value fetched out of 1125 * the process's image indicating which signal caused 1126 * the stop. If addr is (int *)1 then execution continues 1127 * from where it stopped." 1128 */ 1129 1130 /* Check that the data is a valid signal number or zero. */ 1131 if (signo < 0 || signo >= NSIG) { 1132 error = EINVAL; 1133 break; 1134 } 1135 1136 /* Prevent process deadlock */ 1137 if (resume_all) { 1138 #ifdef PT_STEP 1139 if (req == PT_STEP) { 1140 if (lt->l_flag & LW_WSUSPEND) { 1141 error = EDEADLK; 1142 break; 1143 } 1144 } else 1145 #endif 1146 { 1147 error = EDEADLK; 1148 LIST_FOREACH(lt2, &t->p_lwps, l_sibling) { 1149 if ((lt2->l_flag & LW_WSUSPEND) == 0) { 1150 error = 0; 1151 break; 1152 } 1153 } 1154 if (error != 0) 1155 break; 1156 } 1157 } else { 1158 if (lt->l_flag & LW_WSUSPEND) { 1159 error = EDEADLK; 1160 break; 1161 } 1162 } 1163 1164 /* If the address parameter is not (int *)1, set the pc. */ 1165 if ((int *)addr != (int *)1) { 1166 error = process_set_pc(lt, addr); 1167 if (error != 0) 1168 break; 1169 } 1170 #ifdef PT_STEP 1171 /* 1172 * Arrange for a single-step, if that's requested and possible. 1173 * More precisely, set the single step status as requested for 1174 * the requested thread, and clear it for other threads. 1175 */ 1176 LIST_FOREACH(lt2, &t->p_lwps, l_sibling) { 1177 if (ISSET(lt2->l_pflag, LP_SINGLESTEP)) { 1178 lwp_lock(lt2); 1179 process_sstep(lt2, 1); 1180 lwp_unlock(lt2); 1181 } else if (lt != lt2) { 1182 lwp_lock(lt2); 1183 process_sstep(lt2, 0); 1184 lwp_unlock(lt2); 1185 } 1186 } 1187 error = process_sstep(lt, 1188 ISSET(lt->l_pflag, LP_SINGLESTEP) || req == PT_STEP); 1189 if (error) 1190 break; 1191 #endif 1192 if (req == PT_DETACH) { 1193 CLR(t->p_slflag, PSL_TRACED|PSL_SYSCALL); 1194 1195 /* give process back to original parent or init */ 1196 if (t->p_opptr != t->p_pptr) { 1197 struct proc *pp = t->p_opptr; 1198 proc_reparent(t, pp ? pp : initproc); 1199 } 1200 1201 /* not being traced any more */ 1202 t->p_opptr = NULL; 1203 1204 /* clear single step */ 1205 LIST_FOREACH(lt2, &t->p_lwps, l_sibling) { 1206 CLR(lt2->l_pflag, LP_SINGLESTEP); 1207 } 1208 CLR(lt->l_pflag, LP_SINGLESTEP); 1209 } 1210 sendsig: 1211 error = ptrace_sendsig(t, lt, signo, resume_all); 1212 break; 1213 1214 case PT_SYSCALLEMU: 1215 if (!ISSET(t->p_slflag, PSL_SYSCALL) || t->p_stat != SSTOP) { 1216 error = EINVAL; 1217 break; 1218 } 1219 SET(t->p_slflag, PSL_SYSCALLEMU); 1220 break; 1221 1222 #ifdef PT_STEP 1223 case PT_SETSTEP: 1224 write = 1; 1225 1226 case PT_CLEARSTEP: 1227 /* write = 0 done above. */ 1228 if ((error = ptrace_update_lwp(t, <, data)) != 0) 1229 break; 1230 1231 if (write) 1232 SET(lt->l_pflag, LP_SINGLESTEP); 1233 else 1234 CLR(lt->l_pflag, LP_SINGLESTEP); 1235 break; 1236 #endif 1237 1238 case PT_KILL: 1239 /* just send the process a KILL signal. */ 1240 signo = SIGKILL; 1241 goto sendsig; /* in PT_CONTINUE, above. */ 1242 1243 case PT_ATTACH: 1244 /* 1245 * Go ahead and set the trace flag. 1246 * Save the old parent (it's reset in 1247 * _DETACH, and also in kern_exit.c:wait4() 1248 * Reparent the process so that the tracing 1249 * proc gets to see all the action. 1250 * Stop the target. 1251 */ 1252 proc_changeparent(t, p); 1253 signo = SIGSTOP; 1254 goto sendsig; 1255 1256 case PT_GET_EVENT_MASK: 1257 error = ptrace_get_event_mask(t, addr, data); 1258 break; 1259 1260 case PT_SET_EVENT_MASK: 1261 error = ptrace_set_event_mask(t, addr, data); 1262 break; 1263 1264 case PT_GET_PROCESS_STATE: 1265 error = ptrace_get_process_state(t, addr, data); 1266 break; 1267 1268 case PT_LWPINFO: 1269 error = ptrace_lwpinfo(t, <, addr, data); 1270 break; 1271 1272 case PT_SET_SIGINFO: 1273 error = ptrace_set_siginfo(t, <, ptm, addr, data); 1274 break; 1275 1276 case PT_GET_SIGINFO: 1277 error = ptrace_get_siginfo(t, ptm, addr, data); 1278 break; 1279 1280 case PT_RESUME: 1281 case PT_SUSPEND: 1282 error = ptrace_startstop(t, <, req, addr, data); 1283 break; 1284 1285 #ifdef PT_REGISTERS 1286 case_PT_SETREGS 1287 case_PT_GETREGS 1288 case_PT_SETFPREGS 1289 case_PT_GETFPREGS 1290 case_PT_SETDBREGS 1291 case_PT_GETDBREGS 1292 error = ptrace_regs(l, <, req, ptm, addr, data); 1293 break; 1294 #endif 1295 1296 #ifdef __HAVE_PTRACE_MACHDEP 1297 PTRACE_MACHDEP_REQUEST_CASES 1298 error = ptrace_machdep_dorequest(l, lt, req, addr, data); 1299 break; 1300 #endif 1301 } 1302 1303 out: 1304 if (pheld) { 1305 mutex_exit(t->p_lock); 1306 mutex_exit(proc_lock); 1307 } 1308 if (lt != NULL) 1309 lwp_delref(lt); 1310 rw_exit(&t->p_reflock); 1311 1312 return error; 1313 } 1314 1315 typedef int (*regrfunc_t)(struct lwp *, void *, size_t *); 1316 typedef int (*regwfunc_t)(struct lwp *, void *, size_t); 1317 1318 #ifdef PT_REGISTERS 1319 static int 1320 proc_regio(struct lwp *l, struct uio *uio, size_t ks, regrfunc_t r, 1321 regwfunc_t w) 1322 { 1323 char buf[1024]; 1324 int error; 1325 char *kv; 1326 size_t kl; 1327 1328 if (ks > sizeof(buf)) 1329 return E2BIG; 1330 1331 if (uio->uio_offset < 0 || uio->uio_offset > (off_t)ks) 1332 return EINVAL; 1333 1334 kv = buf + uio->uio_offset; 1335 kl = ks - uio->uio_offset; 1336 1337 if (kl > uio->uio_resid) 1338 kl = uio->uio_resid; 1339 1340 error = (*r)(l, buf, &ks); 1341 if (error == 0) 1342 error = uiomove(kv, kl, uio); 1343 if (error == 0 && uio->uio_rw == UIO_WRITE) { 1344 if (l->l_stat != LSSTOP) 1345 error = EBUSY; 1346 else 1347 error = (*w)(l, buf, ks); 1348 } 1349 1350 uio->uio_offset = 0; 1351 return error; 1352 } 1353 #endif 1354 1355 int 1356 process_doregs(struct lwp *curl /*tracer*/, 1357 struct lwp *l /*traced*/, 1358 struct uio *uio) 1359 { 1360 #if defined(PT_GETREGS) || defined(PT_SETREGS) 1361 size_t s; 1362 regrfunc_t r; 1363 regwfunc_t w; 1364 1365 #ifdef COMPAT_NETBSD32 1366 const bool pk32 = (l->l_proc->p_flag & PK_32) != 0; 1367 1368 if (__predict_false(pk32)) { 1369 s = sizeof(process_reg32); 1370 r = (regrfunc_t)process_read_regs32; 1371 w = (regwfunc_t)process_write_regs32; 1372 } else 1373 #endif 1374 { 1375 s = sizeof(struct reg); 1376 r = (regrfunc_t)process_read_regs; 1377 w = (regwfunc_t)process_write_regs; 1378 } 1379 return proc_regio(l, uio, s, r, w); 1380 #else 1381 return EINVAL; 1382 #endif 1383 } 1384 1385 int 1386 process_validregs(struct lwp *l) 1387 { 1388 1389 #if defined(PT_SETREGS) || defined(PT_GETREGS) 1390 return (l->l_flag & LW_SYSTEM) == 0; 1391 #else 1392 return 0; 1393 #endif 1394 } 1395 1396 int 1397 process_dofpregs(struct lwp *curl /*tracer*/, 1398 struct lwp *l /*traced*/, 1399 struct uio *uio) 1400 { 1401 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS) 1402 size_t s; 1403 regrfunc_t r; 1404 regwfunc_t w; 1405 1406 #ifdef COMPAT_NETBSD32 1407 const bool pk32 = (l->l_proc->p_flag & PK_32) != 0; 1408 1409 if (__predict_false(pk32)) { 1410 s = sizeof(process_fpreg32); 1411 r = (regrfunc_t)process_read_fpregs32; 1412 w = (regwfunc_t)process_write_fpregs32; 1413 } else 1414 #endif 1415 { 1416 s = sizeof(struct fpreg); 1417 r = (regrfunc_t)process_read_fpregs; 1418 w = (regwfunc_t)process_write_fpregs; 1419 } 1420 return proc_regio(l, uio, s, r, w); 1421 #else 1422 return EINVAL; 1423 #endif 1424 } 1425 1426 int 1427 process_validfpregs(struct lwp *l) 1428 { 1429 1430 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS) 1431 return (l->l_flag & LW_SYSTEM) == 0; 1432 #else 1433 return 0; 1434 #endif 1435 } 1436 1437 int 1438 process_dodbregs(struct lwp *curl /*tracer*/, 1439 struct lwp *l /*traced*/, 1440 struct uio *uio) 1441 { 1442 #if defined(PT_GETDBREGS) || defined(PT_SETDBREGS) 1443 size_t s; 1444 regrfunc_t r; 1445 regwfunc_t w; 1446 1447 #ifdef COMPAT_NETBSD32 1448 const bool pk32 = (l->l_proc->p_flag & PK_32) != 0; 1449 1450 if (__predict_false(pk32)) { 1451 s = sizeof(process_dbreg32); 1452 r = (regrfunc_t)process_read_dbregs32; 1453 w = (regwfunc_t)process_write_dbregs32; 1454 } else 1455 #endif 1456 { 1457 s = sizeof(struct dbreg); 1458 r = (regrfunc_t)process_read_dbregs; 1459 w = (regwfunc_t)process_write_dbregs; 1460 } 1461 return proc_regio(l, uio, s, r, w); 1462 #else 1463 return EINVAL; 1464 #endif 1465 } 1466 1467 int 1468 process_validdbregs(struct lwp *l) 1469 { 1470 1471 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS) 1472 return (l->l_flag & LW_SYSTEM) == 0; 1473 #else 1474 return 0; 1475 #endif 1476 } 1477 1478 static int 1479 process_auxv_offset(struct proc *p, struct uio *uio) 1480 { 1481 struct ps_strings pss; 1482 int error; 1483 off_t off = (off_t)p->p_psstrp; 1484 1485 if ((error = copyin_psstrings(p, &pss)) != 0) 1486 return error; 1487 1488 if (pss.ps_envstr == NULL) 1489 return EIO; 1490 1491 uio->uio_offset += (off_t)(vaddr_t)(pss.ps_envstr + pss.ps_nenvstr + 1); 1492 #ifdef __MACHINE_STACK_GROWS_UP 1493 if (uio->uio_offset < off) 1494 return EIO; 1495 #else 1496 if (uio->uio_offset > off) 1497 return EIO; 1498 if ((uio->uio_offset + uio->uio_resid) > off) 1499 uio->uio_resid = off - uio->uio_offset; 1500 #endif 1501 return 0; 1502 } 1503 #endif /* PTRACE */ 1504 1505 MODULE(MODULE_CLASS_EXEC, ptrace_common, ""); 1506 1507 static int 1508 ptrace_common_modcmd(modcmd_t cmd, void *arg) 1509 { 1510 int error; 1511 1512 switch (cmd) { 1513 case MODULE_CMD_INIT: 1514 error = ptrace_init(); 1515 break; 1516 case MODULE_CMD_FINI: 1517 error = ptrace_fini(); 1518 break; 1519 default: 1520 ptrace_hooks(); 1521 error = ENOTTY; 1522 break; 1523 } 1524 return error; 1525 } 1526