1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3*43a90889SApple OSS Distributions *
4*43a90889SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*43a90889SApple OSS Distributions *
6*43a90889SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*43a90889SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*43a90889SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*43a90889SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*43a90889SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*43a90889SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*43a90889SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*43a90889SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*43a90889SApple OSS Distributions *
15*43a90889SApple OSS Distributions * Please obtain a copy of the License at
16*43a90889SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*43a90889SApple OSS Distributions *
18*43a90889SApple OSS Distributions * The Original Code and all software distributed under the License are
19*43a90889SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*43a90889SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*43a90889SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*43a90889SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*43a90889SApple OSS Distributions * Please see the License for the specific language governing rights and
24*43a90889SApple OSS Distributions * limitations under the License.
25*43a90889SApple OSS Distributions *
26*43a90889SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*43a90889SApple OSS Distributions */
28*43a90889SApple OSS Distributions /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29*43a90889SApple OSS Distributions /*-
30*43a90889SApple OSS Distributions * Copyright (c) 1982, 1986, 1989, 1993
31*43a90889SApple OSS Distributions * The Regents of the University of California. All rights reserved.
32*43a90889SApple OSS Distributions * (c) UNIX System Laboratories, Inc.
33*43a90889SApple OSS Distributions * All or some portions of this file are derived from material licensed
34*43a90889SApple OSS Distributions * to the University of California by American Telephone and Telegraph
35*43a90889SApple OSS Distributions * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36*43a90889SApple OSS Distributions * the permission of UNIX System Laboratories, Inc.
37*43a90889SApple OSS Distributions *
38*43a90889SApple OSS Distributions * Redistribution and use in source and binary forms, with or without
39*43a90889SApple OSS Distributions * modification, are permitted provided that the following conditions
40*43a90889SApple OSS Distributions * are met:
41*43a90889SApple OSS Distributions * 1. Redistributions of source code must retain the above copyright
42*43a90889SApple OSS Distributions * notice, this list of conditions and the following disclaimer.
43*43a90889SApple OSS Distributions * 2. Redistributions in binary form must reproduce the above copyright
44*43a90889SApple OSS Distributions * notice, this list of conditions and the following disclaimer in the
45*43a90889SApple OSS Distributions * documentation and/or other materials provided with the distribution.
46*43a90889SApple OSS Distributions * 3. All advertising materials mentioning features or use of this software
47*43a90889SApple OSS Distributions * must display the following acknowledgement:
48*43a90889SApple OSS Distributions * This product includes software developed by the University of
49*43a90889SApple OSS Distributions * California, Berkeley and its contributors.
50*43a90889SApple OSS Distributions * 4. Neither the name of the University nor the names of its contributors
51*43a90889SApple OSS Distributions * may be used to endorse or promote products derived from this software
52*43a90889SApple OSS Distributions * without specific prior written permission.
53*43a90889SApple OSS Distributions *
54*43a90889SApple OSS Distributions * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55*43a90889SApple OSS Distributions * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56*43a90889SApple OSS Distributions * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57*43a90889SApple OSS Distributions * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58*43a90889SApple OSS Distributions * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59*43a90889SApple OSS Distributions * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60*43a90889SApple OSS Distributions * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61*43a90889SApple OSS Distributions * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62*43a90889SApple OSS Distributions * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63*43a90889SApple OSS Distributions * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64*43a90889SApple OSS Distributions * SUCH DAMAGE.
65*43a90889SApple OSS Distributions *
66*43a90889SApple OSS Distributions * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
67*43a90889SApple OSS Distributions */
68*43a90889SApple OSS Distributions
69*43a90889SApple OSS Distributions #include <machine/reg.h>
70*43a90889SApple OSS Distributions #include <machine/psl.h>
71*43a90889SApple OSS Distributions
72*43a90889SApple OSS Distributions #include <sys/param.h>
73*43a90889SApple OSS Distributions #include <sys/systm.h>
74*43a90889SApple OSS Distributions #include <sys/proc_internal.h>
75*43a90889SApple OSS Distributions #include <sys/kauth.h>
76*43a90889SApple OSS Distributions #include <sys/errno.h>
77*43a90889SApple OSS Distributions #include <sys/ptrace.h>
78*43a90889SApple OSS Distributions #include <sys/uio.h>
79*43a90889SApple OSS Distributions #include <sys/user.h>
80*43a90889SApple OSS Distributions #include <sys/sysctl.h>
81*43a90889SApple OSS Distributions #include <sys/wait.h>
82*43a90889SApple OSS Distributions
83*43a90889SApple OSS Distributions #include <sys/mount_internal.h>
84*43a90889SApple OSS Distributions #include <sys/sysproto.h>
85*43a90889SApple OSS Distributions #include <sys/kdebug.h>
86*43a90889SApple OSS Distributions #include <sys/codesign.h> /* cs_allow_invalid() */
87*43a90889SApple OSS Distributions
88*43a90889SApple OSS Distributions #include <security/audit/audit.h>
89*43a90889SApple OSS Distributions
90*43a90889SApple OSS Distributions #include <kern/task.h>
91*43a90889SApple OSS Distributions #include <kern/thread.h>
92*43a90889SApple OSS Distributions
93*43a90889SApple OSS Distributions #include <mach/task.h> /* for task_resume() */
94*43a90889SApple OSS Distributions #include <kern/sched_prim.h> /* for thread_exception_return() */
95*43a90889SApple OSS Distributions
96*43a90889SApple OSS Distributions #include <pexpert/pexpert.h>
97*43a90889SApple OSS Distributions
98*43a90889SApple OSS Distributions #if CONFIG_MACF
99*43a90889SApple OSS Distributions #include <security/mac_framework.h>
100*43a90889SApple OSS Distributions #endif
101*43a90889SApple OSS Distributions
102*43a90889SApple OSS Distributions /* XXX ken/bsd_kern.c - prototype should be in common header */
103*43a90889SApple OSS Distributions int get_task_userstop(task_t);
104*43a90889SApple OSS Distributions
105*43a90889SApple OSS Distributions /* Macros to clear/set/test flags. */
106*43a90889SApple OSS Distributions #define SET(t, f) (t) |= (f)
107*43a90889SApple OSS Distributions #define CLR(t, f) (t) &= ~(f)
108*43a90889SApple OSS Distributions #define ISSET(t, f) ((t) & (f))
109*43a90889SApple OSS Distributions
110*43a90889SApple OSS Distributions extern thread_t get_firstthread(task_t);
111*43a90889SApple OSS Distributions
112*43a90889SApple OSS Distributions
113*43a90889SApple OSS Distributions /*
114*43a90889SApple OSS Distributions * sys-trace system call.
115*43a90889SApple OSS Distributions */
116*43a90889SApple OSS Distributions
117*43a90889SApple OSS Distributions int
ptrace(struct proc * p,struct ptrace_args * uap,int32_t * retval)118*43a90889SApple OSS Distributions ptrace(struct proc *p, struct ptrace_args *uap, int32_t *retval)
119*43a90889SApple OSS Distributions {
120*43a90889SApple OSS Distributions struct proc *t; /* target process */
121*43a90889SApple OSS Distributions struct proc_ident tident; /* target ident */
122*43a90889SApple OSS Distributions task_t task;
123*43a90889SApple OSS Distributions thread_t th_act = THREAD_NULL;
124*43a90889SApple OSS Distributions struct uthread *ut;
125*43a90889SApple OSS Distributions int tr_sigexc = 0;
126*43a90889SApple OSS Distributions int error = 0;
127*43a90889SApple OSS Distributions int stopped = 0;
128*43a90889SApple OSS Distributions
129*43a90889SApple OSS Distributions AUDIT_ARG(cmd, uap->req);
130*43a90889SApple OSS Distributions AUDIT_ARG(pid, uap->pid);
131*43a90889SApple OSS Distributions AUDIT_ARG(addr, uap->addr);
132*43a90889SApple OSS Distributions AUDIT_ARG(value32, uap->data);
133*43a90889SApple OSS Distributions
134*43a90889SApple OSS Distributions if (uap->req == PT_DENY_ATTACH) {
135*43a90889SApple OSS Distributions #if (DEVELOPMENT || DEBUG) && !defined(XNU_TARGET_OS_OSX)
136*43a90889SApple OSS Distributions if (PE_i_can_has_debugger(NULL)) {
137*43a90889SApple OSS Distributions return 0;
138*43a90889SApple OSS Distributions }
139*43a90889SApple OSS Distributions #endif
140*43a90889SApple OSS Distributions proc_lock(p);
141*43a90889SApple OSS Distributions if (ISSET(p->p_lflag, P_LTRACED)) {
142*43a90889SApple OSS Distributions proc_unlock(p);
143*43a90889SApple OSS Distributions KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_FRCEXIT) | DBG_FUNC_NONE,
144*43a90889SApple OSS Distributions proc_getpid(p), W_EXITCODE(ENOTSUP, 0), 4, 0, 0);
145*43a90889SApple OSS Distributions exit1(p, W_EXITCODE(ENOTSUP, 0), retval);
146*43a90889SApple OSS Distributions
147*43a90889SApple OSS Distributions thread_exception_return();
148*43a90889SApple OSS Distributions /* NOTREACHED */
149*43a90889SApple OSS Distributions }
150*43a90889SApple OSS Distributions SET(p->p_lflag, P_LNOATTACH);
151*43a90889SApple OSS Distributions proc_unlock(p);
152*43a90889SApple OSS Distributions
153*43a90889SApple OSS Distributions return 0;
154*43a90889SApple OSS Distributions }
155*43a90889SApple OSS Distributions
156*43a90889SApple OSS Distributions if (uap->req == PT_FORCEQUOTA) {
157*43a90889SApple OSS Distributions if (kauth_cred_issuser(kauth_cred_get())) {
158*43a90889SApple OSS Distributions t = current_proc();
159*43a90889SApple OSS Distributions OSBitOrAtomic(P_FORCEQUOTA, &t->p_flag);
160*43a90889SApple OSS Distributions return 0;
161*43a90889SApple OSS Distributions } else {
162*43a90889SApple OSS Distributions return EPERM;
163*43a90889SApple OSS Distributions }
164*43a90889SApple OSS Distributions }
165*43a90889SApple OSS Distributions
166*43a90889SApple OSS Distributions /*
167*43a90889SApple OSS Distributions * Intercept and deal with "please trace me" request.
168*43a90889SApple OSS Distributions */
169*43a90889SApple OSS Distributions if (uap->req == PT_TRACE_ME) {
170*43a90889SApple OSS Distributions retry_trace_me: ;
171*43a90889SApple OSS Distributions proc_t pproc = proc_parent(p);
172*43a90889SApple OSS Distributions if (pproc == NULL) {
173*43a90889SApple OSS Distributions return EINVAL;
174*43a90889SApple OSS Distributions }
175*43a90889SApple OSS Distributions /* holding ref on pproc */
176*43a90889SApple OSS Distributions
177*43a90889SApple OSS Distributions #if CONFIG_MACF
178*43a90889SApple OSS Distributions /*
179*43a90889SApple OSS Distributions * NB: Cannot call kauth_authorize_process(..., KAUTH_PROCESS_CANTRACE, ...)
180*43a90889SApple OSS Distributions * since that assumes the process being checked is the current process
181*43a90889SApple OSS Distributions * when, in this case, it is the current process's parent.
182*43a90889SApple OSS Distributions * Most of the other checks in cantrace() don't apply either.
183*43a90889SApple OSS Distributions */
184*43a90889SApple OSS Distributions struct proc_ident p_ident = proc_ident(p);
185*43a90889SApple OSS Distributions struct proc_ident pproc_ident = proc_ident(pproc);
186*43a90889SApple OSS Distributions kauth_cred_t pproc_cred = kauth_cred_proc_ref(pproc);
187*43a90889SApple OSS Distributions
188*43a90889SApple OSS Distributions /* Release pproc and find it again after MAC call to avoid deadlock */
189*43a90889SApple OSS Distributions proc_rele(pproc);
190*43a90889SApple OSS Distributions error = mac_proc_check_debug(&pproc_ident, pproc_cred, &p_ident);
191*43a90889SApple OSS Distributions kauth_cred_unref(&pproc_cred);
192*43a90889SApple OSS Distributions
193*43a90889SApple OSS Distributions if (error != 0) {
194*43a90889SApple OSS Distributions return error;
195*43a90889SApple OSS Distributions }
196*43a90889SApple OSS Distributions if (proc_find_ident(&pproc_ident) == PROC_NULL) {
197*43a90889SApple OSS Distributions return ESRCH;
198*43a90889SApple OSS Distributions }
199*43a90889SApple OSS Distributions /* re-holding ref on pproc */
200*43a90889SApple OSS Distributions #endif
201*43a90889SApple OSS Distributions proc_lock(p);
202*43a90889SApple OSS Distributions /* Make sure the process wasn't re-parented. */
203*43a90889SApple OSS Distributions if (p->p_ppid != proc_getpid(pproc)) {
204*43a90889SApple OSS Distributions proc_unlock(p);
205*43a90889SApple OSS Distributions proc_rele(pproc); /* pproc ref released */
206*43a90889SApple OSS Distributions goto retry_trace_me;
207*43a90889SApple OSS Distributions }
208*43a90889SApple OSS Distributions SET(p->p_lflag, P_LTRACED);
209*43a90889SApple OSS Distributions /* Non-attached case, our tracer is our parent. */
210*43a90889SApple OSS Distributions p->p_oppid = p->p_ppid;
211*43a90889SApple OSS Distributions proc_unlock(p);
212*43a90889SApple OSS Distributions /* Child and parent will have to be able to run modified code. */
213*43a90889SApple OSS Distributions cs_allow_invalid(p);
214*43a90889SApple OSS Distributions cs_allow_invalid(pproc);
215*43a90889SApple OSS Distributions proc_rele(pproc); /* pproc ref released */
216*43a90889SApple OSS Distributions
217*43a90889SApple OSS Distributions return error;
218*43a90889SApple OSS Distributions }
219*43a90889SApple OSS Distributions if (uap->req == PT_SIGEXC) {
220*43a90889SApple OSS Distributions proc_lock(p);
221*43a90889SApple OSS Distributions if (ISSET(p->p_lflag, P_LTRACED)) {
222*43a90889SApple OSS Distributions SET(p->p_lflag, P_LSIGEXC);
223*43a90889SApple OSS Distributions proc_unlock(p);
224*43a90889SApple OSS Distributions return 0;
225*43a90889SApple OSS Distributions } else {
226*43a90889SApple OSS Distributions proc_unlock(p);
227*43a90889SApple OSS Distributions return EINVAL;
228*43a90889SApple OSS Distributions }
229*43a90889SApple OSS Distributions }
230*43a90889SApple OSS Distributions
231*43a90889SApple OSS Distributions /*
232*43a90889SApple OSS Distributions * We do not want ptrace to do anything with kernel or launchd
233*43a90889SApple OSS Distributions */
234*43a90889SApple OSS Distributions if (uap->pid < 2) {
235*43a90889SApple OSS Distributions return EPERM;
236*43a90889SApple OSS Distributions }
237*43a90889SApple OSS Distributions
238*43a90889SApple OSS Distributions retry_proc_find:
239*43a90889SApple OSS Distributions /*
240*43a90889SApple OSS Distributions * Locate victim, and make sure it is traceable.
241*43a90889SApple OSS Distributions */
242*43a90889SApple OSS Distributions if ((t = proc_find(uap->pid)) == NULL) {
243*43a90889SApple OSS Distributions return ESRCH;
244*43a90889SApple OSS Distributions }
245*43a90889SApple OSS Distributions
246*43a90889SApple OSS Distributions /* Check if the proc has trace wait flag set */
247*43a90889SApple OSS Distributions if (t->p_lflag & P_LTRACE_WAIT) {
248*43a90889SApple OSS Distributions proc_rele(t);
249*43a90889SApple OSS Distributions delay(1);
250*43a90889SApple OSS Distributions goto retry_proc_find;
251*43a90889SApple OSS Distributions }
252*43a90889SApple OSS Distributions
253*43a90889SApple OSS Distributions AUDIT_ARG(process, t);
254*43a90889SApple OSS Distributions
255*43a90889SApple OSS Distributions task = proc_task(t);
256*43a90889SApple OSS Distributions tident = proc_ident(t);
257*43a90889SApple OSS Distributions if (uap->req == PT_ATTACHEXC) {
258*43a90889SApple OSS Distributions #pragma clang diagnostic push
259*43a90889SApple OSS Distributions #pragma clang diagnostic ignored "-Wdeprecated-declarations"
260*43a90889SApple OSS Distributions uap->req = PT_ATTACH;
261*43a90889SApple OSS Distributions tr_sigexc = 1;
262*43a90889SApple OSS Distributions }
263*43a90889SApple OSS Distributions if (uap->req == PT_ATTACH) {
264*43a90889SApple OSS Distributions #pragma clang diagnostic pop
265*43a90889SApple OSS Distributions int err, cb_err;
266*43a90889SApple OSS Distributions
267*43a90889SApple OSS Distributions #if !defined(XNU_TARGET_OS_OSX)
268*43a90889SApple OSS Distributions if (tr_sigexc == 0) {
269*43a90889SApple OSS Distributions error = ENOTSUP;
270*43a90889SApple OSS Distributions goto out;
271*43a90889SApple OSS Distributions }
272*43a90889SApple OSS Distributions #endif
273*43a90889SApple OSS Distributions
274*43a90889SApple OSS Distributions err = kauth_authorize_process(kauth_cred_get(), KAUTH_PROCESS_CANTRACE,
275*43a90889SApple OSS Distributions t, (uintptr_t)&cb_err, 0, 0);
276*43a90889SApple OSS Distributions
277*43a90889SApple OSS Distributions if (err == 0) {
278*43a90889SApple OSS Distributions /* it's OK to attach */
279*43a90889SApple OSS Distributions proc_lock(t);
280*43a90889SApple OSS Distributions SET(t->p_lflag, P_LTRACED);
281*43a90889SApple OSS Distributions if (tr_sigexc) {
282*43a90889SApple OSS Distributions SET(t->p_lflag, P_LSIGEXC);
283*43a90889SApple OSS Distributions }
284*43a90889SApple OSS Distributions
285*43a90889SApple OSS Distributions t->p_oppid = t->p_ppid;
286*43a90889SApple OSS Distributions /* Check whether child and parent are allowed to run modified
287*43a90889SApple OSS Distributions * code (they'll have to) */
288*43a90889SApple OSS Distributions proc_unlock(t);
289*43a90889SApple OSS Distributions cs_allow_invalid(t);
290*43a90889SApple OSS Distributions cs_allow_invalid(p);
291*43a90889SApple OSS Distributions if (t->p_pptr != p) {
292*43a90889SApple OSS Distributions proc_reparentlocked(t, p, 1, 0);
293*43a90889SApple OSS Distributions }
294*43a90889SApple OSS Distributions
295*43a90889SApple OSS Distributions proc_lock(t);
296*43a90889SApple OSS Distributions if (get_task_userstop(task) > 0) {
297*43a90889SApple OSS Distributions stopped = 1;
298*43a90889SApple OSS Distributions }
299*43a90889SApple OSS Distributions t->p_xstat = 0;
300*43a90889SApple OSS Distributions proc_unlock(t);
301*43a90889SApple OSS Distributions psignal(t, SIGSTOP);
302*43a90889SApple OSS Distributions /*
303*43a90889SApple OSS Distributions * If the process was stopped, wake up and run through
304*43a90889SApple OSS Distributions * issignal() again to properly connect to the tracing
305*43a90889SApple OSS Distributions * process.
306*43a90889SApple OSS Distributions */
307*43a90889SApple OSS Distributions if (stopped) {
308*43a90889SApple OSS Distributions task_resume(task);
309*43a90889SApple OSS Distributions }
310*43a90889SApple OSS Distributions error = 0;
311*43a90889SApple OSS Distributions goto out;
312*43a90889SApple OSS Distributions } else {
313*43a90889SApple OSS Distributions error = cb_err;
314*43a90889SApple OSS Distributions if (error == ESRCH) {
315*43a90889SApple OSS Distributions /*
316*43a90889SApple OSS Distributions * The target 't' is not valid anymore as it
317*43a90889SApple OSS Distributions * could not be found after the MAC check.
318*43a90889SApple OSS Distributions */
319*43a90889SApple OSS Distributions return error;
320*43a90889SApple OSS Distributions }
321*43a90889SApple OSS Distributions /* not allowed to attach, proper error code returned by kauth_authorize_process */
322*43a90889SApple OSS Distributions if (ISSET(t->p_lflag, P_LNOATTACH)) {
323*43a90889SApple OSS Distributions psignal(p, SIGSEGV);
324*43a90889SApple OSS Distributions }
325*43a90889SApple OSS Distributions goto out;
326*43a90889SApple OSS Distributions }
327*43a90889SApple OSS Distributions }
328*43a90889SApple OSS Distributions
329*43a90889SApple OSS Distributions /*
330*43a90889SApple OSS Distributions * You can't do what you want to the process if:
331*43a90889SApple OSS Distributions * (1) It's not being traced at all,
332*43a90889SApple OSS Distributions */
333*43a90889SApple OSS Distributions proc_lock(t);
334*43a90889SApple OSS Distributions if (!ISSET(t->p_lflag, P_LTRACED)) {
335*43a90889SApple OSS Distributions proc_unlock(t);
336*43a90889SApple OSS Distributions error = EPERM;
337*43a90889SApple OSS Distributions goto out;
338*43a90889SApple OSS Distributions }
339*43a90889SApple OSS Distributions
340*43a90889SApple OSS Distributions /*
341*43a90889SApple OSS Distributions * (2) it's not being traced by _you_, or
342*43a90889SApple OSS Distributions */
343*43a90889SApple OSS Distributions if (t->p_pptr != p) {
344*43a90889SApple OSS Distributions proc_unlock(t);
345*43a90889SApple OSS Distributions error = EBUSY;
346*43a90889SApple OSS Distributions goto out;
347*43a90889SApple OSS Distributions }
348*43a90889SApple OSS Distributions
349*43a90889SApple OSS Distributions /*
350*43a90889SApple OSS Distributions * (3) it's not currently stopped.
351*43a90889SApple OSS Distributions */
352*43a90889SApple OSS Distributions if (t->p_stat != SSTOP) {
353*43a90889SApple OSS Distributions proc_unlock(t);
354*43a90889SApple OSS Distributions error = EBUSY;
355*43a90889SApple OSS Distributions goto out;
356*43a90889SApple OSS Distributions }
357*43a90889SApple OSS Distributions
358*43a90889SApple OSS Distributions /*
359*43a90889SApple OSS Distributions * Mach version of ptrace executes request directly here,
360*43a90889SApple OSS Distributions * thus simplifying the interaction of ptrace and signals.
361*43a90889SApple OSS Distributions */
362*43a90889SApple OSS Distributions /* proc lock is held here */
363*43a90889SApple OSS Distributions switch (uap->req) {
364*43a90889SApple OSS Distributions case PT_DETACH:
365*43a90889SApple OSS Distributions if (t->p_oppid != t->p_ppid) {
366*43a90889SApple OSS Distributions struct proc *pp;
367*43a90889SApple OSS Distributions
368*43a90889SApple OSS Distributions proc_unlock(t);
369*43a90889SApple OSS Distributions pp = proc_find(t->p_oppid);
370*43a90889SApple OSS Distributions if (pp != PROC_NULL) {
371*43a90889SApple OSS Distributions proc_reparentlocked(t, pp, 1, 0);
372*43a90889SApple OSS Distributions proc_rele(pp);
373*43a90889SApple OSS Distributions } else {
374*43a90889SApple OSS Distributions /* original parent exited while traced */
375*43a90889SApple OSS Distributions proc_list_lock();
376*43a90889SApple OSS Distributions t->p_listflag |= P_LIST_DEADPARENT;
377*43a90889SApple OSS Distributions proc_list_unlock();
378*43a90889SApple OSS Distributions proc_reparentlocked(t, initproc, 1, 0);
379*43a90889SApple OSS Distributions }
380*43a90889SApple OSS Distributions proc_lock(t);
381*43a90889SApple OSS Distributions }
382*43a90889SApple OSS Distributions
383*43a90889SApple OSS Distributions t->p_oppid = 0;
384*43a90889SApple OSS Distributions CLR(t->p_lflag, P_LTRACED);
385*43a90889SApple OSS Distributions CLR(t->p_lflag, P_LSIGEXC);
386*43a90889SApple OSS Distributions proc_unlock(t);
387*43a90889SApple OSS Distributions goto resume;
388*43a90889SApple OSS Distributions
389*43a90889SApple OSS Distributions case PT_KILL:
390*43a90889SApple OSS Distributions /*
391*43a90889SApple OSS Distributions * Tell child process to kill itself after it
392*43a90889SApple OSS Distributions * is resumed by adding NSIG to p_cursig. [see issig]
393*43a90889SApple OSS Distributions */
394*43a90889SApple OSS Distributions proc_unlock(t);
395*43a90889SApple OSS Distributions #if CONFIG_MACF
396*43a90889SApple OSS Distributions error = mac_proc_check_signal(p, NULL, &tident, SIGKILL);
397*43a90889SApple OSS Distributions if (0 != error) {
398*43a90889SApple OSS Distributions goto resume;
399*43a90889SApple OSS Distributions }
400*43a90889SApple OSS Distributions #endif
401*43a90889SApple OSS Distributions psignal(t, SIGKILL);
402*43a90889SApple OSS Distributions goto resume;
403*43a90889SApple OSS Distributions
404*43a90889SApple OSS Distributions case PT_STEP: /* single step the child */
405*43a90889SApple OSS Distributions case PT_CONTINUE: /* continue the child */
406*43a90889SApple OSS Distributions proc_unlock(t);
407*43a90889SApple OSS Distributions th_act = (thread_t)get_firstthread(task);
408*43a90889SApple OSS Distributions if (th_act == THREAD_NULL) {
409*43a90889SApple OSS Distributions error = EINVAL;
410*43a90889SApple OSS Distributions goto out;
411*43a90889SApple OSS Distributions }
412*43a90889SApple OSS Distributions
413*43a90889SApple OSS Distributions /* force use of Mach SPIs (and task_for_pid security checks) to adjust PC */
414*43a90889SApple OSS Distributions if (uap->addr != (user_addr_t)1) {
415*43a90889SApple OSS Distributions error = ENOTSUP;
416*43a90889SApple OSS Distributions goto out;
417*43a90889SApple OSS Distributions }
418*43a90889SApple OSS Distributions
419*43a90889SApple OSS Distributions if ((unsigned)uap->data >= NSIG) {
420*43a90889SApple OSS Distributions error = EINVAL;
421*43a90889SApple OSS Distributions goto out;
422*43a90889SApple OSS Distributions }
423*43a90889SApple OSS Distributions
424*43a90889SApple OSS Distributions if (uap->data != 0) {
425*43a90889SApple OSS Distributions #if CONFIG_MACF
426*43a90889SApple OSS Distributions error = mac_proc_check_signal(p, NULL, &tident, uap->data);
427*43a90889SApple OSS Distributions if (0 != error) {
428*43a90889SApple OSS Distributions goto out;
429*43a90889SApple OSS Distributions }
430*43a90889SApple OSS Distributions #endif
431*43a90889SApple OSS Distributions psignal(t, uap->data);
432*43a90889SApple OSS Distributions }
433*43a90889SApple OSS Distributions
434*43a90889SApple OSS Distributions if (uap->req == PT_STEP) {
435*43a90889SApple OSS Distributions /*
436*43a90889SApple OSS Distributions * set trace bit
437*43a90889SApple OSS Distributions * we use sending SIGSTOP as a comparable security check.
438*43a90889SApple OSS Distributions */
439*43a90889SApple OSS Distributions #if CONFIG_MACF
440*43a90889SApple OSS Distributions error = mac_proc_check_signal(p, NULL, &tident, SIGSTOP);
441*43a90889SApple OSS Distributions if (0 != error) {
442*43a90889SApple OSS Distributions goto out;
443*43a90889SApple OSS Distributions }
444*43a90889SApple OSS Distributions #endif
445*43a90889SApple OSS Distributions if (thread_setsinglestep(th_act, 1) != KERN_SUCCESS) {
446*43a90889SApple OSS Distributions error = ENOTSUP;
447*43a90889SApple OSS Distributions goto out;
448*43a90889SApple OSS Distributions }
449*43a90889SApple OSS Distributions } else {
450*43a90889SApple OSS Distributions /*
451*43a90889SApple OSS Distributions * clear trace bit if on
452*43a90889SApple OSS Distributions * we use sending SIGCONT as a comparable security check.
453*43a90889SApple OSS Distributions */
454*43a90889SApple OSS Distributions #if CONFIG_MACF
455*43a90889SApple OSS Distributions error = mac_proc_check_signal(p, NULL, &tident, SIGCONT);
456*43a90889SApple OSS Distributions if (0 != error) {
457*43a90889SApple OSS Distributions goto out;
458*43a90889SApple OSS Distributions }
459*43a90889SApple OSS Distributions #endif
460*43a90889SApple OSS Distributions if (thread_setsinglestep(th_act, 0) != KERN_SUCCESS) {
461*43a90889SApple OSS Distributions error = ENOTSUP;
462*43a90889SApple OSS Distributions goto out;
463*43a90889SApple OSS Distributions }
464*43a90889SApple OSS Distributions }
465*43a90889SApple OSS Distributions resume:
466*43a90889SApple OSS Distributions proc_lock(t);
467*43a90889SApple OSS Distributions t->p_xstat = uap->data;
468*43a90889SApple OSS Distributions t->p_stat = SRUN;
469*43a90889SApple OSS Distributions if (t->sigwait) {
470*43a90889SApple OSS Distributions wakeup((caddr_t)&(t->sigwait));
471*43a90889SApple OSS Distributions proc_unlock(t);
472*43a90889SApple OSS Distributions if ((t->p_lflag & P_LSIGEXC) == 0) {
473*43a90889SApple OSS Distributions task_resume(task);
474*43a90889SApple OSS Distributions }
475*43a90889SApple OSS Distributions } else {
476*43a90889SApple OSS Distributions proc_unlock(t);
477*43a90889SApple OSS Distributions }
478*43a90889SApple OSS Distributions
479*43a90889SApple OSS Distributions break;
480*43a90889SApple OSS Distributions
481*43a90889SApple OSS Distributions case PT_THUPDATE: {
482*43a90889SApple OSS Distributions proc_unlock(t);
483*43a90889SApple OSS Distributions if ((unsigned)uap->data >= NSIG) {
484*43a90889SApple OSS Distributions error = EINVAL;
485*43a90889SApple OSS Distributions goto out;
486*43a90889SApple OSS Distributions }
487*43a90889SApple OSS Distributions th_act = port_name_to_thread(CAST_MACH_PORT_TO_NAME(uap->addr),
488*43a90889SApple OSS Distributions PORT_INTRANS_OPTIONS_NONE);
489*43a90889SApple OSS Distributions if (th_act == THREAD_NULL) {
490*43a90889SApple OSS Distributions error = ESRCH;
491*43a90889SApple OSS Distributions goto out;
492*43a90889SApple OSS Distributions }
493*43a90889SApple OSS Distributions ut = (uthread_t)get_bsdthread_info(th_act);
494*43a90889SApple OSS Distributions if (uap->data) {
495*43a90889SApple OSS Distributions ut->uu_siglist |= sigmask(uap->data);
496*43a90889SApple OSS Distributions }
497*43a90889SApple OSS Distributions proc_lock(t);
498*43a90889SApple OSS Distributions t->p_xstat = uap->data;
499*43a90889SApple OSS Distributions t->p_stat = SRUN;
500*43a90889SApple OSS Distributions proc_unlock(t);
501*43a90889SApple OSS Distributions error = 0;
502*43a90889SApple OSS Distributions }
503*43a90889SApple OSS Distributions break;
504*43a90889SApple OSS Distributions default:
505*43a90889SApple OSS Distributions proc_unlock(t);
506*43a90889SApple OSS Distributions error = EINVAL;
507*43a90889SApple OSS Distributions goto out;
508*43a90889SApple OSS Distributions }
509*43a90889SApple OSS Distributions
510*43a90889SApple OSS Distributions error = 0;
511*43a90889SApple OSS Distributions out:
512*43a90889SApple OSS Distributions proc_rele(t);
513*43a90889SApple OSS Distributions if (th_act) {
514*43a90889SApple OSS Distributions thread_deallocate(th_act);
515*43a90889SApple OSS Distributions }
516*43a90889SApple OSS Distributions return error;
517*43a90889SApple OSS Distributions }
518*43a90889SApple OSS Distributions
519*43a90889SApple OSS Distributions
520*43a90889SApple OSS Distributions /*
521*43a90889SApple OSS Distributions * determine if one process (cur_procp) can trace another process (traced_procp).
522*43a90889SApple OSS Distributions */
523*43a90889SApple OSS Distributions
524*43a90889SApple OSS Distributions int
cantrace(proc_t cur_procp,kauth_cred_t creds,proc_t traced_procp,int * errp)525*43a90889SApple OSS Distributions cantrace(proc_t cur_procp, kauth_cred_t creds, proc_t traced_procp, int *errp)
526*43a90889SApple OSS Distributions {
527*43a90889SApple OSS Distributions int my_err;
528*43a90889SApple OSS Distributions /*
529*43a90889SApple OSS Distributions * You can't trace a process if:
530*43a90889SApple OSS Distributions * (1) it's the process that's doing the tracing,
531*43a90889SApple OSS Distributions */
532*43a90889SApple OSS Distributions if (proc_getpid(traced_procp) == proc_getpid(cur_procp)) {
533*43a90889SApple OSS Distributions *errp = EINVAL;
534*43a90889SApple OSS Distributions return 0;
535*43a90889SApple OSS Distributions }
536*43a90889SApple OSS Distributions
537*43a90889SApple OSS Distributions /*
538*43a90889SApple OSS Distributions * (2) it's already being traced, or
539*43a90889SApple OSS Distributions */
540*43a90889SApple OSS Distributions if (ISSET(traced_procp->p_lflag, P_LTRACED)) {
541*43a90889SApple OSS Distributions *errp = EBUSY;
542*43a90889SApple OSS Distributions return 0;
543*43a90889SApple OSS Distributions }
544*43a90889SApple OSS Distributions
545*43a90889SApple OSS Distributions if (!proc_is_third_party_debuggable_driver(traced_procp)) {
546*43a90889SApple OSS Distributions kauth_cred_t traced_cred;
547*43a90889SApple OSS Distributions
548*43a90889SApple OSS Distributions /*
549*43a90889SApple OSS Distributions * (3) it's not owned by you, or is set-id on exec
550*43a90889SApple OSS Distributions * (unless you're root).
551*43a90889SApple OSS Distributions */
552*43a90889SApple OSS Distributions traced_cred = kauth_cred_proc_ref(traced_procp);
553*43a90889SApple OSS Distributions if ((kauth_cred_getruid(creds) != kauth_cred_getruid(traced_cred) ||
554*43a90889SApple OSS Distributions ISSET(traced_procp->p_flag, P_SUGID)) &&
555*43a90889SApple OSS Distributions (my_err = suser(creds, &cur_procp->p_acflag)) != 0) {
556*43a90889SApple OSS Distributions kauth_cred_unref(&traced_cred);
557*43a90889SApple OSS Distributions *errp = my_err;
558*43a90889SApple OSS Distributions return 0;
559*43a90889SApple OSS Distributions }
560*43a90889SApple OSS Distributions kauth_cred_unref(&traced_cred);
561*43a90889SApple OSS Distributions }
562*43a90889SApple OSS Distributions
563*43a90889SApple OSS Distributions if ((cur_procp->p_lflag & P_LTRACED) && isinferior(cur_procp, traced_procp)) {
564*43a90889SApple OSS Distributions *errp = EPERM;
565*43a90889SApple OSS Distributions return 0;
566*43a90889SApple OSS Distributions }
567*43a90889SApple OSS Distributions
568*43a90889SApple OSS Distributions if (ISSET(traced_procp->p_lflag, P_LNOATTACH)) {
569*43a90889SApple OSS Distributions *errp = EBUSY;
570*43a90889SApple OSS Distributions return 0;
571*43a90889SApple OSS Distributions }
572*43a90889SApple OSS Distributions
573*43a90889SApple OSS Distributions #if CONFIG_MACF
574*43a90889SApple OSS Distributions struct proc_ident cur_ident = proc_ident(cur_procp);
575*43a90889SApple OSS Distributions struct proc_ident traced_ident = proc_ident(traced_procp);
576*43a90889SApple OSS Distributions kauth_cred_t cur_cred = kauth_cred_proc_ref(cur_procp);
577*43a90889SApple OSS Distributions
578*43a90889SApple OSS Distributions /*
579*43a90889SApple OSS Distributions * Drop the proc reference to avoid a deadlock during an upcall and find
580*43a90889SApple OSS Distributions * (reference) the proc again so our caller can keep using it.
581*43a90889SApple OSS Distributions */
582*43a90889SApple OSS Distributions proc_rele(traced_procp);
583*43a90889SApple OSS Distributions my_err = mac_proc_check_debug(&cur_ident, cur_cred, &traced_ident);
584*43a90889SApple OSS Distributions kauth_cred_unref(&cur_cred);
585*43a90889SApple OSS Distributions
586*43a90889SApple OSS Distributions if (proc_find_ident(&traced_ident) == PROC_NULL) {
587*43a90889SApple OSS Distributions *errp = ESRCH;
588*43a90889SApple OSS Distributions return 0;
589*43a90889SApple OSS Distributions }
590*43a90889SApple OSS Distributions /* restored ref on traced_procp */
591*43a90889SApple OSS Distributions if (my_err != 0) {
592*43a90889SApple OSS Distributions *errp = my_err;
593*43a90889SApple OSS Distributions return 0;
594*43a90889SApple OSS Distributions }
595*43a90889SApple OSS Distributions #endif
596*43a90889SApple OSS Distributions return 1;
597*43a90889SApple OSS Distributions }
598