xref: /netbsd-src/sys/kern/sys_process.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: sys_process.c,v 1.57 1997/04/28 04:49:30 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 #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 == initproc && 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 being traced by procfs (which has
175 		 *	    different signal delivery semantics),
176 		 */
177 		if (ISSET(t->p_flag, P_FSTRACE))
178 			return (EBUSY);
179 
180 		/*
181 		 *	(3) it's not being traced by _you_, or
182 		 */
183 		if (t->p_pptr != p)
184 			return (EBUSY);
185 
186 		/*
187 		 *	(4) it's not currently stopped.
188 		 */
189 		if (t->p_stat != SSTOP || !ISSET(t->p_flag, P_WAITED))
190 			return (EBUSY);
191 		break;
192 
193 	default:			/* It was not a legal request. */
194 		return (EINVAL);
195 	}
196 
197 	/* Do single-step fixup if needed. */
198 	FIX_SSTEP(t);
199 
200 	/* Now do the operation. */
201 	write = 0;
202 	*retval = 0;
203 
204 	switch (SCARG(uap, req)) {
205 	case  PT_TRACE_ME:
206 		/* Just set the trace flag. */
207 		SET(t->p_flag, P_TRACED);
208 		t->p_oppid = t->p_pptr->p_pid;
209 		return (0);
210 
211 	case  PT_WRITE_I:		/* XXX no seperate I and D spaces */
212 	case  PT_WRITE_D:
213 		write = 1;
214 	case  PT_READ_I:		/* XXX no seperate I and D spaces */
215 	case  PT_READ_D:
216 		/* write = 0 done above. */
217 		iov.iov_base =
218 		    write ? (caddr_t)&SCARG(uap, data) : (caddr_t)retval;
219 		iov.iov_len = sizeof(int);
220 		uio.uio_iov = &iov;
221 		uio.uio_iovcnt = 1;
222 		uio.uio_offset = (off_t)(long)SCARG(uap, addr);
223 		uio.uio_resid = sizeof(int);
224 		uio.uio_segflg = UIO_SYSSPACE;
225 		uio.uio_rw = write ? UIO_WRITE : UIO_READ;
226 		uio.uio_procp = p;
227 		return (procfs_domem(p, t, NULL, &uio));
228 
229 #ifdef PT_STEP
230 	case  PT_STEP:
231 		/*
232 		 * From the 4.4BSD PRM:
233 		 * "Execution continues as in request PT_CONTINUE; however
234 		 * as soon as possible after execution of at least one
235 		 * instruction, execution stops again. [ ... ]"
236 		 */
237 #endif
238 	case  PT_CONTINUE:
239 	case  PT_DETACH:
240 		/*
241 		 * From the 4.4BSD PRM:
242 		 * "The data argument is taken as a signal number and the
243 		 * child's execution continues at location addr as if it
244 		 * incurred that signal.  Normally the signal number will
245 		 * be either 0 to indicate that the signal that caused the
246 		 * stop should be ignored, or that value fetched out of
247 		 * the process's image indicating which signal caused
248 		 * the stop.  If addr is (int *)1 then execution continues
249 		 * from where it stopped."
250 		 */
251 
252 		/* Check that the data is a valid signal number or zero. */
253 		if (SCARG(uap, data) < 0 || SCARG(uap, data) >= NSIG)
254 			return (EINVAL);
255 
256 		PHOLD(t);
257 
258 #ifdef PT_STEP
259 		/*
260 		 * Arrange for a single-step, if that's requested and possible.
261 		 */
262 		error = process_sstep(t, SCARG(uap, req) == PT_STEP);
263 		if (error)
264 			goto relebad;
265 #endif
266 
267 		/* If the address paramter is not (int *)1, set the pc. */
268 		if ((int *)SCARG(uap, addr) != (int *)1)
269 			if ((error = process_set_pc(t, SCARG(uap, addr))) != 0)
270 				goto relebad;
271 
272 		PRELE(t);
273 
274 		if (SCARG(uap, req) == PT_DETACH) {
275 			/* give process back to original parent or init */
276 			if (t->p_oppid != t->p_pptr->p_pid) {
277 				struct proc *pp;
278 
279 				pp = pfind(t->p_oppid);
280 				proc_reparent(t, pp ? pp : initproc);
281 			}
282 
283 			/* not being traced any more */
284 			t->p_oppid = 0;
285 			CLR(t->p_flag, P_TRACED|P_WAITED);
286 		}
287 
288 	sendsig:
289 		/* Finally, deliver the requested signal (or none). */
290 		if (t->p_stat == SSTOP) {
291 			t->p_xstat = SCARG(uap, data);
292 			setrunnable(t);
293 		} else {
294 			if (SCARG(uap, data) != 0)
295 				psignal(t, SCARG(uap, data));
296 		}
297 		return (0);
298 
299 	relebad:
300 		PRELE(t);
301 		return (error);
302 
303 	case  PT_KILL:
304 		/* just send the process a KILL signal. */
305 		SCARG(uap, data) = SIGKILL;
306 		goto sendsig;	/* in PT_CONTINUE, above. */
307 
308 	case  PT_ATTACH:
309 		/*
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_oppid = t->p_pptr->p_pid;
319 		if (t->p_pptr != p)
320 			proc_reparent(t, p);
321 		SCARG(uap, data) = SIGSTOP;
322 		goto sendsig;
323 
324 #ifdef PT_SETREGS
325 	case  PT_SETREGS:
326 		write = 1;
327 #endif
328 #ifdef PT_GETREGS
329 	case  PT_GETREGS:
330 		/* write = 0 done above. */
331 #endif
332 #if defined(PT_SETREGS) || defined(PT_GETREGS)
333 		if (!procfs_validregs(t))
334 			return (EINVAL);
335 		else {
336 			iov.iov_base = SCARG(uap, addr);
337 			iov.iov_len = sizeof(struct reg);
338 			uio.uio_iov = &iov;
339 			uio.uio_iovcnt = 1;
340 			uio.uio_offset = 0;
341 			uio.uio_resid = sizeof(struct reg);
342 			uio.uio_segflg = UIO_USERSPACE;
343 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
344 			uio.uio_procp = p;
345 			return (procfs_doregs(p, t, NULL, &uio));
346 		}
347 #endif
348 
349 #ifdef PT_SETFPREGS
350 	case  PT_SETFPREGS:
351 		write = 1;
352 #endif
353 #ifdef PT_GETFPREGS
354 	case  PT_GETFPREGS:
355 		/* write = 0 done above. */
356 #endif
357 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
358 		if (!procfs_validfpregs(t))
359 			return (EINVAL);
360 		else {
361 			iov.iov_base = SCARG(uap, addr);
362 			iov.iov_len = sizeof(struct fpreg);
363 			uio.uio_iov = &iov;
364 			uio.uio_iovcnt = 1;
365 			uio.uio_offset = 0;
366 			uio.uio_resid = sizeof(struct fpreg);
367 			uio.uio_segflg = UIO_USERSPACE;
368 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
369 			uio.uio_procp = p;
370 			return (procfs_dofpregs(p, t, NULL, &uio));
371 		}
372 #endif
373 	}
374 
375 #ifdef DIAGNOSTIC
376 	panic("ptrace: impossible");
377 #endif
378 	return 0;
379 }
380 
381 int
382 trace_req(a1)
383 	struct proc *a1;
384 {
385 
386 	/* just return 1 to keep other parts of the system happy */
387 	return (1);
388 }
389