]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_ktrace.c
a00ed6c067fd86843889374bc84f133feb19c825
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1989, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * @(#)kern_ktrace.c 8.2 (Berkeley) 9/23/93
57 * $FreeBSD: src/sys/kern/kern_ktrace.c,v 1.35.2.4 2001/03/05 13:09:01 obrien Exp $
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/types.h>
64 #include <sys/proc_internal.h>
65 #include <sys/kauth.h>
66 #include <sys/file_internal.h>
67 #include <sys/namei.h>
68 #include <sys/vnode_internal.h>
70 #include <sys/ktrace.h>
72 #include <sys/malloc.h>
73 #include <sys/syslog.h>
74 #include <sys/sysproto.h>
75 #include <sys/uio_internal.h>
77 #include <bsm/audit_kernel.h>
80 static struct ktr_header
*ktrgetheader(int type
);
81 static void ktrwrite(struct vnode
*, struct ktr_header
*, struct uio
*);
82 static int ktrcanset(struct proc
*,struct proc
*);
83 static int ktrsetchildren(struct proc
*,struct proc
*,
84 int, int, struct vnode
*);
85 static int ktrops(struct proc
*,struct proc
*,int,int,struct vnode
*);
88 static struct ktr_header
*
92 register struct ktr_header
*kth
;
93 struct proc
*p
= current_proc(); /* XXX */
95 MALLOC(kth
, struct ktr_header
*, sizeof (struct ktr_header
),
99 microtime(&kth
->ktr_time
);
100 kth
->ktr_pid
= p
->p_pid
;
101 bcopy(p
->p_comm
, kth
->ktr_comm
, MAXCOMLEN
);
108 ktrsyscall(p
, code
, narg
, args
)
115 struct ktr_header
*kth
;
116 struct ktr_syscall
*ktp
;
121 if (!KTRPOINT(p
, KTR_SYSCALL
))
125 len
= __offsetof(struct ktr_syscall
, ktr_args
) +
126 (narg
* sizeof(u_int64_t
));
127 p
->p_traceflag
|= KTRFAC_ACTIVE
;
128 kth
= ktrgetheader(KTR_SYSCALL
);
130 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
133 MALLOC(ktp
, struct ktr_syscall
*, len
, M_KTRACE
, M_WAITOK
);
138 ktp
->ktr_code
= code
;
139 ktp
->ktr_narg
= narg
;
140 argp
= &ktp
->ktr_args
[0];
141 for (i
= 0; i
< narg
; i
++)
143 kth
->ktr_buf
= (caddr_t
)ktp
;
145 ktrwrite(vp
, kth
, NULL
);
148 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
155 ktrsysret(p
, code
, error
, retval
)
162 struct ktr_header
*kth
;
163 struct ktr_sysret ktp
;
165 if (!KTRPOINT(p
, KTR_SYSRET
))
169 p
->p_traceflag
|= KTRFAC_ACTIVE
;
170 kth
= ktrgetheader(KTR_SYSRET
);
172 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
176 ktp
.ktr_error
= error
;
177 ktp
.ktr_retval
= retval
; /* what about val2 ? */
179 kth
->ktr_buf
= (caddr_t
)&ktp
;
180 kth
->ktr_len
= sizeof(struct ktr_sysret
);
182 ktrwrite(vp
, kth
, NULL
);
184 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
196 struct ktr_header
*kth
;
197 struct proc
*p
= current_proc(); /* XXX */
199 p
->p_traceflag
|= KTRFAC_ACTIVE
;
200 kth
= ktrgetheader(KTR_NAMEI
);
202 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
205 kth
->ktr_len
= strlen(path
);
208 ktrwrite(vp
, kth
, NULL
);
210 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
214 ktrgenio(vp
, fd
, rw
, uio
, error
)
221 struct ktr_header
*kth
;
222 struct ktr_genio ktg
;
223 struct proc
*p
= current_proc(); /* XXX */
228 p
->p_traceflag
|= KTRFAC_ACTIVE
;
229 kth
= ktrgetheader(KTR_GENIO
);
231 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
236 kth
->ktr_buf
= (caddr_t
)&ktg
;
237 kth
->ktr_len
= sizeof(struct ktr_genio
);
239 uio
->uio_rw
= UIO_WRITE
;
241 ktrwrite(vp
, kth
, uio
);
243 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
247 ktrpsig(vp
, sig
, action
, mask
, code
)
254 struct ktr_header
*kth
;
256 struct proc
*p
= current_proc(); /* XXX */
258 p
->p_traceflag
|= KTRFAC_ACTIVE
;
259 kth
= ktrgetheader(KTR_PSIG
);
261 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
264 kp
.signo
= (char)sig
;
268 kth
->ktr_buf
= (caddr_t
)&kp
;
269 kth
->ktr_len
= sizeof (struct ktr_psig
);
271 ktrwrite(vp
, kth
, NULL
);
273 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
277 ktrcsw(vp
, out
, user
)
281 struct ktr_header
*kth
;
283 struct proc
*p
= current_proc(); /* XXX */
285 p
->p_traceflag
|= KTRFAC_ACTIVE
;
286 kth
= ktrgetheader(KTR_CSW
);
288 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
293 kth
->ktr_buf
= (caddr_t
)&kc
;
294 kth
->ktr_len
= sizeof (struct ktr_csw
);
296 ktrwrite(vp
, kth
, NULL
);
298 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
302 /* Interface and common routines */
309 ktrace(struct proc
*curp
, register struct ktrace_args
*uap
, __unused register_t
*retval
)
312 register struct vnode
*vp
= NULL
;
313 register struct proc
*p
;
315 int facs
= uap
->facs
& ~KTRFAC_ROOT
;
316 int ops
= KTROP(uap
->ops
);
317 int descend
= uap
->ops
& KTRFLAG_DESCEND
;
321 struct vfs_context context
;
323 AUDIT_ARG(cmd
, uap
->ops
);
324 AUDIT_ARG(pid
, uap
->pid
);
325 AUDIT_ARG(value
, uap
->facs
);
327 context
.vc_proc
= curp
;
328 context
.vc_ucred
= kauth_cred_get();
330 curp
->p_traceflag
|= KTRFAC_ACTIVE
;
331 if (ops
!= KTROP_CLEAR
) {
333 * an operation which requires a file argument.
335 NDINIT(&nd
, LOOKUP
, (NOFOLLOW
|LOCKLEAF
), UIO_USERSPACE
,
336 uap
->fname
, &context
);
337 error
= vn_open(&nd
, FREAD
|FWRITE
|O_NOFOLLOW
, 0);
339 curp
->p_traceflag
&= ~KTRFAC_ACTIVE
;
344 if (vp
->v_type
!= VREG
) {
345 (void) vn_close(vp
, FREAD
|FWRITE
, kauth_cred_get(), curp
);
346 (void) vnode_put(vp
);
348 curp
->p_traceflag
&= ~KTRFAC_ACTIVE
;
353 * Clear all uses of the tracefile
355 if (ops
== KTROP_CLEARFILE
) {
356 LIST_FOREACH(p
, &allproc
, p_list
) {
357 if (p
->p_tracep
== vp
) {
358 if (ktrcanset(curp
, p
)) {
359 struct vnode
*tvp
= p
->p_tracep
;
360 /* no more tracing */
374 * need something to (un)trace (XXX - why is this here?)
387 pg
= pgfind(-uap
->pid
);
392 LIST_FOREACH(p
, &pg
->pg_members
, p_pglist
)
394 ret
|= ktrsetchildren(curp
, p
, ops
, facs
, vp
);
396 ret
|= ktrops(curp
, p
, ops
, facs
, vp
);
407 AUDIT_ARG(process
, p
);
409 ret
|= ktrsetchildren(curp
, p
, ops
, facs
, vp
);
411 ret
|= ktrops(curp
, p
, ops
, facs
, vp
);
417 (void) vn_close(vp
, FWRITE
, kauth_cred_get(), curp
);
418 (void) vnode_put(vp
);
420 curp
->p_traceflag
&= ~KTRFAC_ACTIVE
;
433 utrace(__unused
struct proc
*curp
, register struct utrace_args
*uap
, __unused register_t
*retval
)
436 struct ktr_header
*kth
;
437 struct proc
*p
= current_proc(); /* XXX */
440 if (!KTRPOINT(p
, KTR_USER
))
442 if (uap
->len
> KTR_USER_MAXLEN
)
444 p
->p_traceflag
|= KTRFAC_ACTIVE
;
445 kth
= ktrgetheader(KTR_USER
);
447 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
450 MALLOC(cp
, caddr_t
, uap
->len
, M_KTRACE
, M_WAITOK
);
455 if (copyin(uap
->addr
, cp
, uap
->len
) == 0) {
457 kth
->ktr_len
= uap
->len
;
458 ktrwrite(p
->p_tracep
, kth
, NULL
);
462 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
472 ktrops(curp
, p
, ops
, facs
, vp
)
473 struct proc
*p
, *curp
;
479 if (!ktrcanset(curp
, p
))
481 if (ops
== KTROP_SET
) {
482 if (p
->p_tracep
!= vp
) {
489 * if trace file already in use, relinquish
494 p
->p_traceflag
|= facs
;
495 if (!suser(kauth_cred_get(), NULL
))
496 p
->p_traceflag
|= KTRFAC_ROOT
;
499 if (((p
->p_traceflag
&= ~facs
) & KTRFAC_MASK
) == 0) {
500 /* no more tracing */
514 ktrsetchildren(curp
, top
, ops
, facs
, vp
)
515 struct proc
*curp
, *top
;
519 register struct proc
*p
;
520 register int ret
= 0;
524 ret
|= ktrops(curp
, p
, ops
, facs
, vp
);
526 * If this process has children, descend to them next,
527 * otherwise do any siblings, and if done with this level,
528 * follow back up the tree (but not past top).
530 if (!LIST_EMPTY(&p
->p_children
))
531 p
= LIST_FIRST(&p
->p_children
);
535 if (LIST_NEXT(p
, p_sibling
)) {
536 p
= LIST_NEXT(p
, p_sibling
);
546 ktrwrite(struct vnode
*vp
, struct ktr_header
*kth
, struct uio
*uio
)
549 register struct proc
*p
= current_proc(); /* XXX */
550 struct vfs_context context
;
552 char uio_buf
[ UIO_SIZEOF(2) ];
557 auio
= uio_createwithbuffer(2, 0, UIO_SYSSPACE
, UIO_WRITE
,
558 &uio_buf
[0], sizeof(uio_buf
));
559 uio_addiov(auio
, CAST_USER_ADDR_T(kth
), sizeof(struct ktr_header
));
561 context
.vc_ucred
= kauth_cred_get();
563 if (kth
->ktr_len
> 0) {
564 uio_addiov(auio
, CAST_USER_ADDR_T(kth
->ktr_buf
), kth
->ktr_len
);
566 kth
->ktr_len
+= uio_resid(uio
);
568 if ((error
= vnode_getwithref(vp
)) == 0) {
569 error
= VNOP_WRITE(vp
, auio
, IO_UNIT
| IO_APPEND
, &context
);
570 if (error
== 0 && uio
!= NULL
) {
571 error
= VNOP_WRITE(vp
, uio
, IO_UNIT
| IO_APPEND
, &context
);
577 * If error encountered, give up tracing on this vnode.
579 log(LOG_NOTICE
, "ktrace write failed, errno %d, tracing stopped\n",
581 LIST_FOREACH(p
, &allproc
, p_list
) {
582 if (p
->p_tracep
== vp
) {
592 * Return true if caller has permission to set the ktracing state
593 * of target. Essentially, the target can't possess any
594 * more permissions than the caller. KTRFAC_ROOT signifies that
595 * root previously set the tracing status on the target process, and
596 * so, only root may further change it.
598 * TODO: check groups. use caller effective gid.
601 ktrcanset(__unused
struct proc
*callp
, struct proc
*targetp
)
603 kauth_cred_t caller
= kauth_cred_get();
604 kauth_cred_t target
= targetp
->p_ucred
; /* XXX */
607 /* PRISON_CHECK was defined to 1 always .... */
608 if (!PRISON_CHECK(callp
, targetp
))
611 if ((kauth_cred_getuid(caller
) == target
->cr_ruid
&&
612 target
->cr_ruid
== target
->cr_svuid
&&
613 caller
->cr_rgid
== target
->cr_rgid
&& /* XXX */
614 target
->cr_rgid
== target
->cr_svgid
&&
615 (targetp
->p_traceflag
& KTRFAC_ROOT
) == 0 &&
616 (targetp
->p_flag
& P_SUGID
) == 0) ||
617 !suser(caller
, NULL
))