xref: /freebsd/sys/kern/kern_ktrace.c (revision 0fddbf874719b9bd50cf66ac26d1140bb3f2be69)
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 __P((int type));
59 static void ktrwrite __P((struct vnode *, struct ktr_header *, struct uio *));
60 static int ktrcanset __P((struct proc *,struct proc *));
61 static int ktrsetchildren __P((struct proc *,struct proc *,int,int,struct vnode *));
62 static int ktrops __P((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 	p->p_traceflag |= KTRFAC_ACTIVE;
154 	kth = ktrgetheader(KTR_NAMEI);
155 	kth->ktr_len = strlen(path);
156 	kth->ktr_buffer = path;
157 
158 	ktrwrite(vp, kth, NULL);
159 	FREE(kth, M_KTRACE);
160 	p->p_traceflag &= ~KTRFAC_ACTIVE;
161 }
162 
163 void
164 ktrgenio(vp, fd, rw, uio, error)
165 	struct vnode *vp;
166 	int fd;
167 	enum uio_rw rw;
168 	struct uio *uio;
169 	int error;
170 {
171 	struct ktr_header *kth;
172 	struct ktr_genio ktg;
173 	struct proc *p = curproc;	/* XXX */
174 
175 	if (error)
176 		return;
177 	p->p_traceflag |= KTRFAC_ACTIVE;
178 	kth = ktrgetheader(KTR_GENIO);
179 	ktg.ktr_fd = fd;
180 	ktg.ktr_rw = rw;
181 	kth->ktr_buffer = (caddr_t)&ktg;
182 	kth->ktr_len = sizeof(struct ktr_genio);
183 	uio->uio_offset = 0;
184 	uio->uio_rw = UIO_WRITE;
185 
186 	ktrwrite(vp, kth, uio);
187 	FREE(kth, M_KTRACE);
188 	p->p_traceflag &= ~KTRFAC_ACTIVE;
189 }
190 
191 void
192 ktrpsig(vp, sig, action, mask, code)
193 	struct vnode *vp;
194 	int sig;
195 	sig_t action;
196 	sigset_t *mask;
197 	int code;
198 {
199 	struct ktr_header *kth;
200 	struct ktr_psig	kp;
201 	struct proc *p = curproc;	/* XXX */
202 
203 	p->p_traceflag |= KTRFAC_ACTIVE;
204 	kth = ktrgetheader(KTR_PSIG);
205 	kp.signo = (char)sig;
206 	kp.action = action;
207 	kp.mask = *mask;
208 	kp.code = code;
209 	kth->ktr_buffer = (caddr_t)&kp;
210 	kth->ktr_len = sizeof (struct ktr_psig);
211 
212 	ktrwrite(vp, kth, NULL);
213 	FREE(kth, M_KTRACE);
214 	p->p_traceflag &= ~KTRFAC_ACTIVE;
215 }
216 
217 void
218 ktrcsw(vp, out, user)
219 	struct vnode *vp;
220 	int out, user;
221 {
222 	struct ktr_header *kth;
223 	struct	ktr_csw kc;
224 	struct proc *p = curproc;	/* XXX */
225 
226 	p->p_traceflag |= KTRFAC_ACTIVE;
227 	kth = ktrgetheader(KTR_CSW);
228 	kc.out = out;
229 	kc.user = user;
230 	kth->ktr_buffer = (caddr_t)&kc;
231 	kth->ktr_len = sizeof (struct ktr_csw);
232 
233 	ktrwrite(vp, kth, NULL);
234 	FREE(kth, M_KTRACE);
235 	p->p_traceflag &= ~KTRFAC_ACTIVE;
236 }
237 #endif
238 
239 /* Interface and common routines */
240 
241 /*
242  * ktrace system call
243  */
244 #ifndef _SYS_SYSPROTO_H_
245 struct ktrace_args {
246 	char	*fname;
247 	int	ops;
248 	int	facs;
249 	int	pid;
250 };
251 #endif
252 /* ARGSUSED */
253 int
254 ktrace(curp, uap)
255 	struct proc *curp;
256 	register struct ktrace_args *uap;
257 {
258 #ifdef KTRACE
259 	register struct vnode *vp = NULL;
260 	register struct proc *p;
261 	struct pgrp *pg;
262 	int facs = uap->facs & ~KTRFAC_ROOT;
263 	int ops = KTROP(uap->ops);
264 	int descend = uap->ops & KTRFLAG_DESCEND;
265 	int ret = 0;
266 	int flags, error = 0;
267 	struct nameidata nd;
268 
269 	curp->p_traceflag |= KTRFAC_ACTIVE;
270 	if (ops != KTROP_CLEAR) {
271 		/*
272 		 * an operation which requires a file argument.
273 		 */
274 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, curp);
275 		flags = FREAD | FWRITE | O_NOFOLLOW;
276 		error = vn_open(&nd, &flags, 0);
277 		if (error) {
278 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
279 			return (error);
280 		}
281 		NDFREE(&nd, NDF_ONLY_PNBUF);
282 		vp = nd.ni_vp;
283 		VOP_UNLOCK(vp, 0, curp);
284 		if (vp->v_type != VREG) {
285 			(void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
286 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
287 			return (EACCES);
288 		}
289 	}
290 	/*
291 	 * Clear all uses of the tracefile
292 	 */
293 	if (ops == KTROP_CLEARFILE) {
294 		sx_slock(&allproc_lock);
295 		LIST_FOREACH(p, &allproc, p_list) {
296 			if (p->p_tracep == vp) {
297 				if (ktrcanset(curp, p)) {
298 					p->p_tracep = NULL;
299 					p->p_traceflag = 0;
300 					(void) vn_close(vp, FREAD|FWRITE,
301 						p->p_ucred, p);
302 				} else
303 					error = EPERM;
304 			}
305 		}
306 		sx_sunlock(&allproc_lock);
307 		goto done;
308 	}
309 	/*
310 	 * need something to (un)trace (XXX - why is this here?)
311 	 */
312 	if (!facs) {
313 		error = EINVAL;
314 		goto done;
315 	}
316 	/*
317 	 * do it
318 	 */
319 	if (uap->pid < 0) {
320 		/*
321 		 * by process group
322 		 */
323 		pg = pgfind(-uap->pid);
324 		if (pg == NULL) {
325 			error = ESRCH;
326 			goto done;
327 		}
328 		LIST_FOREACH(p, &pg->pg_members, p_pglist)
329 			if (descend)
330 				ret |= ktrsetchildren(curp, p, ops, facs, vp);
331 			else
332 				ret |= ktrops(curp, p, ops, facs, vp);
333 	} else {
334 		/*
335 		 * by pid
336 		 */
337 		p = pfind(uap->pid);
338 		if (p == NULL) {
339 			error = ESRCH;
340 			goto done;
341 		}
342 		PROC_UNLOCK(p);
343 		if (descend)
344 			ret |= ktrsetchildren(curp, p, ops, facs, vp);
345 		else
346 			ret |= ktrops(curp, p, ops, facs, vp);
347 	}
348 	if (!ret)
349 		error = EPERM;
350 done:
351 	if (vp != NULL)
352 		(void) vn_close(vp, FWRITE, curp->p_ucred, curp);
353 	curp->p_traceflag &= ~KTRFAC_ACTIVE;
354 	return (error);
355 #else
356 	return ENOSYS;
357 #endif
358 }
359 
360 /*
361  * utrace system call
362  */
363 /* ARGSUSED */
364 int
365 utrace(curp, uap)
366 	struct proc *curp;
367 	register struct utrace_args *uap;
368 {
369 #ifdef KTRACE
370 	struct ktr_header *kth;
371 	struct proc *p = curproc;	/* XXX */
372 	register caddr_t cp;
373 
374 	if (!KTRPOINT(p, KTR_USER))
375 		return (0);
376 	if (uap->len > KTR_USER_MAXLEN)
377 		return (EINVAL);
378 	p->p_traceflag |= KTRFAC_ACTIVE;
379 	kth = ktrgetheader(KTR_USER);
380 	MALLOC(cp, caddr_t, uap->len, M_KTRACE, M_WAITOK);
381 	if (!copyin(uap->addr, cp, uap->len)) {
382 		kth->ktr_buffer = cp;
383 		kth->ktr_len = uap->len;
384 		ktrwrite(p->p_tracep, kth, NULL);
385 	}
386 	FREE(kth, M_KTRACE);
387 	FREE(cp, M_KTRACE);
388 	p->p_traceflag &= ~KTRFAC_ACTIVE;
389 
390 	return (0);
391 #else
392 	return (ENOSYS);
393 #endif
394 }
395 
396 #ifdef KTRACE
397 static int
398 ktrops(curp, p, ops, facs, vp)
399 	struct proc *p, *curp;
400 	int ops, facs;
401 	struct vnode *vp;
402 {
403 
404 	if (!ktrcanset(curp, p))
405 		return (0);
406 	if (ops == KTROP_SET) {
407 		if (p->p_tracep != vp) {
408 			/*
409 			 * if trace file already in use, relinquish
410 			 */
411 			if (p->p_tracep != NULL)
412 				vrele(p->p_tracep);
413 			VREF(vp);
414 			p->p_tracep = vp;
415 		}
416 		p->p_traceflag |= facs;
417 		if (curp->p_ucred->cr_uid == 0)
418 			p->p_traceflag |= KTRFAC_ROOT;
419 	} else {
420 		/* KTROP_CLEAR */
421 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
422 			/* no more tracing */
423 			p->p_traceflag = 0;
424 			if (p->p_tracep != NULL) {
425 				vrele(p->p_tracep);
426 				p->p_tracep = NULL;
427 			}
428 		}
429 	}
430 
431 	return (1);
432 }
433 
434 static int
435 ktrsetchildren(curp, top, ops, facs, vp)
436 	struct proc *curp, *top;
437 	int ops, facs;
438 	struct vnode *vp;
439 {
440 	register struct proc *p;
441 	register int ret = 0;
442 
443 	p = top;
444 	sx_slock(&proctree_lock);
445 	for (;;) {
446 		ret |= ktrops(curp, p, ops, facs, vp);
447 		/*
448 		 * If this process has children, descend to them next,
449 		 * otherwise do any siblings, and if done with this level,
450 		 * follow back up the tree (but not past top).
451 		 */
452 		if (!LIST_EMPTY(&p->p_children))
453 			p = LIST_FIRST(&p->p_children);
454 		else for (;;) {
455 			if (p == top) {
456 				sx_sunlock(&proctree_lock);
457 				return (ret);
458 			}
459 			if (LIST_NEXT(p, p_sibling)) {
460 				p = LIST_NEXT(p, p_sibling);
461 				break;
462 			}
463 			p = p->p_pptr;
464 		}
465 	}
466 	/*NOTREACHED*/
467 }
468 
469 static void
470 ktrwrite(vp, kth, uio)
471 	struct vnode *vp;
472 	register struct ktr_header *kth;
473 	struct uio *uio;
474 {
475 	struct uio auio;
476 	struct iovec aiov[2];
477 	struct proc *p = curproc;	/* XXX */
478 	struct mount *mp;
479 	int error;
480 
481 	if (vp == NULL)
482 		return;
483 	auio.uio_iov = &aiov[0];
484 	auio.uio_offset = 0;
485 	auio.uio_segflg = UIO_SYSSPACE;
486 	auio.uio_rw = UIO_WRITE;
487 	aiov[0].iov_base = (caddr_t)kth;
488 	aiov[0].iov_len = sizeof(struct ktr_header);
489 	auio.uio_resid = sizeof(struct ktr_header);
490 	auio.uio_iovcnt = 1;
491 	auio.uio_procp = curproc;
492 	if (kth->ktr_len > 0) {
493 		auio.uio_iovcnt++;
494 		aiov[1].iov_base = kth->ktr_buffer;
495 		aiov[1].iov_len = kth->ktr_len;
496 		auio.uio_resid += kth->ktr_len;
497 		if (uio != NULL)
498 			kth->ktr_len += uio->uio_resid;
499 	}
500 	vn_start_write(vp, &mp, V_WAIT);
501 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
502 	(void)VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
503 	error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, p->p_ucred);
504 	if (error == 0 && uio != NULL) {
505 		(void)VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
506 		error = VOP_WRITE(vp, uio, IO_UNIT | IO_APPEND, p->p_ucred);
507 	}
508 	VOP_UNLOCK(vp, 0, p);
509 	vn_finished_write(mp);
510 	if (!error)
511 		return;
512 	/*
513 	 * If error encountered, give up tracing on this vnode.
514 	 */
515 	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
516 	    error);
517 	sx_slock(&allproc_lock);
518 	LIST_FOREACH(p, &allproc, p_list) {
519 		if (p->p_tracep == vp) {
520 			p->p_tracep = NULL;
521 			p->p_traceflag = 0;
522 			vrele(vp);
523 		}
524 	}
525 	sx_sunlock(&allproc_lock);
526 }
527 
528 /*
529  * Return true if caller has permission to set the ktracing state
530  * of target.  Essentially, the target can't possess any
531  * more permissions than the caller.  KTRFAC_ROOT signifies that
532  * root previously set the tracing status on the target process, and
533  * so, only root may further change it.
534  */
535 static int
536 ktrcanset(callp, targetp)
537 	struct proc *callp, *targetp;
538 {
539 
540 	if (targetp->p_traceflag & KTRFAC_ROOT &&
541 	    suser_xxx(NULL, callp, PRISON_ROOT))
542 		return (0);
543 
544 	if (p_candebug(callp, targetp) != 0)
545 		return (0);
546 
547 	return (1);
548 }
549 
550 #endif /* KTRACE */
551