2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)kern_ktrace.c 8.2 (Berkeley) 9/23/93
34 * $FreeBSD: src/sys/kern/kern_ktrace.c,v 1.35.2.6 2002/07/05 22:36:38 darrenr Exp $
35 * $DragonFly: src/sys/kern/kern_ktrace.c,v 1.29 2007/05/07 15:43:30 dillon Exp $
38 #include "opt_ktrace.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysproto.h>
43 #include <sys/kernel.h>
45 #include <sys/fcntl.h>
47 #include <sys/nlookup.h>
48 #include <sys/vnode.h>
49 #include <sys/ktrace.h>
50 #include <sys/malloc.h>
51 #include <sys/syslog.h>
52 #include <sys/sysent.h>
54 #include <vm/vm_zone.h>
55 static MALLOC_DEFINE(M_KTRACE
, "KTRACE", "KTRACE");
58 static struct ktr_header
*ktrgetheader (int type
);
59 static void ktrwrite (struct proc
*, struct ktr_header
*, struct uio
*);
60 static int ktrcanset (struct proc
*,struct proc
*);
61 static int ktrsetchildren (struct proc
*,struct proc
*,int,int, ktrace_node_t
);
62 static int ktrops (struct proc
*,struct proc
*,int,int, ktrace_node_t
);
64 static struct ktr_header
*
65 ktrgetheader(int type
)
67 struct ktr_header
*kth
;
68 struct proc
*p
= curproc
; /* XXX */
69 struct lwp
*lp
= curthread
->td_lwp
;
71 MALLOC(kth
, struct ktr_header
*, sizeof (struct ktr_header
),
74 /* XXX threaded flag is a hack at the moment */
75 kth
->ktr_flags
= (p
->p_nthreads
> 1) ? KTRH_THREADED
: 0;
76 microtime(&kth
->ktr_time
);
77 kth
->ktr_pid
= p
->p_pid
;
78 kth
->ktr_tid
= lp
->lwp_tid
;
79 bcopy(p
->p_comm
, kth
->ktr_comm
, MAXCOMLEN
+ 1);
84 ktrsyscall(struct proc
*p
, int code
, int narg
, register_t args
[])
86 struct ktr_header
*kth
;
87 struct ktr_syscall
*ktp
;
92 len
= offsetof(struct ktr_syscall
, ktr_args
) +
93 (narg
* sizeof(register_t
));
96 * Setting the active bit prevents a ktrace recursion from the
99 p
->p_traceflag
|= KTRFAC_ACTIVE
;
100 kth
= ktrgetheader(KTR_SYSCALL
);
101 MALLOC(ktp
, struct ktr_syscall
*, len
, M_KTRACE
, M_WAITOK
);
102 ktp
->ktr_code
= code
;
103 ktp
->ktr_narg
= narg
;
104 argp
= &ktp
->ktr_args
[0];
105 for (i
= 0; i
< narg
; i
++)
107 kth
->ktr_buf
= (caddr_t
)ktp
;
109 ktrwrite(p
, kth
, NULL
);
112 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
116 ktrsysret(struct proc
*p
, int code
, int error
, register_t retval
)
118 struct ktr_header
*kth
;
119 struct ktr_sysret ktp
;
121 p
->p_traceflag
|= KTRFAC_ACTIVE
;
122 kth
= ktrgetheader(KTR_SYSRET
);
124 ktp
.ktr_error
= error
;
125 ktp
.ktr_retval
= retval
; /* what about val2 ? */
127 kth
->ktr_buf
= (caddr_t
)&ktp
;
128 kth
->ktr_len
= sizeof(struct ktr_sysret
);
130 ktrwrite(p
, kth
, NULL
);
132 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
136 ktrnamei(struct proc
*p
, char *path
)
138 struct ktr_header
*kth
;
140 p
->p_traceflag
|= KTRFAC_ACTIVE
;
141 kth
= ktrgetheader(KTR_NAMEI
);
142 kth
->ktr_len
= strlen(path
);
145 ktrwrite(p
, kth
, NULL
);
147 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
151 ktrgenio(struct proc
*p
, int fd
, enum uio_rw rw
, struct uio
*uio
, int error
)
153 struct ktr_header
*kth
;
154 struct ktr_genio ktg
;
158 p
->p_traceflag
|= KTRFAC_ACTIVE
;
159 kth
= ktrgetheader(KTR_GENIO
);
162 kth
->ktr_buf
= (caddr_t
)&ktg
;
163 kth
->ktr_len
= sizeof(struct ktr_genio
);
165 uio
->uio_rw
= UIO_WRITE
;
167 ktrwrite(p
, kth
, uio
);
169 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
173 ktrpsig(struct proc
*p
, int sig
, sig_t action
, sigset_t
*mask
, int code
)
175 struct ktr_header
*kth
;
178 p
->p_traceflag
|= KTRFAC_ACTIVE
;
179 kth
= ktrgetheader(KTR_PSIG
);
180 kp
.signo
= (char)sig
;
184 kth
->ktr_buf
= (caddr_t
)&kp
;
185 kth
->ktr_len
= sizeof (struct ktr_psig
);
187 ktrwrite(p
, kth
, NULL
);
189 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
193 ktrcsw(struct proc
*p
, int out
, int user
)
195 struct ktr_header
*kth
;
198 p
->p_traceflag
|= KTRFAC_ACTIVE
;
199 kth
= ktrgetheader(KTR_CSW
);
202 kth
->ktr_buf
= (caddr_t
)&kc
;
203 kth
->ktr_len
= sizeof (struct ktr_csw
);
205 ktrwrite(p
, kth
, NULL
);
207 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
211 /* Interface and common routines */
217 struct ktrace_clear_info
{
218 ktrace_node_t tracenode
;
223 static int ktrace_clear_callback(struct proc
*p
, void *data
);
228 sys_ktrace(struct ktrace_args
*uap
)
231 struct ktrace_clear_info info
;
232 struct thread
*td
= curthread
;
233 struct proc
*curp
= td
->td_proc
;
236 int facs
= uap
->facs
& ~KTRFAC_ROOT
;
237 int ops
= KTROP(uap
->ops
);
238 int descend
= uap
->ops
& KTRFLAG_DESCEND
;
241 struct nlookupdata nd
;
242 ktrace_node_t tracenode
= NULL
;
244 curp
->p_traceflag
|= KTRFAC_ACTIVE
;
245 if (ops
!= KTROP_CLEAR
) {
247 * an operation which requires a file argument.
249 error
= nlookup_init(&nd
, uap
->fname
,
250 UIO_USERSPACE
, NLC_LOCKVP
);
252 error
= vn_open(&nd
, NULL
, FREAD
|FWRITE
|O_NOFOLLOW
, 0);
253 if (error
== 0 && nd
.nl_open_vp
->v_type
!= VREG
)
256 curp
->p_traceflag
&= ~KTRFAC_ACTIVE
;
260 MALLOC(tracenode
, ktrace_node_t
, sizeof (struct ktrace_node
),
261 M_KTRACE
, M_WAITOK
| M_ZERO
);
262 tracenode
->kn_vp
= nd
.nl_open_vp
;
263 tracenode
->kn_refs
= 1;
264 nd
.nl_open_vp
= NULL
;
266 vn_unlock(tracenode
->kn_vp
);
269 * Clear all uses of the tracefile. Not the most efficient operation
272 if (ops
== KTROP_CLEARFILE
) {
273 info
.tracenode
= tracenode
;
276 allproc_scan(ktrace_clear_callback
, &info
);
281 * need something to (un)trace (XXX - why is this here?)
294 pg
= pgfind(-uap
->pid
);
299 LIST_FOREACH(p
, &pg
->pg_members
, p_pglist
) {
301 ret
|= ktrsetchildren(curp
, p
, ops
, facs
, tracenode
);
303 ret
|= ktrops(curp
, p
, ops
, facs
, tracenode
);
315 ret
|= ktrsetchildren(curp
, p
, ops
, facs
, tracenode
);
317 ret
|= ktrops(curp
, p
, ops
, facs
, tracenode
);
323 ktrdestroy(&tracenode
);
324 curp
->p_traceflag
&= ~KTRFAC_ACTIVE
;
334 * NOTE: NOT MPSAFE (yet)
337 ktrace_clear_callback(struct proc
*p
, void *data
)
339 struct ktrace_clear_info
*info
= data
;
341 if (info
->rootclear
) {
342 if (p
->p_tracenode
== info
->tracenode
) {
343 ktrdestroy(&p
->p_tracenode
);
347 if (p
->p_tracenode
->kn_vp
== info
->tracenode
->kn_vp
) {
348 if (ktrcanset(curproc
, p
)) {
349 ktrdestroy(&p
->p_tracenode
);
366 sys_utrace(struct utrace_args
*uap
)
369 struct ktr_header
*kth
;
370 struct thread
*td
= curthread
; /* XXX */
371 struct proc
*p
= td
->td_proc
;
374 if (!KTRPOINT(td
, KTR_USER
))
376 if (uap
->len
> KTR_USER_MAXLEN
)
378 p
->p_traceflag
|= KTRFAC_ACTIVE
;
379 kth
= ktrgetheader(KTR_USER
);
380 MALLOC(cp
, caddr_t
, uap
->len
, M_KTRACE
, M_WAITOK
);
381 if (!copyin(uap
->addr
, cp
, uap
->len
)) {
383 kth
->ktr_len
= uap
->len
;
384 ktrwrite(p
, kth
, NULL
);
388 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
397 ktrdestroy(struct ktrace_node
**tracenodep
)
399 ktrace_node_t tracenode
;
401 if ((tracenode
= *tracenodep
) != NULL
) {
403 KKASSERT(tracenode
->kn_refs
> 0);
404 /* XXX not MP safe yet */
405 --tracenode
->kn_refs
;
406 if (tracenode
->kn_refs
== 0) {
407 vn_close(tracenode
->kn_vp
, FREAD
|FWRITE
);
408 tracenode
->kn_vp
= NULL
;
409 FREE(tracenode
, M_KTRACE
);
415 * This allows a process to inherit a ref on a tracenode and is also used
416 * as a temporary ref to prevent a tracenode from being destroyed out from
417 * under an active operation.
420 ktrinherit(ktrace_node_t tracenode
)
423 KKASSERT(tracenode
->kn_refs
> 0);
424 ++tracenode
->kn_refs
;
431 ktrops(struct proc
*curp
, struct proc
*p
, int ops
, int facs
,
432 ktrace_node_t tracenode
)
434 ktrace_node_t oldnode
;
436 if (!ktrcanset(curp
, p
))
438 if (ops
== KTROP_SET
) {
439 if ((oldnode
= p
->p_tracenode
) != tracenode
) {
440 p
->p_tracenode
= ktrinherit(tracenode
);
441 ktrdestroy(&oldnode
);
443 p
->p_traceflag
|= facs
;
444 if (curp
->p_ucred
->cr_uid
== 0)
445 p
->p_traceflag
|= KTRFAC_ROOT
;
448 if (((p
->p_traceflag
&= ~facs
) & KTRFAC_MASK
) == 0) {
449 /* no more tracing */
451 ktrdestroy(&p
->p_tracenode
);
459 ktrsetchildren(struct proc
*curp
, struct proc
*top
, int ops
, int facs
,
460 ktrace_node_t tracenode
)
467 ret
|= ktrops(curp
, p
, ops
, facs
, tracenode
);
469 * If this process has children, descend to them next,
470 * otherwise do any siblings, and if done with this level,
471 * follow back up the tree (but not past top).
473 if (!LIST_EMPTY(&p
->p_children
))
474 p
= LIST_FIRST(&p
->p_children
);
478 if (LIST_NEXT(p
, p_sibling
)) {
479 p
= LIST_NEXT(p
, p_sibling
);
489 ktrwrite(struct proc
*p
, struct ktr_header
*kth
, struct uio
*uio
)
491 struct ktrace_clear_info info
;
493 struct iovec aiov
[2];
495 ktrace_node_t tracenode
;
498 * We have to ref our tracenode to prevent it from being ripped out
499 * from under us while we are trying to use it. p_tracenode can
500 * go away at any time if another process gets a write error.
504 if (p
->p_tracenode
== NULL
)
506 tracenode
= ktrinherit(p
->p_tracenode
);
507 auio
.uio_iov
= &aiov
[0];
509 auio
.uio_segflg
= UIO_SYSSPACE
;
510 auio
.uio_rw
= UIO_WRITE
;
511 aiov
[0].iov_base
= (caddr_t
)kth
;
512 aiov
[0].iov_len
= sizeof(struct ktr_header
);
513 auio
.uio_resid
= sizeof(struct ktr_header
);
515 auio
.uio_td
= curthread
;
516 if (kth
->ktr_len
> 0) {
518 aiov
[1].iov_base
= kth
->ktr_buf
;
519 aiov
[1].iov_len
= kth
->ktr_len
;
520 auio
.uio_resid
+= kth
->ktr_len
;
522 kth
->ktr_len
+= uio
->uio_resid
;
524 vn_lock(tracenode
->kn_vp
, LK_EXCLUSIVE
| LK_RETRY
);
525 error
= VOP_WRITE(tracenode
->kn_vp
, &auio
,
526 IO_UNIT
| IO_APPEND
, p
->p_ucred
);
527 if (error
== 0 && uio
!= NULL
) {
528 error
= VOP_WRITE(tracenode
->kn_vp
, uio
,
529 IO_UNIT
| IO_APPEND
, p
->p_ucred
);
531 vn_unlock(tracenode
->kn_vp
);
534 * If an error occured, give up tracing on all processes
535 * using this tracenode. This is not MP safe but is
539 "ktrace write failed, errno %d, tracing stopped\n", error
);
540 info
.tracenode
= tracenode
;
543 allproc_scan(ktrace_clear_callback
, &info
);
545 ktrdestroy(&tracenode
);
549 * Return true if caller has permission to set the ktracing state
550 * of target. Essentially, the target can't possess any
551 * more permissions than the caller. KTRFAC_ROOT signifies that
552 * root previously set the tracing status on the target process, and
553 * so, only root may further change it.
555 * TODO: check groups. use caller effective gid.
558 ktrcanset(struct proc
*callp
, struct proc
*targetp
)
560 struct ucred
*caller
= callp
->p_ucred
;
561 struct ucred
*target
= targetp
->p_ucred
;
563 if (!PRISON_CHECK(caller
, target
))
565 if ((caller
->cr_uid
== target
->cr_ruid
&&
566 target
->cr_ruid
== target
->cr_svuid
&&
567 caller
->cr_rgid
== target
->cr_rgid
&& /* XXX */
568 target
->cr_rgid
== target
->cr_svgid
&&
569 (targetp
->p_traceflag
& KTRFAC_ROOT
) == 0 &&
570 (targetp
->p_flag
& P_SUGID
) == 0) ||