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