kern_ktrace.c revision 1.53.2.3 1 /* $NetBSD: kern_ktrace.c,v 1.53.2.3 2001/11/14 19:16:35 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.3 2001/11/14 19:16:35 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/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, size_t argsize, register_t args[])
119 {
120 struct ktr_header kth;
121 struct ktr_syscall *ktp;
122 register_t *argp;
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 #if 0 /* XXX NJWLWP */
219 KDASSERT(p->p_cpu != NULL);
220 KDASSERT(p->p_cpu == curcpu());
221 #endif
222 /* XXX NJWLWP */
223 if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
224 preempt(NULL);
225
226 cnt = min(iov->iov_len, buflen);
227 if (cnt > resid)
228 cnt = resid;
229 if (copyin(iov->iov_base, cp, cnt))
230 break;
231
232 kth.ktr_len = cnt + sizeof(struct ktr_genio);
233
234 if (__predict_false(ktrwrite(p, &kth) != 0))
235 break;
236
237 iov->iov_base = (caddr_t)iov->iov_base + cnt;
238 iov->iov_len -= cnt;
239
240 if (iov->iov_len == 0)
241 iov++;
242
243 resid -= cnt;
244 }
245
246 free(ktp, M_TEMP);
247 p->p_traceflag &= ~KTRFAC_ACTIVE;
248 }
249
250 void
251 ktrpsig(struct proc *p, int sig, sig_t action, sigset_t *mask, int code)
252 {
253 struct ktr_header kth;
254 struct ktr_psig kp;
255
256 p->p_traceflag |= KTRFAC_ACTIVE;
257 ktrinitheader(&kth, p, KTR_PSIG);
258 kp.signo = (char)sig;
259 kp.action = action;
260 kp.mask = *mask;
261 kp.code = code;
262 kth.ktr_buf = (caddr_t)&kp;
263 kth.ktr_len = sizeof(struct ktr_psig);
264
265 (void) ktrwrite(p, &kth);
266 p->p_traceflag &= ~KTRFAC_ACTIVE;
267 }
268
269 void
270 ktrcsw(struct proc *p, int out, int user)
271 {
272 struct ktr_header kth;
273 struct ktr_csw kc;
274
275 p->p_traceflag |= KTRFAC_ACTIVE;
276 ktrinitheader(&kth, p, KTR_CSW);
277 kc.out = out;
278 kc.user = user;
279 kth.ktr_buf = (caddr_t)&kc;
280 kth.ktr_len = sizeof(struct ktr_csw);
281
282 (void) ktrwrite(p, &kth);
283 p->p_traceflag &= ~KTRFAC_ACTIVE;
284 }
285
286 void
287 ktruser(p, id, addr, len, ustr)
288 struct proc *p;
289 const char *id;
290 void *addr;
291 size_t len;
292 int ustr;
293 {
294 struct ktr_header kth;
295 struct ktr_user *ktp;
296 caddr_t user_dta;
297
298 p->p_traceflag |= KTRFAC_ACTIVE;
299 ktrinitheader(&kth, p, KTR_USER);
300 ktp = malloc(sizeof(struct ktr_user) + len, M_TEMP, M_WAITOK);
301 if (ustr) {
302 if (copyinstr(id, ktp->ktr_id, KTR_USER_MAXIDLEN, NULL) != 0)
303 ktp->ktr_id[0] = '\0';
304 } else
305 strncpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN);
306 ktp->ktr_id[KTR_USER_MAXIDLEN-1] = '\0';
307
308 user_dta = (caddr_t) ((char *)ktp + sizeof(struct ktr_user));
309 if (copyin(addr, (void *) user_dta, len) != 0)
310 len = 0;
311
312 kth.ktr_buf = (void *)ktp;
313 kth.ktr_len = sizeof(struct ktr_user) + len;
314 (void) ktrwrite(p, &kth);
315
316 free(ktp, M_TEMP);
317 p->p_traceflag &= ~KTRFAC_ACTIVE;
318
319 }
320
321 /* Interface and common routines */
322
323 int
324 ktrace_common(struct proc *curp, int ops, int facs, int pid, struct file *fp)
325 {
326 int ret = 0;
327 int error = 0;
328 int one = 1;
329 int descend;
330 struct proc *p;
331 struct pgrp *pg;
332
333 curp->p_traceflag |= KTRFAC_ACTIVE;
334 descend = ops & KTRFLAG_DESCEND;
335 facs = facs & ~((unsigned) KTRFAC_ROOT);
336
337 /*
338 * Clear all uses of the tracefile
339 */
340 if (KTROP(ops) == KTROP_CLEARFILE) {
341 proclist_lock_read();
342 for (p = LIST_FIRST(&allproc); p != NULL;
343 p = LIST_NEXT(p, p_list)) {
344 if (ktrsamefile(p->p_tracep, fp)) {
345 if (ktrcanset(curp, p))
346 ktrderef(p);
347 else
348 error = EPERM;
349 }
350 }
351 proclist_unlock_read();
352 goto done;
353 }
354
355 /*
356 * Mark fp non-blocking, to avoid problems from possible deadlocks.
357 */
358
359 if (fp != NULL) {
360 fp->f_flag |= FNONBLOCK;
361 (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&one, curp);
362 }
363
364 /*
365 * need something to (un)trace (XXX - why is this here?)
366 */
367 if (!facs) {
368 error = EINVAL;
369 goto done;
370 }
371 /*
372 * do it
373 */
374 if (pid < 0) {
375 /*
376 * by process group
377 */
378 pg = pgfind(-pid);
379 if (pg == NULL) {
380 error = ESRCH;
381 goto done;
382 }
383 for (p = LIST_FIRST(&pg->pg_members); p != NULL;
384 p = LIST_NEXT(p, p_pglist)) {
385 if (descend)
386 ret |= ktrsetchildren(curp, p, ops, facs, fp);
387 else
388 ret |= ktrops(curp, p, ops, facs, fp);
389 }
390
391 } else {
392 /*
393 * by pid
394 */
395 p = pfind(pid);
396 if (p == NULL) {
397 error = ESRCH;
398 goto done;
399 }
400 if (descend)
401 ret |= ktrsetchildren(curp, p, ops, facs, fp);
402 else
403 ret |= ktrops(curp, p, ops, facs, fp);
404 }
405 if (!ret)
406 error = EPERM;
407 done:
408 curp->p_traceflag &= ~KTRFAC_ACTIVE;
409 return (error);
410 }
411
412 /*
413 * ktrace system call
414 */
415 /* ARGSUSED */
416 int
417 sys_fktrace(struct lwp *l, void *v, register_t *retval)
418 {
419 struct sys_fktrace_args /* {
420 syscallarg(int) fd;
421 syscallarg(int) ops;
422 syscallarg(int) facs;
423 syscallarg(int) pid;
424 } */ *uap = v;
425 struct proc *curp = l->l_proc;
426 struct file *fp = NULL;
427 struct filedesc *fdp = curp->p_fd;
428
429 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
430 return (EBADF);
431
432 if ((fp->f_flag & FWRITE) == 0)
433 return (EBADF);
434
435 return ktrace_common(curp, SCARG(uap, ops),
436 SCARG(uap, facs), SCARG(uap, pid), fp);
437 }
438
439 /*
440 * ktrace system call
441 */
442 /* ARGSUSED */
443 int
444 sys_ktrace(struct lwp *l, void *v, register_t *retval)
445 {
446 struct sys_ktrace_args /* {
447 syscallarg(const char *) fname;
448 syscallarg(int) ops;
449 syscallarg(int) facs;
450 syscallarg(int) pid;
451 } */ *uap = v;
452 struct proc *curp = l->l_proc;
453 struct vnode *vp = NULL;
454 struct file *fp = NULL;
455 int fd;
456 int ops = SCARG(uap, ops);
457 int error = 0;
458 struct nameidata nd;
459
460 ops = KTROP(ops) | (ops & KTRFLAG_DESCEND);
461
462 curp->p_traceflag |= KTRFAC_ACTIVE;
463 if (ops != KTROP_CLEAR) {
464 /*
465 * an operation which requires a file argument.
466 */
467 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
468 curp);
469 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
470 curp->p_traceflag &= ~KTRFAC_ACTIVE;
471 return (error);
472 }
473 vp = nd.ni_vp;
474 VOP_UNLOCK(vp, 0);
475 if (vp->v_type != VREG) {
476 (void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
477 curp->p_traceflag &= ~KTRFAC_ACTIVE;
478 return (EACCES);
479 }
480 /*
481 * XXX This uses up a file descriptor slot in the
482 * tracing process for the duration of this syscall.
483 * This is not expected to be a problem. If
484 * falloc(NULL, ...) DTRT we could skip that part, but
485 * that would require changing its interface to allow
486 * the caller to pass in a ucred..
487 *
488 * This will FILE_USE the fp it returns, if any.
489 * Keep it in use until we return.
490 */
491 if ((error = falloc(curp, &fp, &fd)) != 0)
492 goto done;
493
494 fp->f_flag = FWRITE|FAPPEND;
495 fp->f_type = DTYPE_VNODE;
496 fp->f_ops = &vnops;
497 fp->f_data = (caddr_t)vp;
498 FILE_SET_MATURE(fp);
499 vp = NULL;
500 }
501 error = ktrace_common(curp, SCARG(uap, ops), SCARG(uap, facs),
502 SCARG(uap, pid), fp);
503 done:
504 if (vp != NULL)
505 (void) vn_close(vp, FWRITE, curp->p_ucred, curp);
506 if (fp != NULL) {
507 FILE_UNUSE(fp, curp); /* release file */
508 fdrelease(curp, fd); /* release fd table slot */
509 }
510 return (error);
511 }
512
513 int
514 ktrops(struct proc *curp, struct proc *p, int ops, int facs, struct file *fp)
515 {
516
517 if (!ktrcanset(curp, p))
518 return (0);
519 if (KTROP(ops) == KTROP_SET) {
520 if (p->p_tracep != fp) {
521 /*
522 * if trace file already in use, relinquish
523 */
524 ktrderef(p);
525 p->p_tracep = fp;
526 ktradref(p);
527 }
528 p->p_traceflag |= facs;
529 if (curp->p_ucred->cr_uid == 0)
530 p->p_traceflag |= KTRFAC_ROOT;
531 } else {
532 /* KTROP_CLEAR */
533 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
534 /* no more tracing */
535 ktrderef(p);
536 }
537 }
538
539 /*
540 * Emit an emulation record, every time there is a ktrace
541 * change/attach request.
542 */
543 if (KTRPOINT(p, KTR_EMUL))
544 ktremul(p);
545 #ifdef __HAVE_SYSCALL_INTERN
546 (*p->p_emul->e_syscall_intern)(p);
547 #endif
548
549 return (1);
550 }
551
552 int
553 ktrsetchildren(struct proc *curp, struct proc *top, int ops, int facs,
554 struct file *fp)
555 {
556 struct proc *p;
557 int ret = 0;
558
559 p = top;
560 for (;;) {
561 ret |= ktrops(curp, p, ops, facs, fp);
562 /*
563 * If this process has children, descend to them next,
564 * otherwise do any siblings, and if done with this level,
565 * follow back up the tree (but not past top).
566 */
567 if (LIST_FIRST(&p->p_children) != NULL)
568 p = LIST_FIRST(&p->p_children);
569 else for (;;) {
570 if (p == top)
571 return (ret);
572 if (LIST_NEXT(p, p_sibling) != NULL) {
573 p = LIST_NEXT(p, p_sibling);
574 break;
575 }
576 p = p->p_pptr;
577 }
578 }
579 /*NOTREACHED*/
580 }
581
582 int
583 ktrwrite(struct proc *p, struct ktr_header *kth)
584 {
585 struct uio auio;
586 struct iovec aiov[2];
587 int error, tries;
588 struct file *fp = p->p_tracep;
589
590 if (fp == NULL)
591 return 0;
592
593 auio.uio_iov = &aiov[0];
594 auio.uio_offset = 0;
595 auio.uio_segflg = UIO_SYSSPACE;
596 auio.uio_rw = UIO_WRITE;
597 aiov[0].iov_base = (caddr_t)kth;
598 aiov[0].iov_len = sizeof(struct ktr_header);
599 auio.uio_resid = sizeof(struct ktr_header);
600 auio.uio_iovcnt = 1;
601 auio.uio_procp = (struct proc *)0;
602 if (kth->ktr_len > 0) {
603 auio.uio_iovcnt++;
604 aiov[1].iov_base = kth->ktr_buf;
605 aiov[1].iov_len = kth->ktr_len;
606 auio.uio_resid += kth->ktr_len;
607 }
608
609 FILE_USE(fp);
610
611 tries = 0;
612 do {
613 error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
614 fp->f_cred, FOF_UPDATE_OFFSET);
615 tries++;
616 if (error == EWOULDBLOCK)
617 yield();
618 } while ((error == EWOULDBLOCK) && (tries < 3));
619 FILE_UNUSE(fp, NULL);
620
621 if (__predict_true(error == 0))
622 return (0);
623 /*
624 * If error encountered, give up tracing on this vnode. Don't report
625 * EPIPE as this can easily happen with fktrace()/ktruss.
626 */
627 if (error != EPIPE)
628 log(LOG_NOTICE,
629 "ktrace write failed, errno %d, tracing stopped\n",
630 error);
631 proclist_lock_read();
632 for (p = LIST_FIRST(&allproc); p != NULL; p = LIST_NEXT(p, p_list)) {
633 if (ktrsamefile(p->p_tracep, fp))
634 ktrderef(p);
635 }
636 proclist_unlock_read();
637
638 return (error);
639 }
640
641 /*
642 * Return true if caller has permission to set the ktracing state
643 * of target. Essentially, the target can't possess any
644 * more permissions than the caller. KTRFAC_ROOT signifies that
645 * root previously set the tracing status on the target process, and
646 * so, only root may further change it.
647 *
648 * TODO: check groups. use caller effective gid.
649 */
650 int
651 ktrcanset(struct proc *callp, struct proc *targetp)
652 {
653 struct pcred *caller = callp->p_cred;
654 struct pcred *target = targetp->p_cred;
655
656 if ((caller->pc_ucred->cr_uid == target->p_ruid &&
657 target->p_ruid == target->p_svuid &&
658 caller->p_rgid == target->p_rgid && /* XXX */
659 target->p_rgid == target->p_svgid &&
660 (targetp->p_traceflag & KTRFAC_ROOT) == 0) ||
661 caller->pc_ucred->cr_uid == 0)
662 return (1);
663
664 return (0);
665 }
666 #endif /* KTRACE */
667
668 /*
669 * Put user defined entry to ktrace records.
670 */
671 int
672 sys_utrace(l, v, retval)
673 struct lwp *l;
674 void *v;
675 register_t *retval;
676 {
677 #ifdef KTRACE
678 struct sys_utrace_args /* {
679 syscallarg(const char *) label;
680 syscallarg(void *) addr;
681 syscallarg(size_t) len;
682 } */ *uap = v;
683 struct proc *p = l->l_proc;
684 if (!KTRPOINT(p, KTR_USER))
685 return (0);
686
687 if (SCARG(uap, len) > KTR_USER_MAXLEN)
688 return (EINVAL);
689
690 ktruser(p, SCARG(uap, label), SCARG(uap, addr), SCARG(uap, len), 1);
691
692 return (0);
693 #else /* !KTRACE */
694 return ENOSYS;
695 #endif /* KTRACE */
696 }
697