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