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