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