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