]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/mach_process.c
xnu-1699.22.73.tar.gz
[apple/xnu.git] / bsd / kern / mach_process.c
1 /*
2 * Copyright (c) 2000-2006 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 #include <sys/kdebug.h>
86
87 #include <security/audit/audit.h>
88
89 #include <kern/task.h>
90 #include <kern/thread.h>
91
92 #include <mach/task.h> /* for task_resume() */
93 #include <kern/sched_prim.h> /* for thread_exception_return() */
94
95 #include <vm/vm_protos.h> /* cs_allow_invalid() */
96
97 #include <pexpert/pexpert.h>
98
99 /* XXX ken/bsd_kern.c - prototype should be in common header */
100 int get_task_userstop(task_t);
101
102 /* Macros to clear/set/test flags. */
103 #define SET(t, f) (t) |= (f)
104 #define CLR(t, f) (t) &= ~(f)
105 #define ISSET(t, f) ((t) & (f))
106
107 extern thread_t port_name_to_thread(mach_port_name_t port_name);
108 extern thread_t get_firstthread(task_t);
109
110
111 /*
112 * sys-trace system call.
113 */
114
115 int
116 ptrace(struct proc *p, struct ptrace_args *uap, int32_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 tr_sigexc = 0;
123 int error = 0;
124 int stopped = 0;
125
126 AUDIT_ARG(cmd, uap->req);
127 AUDIT_ARG(pid, uap->pid);
128 AUDIT_ARG(addr, uap->addr);
129 AUDIT_ARG(value32, uap->data);
130
131 if (uap->req == PT_DENY_ATTACH) {
132 #if (DEVELOPMENT || DEBUG) && defined(__arm__)
133 if (PE_i_can_has_debugger(NULL))
134 return(0);
135 #endif
136 proc_lock(p);
137 if (ISSET(p->p_lflag, P_LTRACED)) {
138 proc_unlock(p);
139 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_FRCEXIT) | DBG_FUNC_NONE,
140 p->p_pid, W_EXITCODE(ENOTSUP, 0), 4, 0, 0);
141 exit1(p, W_EXITCODE(ENOTSUP, 0), retval);
142 /* drop funnel before we return */
143 thread_exception_return();
144 /* NOTREACHED */
145 }
146 SET(p->p_lflag, P_LNOATTACH);
147 proc_unlock(p);
148
149 return(0);
150 }
151
152 if (uap->req == PT_FORCEQUOTA) {
153 if (is_suser()) {
154 OSBitOrAtomic(P_FORCEQUOTA, &t->p_flag);
155 return (0);
156 } else
157 return (EPERM);
158 }
159
160 /*
161 * Intercept and deal with "please trace me" request.
162 */
163 if (uap->req == PT_TRACE_ME) {
164 proc_lock(p);
165 SET(p->p_lflag, P_LTRACED);
166 /* Non-attached case, our tracer is our parent. */
167 p->p_oppid = p->p_ppid;
168 /* Check whether child and parent are allowed to run modified
169 * code (they'll have to) */
170 struct proc *pproc=proc_find(p->p_oppid);
171 proc_unlock(p);
172 cs_allow_invalid(p);
173 if(pproc) {
174 cs_allow_invalid(pproc);
175 proc_rele(pproc);
176 }
177 return(0);
178 }
179 if (uap->req == PT_SIGEXC) {
180 proc_lock(p);
181 if (ISSET(p->p_lflag, P_LTRACED)) {
182 SET(p->p_lflag, P_LSIGEXC);
183 proc_unlock(p);
184 return(0);
185 } else {
186 proc_unlock(p);
187 return(EINVAL);
188 }
189 }
190
191 /*
192 * We do not want ptrace to do anything with kernel or launchd
193 */
194 if (uap->pid < 2) {
195 return(EPERM);
196 }
197
198 /*
199 * Locate victim, and make sure it is traceable.
200 */
201 if ((t = proc_find(uap->pid)) == NULL)
202 return (ESRCH);
203
204 AUDIT_ARG(process, t);
205
206 task = t->task;
207 if (uap->req == PT_ATTACHEXC) {
208 uap->req = PT_ATTACH;
209 tr_sigexc = 1;
210 }
211 if (uap->req == PT_ATTACH) {
212 int err;
213
214 if ( kauth_authorize_process(proc_ucred(p), KAUTH_PROCESS_CANTRACE,
215 t, (uintptr_t)&err, 0, 0) == 0 ) {
216 /* it's OK to attach */
217 proc_lock(t);
218 SET(t->p_lflag, P_LTRACED);
219 if (tr_sigexc)
220 SET(t->p_lflag, P_LSIGEXC);
221
222 t->p_oppid = t->p_ppid;
223 /* Check whether child and parent are allowed to run modified
224 * code (they'll have to) */
225 proc_unlock(t);
226 cs_allow_invalid(t);
227 cs_allow_invalid(p);
228 if (t->p_pptr != p)
229 proc_reparentlocked(t, p, 1, 0);
230
231 proc_lock(t);
232 if (get_task_userstop(task) > 0 ) {
233 stopped = 1;
234 }
235 t->p_xstat = 0;
236 proc_unlock(t);
237 psignal(t, SIGSTOP);
238 /*
239 * If the process was stopped, wake up and run through
240 * issignal() again to properly connect to the tracing
241 * process.
242 */
243 if (stopped)
244 task_resume(task);
245 error = 0;
246 goto out;
247 }
248 else {
249 /* not allowed to attach, proper error code returned by kauth_authorize_process */
250 if (ISSET(t->p_lflag, P_LNOATTACH)) {
251 psignal(p, SIGSEGV);
252 }
253
254 error = err;
255 goto out;
256 }
257 }
258
259 /*
260 * You can't do what you want to the process if:
261 * (1) It's not being traced at all,
262 */
263 proc_lock(t);
264 if (!ISSET(t->p_lflag, P_LTRACED)) {
265 proc_unlock(t);
266 error = EPERM;
267 goto out;
268 }
269
270 /*
271 * (2) it's not being traced by _you_, or
272 */
273 if (t->p_pptr != p) {
274 proc_unlock(t);
275 error = EBUSY;
276 goto out;
277 }
278
279 /*
280 * (3) it's not currently stopped.
281 */
282 if (t->p_stat != SSTOP) {
283 proc_unlock(t);
284 error = EBUSY;
285 goto out;
286 }
287
288 /*
289 * Mach version of ptrace executes request directly here,
290 * thus simplifying the interaction of ptrace and signals.
291 */
292 /* proc lock is held here */
293 switch (uap->req) {
294
295 case PT_DETACH:
296 if (t->p_oppid != t->p_ppid) {
297 struct proc *pp;
298
299 proc_unlock(t);
300 pp = proc_find(t->p_oppid);
301 proc_reparentlocked(t, pp ? pp : initproc, 1, 0);
302 if (pp != PROC_NULL)
303 proc_rele(pp);
304 proc_lock(t);
305
306 }
307
308 t->p_oppid = 0;
309 CLR(t->p_lflag, P_LTRACED);
310 CLR(t->p_lflag, P_LSIGEXC);
311 proc_unlock(t);
312 goto resume;
313
314 case PT_KILL:
315 /*
316 * Tell child process to kill itself after it
317 * is resumed by adding NSIG to p_cursig. [see issig]
318 */
319 proc_unlock(t);
320 psignal(t, SIGKILL);
321 goto resume;
322
323 case PT_STEP: /* single step the child */
324 case PT_CONTINUE: /* continue the child */
325 proc_unlock(t);
326 th_act = (thread_t)get_firstthread(task);
327 if (th_act == THREAD_NULL) {
328 error = EINVAL;
329 goto out;
330 }
331
332 if (uap->addr != (user_addr_t)1) {
333 #if defined(ppc)
334 #define ALIGNED(addr,size) (((unsigned)(addr)&((size)-1))==0)
335 if (!ALIGNED((int)uap->addr, sizeof(int)))
336 return (ERESTART);
337 #undef ALIGNED
338 #endif
339 thread_setentrypoint(th_act, uap->addr);
340 }
341
342 if ((unsigned)uap->data >= NSIG) {
343 error = EINVAL;
344 goto out;
345 }
346
347 if (uap->data != 0) {
348 psignal(t, uap->data);
349 }
350
351 if (uap->req == PT_STEP) {
352 /*
353 * set trace bit
354 */
355 if (thread_setsinglestep(th_act, 1) != KERN_SUCCESS) {
356 error = ENOTSUP;
357 goto out;
358 }
359 } else {
360 /*
361 * clear trace bit if on
362 */
363 if (thread_setsinglestep(th_act, 0) != KERN_SUCCESS) {
364 error = ENOTSUP;
365 goto out;
366 }
367 }
368 resume:
369 proc_lock(t);
370 t->p_xstat = uap->data;
371 t->p_stat = SRUN;
372 if (t->sigwait) {
373 wakeup((caddr_t)&(t->sigwait));
374 proc_unlock(t);
375 if ((t->p_lflag & P_LSIGEXC) == 0) {
376 task_resume(task);
377 }
378 } else
379 proc_unlock(t);
380
381 break;
382
383 case PT_THUPDATE: {
384 proc_unlock(t);
385 if ((unsigned)uap->data >= NSIG) {
386 error = EINVAL;
387 goto out;
388 }
389 th_act = port_name_to_thread(CAST_MACH_PORT_TO_NAME(uap->addr));
390 if (th_act == THREAD_NULL)
391 return (ESRCH);
392 ut = (uthread_t)get_bsdthread_info(th_act);
393 if (uap->data)
394 ut->uu_siglist |= sigmask(uap->data);
395 proc_lock(t);
396 t->p_xstat = uap->data;
397 t->p_stat = SRUN;
398 proc_unlock(t);
399 thread_deallocate(th_act);
400 error = 0;
401 }
402 break;
403 default:
404 proc_unlock(t);
405 error = EINVAL;
406 goto out;
407 }
408
409 error = 0;
410 out:
411 proc_rele(t);
412 return(error);
413 }
414
415
416 /*
417 * determine if one process (cur_procp) can trace another process (traced_procp).
418 */
419
420 int
421 cantrace(proc_t cur_procp, kauth_cred_t creds, proc_t traced_procp, int *errp)
422 {
423 int my_err;
424 /*
425 * You can't trace a process if:
426 * (1) it's the process that's doing the tracing,
427 */
428 if (traced_procp->p_pid == cur_procp->p_pid) {
429 *errp = EINVAL;
430 return (0);
431 }
432
433 /*
434 * (2) it's already being traced, or
435 */
436 if (ISSET(traced_procp->p_lflag, P_LTRACED)) {
437 *errp = EBUSY;
438 return (0);
439 }
440
441 /*
442 * (3) it's not owned by you, or is set-id on exec
443 * (unless you're root).
444 */
445 if ((kauth_cred_getruid(creds) != kauth_cred_getruid(proc_ucred(traced_procp)) ||
446 ISSET(traced_procp->p_flag, P_SUGID)) &&
447 (my_err = suser(creds, &cur_procp->p_acflag)) != 0) {
448 *errp = my_err;
449 return (0);
450 }
451
452 if ((cur_procp->p_lflag & P_LTRACED) && isinferior(cur_procp, traced_procp)) {
453 *errp = EPERM;
454 return (0);
455 }
456
457 if (ISSET(traced_procp->p_lflag, P_LNOATTACH)) {
458 *errp = EBUSY;
459 return (0);
460 }
461 return(1);
462 }