2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
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.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
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.
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
66 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
69 #include <machine/reg.h>
70 #include <machine/psl.h>
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>
80 #include <sys/sysctl.h>
83 #include <sys/mount_internal.h>
84 #include <sys/sysproto.h>
85 #include <sys/kdebug.h>
86 #include <sys/codesign.h> /* cs_allow_invalid() */
88 #include <security/audit/audit.h>
90 #include <kern/task.h>
91 #include <kern/thread.h>
93 #include <mach/task.h> /* for task_resume() */
94 #include <kern/sched_prim.h> /* for thread_exception_return() */
96 #include <pexpert/pexpert.h>
99 #include <security/mac_framework.h>
102 /* XXX ken/bsd_kern.c - prototype should be in common header */
103 int get_task_userstop(task_t
);
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))
110 extern thread_t
port_name_to_thread(mach_port_name_t port_name
);
111 extern thread_t
get_firstthread(task_t
);
115 * sys-trace system call.
119 ptrace(struct proc
*p
, struct ptrace_args
*uap
, int32_t *retval
)
121 struct proc
*t
= current_proc(); /* target process */
129 AUDIT_ARG(cmd
, uap
->req
);
130 AUDIT_ARG(pid
, uap
->pid
);
131 AUDIT_ARG(addr
, uap
->addr
);
132 AUDIT_ARG(value32
, uap
->data
);
134 if (uap
->req
== PT_DENY_ATTACH
) {
135 #if (DEVELOPMENT || DEBUG) && CONFIG_EMBEDDED
136 if (PE_i_can_has_debugger(NULL
))
140 if (ISSET(p
->p_lflag
, P_LTRACED
)) {
142 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_FRCEXIT
) | DBG_FUNC_NONE
,
143 p
->p_pid
, W_EXITCODE(ENOTSUP
, 0), 4, 0, 0);
144 exit1(p
, W_EXITCODE(ENOTSUP
, 0), retval
);
146 thread_exception_return();
149 SET(p
->p_lflag
, P_LNOATTACH
);
155 if (uap
->req
== PT_FORCEQUOTA
) {
156 if (kauth_cred_issuser(kauth_cred_get())) {
157 OSBitOrAtomic(P_FORCEQUOTA
, &t
->p_flag
);
164 * Intercept and deal with "please trace me" request.
166 if (uap
->req
== PT_TRACE_ME
) {
168 proc_t pproc
= proc_parent(p
);
173 * NB: Cannot call kauth_authorize_process(..., KAUTH_PROCESS_CANTRACE, ...)
174 * since that assumes the process being checked is the current process
175 * when, in this case, it is the current process's parent.
176 * Most of the other checks in cantrace() don't apply either.
178 if ((error
= mac_proc_check_debug(pproc
, p
)) == 0) {
181 /* Make sure the process wasn't re-parented. */
182 if (p
->p_ppid
!= pproc
->p_pid
) {
187 SET(p
->p_lflag
, P_LTRACED
);
188 /* Non-attached case, our tracer is our parent. */
189 p
->p_oppid
= p
->p_ppid
;
191 /* Child and parent will have to be able to run modified code. */
193 cs_allow_invalid(pproc
);
200 if (uap
->req
== PT_SIGEXC
) {
202 if (ISSET(p
->p_lflag
, P_LTRACED
)) {
203 SET(p
->p_lflag
, P_LSIGEXC
);
213 * We do not want ptrace to do anything with kernel or launchd
220 * Locate victim, and make sure it is traceable.
222 if ((t
= proc_find(uap
->pid
)) == NULL
)
225 AUDIT_ARG(process
, t
);
228 if (uap
->req
== PT_ATTACHEXC
) {
229 #pragma clang diagnostic push
230 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
231 uap
->req
= PT_ATTACH
;
234 if (uap
->req
== PT_ATTACH
) {
235 #pragma clang diagnostic pop
239 if (tr_sigexc
== 0) {
245 if ( kauth_authorize_process(proc_ucred(p
), KAUTH_PROCESS_CANTRACE
,
246 t
, (uintptr_t)&err
, 0, 0) == 0 ) {
247 /* it's OK to attach */
249 SET(t
->p_lflag
, P_LTRACED
);
251 SET(t
->p_lflag
, P_LSIGEXC
);
253 t
->p_oppid
= t
->p_ppid
;
254 /* Check whether child and parent are allowed to run modified
255 * code (they'll have to) */
260 proc_reparentlocked(t
, p
, 1, 0);
263 if (get_task_userstop(task
) > 0 ) {
270 * If the process was stopped, wake up and run through
271 * issignal() again to properly connect to the tracing
280 /* not allowed to attach, proper error code returned by kauth_authorize_process */
281 if (ISSET(t
->p_lflag
, P_LNOATTACH
)) {
291 * You can't do what you want to the process if:
292 * (1) It's not being traced at all,
295 if (!ISSET(t
->p_lflag
, P_LTRACED
)) {
302 * (2) it's not being traced by _you_, or
304 if (t
->p_pptr
!= p
) {
311 * (3) it's not currently stopped.
313 if (t
->p_stat
!= SSTOP
) {
320 * Mach version of ptrace executes request directly here,
321 * thus simplifying the interaction of ptrace and signals.
323 /* proc lock is held here */
327 if (t
->p_oppid
!= t
->p_ppid
) {
331 pp
= proc_find(t
->p_oppid
);
332 if (pp
!= PROC_NULL
) {
333 proc_reparentlocked(t
, pp
, 1, 0);
336 /* original parent exited while traced */
338 t
->p_listflag
|= P_LIST_DEADPARENT
;
340 proc_reparentlocked(t
, initproc
, 1, 0);
346 CLR(t
->p_lflag
, P_LTRACED
);
347 CLR(t
->p_lflag
, P_LSIGEXC
);
353 * Tell child process to kill itself after it
354 * is resumed by adding NSIG to p_cursig. [see issig]
358 error
= mac_proc_check_signal(p
, t
, SIGKILL
);
365 case PT_STEP
: /* single step the child */
366 case PT_CONTINUE
: /* continue the child */
368 th_act
= (thread_t
)get_firstthread(task
);
369 if (th_act
== THREAD_NULL
) {
374 /* force use of Mach SPIs (and task_for_pid security checks) to adjust PC */
375 if (uap
->addr
!= (user_addr_t
)1) {
380 if ((unsigned)uap
->data
>= NSIG
) {
385 if (uap
->data
!= 0) {
387 error
= mac_proc_check_signal(p
, t
, uap
->data
);
391 psignal(t
, uap
->data
);
394 if (uap
->req
== PT_STEP
) {
397 * we use sending SIGSTOP as a comparable security check.
400 error
= mac_proc_check_signal(p
, t
, SIGSTOP
);
405 if (thread_setsinglestep(th_act
, 1) != KERN_SUCCESS
) {
411 * clear trace bit if on
412 * we use sending SIGCONT as a comparable security check.
415 error
= mac_proc_check_signal(p
, t
, SIGCONT
);
420 if (thread_setsinglestep(th_act
, 0) != KERN_SUCCESS
) {
427 t
->p_xstat
= uap
->data
;
430 wakeup((caddr_t
)&(t
->sigwait
));
432 if ((t
->p_lflag
& P_LSIGEXC
) == 0) {
442 if ((unsigned)uap
->data
>= NSIG
) {
446 th_act
= port_name_to_thread(CAST_MACH_PORT_TO_NAME(uap
->addr
));
447 if (th_act
== THREAD_NULL
) {
451 ut
= (uthread_t
)get_bsdthread_info(th_act
);
453 ut
->uu_siglist
|= sigmask(uap
->data
);
455 t
->p_xstat
= uap
->data
;
458 thread_deallocate(th_act
);
476 * determine if one process (cur_procp) can trace another process (traced_procp).
480 cantrace(proc_t cur_procp
, kauth_cred_t creds
, proc_t traced_procp
, int *errp
)
484 * You can't trace a process if:
485 * (1) it's the process that's doing the tracing,
487 if (traced_procp
->p_pid
== cur_procp
->p_pid
) {
493 * (2) it's already being traced, or
495 if (ISSET(traced_procp
->p_lflag
, P_LTRACED
)) {
501 * (3) it's not owned by you, or is set-id on exec
502 * (unless you're root).
504 if ((kauth_cred_getruid(creds
) != kauth_cred_getruid(proc_ucred(traced_procp
)) ||
505 ISSET(traced_procp
->p_flag
, P_SUGID
)) &&
506 (my_err
= suser(creds
, &cur_procp
->p_acflag
)) != 0) {
511 if ((cur_procp
->p_lflag
& P_LTRACED
) && isinferior(cur_procp
, traced_procp
)) {
516 if (ISSET(traced_procp
->p_lflag
, P_LNOATTACH
)) {
522 if ((my_err
= mac_proc_check_debug(cur_procp
, traced_procp
)) != 0) {