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.2 2003/06/17 04:28:41 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/namei.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 __P((int type));
59 static void ktrwrite __P((struct vnode *, struct ktr_header *, struct uio *));
60 static int ktrcanset __P((struct proc *,struct proc *));
61 static int ktrsetchildren __P((struct proc *,struct proc *,int,int,struct vnode *));
62 static int ktrops __P((struct proc *,struct proc *,int,int,struct vnode *));
65 static struct ktr_header *
69 register struct ktr_header *kth;
70 struct proc *p = curproc; /* XXX */
72 MALLOC(kth, struct ktr_header *, sizeof (struct ktr_header),
75 microtime(&kth->ktr_time);
76 kth->ktr_pid = p->p_pid;
77 bcopy(p->p_comm, kth->ktr_comm, MAXCOMLEN + 1);
82 ktrsyscall(vp, code, narg, args)
87 struct ktr_header *kth;
88 struct ktr_syscall *ktp;
89 register int len = offsetof(struct ktr_syscall, ktr_args) +
90 (narg * sizeof(register_t));
91 struct proc *p = curproc; /* XXX */
95 p->p_traceflag |= KTRFAC_ACTIVE;
96 kth = ktrgetheader(KTR_SYSCALL);
97 MALLOC(ktp, struct ktr_syscall *, len, M_KTRACE, M_WAITOK);
100 argp = &ktp->ktr_args[0];
101 for (i = 0; i < narg; i++)
103 kth->ktr_buf = (caddr_t)ktp;
105 ktrwrite(vp, kth, NULL);
108 p->p_traceflag &= ~KTRFAC_ACTIVE;
112 ktrsysret(vp, code, error, retval)
117 struct ktr_header *kth;
118 struct ktr_sysret ktp;
119 struct proc *p = curproc; /* XXX */
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(vp, kth, NULL);
132 p->p_traceflag &= ~KTRFAC_ACTIVE;
140 struct ktr_header *kth;
141 struct proc *p = curproc; /* XXX */
144 * don't let vp get ripped out from under us
148 p->p_traceflag |= KTRFAC_ACTIVE;
149 kth = ktrgetheader(KTR_NAMEI);
150 kth->ktr_len = strlen(path);
153 ktrwrite(vp, kth, NULL);
157 p->p_traceflag &= ~KTRFAC_ACTIVE;
161 ktrgenio(vp, fd, rw, uio, error)
168 struct ktr_header *kth;
169 struct ktr_genio ktg;
170 struct proc *p = curproc; /* XXX */
175 * don't let p_tracep get ripped out from under us
179 p->p_traceflag |= KTRFAC_ACTIVE;
180 kth = ktrgetheader(KTR_GENIO);
183 kth->ktr_buf = (caddr_t)&ktg;
184 kth->ktr_len = sizeof(struct ktr_genio);
186 uio->uio_rw = UIO_WRITE;
188 ktrwrite(vp, kth, uio);
192 p->p_traceflag &= ~KTRFAC_ACTIVE;
196 ktrpsig(vp, sig, action, mask, code)
203 struct ktr_header *kth;
205 struct proc *p = curproc; /* XXX */
208 * don't let vp get ripped out from under us
212 p->p_traceflag |= KTRFAC_ACTIVE;
213 kth = ktrgetheader(KTR_PSIG);
214 kp.signo = (char)sig;
218 kth->ktr_buf = (caddr_t)&kp;
219 kth->ktr_len = sizeof (struct ktr_psig);
221 ktrwrite(vp, kth, NULL);
225 p->p_traceflag &= ~KTRFAC_ACTIVE;
229 ktrcsw(vp, out, user)
233 struct ktr_header *kth;
235 struct proc *p = curproc; /* XXX */
238 * don't let vp get ripped out from under us
242 p->p_traceflag |= KTRFAC_ACTIVE;
243 kth = ktrgetheader(KTR_CSW);
246 kth->ktr_buf = (caddr_t)&kc;
247 kth->ktr_len = sizeof (struct ktr_csw);
249 ktrwrite(vp, kth, NULL);
253 p->p_traceflag &= ~KTRFAC_ACTIVE;
257 /* Interface and common routines */
262 #ifndef _SYS_SYSPROTO_H_
274 register struct ktrace_args *uap;
277 register struct vnode *vp = NULL;
278 register struct proc *p;
280 int facs = uap->facs & ~KTRFAC_ROOT;
281 int ops = KTROP(uap->ops);
282 int descend = uap->ops & KTRFLAG_DESCEND;
287 curp->p_traceflag |= KTRFAC_ACTIVE;
288 if (ops != KTROP_CLEAR) {
290 * an operation which requires a file argument.
292 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, curp);
293 error = vn_open(&nd, FREAD|FWRITE|O_NOFOLLOW, 0);
295 curp->p_traceflag &= ~KTRFAC_ACTIVE;
298 NDFREE(&nd, NDF_ONLY_PNBUF);
300 VOP_UNLOCK(vp, 0, curp);
301 if (vp->v_type != VREG) {
302 (void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
303 curp->p_traceflag &= ~KTRFAC_ACTIVE;
308 * Clear all uses of the tracefile. XXX umm, what happens to the
309 * loop if vn_close() blocks?
311 if (ops == KTROP_CLEARFILE) {
312 LIST_FOREACH(p, &allproc, p_list) {
313 if (p->p_tracep == vp) {
314 if (ktrcanset(curp, p) && p->p_tracep == vp) {
317 (void) vn_close(vp, FREAD|FWRITE,
327 * need something to (un)trace (XXX - why is this here?)
340 pg = pgfind(-uap->pid);
345 LIST_FOREACH(p, &pg->pg_members, p_pglist)
347 ret |= ktrsetchildren(curp, p, ops, facs, vp);
349 ret |= ktrops(curp, p, ops, facs, vp);
361 ret |= ktrsetchildren(curp, p, ops, facs, vp);
363 ret |= ktrops(curp, p, ops, facs, vp);
369 (void) vn_close(vp, FWRITE, curp->p_ucred, curp);
370 curp->p_traceflag &= ~KTRFAC_ACTIVE;
384 register struct utrace_args *uap;
387 struct ktr_header *kth;
388 struct proc *p = curproc; /* XXX */
392 if (!KTRPOINT(p, KTR_USER))
394 if (SCARG(uap, len) > KTR_USER_MAXLEN)
396 p->p_traceflag |= KTRFAC_ACTIVE;
398 * don't let p_tracep get ripped out from under us while we are
401 if ((vp = p->p_tracep) != NULL)
403 kth = ktrgetheader(KTR_USER);
404 MALLOC(cp, caddr_t, uap->len, M_KTRACE, M_WAITOK);
405 if (!copyin(uap->addr, cp, uap->len)) {
407 kth->ktr_len = uap->len;
408 ktrwrite(vp, kth, NULL);
414 p->p_traceflag &= ~KTRFAC_ACTIVE;
424 ktrops(curp, p, ops, facs, vp)
425 struct proc *p, *curp;
430 if (!ktrcanset(curp, p))
432 if (ops == KTROP_SET) {
433 if (p->p_tracep != vp) {
437 * if trace file already in use, relinquish
440 while ((vtmp = p->p_tracep) != NULL) {
446 p->p_traceflag |= facs;
447 if (curp->p_ucred->cr_uid == 0)
448 p->p_traceflag |= KTRFAC_ROOT;
451 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
454 /* no more tracing */
456 if ((vtmp = p->p_tracep) != NULL) {
467 ktrsetchildren(curp, top, ops, facs, vp)
468 struct proc *curp, *top;
472 register struct proc *p;
473 register int ret = 0;
477 ret |= ktrops(curp, p, ops, facs, vp);
479 * If this process has children, descend to them next,
480 * otherwise do any siblings, and if done with this level,
481 * follow back up the tree (but not past top).
483 if (!LIST_EMPTY(&p->p_children))
484 p = LIST_FIRST(&p->p_children);
488 if (LIST_NEXT(p, p_sibling)) {
489 p = LIST_NEXT(p, p_sibling);
499 ktrwrite(vp, kth, uio)
501 register struct ktr_header *kth;
505 struct iovec aiov[2];
506 register struct proc *p = curproc; /* XXX */
511 auio.uio_iov = &aiov[0];
513 auio.uio_segflg = UIO_SYSSPACE;
514 auio.uio_rw = UIO_WRITE;
515 aiov[0].iov_base = (caddr_t)kth;
516 aiov[0].iov_len = sizeof(struct ktr_header);
517 auio.uio_resid = sizeof(struct ktr_header);
519 auio.uio_procp = curproc;
520 if (kth->ktr_len > 0) {
522 aiov[1].iov_base = kth->ktr_buf;
523 aiov[1].iov_len = kth->ktr_len;
524 auio.uio_resid += kth->ktr_len;
526 kth->ktr_len += uio->uio_resid;
528 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
529 (void)VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
530 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, p->p_ucred);
531 if (error == 0 && uio != NULL) {
532 (void)VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
533 error = VOP_WRITE(vp, uio, IO_UNIT | IO_APPEND, p->p_ucred);
535 VOP_UNLOCK(vp, 0, p);
539 * If error encountered, give up tracing on this vnode. XXX what
540 * happens to the loop if vrele() blocks?
542 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
544 LIST_FOREACH(p, &allproc, p_list) {
545 if (p->p_tracep == vp) {
554 * Return true if caller has permission to set the ktracing state
555 * of target. Essentially, the target can't possess any
556 * more permissions than the caller. KTRFAC_ROOT signifies that
557 * root previously set the tracing status on the target process, and
558 * so, only root may further change it.
560 * TODO: check groups. use caller effective gid.
563 ktrcanset(callp, targetp)
564 struct proc *callp, *targetp;
566 register struct pcred *caller = callp->p_cred;
567 register struct pcred *target = targetp->p_cred;
569 if (!PRISON_CHECK(callp, targetp))
571 if ((caller->pc_ucred->cr_uid == target->p_ruid &&
572 target->p_ruid == target->p_svuid &&
573 caller->p_rgid == target->p_rgid && /* XXX */
574 target->p_rgid == target->p_svgid &&
575 (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
576 (targetp->p_flag & P_SUGID) == 0) ||
577 caller->pc_ucred->cr_uid == 0)