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