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