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