kern_ktrace.c revision 1.34 1 /* $NetBSD: kern_ktrace.c,v 1.34 1999/04/11 18:44:00 kleink Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)kern_ktrace.c 8.5 (Berkeley) 5/14/95
36 */
37
38 #include "opt_ktrace.h"
39
40 #ifdef KTRACE
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <sys/file.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/filedesc.h>
52
53 #include <sys/mount.h>
54 #include <sys/syscallargs.h>
55
56 struct ktr_header *ktrgetheader __P((int));
57 int ktrops __P((struct proc *, struct proc *, int, int, void *));
58 int ktrsetchildren __P((struct proc *, struct proc *, int, int, void *));
59 void ktrwrite __P((struct proc *, void *, struct ktr_header *));
60 int ktrcanset __P((struct proc *, struct proc *));
61
62 void
63 ktrderef(p)
64 struct proc *p;
65 {
66 if (p->p_tracep == NULL)
67 return;
68
69 if (p->p_traceflag & KTRFAC_FD) {
70 struct file *fp = p->p_tracep;
71
72 closef(fp, NULL);
73 } else {
74 struct vnode *vp = p->p_tracep;
75
76 vrele(vp);
77 }
78 p->p_tracep = NULL;
79 p->p_traceflag = 0;
80 }
81
82 void
83 ktradref(p)
84 struct proc *p;
85 {
86 if (p->p_traceflag & KTRFAC_FD) {
87 struct file *fp = p->p_tracep;
88
89 fp->f_count++;
90 } else {
91 struct vnode *vp = p->p_tracep;
92
93 VREF(vp);
94 }
95 }
96
97 struct ktr_header *
98 ktrgetheader(type)
99 int type;
100 {
101 struct ktr_header *kth;
102 struct proc *p = curproc; /* XXX */
103
104 MALLOC(kth, struct ktr_header *, sizeof(struct ktr_header),
105 M_TEMP, M_WAITOK);
106 kth->ktr_type = type;
107 microtime(&kth->ktr_time);
108 kth->ktr_pid = p->p_pid;
109 memcpy(kth->ktr_comm, p->p_comm, MAXCOMLEN);
110 /* Note: ktr_len and ktr_buf are left to be filled in by the caller. */
111 return (kth);
112 }
113
114 void
115 ktrsyscall(v, code, argsize, args)
116 void *v;
117 register_t code;
118 size_t argsize;
119 register_t args[];
120 {
121 struct ktr_header *kth;
122 struct ktr_syscall *ktp;
123 struct proc *p = curproc; /* XXX */
124 register_t *argp;
125 int len = sizeof(struct ktr_syscall) + argsize;
126 int i;
127
128 p->p_traceflag |= KTRFAC_ACTIVE;
129 kth = ktrgetheader(KTR_SYSCALL);
130 MALLOC(ktp, struct ktr_syscall *, len, M_TEMP, M_WAITOK);
131 ktp->ktr_code = code;
132 ktp->ktr_argsize = argsize;
133 argp = (register_t *)((char *)ktp + sizeof(struct ktr_syscall));
134 for (i = 0; i < (argsize / sizeof(*argp)); i++)
135 *argp++ = args[i];
136 kth->ktr_buf = (caddr_t)ktp;
137 kth->ktr_len = len;
138 ktrwrite(p, v, kth);
139 FREE(ktp, M_TEMP);
140 FREE(kth, M_TEMP);
141 p->p_traceflag &= ~KTRFAC_ACTIVE;
142 }
143
144 void
145 ktrsysret(v, code, error, retval)
146 void *v;
147 register_t code;
148 int error;
149 register_t retval;
150 {
151 struct ktr_header *kth;
152 struct ktr_sysret ktp;
153 struct proc *p = curproc; /* XXX */
154
155 p->p_traceflag |= KTRFAC_ACTIVE;
156 kth = ktrgetheader(KTR_SYSRET);
157 ktp.ktr_code = code;
158 ktp.ktr_eosys = 0; /* XXX unused */
159 ktp.ktr_error = error;
160 ktp.ktr_retval = retval; /* what about val2 ? */
161
162 kth->ktr_buf = (caddr_t)&ktp;
163 kth->ktr_len = sizeof(struct ktr_sysret);
164
165 ktrwrite(p, v, kth);
166 FREE(kth, M_TEMP);
167 p->p_traceflag &= ~KTRFAC_ACTIVE;
168 }
169
170 void
171 ktrnamei(v, path)
172 void *v;
173 char *path;
174 {
175 struct ktr_header *kth;
176 struct proc *p = curproc; /* XXX */
177
178 p->p_traceflag |= KTRFAC_ACTIVE;
179 kth = ktrgetheader(KTR_NAMEI);
180 kth->ktr_len = strlen(path);
181 kth->ktr_buf = path;
182
183 ktrwrite(p, v, kth);
184 FREE(kth, M_TEMP);
185 p->p_traceflag &= ~KTRFAC_ACTIVE;
186 }
187
188 void
189 ktremul(v, p, emul)
190 void *v;
191 struct proc *p;
192 char *emul;
193 {
194 struct ktr_header *kth;
195
196 p->p_traceflag |= KTRFAC_ACTIVE;
197 kth = ktrgetheader(KTR_EMUL);
198 kth->ktr_len = strlen(emul);
199 kth->ktr_buf = emul;
200
201 ktrwrite(p, v, kth);
202 FREE(kth, M_TEMP);
203 p->p_traceflag &= ~KTRFAC_ACTIVE;
204 }
205
206 void
207 ktrgenio(v, fd, rw, iov, len, error)
208 void *v;
209 int fd;
210 enum uio_rw rw;
211 struct iovec *iov;
212 int len, error;
213 {
214 struct ktr_header *kth;
215 struct ktr_genio *ktp;
216 caddr_t cp;
217 int resid = len, cnt;
218 struct proc *p = curproc; /* XXX */
219
220 if (error)
221 return;
222 p->p_traceflag |= KTRFAC_ACTIVE;
223 kth = ktrgetheader(KTR_GENIO);
224 MALLOC(ktp, struct ktr_genio *, sizeof(struct ktr_genio) + len,
225 M_TEMP, M_WAITOK);
226 ktp->ktr_fd = fd;
227 ktp->ktr_rw = rw;
228 cp = (caddr_t)((char *)ktp + sizeof(struct ktr_genio));
229 while (resid > 0) {
230 if ((cnt = iov->iov_len) > resid)
231 cnt = resid;
232 if (copyin(iov->iov_base, cp, (unsigned)cnt))
233 goto done;
234 cp += cnt;
235 resid -= cnt;
236 iov++;
237 }
238 kth->ktr_buf = (caddr_t)ktp;
239 kth->ktr_len = sizeof(struct ktr_genio) + len;
240
241 ktrwrite(p, v, kth);
242 done:
243 FREE(kth, M_TEMP);
244 FREE(ktp, M_TEMP);
245 p->p_traceflag &= ~KTRFAC_ACTIVE;
246 }
247
248 void
249 ktrpsig(v, sig, action, mask, code)
250 void *v;
251 int sig;
252 sig_t action;
253 sigset_t *mask;
254 int code;
255 {
256 struct ktr_header *kth;
257 struct ktr_psig kp;
258 struct proc *p = curproc; /* XXX */
259
260 p->p_traceflag |= KTRFAC_ACTIVE;
261 kth = ktrgetheader(KTR_PSIG);
262 kp.signo = (char)sig;
263 kp.action = action;
264 kp.mask = *mask;
265 kp.code = code;
266 kth->ktr_buf = (caddr_t)&kp;
267 kth->ktr_len = sizeof(struct ktr_psig);
268
269 ktrwrite(p, v, kth);
270 FREE(kth, M_TEMP);
271 p->p_traceflag &= ~KTRFAC_ACTIVE;
272 }
273
274 void
275 ktrcsw(v, out, user)
276 void *v;
277 int out, user;
278 {
279 struct ktr_header *kth;
280 struct ktr_csw kc;
281 struct proc *p = curproc; /* XXX */
282
283 p->p_traceflag |= KTRFAC_ACTIVE;
284 kth = ktrgetheader(KTR_CSW);
285 kc.out = out;
286 kc.user = user;
287 kth->ktr_buf = (caddr_t)&kc;
288 kth->ktr_len = sizeof(struct ktr_csw);
289
290 ktrwrite(p, v, kth);
291 FREE(kth, M_TEMP);
292 p->p_traceflag &= ~KTRFAC_ACTIVE;
293 }
294
295 /* Interface and common routines */
296
297 /*
298 * ktrace system call
299 */
300 /* ARGSUSED */
301 int
302 sys_fktrace(curp, v, retval)
303 struct proc *curp;
304 void *v;
305 register_t *retval;
306 {
307 struct sys_fktrace_args /* {
308 syscallarg(int) fd;
309 syscallarg(int) ops;
310 syscallarg(int) facs;
311 syscallarg(int) pid;
312 } */ *uap = v;
313 struct file *fp = NULL;
314 struct proc *p;
315 struct filedesc *fdp = curp->p_fd;
316 struct pgrp *pg;
317 int facs;
318 int ops;
319 int descend;
320 int ret = 0;
321 int error = 0;
322
323 if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
324 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
325 (fp->f_flag & FWRITE) == 0)
326 return (EBADF);
327
328 ops = KTROP(SCARG(uap, ops)) | KTRFLAG_FD;
329 descend = SCARG(uap, ops) & KTRFLAG_DESCEND;
330 facs = SCARG(uap, facs) & ~((unsigned) KTRFAC_ROOT);
331 curp->p_traceflag |= KTRFAC_ACTIVE;
332
333 /*
334 * Clear all uses of the tracefile
335 */
336 if (KTROP(ops) == KTROP_CLEARFILE) {
337 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
338 if (p->p_tracep == fp) {
339 if (ktrcanset(curp, p))
340 ktrderef(p);
341 else
342 error = EPERM;
343 }
344 }
345 goto done;
346 }
347 /*
348 * need something to (un)trace (XXX - why is this here?)
349 */
350 if (!facs) {
351 error = EINVAL;
352 goto done;
353 }
354 /*
355 * do it
356 */
357 if (SCARG(uap, pid) < 0) {
358 /*
359 * by process group
360 */
361 pg = pgfind(-SCARG(uap, pid));
362 if (pg == NULL) {
363 error = ESRCH;
364 goto done;
365 }
366 for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next)
367 if (descend)
368 ret |= ktrsetchildren(curp, p, ops, facs, fp);
369 else
370 ret |= ktrops(curp, p, ops, facs, fp);
371
372 } else {
373 /*
374 * by pid
375 */
376 p = pfind(SCARG(uap, pid));
377 if (p == NULL) {
378 error = ESRCH;
379 goto done;
380 }
381 if (descend)
382 ret |= ktrsetchildren(curp, p, ops, facs, fp);
383 else
384 ret |= ktrops(curp, p, ops, facs, fp);
385 }
386 if (!ret)
387 error = EPERM;
388 done:
389 curp->p_traceflag &= ~KTRFAC_ACTIVE;
390 return (error);
391 }
392
393 /*
394 * ktrace system call
395 */
396 /* ARGSUSED */
397 int
398 sys_ktrace(curp, v, retval)
399 struct proc *curp;
400 void *v;
401 register_t *retval;
402 {
403 struct sys_ktrace_args /* {
404 syscallarg(const char *) fname;
405 syscallarg(int) ops;
406 syscallarg(int) facs;
407 syscallarg(int) pid;
408 } */ *uap = v;
409 struct vnode *vp = NULL;
410 struct proc *p;
411 struct pgrp *pg;
412 int facs = SCARG(uap, facs) & ~((unsigned) KTRFAC_ROOT);
413 int ops = KTROP(SCARG(uap, ops));
414 int descend = SCARG(uap, ops) & KTRFLAG_DESCEND;
415 int ret = 0;
416 int error = 0;
417 struct nameidata nd;
418
419 curp->p_traceflag |= KTRFAC_ACTIVE;
420 if (ops != KTROP_CLEAR) {
421 /*
422 * an operation which requires a file argument.
423 */
424 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
425 curp);
426 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
427 curp->p_traceflag &= ~KTRFAC_ACTIVE;
428 return (error);
429 }
430 vp = nd.ni_vp;
431 VOP_UNLOCK(vp, 0);
432 if (vp->v_type != VREG) {
433 (void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
434 curp->p_traceflag &= ~KTRFAC_ACTIVE;
435 return (EACCES);
436 }
437 }
438 /*
439 * Clear all uses of the tracefile
440 */
441 if (KTROP(ops) == KTROP_CLEARFILE) {
442 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
443 if (p->p_tracep == vp &&
444 !ktrops(curp, p, KTROP_CLEAR, ~0, vp))
445 error = EPERM;
446 }
447 goto done;
448 }
449 /*
450 * need something to (un)trace (XXX - why is this here?)
451 */
452 if (!facs) {
453 error = EINVAL;
454 goto done;
455 }
456 /*
457 * do it
458 */
459 if (SCARG(uap, pid) < 0) {
460 /*
461 * by process group
462 */
463 pg = pgfind(-SCARG(uap, pid));
464 if (pg == NULL) {
465 error = ESRCH;
466 goto done;
467 }
468 for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next)
469 if (descend)
470 ret |= ktrsetchildren(curp, p, ops, facs, vp);
471 else
472 ret |= ktrops(curp, p, ops, facs, vp);
473
474 } else {
475 /*
476 * by pid
477 */
478 p = pfind(SCARG(uap, pid));
479 if (p == NULL) {
480 error = ESRCH;
481 goto done;
482 }
483 if (descend)
484 ret |= ktrsetchildren(curp, p, ops, facs, vp);
485 else
486 ret |= ktrops(curp, p, ops, facs, vp);
487 }
488 if (!ret)
489 error = EPERM;
490 done:
491 if (vp != NULL)
492 (void) vn_close(vp, FWRITE, curp->p_ucred, curp);
493 curp->p_traceflag &= ~KTRFAC_ACTIVE;
494 return (error);
495 }
496
497 int
498 ktrops(curp, p, ops, facs, v)
499 struct proc *p, *curp;
500 int ops, facs;
501 void *v;
502 {
503
504 if (!ktrcanset(curp, p))
505 return (0);
506 if (KTROP(ops) == KTROP_SET) {
507 if (p->p_tracep != v) {
508 /*
509 * if trace file already in use, relinquish
510 */
511 ktrderef(p);
512 if (ops & KTRFLAG_FD)
513 p->p_traceflag = KTRFAC_FD;
514 p->p_tracep = v;
515 ktradref(p);
516 }
517 p->p_traceflag |= facs;
518 if (curp->p_ucred->cr_uid == 0)
519 p->p_traceflag |= KTRFAC_ROOT;
520 } else {
521 /* KTROP_CLEAR */
522 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
523 /* no more tracing */
524 ktrderef(p);
525 }
526 }
527
528 /*
529 * Emit an emulation record, every time there is a ktrace
530 * change/attach request.
531 */
532 if (KTRPOINT(p, KTR_EMUL))
533 ktremul(p->p_tracep, p, p->p_emul->e_name);
534
535 return (1);
536 }
537
538 int
539 ktrsetchildren(curp, top, ops, facs, v)
540 struct proc *curp, *top;
541 int ops, facs;
542 void *v;
543 {
544 struct proc *p;
545 int ret = 0;
546
547 p = top;
548 for (;;) {
549 ret |= ktrops(curp, p, ops, facs, v);
550 /*
551 * If this process has children, descend to them next,
552 * otherwise do any siblings, and if done with this level,
553 * follow back up the tree (but not past top).
554 */
555 if (p->p_children.lh_first)
556 p = p->p_children.lh_first;
557 else for (;;) {
558 if (p == top)
559 return (ret);
560 if (p->p_sibling.le_next) {
561 p = p->p_sibling.le_next;
562 break;
563 }
564 p = p->p_pptr;
565 }
566 }
567 /*NOTREACHED*/
568 }
569
570 void
571 ktrwrite(p, v, kth)
572 struct proc *p;
573 void *v;
574 struct ktr_header *kth;
575 {
576 struct uio auio;
577 struct iovec aiov[2];
578 int error;
579
580 if (v == NULL)
581 return;
582 auio.uio_iov = &aiov[0];
583 auio.uio_offset = 0;
584 auio.uio_segflg = UIO_SYSSPACE;
585 auio.uio_rw = UIO_WRITE;
586 aiov[0].iov_base = (caddr_t)kth;
587 aiov[0].iov_len = sizeof(struct ktr_header);
588 auio.uio_resid = sizeof(struct ktr_header);
589 auio.uio_iovcnt = 1;
590 auio.uio_procp = (struct proc *)0;
591 if (kth->ktr_len > 0) {
592 auio.uio_iovcnt++;
593 aiov[1].iov_base = kth->ktr_buf;
594 aiov[1].iov_len = kth->ktr_len;
595 auio.uio_resid += kth->ktr_len;
596 }
597 if (p->p_traceflag & KTRFAC_FD) {
598 struct file *fp = v;
599
600 error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
601 fp->f_cred, FOF_UPDATE_OFFSET);
602 }
603 else {
604 struct vnode *vp = v;
605
606 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
607 error = VOP_WRITE(vp, &auio, IO_UNIT|IO_APPEND, p->p_ucred);
608 VOP_UNLOCK(vp, 0);
609 }
610 if (!error)
611 return;
612 /*
613 * If error encountered, give up tracing on this vnode.
614 */
615 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
616 error);
617 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
618 if (p->p_tracep == v)
619 ktrderef(p);
620 }
621 }
622
623 /*
624 * Return true if caller has permission to set the ktracing state
625 * of target. Essentially, the target can't possess any
626 * more permissions than the caller. KTRFAC_ROOT signifies that
627 * root previously set the tracing status on the target process, and
628 * so, only root may further change it.
629 *
630 * TODO: check groups. use caller effective gid.
631 */
632 int
633 ktrcanset(callp, targetp)
634 struct proc *callp, *targetp;
635 {
636 struct pcred *caller = callp->p_cred;
637 struct pcred *target = targetp->p_cred;
638
639 if ((caller->pc_ucred->cr_uid == target->p_ruid &&
640 target->p_ruid == target->p_svuid &&
641 caller->p_rgid == target->p_rgid && /* XXX */
642 target->p_rgid == target->p_svgid &&
643 (targetp->p_traceflag & KTRFAC_ROOT) == 0) ||
644 caller->pc_ucred->cr_uid == 0)
645 return (1);
646
647 return (0);
648 }
649
650 #endif
651