kern_ktrace.c revision 1.53.2.6 1 /* $NetBSD: kern_ktrace.c,v 1.53.2.6 2002/07/12 01:40:16 nathanw 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 <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.53.2.6 2002/07/12 01:40:16 nathanw Exp $");
40
41 #include "opt_ktrace.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 #include <sys/file.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/filedesc.h>
53 #include <sys/ioctl.h>
54
55 #include <sys/mount.h>
56 #include <sys/sa.h>
57 #include <sys/syscallargs.h>
58
59 #ifdef KTRACE
60
61 int ktrace_common(struct proc *, int, int, int, struct file *);
62 void ktrinitheader(struct ktr_header *, struct proc *, int);
63 int ktrops(struct proc *, struct proc *, int, int, struct file *);
64 int ktrsetchildren(struct proc *, struct proc *, int, int,
65 struct file *);
66 int ktrwrite(struct proc *, struct ktr_header *);
67 int ktrcanset(struct proc *, struct proc *);
68 int ktrsamefile(struct file *, struct file *);
69
70 /*
71 * "deep" compare of two files for the purposes of clearing a trace.
72 * Returns true if they're the same open file, or if they point at the
73 * same underlying vnode/socket.
74 */
75
76 int
77 ktrsamefile(struct file *f1, struct file *f2)
78 {
79 return ((f1 == f2) ||
80 ((f1 != NULL) && (f2 != NULL) &&
81 (f1->f_type == f2->f_type) &&
82 (f1->f_data == f2->f_data)));
83 }
84
85 void
86 ktrderef(struct proc *p)
87 {
88 struct file *fp = p->p_tracep;
89 p->p_traceflag = 0;
90 if (fp == NULL)
91 return;
92 FILE_USE(fp);
93 closef(fp, NULL);
94
95 p->p_tracep = NULL;
96 }
97
98 void
99 ktradref(struct proc *p)
100 {
101 struct file *fp = p->p_tracep;
102
103 fp->f_count++;
104 }
105
106 void
107 ktrinitheader(struct ktr_header *kth, struct proc *p, int type)
108 {
109
110 memset(kth, 0, sizeof(*kth));
111 kth->ktr_type = type;
112 microtime(&kth->ktr_time);
113 kth->ktr_pid = p->p_pid;
114 memcpy(kth->ktr_comm, p->p_comm, MAXCOMLEN);
115 }
116
117 void
118 ktrsyscall(struct proc *p, register_t code, register_t args[])
119 {
120 struct ktr_header kth;
121 struct ktr_syscall *ktp;
122 register_t *argp;
123 int argsize;
124 size_t len;
125 int i;
126
127 argsize = p->p_emul->e_sysent[code].sy_narg * sizeof (register_t);
128 len = sizeof(struct ktr_syscall) + argsize;
129
130 p->p_traceflag |= KTRFAC_ACTIVE;
131 ktrinitheader(&kth, p, KTR_SYSCALL);
132 ktp = malloc(len, M_TEMP, M_WAITOK);
133 ktp->ktr_code = code;
134 ktp->ktr_argsize = argsize;
135 argp = (register_t *)((char *)ktp + sizeof(struct ktr_syscall));
136 for (i = 0; i < (argsize / sizeof(*argp)); i++)
137 *argp++ = args[i];
138 kth.ktr_buf = (caddr_t)ktp;
139 kth.ktr_len = len;
140 (void) ktrwrite(p, &kth);
141 free(ktp, M_TEMP);
142 p->p_traceflag &= ~KTRFAC_ACTIVE;
143 }
144
145 void
146 ktrsysret(struct proc *p, register_t code, int error, register_t retval)
147 {
148 struct ktr_header kth;
149 struct ktr_sysret ktp;
150
151 p->p_traceflag |= KTRFAC_ACTIVE;
152 ktrinitheader(&kth, p, KTR_SYSRET);
153 ktp.ktr_code = code;
154 ktp.ktr_eosys = 0; /* XXX unused */
155 ktp.ktr_error = error;
156 ktp.ktr_retval = retval; /* what about val2 ? */
157
158 kth.ktr_buf = (caddr_t)&ktp;
159 kth.ktr_len = sizeof(struct ktr_sysret);
160
161 (void) ktrwrite(p, &kth);
162 p->p_traceflag &= ~KTRFAC_ACTIVE;
163 }
164
165 void
166 ktrnamei(struct proc *p, char *path)
167 {
168 struct ktr_header kth;
169
170 p->p_traceflag |= KTRFAC_ACTIVE;
171 ktrinitheader(&kth, p, KTR_NAMEI);
172 kth.ktr_len = strlen(path);
173 kth.ktr_buf = path;
174
175 (void) ktrwrite(p, &kth);
176 p->p_traceflag &= ~KTRFAC_ACTIVE;
177 }
178
179 void
180 ktremul(struct proc *p)
181 {
182 struct ktr_header kth;
183 const char *emul = p->p_emul->e_name;
184
185 p->p_traceflag |= KTRFAC_ACTIVE;
186 ktrinitheader(&kth, p, KTR_EMUL);
187 kth.ktr_len = strlen(emul);
188 kth.ktr_buf = (caddr_t)emul;
189
190 (void) ktrwrite(p, &kth);
191 p->p_traceflag &= ~KTRFAC_ACTIVE;
192 }
193
194 void
195 ktrgenio(struct proc *p, int fd, enum uio_rw rw, struct iovec *iov,
196 int len, int error)
197 {
198 struct ktr_header kth;
199 struct ktr_genio *ktp;
200 caddr_t cp;
201 int resid = len, cnt;
202 int buflen;
203
204 if (error)
205 return;
206
207 p->p_traceflag |= KTRFAC_ACTIVE;
208
209 buflen = min(PAGE_SIZE, len + sizeof(struct ktr_genio));
210
211 ktrinitheader(&kth, p, KTR_GENIO);
212 ktp = malloc(buflen, M_TEMP, M_WAITOK);
213 ktp->ktr_fd = fd;
214 ktp->ktr_rw = rw;
215
216 kth.ktr_buf = (caddr_t)ktp;
217
218 cp = (caddr_t)((char *)ktp + sizeof(struct ktr_genio));
219 buflen -= sizeof(struct ktr_genio);
220
221 while (resid > 0) {
222 #if 0 /* XXX NJWLWP */
223 KDASSERT(p->p_cpu != NULL);
224 KDASSERT(p->p_cpu == curcpu());
225 #endif
226 /* XXX NJWLWP */
227 if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
228 preempt(NULL);
229
230 cnt = min(iov->iov_len, buflen);
231 if (cnt > resid)
232 cnt = resid;
233 if (copyin(iov->iov_base, cp, cnt))
234 break;
235
236 kth.ktr_len = cnt + sizeof(struct ktr_genio);
237
238 if (__predict_false(ktrwrite(p, &kth) != 0))
239 break;
240
241 iov->iov_base = (caddr_t)iov->iov_base + cnt;
242 iov->iov_len -= cnt;
243
244 if (iov->iov_len == 0)
245 iov++;
246
247 resid -= cnt;
248 }
249
250 free(ktp, M_TEMP);
251 p->p_traceflag &= ~KTRFAC_ACTIVE;
252 }
253
254 void
255 ktrpsig(struct proc *p, int sig, sig_t action, sigset_t *mask, int code)
256 {
257 struct ktr_header kth;
258 struct ktr_psig kp;
259
260 p->p_traceflag |= KTRFAC_ACTIVE;
261 ktrinitheader(&kth, p, 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 (void) ktrwrite(p, &kth);
270 p->p_traceflag &= ~KTRFAC_ACTIVE;
271 }
272
273 void
274 ktrcsw(struct proc *p, int out, int user)
275 {
276 struct ktr_header kth;
277 struct ktr_csw kc;
278
279 p->p_traceflag |= KTRFAC_ACTIVE;
280 ktrinitheader(&kth, p, KTR_CSW);
281 kc.out = out;
282 kc.user = user;
283 kth.ktr_buf = (caddr_t)&kc;
284 kth.ktr_len = sizeof(struct ktr_csw);
285
286 (void) ktrwrite(p, &kth);
287 p->p_traceflag &= ~KTRFAC_ACTIVE;
288 }
289
290 void
291 ktruser(p, id, addr, len, ustr)
292 struct proc *p;
293 const char *id;
294 void *addr;
295 size_t len;
296 int ustr;
297 {
298 struct ktr_header kth;
299 struct ktr_user *ktp;
300 caddr_t user_dta;
301
302 p->p_traceflag |= KTRFAC_ACTIVE;
303 ktrinitheader(&kth, p, KTR_USER);
304 ktp = malloc(sizeof(struct ktr_user) + len, M_TEMP, M_WAITOK);
305 if (ustr) {
306 if (copyinstr(id, ktp->ktr_id, KTR_USER_MAXIDLEN, NULL) != 0)
307 ktp->ktr_id[0] = '\0';
308 } else
309 strncpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN);
310 ktp->ktr_id[KTR_USER_MAXIDLEN-1] = '\0';
311
312 user_dta = (caddr_t) ((char *)ktp + sizeof(struct ktr_user));
313 if (copyin(addr, (void *) user_dta, len) != 0)
314 len = 0;
315
316 kth.ktr_buf = (void *)ktp;
317 kth.ktr_len = sizeof(struct ktr_user) + len;
318 (void) ktrwrite(p, &kth);
319
320 free(ktp, M_TEMP);
321 p->p_traceflag &= ~KTRFAC_ACTIVE;
322
323 }
324
325 /* Interface and common routines */
326
327 int
328 ktrace_common(struct proc *curp, int ops, int facs, int pid, struct file *fp)
329 {
330 int ret = 0;
331 int error = 0;
332 int one = 1;
333 int descend;
334 struct proc *p;
335 struct pgrp *pg;
336
337 curp->p_traceflag |= KTRFAC_ACTIVE;
338 descend = ops & KTRFLAG_DESCEND;
339 facs = facs & ~((unsigned) KTRFAC_ROOT);
340
341 /*
342 * Clear all uses of the tracefile
343 */
344 if (KTROP(ops) == KTROP_CLEARFILE) {
345 proclist_lock_read();
346 for (p = LIST_FIRST(&allproc); p != NULL;
347 p = LIST_NEXT(p, p_list)) {
348 if (ktrsamefile(p->p_tracep, fp)) {
349 if (ktrcanset(curp, p))
350 ktrderef(p);
351 else
352 error = EPERM;
353 }
354 }
355 proclist_unlock_read();
356 goto done;
357 }
358
359 /*
360 * Mark fp non-blocking, to avoid problems from possible deadlocks.
361 */
362
363 if (fp != NULL) {
364 fp->f_flag |= FNONBLOCK;
365 (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&one, curp);
366 }
367
368 /*
369 * need something to (un)trace (XXX - why is this here?)
370 */
371 if (!facs) {
372 error = EINVAL;
373 goto done;
374 }
375 /*
376 * do it
377 */
378 if (pid < 0) {
379 /*
380 * by process group
381 */
382 pg = pgfind(-pid);
383 if (pg == NULL) {
384 error = ESRCH;
385 goto done;
386 }
387 for (p = LIST_FIRST(&pg->pg_members); p != NULL;
388 p = LIST_NEXT(p, p_pglist)) {
389 if (descend)
390 ret |= ktrsetchildren(curp, p, ops, facs, fp);
391 else
392 ret |= ktrops(curp, p, ops, facs, fp);
393 }
394
395 } else {
396 /*
397 * by pid
398 */
399 p = pfind(pid);
400 if (p == NULL) {
401 error = ESRCH;
402 goto done;
403 }
404 if (descend)
405 ret |= ktrsetchildren(curp, p, ops, facs, fp);
406 else
407 ret |= ktrops(curp, p, ops, facs, fp);
408 }
409 if (!ret)
410 error = EPERM;
411 done:
412 curp->p_traceflag &= ~KTRFAC_ACTIVE;
413 return (error);
414 }
415
416 /*
417 * ktrace system call
418 */
419 /* ARGSUSED */
420 int
421 sys_fktrace(struct lwp *l, void *v, register_t *retval)
422 {
423 struct sys_fktrace_args /* {
424 syscallarg(int) fd;
425 syscallarg(int) ops;
426 syscallarg(int) facs;
427 syscallarg(int) pid;
428 } */ *uap = v;
429 struct proc *curp = l->l_proc;
430 struct file *fp = NULL;
431 struct filedesc *fdp = curp->p_fd;
432
433 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
434 return (EBADF);
435
436 if ((fp->f_flag & FWRITE) == 0)
437 return (EBADF);
438
439 return ktrace_common(curp, SCARG(uap, ops),
440 SCARG(uap, facs), SCARG(uap, pid), fp);
441 }
442
443 /*
444 * ktrace system call
445 */
446 /* ARGSUSED */
447 int
448 sys_ktrace(struct lwp *l, void *v, register_t *retval)
449 {
450 struct sys_ktrace_args /* {
451 syscallarg(const char *) fname;
452 syscallarg(int) ops;
453 syscallarg(int) facs;
454 syscallarg(int) pid;
455 } */ *uap = v;
456 struct proc *curp = l->l_proc;
457 struct vnode *vp = NULL;
458 struct file *fp = NULL;
459 int fd;
460 int ops = SCARG(uap, ops);
461 int error = 0;
462 struct nameidata nd;
463
464 ops = KTROP(ops) | (ops & KTRFLAG_DESCEND);
465
466 curp->p_traceflag |= KTRFAC_ACTIVE;
467 if (ops != KTROP_CLEAR) {
468 /*
469 * an operation which requires a file argument.
470 */
471 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
472 curp);
473 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
474 curp->p_traceflag &= ~KTRFAC_ACTIVE;
475 return (error);
476 }
477 vp = nd.ni_vp;
478 VOP_UNLOCK(vp, 0);
479 if (vp->v_type != VREG) {
480 (void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
481 curp->p_traceflag &= ~KTRFAC_ACTIVE;
482 return (EACCES);
483 }
484 /*
485 * XXX This uses up a file descriptor slot in the
486 * tracing process for the duration of this syscall.
487 * This is not expected to be a problem. If
488 * falloc(NULL, ...) DTRT we could skip that part, but
489 * that would require changing its interface to allow
490 * the caller to pass in a ucred..
491 *
492 * This will FILE_USE the fp it returns, if any.
493 * Keep it in use until we return.
494 */
495 if ((error = falloc(curp, &fp, &fd)) != 0)
496 goto done;
497
498 fp->f_flag = FWRITE|FAPPEND;
499 fp->f_type = DTYPE_VNODE;
500 fp->f_ops = &vnops;
501 fp->f_data = (caddr_t)vp;
502 FILE_SET_MATURE(fp);
503 vp = NULL;
504 }
505 error = ktrace_common(curp, SCARG(uap, ops), SCARG(uap, facs),
506 SCARG(uap, pid), fp);
507 done:
508 if (vp != NULL)
509 (void) vn_close(vp, FWRITE, curp->p_ucred, curp);
510 if (fp != NULL) {
511 FILE_UNUSE(fp, curp); /* release file */
512 fdrelease(curp, fd); /* release fd table slot */
513 }
514 return (error);
515 }
516
517 int
518 ktrops(struct proc *curp, struct proc *p, int ops, int facs, struct file *fp)
519 {
520
521 if (!ktrcanset(curp, p))
522 return (0);
523 if (KTROP(ops) == KTROP_SET) {
524 if (p->p_tracep != fp) {
525 /*
526 * if trace file already in use, relinquish
527 */
528 ktrderef(p);
529 p->p_tracep = fp;
530 ktradref(p);
531 }
532 p->p_traceflag |= facs;
533 if (curp->p_ucred->cr_uid == 0)
534 p->p_traceflag |= KTRFAC_ROOT;
535 } else {
536 /* KTROP_CLEAR */
537 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
538 /* no more tracing */
539 ktrderef(p);
540 }
541 }
542
543 /*
544 * Emit an emulation record, every time there is a ktrace
545 * change/attach request.
546 */
547 if (KTRPOINT(p, KTR_EMUL))
548 ktremul(p);
549 #ifdef __HAVE_SYSCALL_INTERN
550 (*p->p_emul->e_syscall_intern)(p);
551 #endif
552
553 return (1);
554 }
555
556 int
557 ktrsetchildren(struct proc *curp, struct proc *top, int ops, int facs,
558 struct file *fp)
559 {
560 struct proc *p;
561 int ret = 0;
562
563 p = top;
564 for (;;) {
565 ret |= ktrops(curp, p, ops, facs, fp);
566 /*
567 * If this process has children, descend to them next,
568 * otherwise do any siblings, and if done with this level,
569 * follow back up the tree (but not past top).
570 */
571 if (LIST_FIRST(&p->p_children) != NULL)
572 p = LIST_FIRST(&p->p_children);
573 else for (;;) {
574 if (p == top)
575 return (ret);
576 if (LIST_NEXT(p, p_sibling) != NULL) {
577 p = LIST_NEXT(p, p_sibling);
578 break;
579 }
580 p = p->p_pptr;
581 }
582 }
583 /*NOTREACHED*/
584 }
585
586 int
587 ktrwrite(struct proc *p, struct ktr_header *kth)
588 {
589 struct uio auio;
590 struct iovec aiov[2];
591 int error, tries;
592 struct file *fp = p->p_tracep;
593
594 if (fp == NULL)
595 return 0;
596
597 auio.uio_iov = &aiov[0];
598 auio.uio_offset = 0;
599 auio.uio_segflg = UIO_SYSSPACE;
600 auio.uio_rw = UIO_WRITE;
601 aiov[0].iov_base = (caddr_t)kth;
602 aiov[0].iov_len = sizeof(struct ktr_header);
603 auio.uio_resid = sizeof(struct ktr_header);
604 auio.uio_iovcnt = 1;
605 auio.uio_procp = (struct proc *)0;
606 if (kth->ktr_len > 0) {
607 auio.uio_iovcnt++;
608 aiov[1].iov_base = kth->ktr_buf;
609 aiov[1].iov_len = kth->ktr_len;
610 auio.uio_resid += kth->ktr_len;
611 }
612
613 FILE_USE(fp);
614
615 tries = 0;
616 do {
617 error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
618 fp->f_cred, FOF_UPDATE_OFFSET);
619 tries++;
620 if (error == EWOULDBLOCK)
621 yield();
622 } while ((error == EWOULDBLOCK) && (tries < 3));
623 FILE_UNUSE(fp, NULL);
624
625 if (__predict_true(error == 0))
626 return (0);
627 /*
628 * If error encountered, give up tracing on this vnode. Don't report
629 * EPIPE as this can easily happen with fktrace()/ktruss.
630 */
631 if (error != EPIPE)
632 log(LOG_NOTICE,
633 "ktrace write failed, errno %d, tracing stopped\n",
634 error);
635 proclist_lock_read();
636 for (p = LIST_FIRST(&allproc); p != NULL; p = LIST_NEXT(p, p_list)) {
637 if (ktrsamefile(p->p_tracep, fp))
638 ktrderef(p);
639 }
640 proclist_unlock_read();
641
642 return (error);
643 }
644
645 /*
646 * Return true if caller has permission to set the ktracing state
647 * of target. Essentially, the target can't possess any
648 * more permissions than the caller. KTRFAC_ROOT signifies that
649 * root previously set the tracing status on the target process, and
650 * so, only root may further change it.
651 *
652 * TODO: check groups. use caller effective gid.
653 */
654 int
655 ktrcanset(struct proc *callp, struct proc *targetp)
656 {
657 struct pcred *caller = callp->p_cred;
658 struct pcred *target = targetp->p_cred;
659
660 if ((caller->pc_ucred->cr_uid == target->p_ruid &&
661 target->p_ruid == target->p_svuid &&
662 caller->p_rgid == target->p_rgid && /* XXX */
663 target->p_rgid == target->p_svgid &&
664 (targetp->p_traceflag & KTRFAC_ROOT) == 0) ||
665 caller->pc_ucred->cr_uid == 0)
666 return (1);
667
668 return (0);
669 }
670 #endif /* KTRACE */
671
672 /*
673 * Put user defined entry to ktrace records.
674 */
675 int
676 sys_utrace(l, v, retval)
677 struct lwp *l;
678 void *v;
679 register_t *retval;
680 {
681 #ifdef KTRACE
682 struct sys_utrace_args /* {
683 syscallarg(const char *) label;
684 syscallarg(void *) addr;
685 syscallarg(size_t) len;
686 } */ *uap = v;
687 struct proc *p = l->l_proc;
688 if (!KTRPOINT(p, KTR_USER))
689 return (0);
690
691 if (SCARG(uap, len) > KTR_USER_MAXLEN)
692 return (EINVAL);
693
694 ktruser(p, SCARG(uap, label), SCARG(uap, addr), SCARG(uap, len), 1);
695
696 return (0);
697 #else /* !KTRACE */
698 return ENOSYS;
699 #endif /* KTRACE */
700 }
701