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