]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/mach_process.c
xnu-4903.221.2.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>
39236c6e 86#include <sys/codesign.h> /* cs_allow_invalid() */
1c79356b 87
b0d623f7 88#include <security/audit/audit.h>
e5568f75 89
1c79356b
A
90#include <kern/task.h>
91#include <kern/thread.h>
1c79356b 92
2d21ac55
A
93#include <mach/task.h> /* for task_resume() */
94#include <kern/sched_prim.h> /* for thread_exception_return() */
95
6d2010ae
A
96#include <pexpert/pexpert.h>
97
5ba3f43e
A
98#if CONFIG_MACF
99#include <security/mac_framework.h>
100#endif
101
2d21ac55
A
102/* XXX ken/bsd_kern.c - prototype should be in common header */
103int get_task_userstop(task_t);
91447636 104
1c79356b
A
105/* Macros to clear/set/test flags. */
106#define SET(t, f) (t) |= (f)
107#define CLR(t, f) (t) &= ~(f)
108#define ISSET(t, f) ((t) & (f))
109
91447636 110extern thread_t port_name_to_thread(mach_port_name_t port_name);
91447636
A
111extern thread_t get_firstthread(task_t);
112
9bccf70c 113
1c79356b
A
114/*
115 * sys-trace system call.
116 */
9bccf70c 117
1c79356b 118int
b0d623f7 119ptrace(struct proc *p, struct ptrace_args *uap, int32_t *retval)
1c79356b
A
120{
121 struct proc *t = current_proc(); /* target process */
1c79356b 122 task_t task;
91447636 123 thread_t th_act;
1c79356b 124 struct uthread *ut;
9bccf70c 125 int tr_sigexc = 0;
2d21ac55
A
126 int error = 0;
127 int stopped = 0;
1c79356b 128
e5568f75
A
129 AUDIT_ARG(cmd, uap->req);
130 AUDIT_ARG(pid, uap->pid);
131 AUDIT_ARG(addr, uap->addr);
b0d623f7 132 AUDIT_ARG(value32, uap->data);
1c79356b 133
91447636 134 if (uap->req == PT_DENY_ATTACH) {
5ba3f43e
A
135#if (DEVELOPMENT || DEBUG) && CONFIG_EMBEDDED
136 if (PE_i_can_has_debugger(NULL))
137 return(0);
138#endif
2d21ac55
A
139 proc_lock(p);
140 if (ISSET(p->p_lflag, P_LTRACED)) {
141 proc_unlock(p);
b0d623f7
A
142 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_FRCEXIT) | DBG_FUNC_NONE,
143 p->p_pid, W_EXITCODE(ENOTSUP, 0), 4, 0, 0);
91447636 144 exit1(p, W_EXITCODE(ENOTSUP, 0), retval);
fe8ab488 145
91447636
A
146 thread_exception_return();
147 /* NOTREACHED */
148 }
2d21ac55
A
149 SET(p->p_lflag, P_LNOATTACH);
150 proc_unlock(p);
0b4e3aa0
A
151
152 return(0);
153 }
154
9bccf70c 155 if (uap->req == PT_FORCEQUOTA) {
39236c6e 156 if (kauth_cred_issuser(kauth_cred_get())) {
b0d623f7 157 OSBitOrAtomic(P_FORCEQUOTA, &t->p_flag);
9bccf70c
A
158 return (0);
159 } else
160 return (EPERM);
161 }
162
1c79356b
A
163 /*
164 * Intercept and deal with "please trace me" request.
165 */
166 if (uap->req == PT_TRACE_ME) {
3e170ce0
A
167retry_trace_me:;
168 proc_t pproc = proc_parent(p);
169 if (pproc == NULL)
170 return (EINVAL);
171#if CONFIG_MACF
172 /*
173 * NB: Cannot call kauth_authorize_process(..., KAUTH_PROCESS_CANTRACE, ...)
174 * since that assumes the process being checked is the current process
175 * when, in this case, it is the current process's parent.
176 * Most of the other checks in cantrace() don't apply either.
177 */
178 if ((error = mac_proc_check_debug(pproc, p)) == 0) {
179#endif
180 proc_lock(p);
181 /* Make sure the process wasn't re-parented. */
182 if (p->p_ppid != pproc->p_pid) {
183 proc_unlock(p);
184 proc_rele(pproc);
185 goto retry_trace_me;
186 }
187 SET(p->p_lflag, P_LTRACED);
188 /* Non-attached case, our tracer is our parent. */
189 p->p_oppid = p->p_ppid;
190 proc_unlock(p);
191 /* Child and parent will have to be able to run modified code. */
192 cs_allow_invalid(p);
6d2010ae 193 cs_allow_invalid(pproc);
3e170ce0 194#if CONFIG_MACF
6d2010ae 195 }
3e170ce0
A
196#endif
197 proc_rele(pproc);
198 return (error);
1c79356b 199 }
9bccf70c 200 if (uap->req == PT_SIGEXC) {
2d21ac55
A
201 proc_lock(p);
202 if (ISSET(p->p_lflag, P_LTRACED)) {
203 SET(p->p_lflag, P_LSIGEXC);
204 proc_unlock(p);
9bccf70c 205 return(0);
2d21ac55
A
206 } else {
207 proc_unlock(p);
9bccf70c 208 return(EINVAL);
2d21ac55
A
209 }
210 }
211
212 /*
213 * We do not want ptrace to do anything with kernel or launchd
214 */
215 if (uap->pid < 2) {
216 return(EPERM);
9bccf70c 217 }
1c79356b
A
218
219 /*
220 * Locate victim, and make sure it is traceable.
221 */
2d21ac55 222 if ((t = proc_find(uap->pid)) == NULL)
1c79356b
A
223 return (ESRCH);
224
e5568f75
A
225 AUDIT_ARG(process, t);
226
1c79356b 227 task = t->task;
9bccf70c 228 if (uap->req == PT_ATTACHEXC) {
3e170ce0
A
229#pragma clang diagnostic push
230#pragma clang diagnostic ignored "-Wdeprecated-declarations"
9bccf70c
A
231 uap->req = PT_ATTACH;
232 tr_sigexc = 1;
233 }
1c79356b 234 if (uap->req == PT_ATTACH) {
3e170ce0 235#pragma clang diagnostic pop
91447636 236 int err;
3e170ce0 237
5ba3f43e
A
238#if CONFIG_EMBEDDED
239 if (tr_sigexc == 0) {
240 error = ENOTSUP;
241 goto out;
242 }
243#endif
3e170ce0 244
91447636
A
245 if ( kauth_authorize_process(proc_ucred(p), KAUTH_PROCESS_CANTRACE,
246 t, (uintptr_t)&err, 0, 0) == 0 ) {
247 /* it's OK to attach */
2d21ac55
A
248 proc_lock(t);
249 SET(t->p_lflag, P_LTRACED);
91447636 250 if (tr_sigexc)
2d21ac55 251 SET(t->p_lflag, P_LSIGEXC);
91447636 252
2d21ac55 253 t->p_oppid = t->p_ppid;
b0d623f7
A
254 /* Check whether child and parent are allowed to run modified
255 * code (they'll have to) */
2d21ac55 256 proc_unlock(t);
b0d623f7
A
257 cs_allow_invalid(t);
258 cs_allow_invalid(p);
91447636 259 if (t->p_pptr != p)
2d21ac55 260 proc_reparentlocked(t, p, 1, 0);
91447636 261
2d21ac55
A
262 proc_lock(t);
263 if (get_task_userstop(task) > 0 ) {
264 stopped = 1;
91447636 265 }
2d21ac55
A
266 t->p_xstat = 0;
267 proc_unlock(t);
268 psignal(t, SIGSTOP);
269 /*
270 * If the process was stopped, wake up and run through
271 * issignal() again to properly connect to the tracing
272 * process.
273 */
274 if (stopped)
275 task_resume(task);
276 error = 0;
277 goto out;
0b4e3aa0 278 }
91447636
A
279 else {
280 /* not allowed to attach, proper error code returned by kauth_authorize_process */
2d21ac55 281 if (ISSET(t->p_lflag, P_LNOATTACH)) {
91447636
A
282 psignal(p, SIGSEGV);
283 }
2d21ac55
A
284
285 error = err;
286 goto out;
1c79356b 287 }
1c79356b
A
288 }
289
290 /*
291 * You can't do what you want to the process if:
292 * (1) It's not being traced at all,
293 */
2d21ac55
A
294 proc_lock(t);
295 if (!ISSET(t->p_lflag, P_LTRACED)) {
296 proc_unlock(t);
297 error = EPERM;
298 goto out;
299 }
1c79356b
A
300
301 /*
302 * (2) it's not being traced by _you_, or
303 */
2d21ac55
A
304 if (t->p_pptr != p) {
305 proc_unlock(t);
306 error = EBUSY;
307 goto out;
308 }
1c79356b
A
309
310 /*
311 * (3) it's not currently stopped.
312 */
2d21ac55
A
313 if (t->p_stat != SSTOP) {
314 proc_unlock(t);
315 error = EBUSY;
316 goto out;
317 }
1c79356b
A
318
319 /*
320 * Mach version of ptrace executes request directly here,
321 * thus simplifying the interaction of ptrace and signals.
322 */
2d21ac55 323 /* proc lock is held here */
1c79356b
A
324 switch (uap->req) {
325
326 case PT_DETACH:
2d21ac55 327 if (t->p_oppid != t->p_ppid) {
1c79356b
A
328 struct proc *pp;
329
2d21ac55
A
330 proc_unlock(t);
331 pp = proc_find(t->p_oppid);
39236c6e
A
332 if (pp != PROC_NULL) {
333 proc_reparentlocked(t, pp, 1, 0);
2d21ac55 334 proc_rele(pp);
39236c6e
A
335 } else {
336 /* original parent exited while traced */
337 proc_list_lock();
338 t->p_listflag |= P_LIST_DEADPARENT;
339 proc_list_unlock();
340 proc_reparentlocked(t, initproc, 1, 0);
341 }
2d21ac55 342 proc_lock(t);
1c79356b
A
343 }
344
345 t->p_oppid = 0;
2d21ac55
A
346 CLR(t->p_lflag, P_LTRACED);
347 CLR(t->p_lflag, P_LSIGEXC);
348 proc_unlock(t);
1c79356b
A
349 goto resume;
350
351 case PT_KILL:
352 /*
353 * Tell child process to kill itself after it
354 * is resumed by adding NSIG to p_cursig. [see issig]
355 */
2d21ac55 356 proc_unlock(t);
39236c6e 357#if CONFIG_MACF
316670eb
A
358 error = mac_proc_check_signal(p, t, SIGKILL);
359 if (0 != error)
360 goto resume;
361#endif
2d21ac55 362 psignal(t, SIGKILL);
1c79356b
A
363 goto resume;
364
365 case PT_STEP: /* single step the child */
366 case PT_CONTINUE: /* continue the child */
2d21ac55 367 proc_unlock(t);
91447636 368 th_act = (thread_t)get_firstthread(task);
2d21ac55
A
369 if (th_act == THREAD_NULL) {
370 error = EINVAL;
371 goto out;
372 }
0c530ab8 373
13f56ec4 374 /* force use of Mach SPIs (and task_for_pid security checks) to adjust PC */
91447636 375 if (uap->addr != (user_addr_t)1) {
13f56ec4
A
376 error = ENOTSUP;
377 goto out;
0c530ab8 378 }
1c79356b 379
2d21ac55
A
380 if ((unsigned)uap->data >= NSIG) {
381 error = EINVAL;
382 goto out;
383 }
1c79356b
A
384
385 if (uap->data != 0) {
813fb2f6
A
386#if CONFIG_MACF
387 error = mac_proc_check_signal(p, t, uap->data);
388 if (0 != error)
389 goto out;
390#endif
2d21ac55 391 psignal(t, uap->data);
813fb2f6 392 }
1c79356b
A
393
394 if (uap->req == PT_STEP) {
0c530ab8 395 /*
316670eb
A
396 * set trace bit
397 * we use sending SIGSTOP as a comparable security check.
0c530ab8 398 */
39236c6e 399#if CONFIG_MACF
316670eb
A
400 error = mac_proc_check_signal(p, t, SIGSTOP);
401 if (0 != error) {
402 goto out;
403 }
404#endif
2d21ac55
A
405 if (thread_setsinglestep(th_act, 1) != KERN_SUCCESS) {
406 error = ENOTSUP;
407 goto out;
408 }
0c530ab8
A
409 } else {
410 /*
411 * clear trace bit if on
316670eb 412 * we use sending SIGCONT as a comparable security check.
0c530ab8 413 */
39236c6e 414#if CONFIG_MACF
316670eb
A
415 error = mac_proc_check_signal(p, t, SIGCONT);
416 if (0 != error) {
417 goto out;
418 }
419#endif
2d21ac55
A
420 if (thread_setsinglestep(th_act, 0) != KERN_SUCCESS) {
421 error = ENOTSUP;
422 goto out;
423 }
424 }
1c79356b 425 resume:
2d21ac55 426 proc_lock(t);
1c79356b
A
427 t->p_xstat = uap->data;
428 t->p_stat = SRUN;
429 if (t->sigwait) {
430 wakeup((caddr_t)&(t->sigwait));
2d21ac55
A
431 proc_unlock(t);
432 if ((t->p_lflag & P_LSIGEXC) == 0) {
433 task_resume(task);
434 }
435 } else
436 proc_unlock(t);
437
1c79356b
A
438 break;
439
9bccf70c 440 case PT_THUPDATE: {
2d21ac55
A
441 proc_unlock(t);
442 if ((unsigned)uap->data >= NSIG) {
443 error = EINVAL;
444 goto out;
445 }
b0d623f7 446 th_act = port_name_to_thread(CAST_MACH_PORT_TO_NAME(uap->addr));
3e170ce0
A
447 if (th_act == THREAD_NULL) {
448 error = ESRCH;
449 goto out;
450 }
9bccf70c
A
451 ut = (uthread_t)get_bsdthread_info(th_act);
452 if (uap->data)
453 ut->uu_siglist |= sigmask(uap->data);
2d21ac55 454 proc_lock(t);
9bccf70c
A
455 t->p_xstat = uap->data;
456 t->p_stat = SRUN;
2d21ac55 457 proc_unlock(t);
91447636 458 thread_deallocate(th_act);
2d21ac55 459 error = 0;
9bccf70c
A
460 }
461 break;
1c79356b 462 default:
2d21ac55
A
463 proc_unlock(t);
464 error = EINVAL;
465 goto out;
1c79356b 466 }
9bccf70c 467
2d21ac55
A
468 error = 0;
469out:
470 proc_rele(t);
471 return(error);
1c79356b
A
472}
473
91447636
A
474
475/*
476 * determine if one process (cur_procp) can trace another process (traced_procp).
477 */
478
479int
480cantrace(proc_t cur_procp, kauth_cred_t creds, proc_t traced_procp, int *errp)
481{
482 int my_err;
483 /*
484 * You can't trace a process if:
485 * (1) it's the process that's doing the tracing,
486 */
487 if (traced_procp->p_pid == cur_procp->p_pid) {
488 *errp = EINVAL;
489 return (0);
490 }
491
492 /*
493 * (2) it's already being traced, or
494 */
2d21ac55 495 if (ISSET(traced_procp->p_lflag, P_LTRACED)) {
91447636
A
496 *errp = EBUSY;
497 return (0);
498 }
499
500 /*
501 * (3) it's not owned by you, or is set-id on exec
502 * (unless you're root).
503 */
6d2010ae 504 if ((kauth_cred_getruid(creds) != kauth_cred_getruid(proc_ucred(traced_procp)) ||
91447636
A
505 ISSET(traced_procp->p_flag, P_SUGID)) &&
506 (my_err = suser(creds, &cur_procp->p_acflag)) != 0) {
507 *errp = my_err;
508 return (0);
509 }
510
2d21ac55 511 if ((cur_procp->p_lflag & P_LTRACED) && isinferior(cur_procp, traced_procp)) {
91447636
A
512 *errp = EPERM;
513 return (0);
514 }
515
2d21ac55 516 if (ISSET(traced_procp->p_lflag, P_LNOATTACH)) {
91447636
A
517 *errp = EBUSY;
518 return (0);
519 }
fe8ab488
A
520
521#if CONFIG_MACF
522 if ((my_err = mac_proc_check_debug(cur_procp, traced_procp)) != 0) {
523 *errp = my_err;
524 return (0);
525 }
526#endif
527
91447636
A
528 return(1);
529}