]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/mach_process.c
xnu-792.22.5.tar.gz
[apple/xnu.git] / bsd / kern / mach_process.c
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29 /*-
30 * Copyright (c) 1982, 1986, 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
67 */
68
69 #include <machine/reg.h>
70 #include <machine/psl.h>
71
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/proc_internal.h>
75 #include <sys/kauth.h>
76 #include <sys/errno.h>
77 #include <sys/ptrace.h>
78 #include <sys/uio.h>
79 #include <sys/user.h>
80 #include <sys/sysctl.h>
81 #include <sys/wait.h>
82
83 #include <sys/mount_internal.h>
84 #include <sys/sysproto.h>
85
86 #include <bsm/audit_kernel.h>
87
88 #include <kern/task.h>
89 #include <kern/thread.h>
90
91
92 /* Macros to clear/set/test flags. */
93 #define SET(t, f) (t) |= (f)
94 #define CLR(t, f) (t) &= ~(f)
95 #define ISSET(t, f) ((t) & (f))
96
97 extern thread_t port_name_to_thread(mach_port_name_t port_name);
98 extern thread_t get_firstthread(task_t);
99
100
101 /*
102 * sys-trace system call.
103 */
104
105 int
106 ptrace(p, uap, retval)
107 struct proc *p;
108 struct ptrace_args *uap;
109 register_t *retval;
110 {
111 struct proc *t = current_proc(); /* target process */
112 task_t task;
113 thread_t th_act;
114 struct uthread *ut;
115 int tr_sigexc = 0;
116
117 AUDIT_ARG(cmd, uap->req);
118 AUDIT_ARG(pid, uap->pid);
119 AUDIT_ARG(addr, uap->addr);
120 AUDIT_ARG(value, uap->data);
121
122 if (uap->req == PT_DENY_ATTACH) {
123 if (ISSET(p->p_flag, P_TRACED)) {
124 exit1(p, W_EXITCODE(ENOTSUP, 0), retval);
125 /* drop funnel before we return */
126 thread_funnel_set(kernel_flock, FALSE);
127 thread_exception_return();
128 /* NOTREACHED */
129 }
130 SET(p->p_flag, P_NOATTACH);
131
132 return(0);
133 }
134
135 if (uap->req == PT_FORCEQUOTA) {
136 if (is_suser()) {
137 SET(t->p_flag, P_FORCEQUOTA);
138 return (0);
139 } else
140 return (EPERM);
141 }
142
143 /*
144 * Intercept and deal with "please trace me" request.
145 */
146 if (uap->req == PT_TRACE_ME) {
147 SET(p->p_flag, P_TRACED);
148 /* Non-attached case, our tracer is our parent. */
149 t->p_oppid = t->p_pptr->p_pid;
150 return(0);
151 }
152 if (uap->req == PT_SIGEXC) {
153 if (ISSET(p->p_flag, P_TRACED)) {
154 SET(p->p_flag, P_SIGEXC);
155 return(0);
156 } else
157 return(EINVAL);
158 }
159
160 /*
161 * Locate victim, and make sure it is traceable.
162 */
163 if ((t = pfind(uap->pid)) == NULL)
164 return (ESRCH);
165
166 AUDIT_ARG(process, t);
167
168 /* We do not want ptrace to do anything with kernel, init
169 * and mach_init
170 */
171 if (uap->pid <=2 )
172 return (EPERM);
173
174 task = t->task;
175 if (uap->req == PT_ATTACHEXC) {
176 uap->req = PT_ATTACH;
177 tr_sigexc = 1;
178 }
179 if (uap->req == PT_ATTACH) {
180 int err;
181
182 if ( kauth_authorize_process(proc_ucred(p), KAUTH_PROCESS_CANTRACE,
183 t, (uintptr_t)&err, 0, 0) == 0 ) {
184 /* it's OK to attach */
185 SET(t->p_flag, P_TRACED);
186 if (tr_sigexc)
187 SET(t->p_flag, P_SIGEXC);
188
189 t->p_oppid = t->p_pptr->p_pid;
190 if (t->p_pptr != p)
191 proc_reparent(t, p);
192
193 if (get_task_userstop(task) == 0 ) {
194 t->p_xstat = 0;
195 psignal(t, SIGSTOP);
196 } else {
197 t->p_xstat = SIGSTOP;
198 task_resume(task);
199 }
200 return(0);
201 }
202 else {
203 /* not allowed to attach, proper error code returned by kauth_authorize_process */
204 if (ISSET(t->p_flag, P_NOATTACH)) {
205 psignal(p, SIGSEGV);
206 }
207 return (err);
208 }
209 }
210
211 /*
212 * You can't do what you want to the process if:
213 * (1) It's not being traced at all,
214 */
215 if (!ISSET(t->p_flag, P_TRACED))
216 return (EPERM);
217
218 /*
219 * (2) it's not being traced by _you_, or
220 */
221 if (t->p_pptr != p)
222 return (EBUSY);
223
224 /*
225 * (3) it's not currently stopped.
226 */
227 if (t->p_stat != SSTOP)
228 return (EBUSY);
229
230 /*
231 * Mach version of ptrace executes request directly here,
232 * thus simplifying the interaction of ptrace and signals.
233 */
234 switch (uap->req) {
235
236 case PT_DETACH:
237 if (t->p_oppid != t->p_pptr->p_pid) {
238 struct proc *pp;
239
240 pp = pfind(t->p_oppid);
241 proc_reparent(t, pp ? pp : initproc);
242 }
243
244 t->p_oppid = 0;
245 CLR(t->p_flag, P_TRACED);
246 CLR(t->p_flag, P_SIGEXC);
247 goto resume;
248
249 case PT_KILL:
250 /*
251 * Tell child process to kill itself after it
252 * is resumed by adding NSIG to p_cursig. [see issig]
253 */
254 psignal_lock(t, SIGKILL, 0);
255 goto resume;
256
257 case PT_STEP: /* single step the child */
258 case PT_CONTINUE: /* continue the child */
259 th_act = (thread_t)get_firstthread(task);
260 if (th_act == THREAD_NULL)
261 goto errorLabel;
262
263 if (uap->addr != (user_addr_t)1) {
264 #if defined(ppc)
265 #define ALIGNED(addr,size) (((unsigned)(addr)&((size)-1))==0)
266 if (!ALIGNED((int)uap->addr, sizeof(int)))
267 return (ERESTART);
268 #undef ALIGNED
269 #endif
270 thread_setentrypoint(th_act, uap->addr);
271 }
272
273 if ((unsigned)uap->data >= NSIG)
274 goto errorLabel;
275
276 if (uap->data != 0) {
277 psignal_lock(t, uap->data, 0);
278 }
279
280 if (uap->req == PT_STEP) {
281 /*
282 * set trace bit
283 */
284 thread_setsinglestep(th_act, 1);
285 } else {
286 /*
287 * clear trace bit if on
288 */
289 thread_setsinglestep(th_act, 0);
290 }
291 resume:
292 t->p_xstat = uap->data;
293 t->p_stat = SRUN;
294 if (t->sigwait) {
295 wakeup((caddr_t)&(t->sigwait));
296 if ((t->p_flag & P_SIGEXC) == 0)
297 task_release(task);
298 }
299 break;
300
301 case PT_THUPDATE: {
302 if ((unsigned)uap->data >= NSIG)
303 goto errorLabel;
304 th_act = port_name_to_thread(CAST_DOWN(mach_port_name_t, uap->addr));
305 if (th_act == THREAD_NULL)
306 return (ESRCH);
307 ut = (uthread_t)get_bsdthread_info(th_act);
308 if (uap->data)
309 ut->uu_siglist |= sigmask(uap->data);
310 t->p_xstat = uap->data;
311 t->p_stat = SRUN;
312 thread_deallocate(th_act);
313 return(0);
314 }
315 break;
316 errorLabel:
317 default:
318 return(EINVAL);
319 }
320
321 return(0);
322 }
323
324
325 /*
326 * determine if one process (cur_procp) can trace another process (traced_procp).
327 */
328
329 int
330 cantrace(proc_t cur_procp, kauth_cred_t creds, proc_t traced_procp, int *errp)
331 {
332 int my_err;
333 /*
334 * You can't trace a process if:
335 * (1) it's the process that's doing the tracing,
336 */
337 if (traced_procp->p_pid == cur_procp->p_pid) {
338 *errp = EINVAL;
339 return (0);
340 }
341
342 /*
343 * (2) it's already being traced, or
344 */
345 if (ISSET(traced_procp->p_flag, P_TRACED)) {
346 *errp = EBUSY;
347 return (0);
348 }
349
350 /*
351 * (3) it's not owned by you, or is set-id on exec
352 * (unless you're root).
353 */
354 if ((creds->cr_ruid != proc_ucred(traced_procp)->cr_ruid ||
355 ISSET(traced_procp->p_flag, P_SUGID)) &&
356 (my_err = suser(creds, &cur_procp->p_acflag)) != 0) {
357 *errp = my_err;
358 return (0);
359 }
360
361 if ((cur_procp->p_flag & P_TRACED) && isinferior(cur_procp, traced_procp)) {
362 *errp = EPERM;
363 return (0);
364 }
365
366 if (ISSET(traced_procp->p_flag, P_NOATTACH)) {
367 *errp = EBUSY;
368 return (0);
369 }
370 return(1);
371 }