1 /*-
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)kern_ktrace.c 8.2 (Berkeley) 9/23/93
30 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD: releng/6.1/sys/kern/kern_ktrace.c 158179 2006-04-30 16:44:43Z cvs2svn $");
34
35 #include "opt_ktrace.h"
36 #include "opt_mac.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/fcntl.h>
41 #include <sys/kernel.h>
42 #include <sys/kthread.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/mac.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/namei.h>
49 #include <sys/proc.h>
50 #include <sys/unistd.h>
51 #include <sys/vnode.h>
52 #include <sys/ktrace.h>
53 #include <sys/sx.h>
54 #include <sys/sysctl.h>
55 #include <sys/syslog.h>
56 #include <sys/sysproto.h>
57
58 static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
59
60 #ifdef KTRACE
61
62 #ifndef KTRACE_REQUEST_POOL
63 #define KTRACE_REQUEST_POOL 100
64 #endif
65
66 struct ktr_request {
67 struct ktr_header ktr_header;
68 void *ktr_buffer;
69 struct ucred *ktr_cred;
70 struct vnode *ktr_vp;
71 union {
72 struct ktr_syscall ktr_syscall;
73 struct ktr_sysret ktr_sysret;
74 struct ktr_genio ktr_genio;
75 struct ktr_psig ktr_psig;
76 struct ktr_csw ktr_csw;
77 } ktr_data;
78 STAILQ_ENTRY(ktr_request) ktr_list;
79 };
80
81 static int data_lengths[] = {
82 0, /* none */
83 offsetof(struct ktr_syscall, ktr_args), /* KTR_SYSCALL */
84 sizeof(struct ktr_sysret), /* KTR_SYSRET */
85 0, /* KTR_NAMEI */
86 sizeof(struct ktr_genio), /* KTR_GENIO */
87 sizeof(struct ktr_psig), /* KTR_PSIG */
88 sizeof(struct ktr_csw), /* KTR_CSW */
89 0 /* KTR_USER */
90 };
91
92 static STAILQ_HEAD(, ktr_request) ktr_todo;
93 static STAILQ_HEAD(, ktr_request) ktr_free;
94
95 static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
96
97 static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
98 TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
99
100 static u_int ktr_geniosize = PAGE_SIZE;
101 TUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize);
102 SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize,
103 0, "Maximum size of genio event payload");
104
105 static int print_message = 1;
106 struct mtx ktrace_mtx;
107 static struct cv ktrace_cv;
108
109 static void ktrace_init(void *dummy);
110 static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
111 static u_int ktrace_resize_pool(u_int newsize);
112 static struct ktr_request *ktr_getrequest(int type);
113 static void ktr_submitrequest(struct ktr_request *req);
114 static void ktr_freerequest(struct ktr_request *req);
115 static void ktr_loop(void *dummy);
116 static void ktr_writerequest(struct ktr_request *req);
117 static int ktrcanset(struct thread *,struct proc *);
118 static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
119 static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
120
121 static void
122 ktrace_init(void *dummy)
123 {
124 struct ktr_request *req;
125 int i;
126
127 mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
128 cv_init(&ktrace_cv, "ktrace");
129 STAILQ_INIT(&ktr_todo);
130 STAILQ_INIT(&ktr_free);
131 for (i = 0; i < ktr_requestpool; i++) {
132 req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
133 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
134 }
135 kthread_create(ktr_loop, NULL, NULL, RFHIGHPID, 0, "ktrace");
136 }
137 SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
138
139 static int
140 sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
141 {
142 struct thread *td;
143 u_int newsize, oldsize, wantsize;
144 int error;
145
146 /* Handle easy read-only case first to avoid warnings from GCC. */
147 if (!req->newptr) {
148 mtx_lock(&ktrace_mtx);
149 oldsize = ktr_requestpool;
150 mtx_unlock(&ktrace_mtx);
151 return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
152 }
153
154 error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
155 if (error)
156 return (error);
157 td = curthread;
158 td->td_pflags |= TDP_INKTRACE;
159 mtx_lock(&ktrace_mtx);
160 oldsize = ktr_requestpool;
161 newsize = ktrace_resize_pool(wantsize);
162 mtx_unlock(&ktrace_mtx);
163 td->td_pflags &= ~TDP_INKTRACE;
164 error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
165 if (error)
166 return (error);
167 if (wantsize > oldsize && newsize < wantsize)
168 return (ENOSPC);
169 return (0);
170 }
171 SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
172 &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU", "");
173
174 static u_int
175 ktrace_resize_pool(u_int newsize)
176 {
177 struct ktr_request *req;
178 int bound;
179
180 mtx_assert(&ktrace_mtx, MA_OWNED);
181 print_message = 1;
182 bound = newsize - ktr_requestpool;
183 if (bound == 0)
184 return (ktr_requestpool);
185 if (bound < 0)
186 /* Shrink pool down to newsize if possible. */
187 while (bound++ < 0) {
188 req = STAILQ_FIRST(&ktr_free);
189 if (req == NULL)
190 return (ktr_requestpool);
191 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
192 ktr_requestpool--;
193 mtx_unlock(&ktrace_mtx);
194 free(req, M_KTRACE);
195 mtx_lock(&ktrace_mtx);
196 }
197 else
198 /* Grow pool up to newsize. */
199 while (bound-- > 0) {
200 mtx_unlock(&ktrace_mtx);
201 req = malloc(sizeof(struct ktr_request), M_KTRACE,
202 M_WAITOK);
203 mtx_lock(&ktrace_mtx);
204 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
205 ktr_requestpool++;
206 }
207 return (ktr_requestpool);
208 }
209
210 static struct ktr_request *
211 ktr_getrequest(int type)
212 {
213 struct ktr_request *req;
214 struct thread *td = curthread;
215 struct proc *p = td->td_proc;
216 int pm;
217
218 td->td_pflags |= TDP_INKTRACE;
219 mtx_lock(&ktrace_mtx);
220 if (!KTRCHECK(td, type)) {
221 mtx_unlock(&ktrace_mtx);
222 td->td_pflags &= ~TDP_INKTRACE;
223 return (NULL);
224 }
225 req = STAILQ_FIRST(&ktr_free);
226 if (req != NULL) {
227 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
228 req->ktr_header.ktr_type = type;
229 if (p->p_traceflag & KTRFAC_DROP) {
230 req->ktr_header.ktr_type |= KTR_DROP;
231 p->p_traceflag &= ~KTRFAC_DROP;
232 }
233 KASSERT(p->p_tracevp != NULL, ("ktrace: no trace vnode"));
234 KASSERT(p->p_tracecred != NULL, ("ktrace: no trace cred"));
235 req->ktr_vp = p->p_tracevp;
236 VREF(p->p_tracevp);
237 req->ktr_cred = crhold(p->p_tracecred);
238 mtx_unlock(&ktrace_mtx);
239 microtime(&req->ktr_header.ktr_time);
240 req->ktr_header.ktr_pid = p->p_pid;
241 req->ktr_header.ktr_tid = td->td_tid;
242 bcopy(p->p_comm, req->ktr_header.ktr_comm, MAXCOMLEN + 1);
243 req->ktr_buffer = NULL;
244 req->ktr_header.ktr_len = 0;
245 } else {
246 p->p_traceflag |= KTRFAC_DROP;
247 pm = print_message;
248 print_message = 0;
249 mtx_unlock(&ktrace_mtx);
250 if (pm)
251 printf("Out of ktrace request objects.\n");
252 td->td_pflags &= ~TDP_INKTRACE;
253 }
254 return (req);
255 }
256
257 static void
258 ktr_submitrequest(struct ktr_request *req)
259 {
260
261 mtx_lock(&ktrace_mtx);
262 STAILQ_INSERT_TAIL(&ktr_todo, req, ktr_list);
263 cv_signal(&ktrace_cv);
264 mtx_unlock(&ktrace_mtx);
265 curthread->td_pflags &= ~TDP_INKTRACE;
266 }
267
268 static void
269 ktr_freerequest(struct ktr_request *req)
270 {
271
272 crfree(req->ktr_cred);
273 if (req->ktr_vp != NULL) {
274 mtx_lock(&Giant);
275 vrele(req->ktr_vp);
276 mtx_unlock(&Giant);
277 }
278 if (req->ktr_buffer != NULL)
279 free(req->ktr_buffer, M_KTRACE);
280 mtx_lock(&ktrace_mtx);
281 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
282 mtx_unlock(&ktrace_mtx);
283 }
284
285 static void
286 ktr_loop(void *dummy)
287 {
288 struct ktr_request *req;
289 struct thread *td;
290 struct ucred *cred;
291
292 /* Only cache these values once. */
293 td = curthread;
294 cred = td->td_ucred;
295 for (;;) {
296 mtx_lock(&ktrace_mtx);
297 while (STAILQ_EMPTY(&ktr_todo))
298 cv_wait(&ktrace_cv, &ktrace_mtx);
299 req = STAILQ_FIRST(&ktr_todo);
300 STAILQ_REMOVE_HEAD(&ktr_todo, ktr_list);
301 KASSERT(req != NULL, ("got a NULL request"));
302 mtx_unlock(&ktrace_mtx);
303 /*
304 * It is not enough just to pass the cached cred
305 * to the VOP's in ktr_writerequest(). Some VFS
306 * operations use curthread->td_ucred, so we need
307 * to modify our thread's credentials as well.
308 * Evil.
309 */
310 td->td_ucred = req->ktr_cred;
311 ktr_writerequest(req);
312 td->td_ucred = cred;
313 ktr_freerequest(req);
314 }
315 }
316
317 /*
318 * MPSAFE
319 */
320 void
321 ktrsyscall(code, narg, args)
322 int code, narg;
323 register_t args[];
324 {
325 struct ktr_request *req;
326 struct ktr_syscall *ktp;
327 size_t buflen;
328 char *buf = NULL;
329
330 buflen = sizeof(register_t) * narg;
331 if (buflen > 0) {
332 buf = malloc(buflen, M_KTRACE, M_WAITOK);
333 bcopy(args, buf, buflen);
334 }
335 req = ktr_getrequest(KTR_SYSCALL);
336 if (req == NULL) {
337 if (buf != NULL)
338 free(buf, M_KTRACE);
339 return;
340 }
341 ktp = &req->ktr_data.ktr_syscall;
342 ktp->ktr_code = code;
343 ktp->ktr_narg = narg;
344 if (buflen > 0) {
345 req->ktr_header.ktr_len = buflen;
346 req->ktr_buffer = buf;
347 }
348 ktr_submitrequest(req);
349 }
350
351 /*
352 * MPSAFE
353 */
354 void
355 ktrsysret(code, error, retval)
356 int code, error;
357 register_t retval;
358 {
359 struct ktr_request *req;
360 struct ktr_sysret *ktp;
361
362 req = ktr_getrequest(KTR_SYSRET);
363 if (req == NULL)
364 return;
365 ktp = &req->ktr_data.ktr_sysret;
366 ktp->ktr_code = code;
367 ktp->ktr_error = error;
368 ktp->ktr_retval = retval; /* what about val2 ? */
369 ktr_submitrequest(req);
370 }
371
372 void
373 ktrnamei(path)
374 char *path;
375 {
376 struct ktr_request *req;
377 int namelen;
378 char *buf = NULL;
379
380 namelen = strlen(path);
381 if (namelen > 0) {
382 buf = malloc(namelen, M_KTRACE, M_WAITOK);
383 bcopy(path, buf, namelen);
384 }
385 req = ktr_getrequest(KTR_NAMEI);
386 if (req == NULL) {
387 if (buf != NULL)
388 free(buf, M_KTRACE);
389 return;
390 }
391 if (namelen > 0) {
392 req->ktr_header.ktr_len = namelen;
393 req->ktr_buffer = buf;
394 }
395 ktr_submitrequest(req);
396 }
397
398 /*
399 * Since the uio may not stay valid, we can not hand off this request to
400 * the thread and need to process it synchronously. However, we wish to
401 * keep the relative order of records in a trace file correct, so we
402 * do put this request on the queue (if it isn't empty) and then block.
403 * The ktrace thread waks us back up when it is time for this event to
404 * be posted and blocks until we have completed writing out the event
405 * and woken it back up.
406 */
407 void
408 ktrgenio(fd, rw, uio, error)
409 int fd;
410 enum uio_rw rw;
411 struct uio *uio;
412 int error;
413 {
414 struct ktr_request *req;
415 struct ktr_genio *ktg;
416 int datalen;
417 char *buf;
418
419 if (error) {
420 free(uio, M_IOV);
421 return;
422 }
423 uio->uio_offset = 0;
424 uio->uio_rw = UIO_WRITE;
425 datalen = imin(uio->uio_resid, ktr_geniosize);
426 buf = malloc(datalen, M_KTRACE, M_WAITOK);
427 error = uiomove(buf, datalen, uio);
428 free(uio, M_IOV);
429 if (error) {
430 free(buf, M_KTRACE);
431 return;
432 }
433 req = ktr_getrequest(KTR_GENIO);
434 if (req == NULL) {
435 free(buf, M_KTRACE);
436 return;
437 }
438 ktg = &req->ktr_data.ktr_genio;
439 ktg->ktr_fd = fd;
440 ktg->ktr_rw = rw;
441 req->ktr_header.ktr_len = datalen;
442 req->ktr_buffer = buf;
443 ktr_submitrequest(req);
444 }
445
446 void
447 ktrpsig(sig, action, mask, code)
448 int sig;
449 sig_t action;
450 sigset_t *mask;
451 int code;
452 {
453 struct ktr_request *req;
454 struct ktr_psig *kp;
455
456 req = ktr_getrequest(KTR_PSIG);
457 if (req == NULL)
458 return;
459 kp = &req->ktr_data.ktr_psig;
460 kp->signo = (char)sig;
461 kp->action = action;
462 kp->mask = *mask;
463 kp->code = code;
464 ktr_submitrequest(req);
465 }
466
467 void
468 ktrcsw(out, user)
469 int out, user;
470 {
471 struct ktr_request *req;
472 struct ktr_csw *kc;
473
474 req = ktr_getrequest(KTR_CSW);
475 if (req == NULL)
476 return;
477 kc = &req->ktr_data.ktr_csw;
478 kc->out = out;
479 kc->user = user;
480 ktr_submitrequest(req);
481 }
482 #endif /* KTRACE */
483
484 /* Interface and common routines */
485
486 /*
487 * ktrace system call
488 *
489 * MPSAFE
490 */
491 #ifndef _SYS_SYSPROTO_H_
492 struct ktrace_args {
493 char *fname;
494 int ops;
495 int facs;
496 int pid;
497 };
498 #endif
499 /* ARGSUSED */
500 int
501 ktrace(td, uap)
502 struct thread *td;
503 register struct ktrace_args *uap;
504 {
505 #ifdef KTRACE
506 register struct vnode *vp = NULL;
507 register struct proc *p;
508 struct pgrp *pg;
509 int facs = uap->facs & ~KTRFAC_ROOT;
510 int ops = KTROP(uap->ops);
511 int descend = uap->ops & KTRFLAG_DESCEND;
512 int nfound, ret = 0;
513 int flags, error = 0;
514 struct nameidata nd;
515 struct ucred *cred;
516
517 /*
518 * Need something to (un)trace.
519 */
520 if (ops != KTROP_CLEARFILE && facs == 0)
521 return (EINVAL);
522
523 td->td_pflags |= TDP_INKTRACE;
524 if (ops != KTROP_CLEAR) {
525 /*
526 * an operation which requires a file argument.
527 */
528 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td);
529 flags = FREAD | FWRITE | O_NOFOLLOW;
530 mtx_lock(&Giant);
531 error = vn_open(&nd, &flags, 0, -1);
532 if (error) {
533 mtx_unlock(&Giant);
534 td->td_pflags &= ~TDP_INKTRACE;
535 return (error);
536 }
537 NDFREE(&nd, NDF_ONLY_PNBUF);
538 vp = nd.ni_vp;
539 VOP_UNLOCK(vp, 0, td);
540 if (vp->v_type != VREG) {
541 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
542 mtx_unlock(&Giant);
543 td->td_pflags &= ~TDP_INKTRACE;
544 return (EACCES);
545 }
546 mtx_unlock(&Giant);
547 }
548 /*
549 * Clear all uses of the tracefile.
550 */
551 if (ops == KTROP_CLEARFILE) {
552 sx_slock(&allproc_lock);
553 LIST_FOREACH(p, &allproc, p_list) {
554 PROC_LOCK(p);
555 if (p->p_tracevp == vp) {
556 if (ktrcanset(td, p)) {
557 mtx_lock(&ktrace_mtx);
558 cred = p->p_tracecred;
559 p->p_tracecred = NULL;
560 p->p_tracevp = NULL;
561 p->p_traceflag = 0;
562 mtx_unlock(&ktrace_mtx);
563 PROC_UNLOCK(p);
564 mtx_lock(&Giant);
565 (void) vn_close(vp, FREAD|FWRITE,
566 cred, td);
567 mtx_unlock(&Giant);
568 crfree(cred);
569 } else {
570 PROC_UNLOCK(p);
571 error = EPERM;
572 }
573 } else
574 PROC_UNLOCK(p);
575 }
576 sx_sunlock(&allproc_lock);
577 goto done;
578 }
579 /*
580 * do it
581 */
582 sx_slock(&proctree_lock);
583 if (uap->pid < 0) {
584 /*
585 * by process group
586 */
587 pg = pgfind(-uap->pid);
588 if (pg == NULL) {
589 sx_sunlock(&proctree_lock);
590 error = ESRCH;
591 goto done;
592 }
593 /*
594 * ktrops() may call vrele(). Lock pg_members
595 * by the proctree_lock rather than pg_mtx.
596 */
597 PGRP_UNLOCK(pg);
598 nfound = 0;
599 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
600 PROC_LOCK(p);
601 if (p_cansee(td, p) != 0) {
602 PROC_UNLOCK(p);
603 continue;
604 }
605 PROC_UNLOCK(p);
606 nfound++;
607 if (descend)
608 ret |= ktrsetchildren(td, p, ops, facs, vp);
609 else
610 ret |= ktrops(td, p, ops, facs, vp);
611 }
612 if (nfound == 0) {
613 sx_sunlock(&proctree_lock);
614 error = ESRCH;
615 goto done;
616 }
617 } else {
618 /*
619 * by pid
620 */
621 p = pfind(uap->pid);
622 if (p == NULL) {
623 sx_sunlock(&proctree_lock);
624 error = ESRCH;
625 goto done;
626 }
627 error = p_cansee(td, p);
628 /*
629 * The slock of the proctree lock will keep this process
630 * from going away, so unlocking the proc here is ok.
631 */
632 PROC_UNLOCK(p);
633 if (error) {
634 sx_sunlock(&proctree_lock);
635 goto done;
636 }
637 if (descend)
638 ret |= ktrsetchildren(td, p, ops, facs, vp);
639 else
640 ret |= ktrops(td, p, ops, facs, vp);
641 }
642 sx_sunlock(&proctree_lock);
643 if (!ret)
644 error = EPERM;
645 done:
646 if (vp != NULL) {
647 mtx_lock(&Giant);
648 (void) vn_close(vp, FWRITE, td->td_ucred, td);
649 mtx_unlock(&Giant);
650 }
651 td->td_pflags &= ~TDP_INKTRACE;
652 return (error);
653 #else /* !KTRACE */
654 return (ENOSYS);
655 #endif /* KTRACE */
656 }
657
658 /*
659 * utrace system call
660 *
661 * MPSAFE
662 */
663 /* ARGSUSED */
664 int
665 utrace(td, uap)
666 struct thread *td;
667 register struct utrace_args *uap;
668 {
669
670 #ifdef KTRACE
671 struct ktr_request *req;
672 void *cp;
673 int error;
674
675 if (!KTRPOINT(td, KTR_USER))
676 return (0);
677 if (uap->len > KTR_USER_MAXLEN)
678 return (EINVAL);
679 cp = malloc(uap->len, M_KTRACE, M_WAITOK);
680 error = copyin(uap->addr, cp, uap->len);
681 if (error) {
682 free(cp, M_KTRACE);
683 return (error);
684 }
685 req = ktr_getrequest(KTR_USER);
686 if (req == NULL) {
687 free(cp, M_KTRACE);
688 return (ENOMEM);
689 }
690 req->ktr_buffer = cp;
691 req->ktr_header.ktr_len = uap->len;
692 ktr_submitrequest(req);
693 return (0);
694 #else /* !KTRACE */
695 return (ENOSYS);
696 #endif /* KTRACE */
697 }
698
699 #ifdef KTRACE
700 static int
701 ktrops(td, p, ops, facs, vp)
702 struct thread *td;
703 struct proc *p;
704 int ops, facs;
705 struct vnode *vp;
706 {
707 struct vnode *tracevp = NULL;
708 struct ucred *tracecred = NULL;
709
710 PROC_LOCK(p);
711 if (!ktrcanset(td, p)) {
712 PROC_UNLOCK(p);
713 return (0);
714 }
715 mtx_lock(&ktrace_mtx);
716 if (ops == KTROP_SET) {
717 if (p->p_tracevp != vp) {
718 /*
719 * if trace file already in use, relinquish below
720 */
721 tracevp = p->p_tracevp;
722 VREF(vp);
723 p->p_tracevp = vp;
724 }
725 if (p->p_tracecred != td->td_ucred) {
726 tracecred = p->p_tracecred;
727 p->p_tracecred = crhold(td->td_ucred);
728 }
729 p->p_traceflag |= facs;
730 if (td->td_ucred->cr_uid == 0)
731 p->p_traceflag |= KTRFAC_ROOT;
732 } else {
733 /* KTROP_CLEAR */
734 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
735 /* no more tracing */
736 p->p_traceflag = 0;
737 tracevp = p->p_tracevp;
738 p->p_tracevp = NULL;
739 tracecred = p->p_tracecred;
740 p->p_tracecred = NULL;
741 }
742 }
743 mtx_unlock(&ktrace_mtx);
744 PROC_UNLOCK(p);
745 if (tracevp != NULL) {
746 int vfslocked;
747
748 vfslocked = VFS_LOCK_GIANT(tracevp->v_mount);
749 vrele(tracevp);
750 VFS_UNLOCK_GIANT(vfslocked);
751 }
752 if (tracecred != NULL)
753 crfree(tracecred);
754
755 return (1);
756 }
757
758 static int
759 ktrsetchildren(td, top, ops, facs, vp)
760 struct thread *td;
761 struct proc *top;
762 int ops, facs;
763 struct vnode *vp;
764 {
765 register struct proc *p;
766 register int ret = 0;
767
768 p = top;
769 sx_assert(&proctree_lock, SX_LOCKED);
770 for (;;) {
771 ret |= ktrops(td, p, ops, facs, vp);
772 /*
773 * If this process has children, descend to them next,
774 * otherwise do any siblings, and if done with this level,
775 * follow back up the tree (but not past top).
776 */
777 if (!LIST_EMPTY(&p->p_children))
778 p = LIST_FIRST(&p->p_children);
779 else for (;;) {
780 if (p == top)
781 return (ret);
782 if (LIST_NEXT(p, p_sibling)) {
783 p = LIST_NEXT(p, p_sibling);
784 break;
785 }
786 p = p->p_pptr;
787 }
788 }
789 /*NOTREACHED*/
790 }
791
792 static void
793 ktr_writerequest(struct ktr_request *req)
794 {
795 struct ktr_header *kth;
796 struct vnode *vp;
797 struct proc *p;
798 struct thread *td;
799 struct ucred *cred;
800 struct uio auio;
801 struct iovec aiov[3];
802 struct mount *mp;
803 int datalen, buflen, vrele_count;
804 int error;
805
806 vp = req->ktr_vp;
807 /*
808 * If vp is NULL, the vp has been cleared out from under this
809 * request, so just drop it.
810 */
811 if (vp == NULL)
812 return;
813 kth = &req->ktr_header;
814 datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
815 buflen = kth->ktr_len;
816 cred = req->ktr_cred;
817 td = curthread;
818 auio.uio_iov = &aiov[0];
819 auio.uio_offset = 0;
820 auio.uio_segflg = UIO_SYSSPACE;
821 auio.uio_rw = UIO_WRITE;
822 aiov[0].iov_base = (caddr_t)kth;
823 aiov[0].iov_len = sizeof(struct ktr_header);
824 auio.uio_resid = sizeof(struct ktr_header);
825 auio.uio_iovcnt = 1;
826 auio.uio_td = td;
827 if (datalen != 0) {
828 aiov[1].iov_base = (caddr_t)&req->ktr_data;
829 aiov[1].iov_len = datalen;
830 auio.uio_resid += datalen;
831 auio.uio_iovcnt++;
832 kth->ktr_len += datalen;
833 }
834 if (buflen != 0) {
835 KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
836 aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
837 aiov[auio.uio_iovcnt].iov_len = buflen;
838 auio.uio_resid += buflen;
839 auio.uio_iovcnt++;
840 }
841 mtx_lock(&Giant);
842 vn_start_write(vp, &mp, V_WAIT);
843 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
844 (void)VOP_LEASE(vp, td, cred, LEASE_WRITE);
845 #ifdef MAC
846 error = mac_check_vnode_write(cred, NOCRED, vp);
847 if (error == 0)
848 #endif
849 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
850 VOP_UNLOCK(vp, 0, td);
851 vn_finished_write(mp);
852 mtx_unlock(&Giant);
853 if (!error)
854 return;
855 /*
856 * If error encountered, give up tracing on this vnode. We defer
857 * all the vrele()'s on the vnode until after we are finished walking
858 * the various lists to avoid needlessly holding locks.
859 */
860 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
861 error);
862 vrele_count = 0;
863 /*
864 * First, clear this vnode from being used by any processes in the
865 * system.
866 * XXX - If one process gets an EPERM writing to the vnode, should
867 * we really do this? Other processes might have suitable
868 * credentials for the operation.
869 */
870 cred = NULL;
871 sx_slock(&allproc_lock);
872 LIST_FOREACH(p, &allproc, p_list) {
873 PROC_LOCK(p);
874 if (p->p_tracevp == vp) {
875 mtx_lock(&ktrace_mtx);
876 p->p_tracevp = NULL;
877 p->p_traceflag = 0;
878 cred = p->p_tracecred;
879 p->p_tracecred = NULL;
880 mtx_unlock(&ktrace_mtx);
881 vrele_count++;
882 }
883 PROC_UNLOCK(p);
884 if (cred != NULL) {
885 crfree(cred);
886 cred = NULL;
887 }
888 }
889 sx_sunlock(&allproc_lock);
890 /*
891 * Second, clear this vnode from any pending requests.
892 */
893 mtx_lock(&ktrace_mtx);
894 STAILQ_FOREACH(req, &ktr_todo, ktr_list) {
895 if (req->ktr_vp == vp) {
896 req->ktr_vp = NULL;
897 vrele_count++;
898 }
899 }
900 mtx_unlock(&ktrace_mtx);
901 mtx_lock(&Giant);
902 while (vrele_count-- > 0)
903 vrele(vp);
904 mtx_unlock(&Giant);
905 }
906
907 /*
908 * Return true if caller has permission to set the ktracing state
909 * of target. Essentially, the target can't possess any
910 * more permissions than the caller. KTRFAC_ROOT signifies that
911 * root previously set the tracing status on the target process, and
912 * so, only root may further change it.
913 */
914 static int
915 ktrcanset(td, targetp)
916 struct thread *td;
917 struct proc *targetp;
918 {
919
920 PROC_LOCK_ASSERT(targetp, MA_OWNED);
921 if (targetp->p_traceflag & KTRFAC_ROOT &&
922 suser_cred(td->td_ucred, SUSER_ALLOWJAIL))
923 return (0);
924
925 if (p_candebug(td, targetp) != 0)
926 return (0);
927
928 return (1);
929 }
930
931 #endif /* KTRACE */
Cache object: cae5b39c4ec64bf888285948c45cfca2
|