1 /* $NetBSD: sys_process.c,v 1.70 2001/11/12 15:25:25 lukem 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.70 2001/11/12 15:25:25 lukem 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 /* 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 a system process 126 */ 127 if (t->p_flag & P_SYSTEM) 128 return (EPERM); 129 130 /* 131 * (3) it's already being traced, or 132 */ 133 if (ISSET(t->p_flag, P_TRACED)) 134 return (EBUSY); 135 136 /* 137 * (4) it's not owned by you, or is set-id on exec 138 * (unless you're root), or... 139 */ 140 if ((t->p_cred->p_ruid != p->p_cred->p_ruid || 141 ISSET(t->p_flag, P_SUGID)) && 142 (error = suser(p->p_ucred, &p->p_acflag)) != 0) 143 return (error); 144 145 /* 146 * (5) ...it's init, which controls the security level 147 * of the entire system, and the system was not 148 * compiled with permanently insecure mode turned on 149 */ 150 if (t == initproc && securelevel > -1) 151 return (EPERM); 152 153 /* 154 * (6) the tracer is chrooted, and its root directory is 155 * not at or above the root directory of the tracee 156 */ 157 158 if (!proc_isunder(t, p)) 159 return EPERM; 160 break; 161 162 case PT_READ_I: 163 case PT_READ_D: 164 case PT_WRITE_I: 165 case PT_WRITE_D: 166 case PT_CONTINUE: 167 case PT_KILL: 168 case PT_DETACH: 169 #ifdef PT_STEP 170 case PT_STEP: 171 #endif 172 #ifdef PT_GETREGS 173 case PT_GETREGS: 174 #endif 175 #ifdef PT_SETREGS 176 case PT_SETREGS: 177 #endif 178 #ifdef PT_GETFPREGS 179 case PT_GETFPREGS: 180 #endif 181 #ifdef PT_SETFPREGS 182 case PT_SETFPREGS: 183 #endif 184 /* 185 * You can't do what you want to the process if: 186 * (1) It's not being traced at all, 187 */ 188 if (!ISSET(t->p_flag, P_TRACED)) 189 return (EPERM); 190 191 /* 192 * (2) it's being traced by procfs (which has 193 * different signal delivery semantics), 194 */ 195 if (ISSET(t->p_flag, P_FSTRACE)) 196 return (EBUSY); 197 198 /* 199 * (3) it's not being traced by _you_, or 200 */ 201 if (t->p_pptr != p) 202 return (EBUSY); 203 204 /* 205 * (4) it's not currently stopped. 206 */ 207 if (t->p_stat != SSTOP || !ISSET(t->p_flag, P_WAITED)) 208 return (EBUSY); 209 break; 210 211 default: /* It was not a legal request. */ 212 return (EINVAL); 213 } 214 215 /* Do single-step fixup if needed. */ 216 FIX_SSTEP(t); 217 218 /* Now do the operation. */ 219 write = 0; 220 *retval = 0; 221 tmp = 0; 222 223 switch (SCARG(uap, req)) { 224 case PT_TRACE_ME: 225 /* Just set the trace flag. */ 226 SET(t->p_flag, P_TRACED); 227 t->p_oppid = t->p_pptr->p_pid; 228 return (0); 229 230 case PT_WRITE_I: /* XXX no separate I and D spaces */ 231 case PT_WRITE_D: 232 write = 1; 233 tmp = SCARG(uap, data); 234 case PT_READ_I: /* XXX no separate I and D spaces */ 235 case PT_READ_D: 236 /* write = 0 done above. */ 237 iov.iov_base = (caddr_t)&tmp; 238 iov.iov_len = sizeof(tmp); 239 uio.uio_iov = &iov; 240 uio.uio_iovcnt = 1; 241 uio.uio_offset = (off_t)(long)SCARG(uap, addr); 242 uio.uio_resid = sizeof(tmp); 243 uio.uio_segflg = UIO_SYSSPACE; 244 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 245 uio.uio_procp = p; 246 error = procfs_domem(p, t, NULL, &uio); 247 if (!write) 248 *retval = tmp; 249 return (error); 250 251 #ifdef PT_STEP 252 case PT_STEP: 253 /* 254 * From the 4.4BSD PRM: 255 * "Execution continues as in request PT_CONTINUE; however 256 * as soon as possible after execution of at least one 257 * instruction, execution stops again. [ ... ]" 258 */ 259 #endif 260 case PT_CONTINUE: 261 case PT_DETACH: 262 /* 263 * From the 4.4BSD PRM: 264 * "The data argument is taken as a signal number and the 265 * child's execution continues at location addr as if it 266 * incurred that signal. Normally the signal number will 267 * be either 0 to indicate that the signal that caused the 268 * stop should be ignored, or that value fetched out of 269 * the process's image indicating which signal caused 270 * the stop. If addr is (int *)1 then execution continues 271 * from where it stopped." 272 */ 273 274 /* Check that the data is a valid signal number or zero. */ 275 if (SCARG(uap, data) < 0 || SCARG(uap, data) >= NSIG) 276 return (EINVAL); 277 278 PHOLD(t); 279 280 #ifdef PT_STEP 281 /* 282 * Arrange for a single-step, if that's requested and possible. 283 */ 284 error = process_sstep(t, SCARG(uap, req) == PT_STEP); 285 if (error) 286 goto relebad; 287 #endif 288 289 /* If the address parameter is not (int *)1, set the pc. */ 290 if ((int *)SCARG(uap, addr) != (int *)1) 291 if ((error = process_set_pc(t, SCARG(uap, addr))) != 0) 292 goto relebad; 293 294 PRELE(t); 295 296 if (SCARG(uap, req) == PT_DETACH) { 297 /* give process back to original parent or init */ 298 if (t->p_oppid != t->p_pptr->p_pid) { 299 struct proc *pp; 300 301 pp = pfind(t->p_oppid); 302 proc_reparent(t, pp ? pp : initproc); 303 } 304 305 /* not being traced any more */ 306 t->p_oppid = 0; 307 CLR(t->p_flag, P_TRACED|P_WAITED); 308 } 309 310 sendsig: 311 /* Finally, deliver the requested signal (or none). */ 312 if (t->p_stat == SSTOP) { 313 t->p_xstat = SCARG(uap, data); 314 SCHED_LOCK(s); 315 setrunnable(t); 316 SCHED_UNLOCK(s); 317 } else { 318 if (SCARG(uap, data) != 0) 319 psignal(t, SCARG(uap, data)); 320 } 321 return (0); 322 323 relebad: 324 PRELE(t); 325 return (error); 326 327 case PT_KILL: 328 /* just send the process a KILL signal. */ 329 SCARG(uap, data) = SIGKILL; 330 goto sendsig; /* in PT_CONTINUE, above. */ 331 332 case PT_ATTACH: 333 /* 334 * Go ahead and set the trace flag. 335 * Save the old parent (it's reset in 336 * _DETACH, and also in kern_exit.c:wait4() 337 * Reparent the process so that the tracing 338 * proc gets to see all the action. 339 * Stop the target. 340 */ 341 SET(t->p_flag, P_TRACED); 342 t->p_oppid = t->p_pptr->p_pid; 343 if (t->p_pptr != p) 344 proc_reparent(t, p); 345 SCARG(uap, data) = SIGSTOP; 346 goto sendsig; 347 348 #ifdef PT_SETREGS 349 case PT_SETREGS: 350 write = 1; 351 #endif 352 #ifdef PT_GETREGS 353 case PT_GETREGS: 354 /* write = 0 done above. */ 355 #endif 356 #if defined(PT_SETREGS) || defined(PT_GETREGS) 357 if (!procfs_validregs(t, NULL)) 358 return (EINVAL); 359 else { 360 iov.iov_base = SCARG(uap, addr); 361 iov.iov_len = sizeof(struct reg); 362 uio.uio_iov = &iov; 363 uio.uio_iovcnt = 1; 364 uio.uio_offset = 0; 365 uio.uio_resid = sizeof(struct reg); 366 uio.uio_segflg = UIO_USERSPACE; 367 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 368 uio.uio_procp = p; 369 return (procfs_doregs(p, t, NULL, &uio)); 370 } 371 #endif 372 373 #ifdef PT_SETFPREGS 374 case PT_SETFPREGS: 375 write = 1; 376 #endif 377 #ifdef PT_GETFPREGS 378 case PT_GETFPREGS: 379 /* write = 0 done above. */ 380 #endif 381 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS) 382 if (!procfs_validfpregs(t, NULL)) 383 return (EINVAL); 384 else { 385 iov.iov_base = SCARG(uap, addr); 386 iov.iov_len = sizeof(struct fpreg); 387 uio.uio_iov = &iov; 388 uio.uio_iovcnt = 1; 389 uio.uio_offset = 0; 390 uio.uio_resid = sizeof(struct fpreg); 391 uio.uio_segflg = UIO_USERSPACE; 392 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 393 uio.uio_procp = p; 394 return (procfs_dofpregs(p, t, NULL, &uio)); 395 } 396 #endif 397 } 398 399 #ifdef DIAGNOSTIC 400 panic("ptrace: impossible"); 401 #endif 402 return 0; 403 } 404