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