]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/mach_process.c
d5dc02ed4bde09ba0f4ba0889cd08f561f0be0b4
[apple/xnu.git] / bsd / kern / mach_process.c
1 /*
2 * Copyright (c) 2000-2002 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
93
94 /* Macros to clear/set/test flags. */
95 #define SET(t, f) (t) |= (f)
96 #define CLR(t, f) (t) &= ~(f)
97 #define ISSET(t, f) ((t) & (f))
98
99 extern thread_t port_name_to_thread(mach_port_name_t port_name);
100 extern thread_t get_firstthread(task_t);
101
102
103 /*
104 * sys-trace system call.
105 */
106
107 int
108 ptrace(p, uap, retval)
109 struct proc *p;
110 struct ptrace_args *uap;
111 register_t *retval;
112 {
113 struct proc *t = current_proc(); /* target process */
114 task_t task;
115 thread_t th_act;
116 struct uthread *ut;
117 int tr_sigexc = 0;
118
119 AUDIT_ARG(cmd, uap->req);
120 AUDIT_ARG(pid, uap->pid);
121 AUDIT_ARG(addr, uap->addr);
122 AUDIT_ARG(value, uap->data);
123
124 if (uap->req == PT_DENY_ATTACH) {
125 if (ISSET(p->p_flag, P_TRACED)) {
126 exit1(p, W_EXITCODE(ENOTSUP, 0), retval);
127 /* drop funnel before we return */
128 thread_funnel_set(kernel_flock, FALSE);
129 thread_exception_return();
130 /* NOTREACHED */
131 }
132 SET(p->p_flag, P_NOATTACH);
133
134 return(0);
135 }
136
137 if (uap->req == PT_FORCEQUOTA) {
138 if (is_suser()) {
139 SET(t->p_flag, P_FORCEQUOTA);
140 return (0);
141 } else
142 return (EPERM);
143 }
144
145 /*
146 * Intercept and deal with "please trace me" request.
147 */
148 if (uap->req == PT_TRACE_ME) {
149 SET(p->p_flag, P_TRACED);
150 /* Non-attached case, our tracer is our parent. */
151 t->p_oppid = t->p_pptr->p_pid;
152 return(0);
153 }
154 if (uap->req == PT_SIGEXC) {
155 if (ISSET(p->p_flag, P_TRACED)) {
156 SET(p->p_flag, P_SIGEXC);
157 return(0);
158 } else
159 return(EINVAL);
160 }
161
162 /*
163 * Locate victim, and make sure it is traceable.
164 */
165 if ((t = pfind(uap->pid)) == NULL)
166 return (ESRCH);
167
168 AUDIT_ARG(process, t);
169
170 /* We do not want ptrace to do anything with kernel, init
171 * and mach_init
172 */
173 if (uap->pid <=2 )
174 return (EPERM);
175
176 task = t->task;
177 if (uap->req == PT_ATTACHEXC) {
178 uap->req = PT_ATTACH;
179 tr_sigexc = 1;
180 }
181 if (uap->req == PT_ATTACH) {
182 int err;
183
184 if ( kauth_authorize_process(proc_ucred(p), KAUTH_PROCESS_CANTRACE,
185 t, (uintptr_t)&err, 0, 0) == 0 ) {
186 /* it's OK to attach */
187 SET(t->p_flag, P_TRACED);
188 if (tr_sigexc)
189 SET(t->p_flag, P_SIGEXC);
190
191 t->p_oppid = t->p_pptr->p_pid;
192 if (t->p_pptr != p)
193 proc_reparent(t, p);
194
195 if (get_task_userstop(task) == 0 ) {
196 t->p_xstat = 0;
197 psignal(t, SIGSTOP);
198 } else {
199 t->p_xstat = SIGSTOP;
200 task_resume(task);
201 }
202 return(0);
203 }
204 else {
205 /* not allowed to attach, proper error code returned by kauth_authorize_process */
206 if (ISSET(t->p_flag, P_NOATTACH)) {
207 psignal(p, SIGSEGV);
208 }
209 return (err);
210 }
211 }
212
213 /*
214 * You can't do what you want to the process if:
215 * (1) It's not being traced at all,
216 */
217 if (!ISSET(t->p_flag, P_TRACED))
218 return (EPERM);
219
220 /*
221 * (2) it's not being traced by _you_, or
222 */
223 if (t->p_pptr != p)
224 return (EBUSY);
225
226 /*
227 * (3) it's not currently stopped.
228 */
229 if (t->p_stat != SSTOP)
230 return (EBUSY);
231
232 /*
233 * Mach version of ptrace executes request directly here,
234 * thus simplifying the interaction of ptrace and signals.
235 */
236 switch (uap->req) {
237
238 case PT_DETACH:
239 if (t->p_oppid != t->p_pptr->p_pid) {
240 struct proc *pp;
241
242 pp = pfind(t->p_oppid);
243 proc_reparent(t, pp ? pp : initproc);
244 }
245
246 t->p_oppid = 0;
247 CLR(t->p_flag, P_TRACED);
248 CLR(t->p_flag, P_SIGEXC);
249 goto resume;
250
251 case PT_KILL:
252 /*
253 * Tell child process to kill itself after it
254 * is resumed by adding NSIG to p_cursig. [see issig]
255 */
256 psignal_lock(t, SIGKILL, 0);
257 goto resume;
258
259 case PT_STEP: /* single step the child */
260 case PT_CONTINUE: /* continue the child */
261 th_act = (thread_t)get_firstthread(task);
262 if (th_act == THREAD_NULL)
263 goto errorLabel;
264
265 if (uap->addr != (user_addr_t)1) {
266 #if defined(ppc)
267 #define ALIGNED(addr,size) (((unsigned)(addr)&((size)-1))==0)
268 if (!ALIGNED((int)uap->addr, sizeof(int)))
269 return (ERESTART);
270 #undef ALIGNED
271 #endif
272 thread_setentrypoint(th_act, uap->addr);
273 }
274
275 if ((unsigned)uap->data >= NSIG)
276 goto errorLabel;
277
278 if (uap->data != 0) {
279 psignal_lock(t, uap->data, 0);
280 }
281
282 if (uap->req == PT_STEP) {
283 /*
284 * set trace bit
285 */
286 thread_setsinglestep(th_act, 1);
287 } else {
288 /*
289 * clear trace bit if on
290 */
291 thread_setsinglestep(th_act, 0);
292 }
293 resume:
294 t->p_xstat = uap->data;
295 t->p_stat = SRUN;
296 if (t->sigwait) {
297 wakeup((caddr_t)&(t->sigwait));
298 if ((t->p_flag & P_SIGEXC) == 0)
299 task_release(task);
300 }
301 break;
302
303 case PT_THUPDATE: {
304 if ((unsigned)uap->data >= NSIG)
305 goto errorLabel;
306 th_act = port_name_to_thread(CAST_DOWN(mach_port_name_t, uap->addr));
307 if (th_act == THREAD_NULL)
308 return (ESRCH);
309 ut = (uthread_t)get_bsdthread_info(th_act);
310 if (uap->data)
311 ut->uu_siglist |= sigmask(uap->data);
312 t->p_xstat = uap->data;
313 t->p_stat = SRUN;
314 thread_deallocate(th_act);
315 return(0);
316 }
317 break;
318 errorLabel:
319 default:
320 return(EINVAL);
321 }
322
323 return(0);
324 }
325
326
327 /*
328 * determine if one process (cur_procp) can trace another process (traced_procp).
329 */
330
331 int
332 cantrace(proc_t cur_procp, kauth_cred_t creds, proc_t traced_procp, int *errp)
333 {
334 int my_err;
335 /*
336 * You can't trace a process if:
337 * (1) it's the process that's doing the tracing,
338 */
339 if (traced_procp->p_pid == cur_procp->p_pid) {
340 *errp = EINVAL;
341 return (0);
342 }
343
344 /*
345 * (2) it's already being traced, or
346 */
347 if (ISSET(traced_procp->p_flag, P_TRACED)) {
348 *errp = EBUSY;
349 return (0);
350 }
351
352 /*
353 * (3) it's not owned by you, or is set-id on exec
354 * (unless you're root).
355 */
356 if ((creds->cr_ruid != proc_ucred(traced_procp)->cr_ruid ||
357 ISSET(traced_procp->p_flag, P_SUGID)) &&
358 (my_err = suser(creds, &cur_procp->p_acflag)) != 0) {
359 *errp = my_err;
360 return (0);
361 }
362
363 if ((cur_procp->p_flag & P_TRACED) && isinferior(cur_procp, traced_procp)) {
364 *errp = EPERM;
365 return (0);
366 }
367
368 if (ISSET(traced_procp->p_flag, P_NOATTACH)) {
369 *errp = EBUSY;
370 return (0);
371 }
372 return(1);
373 }