1 /* $OpenBSD: sys_process.c,v 1.13 2001/06/27 04:49:47 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 <vm/vm.h> 69 #include <uvm/uvm_extern.h> 70 71 #include <machine/reg.h> 72 73 #include <miscfs/procfs/procfs.h> 74 75 /* Macros to clear/set/test flags. */ 76 #define SET(t, f) (t) |= (f) 77 #define CLR(t, f) (t) &= ~(f) 78 #define ISSET(t, f) ((t) & (f)) 79 80 /* 81 * Process debugging system call. 82 */ 83 int 84 sys_ptrace(p, v, retval) 85 struct proc *p; 86 void *v; 87 register_t *retval; 88 { 89 struct sys_ptrace_args /* { 90 syscallarg(int) req; 91 syscallarg(pid_t) pid; 92 syscallarg(caddr_t) addr; 93 syscallarg(int) data; 94 } */ *uap = v; 95 struct proc *t; /* target process */ 96 struct uio uio; 97 struct iovec iov; 98 int error, write; 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 case PT_READ_I: /* XXX no separate I and D spaces */ 220 case PT_READ_D: 221 /* write = 0 done above. */ 222 iov.iov_base = 223 write ? (caddr_t)&SCARG(uap, data) : (caddr_t)retval; 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 return (procfs_domem(p, t, NULL, &uio)); 233 234 #ifdef PT_STEP 235 case PT_STEP: 236 /* 237 * From the 4.4BSD PRM: 238 * "Execution continues as in request PT_CONTINUE; however 239 * as soon as possible after execution of at least one 240 * instruction, execution stops again. [ ... ]" 241 */ 242 #endif 243 case PT_CONTINUE: 244 /* 245 * From the 4.4BSD PRM: 246 * "The data argument is taken as a signal number and the 247 * child's execution continues at location addr as if it 248 * incurred that signal. Normally the signal number will 249 * be either 0 to indicate that the signal that caused the 250 * stop should be ignored, or that value fetched out of 251 * the process's image indicating which signal caused 252 * the stop. If addr is (int *)1 then execution continues 253 * from where it stopped." 254 */ 255 256 /* Check that the data is a valid signal number or zero. */ 257 if (SCARG(uap, data) < 0 || SCARG(uap, data) >= NSIG) 258 return (EINVAL); 259 260 PHOLD(t); 261 #ifdef PT_STEP 262 /* 263 * Arrange for a single-step, if that's requested and possible. 264 */ 265 error = process_sstep(t, SCARG(uap, req) == PT_STEP); 266 if (error) 267 goto relebad; 268 #endif 269 270 /* If the address paramter is not (int *)1, set the pc. */ 271 if ((int *)SCARG(uap, addr) != (int *)1) 272 if ((error = process_set_pc(t, SCARG(uap, addr))) != 0) 273 goto relebad; 274 PRELE(t); 275 goto sendsig; 276 277 case PT_DETACH: 278 /* 279 * From the 4.4BSD PRM: 280 * "The data argument is taken as a signal number and the 281 * child's execution continues at location addr as if it 282 * incurred that signal. Normally the signal number will 283 * be either 0 to indicate that the signal that caused the 284 * stop should be ignored, or that value fetched out of 285 * the process's image indicating which signal caused 286 * the stop. If addr is (int *)1 then execution continues 287 * from where it stopped." 288 */ 289 290 /* Check that the data is a valid signal number or zero. */ 291 if (SCARG(uap, data) < 0 || SCARG(uap, data) >= NSIG) 292 return (EINVAL); 293 294 PHOLD(t); 295 #ifdef PT_STEP 296 /* 297 * Arrange for a single-step, if that's requested and possible. 298 */ 299 error = process_sstep(t, SCARG(uap, req) == PT_STEP); 300 if (error) 301 goto relebad; 302 #endif 303 PRELE(t); 304 305 /* give process back to original parent or init */ 306 if (t->p_oppid != t->p_pptr->p_pid) { 307 struct proc *pp; 308 309 pp = pfind(t->p_oppid); 310 proc_reparent(t, pp ? pp : initproc); 311 } 312 313 /* not being traced any more */ 314 t->p_oppid = 0; 315 CLR(t->p_flag, P_TRACED|P_WAITED); 316 317 sendsig: 318 /* Finally, deliver the requested signal (or none). */ 319 if (t->p_stat == SSTOP) { 320 t->p_xstat = SCARG(uap, data); 321 setrunnable(t); 322 } else { 323 if (SCARG(uap, data) != 0) 324 psignal(t, SCARG(uap, data)); 325 } 326 return (0); 327 328 relebad: 329 PRELE(t); 330 return (error); 331 332 case PT_KILL: 333 /* just send the process a KILL signal. */ 334 SCARG(uap, data) = SIGKILL; 335 goto sendsig; /* in PT_CONTINUE, above. */ 336 337 case PT_ATTACH: 338 /* 339 * As done in procfs: 340 * Go ahead and set the trace flag. 341 * Save the old parent (it's reset in 342 * _DETACH, and also in kern_exit.c:wait4() 343 * Reparent the process so that the tracing 344 * proc gets to see all the action. 345 * Stop the target. 346 */ 347 SET(t->p_flag, P_TRACED); 348 t->p_oppid = t->p_pptr->p_pid; 349 if (t->p_pptr != p) 350 proc_reparent(t, p); 351 SCARG(uap, data) = SIGSTOP; 352 goto sendsig; 353 354 #ifdef PT_SETREGS 355 case PT_SETREGS: 356 write = 1; 357 #endif 358 #ifdef PT_GETREGS 359 case PT_GETREGS: 360 /* write = 0 done above. */ 361 #endif 362 #if defined(PT_SETREGS) || defined(PT_GETREGS) 363 if (!procfs_validregs(t, NULL)) 364 return (EINVAL); 365 else { 366 iov.iov_base = SCARG(uap, addr); 367 iov.iov_len = sizeof(struct reg); 368 uio.uio_iov = &iov; 369 uio.uio_iovcnt = 1; 370 uio.uio_offset = 0; 371 uio.uio_resid = sizeof(struct reg); 372 uio.uio_segflg = UIO_USERSPACE; 373 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 374 uio.uio_procp = p; 375 return (procfs_doregs(p, t, NULL, &uio)); 376 } 377 #endif 378 379 #ifdef PT_SETFPREGS 380 case PT_SETFPREGS: 381 write = 1; 382 #endif 383 #ifdef PT_GETFPREGS 384 case PT_GETFPREGS: 385 /* write = 0 done above. */ 386 #endif 387 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS) 388 if (!procfs_validfpregs(t, NULL)) 389 return (EINVAL); 390 else { 391 iov.iov_base = SCARG(uap, addr); 392 iov.iov_len = sizeof(struct fpreg); 393 uio.uio_iov = &iov; 394 uio.uio_iovcnt = 1; 395 uio.uio_offset = 0; 396 uio.uio_resid = sizeof(struct fpreg); 397 uio.uio_segflg = UIO_USERSPACE; 398 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 399 uio.uio_procp = p; 400 return (procfs_dofpregs(p, t, NULL, &uio)); 401 } 402 #endif 403 } 404 405 #ifdef DIAGNOSTIC 406 panic("ptrace: impossible"); 407 #endif 408 return 0; 409 } 410