xref: /dragonfly/sys/kern/kern_ktrace.c (revision 17b61719)
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.15 2004/05/21 15:41:23 drhodus 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/namei.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 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(int type)
67 {
68 	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(struct vnode *vp, int code, int narg, register_t args[])
82 {
83 	struct	ktr_header *kth;
84 	struct	ktr_syscall *ktp;
85 	int len = offsetof(struct ktr_syscall, ktr_args) +
86 	    (narg * sizeof(register_t));
87 	struct proc *p = curproc;	/* XXX */
88 	register_t *argp;
89 	int i;
90 
91 	p->p_traceflag |= KTRFAC_ACTIVE;
92 	kth = ktrgetheader(KTR_SYSCALL);
93 	MALLOC(ktp, struct ktr_syscall *, len, M_KTRACE, M_WAITOK);
94 	ktp->ktr_code = code;
95 	ktp->ktr_narg = narg;
96 	argp = &ktp->ktr_args[0];
97 	for (i = 0; i < narg; i++)
98 		*argp++ = args[i];
99 	kth->ktr_buf = (caddr_t)ktp;
100 	kth->ktr_len = len;
101 	ktrwrite(vp, kth, NULL);
102 	FREE(ktp, M_KTRACE);
103 	FREE(kth, M_KTRACE);
104 	p->p_traceflag &= ~KTRFAC_ACTIVE;
105 }
106 
107 void
108 ktrsysret(struct vnode *vp, int code, int error, register_t retval)
109 {
110 	struct ktr_header *kth;
111 	struct ktr_sysret ktp;
112 	struct proc *p = curproc;	/* XXX */
113 
114 	p->p_traceflag |= KTRFAC_ACTIVE;
115 	kth = ktrgetheader(KTR_SYSRET);
116 	ktp.ktr_code = code;
117 	ktp.ktr_error = error;
118 	ktp.ktr_retval = retval;		/* what about val2 ? */
119 
120 	kth->ktr_buf = (caddr_t)&ktp;
121 	kth->ktr_len = sizeof(struct ktr_sysret);
122 
123 	ktrwrite(vp, kth, NULL);
124 	FREE(kth, M_KTRACE);
125 	p->p_traceflag &= ~KTRFAC_ACTIVE;
126 }
127 
128 void
129 ktrnamei(struct vnode *vp, char *path)
130 {
131 	struct ktr_header *kth;
132 	struct proc *p = curproc;	/* XXX */
133 
134 	/*
135 	 * don't let vp get ripped out from under us
136 	 */
137 	if (vp)
138 		vref(vp);
139 	p->p_traceflag |= KTRFAC_ACTIVE;
140 	kth = ktrgetheader(KTR_NAMEI);
141 	kth->ktr_len = strlen(path);
142 	kth->ktr_buf = path;
143 
144 	ktrwrite(vp, kth, NULL);
145 	if (vp)
146 		vrele(vp);
147 	FREE(kth, M_KTRACE);
148 	p->p_traceflag &= ~KTRFAC_ACTIVE;
149 }
150 
151 void
152 ktrgenio(struct vnode *vp, int fd, enum uio_rw rw, struct uio *uio, int error)
153 {
154 	struct ktr_header *kth;
155 	struct ktr_genio ktg;
156 	struct proc *p = curproc;	/* XXX */
157 
158 	if (error)
159 		return;
160 	/*
161 	 * don't let p_tracep get ripped out from under us
162 	 */
163 	if (vp)
164 		vref(vp);
165 	p->p_traceflag |= KTRFAC_ACTIVE;
166 	kth = ktrgetheader(KTR_GENIO);
167 	ktg.ktr_fd = fd;
168 	ktg.ktr_rw = rw;
169 	kth->ktr_buf = (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 	if (vp)
176 		vrele(vp);
177 	FREE(kth, M_KTRACE);
178 	p->p_traceflag &= ~KTRFAC_ACTIVE;
179 }
180 
181 void
182 ktrpsig(struct vnode *vp, int sig, sig_t action, sigset_t *mask, int code)
183 {
184 	struct ktr_header *kth;
185 	struct ktr_psig	kp;
186 	struct proc *p = curproc;	/* XXX */
187 
188 	/*
189 	 * don't let vp get ripped out from under us
190 	 */
191 	if (vp)
192 		vref(vp);
193 	p->p_traceflag |= KTRFAC_ACTIVE;
194 	kth = ktrgetheader(KTR_PSIG);
195 	kp.signo = (char)sig;
196 	kp.action = action;
197 	kp.mask = *mask;
198 	kp.code = code;
199 	kth->ktr_buf = (caddr_t)&kp;
200 	kth->ktr_len = sizeof (struct ktr_psig);
201 
202 	ktrwrite(vp, kth, NULL);
203 	if (vp)
204 		vrele(vp);
205 	FREE(kth, M_KTRACE);
206 	p->p_traceflag &= ~KTRFAC_ACTIVE;
207 }
208 
209 void
210 ktrcsw(struct vnode *vp, int out, int user)
211 {
212 	struct ktr_header *kth;
213 	struct	ktr_csw kc;
214 	struct proc *p = curproc;	/* XXX */
215 
216 	/*
217 	 * don't let vp get ripped out from under us
218 	 */
219 	if (vp)
220 		vref(vp);
221 	p->p_traceflag |= KTRFAC_ACTIVE;
222 	kth = ktrgetheader(KTR_CSW);
223 	kc.out = out;
224 	kc.user = user;
225 	kth->ktr_buf = (caddr_t)&kc;
226 	kth->ktr_len = sizeof (struct ktr_csw);
227 
228 	ktrwrite(vp, kth, NULL);
229 	if (vp)
230 		vrele(vp);
231 	FREE(kth, M_KTRACE);
232 	p->p_traceflag &= ~KTRFAC_ACTIVE;
233 }
234 #endif
235 
236 /* Interface and common routines */
237 
238 /*
239  * ktrace system call
240  */
241 /* ARGSUSED */
242 int
243 ktrace(struct ktrace_args *uap)
244 {
245 #ifdef KTRACE
246 	struct thread *td = curthread;
247 	struct proc *curp = td->td_proc;
248 	struct vnode *vp = NULL;
249 	struct proc *p;
250 	struct pgrp *pg;
251 	int facs = uap->facs & ~KTRFAC_ROOT;
252 	int ops = KTROP(uap->ops);
253 	int descend = uap->ops & KTRFLAG_DESCEND;
254 	int ret = 0;
255 	int error = 0;
256 	struct nameidata nd;
257 
258 	curp->p_traceflag |= KTRFAC_ACTIVE;
259 	if (ops != KTROP_CLEAR) {
260 		/*
261 		 * an operation which requires a file argument.
262 		 */
263 		NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->fname, td);
264 		error = vn_open(&nd, FREAD|FWRITE|O_NOFOLLOW, 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, NULL, 0, td);
272 		if (vp->v_type != VREG) {
273 			(void) vn_close(vp, FREAD|FWRITE, td);
274 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
275 			return (EACCES);
276 		}
277 	}
278 	/*
279 	 * Clear all uses of the tracefile.  XXX umm, what happens to the
280 	 * loop if vn_close() blocks?
281 	 */
282 	if (ops == KTROP_CLEARFILE) {
283 		FOREACH_PROC_IN_SYSTEM(p) {
284 			if (p->p_tracep == vp) {
285 				if (ktrcanset(curp, p) && p->p_tracep == vp) {
286 					p->p_tracep = NULL;
287 					p->p_traceflag = 0;
288 					(void) vn_close(vp, FREAD|FWRITE, td);
289 				} else {
290 					error = EPERM;
291 				}
292 			}
293 		}
294 		goto done;
295 	}
296 	/*
297 	 * need something to (un)trace (XXX - why is this here?)
298 	 */
299 	if (!facs) {
300 		error = EINVAL;
301 		goto done;
302 	}
303 	/*
304 	 * do it
305 	 */
306 	if (uap->pid < 0) {
307 		/*
308 		 * by process group
309 		 */
310 		pg = pgfind(-uap->pid);
311 		if (pg == NULL) {
312 			error = ESRCH;
313 			goto done;
314 		}
315 		LIST_FOREACH(p, &pg->pg_members, p_pglist)
316 			if (descend)
317 				ret |= ktrsetchildren(curp, p, ops, facs, vp);
318 			else
319 				ret |= ktrops(curp, p, ops, facs, vp);
320 
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, td);
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(struct utrace_args *uap)
353 {
354 #ifdef KTRACE
355 	struct ktr_header *kth;
356 	struct thread *td = curthread;	/* XXX */
357 	struct proc *p = td->td_proc;
358 	struct vnode *vp;
359 	caddr_t cp;
360 
361 	if (!KTRPOINT(td, KTR_USER))
362 		return (0);
363 	if (SCARG(uap, len) > KTR_USER_MAXLEN)
364 		return (EINVAL);
365 	p->p_traceflag |= KTRFAC_ACTIVE;
366 	/*
367 	 * don't let p_tracep get ripped out from under us while we are
368 	 * writing.
369 	 */
370 	if ((vp = p->p_tracep) != NULL)
371 		vref(vp);
372 	kth = ktrgetheader(KTR_USER);
373 	MALLOC(cp, caddr_t, uap->len, M_KTRACE, M_WAITOK);
374 	if (!copyin(uap->addr, cp, uap->len)) {
375 		kth->ktr_buf = cp;
376 		kth->ktr_len = uap->len;
377 		ktrwrite(vp, kth, NULL);
378 	}
379 	if (vp)
380 		vrele(vp);
381 	FREE(kth, M_KTRACE);
382 	FREE(cp, M_KTRACE);
383 	p->p_traceflag &= ~KTRFAC_ACTIVE;
384 
385 	return (0);
386 #else
387 	return (ENOSYS);
388 #endif
389 }
390 
391 #ifdef KTRACE
392 static int
393 ktrops(struct proc *curp, struct proc *p, int ops, int facs, struct vnode *vp)
394 {
395 
396 	if (!ktrcanset(curp, p))
397 		return (0);
398 	if (ops == KTROP_SET) {
399 		if (p->p_tracep != vp) {
400 			struct vnode *vtmp;
401 
402 			/*
403 			 * if trace file already in use, relinquish
404 			 */
405 			vref(vp);
406 			while ((vtmp = p->p_tracep) != NULL) {
407 				p->p_tracep = NULL;
408 				vrele(vtmp);
409 			}
410 			p->p_tracep = vp;
411 		}
412 		p->p_traceflag |= facs;
413 		if (curp->p_ucred->cr_uid == 0)
414 			p->p_traceflag |= KTRFAC_ROOT;
415 	} else {
416 		/* KTROP_CLEAR */
417 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
418 			struct vnode *vtmp;
419 
420 			/* no more tracing */
421 			p->p_traceflag = 0;
422 			if ((vtmp = p->p_tracep) != NULL) {
423 				p->p_tracep = NULL;
424 				vrele(vtmp);
425 			}
426 		}
427 	}
428 
429 	return (1);
430 }
431 
432 static int
433 ktrsetchildren(struct proc *curp, struct proc *top, int ops, int facs,
434                struct vnode *vp)
435 {
436 	struct proc *p;
437 	int ret = 0;
438 
439 	p = top;
440 	for (;;) {
441 		ret |= ktrops(curp, p, ops, facs, vp);
442 		/*
443 		 * If this process has children, descend to them next,
444 		 * otherwise do any siblings, and if done with this level,
445 		 * follow back up the tree (but not past top).
446 		 */
447 		if (!LIST_EMPTY(&p->p_children))
448 			p = LIST_FIRST(&p->p_children);
449 		else for (;;) {
450 			if (p == top)
451 				return (ret);
452 			if (LIST_NEXT(p, p_sibling)) {
453 				p = LIST_NEXT(p, p_sibling);
454 				break;
455 			}
456 			p = p->p_pptr;
457 		}
458 	}
459 	/*NOTREACHED*/
460 }
461 
462 static void
463 ktrwrite(struct vnode *vp, struct ktr_header *kth, struct uio *uio)
464 {
465 	struct uio auio;
466 	struct iovec aiov[2];
467 	struct thread *td = curthread;
468 	struct proc *p = td->td_proc;	/* XXX */
469 	int error;
470 
471 	if (vp == NULL)
472 		return;
473 	auio.uio_iov = &aiov[0];
474 	auio.uio_offset = 0;
475 	auio.uio_segflg = UIO_SYSSPACE;
476 	auio.uio_rw = UIO_WRITE;
477 	aiov[0].iov_base = (caddr_t)kth;
478 	aiov[0].iov_len = sizeof(struct ktr_header);
479 	auio.uio_resid = sizeof(struct ktr_header);
480 	auio.uio_iovcnt = 1;
481 	auio.uio_td = curthread;
482 	if (kth->ktr_len > 0) {
483 		auio.uio_iovcnt++;
484 		aiov[1].iov_base = kth->ktr_buf;
485 		aiov[1].iov_len = kth->ktr_len;
486 		auio.uio_resid += kth->ktr_len;
487 		if (uio != NULL)
488 			kth->ktr_len += uio->uio_resid;
489 	}
490 	VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
491 	vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
492 	error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, p->p_ucred);
493 	if (error == 0 && uio != NULL) {
494 		VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
495 		error = VOP_WRITE(vp, uio, IO_UNIT | IO_APPEND, p->p_ucred);
496 	}
497 	VOP_UNLOCK(vp, NULL, 0, td);
498 	if (!error)
499 		return;
500 	/*
501 	 * If error encountered, give up tracing on this vnode.  XXX what
502 	 * happens to the loop if vrele() blocks?
503 	 */
504 	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
505 	    error);
506 	FOREACH_PROC_IN_SYSTEM(p) {
507 		if (p->p_tracep == vp) {
508 			p->p_tracep = NULL;
509 			p->p_traceflag = 0;
510 			vrele(vp);
511 		}
512 	}
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  * TODO: check groups.  use caller effective gid.
523  */
524 static int
525 ktrcanset(struct proc *callp, struct proc *targetp)
526 {
527 	struct ucred *caller = callp->p_ucred;
528 	struct ucred *target = targetp->p_ucred;
529 
530 	if (!PRISON_CHECK(caller, target))
531 		return (0);
532 	if ((caller->cr_uid == target->cr_ruid &&
533 	     target->cr_ruid == target->cr_svuid &&
534 	     caller->cr_rgid == target->cr_rgid &&	/* XXX */
535 	     target->cr_rgid == target->cr_svgid &&
536 	     (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
537 	     (targetp->p_flag & P_SUGID) == 0) ||
538 	     caller->cr_uid == 0)
539 		return (1);
540 
541 	return (0);
542 }
543 
544 #endif /* KTRACE */
545