xref: /freebsd/sys/kern/kern_ktrace.c (revision c17d43407fe04133a94055b0dbc7ea8965654a9f)
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$
35  */
36 
37 #include "opt_ktrace.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/sysproto.h>
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/fcntl.h>
47 #include <sys/namei.h>
48 #include <sys/vnode.h>
49 #include <sys/ktrace.h>
50 #include <sys/malloc.h>
51 #include <sys/sx.h>
52 #include <sys/syslog.h>
53 #include <sys/jail.h>
54 
55 static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
56 
57 #ifdef KTRACE
58 static struct ktr_header *ktrgetheader(int type);
59 static void ktrwrite(struct vnode *, struct ktr_header *, struct uio *);
60 static int ktrcanset(struct proc *,struct proc *);
61 static int ktrsetchildren(struct proc *,struct proc *,int,int,struct vnode *);
62 static int ktrops(struct proc *,struct proc *,int,int,struct vnode *);
63 
64 
65 static struct ktr_header *
66 ktrgetheader(type)
67 	int type;
68 {
69 	register struct ktr_header *kth;
70 	struct proc *p = curproc;	/* XXX */
71 
72 	MALLOC(kth, struct ktr_header *, sizeof (struct ktr_header),
73 		M_KTRACE, M_WAITOK);
74 	kth->ktr_type = type;
75 	microtime(&kth->ktr_time);
76 	kth->ktr_pid = p->p_pid;
77 	bcopy(p->p_comm, kth->ktr_comm, MAXCOMLEN + 1);
78 	return (kth);
79 }
80 
81 /*
82  * MPSAFE
83  */
84 void
85 ktrsyscall(vp, code, narg, args)
86 	struct vnode *vp;
87 	int code, narg;
88 	register_t args[];
89 {
90 	struct	ktr_header *kth;
91 	struct	ktr_syscall *ktp;
92 	register int len = offsetof(struct ktr_syscall, ktr_args) +
93 	    (narg * sizeof(register_t));
94 	struct proc *p = curproc;	/* XXX */
95 	register_t *argp;
96 	int i;
97 
98 	mtx_lock(&Giant);
99 	p->p_traceflag |= KTRFAC_ACTIVE;
100 	kth = ktrgetheader(KTR_SYSCALL);
101 	MALLOC(ktp, struct ktr_syscall *, len, M_KTRACE, M_WAITOK);
102 	ktp->ktr_code = code;
103 	ktp->ktr_narg = narg;
104 	argp = &ktp->ktr_args[0];
105 	for (i = 0; i < narg; i++)
106 		*argp++ = args[i];
107 	kth->ktr_buffer = (caddr_t)ktp;
108 	kth->ktr_len = len;
109 	ktrwrite(vp, kth, NULL);
110 	FREE(ktp, M_KTRACE);
111 	FREE(kth, M_KTRACE);
112 	p->p_traceflag &= ~KTRFAC_ACTIVE;
113 	mtx_unlock(&Giant);
114 }
115 
116 /*
117  * MPSAFE
118  */
119 void
120 ktrsysret(vp, code, error, retval)
121 	struct vnode *vp;
122 	int code, error;
123 	register_t retval;
124 {
125 	struct ktr_header *kth;
126 	struct ktr_sysret ktp;
127 	struct proc *p = curproc;	/* XXX */
128 
129 	mtx_lock(&Giant);
130 	p->p_traceflag |= KTRFAC_ACTIVE;
131 	kth = ktrgetheader(KTR_SYSRET);
132 	ktp.ktr_code = code;
133 	ktp.ktr_error = error;
134 	ktp.ktr_retval = retval;		/* what about val2 ? */
135 
136 	kth->ktr_buffer = (caddr_t)&ktp;
137 	kth->ktr_len = sizeof(struct ktr_sysret);
138 
139 	ktrwrite(vp, kth, NULL);
140 	FREE(kth, M_KTRACE);
141 	p->p_traceflag &= ~KTRFAC_ACTIVE;
142 	mtx_unlock(&Giant);
143 }
144 
145 void
146 ktrnamei(vp, path)
147 	struct vnode *vp;
148 	char *path;
149 {
150 	struct ktr_header *kth;
151 	struct proc *p = curproc;	/* XXX */
152 
153 	/*
154 	 * don't let p_tracep get ripped out from under us
155 	 */
156 	if (vp)
157 		VREF(vp);
158 	p->p_traceflag |= KTRFAC_ACTIVE;
159 	kth = ktrgetheader(KTR_NAMEI);
160 	kth->ktr_len = strlen(path);
161 	kth->ktr_buffer = path;
162 
163 	ktrwrite(vp, kth, NULL);
164 	if (vp)
165 		vrele(vp);
166 	FREE(kth, M_KTRACE);
167 	p->p_traceflag &= ~KTRFAC_ACTIVE;
168 }
169 
170 void
171 ktrgenio(vp, fd, rw, uio, error)
172 	struct vnode *vp;
173 	int fd;
174 	enum uio_rw rw;
175 	struct uio *uio;
176 	int error;
177 {
178 	struct ktr_header *kth;
179 	struct ktr_genio ktg;
180 	struct proc *p = curproc;	/* XXX */
181 
182 	if (error)
183 		return;
184 
185 	mtx_lock(&Giant);
186 	/*
187 	 * don't let p_tracep get ripped out from under us
188 	 */
189 	if (vp)
190 		VREF(vp);
191 	p->p_traceflag |= KTRFAC_ACTIVE;
192 	kth = ktrgetheader(KTR_GENIO);
193 	ktg.ktr_fd = fd;
194 	ktg.ktr_rw = rw;
195 	kth->ktr_buffer = (caddr_t)&ktg;
196 	kth->ktr_len = sizeof(struct ktr_genio);
197 	uio->uio_offset = 0;
198 	uio->uio_rw = UIO_WRITE;
199 
200 	ktrwrite(vp, kth, uio);
201 	if (vp)
202 		vrele(vp);
203 	FREE(kth, M_KTRACE);
204 	p->p_traceflag &= ~KTRFAC_ACTIVE;
205 	mtx_unlock(&Giant);
206 }
207 
208 void
209 ktrpsig(vp, sig, action, mask, code)
210 	struct vnode *vp;
211 	int sig;
212 	sig_t action;
213 	sigset_t *mask;
214 	int code;
215 {
216 	struct ktr_header *kth;
217 	struct ktr_psig	kp;
218 	struct proc *p = curproc;	/* XXX */
219 
220 	/*
221 	 * don't let vp get ripped out from under us
222 	 */
223 	if (vp)
224 		VREF(vp);
225 	p->p_traceflag |= KTRFAC_ACTIVE;
226 	kth = ktrgetheader(KTR_PSIG);
227 	kp.signo = (char)sig;
228 	kp.action = action;
229 	kp.mask = *mask;
230 	kp.code = code;
231 	kth->ktr_buffer = (caddr_t)&kp;
232 	kth->ktr_len = sizeof (struct ktr_psig);
233 
234 	ktrwrite(vp, kth, NULL);
235 	if (vp)
236 		vrele(vp);
237 	FREE(kth, M_KTRACE);
238 	p->p_traceflag &= ~KTRFAC_ACTIVE;
239 }
240 
241 void
242 ktrcsw(vp, out, user)
243 	struct vnode *vp;
244 	int out, user;
245 {
246 	struct ktr_header *kth;
247 	struct	ktr_csw kc;
248 	struct proc *p = curproc;	/* XXX */
249 
250 	/*
251 	 * don't let vp get ripped out from under us
252 	 */
253 	if (vp)
254 		VREF(vp);
255 	p->p_traceflag |= KTRFAC_ACTIVE;
256 	kth = ktrgetheader(KTR_CSW);
257 	kc.out = out;
258 	kc.user = user;
259 	kth->ktr_buffer = (caddr_t)&kc;
260 	kth->ktr_len = sizeof (struct ktr_csw);
261 
262 	ktrwrite(vp, kth, NULL);
263 	if (vp)
264 		vrele(vp);
265 	FREE(kth, M_KTRACE);
266 	p->p_traceflag &= ~KTRFAC_ACTIVE;
267 }
268 #endif
269 
270 /* Interface and common routines */
271 
272 /*
273  * ktrace system call
274  */
275 #ifndef _SYS_SYSPROTO_H_
276 struct ktrace_args {
277 	char	*fname;
278 	int	ops;
279 	int	facs;
280 	int	pid;
281 };
282 #endif
283 /* ARGSUSED */
284 int
285 ktrace(td, uap)
286 	struct thread *td;
287 	register struct ktrace_args *uap;
288 {
289 #ifdef KTRACE
290 	struct proc *curp = td->td_proc;
291 	register struct vnode *vp = NULL;
292 	register struct proc *p;
293 	struct pgrp *pg;
294 	int facs = uap->facs & ~KTRFAC_ROOT;
295 	int ops = KTROP(uap->ops);
296 	int descend = uap->ops & KTRFLAG_DESCEND;
297 	int ret = 0;
298 	int flags, error = 0;
299 	struct nameidata nd;
300 
301 	curp->p_traceflag |= KTRFAC_ACTIVE;
302 	if (ops != KTROP_CLEAR) {
303 		/*
304 		 * an operation which requires a file argument.
305 		 */
306 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td);
307 		flags = FREAD | FWRITE | O_NOFOLLOW;
308 		error = vn_open(&nd, &flags, 0);
309 		if (error) {
310 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
311 			return (error);
312 		}
313 		NDFREE(&nd, NDF_ONLY_PNBUF);
314 		vp = nd.ni_vp;
315 		VOP_UNLOCK(vp, 0, td);
316 		if (vp->v_type != VREG) {
317 			(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
318 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
319 			return (EACCES);
320 		}
321 	}
322 	/*
323 	 * Clear all uses of the tracefile.
324 	 */
325 	if (ops == KTROP_CLEARFILE) {
326 		sx_slock(&allproc_lock);
327 		LIST_FOREACH(p, &allproc, p_list) {
328 			if (p->p_tracep == vp) {
329 				if (ktrcanset(curp, p) && p->p_tracep == vp) {
330 					p->p_tracep = NULL;
331 					p->p_traceflag = 0;
332 					(void) vn_close(vp, FREAD|FWRITE,
333 						td->td_ucred, td);
334 				} else {
335 					error = EPERM;
336 				}
337 			}
338 		}
339 		sx_sunlock(&allproc_lock);
340 		goto done;
341 	}
342 	/*
343 	 * need something to (un)trace (XXX - why is this here?)
344 	 */
345 	if (!facs) {
346 		error = EINVAL;
347 		goto done;
348 	}
349 	/*
350 	 * do it
351 	 */
352 	if (uap->pid < 0) {
353 		/*
354 		 * by process group
355 		 */
356 		PGRPSESS_SLOCK();
357 		pg = pgfind(-uap->pid);
358 		if (pg == NULL) {
359 			PGRPSESS_SUNLOCK();
360 			error = ESRCH;
361 			goto done;
362 		}
363 		/*
364 		 * ktrops() may call vrele(). Lock pg_members
365 		 * by the pgrpsess_lock rather than pg_mtx.
366 		 */
367 		PGRP_UNLOCK(pg);
368 		LIST_FOREACH(p, &pg->pg_members, p_pglist)
369 			if (descend)
370 				ret |= ktrsetchildren(curp, p, ops, facs, vp);
371 			else
372 				ret |= ktrops(curp, p, ops, facs, vp);
373 		PGRPSESS_SUNLOCK();
374 	} else {
375 		/*
376 		 * by pid
377 		 */
378 		p = pfind(uap->pid);
379 		if (p == NULL) {
380 			error = ESRCH;
381 			goto done;
382 		}
383 		PROC_UNLOCK(p);
384 		if (descend)
385 			ret |= ktrsetchildren(curp, p, ops, facs, vp);
386 		else
387 			ret |= ktrops(curp, p, ops, facs, vp);
388 	}
389 	if (!ret)
390 		error = EPERM;
391 done:
392 	if (vp != NULL)
393 		(void) vn_close(vp, FWRITE, td->td_ucred, td);
394 	curp->p_traceflag &= ~KTRFAC_ACTIVE;
395 	return (error);
396 #else
397 	return ENOSYS;
398 #endif
399 }
400 
401 /*
402  * utrace system call
403  */
404 /* ARGSUSED */
405 int
406 utrace(td, uap)
407 	struct thread *td;
408 	register struct utrace_args *uap;
409 {
410 
411 #ifdef KTRACE
412 	struct ktr_header *kth;
413 	struct proc *p = curproc;	/* XXX */
414 	struct vnode *vp;
415 	register caddr_t cp;
416 
417 	if (!KTRPOINT(p, KTR_USER))
418 		return (0);
419 	if (uap->len > KTR_USER_MAXLEN)
420 		return (EINVAL);
421 	p->p_traceflag |= KTRFAC_ACTIVE;
422 	if ((vp = p->p_tracep) != NULL)
423 		VREF(vp);
424 	kth = ktrgetheader(KTR_USER);
425 	MALLOC(cp, caddr_t, uap->len, M_KTRACE, M_WAITOK);
426 	if (!copyin(uap->addr, cp, uap->len)) {
427 		kth->ktr_buffer = cp;
428 		kth->ktr_len = uap->len;
429 		ktrwrite(vp, kth, NULL);
430 	}
431 	if (vp)
432 		vrele(vp);
433 	FREE(kth, M_KTRACE);
434 	FREE(cp, M_KTRACE);
435 	p->p_traceflag &= ~KTRFAC_ACTIVE;
436 
437 	return (0);
438 #else
439 	return (ENOSYS);
440 #endif
441 }
442 
443 #ifdef KTRACE
444 static int
445 ktrops(curp, p, ops, facs, vp)
446 	struct proc *p, *curp;
447 	int ops, facs;
448 	struct vnode *vp;
449 {
450 
451 	if (!ktrcanset(curp, p))
452 		return (0);
453 	if (ops == KTROP_SET) {
454 		if (p->p_tracep != vp) {
455 			struct vnode *vtmp;
456 
457 			/*
458 			 * if trace file already in use, relinquish
459 			 */
460 			VREF(vp);
461 			while ((vtmp = p->p_tracep) != NULL) {
462 				p->p_tracep = NULL;
463 				vrele(vtmp);
464 			}
465 			p->p_tracep = vp;
466 		}
467 		p->p_traceflag |= facs;
468 		/* XXX: Not safe */
469 		if (curp->p_ucred->cr_uid == 0)
470 			p->p_traceflag |= KTRFAC_ROOT;
471 	} else {
472 		/* KTROP_CLEAR */
473 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
474 			struct vnode *vtmp;
475 
476 			/* no more tracing */
477 			p->p_traceflag = 0;
478 			if ((vtmp = p->p_tracep) != NULL) {
479 				p->p_tracep = NULL;
480 				vrele(vtmp);
481 			}
482 		}
483 	}
484 
485 	return (1);
486 }
487 
488 static int
489 ktrsetchildren(curp, top, ops, facs, vp)
490 	struct proc *curp, *top;
491 	int ops, facs;
492 	struct vnode *vp;
493 {
494 	register struct proc *p;
495 	register int ret = 0;
496 
497 	p = top;
498 	sx_slock(&proctree_lock);
499 	for (;;) {
500 		ret |= ktrops(curp, p, ops, facs, vp);
501 		/*
502 		 * If this process has children, descend to them next,
503 		 * otherwise do any siblings, and if done with this level,
504 		 * follow back up the tree (but not past top).
505 		 */
506 		if (!LIST_EMPTY(&p->p_children))
507 			p = LIST_FIRST(&p->p_children);
508 		else for (;;) {
509 			if (p == top) {
510 				sx_sunlock(&proctree_lock);
511 				return (ret);
512 			}
513 			if (LIST_NEXT(p, p_sibling)) {
514 				p = LIST_NEXT(p, p_sibling);
515 				break;
516 			}
517 			p = p->p_pptr;
518 		}
519 	}
520 	/*NOTREACHED*/
521 }
522 
523 static void
524 ktrwrite(vp, kth, uio)
525 	struct vnode *vp;
526 	register struct ktr_header *kth;
527 	struct uio *uio;
528 {
529 	struct uio auio;
530 	struct iovec aiov[2];
531 	struct thread *td = curthread;	/* XXX */
532 	struct proc *p = td->td_proc;	/* XXX */
533 	struct mount *mp;
534 	int error;
535 
536 	if (vp == NULL)
537 		return;
538 	auio.uio_iov = &aiov[0];
539 	auio.uio_offset = 0;
540 	auio.uio_segflg = UIO_SYSSPACE;
541 	auio.uio_rw = UIO_WRITE;
542 	aiov[0].iov_base = (caddr_t)kth;
543 	aiov[0].iov_len = sizeof(struct ktr_header);
544 	auio.uio_resid = sizeof(struct ktr_header);
545 	auio.uio_iovcnt = 1;
546 	auio.uio_td = curthread;
547 	if (kth->ktr_len > 0) {
548 		auio.uio_iovcnt++;
549 		aiov[1].iov_base = kth->ktr_buffer;
550 		aiov[1].iov_len = kth->ktr_len;
551 		auio.uio_resid += kth->ktr_len;
552 		if (uio != NULL)
553 			kth->ktr_len += uio->uio_resid;
554 	}
555 	vn_start_write(vp, &mp, V_WAIT);
556 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
557 	(void)VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
558 	error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, td->td_ucred);
559 	if (error == 0 && uio != NULL) {
560 		(void)VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
561 		error = VOP_WRITE(vp, uio, IO_UNIT | IO_APPEND, td->td_ucred);
562 	}
563 	VOP_UNLOCK(vp, 0, td);
564 	vn_finished_write(mp);
565 	if (!error)
566 		return;
567 	/*
568 	 * If error encountered, give up tracing on this vnode.  XXX what
569 	 * happens to the loop if vrele() blocks?
570 	 */
571 	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
572 	    error);
573 	sx_slock(&allproc_lock);
574 	LIST_FOREACH(p, &allproc, p_list) {
575 		if (p->p_tracep == vp) {
576 			p->p_tracep = NULL;
577 			p->p_traceflag = 0;
578 			vrele(vp);
579 		}
580 	}
581 	sx_sunlock(&allproc_lock);
582 }
583 
584 /*
585  * Return true if caller has permission to set the ktracing state
586  * of target.  Essentially, the target can't possess any
587  * more permissions than the caller.  KTRFAC_ROOT signifies that
588  * root previously set the tracing status on the target process, and
589  * so, only root may further change it.
590  */
591 static int
592 ktrcanset(callp, targetp)
593 	struct proc *callp, *targetp;
594 {
595 
596 	if (targetp->p_traceflag & KTRFAC_ROOT &&
597 	    suser_xxx(NULL, callp, PRISON_ROOT))
598 		return (0);
599 
600 	if (p_candebug(callp, targetp) != 0)
601 		return (0);
602 
603 	return (1);
604 }
605 
606 #endif /* KTRACE */
607