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