]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/mach_process.c
xnu-792.2.4.tar.gz
[apple/xnu.git] / bsd / kern / mach_process.c
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
e5568f75
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
1c79356b 11 *
e5568f75
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
e5568f75
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23/*-
24 * Copyright (c) 1982, 1986, 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
61 */
1c79356b
A
62
63#include <machine/reg.h>
64#include <machine/psl.h>
65
66#include <sys/param.h>
67#include <sys/systm.h>
91447636
A
68#include <sys/proc_internal.h>
69#include <sys/kauth.h>
1c79356b
A
70#include <sys/errno.h>
71#include <sys/ptrace.h>
72#include <sys/uio.h>
73#include <sys/user.h>
74#include <sys/sysctl.h>
0b4e3aa0 75#include <sys/wait.h>
1c79356b 76
91447636
A
77#include <sys/mount_internal.h>
78#include <sys/sysproto.h>
1c79356b 79
e5568f75
A
80#include <bsm/audit_kernel.h>
81
1c79356b
A
82#include <kern/task.h>
83#include <kern/thread.h>
84#include <mach/machine/thread_status.h>
85
91447636 86
1c79356b
A
87/* Macros to clear/set/test flags. */
88#define SET(t, f) (t) |= (f)
89#define CLR(t, f) (t) &= ~(f)
90#define ISSET(t, f) ((t) & (f))
91
91447636
A
92extern thread_t port_name_to_thread(mach_port_name_t port_name);
93extern kern_return_t thread_getstatus(thread_t thread, int flavor, thread_state_t tstate, mach_msg_type_number_t *count);
94extern thread_t get_firstthread(task_t);
95
96#if defined (ppc)
97extern kern_return_t thread_setstatus(thread_t thread, int flavor, thread_state_t tstate, mach_msg_type_number_t count);
98#endif
9bccf70c 99
1c79356b
A
100/*
101 * sys-trace system call.
102 */
9bccf70c 103
1c79356b
A
104int
105ptrace(p, uap, retval)
106 struct proc *p;
107 struct ptrace_args *uap;
108 register_t *retval;
109{
110 struct proc *t = current_proc(); /* target process */
1c79356b 111 task_t task;
91447636 112 thread_t th_act;
1c79356b
A
113 struct uthread *ut;
114 int *locr0;
1c79356b 115#if defined(ppc)
55e303ae 116 struct ppc_thread_state64 statep;
1c79356b
A
117#elif defined(i386)
118 struct i386_saved_state statep;
119#else
120#error architecture not supported
121#endif
122 unsigned long state_count;
9bccf70c 123 int tr_sigexc = 0;
1c79356b 124
e5568f75
A
125 AUDIT_ARG(cmd, uap->req);
126 AUDIT_ARG(pid, uap->pid);
127 AUDIT_ARG(addr, uap->addr);
128 AUDIT_ARG(value, uap->data);
1c79356b 129
91447636
A
130 if (uap->req == PT_DENY_ATTACH) {
131 if (ISSET(p->p_flag, P_TRACED)) {
132 exit1(p, W_EXITCODE(ENOTSUP, 0), retval);
133 /* drop funnel before we return */
134 thread_funnel_set(kernel_flock, FALSE);
135 thread_exception_return();
136 /* NOTREACHED */
137 }
0b4e3aa0
A
138 SET(p->p_flag, P_NOATTACH);
139
140 return(0);
141 }
142
9bccf70c
A
143 if (uap->req == PT_FORCEQUOTA) {
144 if (is_suser()) {
145 SET(t->p_flag, P_FORCEQUOTA);
146 return (0);
147 } else
148 return (EPERM);
149 }
150
1c79356b
A
151 /*
152 * Intercept and deal with "please trace me" request.
153 */
154 if (uap->req == PT_TRACE_ME) {
155 SET(p->p_flag, P_TRACED);
156 /* Non-attached case, our tracer is our parent. */
157 t->p_oppid = t->p_pptr->p_pid;
158 return(0);
159 }
9bccf70c
A
160 if (uap->req == PT_SIGEXC) {
161 if (ISSET(p->p_flag, P_TRACED)) {
162 SET(p->p_flag, P_SIGEXC);
163 return(0);
164 } else
165 return(EINVAL);
166 }
1c79356b
A
167
168 /*
169 * Locate victim, and make sure it is traceable.
170 */
171 if ((t = pfind(uap->pid)) == NULL)
172 return (ESRCH);
173
e5568f75
A
174 AUDIT_ARG(process, t);
175
1c79356b
A
176 /* We do not want ptrace to do anything with kernel, init
177 * and mach_init
178 */
179 if (uap->pid <=2 )
180 return (EPERM);
181
182 task = t->task;
9bccf70c
A
183 if (uap->req == PT_ATTACHEXC) {
184 uap->req = PT_ATTACH;
185 tr_sigexc = 1;
186 }
1c79356b 187 if (uap->req == PT_ATTACH) {
91447636
A
188 int err;
189
190 if ( kauth_authorize_process(proc_ucred(p), KAUTH_PROCESS_CANTRACE,
191 t, (uintptr_t)&err, 0, 0) == 0 ) {
192 /* it's OK to attach */
193 SET(t->p_flag, P_TRACED);
194 if (tr_sigexc)
195 SET(t->p_flag, P_SIGEXC);
196
197 t->p_oppid = t->p_pptr->p_pid;
198 if (t->p_pptr != p)
199 proc_reparent(t, p);
200
201 if (get_task_userstop(task) == 0 ) {
202 t->p_xstat = 0;
203 psignal(t, SIGSTOP);
204 } else {
205 t->p_xstat = SIGSTOP;
206 task_resume(task);
207 }
208 return(0);
0b4e3aa0 209 }
91447636
A
210 else {
211 /* not allowed to attach, proper error code returned by kauth_authorize_process */
212 if (ISSET(t->p_flag, P_NOATTACH)) {
213 psignal(p, SIGSEGV);
214 }
215 return (err);
1c79356b 216 }
1c79356b
A
217 }
218
219 /*
220 * You can't do what you want to the process if:
221 * (1) It's not being traced at all,
222 */
223 if (!ISSET(t->p_flag, P_TRACED))
224 return (EPERM);
225
226 /*
227 * (2) it's not being traced by _you_, or
228 */
229 if (t->p_pptr != p)
230 return (EBUSY);
231
232 /*
233 * (3) it's not currently stopped.
234 */
235 if (t->p_stat != SSTOP)
236 return (EBUSY);
237
238 /*
239 * Mach version of ptrace executes request directly here,
240 * thus simplifying the interaction of ptrace and signals.
241 */
242 switch (uap->req) {
243
244 case PT_DETACH:
245 if (t->p_oppid != t->p_pptr->p_pid) {
246 struct proc *pp;
247
248 pp = pfind(t->p_oppid);
249 proc_reparent(t, pp ? pp : initproc);
250 }
251
252 t->p_oppid = 0;
253 CLR(t->p_flag, P_TRACED);
9bccf70c 254 CLR(t->p_flag, P_SIGEXC);
1c79356b
A
255 goto resume;
256
257 case PT_KILL:
258 /*
259 * Tell child process to kill itself after it
260 * is resumed by adding NSIG to p_cursig. [see issig]
261 */
9bccf70c 262 psignal_lock(t, SIGKILL, 0);
1c79356b
A
263 goto resume;
264
265 case PT_STEP: /* single step the child */
266 case PT_CONTINUE: /* continue the child */
91447636
A
267 th_act = (thread_t)get_firstthread(task);
268 if (th_act == THREAD_NULL)
1c79356b
A
269 goto errorLabel;
270 ut = (uthread_t)get_bsdthread_info(th_act);
271 locr0 = ut->uu_ar0;
272#if defined(i386)
273 state_count = i386_NEW_THREAD_STATE_COUNT;
9bccf70c 274 if (thread_getstatus(th_act, i386_NEW_THREAD_STATE, &statep, &state_count) != KERN_SUCCESS) {
1c79356b
A
275 goto errorLabel;
276 }
277#elif defined(ppc)
55e303ae 278 state_count = PPC_THREAD_STATE64_COUNT;
91447636 279 if (thread_getstatus(th_act, PPC_THREAD_STATE64, (thread_state_t)&statep, (mach_msg_type_number_t *)&state_count) != KERN_SUCCESS) {
1c79356b
A
280 goto errorLabel;
281 }
282#else
283#error architecture not supported
284#endif
91447636 285 if (uap->addr != (user_addr_t)1) {
1c79356b
A
286#if defined(i386)
287 locr0[PC] = (int)uap->addr;
288#elif defined(ppc)
289#define ALIGNED(addr,size) (((unsigned)(addr)&((size)-1))==0)
290 if (!ALIGNED((int)uap->addr, sizeof(int)))
291 return (ERESTART);
292
91447636 293 statep.srr0 = uap->addr;
55e303ae 294 state_count = PPC_THREAD_STATE64_COUNT;
91447636 295 if (thread_setstatus(th_act, PPC_THREAD_STATE64, (thread_state_t)&statep, state_count) != KERN_SUCCESS) {
1c79356b
A
296 goto errorLabel;
297 }
298#undef ALIGNED
299#else
300#error architecture not implemented!
301#endif
91447636 302 } /* uap->addr != (user_addr_t)1 */
1c79356b 303
91447636 304 if ((unsigned)uap->data >= NSIG)
1c79356b
A
305 goto errorLabel;
306
307 if (uap->data != 0) {
9bccf70c 308 psignal_lock(t, uap->data, 0);
1c79356b
A
309 }
310#if defined(ppc)
55e303ae 311 state_count = PPC_THREAD_STATE64_COUNT;
91447636 312 if (thread_getstatus(th_act, PPC_THREAD_STATE64, (thread_state_t)&statep, (mach_msg_type_number_t *)&state_count) != KERN_SUCCESS) {
1c79356b
A
313 goto errorLabel;
314 }
315#endif
316
317#define MSR_SE_BIT 21
318
319 if (uap->req == PT_STEP) {
320#if defined(i386)
321 locr0[PS] |= PSL_T;
322#elif defined(ppc)
323 statep.srr1 |= MASK(MSR_SE);
324#else
325#error architecture not implemented!
326#endif
327 } /* uap->req == PT_STEP */
328 else { /* PT_CONTINUE - clear trace bit if set */
329#if defined(i386)
330 locr0[PS] &= ~PSL_T;
331#elif defined(ppc)
332 statep.srr1 &= ~MASK(MSR_SE);
333#endif
334 }
335#if defined (ppc)
55e303ae 336 state_count = PPC_THREAD_STATE64_COUNT;
91447636 337 if (thread_setstatus(th_act, PPC_THREAD_STATE64, (thread_state_t)&statep, state_count) != KERN_SUCCESS) {
1c79356b
A
338 goto errorLabel;
339 }
340#endif
341 resume:
342 t->p_xstat = uap->data;
343 t->p_stat = SRUN;
344 if (t->sigwait) {
345 wakeup((caddr_t)&(t->sigwait));
55e303ae
A
346 if ((t->p_flag & P_SIGEXC) == 0)
347 task_release(task);
1c79356b
A
348 }
349 break;
350
9bccf70c 351 case PT_THUPDATE: {
9bccf70c
A
352 if ((unsigned)uap->data >= NSIG)
353 goto errorLabel;
91447636
A
354 th_act = port_name_to_thread(CAST_DOWN(mach_port_name_t, uap->addr));
355 if (th_act == THREAD_NULL)
9bccf70c
A
356 return (ESRCH);
357 ut = (uthread_t)get_bsdthread_info(th_act);
358 if (uap->data)
359 ut->uu_siglist |= sigmask(uap->data);
360 t->p_xstat = uap->data;
361 t->p_stat = SRUN;
91447636 362 thread_deallocate(th_act);
9bccf70c
A
363 return(0);
364 }
365 break;
366errorLabel:
1c79356b 367 default:
1c79356b
A
368 return(EINVAL);
369 }
9bccf70c 370
1c79356b
A
371 return(0);
372}
373
91447636
A
374
375/*
376 * determine if one process (cur_procp) can trace another process (traced_procp).
377 */
378
379int
380cantrace(proc_t cur_procp, kauth_cred_t creds, proc_t traced_procp, int *errp)
381{
382 int my_err;
383 /*
384 * You can't trace a process if:
385 * (1) it's the process that's doing the tracing,
386 */
387 if (traced_procp->p_pid == cur_procp->p_pid) {
388 *errp = EINVAL;
389 return (0);
390 }
391
392 /*
393 * (2) it's already being traced, or
394 */
395 if (ISSET(traced_procp->p_flag, P_TRACED)) {
396 *errp = EBUSY;
397 return (0);
398 }
399
400 /*
401 * (3) it's not owned by you, or is set-id on exec
402 * (unless you're root).
403 */
404 if ((creds->cr_ruid != proc_ucred(traced_procp)->cr_ruid ||
405 ISSET(traced_procp->p_flag, P_SUGID)) &&
406 (my_err = suser(creds, &cur_procp->p_acflag)) != 0) {
407 *errp = my_err;
408 return (0);
409 }
410
411 if ((cur_procp->p_flag & P_TRACED) && isinferior(cur_procp, traced_procp)) {
412 *errp = EPERM;
413 return (0);
414 }
415
416 if (ISSET(traced_procp->p_flag, P_NOATTACH)) {
417 *errp = EBUSY;
418 return (0);
419 }
420 return(1);
421}