]>
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_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1989, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * @(#)kern_ktrace.c 8.2 (Berkeley) 9/23/93
56 * $FreeBSD: src/sys/kern/kern_ktrace.c,v 1.35.2.4 2001/03/05 13:09:01 obrien Exp $
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/types.h>
63 #include <sys/proc_internal.h>
64 #include <sys/kauth.h>
65 #include <sys/file_internal.h>
66 #include <sys/namei.h>
67 #include <sys/vnode_internal.h>
69 #include <sys/ktrace.h>
71 #include <sys/malloc.h>
72 #include <sys/syslog.h>
73 #include <sys/sysproto.h>
74 #include <sys/uio_internal.h>
76 #include <bsm/audit_kernel.h>
79 static struct ktr_header
*ktrgetheader(int type
);
80 static void ktrwrite(struct vnode
*, struct ktr_header
*, struct uio
*);
81 static int ktrcanset(struct proc
*,struct proc
*);
82 static int ktrsetchildren(struct proc
*,struct proc
*,
83 int, int, struct vnode
*);
84 static int ktrops(struct proc
*,struct proc
*,int,int,struct vnode
*);
87 static struct ktr_header
*
91 register struct ktr_header
*kth
;
92 struct proc
*p
= current_proc(); /* XXX */
94 MALLOC(kth
, struct ktr_header
*, sizeof (struct ktr_header
),
98 microtime(&kth
->ktr_time
);
99 kth
->ktr_pid
= p
->p_pid
;
100 bcopy(p
->p_comm
, kth
->ktr_comm
, MAXCOMLEN
);
107 ktrsyscall(p
, code
, narg
, args
)
114 struct ktr_header
*kth
;
115 struct ktr_syscall
*ktp
;
120 if (!KTRPOINT(p
, KTR_SYSCALL
))
124 len
= __offsetof(struct ktr_syscall
, ktr_args
) +
125 (narg
* sizeof(u_int64_t
));
126 p
->p_traceflag
|= KTRFAC_ACTIVE
;
127 kth
= ktrgetheader(KTR_SYSCALL
);
129 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
132 MALLOC(ktp
, struct ktr_syscall
*, len
, M_KTRACE
, M_WAITOK
);
137 ktp
->ktr_code
= code
;
138 ktp
->ktr_narg
= narg
;
139 argp
= &ktp
->ktr_args
[0];
140 for (i
= 0; i
< narg
; i
++)
142 kth
->ktr_buf
= (caddr_t
)ktp
;
144 ktrwrite(vp
, kth
, NULL
);
147 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
154 ktrsysret(p
, code
, error
, retval
)
161 struct ktr_header
*kth
;
162 struct ktr_sysret ktp
;
164 if (!KTRPOINT(p
, KTR_SYSRET
))
168 p
->p_traceflag
|= KTRFAC_ACTIVE
;
169 kth
= ktrgetheader(KTR_SYSRET
);
171 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
175 ktp
.ktr_error
= error
;
176 ktp
.ktr_retval
= retval
; /* what about val2 ? */
178 kth
->ktr_buf
= (caddr_t
)&ktp
;
179 kth
->ktr_len
= sizeof(struct ktr_sysret
);
181 ktrwrite(vp
, kth
, NULL
);
183 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
195 struct ktr_header
*kth
;
196 struct proc
*p
= current_proc(); /* XXX */
198 p
->p_traceflag
|= KTRFAC_ACTIVE
;
199 kth
= ktrgetheader(KTR_NAMEI
);
201 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
204 kth
->ktr_len
= strlen(path
);
207 ktrwrite(vp
, kth
, NULL
);
209 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
213 ktrgenio(vp
, fd
, rw
, uio
, error
)
220 struct ktr_header
*kth
;
221 struct ktr_genio ktg
;
222 struct proc
*p
= current_proc(); /* XXX */
227 p
->p_traceflag
|= KTRFAC_ACTIVE
;
228 kth
= ktrgetheader(KTR_GENIO
);
230 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
235 kth
->ktr_buf
= (caddr_t
)&ktg
;
236 kth
->ktr_len
= sizeof(struct ktr_genio
);
238 uio
->uio_rw
= UIO_WRITE
;
240 ktrwrite(vp
, kth
, uio
);
242 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
246 ktrpsig(vp
, sig
, action
, mask
, code
)
253 struct ktr_header
*kth
;
255 struct proc
*p
= current_proc(); /* XXX */
257 p
->p_traceflag
|= KTRFAC_ACTIVE
;
258 kth
= ktrgetheader(KTR_PSIG
);
260 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
263 kp
.signo
= (char)sig
;
267 kth
->ktr_buf
= (caddr_t
)&kp
;
268 kth
->ktr_len
= sizeof (struct ktr_psig
);
270 ktrwrite(vp
, kth
, NULL
);
272 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
276 ktrcsw(vp
, out
, user
)
280 struct ktr_header
*kth
;
282 struct proc
*p
= current_proc(); /* XXX */
284 p
->p_traceflag
|= KTRFAC_ACTIVE
;
285 kth
= ktrgetheader(KTR_CSW
);
287 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
292 kth
->ktr_buf
= (caddr_t
)&kc
;
293 kth
->ktr_len
= sizeof (struct ktr_csw
);
295 ktrwrite(vp
, kth
, NULL
);
297 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
301 /* Interface and common routines */
308 ktrace(struct proc
*curp
, register struct ktrace_args
*uap
, __unused register_t
*retval
)
311 register struct vnode
*vp
= NULL
;
312 register struct proc
*p
;
314 int facs
= uap
->facs
& ~KTRFAC_ROOT
;
315 int ops
= KTROP(uap
->ops
);
316 int descend
= uap
->ops
& KTRFLAG_DESCEND
;
320 struct vfs_context context
;
322 AUDIT_ARG(cmd
, uap
->ops
);
323 AUDIT_ARG(pid
, uap
->pid
);
324 AUDIT_ARG(value
, uap
->facs
);
326 context
.vc_proc
= curp
;
327 context
.vc_ucred
= kauth_cred_get();
329 curp
->p_traceflag
|= KTRFAC_ACTIVE
;
330 if (ops
!= KTROP_CLEAR
) {
332 * an operation which requires a file argument.
334 NDINIT(&nd
, LOOKUP
, (NOFOLLOW
|LOCKLEAF
), UIO_USERSPACE
,
335 uap
->fname
, &context
);
336 error
= vn_open(&nd
, FREAD
|FWRITE
|O_NOFOLLOW
, 0);
338 curp
->p_traceflag
&= ~KTRFAC_ACTIVE
;
343 if (vp
->v_type
!= VREG
) {
344 (void) vn_close(vp
, FREAD
|FWRITE
, kauth_cred_get(), curp
);
345 (void) vnode_put(vp
);
347 curp
->p_traceflag
&= ~KTRFAC_ACTIVE
;
352 * Clear all uses of the tracefile
354 if (ops
== KTROP_CLEARFILE
) {
355 LIST_FOREACH(p
, &allproc
, p_list
) {
356 if (p
->p_tracep
== vp
) {
357 if (ktrcanset(curp
, p
)) {
358 struct vnode
*tvp
= p
->p_tracep
;
359 /* no more tracing */
373 * need something to (un)trace (XXX - why is this here?)
386 pg
= pgfind(-uap
->pid
);
391 LIST_FOREACH(p
, &pg
->pg_members
, p_pglist
)
393 ret
|= ktrsetchildren(curp
, p
, ops
, facs
, vp
);
395 ret
|= ktrops(curp
, p
, ops
, facs
, vp
);
406 AUDIT_ARG(process
, p
);
408 ret
|= ktrsetchildren(curp
, p
, ops
, facs
, vp
);
410 ret
|= ktrops(curp
, p
, ops
, facs
, vp
);
416 (void) vn_close(vp
, FWRITE
, kauth_cred_get(), curp
);
417 (void) vnode_put(vp
);
419 curp
->p_traceflag
&= ~KTRFAC_ACTIVE
;
432 utrace(__unused
struct proc
*curp
, register struct utrace_args
*uap
, __unused register_t
*retval
)
435 struct ktr_header
*kth
;
436 struct proc
*p
= current_proc(); /* XXX */
439 if (!KTRPOINT(p
, KTR_USER
))
441 if (uap
->len
> KTR_USER_MAXLEN
)
443 p
->p_traceflag
|= KTRFAC_ACTIVE
;
444 kth
= ktrgetheader(KTR_USER
);
446 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
449 MALLOC(cp
, caddr_t
, uap
->len
, M_KTRACE
, M_WAITOK
);
454 if (copyin(uap
->addr
, cp
, uap
->len
) == 0) {
456 kth
->ktr_len
= uap
->len
;
457 ktrwrite(p
->p_tracep
, kth
, NULL
);
461 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
471 ktrops(curp
, p
, ops
, facs
, vp
)
472 struct proc
*p
, *curp
;
478 if (!ktrcanset(curp
, p
))
480 if (ops
== KTROP_SET
) {
481 if (p
->p_tracep
!= vp
) {
488 * if trace file already in use, relinquish
493 p
->p_traceflag
|= facs
;
494 if (!suser(kauth_cred_get(), NULL
))
495 p
->p_traceflag
|= KTRFAC_ROOT
;
498 if (((p
->p_traceflag
&= ~facs
) & KTRFAC_MASK
) == 0) {
499 /* no more tracing */
513 ktrsetchildren(curp
, top
, ops
, facs
, vp
)
514 struct proc
*curp
, *top
;
518 register struct proc
*p
;
519 register int ret
= 0;
523 ret
|= ktrops(curp
, p
, ops
, facs
, vp
);
525 * If this process has children, descend to them next,
526 * otherwise do any siblings, and if done with this level,
527 * follow back up the tree (but not past top).
529 if (!LIST_EMPTY(&p
->p_children
))
530 p
= LIST_FIRST(&p
->p_children
);
534 if (LIST_NEXT(p
, p_sibling
)) {
535 p
= LIST_NEXT(p
, p_sibling
);
545 ktrwrite(struct vnode
*vp
, struct ktr_header
*kth
, struct uio
*uio
)
548 register struct proc
*p
= current_proc(); /* XXX */
549 struct vfs_context context
;
551 char uio_buf
[ UIO_SIZEOF(2) ];
556 auio
= uio_createwithbuffer(2, 0, UIO_SYSSPACE
, UIO_WRITE
,
557 &uio_buf
[0], sizeof(uio_buf
));
558 uio_addiov(auio
, CAST_USER_ADDR_T(kth
), sizeof(struct ktr_header
));
560 context
.vc_ucred
= kauth_cred_get();
562 if (kth
->ktr_len
> 0) {
563 uio_addiov(auio
, CAST_USER_ADDR_T(kth
->ktr_buf
), kth
->ktr_len
);
565 kth
->ktr_len
+= uio_resid(uio
);
567 if ((error
= vnode_getwithref(vp
)) == 0) {
568 error
= VNOP_WRITE(vp
, auio
, IO_UNIT
| IO_APPEND
, &context
);
569 if (error
== 0 && uio
!= NULL
) {
570 error
= VNOP_WRITE(vp
, uio
, IO_UNIT
| IO_APPEND
, &context
);
576 * If error encountered, give up tracing on this vnode.
578 log(LOG_NOTICE
, "ktrace write failed, errno %d, tracing stopped\n",
580 LIST_FOREACH(p
, &allproc
, p_list
) {
581 if (p
->p_tracep
== vp
) {
591 * Return true if caller has permission to set the ktracing state
592 * of target. Essentially, the target can't possess any
593 * more permissions than the caller. KTRFAC_ROOT signifies that
594 * root previously set the tracing status on the target process, and
595 * so, only root may further change it.
597 * TODO: check groups. use caller effective gid.
600 ktrcanset(__unused
struct proc
*callp
, struct proc
*targetp
)
602 kauth_cred_t caller
= kauth_cred_get();
603 kauth_cred_t target
= targetp
->p_ucred
; /* XXX */
606 /* PRISON_CHECK was defined to 1 always .... */
607 if (!PRISON_CHECK(callp
, targetp
))
610 if ((kauth_cred_getuid(caller
) == target
->cr_ruid
&&
611 target
->cr_ruid
== target
->cr_svuid
&&
612 caller
->cr_rgid
== target
->cr_rgid
&& /* XXX */
613 target
->cr_rgid
== target
->cr_svgid
&&
614 (targetp
->p_traceflag
& KTRFAC_ROOT
) == 0 &&
615 (targetp
->p_flag
& P_SUGID
) == 0) ||
616 !suser(caller
, NULL
))