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