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