]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_ktrace.c
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)kern_ktrace.c 8.2 (Berkeley) 9/23/93
62 * $FreeBSD: src/sys/kern/kern_ktrace.c,v 1.35.2.4 2001/03/05 13:09:01 obrien Exp $
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/types.h>
69 #include <sys/proc_internal.h>
70 #include <sys/kauth.h>
71 #include <sys/file_internal.h>
72 #include <sys/namei.h>
73 #include <sys/vnode_internal.h>
75 #include <sys/ktrace.h>
77 #include <sys/malloc.h>
78 #include <sys/syslog.h>
79 #include <sys/sysproto.h>
80 #include <sys/uio_internal.h>
82 #include <bsm/audit_kernel.h>
85 static struct ktr_header
*ktrgetheader(int type
);
86 static void ktrwrite(struct vnode
*, struct ktr_header
*, struct uio
*);
87 static int ktrcanset(struct proc
*,struct proc
*);
88 static int ktrsetchildren(struct proc
*,struct proc
*,
89 int, int, struct vnode
*);
90 static int ktrops(struct proc
*,struct proc
*,int,int,struct vnode
*);
93 static struct ktr_header
*
97 register struct ktr_header
*kth
;
98 struct proc
*p
= current_proc(); /* XXX */
100 MALLOC(kth
, struct ktr_header
*, sizeof (struct ktr_header
),
103 kth
->ktr_type
= type
;
104 microtime(&kth
->ktr_time
);
105 kth
->ktr_pid
= p
->p_pid
;
106 bcopy(p
->p_comm
, kth
->ktr_comm
, MAXCOMLEN
);
113 ktrsyscall(p
, code
, narg
, args
)
120 struct ktr_header
*kth
;
121 struct ktr_syscall
*ktp
;
126 if (!KTRPOINT(p
, KTR_SYSCALL
))
130 len
= __offsetof(struct ktr_syscall
, ktr_args
) +
131 (narg
* sizeof(u_int64_t
));
132 p
->p_traceflag
|= KTRFAC_ACTIVE
;
133 kth
= ktrgetheader(KTR_SYSCALL
);
135 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
138 MALLOC(ktp
, struct ktr_syscall
*, len
, M_KTRACE
, M_WAITOK
);
143 ktp
->ktr_code
= code
;
144 ktp
->ktr_narg
= narg
;
145 argp
= &ktp
->ktr_args
[0];
146 for (i
= 0; i
< narg
; i
++)
148 kth
->ktr_buf
= (caddr_t
)ktp
;
150 ktrwrite(vp
, kth
, NULL
);
153 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
160 ktrsysret(p
, code
, error
, retval
)
167 struct ktr_header
*kth
;
168 struct ktr_sysret ktp
;
170 if (!KTRPOINT(p
, KTR_SYSRET
))
174 p
->p_traceflag
|= KTRFAC_ACTIVE
;
175 kth
= ktrgetheader(KTR_SYSRET
);
177 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
181 ktp
.ktr_error
= error
;
182 ktp
.ktr_retval
= retval
; /* what about val2 ? */
184 kth
->ktr_buf
= (caddr_t
)&ktp
;
185 kth
->ktr_len
= sizeof(struct ktr_sysret
);
187 ktrwrite(vp
, kth
, NULL
);
189 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
201 struct ktr_header
*kth
;
202 struct proc
*p
= current_proc(); /* XXX */
204 p
->p_traceflag
|= KTRFAC_ACTIVE
;
205 kth
= ktrgetheader(KTR_NAMEI
);
207 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
210 kth
->ktr_len
= strlen(path
);
213 ktrwrite(vp
, kth
, NULL
);
215 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
219 ktrgenio(vp
, fd
, rw
, uio
, error
)
226 struct ktr_header
*kth
;
227 struct ktr_genio ktg
;
228 struct proc
*p
= current_proc(); /* XXX */
233 p
->p_traceflag
|= KTRFAC_ACTIVE
;
234 kth
= ktrgetheader(KTR_GENIO
);
236 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
241 kth
->ktr_buf
= (caddr_t
)&ktg
;
242 kth
->ktr_len
= sizeof(struct ktr_genio
);
244 uio
->uio_rw
= UIO_WRITE
;
246 ktrwrite(vp
, kth
, uio
);
248 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
252 ktrpsig(vp
, sig
, action
, mask
, code
)
259 struct ktr_header
*kth
;
261 struct proc
*p
= current_proc(); /* XXX */
263 p
->p_traceflag
|= KTRFAC_ACTIVE
;
264 kth
= ktrgetheader(KTR_PSIG
);
266 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
269 kp
.signo
= (char)sig
;
273 kth
->ktr_buf
= (caddr_t
)&kp
;
274 kth
->ktr_len
= sizeof (struct ktr_psig
);
276 ktrwrite(vp
, kth
, NULL
);
278 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
282 ktrcsw(vp
, out
, user
)
286 struct ktr_header
*kth
;
288 struct proc
*p
= current_proc(); /* XXX */
290 p
->p_traceflag
|= KTRFAC_ACTIVE
;
291 kth
= ktrgetheader(KTR_CSW
);
293 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
298 kth
->ktr_buf
= (caddr_t
)&kc
;
299 kth
->ktr_len
= sizeof (struct ktr_csw
);
301 ktrwrite(vp
, kth
, NULL
);
303 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
307 /* Interface and common routines */
314 ktrace(struct proc
*curp
, register struct ktrace_args
*uap
, __unused register_t
*retval
)
317 register struct vnode
*vp
= NULL
;
318 register struct proc
*p
;
320 int facs
= uap
->facs
& ~KTRFAC_ROOT
;
321 int ops
= KTROP(uap
->ops
);
322 int descend
= uap
->ops
& KTRFLAG_DESCEND
;
326 struct vfs_context context
;
328 AUDIT_ARG(cmd
, uap
->ops
);
329 AUDIT_ARG(pid
, uap
->pid
);
330 AUDIT_ARG(value
, uap
->facs
);
332 context
.vc_proc
= curp
;
333 context
.vc_ucred
= kauth_cred_get();
335 curp
->p_traceflag
|= KTRFAC_ACTIVE
;
336 if (ops
!= KTROP_CLEAR
) {
338 * an operation which requires a file argument.
340 NDINIT(&nd
, LOOKUP
, (NOFOLLOW
|LOCKLEAF
), UIO_USERSPACE
,
341 uap
->fname
, &context
);
342 error
= vn_open(&nd
, FREAD
|FWRITE
|O_NOFOLLOW
, 0);
344 curp
->p_traceflag
&= ~KTRFAC_ACTIVE
;
349 if (vp
->v_type
!= VREG
) {
350 (void) vn_close(vp
, FREAD
|FWRITE
, kauth_cred_get(), curp
);
351 (void) vnode_put(vp
);
353 curp
->p_traceflag
&= ~KTRFAC_ACTIVE
;
358 * Clear all uses of the tracefile
360 if (ops
== KTROP_CLEARFILE
) {
361 LIST_FOREACH(p
, &allproc
, p_list
) {
362 if (p
->p_tracep
== vp
) {
363 if (ktrcanset(curp
, p
)) {
364 struct vnode
*tvp
= p
->p_tracep
;
365 /* no more tracing */
379 * need something to (un)trace (XXX - why is this here?)
392 pg
= pgfind(-uap
->pid
);
397 LIST_FOREACH(p
, &pg
->pg_members
, p_pglist
)
399 ret
|= ktrsetchildren(curp
, p
, ops
, facs
, vp
);
401 ret
|= ktrops(curp
, p
, ops
, facs
, vp
);
412 AUDIT_ARG(process
, p
);
414 ret
|= ktrsetchildren(curp
, p
, ops
, facs
, vp
);
416 ret
|= ktrops(curp
, p
, ops
, facs
, vp
);
422 (void) vn_close(vp
, FWRITE
, kauth_cred_get(), curp
);
423 (void) vnode_put(vp
);
425 curp
->p_traceflag
&= ~KTRFAC_ACTIVE
;
438 utrace(__unused
struct proc
*curp
, register struct utrace_args
*uap
, __unused register_t
*retval
)
441 struct ktr_header
*kth
;
442 struct proc
*p
= current_proc(); /* XXX */
445 if (!KTRPOINT(p
, KTR_USER
))
447 if (uap
->len
> KTR_USER_MAXLEN
)
449 p
->p_traceflag
|= KTRFAC_ACTIVE
;
450 kth
= ktrgetheader(KTR_USER
);
452 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
455 MALLOC(cp
, caddr_t
, uap
->len
, M_KTRACE
, M_WAITOK
);
460 if (copyin(uap
->addr
, cp
, uap
->len
) == 0) {
462 kth
->ktr_len
= uap
->len
;
463 ktrwrite(p
->p_tracep
, kth
, NULL
);
467 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
477 ktrops(curp
, p
, ops
, facs
, vp
)
478 struct proc
*p
, *curp
;
484 if (!ktrcanset(curp
, p
))
486 if (ops
== KTROP_SET
) {
487 if (p
->p_tracep
!= vp
) {
494 * if trace file already in use, relinquish
499 p
->p_traceflag
|= facs
;
500 if (!suser(kauth_cred_get(), NULL
))
501 p
->p_traceflag
|= KTRFAC_ROOT
;
504 if (((p
->p_traceflag
&= ~facs
) & KTRFAC_MASK
) == 0) {
505 /* no more tracing */
519 ktrsetchildren(curp
, top
, ops
, facs
, vp
)
520 struct proc
*curp
, *top
;
524 register struct proc
*p
;
525 register int ret
= 0;
529 ret
|= ktrops(curp
, p
, ops
, facs
, vp
);
531 * If this process has children, descend to them next,
532 * otherwise do any siblings, and if done with this level,
533 * follow back up the tree (but not past top).
535 if (!LIST_EMPTY(&p
->p_children
))
536 p
= LIST_FIRST(&p
->p_children
);
540 if (LIST_NEXT(p
, p_sibling
)) {
541 p
= LIST_NEXT(p
, p_sibling
);
551 ktrwrite(struct vnode
*vp
, struct ktr_header
*kth
, struct uio
*uio
)
554 register struct proc
*p
= current_proc(); /* XXX */
555 struct vfs_context context
;
557 char uio_buf
[ UIO_SIZEOF(2) ];
562 auio
= uio_createwithbuffer(2, 0, UIO_SYSSPACE
, UIO_WRITE
,
563 &uio_buf
[0], sizeof(uio_buf
));
564 uio_addiov(auio
, CAST_USER_ADDR_T(kth
), sizeof(struct ktr_header
));
566 context
.vc_ucred
= kauth_cred_get();
568 if (kth
->ktr_len
> 0) {
569 uio_addiov(auio
, CAST_USER_ADDR_T(kth
->ktr_buf
), kth
->ktr_len
);
571 kth
->ktr_len
+= uio_resid(uio
);
573 if ((error
= vnode_getwithref(vp
)) == 0) {
574 error
= VNOP_WRITE(vp
, auio
, IO_UNIT
| IO_APPEND
, &context
);
575 if (error
== 0 && uio
!= NULL
) {
576 error
= VNOP_WRITE(vp
, uio
, IO_UNIT
| IO_APPEND
, &context
);
582 * If error encountered, give up tracing on this vnode.
584 log(LOG_NOTICE
, "ktrace write failed, errno %d, tracing stopped\n",
586 LIST_FOREACH(p
, &allproc
, p_list
) {
587 if (p
->p_tracep
== vp
) {
597 * Return true if caller has permission to set the ktracing state
598 * of target. Essentially, the target can't possess any
599 * more permissions than the caller. KTRFAC_ROOT signifies that
600 * root previously set the tracing status on the target process, and
601 * so, only root may further change it.
603 * TODO: check groups. use caller effective gid.
606 ktrcanset(__unused
struct proc
*callp
, struct proc
*targetp
)
608 kauth_cred_t caller
= kauth_cred_get();
609 kauth_cred_t target
= targetp
->p_ucred
; /* XXX */
612 /* PRISON_CHECK was defined to 1 always .... */
613 if (!PRISON_CHECK(callp
, targetp
))
616 if ((kauth_cred_getuid(caller
) == target
->cr_ruid
&&
617 target
->cr_ruid
== target
->cr_svuid
&&
618 caller
->cr_rgid
== target
->cr_rgid
&& /* XXX */
619 target
->cr_rgid
== target
->cr_svgid
&&
620 (targetp
->p_traceflag
& KTRFAC_ROOT
) == 0 &&
621 (targetp
->p_flag
& P_SUGID
) == 0) ||
622 !suser(caller
, NULL
))