]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/mach_process.c
xnu-1699.26.8.tar.gz
[apple/xnu.git] / bsd / kern / mach_process.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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 */
1c79356b
A
68
69#include <machine/reg.h>
70#include <machine/psl.h>
71
72#include <sys/param.h>
73#include <sys/systm.h>
91447636
A
74#include <sys/proc_internal.h>
75#include <sys/kauth.h>
1c79356b
A
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>
0b4e3aa0 81#include <sys/wait.h>
1c79356b 82
91447636
A
83#include <sys/mount_internal.h>
84#include <sys/sysproto.h>
b0d623f7 85#include <sys/kdebug.h>
1c79356b 86
b0d623f7 87#include <security/audit/audit.h>
e5568f75 88
1c79356b
A
89#include <kern/task.h>
90#include <kern/thread.h>
1c79356b 91
2d21ac55
A
92#include <mach/task.h> /* for task_resume() */
93#include <kern/sched_prim.h> /* for thread_exception_return() */
94
b0d623f7
A
95#include <vm/vm_protos.h> /* cs_allow_invalid() */
96
6d2010ae
A
97#include <pexpert/pexpert.h>
98
2d21ac55
A
99/* XXX ken/bsd_kern.c - prototype should be in common header */
100int get_task_userstop(task_t);
91447636 101
1c79356b
A
102/* Macros to clear/set/test flags. */
103#define SET(t, f) (t) |= (f)
104#define CLR(t, f) (t) &= ~(f)
105#define ISSET(t, f) ((t) & (f))
106
91447636 107extern thread_t port_name_to_thread(mach_port_name_t port_name);
91447636
A
108extern thread_t get_firstthread(task_t);
109
9bccf70c 110
1c79356b
A
111/*
112 * sys-trace system call.
113 */
9bccf70c 114
1c79356b 115int
b0d623f7 116ptrace(struct proc *p, struct ptrace_args *uap, int32_t *retval)
1c79356b
A
117{
118 struct proc *t = current_proc(); /* target process */
1c79356b 119 task_t task;
91447636 120 thread_t th_act;
1c79356b 121 struct uthread *ut;
9bccf70c 122 int tr_sigexc = 0;
2d21ac55
A
123 int error = 0;
124 int stopped = 0;
1c79356b 125
e5568f75
A
126 AUDIT_ARG(cmd, uap->req);
127 AUDIT_ARG(pid, uap->pid);
128 AUDIT_ARG(addr, uap->addr);
b0d623f7 129 AUDIT_ARG(value32, uap->data);
1c79356b 130
91447636 131 if (uap->req == PT_DENY_ATTACH) {
2d21ac55
A
132 proc_lock(p);
133 if (ISSET(p->p_lflag, P_LTRACED)) {
134 proc_unlock(p);
b0d623f7
A
135 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_FRCEXIT) | DBG_FUNC_NONE,
136 p->p_pid, W_EXITCODE(ENOTSUP, 0), 4, 0, 0);
91447636
A
137 exit1(p, W_EXITCODE(ENOTSUP, 0), retval);
138 /* drop funnel before we return */
91447636
A
139 thread_exception_return();
140 /* NOTREACHED */
141 }
2d21ac55
A
142 SET(p->p_lflag, P_LNOATTACH);
143 proc_unlock(p);
0b4e3aa0
A
144
145 return(0);
146 }
147
9bccf70c
A
148 if (uap->req == PT_FORCEQUOTA) {
149 if (is_suser()) {
b0d623f7 150 OSBitOrAtomic(P_FORCEQUOTA, &t->p_flag);
9bccf70c
A
151 return (0);
152 } else
153 return (EPERM);
154 }
155
1c79356b
A
156 /*
157 * Intercept and deal with "please trace me" request.
158 */
159 if (uap->req == PT_TRACE_ME) {
2d21ac55
A
160 proc_lock(p);
161 SET(p->p_lflag, P_LTRACED);
1c79356b 162 /* Non-attached case, our tracer is our parent. */
2d21ac55 163 p->p_oppid = p->p_ppid;
b0d623f7
A
164 /* Check whether child and parent are allowed to run modified
165 * code (they'll have to) */
166 struct proc *pproc=proc_find(p->p_oppid);
2d21ac55 167 proc_unlock(p);
b0d623f7 168 cs_allow_invalid(p);
6d2010ae
A
169 if(pproc) {
170 cs_allow_invalid(pproc);
171 proc_rele(pproc);
172 }
1c79356b
A
173 return(0);
174 }
9bccf70c 175 if (uap->req == PT_SIGEXC) {
2d21ac55
A
176 proc_lock(p);
177 if (ISSET(p->p_lflag, P_LTRACED)) {
178 SET(p->p_lflag, P_LSIGEXC);
179 proc_unlock(p);
9bccf70c 180 return(0);
2d21ac55
A
181 } else {
182 proc_unlock(p);
9bccf70c 183 return(EINVAL);
2d21ac55
A
184 }
185 }
186
187 /*
188 * We do not want ptrace to do anything with kernel or launchd
189 */
190 if (uap->pid < 2) {
191 return(EPERM);
9bccf70c 192 }
1c79356b
A
193
194 /*
195 * Locate victim, and make sure it is traceable.
196 */
2d21ac55 197 if ((t = proc_find(uap->pid)) == NULL)
1c79356b
A
198 return (ESRCH);
199
e5568f75
A
200 AUDIT_ARG(process, t);
201
1c79356b 202 task = t->task;
9bccf70c
A
203 if (uap->req == PT_ATTACHEXC) {
204 uap->req = PT_ATTACH;
205 tr_sigexc = 1;
206 }
1c79356b 207 if (uap->req == PT_ATTACH) {
91447636
A
208 int err;
209
210 if ( kauth_authorize_process(proc_ucred(p), KAUTH_PROCESS_CANTRACE,
211 t, (uintptr_t)&err, 0, 0) == 0 ) {
212 /* it's OK to attach */
2d21ac55
A
213 proc_lock(t);
214 SET(t->p_lflag, P_LTRACED);
91447636 215 if (tr_sigexc)
2d21ac55 216 SET(t->p_lflag, P_LSIGEXC);
91447636 217
2d21ac55 218 t->p_oppid = t->p_ppid;
b0d623f7
A
219 /* Check whether child and parent are allowed to run modified
220 * code (they'll have to) */
2d21ac55 221 proc_unlock(t);
b0d623f7
A
222 cs_allow_invalid(t);
223 cs_allow_invalid(p);
91447636 224 if (t->p_pptr != p)
2d21ac55 225 proc_reparentlocked(t, p, 1, 0);
91447636 226
2d21ac55
A
227 proc_lock(t);
228 if (get_task_userstop(task) > 0 ) {
229 stopped = 1;
91447636 230 }
2d21ac55
A
231 t->p_xstat = 0;
232 proc_unlock(t);
233 psignal(t, SIGSTOP);
234 /*
235 * If the process was stopped, wake up and run through
236 * issignal() again to properly connect to the tracing
237 * process.
238 */
239 if (stopped)
240 task_resume(task);
241 error = 0;
242 goto out;
0b4e3aa0 243 }
91447636
A
244 else {
245 /* not allowed to attach, proper error code returned by kauth_authorize_process */
2d21ac55 246 if (ISSET(t->p_lflag, P_LNOATTACH)) {
91447636
A
247 psignal(p, SIGSEGV);
248 }
2d21ac55
A
249
250 error = err;
251 goto out;
1c79356b 252 }
1c79356b
A
253 }
254
255 /*
256 * You can't do what you want to the process if:
257 * (1) It's not being traced at all,
258 */
2d21ac55
A
259 proc_lock(t);
260 if (!ISSET(t->p_lflag, P_LTRACED)) {
261 proc_unlock(t);
262 error = EPERM;
263 goto out;
264 }
1c79356b
A
265
266 /*
267 * (2) it's not being traced by _you_, or
268 */
2d21ac55
A
269 if (t->p_pptr != p) {
270 proc_unlock(t);
271 error = EBUSY;
272 goto out;
273 }
1c79356b
A
274
275 /*
276 * (3) it's not currently stopped.
277 */
2d21ac55
A
278 if (t->p_stat != SSTOP) {
279 proc_unlock(t);
280 error = EBUSY;
281 goto out;
282 }
1c79356b
A
283
284 /*
285 * Mach version of ptrace executes request directly here,
286 * thus simplifying the interaction of ptrace and signals.
287 */
2d21ac55 288 /* proc lock is held here */
1c79356b
A
289 switch (uap->req) {
290
291 case PT_DETACH:
2d21ac55 292 if (t->p_oppid != t->p_ppid) {
1c79356b
A
293 struct proc *pp;
294
2d21ac55
A
295 proc_unlock(t);
296 pp = proc_find(t->p_oppid);
297 proc_reparentlocked(t, pp ? pp : initproc, 1, 0);
298 if (pp != PROC_NULL)
299 proc_rele(pp);
300 proc_lock(t);
301
1c79356b
A
302 }
303
304 t->p_oppid = 0;
2d21ac55
A
305 CLR(t->p_lflag, P_LTRACED);
306 CLR(t->p_lflag, P_LSIGEXC);
307 proc_unlock(t);
1c79356b
A
308 goto resume;
309
310 case PT_KILL:
311 /*
312 * Tell child process to kill itself after it
313 * is resumed by adding NSIG to p_cursig. [see issig]
314 */
2d21ac55
A
315 proc_unlock(t);
316 psignal(t, SIGKILL);
1c79356b
A
317 goto resume;
318
319 case PT_STEP: /* single step the child */
320 case PT_CONTINUE: /* continue the child */
2d21ac55 321 proc_unlock(t);
91447636 322 th_act = (thread_t)get_firstthread(task);
2d21ac55
A
323 if (th_act == THREAD_NULL) {
324 error = EINVAL;
325 goto out;
326 }
0c530ab8 327
91447636 328 if (uap->addr != (user_addr_t)1) {
0c530ab8 329#if defined(ppc)
1c79356b 330#define ALIGNED(addr,size) (((unsigned)(addr)&((size)-1))==0)
2d21ac55
A
331 if (!ALIGNED((int)uap->addr, sizeof(int)))
332 return (ERESTART);
1c79356b 333#undef ALIGNED
1c79356b 334#endif
2d21ac55 335 thread_setentrypoint(th_act, uap->addr);
0c530ab8 336 }
1c79356b 337
2d21ac55
A
338 if ((unsigned)uap->data >= NSIG) {
339 error = EINVAL;
340 goto out;
341 }
1c79356b
A
342
343 if (uap->data != 0) {
2d21ac55 344 psignal(t, uap->data);
1c79356b 345 }
1c79356b
A
346
347 if (uap->req == PT_STEP) {
0c530ab8
A
348 /*
349 * set trace bit
350 */
2d21ac55
A
351 if (thread_setsinglestep(th_act, 1) != KERN_SUCCESS) {
352 error = ENOTSUP;
353 goto out;
354 }
0c530ab8
A
355 } else {
356 /*
357 * clear trace bit if on
358 */
2d21ac55
A
359 if (thread_setsinglestep(th_act, 0) != KERN_SUCCESS) {
360 error = ENOTSUP;
361 goto out;
362 }
363 }
1c79356b 364 resume:
2d21ac55 365 proc_lock(t);
1c79356b
A
366 t->p_xstat = uap->data;
367 t->p_stat = SRUN;
368 if (t->sigwait) {
369 wakeup((caddr_t)&(t->sigwait));
2d21ac55
A
370 proc_unlock(t);
371 if ((t->p_lflag & P_LSIGEXC) == 0) {
372 task_resume(task);
373 }
374 } else
375 proc_unlock(t);
376
1c79356b
A
377 break;
378
9bccf70c 379 case PT_THUPDATE: {
2d21ac55
A
380 proc_unlock(t);
381 if ((unsigned)uap->data >= NSIG) {
382 error = EINVAL;
383 goto out;
384 }
b0d623f7 385 th_act = port_name_to_thread(CAST_MACH_PORT_TO_NAME(uap->addr));
91447636 386 if (th_act == THREAD_NULL)
9bccf70c
A
387 return (ESRCH);
388 ut = (uthread_t)get_bsdthread_info(th_act);
389 if (uap->data)
390 ut->uu_siglist |= sigmask(uap->data);
2d21ac55 391 proc_lock(t);
9bccf70c
A
392 t->p_xstat = uap->data;
393 t->p_stat = SRUN;
2d21ac55 394 proc_unlock(t);
91447636 395 thread_deallocate(th_act);
2d21ac55 396 error = 0;
9bccf70c
A
397 }
398 break;
1c79356b 399 default:
2d21ac55
A
400 proc_unlock(t);
401 error = EINVAL;
402 goto out;
1c79356b 403 }
9bccf70c 404
2d21ac55
A
405 error = 0;
406out:
407 proc_rele(t);
408 return(error);
1c79356b
A
409}
410
91447636
A
411
412/*
413 * determine if one process (cur_procp) can trace another process (traced_procp).
414 */
415
416int
417cantrace(proc_t cur_procp, kauth_cred_t creds, proc_t traced_procp, int *errp)
418{
419 int my_err;
420 /*
421 * You can't trace a process if:
422 * (1) it's the process that's doing the tracing,
423 */
424 if (traced_procp->p_pid == cur_procp->p_pid) {
425 *errp = EINVAL;
426 return (0);
427 }
428
429 /*
430 * (2) it's already being traced, or
431 */
2d21ac55 432 if (ISSET(traced_procp->p_lflag, P_LTRACED)) {
91447636
A
433 *errp = EBUSY;
434 return (0);
435 }
436
437 /*
438 * (3) it's not owned by you, or is set-id on exec
439 * (unless you're root).
440 */
6d2010ae 441 if ((kauth_cred_getruid(creds) != kauth_cred_getruid(proc_ucred(traced_procp)) ||
91447636
A
442 ISSET(traced_procp->p_flag, P_SUGID)) &&
443 (my_err = suser(creds, &cur_procp->p_acflag)) != 0) {
444 *errp = my_err;
445 return (0);
446 }
447
2d21ac55 448 if ((cur_procp->p_lflag & P_LTRACED) && isinferior(cur_procp, traced_procp)) {
91447636
A
449 *errp = EPERM;
450 return (0);
451 }
452
2d21ac55 453 if (ISSET(traced_procp->p_lflag, P_LNOATTACH)) {
91447636
A
454 *errp = EBUSY;
455 return (0);
456 }
457 return(1);
458}