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