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