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