]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_ktrace.c
399fb53b039c3e4db8bed0393b78d2bafe9e7f7e
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1989, 1993
33 * The Regents of the University of California. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)kern_ktrace.c 8.2 (Berkeley) 9/23/93
64 * $FreeBSD: src/sys/kern/kern_ktrace.c,v 1.35.2.4 2001/03/05 13:09:01 obrien Exp $
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/types.h>
71 #include <sys/proc_internal.h>
72 #include <sys/kauth.h>
73 #include <sys/file_internal.h>
74 #include <sys/namei.h>
75 #include <sys/vnode_internal.h>
77 #include <sys/ktrace.h>
79 #include <sys/malloc.h>
80 #include <sys/syslog.h>
81 #include <sys/sysproto.h>
82 #include <sys/uio_internal.h>
84 #include <bsm/audit_kernel.h>
87 static struct ktr_header
*ktrgetheader(int type
);
88 static void ktrwrite(struct vnode
*, struct ktr_header
*, struct uio
*);
89 static int ktrcanset(struct proc
*,struct proc
*);
90 static int ktrsetchildren(struct proc
*,struct proc
*,
91 int, int, struct vnode
*);
92 static int ktrops(struct proc
*,struct proc
*,int,int,struct vnode
*);
95 static struct ktr_header
*
99 register struct ktr_header
*kth
;
100 struct proc
*p
= current_proc(); /* XXX */
102 MALLOC(kth
, struct ktr_header
*, sizeof (struct ktr_header
),
105 kth
->ktr_type
= type
;
106 microtime(&kth
->ktr_time
);
107 kth
->ktr_pid
= p
->p_pid
;
108 bcopy(p
->p_comm
, kth
->ktr_comm
, MAXCOMLEN
);
115 ktrsyscall(p
, code
, narg
, args
)
122 struct ktr_header
*kth
;
123 struct ktr_syscall
*ktp
;
128 if (!KTRPOINT(p
, KTR_SYSCALL
))
132 len
= __offsetof(struct ktr_syscall
, ktr_args
) +
133 (narg
* sizeof(u_int64_t
));
134 p
->p_traceflag
|= KTRFAC_ACTIVE
;
135 kth
= ktrgetheader(KTR_SYSCALL
);
137 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
140 MALLOC(ktp
, struct ktr_syscall
*, len
, M_KTRACE
, M_WAITOK
);
145 ktp
->ktr_code
= code
;
146 ktp
->ktr_narg
= narg
;
147 argp
= &ktp
->ktr_args
[0];
148 for (i
= 0; i
< narg
; i
++)
150 kth
->ktr_buf
= (caddr_t
)ktp
;
152 ktrwrite(vp
, kth
, NULL
);
155 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
162 ktrsysret(p
, code
, error
, retval
)
169 struct ktr_header
*kth
;
170 struct ktr_sysret ktp
;
172 if (!KTRPOINT(p
, KTR_SYSRET
))
176 p
->p_traceflag
|= KTRFAC_ACTIVE
;
177 kth
= ktrgetheader(KTR_SYSRET
);
179 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
183 ktp
.ktr_error
= error
;
184 ktp
.ktr_retval
= retval
; /* what about val2 ? */
186 kth
->ktr_buf
= (caddr_t
)&ktp
;
187 kth
->ktr_len
= sizeof(struct ktr_sysret
);
189 ktrwrite(vp
, kth
, NULL
);
191 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
203 struct ktr_header
*kth
;
204 struct proc
*p
= current_proc(); /* XXX */
206 p
->p_traceflag
|= KTRFAC_ACTIVE
;
207 kth
= ktrgetheader(KTR_NAMEI
);
209 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
212 kth
->ktr_len
= strlen(path
);
215 ktrwrite(vp
, kth
, NULL
);
217 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
221 ktrgenio(vp
, fd
, rw
, uio
, error
)
228 struct ktr_header
*kth
;
229 struct ktr_genio ktg
;
230 struct proc
*p
= current_proc(); /* XXX */
235 p
->p_traceflag
|= KTRFAC_ACTIVE
;
236 kth
= ktrgetheader(KTR_GENIO
);
238 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
243 kth
->ktr_buf
= (caddr_t
)&ktg
;
244 kth
->ktr_len
= sizeof(struct ktr_genio
);
246 uio
->uio_rw
= UIO_WRITE
;
248 ktrwrite(vp
, kth
, uio
);
250 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
254 ktrpsig(vp
, sig
, action
, mask
, code
)
261 struct ktr_header
*kth
;
263 struct proc
*p
= current_proc(); /* XXX */
265 p
->p_traceflag
|= KTRFAC_ACTIVE
;
266 kth
= ktrgetheader(KTR_PSIG
);
268 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
271 kp
.signo
= (char)sig
;
275 kth
->ktr_buf
= (caddr_t
)&kp
;
276 kth
->ktr_len
= sizeof (struct ktr_psig
);
278 ktrwrite(vp
, kth
, NULL
);
280 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
284 ktrcsw(vp
, out
, user
)
288 struct ktr_header
*kth
;
290 struct proc
*p
= current_proc(); /* XXX */
292 p
->p_traceflag
|= KTRFAC_ACTIVE
;
293 kth
= ktrgetheader(KTR_CSW
);
295 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
300 kth
->ktr_buf
= (caddr_t
)&kc
;
301 kth
->ktr_len
= sizeof (struct ktr_csw
);
303 ktrwrite(vp
, kth
, NULL
);
305 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
309 /* Interface and common routines */
316 ktrace(struct proc
*curp
, register struct ktrace_args
*uap
, __unused register_t
*retval
)
319 register struct vnode
*vp
= NULL
;
320 register struct proc
*p
;
322 int facs
= uap
->facs
& ~KTRFAC_ROOT
;
323 int ops
= KTROP(uap
->ops
);
324 int descend
= uap
->ops
& KTRFLAG_DESCEND
;
328 struct vfs_context context
;
330 AUDIT_ARG(cmd
, uap
->ops
);
331 AUDIT_ARG(pid
, uap
->pid
);
332 AUDIT_ARG(value
, uap
->facs
);
334 context
.vc_proc
= curp
;
335 context
.vc_ucred
= kauth_cred_get();
337 curp
->p_traceflag
|= KTRFAC_ACTIVE
;
338 if (ops
!= KTROP_CLEAR
) {
340 * an operation which requires a file argument.
342 NDINIT(&nd
, LOOKUP
, (NOFOLLOW
|LOCKLEAF
), UIO_USERSPACE
,
343 uap
->fname
, &context
);
344 error
= vn_open(&nd
, FREAD
|FWRITE
|O_NOFOLLOW
, 0);
346 curp
->p_traceflag
&= ~KTRFAC_ACTIVE
;
351 if (vp
->v_type
!= VREG
) {
352 (void) vn_close(vp
, FREAD
|FWRITE
, kauth_cred_get(), curp
);
353 (void) vnode_put(vp
);
355 curp
->p_traceflag
&= ~KTRFAC_ACTIVE
;
360 * Clear all uses of the tracefile
362 if (ops
== KTROP_CLEARFILE
) {
363 LIST_FOREACH(p
, &allproc
, p_list
) {
364 if (p
->p_tracep
== vp
) {
365 if (ktrcanset(curp
, p
)) {
366 struct vnode
*tvp
= p
->p_tracep
;
367 /* no more tracing */
381 * need something to (un)trace (XXX - why is this here?)
394 pg
= pgfind(-uap
->pid
);
399 LIST_FOREACH(p
, &pg
->pg_members
, p_pglist
)
401 ret
|= ktrsetchildren(curp
, p
, ops
, facs
, vp
);
403 ret
|= ktrops(curp
, p
, ops
, facs
, vp
);
414 AUDIT_ARG(process
, p
);
416 ret
|= ktrsetchildren(curp
, p
, ops
, facs
, vp
);
418 ret
|= ktrops(curp
, p
, ops
, facs
, vp
);
424 (void) vn_close(vp
, FWRITE
, kauth_cred_get(), curp
);
425 (void) vnode_put(vp
);
427 curp
->p_traceflag
&= ~KTRFAC_ACTIVE
;
440 utrace(__unused
struct proc
*curp
, register struct utrace_args
*uap
, __unused register_t
*retval
)
443 struct ktr_header
*kth
;
444 struct proc
*p
= current_proc(); /* XXX */
447 if (!KTRPOINT(p
, KTR_USER
))
449 if (uap
->len
> KTR_USER_MAXLEN
)
451 p
->p_traceflag
|= KTRFAC_ACTIVE
;
452 kth
= ktrgetheader(KTR_USER
);
454 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
457 MALLOC(cp
, caddr_t
, uap
->len
, M_KTRACE
, M_WAITOK
);
462 if (copyin(uap
->addr
, cp
, uap
->len
) == 0) {
464 kth
->ktr_len
= uap
->len
;
465 ktrwrite(p
->p_tracep
, kth
, NULL
);
469 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
479 ktrops(curp
, p
, ops
, facs
, vp
)
480 struct proc
*p
, *curp
;
486 if (!ktrcanset(curp
, p
))
488 if (ops
== KTROP_SET
) {
489 if (p
->p_tracep
!= vp
) {
496 * if trace file already in use, relinquish
501 p
->p_traceflag
|= facs
;
502 if (!suser(kauth_cred_get(), NULL
))
503 p
->p_traceflag
|= KTRFAC_ROOT
;
506 if (((p
->p_traceflag
&= ~facs
) & KTRFAC_MASK
) == 0) {
507 /* no more tracing */
521 ktrsetchildren(curp
, top
, ops
, facs
, vp
)
522 struct proc
*curp
, *top
;
526 register struct proc
*p
;
527 register int ret
= 0;
531 ret
|= ktrops(curp
, p
, ops
, facs
, vp
);
533 * If this process has children, descend to them next,
534 * otherwise do any siblings, and if done with this level,
535 * follow back up the tree (but not past top).
537 if (!LIST_EMPTY(&p
->p_children
))
538 p
= LIST_FIRST(&p
->p_children
);
542 if (LIST_NEXT(p
, p_sibling
)) {
543 p
= LIST_NEXT(p
, p_sibling
);
553 ktrwrite(struct vnode
*vp
, struct ktr_header
*kth
, struct uio
*uio
)
556 register struct proc
*p
= current_proc(); /* XXX */
557 struct vfs_context context
;
559 char uio_buf
[ UIO_SIZEOF(2) ];
564 auio
= uio_createwithbuffer(2, 0, UIO_SYSSPACE
, UIO_WRITE
,
565 &uio_buf
[0], sizeof(uio_buf
));
566 uio_addiov(auio
, CAST_USER_ADDR_T(kth
), sizeof(struct ktr_header
));
568 context
.vc_ucred
= kauth_cred_get();
570 if (kth
->ktr_len
> 0) {
571 uio_addiov(auio
, CAST_USER_ADDR_T(kth
->ktr_buf
), kth
->ktr_len
);
573 kth
->ktr_len
+= uio_resid(uio
);
575 if ((error
= vnode_getwithref(vp
)) == 0) {
576 error
= VNOP_WRITE(vp
, auio
, IO_UNIT
| IO_APPEND
, &context
);
577 if (error
== 0 && uio
!= NULL
) {
578 error
= VNOP_WRITE(vp
, uio
, IO_UNIT
| IO_APPEND
, &context
);
584 * If error encountered, give up tracing on this vnode.
586 log(LOG_NOTICE
, "ktrace write failed, errno %d, tracing stopped\n",
588 LIST_FOREACH(p
, &allproc
, p_list
) {
589 if (p
->p_tracep
== vp
) {
599 * Return true if caller has permission to set the ktracing state
600 * of target. Essentially, the target can't possess any
601 * more permissions than the caller. KTRFAC_ROOT signifies that
602 * root previously set the tracing status on the target process, and
603 * so, only root may further change it.
605 * TODO: check groups. use caller effective gid.
608 ktrcanset(__unused
struct proc
*callp
, struct proc
*targetp
)
610 kauth_cred_t caller
= kauth_cred_get();
611 kauth_cred_t target
= targetp
->p_ucred
; /* XXX */
614 /* PRISON_CHECK was defined to 1 always .... */
615 if (!PRISON_CHECK(callp
, targetp
))
618 if ((kauth_cred_getuid(caller
) == target
->cr_ruid
&&
619 target
->cr_ruid
== target
->cr_svuid
&&
620 caller
->cr_rgid
== target
->cr_rgid
&& /* XXX */
621 target
->cr_rgid
== target
->cr_svgid
&&
622 (targetp
->p_traceflag
& KTRFAC_ROOT
) == 0 &&
623 (targetp
->p_flag
& P_SUGID
) == 0) ||
624 !suser(caller
, NULL
))