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