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