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