xref: /dragonfly/sys/kern/kern_ktrace.c (revision dcd37f7d)
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)kern_ktrace.c	8.2 (Berkeley) 9/23/93
34  * $FreeBSD: src/sys/kern/kern_ktrace.c,v 1.35.2.6 2002/07/05 22:36:38 darrenr Exp $
35  * $DragonFly: src/sys/kern/kern_ktrace.c,v 1.30 2008/04/14 12:01:50 dillon Exp $
36  */
37 
38 #include "opt_ktrace.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysproto.h>
43 #include <sys/kernel.h>
44 #include <sys/proc.h>
45 #include <sys/fcntl.h>
46 #include <sys/lock.h>
47 #include <sys/nlookup.h>
48 #include <sys/vnode.h>
49 #include <sys/ktrace.h>
50 #include <sys/malloc.h>
51 #include <sys/syslog.h>
52 #include <sys/sysent.h>
53 
54 #include <vm/vm_zone.h>
55 
56 #include <sys/mplock2.h>
57 
58 static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
59 
60 #ifdef KTRACE
61 static struct ktr_header *ktrgetheader (int type);
62 static void ktrwrite (struct lwp *, struct ktr_header *, struct uio *);
63 static int ktrcanset (struct thread *,struct proc *);
64 static int ktrsetchildren (struct thread *,struct proc *,int,int, ktrace_node_t);
65 static int ktrops (struct thread *,struct proc *,int,int, ktrace_node_t);
66 
67 /*
68  * MPSAFE
69  */
70 static struct ktr_header *
71 ktrgetheader(int type)
72 {
73 	struct ktr_header *kth;
74 	struct proc *p = curproc;	/* XXX */
75 	struct lwp *lp = curthread->td_lwp;
76 
77 	MALLOC(kth, struct ktr_header *, sizeof (struct ktr_header),
78 		M_KTRACE, M_WAITOK);
79 	kth->ktr_type = type;
80 	/* XXX threaded flag is a hack at the moment */
81 	kth->ktr_flags = (p->p_nthreads > 1) ? KTRH_THREADED : 0;
82 	microtime(&kth->ktr_time);
83 	kth->ktr_pid = p->p_pid;
84 	kth->ktr_tid = lp->lwp_tid;
85 	bcopy(p->p_comm, kth->ktr_comm, MAXCOMLEN + 1);
86 	return (kth);
87 }
88 
89 void
90 ktrsyscall(struct lwp *lp, int code, int narg, register_t args[])
91 {
92 	struct	ktr_header *kth;
93 	struct	ktr_syscall *ktp;
94 	int len;
95 	register_t *argp;
96 	int i;
97 
98 	len = offsetof(struct ktr_syscall, ktr_args) +
99 	      (narg * sizeof(register_t));
100 
101 	/*
102 	 * Setting the active bit prevents a ktrace recursion from the
103 	 * ktracing op itself.
104 	 */
105 	lp->lwp_traceflag |= KTRFAC_ACTIVE;
106 	kth = ktrgetheader(KTR_SYSCALL);
107 	MALLOC(ktp, struct ktr_syscall *, len, M_KTRACE, M_WAITOK);
108 	ktp->ktr_code = code;
109 	ktp->ktr_narg = narg;
110 	argp = &ktp->ktr_args[0];
111 	for (i = 0; i < narg; i++)
112 		*argp++ = args[i];
113 	kth->ktr_buf = (caddr_t)ktp;
114 	kth->ktr_len = len;
115 	ktrwrite(lp, kth, NULL);
116 	FREE(ktp, M_KTRACE);
117 	FREE(kth, M_KTRACE);
118 	lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
119 }
120 
121 void
122 ktrsysret(struct lwp *lp, int code, int error, register_t retval)
123 {
124 	struct ktr_header *kth;
125 	struct ktr_sysret ktp;
126 
127 	lp->lwp_traceflag |= KTRFAC_ACTIVE;
128 	kth = ktrgetheader(KTR_SYSRET);
129 	ktp.ktr_code = code;
130 	ktp.ktr_error = error;
131 	ktp.ktr_retval = retval;		/* what about val2 ? */
132 
133 	kth->ktr_buf = (caddr_t)&ktp;
134 	kth->ktr_len = sizeof(struct ktr_sysret);
135 
136 	ktrwrite(lp, kth, NULL);
137 	FREE(kth, M_KTRACE);
138 	lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
139 }
140 
141 void
142 ktrnamei(struct lwp *lp, char *path)
143 {
144 	struct ktr_header *kth;
145 
146 	lp->lwp_traceflag |= KTRFAC_ACTIVE;
147 	kth = ktrgetheader(KTR_NAMEI);
148 	kth->ktr_len = strlen(path);
149 	kth->ktr_buf = path;
150 
151 	ktrwrite(lp, kth, NULL);
152 	FREE(kth, M_KTRACE);
153 	lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
154 }
155 
156 void
157 ktrgenio(struct lwp *lp, int fd, enum uio_rw rw, struct uio *uio, int error)
158 {
159 	struct ktr_header *kth;
160 	struct ktr_genio ktg;
161 
162 	if (error)
163 		return;
164 	lp->lwp_traceflag |= KTRFAC_ACTIVE;
165 	kth = ktrgetheader(KTR_GENIO);
166 	ktg.ktr_fd = fd;
167 	ktg.ktr_rw = rw;
168 	kth->ktr_buf = (caddr_t)&ktg;
169 	kth->ktr_len = sizeof(struct ktr_genio);
170 	uio->uio_offset = 0;
171 	uio->uio_rw = UIO_WRITE;
172 
173 	ktrwrite(lp, kth, uio);
174 	FREE(kth, M_KTRACE);
175 	lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
176 }
177 
178 void
179 ktrpsig(struct lwp *lp, int sig, sig_t action, sigset_t *mask, int code)
180 {
181 	struct ktr_header *kth;
182 	struct ktr_psig	kp;
183 
184 	lp->lwp_traceflag |= KTRFAC_ACTIVE;
185 	kth = ktrgetheader(KTR_PSIG);
186 	kp.signo = (char)sig;
187 	kp.action = action;
188 	kp.mask = *mask;
189 	kp.code = code;
190 	kth->ktr_buf = (caddr_t)&kp;
191 	kth->ktr_len = sizeof (struct ktr_psig);
192 
193 	ktrwrite(lp, kth, NULL);
194 	FREE(kth, M_KTRACE);
195 	lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
196 }
197 
198 void
199 ktrcsw(struct lwp *lp, int out, int user)
200 {
201 	struct ktr_header *kth;
202 	struct	ktr_csw kc;
203 
204 	lp->lwp_traceflag |= KTRFAC_ACTIVE;
205 	kth = ktrgetheader(KTR_CSW);
206 	kc.out = out;
207 	kc.user = user;
208 	kth->ktr_buf = (caddr_t)&kc;
209 	kth->ktr_len = sizeof (struct ktr_csw);
210 
211 	ktrwrite(lp, kth, NULL);
212 	FREE(kth, M_KTRACE);
213 	lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
214 }
215 #endif
216 
217 /* Interface and common routines */
218 
219 #ifdef KTRACE
220 /*
221  * ktrace system call
222  */
223 struct ktrace_clear_info {
224 	ktrace_node_t tracenode;
225 	int rootclear;
226 	int error;
227 };
228 
229 static int ktrace_clear_callback(struct proc *p, void *data);
230 
231 #endif
232 
233 /*
234  * MPALMOSTSAFE
235  */
236 int
237 sys_ktrace(struct ktrace_args *uap)
238 {
239 #ifdef KTRACE
240 	struct ktrace_clear_info info;
241 	struct thread *td = curthread;
242 	struct proc *curp = td->td_proc;
243 	struct proc *p;
244 	struct pgrp *pg;
245 	int facs = uap->facs & ~KTRFAC_ROOT;
246 	int ops = KTROP(uap->ops);
247 	int descend = uap->ops & KTRFLAG_DESCEND;
248 	int ret = 0;
249 	int error = 0;
250 	struct nlookupdata nd;
251 	ktrace_node_t tracenode = NULL;
252 
253 	get_mplock();
254 	curp->p_traceflag |= KTRFAC_ACTIVE;
255 	if (ops != KTROP_CLEAR) {
256 		/*
257 		 * an operation which requires a file argument.
258 		 */
259 		error = nlookup_init(&nd, uap->fname,
260 					UIO_USERSPACE, NLC_LOCKVP);
261 		if (error == 0)
262 			error = vn_open(&nd, NULL, FREAD|FWRITE|O_NOFOLLOW, 0);
263 		if (error == 0 && nd.nl_open_vp->v_type != VREG)
264 			error = EACCES;
265 		if (error) {
266 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
267 			nlookup_done(&nd);
268 			goto done;
269 		}
270 		MALLOC(tracenode, ktrace_node_t, sizeof (struct ktrace_node),
271 		       M_KTRACE, M_WAITOK | M_ZERO);
272 		tracenode->kn_vp = nd.nl_open_vp;
273 		tracenode->kn_refs = 1;
274 		nd.nl_open_vp = NULL;
275 		nlookup_done(&nd);
276 		vn_unlock(tracenode->kn_vp);
277 	}
278 	/*
279 	 * Clear all uses of the tracefile.  Not the most efficient operation
280 	 * in the world.
281 	 */
282 	if (ops == KTROP_CLEARFILE) {
283 		info.tracenode = tracenode;
284 		info.error = 0;
285 		info.rootclear = 0;
286 		allproc_scan(ktrace_clear_callback, &info);
287 		error = info.error;
288 		goto done;
289 	}
290 	/*
291 	 * need something to (un)trace (XXX - why is this here?)
292 	 */
293 	if (!facs) {
294 		error = EINVAL;
295 		goto done;
296 	}
297 	/*
298 	 * do it
299 	 */
300 	if (uap->pid < 0) {
301 		/*
302 		 * by process group
303 		 */
304 		pg = pgfind(-uap->pid);
305 		if (pg == NULL) {
306 			error = ESRCH;
307 			goto done;
308 		}
309 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
310 			if (descend)
311 				ret |= ktrsetchildren(td, p, ops, facs, tracenode);
312 			else
313 				ret |= ktrops(td, p, ops, facs, tracenode);
314 		}
315 	} else {
316 		/*
317 		 * by pid
318 		 */
319 		p = pfind(uap->pid);
320 		if (p == NULL) {
321 			error = ESRCH;
322 			goto done;
323 		}
324 		if (descend)
325 			ret |= ktrsetchildren(td, p, ops, facs, tracenode);
326 		else
327 			ret |= ktrops(td, p, ops, facs, tracenode);
328 	}
329 	if (!ret)
330 		error = EPERM;
331 done:
332 	if (tracenode)
333 		ktrdestroy(&tracenode);
334 	curp->p_traceflag &= ~KTRFAC_ACTIVE;
335 	rel_mplock();
336 	return (error);
337 #else
338 	return ENOSYS;
339 #endif
340 }
341 
342 #ifdef KTRACE
343 
344 /*
345  * NOTE: NOT MPSAFE (yet)
346  */
347 static int
348 ktrace_clear_callback(struct proc *p, void *data)
349 {
350 	struct ktrace_clear_info *info = data;
351 
352 	if (p->p_tracenode) {
353 		if (info->rootclear) {
354 			if (p->p_tracenode == info->tracenode) {
355 				ktrdestroy(&p->p_tracenode);
356 				p->p_traceflag = 0;
357 			}
358 		} else {
359 			if (p->p_tracenode->kn_vp == info->tracenode->kn_vp) {
360 				if (ktrcanset(curthread, p)) {
361 					ktrdestroy(&p->p_tracenode);
362 					p->p_traceflag = 0;
363 				} else {
364 					info->error = EPERM;
365 				}
366 			}
367 		}
368 	}
369 	return(0);
370 }
371 
372 #endif
373 
374 /*
375  * utrace system call
376  *
377  * MPALMOSTSAFE
378  */
379 int
380 sys_utrace(struct utrace_args *uap)
381 {
382 #ifdef KTRACE
383 	struct ktr_header *kth;
384 	struct thread *td = curthread;	/* XXX */
385 	caddr_t cp;
386 
387 	if (!KTRPOINT(td, KTR_USER))
388 		return (0);
389 	if (uap->len > KTR_USER_MAXLEN)
390 		return (EINVAL);
391 	td->td_lwp->lwp_traceflag |= KTRFAC_ACTIVE;
392 	kth = ktrgetheader(KTR_USER);
393 	MALLOC(cp, caddr_t, uap->len, M_KTRACE, M_WAITOK);
394 	if (!copyin(uap->addr, cp, uap->len)) {
395 		kth->ktr_buf = cp;
396 		kth->ktr_len = uap->len;
397 		get_mplock();
398 		ktrwrite(td->td_lwp, kth, NULL);
399 		rel_mplock();
400 	}
401 	FREE(kth, M_KTRACE);
402 	FREE(cp, M_KTRACE);
403 	td->td_lwp->lwp_traceflag &= ~KTRFAC_ACTIVE;
404 
405 	return (0);
406 #else
407 	return (ENOSYS);
408 #endif
409 }
410 
411 void
412 ktrdestroy(struct ktrace_node **tracenodep)
413 {
414 	ktrace_node_t tracenode;
415 
416 	if ((tracenode = *tracenodep) != NULL) {
417 		*tracenodep = NULL;
418 		KKASSERT(tracenode->kn_refs > 0);
419 		/* XXX not MP safe yet */
420 		--tracenode->kn_refs;
421 		if (tracenode->kn_refs == 0) {
422 			vn_close(tracenode->kn_vp, FREAD|FWRITE);
423 			tracenode->kn_vp = NULL;
424 			FREE(tracenode, M_KTRACE);
425 		}
426 	}
427 }
428 
429 /*
430  * This allows a process to inherit a ref on a tracenode and is also used
431  * as a temporary ref to prevent a tracenode from being destroyed out from
432  * under an active operation.
433  */
434 ktrace_node_t
435 ktrinherit(ktrace_node_t tracenode)
436 {
437 	if (tracenode) {
438 		KKASSERT(tracenode->kn_refs > 0);
439 		++tracenode->kn_refs;
440 	}
441 	return(tracenode);
442 }
443 
444 #ifdef KTRACE
445 static int
446 ktrops(struct thread *td, struct proc *p, int ops, int facs,
447        ktrace_node_t tracenode)
448 {
449 	ktrace_node_t oldnode;
450 
451 	if (!ktrcanset(td, p))
452 		return (0);
453 	if (ops == KTROP_SET) {
454 		if ((oldnode = p->p_tracenode) != tracenode) {
455 			p->p_tracenode = ktrinherit(tracenode);
456 			ktrdestroy(&oldnode);
457 		}
458 		p->p_traceflag |= facs;
459 		if (td->td_ucred->cr_uid == 0)
460 			p->p_traceflag |= KTRFAC_ROOT;
461 	} else {
462 		/* KTROP_CLEAR */
463 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
464 			/* no more tracing */
465 			p->p_traceflag = 0;
466 			ktrdestroy(&p->p_tracenode);
467 		}
468 	}
469 
470 	return (1);
471 }
472 
473 static int
474 ktrsetchildren(struct thread *td, struct proc *top, int ops, int facs,
475 	       ktrace_node_t tracenode)
476 {
477 	struct proc *p;
478 	int ret = 0;
479 
480 	p = top;
481 	for (;;) {
482 		ret |= ktrops(td, p, ops, facs, tracenode);
483 		/*
484 		 * If this process has children, descend to them next,
485 		 * otherwise do any siblings, and if done with this level,
486 		 * follow back up the tree (but not past top).
487 		 */
488 		if (!LIST_EMPTY(&p->p_children))
489 			p = LIST_FIRST(&p->p_children);
490 		else for (;;) {
491 			if (p == top)
492 				return (ret);
493 			if (LIST_NEXT(p, p_sibling)) {
494 				p = LIST_NEXT(p, p_sibling);
495 				break;
496 			}
497 			p = p->p_pptr;
498 		}
499 	}
500 	/*NOTREACHED*/
501 }
502 
503 static void
504 ktrwrite(struct lwp *lp, struct ktr_header *kth, struct uio *uio)
505 {
506 	struct ktrace_clear_info info;
507 	struct uio auio;
508 	struct iovec aiov[2];
509 	int error;
510 	ktrace_node_t tracenode;
511 
512 	/*
513 	 * We have to ref our tracenode to prevent it from being ripped out
514 	 * from under us while we are trying to use it.   p_tracenode can
515 	 * go away at any time if another process gets a write error.
516 	 *
517 	 * XXX not MP safe
518 	 */
519 	if (lp->lwp_proc->p_tracenode == NULL)
520 		return;
521 	tracenode = ktrinherit(lp->lwp_proc->p_tracenode);
522 	auio.uio_iov = &aiov[0];
523 	auio.uio_offset = 0;
524 	auio.uio_segflg = UIO_SYSSPACE;
525 	auio.uio_rw = UIO_WRITE;
526 	aiov[0].iov_base = (caddr_t)kth;
527 	aiov[0].iov_len = sizeof(struct ktr_header);
528 	auio.uio_resid = sizeof(struct ktr_header);
529 	auio.uio_iovcnt = 1;
530 	auio.uio_td = curthread;
531 	if (kth->ktr_len > 0) {
532 		auio.uio_iovcnt++;
533 		aiov[1].iov_base = kth->ktr_buf;
534 		aiov[1].iov_len = kth->ktr_len;
535 		auio.uio_resid += kth->ktr_len;
536 		if (uio != NULL)
537 			kth->ktr_len += uio->uio_resid;
538 	}
539 	vn_lock(tracenode->kn_vp, LK_EXCLUSIVE | LK_RETRY);
540 	error = VOP_WRITE(tracenode->kn_vp, &auio,
541 			  IO_UNIT | IO_APPEND, lp->lwp_thread->td_ucred);
542 	if (error == 0 && uio != NULL) {
543 		error = VOP_WRITE(tracenode->kn_vp, uio,
544 			      IO_UNIT | IO_APPEND, lp->lwp_thread->td_ucred);
545 	}
546 	vn_unlock(tracenode->kn_vp);
547 	if (error) {
548 		/*
549 		 * If an error occured, give up tracing on all processes
550 		 * using this tracenode.  This is not MP safe but is
551 		 * blocking-safe.
552 		 */
553 		log(LOG_NOTICE,
554 		    "ktrace write failed, errno %d, tracing stopped\n", error);
555 		info.tracenode = tracenode;
556 		info.error = 0;
557 		info.rootclear = 1;
558 		allproc_scan(ktrace_clear_callback, &info);
559 	}
560 	ktrdestroy(&tracenode);
561 }
562 
563 /*
564  * Return true if caller has permission to set the ktracing state
565  * of target.  Essentially, the target can't possess any
566  * more permissions than the caller.  KTRFAC_ROOT signifies that
567  * root previously set the tracing status on the target process, and
568  * so, only root may further change it.
569  *
570  * TODO: check groups.  use caller effective gid.
571  */
572 static int
573 ktrcanset(struct thread *calltd, struct proc *targetp)
574 {
575 	struct ucred *caller = calltd->td_ucred;
576 	struct ucred *target = targetp->p_ucred;
577 
578 	if (!PRISON_CHECK(caller, target))
579 		return (0);
580 	if ((caller->cr_uid == target->cr_ruid &&
581 	     target->cr_ruid == target->cr_svuid &&
582 	     caller->cr_rgid == target->cr_rgid &&	/* XXX */
583 	     target->cr_rgid == target->cr_svgid &&
584 	     (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
585 	     (targetp->p_flag & P_SUGID) == 0) ||
586 	     caller->cr_uid == 0)
587 		return (1);
588 
589 	return (0);
590 }
591 
592 #endif /* KTRACE */
593