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