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 $
37 #include "opt_ktrace.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/sysproto.h>
42 #include <sys/kernel.h>
44 #include <sys/fcntl.h>
46 #include <sys/namei.h>
47 #include <sys/vnode.h>
48 #include <sys/ktrace.h>
49 #include <sys/malloc.h>
50 #include <sys/syslog.h>
51 #include <sys/sysent.h>
53 #include <vm/vm_zone.h>
54 static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
57 static struct ktr_header *ktrgetheader __P((int type));
58 static void ktrwrite __P((struct vnode *, struct ktr_header *, struct uio *));
59 static int ktrcanset __P((struct proc *,struct proc *));
60 static int ktrsetchildren __P((struct proc *,struct proc *,int,int,struct vnode *));
61 static int ktrops __P((struct proc *,struct proc *,int,int,struct vnode *));
64 static struct ktr_header *
68 register struct ktr_header *kth;
69 struct proc *p = curproc; /* XXX */
71 MALLOC(kth, struct ktr_header *, sizeof (struct ktr_header),
74 microtime(&kth->ktr_time);
75 kth->ktr_pid = p->p_pid;
76 bcopy(p->p_comm, kth->ktr_comm, MAXCOMLEN + 1);
81 ktrsyscall(vp, code, narg, args)
86 struct ktr_header *kth;
87 struct ktr_syscall *ktp;
88 register int len = offsetof(struct ktr_syscall, ktr_args) +
89 (narg * sizeof(register_t));
90 struct proc *p = curproc; /* XXX */
94 p->p_traceflag |= KTRFAC_ACTIVE;
95 kth = ktrgetheader(KTR_SYSCALL);
96 MALLOC(ktp, struct ktr_syscall *, len, M_KTRACE, M_WAITOK);
99 argp = &ktp->ktr_args[0];
100 for (i = 0; i < narg; i++)
102 kth->ktr_buf = (caddr_t)ktp;
104 ktrwrite(vp, kth, NULL);
107 p->p_traceflag &= ~KTRFAC_ACTIVE;
111 ktrsysret(vp, code, error, retval)
116 struct ktr_header *kth;
117 struct ktr_sysret ktp;
118 struct proc *p = curproc; /* XXX */
120 p->p_traceflag |= KTRFAC_ACTIVE;
121 kth = ktrgetheader(KTR_SYSRET);
123 ktp.ktr_error = error;
124 ktp.ktr_retval = retval; /* what about val2 ? */
126 kth->ktr_buf = (caddr_t)&ktp;
127 kth->ktr_len = sizeof(struct ktr_sysret);
129 ktrwrite(vp, kth, NULL);
131 p->p_traceflag &= ~KTRFAC_ACTIVE;
139 struct ktr_header *kth;
140 struct proc *p = curproc; /* XXX */
143 * don't let vp get ripped out from under us
147 p->p_traceflag |= KTRFAC_ACTIVE;
148 kth = ktrgetheader(KTR_NAMEI);
149 kth->ktr_len = strlen(path);
152 ktrwrite(vp, kth, NULL);
156 p->p_traceflag &= ~KTRFAC_ACTIVE;
160 ktrgenio(vp, fd, rw, uio, error)
167 struct ktr_header *kth;
168 struct ktr_genio ktg;
169 struct proc *p = curproc; /* XXX */
174 * don't let p_tracep get ripped out from under us
178 p->p_traceflag |= KTRFAC_ACTIVE;
179 kth = ktrgetheader(KTR_GENIO);
182 kth->ktr_buf = (caddr_t)&ktg;
183 kth->ktr_len = sizeof(struct ktr_genio);
185 uio->uio_rw = UIO_WRITE;
187 ktrwrite(vp, kth, uio);
191 p->p_traceflag &= ~KTRFAC_ACTIVE;
195 ktrpsig(vp, sig, action, mask, code)
202 struct ktr_header *kth;
204 struct proc *p = curproc; /* XXX */
207 * don't let vp get ripped out from under us
211 p->p_traceflag |= KTRFAC_ACTIVE;
212 kth = ktrgetheader(KTR_PSIG);
213 kp.signo = (char)sig;
217 kth->ktr_buf = (caddr_t)&kp;
218 kth->ktr_len = sizeof (struct ktr_psig);
220 ktrwrite(vp, kth, NULL);
224 p->p_traceflag &= ~KTRFAC_ACTIVE;
228 ktrcsw(vp, out, user)
232 struct ktr_header *kth;
234 struct proc *p = curproc; /* XXX */
237 * don't let vp get ripped out from under us
241 p->p_traceflag |= KTRFAC_ACTIVE;
242 kth = ktrgetheader(KTR_CSW);
245 kth->ktr_buf = (caddr_t)&kc;
246 kth->ktr_len = sizeof (struct ktr_csw);
248 ktrwrite(vp, kth, NULL);
252 p->p_traceflag &= ~KTRFAC_ACTIVE;
256 /* Interface and common routines */
261 #ifndef _SYS_SYSPROTO_H_
273 register struct ktrace_args *uap;
276 register struct vnode *vp = NULL;
277 register struct proc *p;
279 int facs = uap->facs & ~KTRFAC_ROOT;
280 int ops = KTROP(uap->ops);
281 int descend = uap->ops & KTRFLAG_DESCEND;
286 curp->p_traceflag |= KTRFAC_ACTIVE;
287 if (ops != KTROP_CLEAR) {
289 * an operation which requires a file argument.
291 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, curp);
292 error = vn_open(&nd, FREAD|FWRITE|O_NOFOLLOW, 0);
294 curp->p_traceflag &= ~KTRFAC_ACTIVE;
297 NDFREE(&nd, NDF_ONLY_PNBUF);
299 VOP_UNLOCK(vp, 0, curp);
300 if (vp->v_type != VREG) {
301 (void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
302 curp->p_traceflag &= ~KTRFAC_ACTIVE;
307 * Clear all uses of the tracefile. XXX umm, what happens to the
308 * loop if vn_close() blocks?
310 if (ops == KTROP_CLEARFILE) {
311 LIST_FOREACH(p, &allproc, p_list) {
312 if (p->p_tracep == vp) {
313 if (ktrcanset(curp, p) && p->p_tracep == vp) {
316 (void) vn_close(vp, FREAD|FWRITE,
326 * need something to (un)trace (XXX - why is this here?)
339 pg = pgfind(-uap->pid);
344 LIST_FOREACH(p, &pg->pg_members, p_pglist)
346 ret |= ktrsetchildren(curp, p, ops, facs, vp);
348 ret |= ktrops(curp, p, ops, facs, vp);
360 ret |= ktrsetchildren(curp, p, ops, facs, vp);
362 ret |= ktrops(curp, p, ops, facs, vp);
368 (void) vn_close(vp, FWRITE, curp->p_ucred, curp);
369 curp->p_traceflag &= ~KTRFAC_ACTIVE;
383 register struct utrace_args *uap;
386 struct ktr_header *kth;
387 struct proc *p = curproc; /* XXX */
391 if (!KTRPOINT(p, KTR_USER))
393 if (SCARG(uap, len) > KTR_USER_MAXLEN)
395 p->p_traceflag |= KTRFAC_ACTIVE;
397 * don't let p_tracep get ripped out from under us while we are
400 if ((vp = p->p_tracep) != NULL)
402 kth = ktrgetheader(KTR_USER);
403 MALLOC(cp, caddr_t, uap->len, M_KTRACE, M_WAITOK);
404 if (!copyin(uap->addr, cp, uap->len)) {
406 kth->ktr_len = uap->len;
407 ktrwrite(vp, kth, NULL);
413 p->p_traceflag &= ~KTRFAC_ACTIVE;
423 ktrops(curp, p, ops, facs, vp)
424 struct proc *p, *curp;
429 if (!ktrcanset(curp, p))
431 if (ops == KTROP_SET) {
432 if (p->p_tracep != vp) {
436 * if trace file already in use, relinquish
439 while ((vtmp = p->p_tracep) != NULL) {
445 p->p_traceflag |= facs;
446 if (curp->p_ucred->cr_uid == 0)
447 p->p_traceflag |= KTRFAC_ROOT;
450 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
453 /* no more tracing */
455 if ((vtmp = p->p_tracep) != NULL) {
466 ktrsetchildren(curp, top, ops, facs, vp)
467 struct proc *curp, *top;
471 register struct proc *p;
472 register int ret = 0;
476 ret |= ktrops(curp, p, ops, facs, vp);
478 * If this process has children, descend to them next,
479 * otherwise do any siblings, and if done with this level,
480 * follow back up the tree (but not past top).
482 if (!LIST_EMPTY(&p->p_children))
483 p = LIST_FIRST(&p->p_children);
487 if (LIST_NEXT(p, p_sibling)) {
488 p = LIST_NEXT(p, p_sibling);
498 ktrwrite(vp, kth, uio)
500 register struct ktr_header *kth;
504 struct iovec aiov[2];
505 register struct proc *p = curproc; /* XXX */
510 auio.uio_iov = &aiov[0];
512 auio.uio_segflg = UIO_SYSSPACE;
513 auio.uio_rw = UIO_WRITE;
514 aiov[0].iov_base = (caddr_t)kth;
515 aiov[0].iov_len = sizeof(struct ktr_header);
516 auio.uio_resid = sizeof(struct ktr_header);
518 auio.uio_procp = curproc;
519 if (kth->ktr_len > 0) {
521 aiov[1].iov_base = kth->ktr_buf;
522 aiov[1].iov_len = kth->ktr_len;
523 auio.uio_resid += kth->ktr_len;
525 kth->ktr_len += uio->uio_resid;
527 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
528 (void)VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
529 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, p->p_ucred);
530 if (error == 0 && uio != NULL) {
531 (void)VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
532 error = VOP_WRITE(vp, uio, IO_UNIT | IO_APPEND, p->p_ucred);
534 VOP_UNLOCK(vp, 0, p);
538 * If error encountered, give up tracing on this vnode. XXX what
539 * happens to the loop if vrele() blocks?
541 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
543 LIST_FOREACH(p, &allproc, p_list) {
544 if (p->p_tracep == vp) {
553 * Return true if caller has permission to set the ktracing state
554 * of target. Essentially, the target can't possess any
555 * more permissions than the caller. KTRFAC_ROOT signifies that
556 * root previously set the tracing status on the target process, and
557 * so, only root may further change it.
559 * TODO: check groups. use caller effective gid.
562 ktrcanset(callp, targetp)
563 struct proc *callp, *targetp;
565 register struct pcred *caller = callp->p_cred;
566 register struct pcred *target = targetp->p_cred;
568 if (!PRISON_CHECK(callp, targetp))
570 if ((caller->pc_ucred->cr_uid == target->p_ruid &&
571 target->p_ruid == target->p_svuid &&
572 caller->p_rgid == target->p_rgid && /* XXX */
573 target->p_rgid == target->p_svgid &&
574 (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
575 (targetp->p_flag & P_SUGID) == 0) ||
576 caller->pc_ucred->cr_uid == 0)