Pass the process (p) instead of the vnode (p->p_tracep) to the kernel tracing
[dragonfly/netmp.git] / sys / kern / kern_ktrace.c
blobf2aa20856badc26927668f9366da2166ed5478ef
1 /*
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
7 * are met:
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
31 * SUCH DAMAGE.
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.23 2006/05/17 18:30:20 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>
44 #include <sys/proc.h>
45 #include <sys/fcntl.h>
46 #include <sys/lock.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");
57 #ifdef 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,struct vnode *);
62 static int ktrops (struct proc *,struct proc *,int,int,struct vnode *);
65 static struct ktr_header *
66 ktrgetheader(int type)
68 struct ktr_header *kth;
69 struct proc *p = curproc; /* XXX */
71 MALLOC(kth, struct ktr_header *, sizeof (struct ktr_header),
72 M_KTRACE, M_WAITOK);
73 kth->ktr_type = type;
74 microtime(&kth->ktr_time);
75 kth->ktr_pid = p->p_pid;
76 bcopy(p->p_comm, kth->ktr_comm, MAXCOMLEN + 1);
77 return (kth);
80 void
81 ktrsyscall(struct proc *p, int code, int narg, register_t args[])
83 struct ktr_header *kth;
84 struct ktr_syscall *ktp;
85 int len;
86 register_t *argp;
87 int i;
89 len = offsetof(struct ktr_syscall, ktr_args) +
90 (narg * sizeof(register_t));
91 p->p_traceflag |= KTRFAC_ACTIVE;
92 kth = ktrgetheader(KTR_SYSCALL);
93 MALLOC(ktp, struct ktr_syscall *, len, M_KTRACE, M_WAITOK);
94 ktp->ktr_code = code;
95 ktp->ktr_narg = narg;
96 argp = &ktp->ktr_args[0];
97 for (i = 0; i < narg; i++)
98 *argp++ = args[i];
99 kth->ktr_buf = (caddr_t)ktp;
100 kth->ktr_len = len;
101 ktrwrite(p, kth, NULL);
102 FREE(ktp, M_KTRACE);
103 FREE(kth, M_KTRACE);
104 p->p_traceflag &= ~KTRFAC_ACTIVE;
107 void
108 ktrsysret(struct proc *p, int code, int error, register_t retval)
110 struct ktr_header *kth;
111 struct ktr_sysret ktp;
113 p->p_traceflag |= KTRFAC_ACTIVE;
114 kth = ktrgetheader(KTR_SYSRET);
115 ktp.ktr_code = code;
116 ktp.ktr_error = error;
117 ktp.ktr_retval = retval; /* what about val2 ? */
119 kth->ktr_buf = (caddr_t)&ktp;
120 kth->ktr_len = sizeof(struct ktr_sysret);
122 ktrwrite(p, kth, NULL);
123 FREE(kth, M_KTRACE);
124 p->p_traceflag &= ~KTRFAC_ACTIVE;
127 void
128 ktrnamei(struct proc *p, char *path)
130 struct ktr_header *kth;
132 p->p_traceflag |= KTRFAC_ACTIVE;
133 kth = ktrgetheader(KTR_NAMEI);
134 kth->ktr_len = strlen(path);
135 kth->ktr_buf = path;
137 ktrwrite(p, kth, NULL);
138 FREE(kth, M_KTRACE);
139 p->p_traceflag &= ~KTRFAC_ACTIVE;
142 void
143 ktrgenio(struct proc *p, int fd, enum uio_rw rw, struct uio *uio, int error)
145 struct ktr_header *kth;
146 struct ktr_genio ktg;
148 if (error)
149 return;
151 * don't let p_tracep get ripped out from under us
153 p->p_traceflag |= KTRFAC_ACTIVE;
154 kth = ktrgetheader(KTR_GENIO);
155 ktg.ktr_fd = fd;
156 ktg.ktr_rw = rw;
157 kth->ktr_buf = (caddr_t)&ktg;
158 kth->ktr_len = sizeof(struct ktr_genio);
159 uio->uio_offset = 0;
160 uio->uio_rw = UIO_WRITE;
162 ktrwrite(p, kth, uio);
163 FREE(kth, M_KTRACE);
164 p->p_traceflag &= ~KTRFAC_ACTIVE;
167 void
168 ktrpsig(struct proc *p, int sig, sig_t action, sigset_t *mask, int code)
170 struct ktr_header *kth;
171 struct ktr_psig kp;
173 p->p_traceflag |= KTRFAC_ACTIVE;
174 kth = ktrgetheader(KTR_PSIG);
175 kp.signo = (char)sig;
176 kp.action = action;
177 kp.mask = *mask;
178 kp.code = code;
179 kth->ktr_buf = (caddr_t)&kp;
180 kth->ktr_len = sizeof (struct ktr_psig);
182 ktrwrite(p, kth, NULL);
183 FREE(kth, M_KTRACE);
184 p->p_traceflag &= ~KTRFAC_ACTIVE;
187 void
188 ktrcsw(struct proc *p, int out, int user)
190 struct ktr_header *kth;
191 struct ktr_csw kc;
193 p->p_traceflag |= KTRFAC_ACTIVE;
194 kth = ktrgetheader(KTR_CSW);
195 kc.out = out;
196 kc.user = user;
197 kth->ktr_buf = (caddr_t)&kc;
198 kth->ktr_len = sizeof (struct ktr_csw);
200 ktrwrite(p, kth, NULL);
201 FREE(kth, M_KTRACE);
202 p->p_traceflag &= ~KTRFAC_ACTIVE;
204 #endif
206 /* Interface and common routines */
209 * ktrace system call
211 /* ARGSUSED */
213 ktrace(struct ktrace_args *uap)
215 #ifdef KTRACE
216 struct thread *td = curthread;
217 struct proc *curp = td->td_proc;
218 struct vnode *vp = NULL;
219 struct proc *p;
220 struct pgrp *pg;
221 int facs = uap->facs & ~KTRFAC_ROOT;
222 int ops = KTROP(uap->ops);
223 int descend = uap->ops & KTRFLAG_DESCEND;
224 int ret = 0;
225 int error = 0;
226 struct nlookupdata nd;
228 curp->p_traceflag |= KTRFAC_ACTIVE;
229 if (ops != KTROP_CLEAR) {
231 * an operation which requires a file argument.
233 error = nlookup_init(&nd, uap->fname,
234 UIO_USERSPACE, NLC_LOCKVP);
235 if (error == 0)
236 error = vn_open(&nd, NULL, FREAD|FWRITE|O_NOFOLLOW, 0);
237 if (error == 0 && nd.nl_open_vp->v_type != VREG)
238 error = EACCES;
239 if (error) {
240 curp->p_traceflag &= ~KTRFAC_ACTIVE;
241 nlookup_done(&nd);
242 return (error);
244 vp = nd.nl_open_vp;
245 nd.nl_open_vp = NULL;
246 nlookup_done(&nd);
247 VOP_UNLOCK(vp, 0);
250 * Clear all uses of the tracefile. XXX umm, what happens to the
251 * loop if vn_close() blocks?
253 if (ops == KTROP_CLEARFILE) {
254 FOREACH_PROC_IN_SYSTEM(p) {
255 if (p->p_tracep == vp) {
256 if (ktrcanset(curp, p) && p->p_tracep == vp) {
257 p->p_tracep = NULL;
258 p->p_traceflag = 0;
259 vn_close(vp, FREAD|FWRITE);
260 } else {
261 error = EPERM;
265 goto done;
268 * need something to (un)trace (XXX - why is this here?)
270 if (!facs) {
271 error = EINVAL;
272 goto done;
275 * do it
277 if (uap->pid < 0) {
279 * by process group
281 pg = pgfind(-uap->pid);
282 if (pg == NULL) {
283 error = ESRCH;
284 goto done;
286 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
287 if (descend)
288 ret |= ktrsetchildren(curp, p, ops, facs, vp);
289 else
290 ret |= ktrops(curp, p, ops, facs, vp);
292 } else {
294 * by pid
296 p = pfind(uap->pid);
297 if (p == NULL) {
298 error = ESRCH;
299 goto done;
301 if (descend)
302 ret |= ktrsetchildren(curp, p, ops, facs, vp);
303 else
304 ret |= ktrops(curp, p, ops, facs, vp);
306 if (!ret)
307 error = EPERM;
308 done:
309 if (vp != NULL)
310 vn_close(vp, FWRITE);
311 curp->p_traceflag &= ~KTRFAC_ACTIVE;
312 return (error);
313 #else
314 return ENOSYS;
315 #endif
319 * utrace system call
321 /* ARGSUSED */
323 utrace(struct utrace_args *uap)
325 #ifdef KTRACE
326 struct ktr_header *kth;
327 struct thread *td = curthread; /* XXX */
328 struct proc *p = td->td_proc;
329 caddr_t cp;
331 if (!KTRPOINT(td, KTR_USER))
332 return (0);
333 if (uap->len > KTR_USER_MAXLEN)
334 return (EINVAL);
335 p->p_traceflag |= KTRFAC_ACTIVE;
337 * don't let p_tracep get ripped out from under us while we are
338 * writing.
340 kth = ktrgetheader(KTR_USER);
341 MALLOC(cp, caddr_t, uap->len, M_KTRACE, M_WAITOK);
342 if (!copyin(uap->addr, cp, uap->len)) {
343 kth->ktr_buf = cp;
344 kth->ktr_len = uap->len;
345 ktrwrite(p, kth, NULL);
347 FREE(kth, M_KTRACE);
348 FREE(cp, M_KTRACE);
349 p->p_traceflag &= ~KTRFAC_ACTIVE;
351 return (0);
352 #else
353 return (ENOSYS);
354 #endif
357 #ifdef KTRACE
358 static int
359 ktrops(struct proc *curp, struct proc *p, int ops, int facs, struct vnode *vp)
362 if (!ktrcanset(curp, p))
363 return (0);
364 if (ops == KTROP_SET) {
365 if (p->p_tracep != vp) {
366 struct vnode *vtmp;
369 * if trace file already in use, relinquish
371 vref(vp);
372 while ((vtmp = p->p_tracep) != NULL) {
373 p->p_tracep = NULL;
374 vrele(vtmp);
376 p->p_tracep = vp;
378 p->p_traceflag |= facs;
379 if (curp->p_ucred->cr_uid == 0)
380 p->p_traceflag |= KTRFAC_ROOT;
381 } else {
382 /* KTROP_CLEAR */
383 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
384 struct vnode *vtmp;
386 /* no more tracing */
387 p->p_traceflag = 0;
388 if ((vtmp = p->p_tracep) != NULL) {
389 p->p_tracep = NULL;
390 vrele(vtmp);
395 return (1);
398 static int
399 ktrsetchildren(struct proc *curp, struct proc *top, int ops, int facs,
400 struct vnode *vp)
402 struct proc *p;
403 int ret = 0;
405 p = top;
406 for (;;) {
407 ret |= ktrops(curp, p, ops, facs, vp);
409 * If this process has children, descend to them next,
410 * otherwise do any siblings, and if done with this level,
411 * follow back up the tree (but not past top).
413 if (!LIST_EMPTY(&p->p_children))
414 p = LIST_FIRST(&p->p_children);
415 else for (;;) {
416 if (p == top)
417 return (ret);
418 if (LIST_NEXT(p, p_sibling)) {
419 p = LIST_NEXT(p, p_sibling);
420 break;
422 p = p->p_pptr;
425 /*NOTREACHED*/
428 static void
429 ktrwrite(struct proc *p, struct ktr_header *kth, struct uio *uio)
431 struct uio auio;
432 struct iovec aiov[2];
433 struct vnode *vp;
434 int error;
437 * Since multiple processes may be trying to ktrace, we must vref
438 * the vnode to prevent it from being ripped out from under us in
439 * case another process gets a write error while ktracing and removes
440 * the reference from the proc.
442 * XXX not MP safe
444 if ((vp = p->p_tracep) == NULL)
445 return;
446 vref(vp);
447 auio.uio_iov = &aiov[0];
448 auio.uio_offset = 0;
449 auio.uio_segflg = UIO_SYSSPACE;
450 auio.uio_rw = UIO_WRITE;
451 aiov[0].iov_base = (caddr_t)kth;
452 aiov[0].iov_len = sizeof(struct ktr_header);
453 auio.uio_resid = sizeof(struct ktr_header);
454 auio.uio_iovcnt = 1;
455 auio.uio_td = curthread;
456 if (kth->ktr_len > 0) {
457 auio.uio_iovcnt++;
458 aiov[1].iov_base = kth->ktr_buf;
459 aiov[1].iov_len = kth->ktr_len;
460 auio.uio_resid += kth->ktr_len;
461 if (uio != NULL)
462 kth->ktr_len += uio->uio_resid;
464 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
465 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, p->p_ucred);
466 if (error == 0 && uio != NULL) {
467 error = VOP_WRITE(vp, uio, IO_UNIT | IO_APPEND, p->p_ucred);
469 VOP_UNLOCK(vp, 0);
470 vrele(vp);
471 if (!error)
472 return;
474 * If error encountered, give up tracing on this vnode. XXX what
475 * happens to the loop if vrele() blocks?
477 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
478 error);
479 FOREACH_PROC_IN_SYSTEM(p) {
480 if (p->p_tracep == vp) {
481 p->p_tracep = NULL;
482 p->p_traceflag = 0;
483 vrele(vp);
489 * Return true if caller has permission to set the ktracing state
490 * of target. Essentially, the target can't possess any
491 * more permissions than the caller. KTRFAC_ROOT signifies that
492 * root previously set the tracing status on the target process, and
493 * so, only root may further change it.
495 * TODO: check groups. use caller effective gid.
497 static int
498 ktrcanset(struct proc *callp, struct proc *targetp)
500 struct ucred *caller = callp->p_ucred;
501 struct ucred *target = targetp->p_ucred;
503 if (!PRISON_CHECK(caller, target))
504 return (0);
505 if ((caller->cr_uid == target->cr_ruid &&
506 target->cr_ruid == target->cr_svuid &&
507 caller->cr_rgid == target->cr_rgid && /* XXX */
508 target->cr_rgid == target->cr_svgid &&
509 (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
510 (targetp->p_flag & P_SUGID) == 0) ||
511 caller->cr_uid == 0)
512 return (1);
514 return (0);
517 #endif /* KTRACE */