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 * $Id: sys_process.c,v 1.33 1997/12/06 04:11:10 sef Exp $ 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 <vm/vm_prot.h> 44 #include <sys/lock.h> 45 #include <vm/pmap.h> 46 #include <vm/vm_map.h> 47 #include <vm/vm_page.h> 48 #include <vm/vm_extern.h> 49 50 #include <sys/user.h> 51 #include <miscfs/procfs/procfs.h> 52 53 /* use the equivalent procfs code */ 54 #if 0 55 static int 56 pread (struct proc *procp, unsigned int addr, unsigned int *retval) { 57 int rv; 58 vm_map_t map, tmap; 59 vm_object_t object; 60 vm_offset_t kva = 0; 61 int page_offset; /* offset into page */ 62 vm_offset_t pageno; /* page number */ 63 vm_map_entry_t out_entry; 64 vm_prot_t out_prot; 65 boolean_t wired; 66 vm_pindex_t pindex; 67 68 /* Map page into kernel space */ 69 70 map = &procp->p_vmspace->vm_map; 71 72 page_offset = addr - trunc_page(addr); 73 pageno = trunc_page(addr); 74 75 tmap = map; 76 rv = vm_map_lookup (&tmap, pageno, VM_PROT_READ, &out_entry, 77 &object, &pindex, &out_prot, &wired); 78 79 if (rv != KERN_SUCCESS) 80 return EINVAL; 81 82 vm_map_lookup_done (tmap, out_entry); 83 84 /* Find space in kernel_map for the page we're interested in */ 85 rv = vm_map_find (kernel_map, object, IDX_TO_OFF(pindex), 86 &kva, PAGE_SIZE, 0, VM_PROT_ALL, VM_PROT_ALL, 0); 87 88 if (!rv) { 89 vm_object_reference (object); 90 91 rv = vm_map_pageable (kernel_map, kva, kva + PAGE_SIZE, 0); 92 if (!rv) { 93 *retval = 0; 94 bcopy ((caddr_t)kva + page_offset, 95 retval, sizeof *retval); 96 } 97 vm_map_remove (kernel_map, kva, kva + PAGE_SIZE); 98 } 99 100 return rv; 101 } 102 103 static int 104 pwrite (struct proc *procp, unsigned int addr, unsigned int datum) { 105 int rv; 106 vm_map_t map, tmap; 107 vm_object_t object; 108 vm_offset_t kva = 0; 109 int page_offset; /* offset into page */ 110 vm_offset_t pageno; /* page number */ 111 vm_map_entry_t out_entry; 112 vm_prot_t out_prot; 113 boolean_t wired; 114 vm_pindex_t pindex; 115 boolean_t fix_prot = 0; 116 117 /* Map page into kernel space */ 118 119 map = &procp->p_vmspace->vm_map; 120 121 page_offset = addr - trunc_page(addr); 122 pageno = trunc_page(addr); 123 124 /* 125 * Check the permissions for the area we're interested in. 126 */ 127 128 if (vm_map_check_protection (map, pageno, pageno + PAGE_SIZE, 129 VM_PROT_WRITE) == FALSE) { 130 /* 131 * If the page was not writable, we make it so. 132 * XXX It is possible a page may *not* be read/executable, 133 * if a process changes that! 134 */ 135 fix_prot = 1; 136 /* The page isn't writable, so let's try making it so... */ 137 if ((rv = vm_map_protect (map, pageno, pageno + PAGE_SIZE, 138 VM_PROT_ALL, 0)) != KERN_SUCCESS) 139 return EFAULT; /* I guess... */ 140 } 141 142 /* 143 * Now we need to get the page. out_entry, out_prot, wired, and 144 * single_use aren't used. One would think the vm code would be 145 * a *bit* nicer... We use tmap because vm_map_lookup() can 146 * change the map argument. 147 */ 148 149 tmap = map; 150 rv = vm_map_lookup (&tmap, pageno, VM_PROT_WRITE, &out_entry, 151 &object, &pindex, &out_prot, &wired); 152 if (rv != KERN_SUCCESS) { 153 return EINVAL; 154 } 155 156 /* 157 * Okay, we've got the page. Let's release tmap. 158 */ 159 160 vm_map_lookup_done (tmap, out_entry); 161 162 /* 163 * Fault the page in... 164 */ 165 166 rv = vm_fault(map, pageno, VM_PROT_WRITE|VM_PROT_READ, FALSE); 167 if (rv != KERN_SUCCESS) 168 return EFAULT; 169 170 /* Find space in kernel_map for the page we're interested in */ 171 rv = vm_map_find (kernel_map, object, IDX_TO_OFF(pindex), 172 &kva, PAGE_SIZE, 0, 173 VM_PROT_ALL, VM_PROT_ALL, 0); 174 if (!rv) { 175 vm_object_reference (object); 176 177 rv = vm_map_pageable (kernel_map, kva, kva + PAGE_SIZE, 0); 178 if (!rv) { 179 bcopy (&datum, (caddr_t)kva + page_offset, sizeof datum); 180 } 181 vm_map_remove (kernel_map, kva, kva + PAGE_SIZE); 182 } 183 184 if (fix_prot) 185 vm_map_protect (map, pageno, pageno + PAGE_SIZE, 186 VM_PROT_READ|VM_PROT_EXECUTE, 0); 187 return rv; 188 } 189 #endif 190 191 /* 192 * Process debugging system call. 193 */ 194 #ifndef _SYS_SYSPROTO_H_ 195 struct ptrace_args { 196 int req; 197 pid_t pid; 198 caddr_t addr; 199 int data; 200 }; 201 #endif 202 203 int 204 ptrace(curp, uap) 205 struct proc *curp; 206 struct ptrace_args *uap; 207 { 208 struct proc *p; 209 struct iovec iov; 210 struct uio uio; 211 int error = 0; 212 int write; 213 int s; 214 215 if (uap->req == PT_TRACE_ME) 216 p = curp; 217 else { 218 if ((p = pfind(uap->pid)) == NULL) 219 return ESRCH; 220 } 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 if (p->p_flag & P_TRACED) 237 return EBUSY; 238 239 /* not owned by you, has done setuid (unless you're root) */ 240 if ((p->p_cred->p_ruid != curp->p_cred->p_ruid) || 241 (p->p_flag & P_SUGID)) { 242 if (error = suser(curp->p_ucred, &curp->p_acflag)) 243 return error; 244 } 245 246 /* can't trace init when securelevel > 0 */ 247 if (securelevel > 0 && p->p_pid == 1) 248 return EPERM; 249 250 /* OK */ 251 break; 252 253 case PT_READ_I: 254 case PT_READ_D: 255 case PT_READ_U: 256 case PT_WRITE_I: 257 case PT_WRITE_D: 258 case PT_WRITE_U: 259 case PT_CONTINUE: 260 case PT_KILL: 261 case PT_STEP: 262 case PT_DETACH: 263 #ifdef PT_GETREGS 264 case PT_GETREGS: 265 #endif 266 #ifdef PT_SETREGS 267 case PT_SETREGS: 268 #endif 269 #ifdef PT_GETFPREGS 270 case PT_GETFPREGS: 271 #endif 272 #ifdef PT_SETFPREGS 273 case PT_SETFPREGS: 274 #endif 275 /* not being traced... */ 276 if ((p->p_flag & P_TRACED) == 0) 277 return EPERM; 278 279 /* not being traced by YOU */ 280 if (p->p_pptr != curp) 281 return EBUSY; 282 283 /* not currently stopped */ 284 if (p->p_stat != SSTOP || (p->p_flag & P_WAITED) == 0) 285 return EBUSY; 286 287 /* OK */ 288 break; 289 290 default: 291 return EINVAL; 292 } 293 294 #ifdef FIX_SSTEP 295 /* 296 * Single step fixup ala procfs 297 */ 298 FIX_SSTEP(p); 299 #endif 300 301 /* 302 * Actually do the requests 303 */ 304 305 write = 0; 306 curp->p_retval[0] = 0; 307 308 switch (uap->req) { 309 case PT_TRACE_ME: 310 /* set my trace flag and "owner" so it can read/write me */ 311 p->p_flag |= P_TRACED; 312 p->p_oppid = p->p_pptr->p_pid; 313 return 0; 314 315 case PT_ATTACH: 316 /* security check done above */ 317 p->p_flag |= P_TRACED; 318 p->p_oppid = p->p_pptr->p_pid; 319 if (p->p_pptr != curp) 320 proc_reparent(p, curp); 321 uap->data = SIGSTOP; 322 goto sendsig; /* in PT_CONTINUE below */ 323 324 case PT_STEP: 325 case PT_CONTINUE: 326 case PT_DETACH: 327 if ((unsigned)uap->data >= NSIG) 328 return EINVAL; 329 330 PHOLD(p); 331 332 if (uap->req == PT_STEP) { 333 if ((error = ptrace_single_step (p))) { 334 PRELE(p); 335 return error; 336 } 337 } 338 339 if (uap->addr != (caddr_t)1) { 340 fill_eproc (p, &p->p_addr->u_kproc.kp_eproc); 341 if ((error = ptrace_set_pc (p, (u_int)uap->addr))) { 342 PRELE(p); 343 return error; 344 } 345 } 346 PRELE(p); 347 348 if (uap->req == PT_DETACH) { 349 /* reset process parent */ 350 if (p->p_oppid != p->p_pptr->p_pid) { 351 struct proc *pp; 352 353 pp = pfind(p->p_oppid); 354 proc_reparent(p, pp ? pp : initproc); 355 } 356 357 p->p_flag &= ~(P_TRACED | P_WAITED); 358 p->p_oppid = 0; 359 360 /* should we send SIGCHLD? */ 361 362 } 363 364 sendsig: 365 /* deliver or queue signal */ 366 s = splhigh(); 367 if (p->p_stat == SSTOP) { 368 p->p_xstat = uap->data; 369 setrunnable(p); 370 } else if (uap->data) { 371 psignal(p, uap->data); 372 } 373 splx(s); 374 return 0; 375 376 case PT_WRITE_I: 377 case PT_WRITE_D: 378 write = 1; 379 /* fallthrough */ 380 case PT_READ_I: 381 case PT_READ_D: 382 /* write = 0 set above */ 383 iov.iov_base = write ? (caddr_t)&uap->data : (caddr_t)curp->p_retval; 384 iov.iov_len = sizeof(int); 385 uio.uio_iov = &iov; 386 uio.uio_iovcnt = 1; 387 uio.uio_offset = (off_t)(u_long)uap->addr; 388 uio.uio_resid = sizeof(int); 389 uio.uio_segflg = UIO_SYSSPACE; /* ie: the uap */ 390 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 391 uio.uio_procp = p; 392 error = procfs_domem(curp, p, NULL, &uio); 393 if (uio.uio_resid != 0) { 394 /* 395 * XXX procfs_domem() doesn't currently return ENOSPC, 396 * so I think write() can bogusly return 0. 397 * XXX what happens for short writes? We don't want 398 * to write partial data. 399 * XXX procfs_domem() returns EPERM for other invalid 400 * addresses. Convert this to EINVAL. Does this 401 * clobber returns of EPERM for other reasons? 402 */ 403 if (error == 0 || error == ENOSPC || error == EPERM) 404 error = EINVAL; /* EOF */ 405 } 406 return (error); 407 408 case PT_READ_U: 409 if ((u_int)uap->addr > (UPAGES * PAGE_SIZE - sizeof(int))) { 410 return EFAULT; 411 } 412 error = 0; 413 PHOLD(p); /* user had damn well better be incore! */ 414 if (p->p_flag & P_INMEM) { 415 p->p_addr->u_kproc.kp_proc = *p; 416 fill_eproc (p, &p->p_addr->u_kproc.kp_eproc); 417 curp->p_retval[0] = *(int*)((u_int)p->p_addr + (u_int)uap->addr); 418 } else { 419 curp->p_retval[0] = 0; 420 error = EFAULT; 421 } 422 PRELE(p); 423 return error; 424 425 case PT_WRITE_U: 426 PHOLD(p); /* user had damn well better be incore! */ 427 if (p->p_flag & P_INMEM) { 428 p->p_addr->u_kproc.kp_proc = *p; 429 fill_eproc (p, &p->p_addr->u_kproc.kp_eproc); 430 error = ptrace_write_u(p, (vm_offset_t)uap->addr, uap->data); 431 } else { 432 error = EFAULT; 433 } 434 PRELE(p); 435 return error; 436 437 case PT_KILL: 438 uap->data = SIGKILL; 439 goto sendsig; /* in PT_CONTINUE above */ 440 441 #ifdef PT_SETREGS 442 case PT_SETREGS: 443 write = 1; 444 /* fallthrough */ 445 #endif /* PT_SETREGS */ 446 #ifdef PT_GETREGS 447 case PT_GETREGS: 448 /* write = 0 above */ 449 #endif /* PT_SETREGS */ 450 #if defined(PT_SETREGS) || defined(PT_GETREGS) 451 if (!procfs_validregs(p)) /* no P_SYSTEM procs please */ 452 return EINVAL; 453 else { 454 iov.iov_base = uap->addr; 455 iov.iov_len = sizeof(struct reg); 456 uio.uio_iov = &iov; 457 uio.uio_iovcnt = 1; 458 uio.uio_offset = 0; 459 uio.uio_resid = sizeof(struct reg); 460 uio.uio_segflg = UIO_USERSPACE; 461 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 462 uio.uio_procp = curp; 463 return (procfs_doregs(curp, p, NULL, &uio)); 464 } 465 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */ 466 467 #ifdef PT_SETFPREGS 468 case PT_SETFPREGS: 469 write = 1; 470 /* fallthrough */ 471 #endif /* PT_SETFPREGS */ 472 #ifdef PT_GETFPREGS 473 case PT_GETFPREGS: 474 /* write = 0 above */ 475 #endif /* PT_SETFPREGS */ 476 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS) 477 if (!procfs_validfpregs(p)) /* no P_SYSTEM procs please */ 478 return EINVAL; 479 else { 480 iov.iov_base = uap->addr; 481 iov.iov_len = sizeof(struct fpreg); 482 uio.uio_iov = &iov; 483 uio.uio_iovcnt = 1; 484 uio.uio_offset = 0; 485 uio.uio_resid = sizeof(struct fpreg); 486 uio.uio_segflg = UIO_USERSPACE; 487 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 488 uio.uio_procp = curp; 489 return (procfs_dofpregs(curp, p, NULL, &uio)); 490 } 491 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */ 492 493 default: 494 break; 495 } 496 497 return 0; 498 } 499 500 int 501 trace_req(p) 502 struct proc *p; 503 { 504 return 1; 505 } 506 507 /* 508 * stopevent() 509 * Stop a process because of a procfs event; 510 * stay stopped until p->p_step is cleared 511 * (cleared by PIOCCONT in procfs). 512 */ 513 514 void 515 stopevent(struct proc *p, unsigned int event, unsigned int val) { 516 p->p_step = 1; 517 518 do { 519 p->p_xstat = val; 520 p->p_stype = event; /* Which event caused the stop? */ 521 wakeup(&p->p_stype); /* Wake up any PIOCWAIT'ing procs */ 522 tsleep(&p->p_step, PWAIT, "stopevent", 0); 523 } while (p->p_step); 524 } 525