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