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