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