1 /* 2 * Copyright (c) 1994, Sean Eric Fagan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Sean Eric Fagan. 16 * 4. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $FreeBSD$ 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/sysproto.h> 37 #include <sys/proc.h> 38 #include <sys/vnode.h> 39 #include <sys/ptrace.h> 40 41 #include <machine/reg.h> 42 #include <vm/vm.h> 43 #include <sys/lock.h> 44 #include <vm/pmap.h> 45 #include <vm/vm_map.h> 46 #include <vm/vm_page.h> 47 48 #include <sys/user.h> 49 #include <miscfs/procfs/procfs.h> 50 51 /* use the equivalent procfs code */ 52 #if 0 53 static int 54 pread (struct proc *procp, unsigned int addr, unsigned int *retval) { 55 int rv; 56 vm_map_t map, tmap; 57 vm_object_t object; 58 vm_offset_t kva = 0; 59 int page_offset; /* offset into page */ 60 vm_offset_t pageno; /* page number */ 61 vm_map_entry_t out_entry; 62 vm_prot_t out_prot; 63 boolean_t wired; 64 vm_pindex_t pindex; 65 66 /* Map page into kernel space */ 67 68 map = &procp->p_vmspace->vm_map; 69 70 page_offset = addr - trunc_page(addr); 71 pageno = trunc_page(addr); 72 73 tmap = map; 74 rv = vm_map_lookup (&tmap, pageno, VM_PROT_READ, &out_entry, 75 &object, &pindex, &out_prot, &wired); 76 77 if (rv != KERN_SUCCESS) 78 return EINVAL; 79 80 vm_map_lookup_done (tmap, out_entry); 81 82 /* Find space in kernel_map for the page we're interested in */ 83 rv = vm_map_find (kernel_map, object, IDX_TO_OFF(pindex), 84 &kva, PAGE_SIZE, 0, VM_PROT_ALL, VM_PROT_ALL, 0); 85 86 if (!rv) { 87 vm_object_reference (object); 88 89 rv = vm_map_pageable (kernel_map, kva, kva + PAGE_SIZE, 0); 90 if (!rv) { 91 *retval = 0; 92 bcopy ((caddr_t)kva + page_offset, 93 retval, sizeof *retval); 94 } 95 vm_map_remove (kernel_map, kva, kva + PAGE_SIZE); 96 } 97 98 return rv; 99 } 100 101 static int 102 pwrite (struct proc *procp, unsigned int addr, unsigned int datum) { 103 int rv; 104 vm_map_t map, tmap; 105 vm_object_t object; 106 vm_offset_t kva = 0; 107 int page_offset; /* offset into page */ 108 vm_offset_t pageno; /* page number */ 109 vm_map_entry_t out_entry; 110 vm_prot_t out_prot; 111 boolean_t wired; 112 vm_pindex_t pindex; 113 boolean_t fix_prot = 0; 114 115 /* Map page into kernel space */ 116 117 map = &procp->p_vmspace->vm_map; 118 119 page_offset = addr - trunc_page(addr); 120 pageno = trunc_page(addr); 121 122 /* 123 * Check the permissions for the area we're interested in. 124 */ 125 126 if (vm_map_check_protection (map, pageno, pageno + PAGE_SIZE, 127 VM_PROT_WRITE) == FALSE) { 128 /* 129 * If the page was not writable, we make it so. 130 * XXX It is possible a page may *not* be read/executable, 131 * if a process changes that! 132 */ 133 fix_prot = 1; 134 /* The page isn't writable, so let's try making it so... */ 135 if ((rv = vm_map_protect (map, pageno, pageno + PAGE_SIZE, 136 VM_PROT_ALL, 0)) != KERN_SUCCESS) 137 return EFAULT; /* I guess... */ 138 } 139 140 /* 141 * Now we need to get the page. out_entry, out_prot, wired, and 142 * single_use aren't used. One would think the vm code would be 143 * a *bit* nicer... We use tmap because vm_map_lookup() can 144 * change the map argument. 145 */ 146 147 tmap = map; 148 rv = vm_map_lookup (&tmap, pageno, VM_PROT_WRITE, &out_entry, 149 &object, &pindex, &out_prot, &wired); 150 if (rv != KERN_SUCCESS) { 151 return EINVAL; 152 } 153 154 /* 155 * Okay, we've got the page. Let's release tmap. 156 */ 157 158 vm_map_lookup_done (tmap, out_entry); 159 160 /* 161 * Fault the page in... 162 */ 163 164 rv = vm_fault(map, pageno, VM_PROT_WRITE|VM_PROT_READ, FALSE); 165 if (rv != KERN_SUCCESS) 166 return EFAULT; 167 168 /* Find space in kernel_map for the page we're interested in */ 169 rv = vm_map_find (kernel_map, object, IDX_TO_OFF(pindex), 170 &kva, PAGE_SIZE, 0, 171 VM_PROT_ALL, VM_PROT_ALL, 0); 172 if (!rv) { 173 vm_object_reference (object); 174 175 rv = vm_map_pageable (kernel_map, kva, kva + PAGE_SIZE, 0); 176 if (!rv) { 177 bcopy (&datum, (caddr_t)kva + page_offset, sizeof datum); 178 } 179 vm_map_remove (kernel_map, kva, kva + PAGE_SIZE); 180 } 181 182 if (fix_prot) 183 vm_map_protect (map, pageno, pageno + PAGE_SIZE, 184 VM_PROT_READ|VM_PROT_EXECUTE, 0); 185 return rv; 186 } 187 #endif 188 189 /* 190 * Process debugging system call. 191 */ 192 #ifndef _SYS_SYSPROTO_H_ 193 struct ptrace_args { 194 int req; 195 pid_t pid; 196 caddr_t addr; 197 int data; 198 }; 199 #endif 200 201 int 202 ptrace(curp, uap) 203 struct proc *curp; 204 struct ptrace_args *uap; 205 { 206 struct proc *p; 207 struct iovec iov; 208 struct uio uio; 209 int error = 0; 210 int write; 211 212 write = 0; 213 if (uap->req == PT_TRACE_ME) 214 p = curp; 215 else { 216 if ((p = pfind(uap->pid)) == NULL) 217 return ESRCH; 218 } 219 if (p_can(curp, p, P_CAN_SEE, NULL)) 220 return (ESRCH); 221 222 /* 223 * Permissions check 224 */ 225 switch (uap->req) { 226 case PT_TRACE_ME: 227 /* Always legal. */ 228 break; 229 230 case PT_ATTACH: 231 /* Self */ 232 if (p->p_pid == curp->p_pid) 233 return EINVAL; 234 235 /* Already traced */ 236 PROC_LOCK(p); 237 if (p->p_flag & P_TRACED) { 238 PROC_UNLOCK(p); 239 return EBUSY; 240 } 241 PROC_UNLOCK(p); 242 243 if ((error = p_can(curp, p, P_CAN_DEBUG, NULL))) 244 return error; 245 246 /* OK */ 247 break; 248 249 case PT_READ_I: 250 case PT_READ_D: 251 case PT_READ_U: 252 case PT_WRITE_I: 253 case PT_WRITE_D: 254 case PT_WRITE_U: 255 case PT_CONTINUE: 256 case PT_KILL: 257 case PT_STEP: 258 case PT_DETACH: 259 #ifdef PT_GETREGS 260 case PT_GETREGS: 261 #endif 262 #ifdef PT_SETREGS 263 case PT_SETREGS: 264 #endif 265 #ifdef PT_GETFPREGS 266 case PT_GETFPREGS: 267 #endif 268 #ifdef PT_SETFPREGS 269 case PT_SETFPREGS: 270 #endif 271 #ifdef PT_GETDBREGS 272 case PT_GETDBREGS: 273 #endif 274 #ifdef PT_SETDBREGS 275 case PT_SETDBREGS: 276 #endif 277 /* not being traced... */ 278 PROC_LOCK(p); 279 if ((p->p_flag & P_TRACED) == 0) { 280 PROC_UNLOCK(p); 281 return EPERM; 282 } 283 284 /* not being traced by YOU */ 285 if (p->p_pptr != curp) { 286 PROC_UNLOCK(p); 287 return EBUSY; 288 } 289 290 /* not currently stopped */ 291 mtx_lock_spin(&sched_lock); 292 if (p->p_stat != SSTOP || (p->p_flag & P_WAITED) == 0) { 293 mtx_unlock_spin(&sched_lock); 294 PROC_UNLOCK(p); 295 return EBUSY; 296 } 297 mtx_unlock_spin(&sched_lock); 298 PROC_UNLOCK(p); 299 300 /* OK */ 301 break; 302 303 default: 304 return EINVAL; 305 } 306 307 #ifdef FIX_SSTEP 308 /* 309 * Single step fixup ala procfs 310 */ 311 FIX_SSTEP(p); 312 #endif 313 314 /* 315 * Actually do the requests 316 */ 317 318 curp->p_retval[0] = 0; 319 320 switch (uap->req) { 321 case PT_TRACE_ME: 322 /* set my trace flag and "owner" so it can read/write me */ 323 PROCTREE_LOCK(PT_EXCLUSIVE); 324 PROC_LOCK(p); 325 p->p_flag |= P_TRACED; 326 p->p_oppid = p->p_pptr->p_pid; 327 PROC_UNLOCK(p); 328 PROCTREE_LOCK(PT_RELEASE); 329 return 0; 330 331 case PT_ATTACH: 332 /* security check done above */ 333 PROCTREE_LOCK(PT_EXCLUSIVE); 334 PROC_LOCK(p); 335 p->p_flag |= P_TRACED; 336 p->p_oppid = p->p_pptr->p_pid; 337 if (p->p_pptr != curp) 338 proc_reparent(p, curp); 339 PROC_UNLOCK(p); 340 PROCTREE_LOCK(PT_RELEASE); 341 uap->data = SIGSTOP; 342 goto sendsig; /* in PT_CONTINUE below */ 343 344 case PT_STEP: 345 case PT_CONTINUE: 346 case PT_DETACH: 347 if ((uap->req != PT_STEP) && ((unsigned)uap->data >= NSIG)) 348 return EINVAL; 349 350 PHOLD(p); 351 352 if (uap->req == PT_STEP) { 353 if ((error = ptrace_single_step (p))) { 354 PRELE(p); 355 return error; 356 } 357 } 358 359 if (uap->addr != (caddr_t)1) { 360 fill_kinfo_proc (p, &p->p_addr->u_kproc); 361 if ((error = ptrace_set_pc (p, 362 (u_long)(uintfptr_t)uap->addr))) { 363 PRELE(p); 364 return error; 365 } 366 } 367 PRELE(p); 368 369 if (uap->req == PT_DETACH) { 370 /* reset process parent */ 371 PROCTREE_LOCK(PT_EXCLUSIVE); 372 if (p->p_oppid != p->p_pptr->p_pid) { 373 struct proc *pp; 374 375 pp = pfind(p->p_oppid); 376 PROC_LOCK(p); 377 proc_reparent(p, pp ? pp : initproc); 378 } else 379 PROC_LOCK(p); 380 p->p_flag &= ~(P_TRACED | P_WAITED); 381 p->p_oppid = 0; 382 383 PROC_UNLOCK(p); 384 PROCTREE_LOCK(PT_RELEASE); 385 386 /* should we send SIGCHLD? */ 387 388 } 389 390 sendsig: 391 /* deliver or queue signal */ 392 PROC_LOCK(p); 393 mtx_lock_spin(&sched_lock); 394 if (p->p_stat == SSTOP) { 395 p->p_xstat = uap->data; 396 setrunnable(p); 397 mtx_unlock_spin(&sched_lock); 398 } else { 399 mtx_unlock_spin(&sched_lock); 400 if (uap->data) 401 psignal(p, uap->data); 402 403 } 404 PROC_UNLOCK(p); 405 return 0; 406 407 case PT_WRITE_I: 408 case PT_WRITE_D: 409 write = 1; 410 /* fallthrough */ 411 case PT_READ_I: 412 case PT_READ_D: 413 /* write = 0 set above */ 414 iov.iov_base = write ? (caddr_t)&uap->data : (caddr_t)curp->p_retval; 415 iov.iov_len = sizeof(int); 416 uio.uio_iov = &iov; 417 uio.uio_iovcnt = 1; 418 uio.uio_offset = (off_t)(uintptr_t)uap->addr; 419 uio.uio_resid = sizeof(int); 420 uio.uio_segflg = UIO_SYSSPACE; /* ie: the uap */ 421 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 422 uio.uio_procp = p; 423 error = procfs_domem(curp, p, NULL, &uio); 424 if (uio.uio_resid != 0) { 425 /* 426 * XXX procfs_domem() doesn't currently return ENOSPC, 427 * so I think write() can bogusly return 0. 428 * XXX what happens for short writes? We don't want 429 * to write partial data. 430 * XXX procfs_domem() returns EPERM for other invalid 431 * addresses. Convert this to EINVAL. Does this 432 * clobber returns of EPERM for other reasons? 433 */ 434 if (error == 0 || error == ENOSPC || error == EPERM) 435 error = EINVAL; /* EOF */ 436 } 437 return (error); 438 439 case PT_READ_U: 440 if ((uintptr_t)uap->addr > UPAGES * PAGE_SIZE - sizeof(int)) { 441 return EFAULT; 442 } 443 if ((uintptr_t)uap->addr & (sizeof(int) - 1)) { 444 return EFAULT; 445 } 446 if (ptrace_read_u_check(p,(vm_offset_t) uap->addr, 447 sizeof(int))) { 448 return EFAULT; 449 } 450 error = 0; 451 PHOLD(p); /* user had damn well better be incore! */ 452 mtx_lock_spin(&sched_lock); 453 if (p->p_sflag & PS_INMEM) { 454 mtx_unlock_spin(&sched_lock); 455 fill_kinfo_proc (p, &p->p_addr->u_kproc); 456 curp->p_retval[0] = *(int *) 457 ((uintptr_t)p->p_addr + (uintptr_t)uap->addr); 458 } else { 459 mtx_unlock_spin(&sched_lock); 460 curp->p_retval[0] = 0; 461 error = EFAULT; 462 } 463 PRELE(p); 464 return error; 465 466 case PT_WRITE_U: 467 PHOLD(p); /* user had damn well better be incore! */ 468 mtx_lock_spin(&sched_lock); 469 if (p->p_sflag & PS_INMEM) { 470 mtx_unlock_spin(&sched_lock); 471 fill_kinfo_proc (p, &p->p_addr->u_kproc); 472 error = ptrace_write_u(p, (vm_offset_t)uap->addr, uap->data); 473 } else { 474 mtx_unlock_spin(&sched_lock); 475 error = EFAULT; 476 } 477 PRELE(p); 478 return error; 479 480 case PT_KILL: 481 uap->data = SIGKILL; 482 goto sendsig; /* in PT_CONTINUE above */ 483 484 #ifdef PT_SETREGS 485 case PT_SETREGS: 486 write = 1; 487 /* fallthrough */ 488 #endif /* PT_SETREGS */ 489 #ifdef PT_GETREGS 490 case PT_GETREGS: 491 /* write = 0 above */ 492 #endif /* PT_SETREGS */ 493 #if defined(PT_SETREGS) || defined(PT_GETREGS) 494 if (!procfs_validregs(p)) /* no P_SYSTEM procs please */ 495 return EINVAL; 496 else { 497 iov.iov_base = uap->addr; 498 iov.iov_len = sizeof(struct reg); 499 uio.uio_iov = &iov; 500 uio.uio_iovcnt = 1; 501 uio.uio_offset = 0; 502 uio.uio_resid = sizeof(struct reg); 503 uio.uio_segflg = UIO_USERSPACE; 504 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 505 uio.uio_procp = curp; 506 return (procfs_doregs(curp, p, NULL, &uio)); 507 } 508 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */ 509 510 #ifdef PT_SETFPREGS 511 case PT_SETFPREGS: 512 write = 1; 513 /* fallthrough */ 514 #endif /* PT_SETFPREGS */ 515 #ifdef PT_GETFPREGS 516 case PT_GETFPREGS: 517 /* write = 0 above */ 518 #endif /* PT_SETFPREGS */ 519 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS) 520 if (!procfs_validfpregs(p)) /* no P_SYSTEM procs please */ 521 return EINVAL; 522 else { 523 iov.iov_base = uap->addr; 524 iov.iov_len = sizeof(struct fpreg); 525 uio.uio_iov = &iov; 526 uio.uio_iovcnt = 1; 527 uio.uio_offset = 0; 528 uio.uio_resid = sizeof(struct fpreg); 529 uio.uio_segflg = UIO_USERSPACE; 530 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 531 uio.uio_procp = curp; 532 return (procfs_dofpregs(curp, p, NULL, &uio)); 533 } 534 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */ 535 536 #ifdef PT_SETDBREGS 537 case PT_SETDBREGS: 538 write = 1; 539 /* fallthrough */ 540 #endif /* PT_SETDBREGS */ 541 #ifdef PT_GETDBREGS 542 case PT_GETDBREGS: 543 /* write = 0 above */ 544 #endif /* PT_SETDBREGS */ 545 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS) 546 if (!procfs_validdbregs(p)) /* no P_SYSTEM procs please */ 547 return EINVAL; 548 else { 549 iov.iov_base = uap->addr; 550 iov.iov_len = sizeof(struct dbreg); 551 uio.uio_iov = &iov; 552 uio.uio_iovcnt = 1; 553 uio.uio_offset = 0; 554 uio.uio_resid = sizeof(struct dbreg); 555 uio.uio_segflg = UIO_USERSPACE; 556 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 557 uio.uio_procp = curp; 558 return (procfs_dodbregs(curp, p, NULL, &uio)); 559 } 560 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */ 561 562 default: 563 break; 564 } 565 566 return 0; 567 } 568 569 int 570 trace_req(p) 571 struct proc *p; 572 { 573 return 1; 574 } 575 576 /* 577 * stopevent() 578 * Stop a process because of a procfs event; 579 * stay stopped until p->p_step is cleared 580 * (cleared by PIOCCONT in procfs). 581 * 582 * Must be called with the proc struct mutex held. 583 */ 584 585 void 586 stopevent(p, event, val) 587 struct proc *p; 588 unsigned int event; 589 unsigned int val; 590 { 591 592 PROC_LOCK_ASSERT(p, MA_OWNED | MA_NOTRECURSED); 593 p->p_step = 1; 594 595 do { 596 p->p_xstat = val; 597 p->p_stype = event; /* Which event caused the stop? */ 598 wakeup(&p->p_stype); /* Wake up any PIOCWAIT'ing procs */ 599 msleep(&p->p_step, &p->p_mtx, PWAIT, "stopevent", 0); 600 } while (p->p_step); 601 } 602