1 /* $NetBSD: sys_process.c,v 1.72 2002/01/11 21:16:28 christos 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/cdefs.h> 56 __KERNEL_RCSID(0, "$NetBSD: sys_process.c,v 1.72 2002/01/11 21:16:28 christos Exp $"); 57 58 #include <sys/param.h> 59 #include <sys/systm.h> 60 #include <sys/proc.h> 61 #include <sys/errno.h> 62 #include <sys/ptrace.h> 63 #include <sys/uio.h> 64 #include <sys/user.h> 65 66 #include <sys/mount.h> 67 #include <sys/syscallargs.h> 68 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 s, error, write, tmp; 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 /* Can't trace a process that's currently exec'ing. */ 111 if ((t->p_flag & P_INEXEC) != 0) 112 return EAGAIN; 113 114 /* Make sure we can operate on it. */ 115 switch (SCARG(uap, req)) { 116 case PT_TRACE_ME: 117 /* Saying that you're being traced is always legal. */ 118 break; 119 120 case PT_ATTACH: 121 /* 122 * You can't attach to a process if: 123 * (1) it's the process that's doing the attaching, 124 */ 125 if (t->p_pid == p->p_pid) 126 return (EINVAL); 127 128 /* 129 * (2) it's a system process 130 */ 131 if (t->p_flag & P_SYSTEM) 132 return (EPERM); 133 134 /* 135 * (3) it's already being traced, or 136 */ 137 if (ISSET(t->p_flag, P_TRACED)) 138 return (EBUSY); 139 140 /* 141 * (4) it's not owned by you, or is set-id on exec 142 * (unless you're root), or... 143 */ 144 if ((t->p_cred->p_ruid != p->p_cred->p_ruid || 145 ISSET(t->p_flag, P_SUGID)) && 146 (error = suser(p->p_ucred, &p->p_acflag)) != 0) 147 return (error); 148 149 /* 150 * (5) ...it's init, which controls the security level 151 * of the entire system, and the system was not 152 * compiled with permanently insecure mode turned on 153 */ 154 if (t == initproc && securelevel > -1) 155 return (EPERM); 156 157 /* 158 * (6) the tracer is chrooted, and its root directory is 159 * not at or above the root directory of the tracee 160 */ 161 162 if (!proc_isunder(t, p)) 163 return EPERM; 164 break; 165 166 case PT_READ_I: 167 case PT_READ_D: 168 case PT_WRITE_I: 169 case PT_WRITE_D: 170 case PT_CONTINUE: 171 case PT_KILL: 172 case PT_DETACH: 173 #ifdef PT_STEP 174 case PT_STEP: 175 #endif 176 #ifdef PT_GETREGS 177 case PT_GETREGS: 178 #endif 179 #ifdef PT_SETREGS 180 case PT_SETREGS: 181 #endif 182 #ifdef PT_GETFPREGS 183 case PT_GETFPREGS: 184 #endif 185 #ifdef PT_SETFPREGS 186 case PT_SETFPREGS: 187 #endif 188 189 #ifdef __HAVE_PTRACE_MACHDEP 190 PTRACE_MACHDEP_REQUEST_CASES 191 #endif 192 193 /* 194 * You can't do what you want to the process if: 195 * (1) It's not being traced at all, 196 */ 197 if (!ISSET(t->p_flag, P_TRACED)) 198 return (EPERM); 199 200 /* 201 * (2) it's being traced by procfs (which has 202 * different signal delivery semantics), 203 */ 204 if (ISSET(t->p_flag, P_FSTRACE)) 205 return (EBUSY); 206 207 /* 208 * (3) it's not being traced by _you_, or 209 */ 210 if (t->p_pptr != p) 211 return (EBUSY); 212 213 /* 214 * (4) it's not currently stopped. 215 */ 216 if (t->p_stat != SSTOP || !ISSET(t->p_flag, P_WAITED)) 217 return (EBUSY); 218 break; 219 220 default: /* It was not a legal request. */ 221 return (EINVAL); 222 } 223 224 /* Do single-step fixup if needed. */ 225 FIX_SSTEP(t); 226 227 /* Now do the operation. */ 228 write = 0; 229 *retval = 0; 230 tmp = 0; 231 232 switch (SCARG(uap, req)) { 233 case PT_TRACE_ME: 234 /* Just set the trace flag. */ 235 SET(t->p_flag, P_TRACED); 236 t->p_oppid = t->p_pptr->p_pid; 237 return (0); 238 239 case PT_WRITE_I: /* XXX no separate I and D spaces */ 240 case PT_WRITE_D: 241 write = 1; 242 tmp = SCARG(uap, data); 243 case PT_READ_I: /* XXX no separate I and D spaces */ 244 case PT_READ_D: 245 /* write = 0 done above. */ 246 iov.iov_base = (caddr_t)&tmp; 247 iov.iov_len = sizeof(tmp); 248 uio.uio_iov = &iov; 249 uio.uio_iovcnt = 1; 250 uio.uio_offset = (off_t)(long)SCARG(uap, addr); 251 uio.uio_resid = sizeof(tmp); 252 uio.uio_segflg = UIO_SYSSPACE; 253 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 254 uio.uio_procp = p; 255 error = procfs_domem(p, t, NULL, &uio); 256 if (!write) 257 *retval = tmp; 258 return (error); 259 260 #ifdef PT_STEP 261 case PT_STEP: 262 /* 263 * From the 4.4BSD PRM: 264 * "Execution continues as in request PT_CONTINUE; however 265 * as soon as possible after execution of at least one 266 * instruction, execution stops again. [ ... ]" 267 */ 268 #endif 269 case PT_CONTINUE: 270 case PT_DETACH: 271 /* 272 * From the 4.4BSD PRM: 273 * "The data argument is taken as a signal number and the 274 * child's execution continues at location addr as if it 275 * incurred that signal. Normally the signal number will 276 * be either 0 to indicate that the signal that caused the 277 * stop should be ignored, or that value fetched out of 278 * the process's image indicating which signal caused 279 * the stop. If addr is (int *)1 then execution continues 280 * from where it stopped." 281 */ 282 283 /* Check that the data is a valid signal number or zero. */ 284 if (SCARG(uap, data) < 0 || SCARG(uap, data) >= NSIG) 285 return (EINVAL); 286 287 PHOLD(t); 288 289 #ifdef PT_STEP 290 /* 291 * Arrange for a single-step, if that's requested and possible. 292 */ 293 error = process_sstep(t, SCARG(uap, req) == PT_STEP); 294 if (error) 295 goto relebad; 296 #endif 297 298 /* If the address parameter is not (int *)1, set the pc. */ 299 if ((int *)SCARG(uap, addr) != (int *)1) 300 if ((error = process_set_pc(t, SCARG(uap, addr))) != 0) 301 goto relebad; 302 303 PRELE(t); 304 305 if (SCARG(uap, req) == PT_DETACH) { 306 /* give process back to original parent or init */ 307 if (t->p_oppid != t->p_pptr->p_pid) { 308 struct proc *pp; 309 310 pp = pfind(t->p_oppid); 311 proc_reparent(t, pp ? pp : initproc); 312 } 313 314 /* not being traced any more */ 315 t->p_oppid = 0; 316 CLR(t->p_flag, P_TRACED|P_WAITED); 317 } 318 319 sendsig: 320 /* Finally, deliver the requested signal (or none). */ 321 if (t->p_stat == SSTOP) { 322 t->p_xstat = SCARG(uap, data); 323 SCHED_LOCK(s); 324 setrunnable(t); 325 SCHED_UNLOCK(s); 326 } else { 327 if (SCARG(uap, data) != 0) 328 psignal(t, SCARG(uap, data)); 329 } 330 return (0); 331 332 relebad: 333 PRELE(t); 334 return (error); 335 336 case PT_KILL: 337 /* just send the process a KILL signal. */ 338 SCARG(uap, data) = SIGKILL; 339 goto sendsig; /* in PT_CONTINUE, above. */ 340 341 case PT_ATTACH: 342 /* 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 #ifdef __HAVE_PTRACE_MACHDEP 408 PTRACE_MACHDEP_REQUEST_CASES 409 return (ptrace_machdep_dorequest(p, t, 410 SCARG(uap, req), SCARG(uap, addr), 411 SCARG(uap, data))); 412 #endif 413 } 414 415 #ifdef DIAGNOSTIC 416 panic("ptrace: impossible"); 417 #endif 418 return 0; 419 } 420