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