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