]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_ktrace.c
xnu-517.tar.gz
[apple/xnu.git] / bsd / kern / kern_ktrace.c
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
26 /*
27 * Copyright (c) 1989, 1993
28 * The Regents of the University of California. All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 *
58 * @(#)kern_ktrace.c 8.2 (Berkeley) 9/23/93
59 * $FreeBSD: src/sys/kern/kern_ktrace.c,v 1.35.2.4 2001/03/05 13:09:01 obrien Exp $
60 */
61
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/types.h>
66 #include <sys/proc.h>
67 #include <sys/file.h>
68 #include <sys/namei.h>
69 #include <sys/vnode.h>
70 #if KTRACE
71 #include <sys/ktrace.h>
72 #endif
73 #include <sys/malloc.h>
74 #include <sys/syslog.h>
75
76 #if KTRACE
77 static struct ktr_header *ktrgetheader __P((int type));
78 static void ktrwrite __P((struct vnode *, struct ktr_header *,
79 struct uio *, int));
80 static int ktrcanset __P((struct proc *,struct proc *));
81 static int ktrsetchildren __P((struct proc *,struct proc *,
82 int, int, struct vnode *));
83 static int ktrops __P((struct proc *,struct proc *,int,int,struct vnode *));
84
85
86 static struct ktr_header *
87 ktrgetheader(type)
88 int type;
89 {
90 register struct ktr_header *kth;
91 struct proc *p = current_proc(); /* XXX */
92
93 MALLOC(kth, struct ktr_header *, sizeof (struct ktr_header),
94 M_KTRACE, M_WAITOK);
95 kth->ktr_type = type;
96 microtime(&kth->ktr_time);
97 kth->ktr_pid = p->p_pid;
98 bcopy(p->p_comm, kth->ktr_comm, MAXCOMLEN);
99 return (kth);
100 }
101 #endif
102
103 void
104 ktrsyscall(p, code, narg, args, funnel_type)
105 struct proc *p;
106 int code, narg;
107 register_t args[];
108 int funnel_type;
109 {
110 #if KTRACE
111 struct vnode *vp;
112 struct ktr_header *kth;
113 struct ktr_syscall *ktp;
114 register int len;
115 register_t *argp;
116 int i;
117
118 if (!KTRPOINT(p, KTR_SYSCALL))
119 return;
120
121 vp = p->p_tracep;
122 len = __offsetof(struct ktr_syscall, ktr_args) +
123 (narg * sizeof(register_t));
124 p->p_traceflag |= KTRFAC_ACTIVE;
125 kth = ktrgetheader(KTR_SYSCALL);
126 MALLOC(ktp, struct ktr_syscall *, len, M_KTRACE, M_WAITOK);
127 ktp->ktr_code = code;
128 ktp->ktr_narg = narg;
129 argp = &ktp->ktr_args[0];
130 for (i = 0; i < narg; i++)
131 *argp++ = args[i];
132 kth->ktr_buf = (caddr_t)ktp;
133 kth->ktr_len = len;
134 ktrwrite(vp, kth, NULL, funnel_type);
135 FREE(ktp, M_KTRACE);
136 FREE(kth, M_KTRACE);
137 p->p_traceflag &= ~KTRFAC_ACTIVE;
138 #else
139 return;
140 #endif
141 }
142
143 void
144 ktrsysret(p, code, error, retval, funnel_type)
145 struct proc *p;
146 int code, error;
147 register_t retval;
148 int funnel_type;
149 {
150 #if KTRACE
151 struct vnode *vp;
152 struct ktr_header *kth;
153 struct ktr_sysret ktp;
154
155 if (!KTRPOINT(p, KTR_SYSRET))
156 return;
157
158 vp = p->p_tracep;
159 p->p_traceflag |= KTRFAC_ACTIVE;
160 kth = ktrgetheader(KTR_SYSRET);
161 ktp.ktr_code = code;
162 ktp.ktr_error = error;
163 ktp.ktr_retval = retval; /* what about val2 ? */
164
165 kth->ktr_buf = (caddr_t)&ktp;
166 kth->ktr_len = sizeof(struct ktr_sysret);
167
168 ktrwrite(vp, kth, NULL, funnel_type);
169 FREE(kth, M_KTRACE);
170 p->p_traceflag &= ~KTRFAC_ACTIVE;
171 #else
172 return;
173 #endif
174 }
175
176 #if KTRACE
177 void
178 ktrnamei(vp, path)
179 struct vnode *vp;
180 char *path;
181 {
182 struct ktr_header *kth;
183 struct proc *p = current_proc(); /* XXX */
184
185 p->p_traceflag |= KTRFAC_ACTIVE;
186 kth = ktrgetheader(KTR_NAMEI);
187 kth->ktr_len = strlen(path);
188 kth->ktr_buf = path;
189
190 ktrwrite(vp, kth, NULL, KERNEL_FUNNEL);
191 FREE(kth, M_KTRACE);
192 p->p_traceflag &= ~KTRFAC_ACTIVE;
193 }
194
195 void
196 ktrgenio(vp, fd, rw, uio, error, funnel_type)
197 struct vnode *vp;
198 int fd;
199 enum uio_rw rw;
200 struct uio *uio;
201 int error;
202 int funnel_type;
203 {
204 struct ktr_header *kth;
205 struct ktr_genio ktg;
206 struct proc *p = current_proc(); /* XXX */
207
208 if (error)
209 return;
210
211 p->p_traceflag |= KTRFAC_ACTIVE;
212 kth = ktrgetheader(KTR_GENIO);
213 ktg.ktr_fd = fd;
214 ktg.ktr_rw = rw;
215 kth->ktr_buf = (caddr_t)&ktg;
216 kth->ktr_len = sizeof(struct ktr_genio);
217 uio->uio_offset = 0;
218 uio->uio_rw = UIO_WRITE;
219
220 ktrwrite(vp, kth, uio, funnel_type);
221 FREE(kth, M_KTRACE);
222 p->p_traceflag &= ~KTRFAC_ACTIVE;
223 }
224
225 void
226 ktrpsig(vp, sig, action, mask, code, funnel_type)
227 struct vnode *vp;
228 int sig;
229 sig_t action;
230 sigset_t *mask;
231 int code;
232 int funnel_type;
233 {
234 struct ktr_header *kth;
235 struct ktr_psig kp;
236 struct proc *p = current_proc(); /* XXX */
237
238 p->p_traceflag |= KTRFAC_ACTIVE;
239 kth = ktrgetheader(KTR_PSIG);
240 kp.signo = (char)sig;
241 kp.action = action;
242 kp.mask = *mask;
243 kp.code = code;
244 kth->ktr_buf = (caddr_t)&kp;
245 kth->ktr_len = sizeof (struct ktr_psig);
246
247 ktrwrite(vp, kth, NULL, funnel_type);
248 FREE(kth, M_KTRACE);
249 p->p_traceflag &= ~KTRFAC_ACTIVE;
250 }
251
252 void
253 ktrcsw(vp, out, user, funnel_type)
254 struct vnode *vp;
255 int out, user;
256 int funnel_type;
257 {
258 struct ktr_header *kth;
259 struct ktr_csw kc;
260 struct proc *p = current_proc(); /* XXX */
261
262 p->p_traceflag |= KTRFAC_ACTIVE;
263 kth = ktrgetheader(KTR_CSW);
264 kc.out = out;
265 kc.user = user;
266 kth->ktr_buf = (caddr_t)&kc;
267 kth->ktr_len = sizeof (struct ktr_csw);
268
269 ktrwrite(vp, kth, NULL, funnel_type);
270 FREE(kth, M_KTRACE);
271 p->p_traceflag &= ~KTRFAC_ACTIVE;
272 }
273 #endif /* KTRACE */
274
275 /* Interface and common routines */
276
277 /*
278 * ktrace system call
279 */
280 struct ktrace_args {
281 char *fname;
282 int ops;
283 int facs;
284 int pid;
285 };
286 /* ARGSUSED */
287 int
288 ktrace(curp, uap, retval)
289 struct proc *curp;
290 register struct ktrace_args *uap;
291 register_t *retval;
292 {
293 #if KTRACE
294 register struct vnode *vp = NULL;
295 register struct proc *p;
296 struct pgrp *pg;
297 int facs = uap->facs & ~KTRFAC_ROOT;
298 int ops = KTROP(uap->ops);
299 int descend = uap->ops & KTRFLAG_DESCEND;
300 int ret = 0;
301 int error = 0;
302 struct nameidata nd;
303
304 curp->p_traceflag |= KTRFAC_ACTIVE;
305 if (ops != KTROP_CLEAR) {
306 /*
307 * an operation which requires a file argument.
308 */
309 NDINIT(&nd, LOOKUP, (NOFOLLOW|LOCKLEAF), UIO_USERSPACE, uap->fname, curp);
310 error = vn_open(&nd, FREAD|FWRITE|O_NOFOLLOW, 0);
311 if (error) {
312 curp->p_traceflag &= ~KTRFAC_ACTIVE;
313 return (error);
314 }
315 vp = nd.ni_vp;
316 VOP_UNLOCK(vp, 0, curp);
317 if (vp->v_type != VREG) {
318 (void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
319 curp->p_traceflag &= ~KTRFAC_ACTIVE;
320 return (EACCES);
321 }
322 }
323 /*
324 * Clear all uses of the tracefile
325 */
326 if (ops == KTROP_CLEARFILE) {
327 LIST_FOREACH(p, &allproc, p_list) {
328 if (p->p_tracep == vp) {
329 if (ktrcanset(curp, p)) {
330 struct vnode *tvp = p->p_tracep;
331 /* no more tracing */
332 p->p_traceflag = 0;
333 if (tvp != NULL) {
334 p->p_tracep = NULL;
335 vrele(tvp);
336 }
337 } else
338 error = EPERM;
339 }
340 }
341 goto done;
342 }
343
344 /*
345 * need something to (un)trace (XXX - why is this here?)
346 */
347 if (!facs) {
348 error = EINVAL;
349 goto done;
350 }
351 /*
352 * do it
353 */
354 if (uap->pid < 0) {
355 /*
356 * by process group
357 */
358 pg = pgfind(-uap->pid);
359 if (pg == NULL) {
360 error = ESRCH;
361 goto done;
362 }
363 LIST_FOREACH(p, &pg->pg_members, p_pglist)
364 if (descend)
365 ret |= ktrsetchildren(curp, p, ops, facs, vp);
366 else
367 ret |= ktrops(curp, p, ops, facs, vp);
368
369 } else {
370 /*
371 * by pid
372 */
373 p = pfind(uap->pid);
374 if (p == NULL) {
375 error = ESRCH;
376 goto done;
377 }
378 if (descend)
379 ret |= ktrsetchildren(curp, p, ops, facs, vp);
380 else
381 ret |= ktrops(curp, p, ops, facs, vp);
382 }
383 if (!ret)
384 error = EPERM;
385 done:
386 if (vp != NULL)
387 (void) vn_close(vp, FWRITE, curp->p_ucred, curp);
388 curp->p_traceflag &= ~KTRFAC_ACTIVE;
389 return (error);
390 #else
391 return ENOSYS;
392 #endif
393 }
394
395 /*
396 * utrace system call
397 */
398 struct utrace_args {
399 const void * addr;
400 size_t len;
401 };
402
403 /* ARGSUSED */
404 int
405 utrace(curp, uap, retval)
406 struct proc *curp;
407 register struct utrace_args *uap;
408 register_t *retval;
409 {
410 #if KTRACE
411 struct ktr_header *kth;
412 struct proc *p = current_proc(); /* XXX */
413 register caddr_t cp;
414
415 if (!KTRPOINT(p, KTR_USER))
416 return (0);
417 if (uap->len > KTR_USER_MAXLEN)
418 return (EINVAL);
419 p->p_traceflag |= KTRFAC_ACTIVE;
420 kth = ktrgetheader(KTR_USER);
421 MALLOC(cp, caddr_t, uap->len, M_KTRACE, M_WAITOK);
422 if (!copyin((caddr_t)uap->addr, cp, uap->len)) {
423 kth->ktr_buf = cp;
424 kth->ktr_len = uap->len;
425 ktrwrite(p->p_tracep, kth, NULL, KERNEL_FUNNEL);
426 }
427 FREE(kth, M_KTRACE);
428 FREE(cp, M_KTRACE);
429 p->p_traceflag &= ~KTRFAC_ACTIVE;
430
431 return (0);
432 #else
433 return (ENOSYS);
434 #endif
435 }
436
437 #if KTRACE
438 static int
439 ktrops(curp, p, ops, facs, vp)
440 struct proc *p, *curp;
441 int ops, facs;
442 struct vnode *vp;
443 {
444 struct vnode *tvp;
445
446 if (!ktrcanset(curp, p))
447 return (0);
448 if (ops == KTROP_SET) {
449 if (p->p_tracep != vp) {
450 /*
451 * if trace file already in use, relinquish
452 */
453 tvp = p->p_tracep;
454 VREF(vp);
455 p->p_tracep = vp;
456 if (tvp != NULL)
457 vrele(tvp);
458 }
459 p->p_traceflag |= facs;
460 if (curp->p_ucred->cr_uid == 0)
461 p->p_traceflag |= KTRFAC_ROOT;
462 } else {
463 /* KTROP_CLEAR */
464 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
465 /* no more tracing */
466 tvp = p->p_tracep;
467 p->p_traceflag = 0;
468 if (tvp != NULL) {
469 p->p_tracep = NULL;
470 vrele(tvp);
471 }
472 }
473 }
474
475 return (1);
476 }
477
478 static int
479 ktrsetchildren(curp, top, ops, facs, vp)
480 struct proc *curp, *top;
481 int ops, facs;
482 struct vnode *vp;
483 {
484 register struct proc *p;
485 register int ret = 0;
486
487 p = top;
488 for (;;) {
489 ret |= ktrops(curp, p, ops, facs, vp);
490 /*
491 * If this process has children, descend to them next,
492 * otherwise do any siblings, and if done with this level,
493 * follow back up the tree (but not past top).
494 */
495 if (!LIST_EMPTY(&p->p_children))
496 p = LIST_FIRST(&p->p_children);
497 else for (;;) {
498 if (p == top)
499 return (ret);
500 if (LIST_NEXT(p, p_sibling)) {
501 p = LIST_NEXT(p, p_sibling);
502 break;
503 }
504 p = p->p_pptr;
505 }
506 }
507 /*NOTREACHED*/
508 }
509
510 static void
511 ktrwrite(vp, kth, uio, funnel_type)
512 struct vnode *vp;
513 register struct ktr_header *kth;
514 struct uio *uio;
515 {
516 struct uio auio;
517 struct iovec aiov[2];
518 register struct proc *p = current_proc(); /* XXX */
519 int error;
520
521 if (vp == NULL)
522 return;
523
524 if (funnel_type == -1) {
525 funnel_t *f = thread_funnel_get();
526 if(f == THR_FUNNEL_NULL)
527 funnel_type = NO_FUNNEL;
528 else if (f == (funnel_t *)network_flock)
529 funnel_type = NETWORK_FUNNEL;
530 else if (f == (funnel_t *)kernel_flock)
531 funnel_type = KERNEL_FUNNEL;
532 }
533
534 switch (funnel_type) {
535 case KERNEL_FUNNEL:
536 /* Nothing more to do */
537 break;
538 case NETWORK_FUNNEL:
539 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
540 break;
541 case NO_FUNNEL:
542 (void) thread_funnel_set(kernel_flock, TRUE);
543 break;
544 default:
545 panic("Invalid funnel (%)", funnel_type);
546 }
547 auio.uio_iov = &aiov[0];
548 auio.uio_offset = 0;
549 auio.uio_segflg = UIO_SYSSPACE;
550 auio.uio_rw = UIO_WRITE;
551 aiov[0].iov_base = (caddr_t)kth;
552 aiov[0].iov_len = sizeof(struct ktr_header);
553 auio.uio_resid = sizeof(struct ktr_header);
554 auio.uio_iovcnt = 1;
555 auio.uio_procp = current_proc();
556 if (kth->ktr_len > 0) {
557 auio.uio_iovcnt++;
558 aiov[1].iov_base = kth->ktr_buf;
559 aiov[1].iov_len = kth->ktr_len;
560 auio.uio_resid += kth->ktr_len;
561 if (uio != NULL)
562 kth->ktr_len += uio->uio_resid;
563 }
564 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
565 if (error)
566 goto bad;
567 (void)VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
568 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, p->p_ucred);
569 if (error == 0 && uio != NULL) {
570 (void)VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
571 error = VOP_WRITE(vp, uio, IO_UNIT | IO_APPEND, p->p_ucred);
572 }
573 VOP_UNLOCK(vp, 0, p);
574 if (!error) {
575 switch (funnel_type) {
576 case KERNEL_FUNNEL:
577 /* Nothing more to do */
578 break;
579 case NETWORK_FUNNEL:
580 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
581 /* switch funnel to NETWORK_FUNNEL */
582 break;
583 case NO_FUNNEL:
584 (void) thread_funnel_set(kernel_flock, FALSE);
585 break;
586 default:
587 panic("Invalid funnel (%)", funnel_type);
588 }
589 return;
590 }
591
592 bad:
593 /*
594 * If error encountered, give up tracing on this vnode.
595 */
596 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
597 error);
598 LIST_FOREACH(p, &allproc, p_list) {
599 if (p->p_tracep == vp) {
600 p->p_tracep = NULL;
601 p->p_traceflag = 0;
602 vrele(vp);
603 }
604 }
605
606 switch (funnel_type) {
607 case KERNEL_FUNNEL:
608 /* Nothing more to do */
609 break;
610 case NETWORK_FUNNEL:
611 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
612 /* switch funnel to NETWORK_FUNNEL */
613 break;
614 case NO_FUNNEL:
615 (void) thread_funnel_set(kernel_flock, FALSE);
616 break;
617 default:
618 panic("Invalid funnel (%)", funnel_type);
619 }
620 }
621
622 /*
623 * Return true if caller has permission to set the ktracing state
624 * of target. Essentially, the target can't possess any
625 * more permissions than the caller. KTRFAC_ROOT signifies that
626 * root previously set the tracing status on the target process, and
627 * so, only root may further change it.
628 *
629 * TODO: check groups. use caller effective gid.
630 */
631 static int
632 ktrcanset(callp, targetp)
633 struct proc *callp, *targetp;
634 {
635 register struct pcred *caller = callp->p_cred;
636 register struct pcred *target = targetp->p_cred;
637
638 if (!PRISON_CHECK(callp, targetp))
639 return (0);
640 if ((caller->pc_ucred->cr_uid == target->p_ruid &&
641 target->p_ruid == target->p_svuid &&
642 caller->p_rgid == target->p_rgid && /* XXX */
643 target->p_rgid == target->p_svgid &&
644 (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
645 (targetp->p_flag & P_SUGID) == 0) ||
646 caller->pc_ucred->cr_uid == 0)
647 return (1);
648
649 return (0);
650 }
651
652 #endif /* KTRACE */