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