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