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