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