1 /* $OpenBSD: sys_process.c,v 1.22 2002/04/10 17:31:59 fgsch Exp $ */ 2 /* $NetBSD: sys_process.c,v 1.55 1996/05/15 06:17:47 tls Exp $ */ 3 4 /*- 5 * Copyright (c) 1994 Christopher G. Demetriou. All rights reserved. 6 * Copyright (c) 1982, 1986, 1989, 1993 7 * The Regents of the University of California. All rights reserved. 8 * (c) UNIX System Laboratories, Inc. 9 * All or some portions of this file are derived from material licensed 10 * to the University of California by American Telephone and Telegraph 11 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 12 * the permission of UNIX System Laboratories, Inc. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. All advertising materials mentioning features or use of this software 23 * must display the following acknowledgement: 24 * This product includes software developed by the University of 25 * California, Berkeley and its contributors. 26 * 4. Neither the name of the University nor the names of its contributors 27 * may be used to endorse or promote products derived from this software 28 * without specific prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40 * SUCH DAMAGE. 41 * 42 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93 43 */ 44 45 /* 46 * References: 47 * (1) Bach's "The Design of the UNIX Operating System", 48 * (2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution, 49 * (3) the "4.4BSD Programmer's Reference Manual" published 50 * by USENIX and O'Reilly & Associates. 51 * The 4.4BSD PRM does a reasonably good job of documenting what the various 52 * ptrace() requests should actually do, and its text is quoted several times 53 * in this file. 54 */ 55 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/proc.h> 59 #include <sys/signalvar.h> 60 #include <sys/errno.h> 61 #include <sys/ptrace.h> 62 #include <sys/uio.h> 63 #include <sys/user.h> 64 65 #include <sys/mount.h> 66 #include <sys/syscallargs.h> 67 68 #include <uvm/uvm_extern.h> 69 70 #include <machine/reg.h> 71 72 #include <miscfs/procfs/procfs.h> 73 74 /* 75 * Process debugging system call. 76 */ 77 int 78 sys_ptrace(p, v, retval) 79 struct proc *p; 80 void *v; 81 register_t *retval; 82 { 83 struct sys_ptrace_args /* { 84 syscallarg(int) req; 85 syscallarg(pid_t) pid; 86 syscallarg(caddr_t) addr; 87 syscallarg(int) data; 88 } */ *uap = v; 89 struct proc *t; /* target process */ 90 struct uio uio; 91 struct iovec iov; 92 struct ptrace_io_desc piod; 93 int error, write; 94 int temp; 95 96 /* "A foolish consistency..." XXX */ 97 if (SCARG(uap, req) == PT_TRACE_ME) 98 t = p; 99 else { 100 101 /* Find the process we're supposed to be operating on. */ 102 if ((t = pfind(SCARG(uap, pid))) == NULL) 103 return (ESRCH); 104 } 105 106 if ((t->p_flag & P_INEXEC) != 0) 107 return (EAGAIN); 108 109 /* Make sure we can operate on it. */ 110 switch (SCARG(uap, req)) { 111 case PT_TRACE_ME: 112 /* Saying that you're being traced is always legal. */ 113 break; 114 115 case PT_ATTACH: 116 /* 117 * You can't attach to a process if: 118 * (1) it's the process that's doing the attaching, 119 */ 120 if (t->p_pid == p->p_pid) 121 return (EINVAL); 122 123 /* 124 * (2) it's a system process 125 */ 126 if (ISSET(t->p_flag, P_SYSTEM)) 127 return (EPERM); 128 129 /* 130 * (3) it's already being traced, or 131 */ 132 if (ISSET(t->p_flag, P_TRACED)) 133 return (EBUSY); 134 135 /* 136 * (4) it's not owned by you, or the last exec 137 * gave us setuid/setgid privs (unless 138 * you're root), or... 139 * 140 * [Note: once P_SUGID gets set in execve(), it stays 141 * set until the process does another execve(). Hence 142 * this prevents a setuid process which revokes it's 143 * special privilidges using setuid() from being 144 * traced. This is good security.] 145 */ 146 if ((t->p_cred->p_ruid != p->p_cred->p_ruid || 147 ISSET(t->p_flag, P_SUGID)) && 148 (error = suser(p->p_ucred, &p->p_acflag)) != 0) 149 return (error); 150 151 /* 152 * (5) ...it's init, which controls the security level 153 * of the entire system, and the system was not 154 * compiled with permanently insecure mode turned 155 * on. 156 */ 157 if ((t->p_pid == 1) && (securelevel > -1)) 158 return (EPERM); 159 break; 160 161 case PT_READ_I: 162 case PT_READ_D: 163 case PT_WRITE_I: 164 case PT_WRITE_D: 165 case PT_IO: 166 case PT_CONTINUE: 167 case PT_KILL: 168 case PT_DETACH: 169 #ifdef PT_STEP 170 case PT_STEP: 171 #endif 172 case PT_GETREGS: 173 case PT_SETREGS: 174 #ifdef PT_GETFPREGS 175 case PT_GETFPREGS: 176 #endif 177 #ifdef PT_SETFPREGS 178 case PT_SETFPREGS: 179 #endif 180 /* 181 * You can't do what you want to the process if: 182 * (1) It's not being traced at all, 183 */ 184 if (!ISSET(t->p_flag, P_TRACED)) 185 return (EPERM); 186 187 /* 188 * (2) it's not being traced by _you_, or 189 */ 190 if (t->p_pptr != p) 191 return (EBUSY); 192 193 /* 194 * (3) it's not currently stopped. 195 */ 196 if (t->p_stat != SSTOP || !ISSET(t->p_flag, P_WAITED)) 197 return (EBUSY); 198 break; 199 200 default: /* It was not a legal request. */ 201 return (EINVAL); 202 } 203 204 /* Do single-step fixup if needed. */ 205 FIX_SSTEP(t); 206 207 /* Now do the operation. */ 208 write = 0; 209 *retval = 0; 210 211 switch (SCARG(uap, req)) { 212 case PT_TRACE_ME: 213 /* Just set the trace flag. */ 214 SET(t->p_flag, P_TRACED); 215 t->p_oppid = t->p_pptr->p_pid; 216 return (0); 217 218 case PT_WRITE_I: /* XXX no separate I and D spaces */ 219 case PT_WRITE_D: 220 write = 1; 221 temp = SCARG(uap, data); 222 case PT_READ_I: /* XXX no separate I and D spaces */ 223 case PT_READ_D: 224 /* write = 0 done above. */ 225 iov.iov_base = (caddr_t)&temp; 226 iov.iov_len = sizeof(int); 227 uio.uio_iov = &iov; 228 uio.uio_iovcnt = 1; 229 uio.uio_offset = (off_t)(long)SCARG(uap, addr); 230 uio.uio_resid = sizeof(int); 231 uio.uio_segflg = UIO_SYSSPACE; 232 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 233 uio.uio_procp = p; 234 error = procfs_domem(p, t, NULL, &uio); 235 if (write == 0) 236 *retval = temp; 237 return (error); 238 case PT_IO: 239 error = copyin(SCARG(uap, addr), &piod, sizeof(piod)); 240 if (error) 241 return (error); 242 iov.iov_base = piod.piod_addr; 243 iov.iov_len = piod.piod_len; 244 uio.uio_iov = &iov; 245 uio.uio_iovcnt = 1; 246 uio.uio_offset = (off_t)(long)piod.piod_offs; 247 uio.uio_resid = piod.piod_len; 248 uio.uio_segflg = UIO_USERSPACE; 249 uio.uio_procp = p; 250 switch (piod.piod_op) { 251 case PIOD_READ_D: 252 case PIOD_READ_I: 253 uio.uio_rw = UIO_READ; 254 break; 255 case PIOD_WRITE_D: 256 case PIOD_WRITE_I: 257 uio.uio_rw = UIO_WRITE; 258 break; 259 default: 260 return (EINVAL); 261 } 262 error = procfs_domem(p, t, NULL, &uio); 263 piod.piod_len -= uio.uio_resid; 264 (void) copyout(&piod, SCARG(uap, addr), sizeof(piod)); 265 return (error); 266 #ifdef PT_STEP 267 case PT_STEP: 268 /* 269 * From the 4.4BSD PRM: 270 * "Execution continues as in request PT_CONTINUE; however 271 * as soon as possible after execution of at least one 272 * instruction, execution stops again. [ ... ]" 273 */ 274 #endif 275 case PT_CONTINUE: 276 /* 277 * From the 4.4BSD PRM: 278 * "The data argument is taken as a signal number and the 279 * child's execution continues at location addr as if it 280 * incurred that signal. Normally the signal number will 281 * be either 0 to indicate that the signal that caused the 282 * stop should be ignored, or that value fetched out of 283 * the process's image indicating which signal caused 284 * the stop. If addr is (int *)1 then execution continues 285 * from where it stopped." 286 */ 287 288 /* Check that the data is a valid signal number or zero. */ 289 if (SCARG(uap, data) < 0 || SCARG(uap, data) >= NSIG) 290 return (EINVAL); 291 292 PHOLD(t); 293 /* If the address paramter is not (int *)1, set the pc. */ 294 if ((int *)SCARG(uap, addr) != (int *)1) 295 if ((error = process_set_pc(t, SCARG(uap, addr))) != 0) 296 goto relebad; 297 298 #ifdef PT_STEP 299 /* 300 * Arrange for a single-step, if that's requested and possible. 301 */ 302 error = process_sstep(t, SCARG(uap, req) == PT_STEP); 303 if (error) 304 goto relebad; 305 #endif 306 PRELE(t); 307 goto sendsig; 308 309 case PT_DETACH: 310 /* 311 * From the 4.4BSD PRM: 312 * "The data argument is taken as a signal number and the 313 * child's execution continues at location addr as if it 314 * incurred that signal. Normally the signal number will 315 * be either 0 to indicate that the signal that caused the 316 * stop should be ignored, or that value fetched out of 317 * the process's image indicating which signal caused 318 * the stop. If addr is (int *)1 then execution continues 319 * from where it stopped." 320 */ 321 322 /* Check that the data is a valid signal number or zero. */ 323 if (SCARG(uap, data) < 0 || SCARG(uap, data) >= NSIG) 324 return (EINVAL); 325 326 PHOLD(t); 327 #ifdef PT_STEP 328 /* 329 * Arrange for a single-step, if that's requested and possible. 330 */ 331 error = process_sstep(t, SCARG(uap, req) == PT_STEP); 332 if (error) 333 goto relebad; 334 #endif 335 PRELE(t); 336 337 /* give process back to original parent or init */ 338 if (t->p_oppid != t->p_pptr->p_pid) { 339 struct proc *pp; 340 341 pp = pfind(t->p_oppid); 342 proc_reparent(t, pp ? pp : initproc); 343 } 344 345 /* not being traced any more */ 346 t->p_oppid = 0; 347 CLR(t->p_flag, P_TRACED|P_WAITED); 348 349 sendsig: 350 /* Finally, deliver the requested signal (or none). */ 351 if (t->p_stat == SSTOP) { 352 t->p_xstat = SCARG(uap, data); 353 setrunnable(t); 354 } else { 355 if (SCARG(uap, data) != 0) 356 psignal(t, SCARG(uap, data)); 357 } 358 return (0); 359 360 relebad: 361 PRELE(t); 362 return (error); 363 364 case PT_KILL: 365 /* just send the process a KILL signal. */ 366 SCARG(uap, data) = SIGKILL; 367 goto sendsig; /* in PT_CONTINUE, above. */ 368 369 case PT_ATTACH: 370 /* 371 * As done in procfs: 372 * Go ahead and set the trace flag. 373 * Save the old parent (it's reset in 374 * _DETACH, and also in kern_exit.c:wait4() 375 * Reparent the process so that the tracing 376 * proc gets to see all the action. 377 * Stop the target. 378 */ 379 SET(t->p_flag, P_TRACED); 380 t->p_oppid = t->p_pptr->p_pid; 381 if (t->p_pptr != p) 382 proc_reparent(t, p); 383 SCARG(uap, data) = SIGSTOP; 384 goto sendsig; 385 386 case PT_SETREGS: 387 write = 1; 388 case PT_GETREGS: 389 /* write = 0 done above. */ 390 if (!procfs_validregs(t, NULL)) 391 return (EINVAL); 392 else { 393 iov.iov_base = SCARG(uap, addr); 394 iov.iov_len = sizeof(struct reg); 395 uio.uio_iov = &iov; 396 uio.uio_iovcnt = 1; 397 uio.uio_offset = 0; 398 uio.uio_resid = sizeof(struct reg); 399 uio.uio_segflg = UIO_USERSPACE; 400 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 401 uio.uio_procp = p; 402 return (procfs_doregs(p, t, NULL, &uio)); 403 } 404 #ifdef PT_SETFPREGS 405 case PT_SETFPREGS: 406 write = 1; 407 #endif 408 #ifdef PT_GETFPREGS 409 case PT_GETFPREGS: 410 /* write = 0 done above. */ 411 #endif 412 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS) 413 if (!procfs_validfpregs(t, NULL)) 414 return (EINVAL); 415 else { 416 iov.iov_base = SCARG(uap, addr); 417 iov.iov_len = sizeof(struct fpreg); 418 uio.uio_iov = &iov; 419 uio.uio_iovcnt = 1; 420 uio.uio_offset = 0; 421 uio.uio_resid = sizeof(struct fpreg); 422 uio.uio_segflg = UIO_USERSPACE; 423 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 424 uio.uio_procp = p; 425 return (procfs_dofpregs(p, t, NULL, &uio)); 426 } 427 #endif 428 } 429 430 #ifdef DIAGNOSTIC 431 panic("ptrace: impossible"); 432 #endif 433 return 0; 434 } 435