1 /* $OpenBSD: sys_process.c,v 1.15 2002/01/02 02:38:42 art 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 /* Macros to clear/set/test flags. */ 75 #define SET(t, f) (t) |= (f) 76 #define CLR(t, f) (t) &= ~(f) 77 #define ISSET(t, f) ((t) & (f)) 78 79 /* 80 * Process debugging system call. 81 */ 82 int 83 sys_ptrace(p, v, retval) 84 struct proc *p; 85 void *v; 86 register_t *retval; 87 { 88 struct sys_ptrace_args /* { 89 syscallarg(int) req; 90 syscallarg(pid_t) pid; 91 syscallarg(caddr_t) addr; 92 syscallarg(int) data; 93 } */ *uap = v; 94 struct proc *t; /* target process */ 95 struct uio uio; 96 struct iovec iov; 97 int error, write; 98 int temp; 99 100 /* "A foolish consistency..." XXX */ 101 if (SCARG(uap, req) == PT_TRACE_ME) 102 t = p; 103 else { 104 105 /* Find the process we're supposed to be operating on. */ 106 if ((t = pfind(SCARG(uap, pid))) == NULL) 107 return (ESRCH); 108 } 109 110 /* Make sure we can operate on it. */ 111 switch (SCARG(uap, req)) { 112 case PT_TRACE_ME: 113 /* Saying that you're being traced is always legal. */ 114 break; 115 116 case PT_ATTACH: 117 /* 118 * You can't attach to a process if: 119 * (1) it's the process that's doing the attaching, 120 */ 121 if (t->p_pid == p->p_pid) 122 return (EINVAL); 123 124 /* 125 * (2) it's already being traced, or 126 */ 127 if (ISSET(t->p_flag, P_TRACED)) 128 return (EBUSY); 129 130 /* 131 * (3) it's not owned by you, or the last exec 132 * gave us setuid/setgid privs (unless 133 * you're root), or... 134 * 135 * [Note: once P_SUGID gets set in execve(), it stays 136 * set until the process does another execve(). Hence 137 * this prevents a setuid process which revokes it's 138 * special privilidges using setuid() from being 139 * traced. This is good security.] 140 */ 141 if ((t->p_cred->p_ruid != p->p_cred->p_ruid || 142 ISSET(t->p_flag, P_SUGID)) && 143 (error = suser(p->p_ucred, &p->p_acflag)) != 0) 144 return (error); 145 146 /* 147 * (4) ...it's init, which controls the security level 148 * of the entire system, and the system was not 149 * compiled with permanently insecure mode turned 150 * on. 151 */ 152 if ((t->p_pid == 1) && (securelevel > -1)) 153 return (EPERM); 154 break; 155 156 case PT_READ_I: 157 case PT_READ_D: 158 case PT_WRITE_I: 159 case PT_WRITE_D: 160 case PT_CONTINUE: 161 case PT_KILL: 162 case PT_DETACH: 163 #ifdef PT_STEP 164 case PT_STEP: 165 #endif 166 #ifdef PT_GETREGS 167 case PT_GETREGS: 168 #endif 169 #ifdef PT_SETREGS 170 case PT_SETREGS: 171 #endif 172 #ifdef PT_GETFPREGS 173 case PT_GETFPREGS: 174 #endif 175 #ifdef PT_SETFPREGS 176 case PT_SETFPREGS: 177 #endif 178 /* 179 * You can't do what you want to the process if: 180 * (1) It's not being traced at all, 181 */ 182 if (!ISSET(t->p_flag, P_TRACED)) 183 return (EPERM); 184 185 /* 186 * (2) it's not being traced by _you_, or 187 */ 188 if (t->p_pptr != p) 189 return (EBUSY); 190 191 /* 192 * (3) it's not currently stopped. 193 */ 194 if (t->p_stat != SSTOP || !ISSET(t->p_flag, P_WAITED)) 195 return (EBUSY); 196 break; 197 198 default: /* It was not a legal request. */ 199 return (EINVAL); 200 } 201 202 /* Do single-step fixup if needed. */ 203 FIX_SSTEP(t); 204 205 /* Now do the operation. */ 206 write = 0; 207 *retval = 0; 208 209 switch (SCARG(uap, req)) { 210 case PT_TRACE_ME: 211 /* Just set the trace flag. */ 212 SET(t->p_flag, P_TRACED); 213 t->p_oppid = t->p_pptr->p_pid; 214 return (0); 215 216 case PT_WRITE_I: /* XXX no separate I and D spaces */ 217 case PT_WRITE_D: 218 write = 1; 219 temp = SCARG(uap, data); 220 case PT_READ_I: /* XXX no separate I and D spaces */ 221 case PT_READ_D: 222 /* write = 0 done above. */ 223 iov.iov_base = (caddr_t)&temp; 224 iov.iov_len = sizeof(int); 225 uio.uio_iov = &iov; 226 uio.uio_iovcnt = 1; 227 uio.uio_offset = (off_t)(long)SCARG(uap, addr); 228 uio.uio_resid = sizeof(int); 229 uio.uio_segflg = UIO_SYSSPACE; 230 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 231 uio.uio_procp = p; 232 error = procfs_domem(p, t, NULL, &uio); 233 if (write == 0) 234 *retval = temp; 235 return (error); 236 237 #ifdef PT_STEP 238 case PT_STEP: 239 /* 240 * From the 4.4BSD PRM: 241 * "Execution continues as in request PT_CONTINUE; however 242 * as soon as possible after execution of at least one 243 * instruction, execution stops again. [ ... ]" 244 */ 245 #endif 246 case PT_CONTINUE: 247 /* 248 * From the 4.4BSD PRM: 249 * "The data argument is taken as a signal number and the 250 * child's execution continues at location addr as if it 251 * incurred that signal. Normally the signal number will 252 * be either 0 to indicate that the signal that caused the 253 * stop should be ignored, or that value fetched out of 254 * the process's image indicating which signal caused 255 * the stop. If addr is (int *)1 then execution continues 256 * from where it stopped." 257 */ 258 259 /* Check that the data is a valid signal number or zero. */ 260 if (SCARG(uap, data) < 0 || SCARG(uap, data) >= NSIG) 261 return (EINVAL); 262 263 PHOLD(t); 264 #ifdef PT_STEP 265 /* 266 * Arrange for a single-step, if that's requested and possible. 267 */ 268 error = process_sstep(t, SCARG(uap, req) == PT_STEP); 269 if (error) 270 goto relebad; 271 #endif 272 273 /* If the address paramter is not (int *)1, set the pc. */ 274 if ((int *)SCARG(uap, addr) != (int *)1) 275 if ((error = process_set_pc(t, SCARG(uap, addr))) != 0) 276 goto relebad; 277 PRELE(t); 278 goto sendsig; 279 280 case PT_DETACH: 281 /* 282 * From the 4.4BSD PRM: 283 * "The data argument is taken as a signal number and the 284 * child's execution continues at location addr as if it 285 * incurred that signal. Normally the signal number will 286 * be either 0 to indicate that the signal that caused the 287 * stop should be ignored, or that value fetched out of 288 * the process's image indicating which signal caused 289 * the stop. If addr is (int *)1 then execution continues 290 * from where it stopped." 291 */ 292 293 /* Check that the data is a valid signal number or zero. */ 294 if (SCARG(uap, data) < 0 || SCARG(uap, data) >= NSIG) 295 return (EINVAL); 296 297 PHOLD(t); 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 308 /* give process back to original parent or init */ 309 if (t->p_oppid != t->p_pptr->p_pid) { 310 struct proc *pp; 311 312 pp = pfind(t->p_oppid); 313 proc_reparent(t, pp ? pp : initproc); 314 } 315 316 /* not being traced any more */ 317 t->p_oppid = 0; 318 CLR(t->p_flag, P_TRACED|P_WAITED); 319 320 sendsig: 321 /* Finally, deliver the requested signal (or none). */ 322 if (t->p_stat == SSTOP) { 323 t->p_xstat = SCARG(uap, data); 324 setrunnable(t); 325 } else { 326 if (SCARG(uap, data) != 0) 327 psignal(t, SCARG(uap, data)); 328 } 329 return (0); 330 331 relebad: 332 PRELE(t); 333 return (error); 334 335 case PT_KILL: 336 /* just send the process a KILL signal. */ 337 SCARG(uap, data) = SIGKILL; 338 goto sendsig; /* in PT_CONTINUE, above. */ 339 340 case PT_ATTACH: 341 /* 342 * As done in procfs: 343 * Go ahead and set the trace flag. 344 * Save the old parent (it's reset in 345 * _DETACH, and also in kern_exit.c:wait4() 346 * Reparent the process so that the tracing 347 * proc gets to see all the action. 348 * Stop the target. 349 */ 350 SET(t->p_flag, P_TRACED); 351 t->p_oppid = t->p_pptr->p_pid; 352 if (t->p_pptr != p) 353 proc_reparent(t, p); 354 SCARG(uap, data) = SIGSTOP; 355 goto sendsig; 356 357 #ifdef PT_SETREGS 358 case PT_SETREGS: 359 write = 1; 360 #endif 361 #ifdef PT_GETREGS 362 case PT_GETREGS: 363 /* write = 0 done above. */ 364 #endif 365 #if defined(PT_SETREGS) || defined(PT_GETREGS) 366 if (!procfs_validregs(t, NULL)) 367 return (EINVAL); 368 else { 369 iov.iov_base = SCARG(uap, addr); 370 iov.iov_len = sizeof(struct reg); 371 uio.uio_iov = &iov; 372 uio.uio_iovcnt = 1; 373 uio.uio_offset = 0; 374 uio.uio_resid = sizeof(struct reg); 375 uio.uio_segflg = UIO_USERSPACE; 376 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 377 uio.uio_procp = p; 378 return (procfs_doregs(p, t, NULL, &uio)); 379 } 380 #endif 381 382 #ifdef PT_SETFPREGS 383 case PT_SETFPREGS: 384 write = 1; 385 #endif 386 #ifdef PT_GETFPREGS 387 case PT_GETFPREGS: 388 /* write = 0 done above. */ 389 #endif 390 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS) 391 if (!procfs_validfpregs(t, NULL)) 392 return (EINVAL); 393 else { 394 iov.iov_base = SCARG(uap, addr); 395 iov.iov_len = sizeof(struct fpreg); 396 uio.uio_iov = &iov; 397 uio.uio_iovcnt = 1; 398 uio.uio_offset = 0; 399 uio.uio_resid = sizeof(struct fpreg); 400 uio.uio_segflg = UIO_USERSPACE; 401 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 402 uio.uio_procp = p; 403 return (procfs_dofpregs(p, t, NULL, &uio)); 404 } 405 #endif 406 } 407 408 #ifdef DIAGNOSTIC 409 panic("ptrace: impossible"); 410 #endif 411 return 0; 412 } 413