kern_ktrace.c revision 1.27 1 /* $NetBSD: kern_ktrace.c,v 1.27 1998/04/28 22:27:22 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)kern_ktrace.c 8.5 (Berkeley) 5/14/95
36 */
37
38 #ifdef KTRACE
39
40 #include <sys/param.h>
41 #include <sys/systm.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 #include <sys/mount.h>
51 #include <sys/syscallargs.h>
52
53 struct ktr_header *ktrgetheader __P((int));
54 int ktrops __P((struct proc *, struct proc *, int, int, struct vnode *));
55 int ktrsetchildren __P((struct proc *, struct proc *, int, int,
56 struct vnode *));
57 void ktrwrite __P((struct vnode *, struct ktr_header *));
58 int ktrcanset __P((struct proc *, struct proc *));
59
60 struct ktr_header *
61 ktrgetheader(type)
62 int type;
63 {
64 register struct ktr_header *kth;
65 struct proc *p = curproc; /* XXX */
66
67 MALLOC(kth, struct ktr_header *, sizeof (struct ktr_header),
68 M_TEMP, M_WAITOK);
69 kth->ktr_type = type;
70 microtime(&kth->ktr_time);
71 kth->ktr_pid = p->p_pid;
72 bcopy(p->p_comm, kth->ktr_comm, MAXCOMLEN);
73 return (kth);
74 }
75
76 void
77 ktrsyscall(vp, code, argsize, args)
78 struct vnode *vp;
79 register_t code;
80 size_t argsize;
81 register_t args[];
82 {
83 struct ktr_header *kth;
84 struct ktr_syscall *ktp;
85 struct proc *p = curproc; /* XXX */
86 register_t *argp;
87 int len = sizeof(struct ktr_syscall) + argsize;
88 int i;
89
90 p->p_traceflag |= KTRFAC_ACTIVE;
91 kth = ktrgetheader(KTR_SYSCALL);
92 MALLOC(ktp, struct ktr_syscall *, len, M_TEMP, M_WAITOK);
93 ktp->ktr_code = code;
94 ktp->ktr_argsize = argsize;
95 argp = (register_t *)((char *)ktp + sizeof(struct ktr_syscall));
96 for (i = 0; i < (argsize / sizeof *argp); i++)
97 *argp++ = args[i];
98 kth->ktr_buf = (caddr_t)ktp;
99 kth->ktr_len = len;
100 ktrwrite(vp, kth);
101 FREE(ktp, M_TEMP);
102 FREE(kth, M_TEMP);
103 p->p_traceflag &= ~KTRFAC_ACTIVE;
104 }
105
106 void
107 ktrsysret(vp, code, error, retval)
108 struct vnode *vp;
109 register_t code;
110 int error;
111 register_t retval;
112 {
113 struct ktr_header *kth;
114 struct ktr_sysret ktp;
115 struct proc *p = curproc; /* XXX */
116
117 p->p_traceflag |= KTRFAC_ACTIVE;
118 kth = ktrgetheader(KTR_SYSRET);
119 ktp.ktr_code = code;
120 ktp.ktr_error = error;
121 ktp.ktr_retval = retval; /* what about val2 ? */
122
123 kth->ktr_buf = (caddr_t)&ktp;
124 kth->ktr_len = sizeof(struct ktr_sysret);
125
126 ktrwrite(vp, kth);
127 FREE(kth, M_TEMP);
128 p->p_traceflag &= ~KTRFAC_ACTIVE;
129 }
130
131 void
132 ktrnamei(vp, path)
133 struct vnode *vp;
134 char *path;
135 {
136 struct ktr_header *kth;
137 struct proc *p = curproc; /* XXX */
138
139 p->p_traceflag |= KTRFAC_ACTIVE;
140 kth = ktrgetheader(KTR_NAMEI);
141 kth->ktr_len = strlen(path);
142 kth->ktr_buf = path;
143
144 ktrwrite(vp, kth);
145 FREE(kth, M_TEMP);
146 p->p_traceflag &= ~KTRFAC_ACTIVE;
147 }
148
149 void
150 ktremul(vp, emul)
151 struct vnode *vp;
152 char *emul;
153 {
154 struct ktr_header *kth;
155 struct proc *p = curproc; /* XXX */
156
157 p->p_traceflag |= KTRFAC_ACTIVE;
158 kth = ktrgetheader(KTR_EMUL);
159 kth->ktr_len = strlen(emul);
160 kth->ktr_buf = emul;
161
162 ktrwrite(vp, kth);
163 FREE(kth, M_TEMP);
164 p->p_traceflag &= ~KTRFAC_ACTIVE;
165 }
166
167 void
168 ktrgenio(vp, fd, rw, iov, len, error)
169 struct vnode *vp;
170 int fd;
171 enum uio_rw rw;
172 register struct iovec *iov;
173 int len, error;
174 {
175 struct ktr_header *kth;
176 register struct ktr_genio *ktp;
177 register caddr_t cp;
178 register int resid = len, cnt;
179 struct proc *p = curproc; /* XXX */
180
181 if (error)
182 return;
183 p->p_traceflag |= KTRFAC_ACTIVE;
184 kth = ktrgetheader(KTR_GENIO);
185 MALLOC(ktp, struct ktr_genio *, sizeof(struct ktr_genio) + len,
186 M_TEMP, M_WAITOK);
187 ktp->ktr_fd = fd;
188 ktp->ktr_rw = rw;
189 cp = (caddr_t)((char *)ktp + sizeof (struct ktr_genio));
190 while (resid > 0) {
191 if ((cnt = iov->iov_len) > resid)
192 cnt = resid;
193 if (copyin(iov->iov_base, cp, (unsigned)cnt))
194 goto done;
195 cp += cnt;
196 resid -= cnt;
197 iov++;
198 }
199 kth->ktr_buf = (caddr_t)ktp;
200 kth->ktr_len = sizeof (struct ktr_genio) + len;
201
202 ktrwrite(vp, kth);
203 done:
204 FREE(kth, M_TEMP);
205 FREE(ktp, M_TEMP);
206 p->p_traceflag &= ~KTRFAC_ACTIVE;
207 }
208
209 void
210 ktrpsig(vp, sig, action, mask, code)
211 struct vnode *vp;
212 int sig;
213 sig_t action;
214 int mask, code;
215 {
216 struct ktr_header *kth;
217 struct ktr_psig kp;
218 struct proc *p = curproc; /* XXX */
219
220 p->p_traceflag |= KTRFAC_ACTIVE;
221 kth = ktrgetheader(KTR_PSIG);
222 kp.signo = (char)sig;
223 kp.action = action;
224 kp.mask = mask;
225 kp.code = code;
226 kth->ktr_buf = (caddr_t)&kp;
227 kth->ktr_len = sizeof (struct ktr_psig);
228
229 ktrwrite(vp, kth);
230 FREE(kth, M_TEMP);
231 p->p_traceflag &= ~KTRFAC_ACTIVE;
232 }
233
234 void
235 ktrcsw(vp, out, user)
236 struct vnode *vp;
237 int out, user;
238 {
239 struct ktr_header *kth;
240 struct ktr_csw kc;
241 struct proc *p = curproc; /* XXX */
242
243 p->p_traceflag |= KTRFAC_ACTIVE;
244 kth = ktrgetheader(KTR_CSW);
245 kc.out = out;
246 kc.user = user;
247 kth->ktr_buf = (caddr_t)&kc;
248 kth->ktr_len = sizeof (struct ktr_csw);
249
250 ktrwrite(vp, kth);
251 FREE(kth, M_TEMP);
252 p->p_traceflag &= ~KTRFAC_ACTIVE;
253 }
254
255 /* Interface and common routines */
256
257 /*
258 * ktrace system call
259 */
260 /* ARGSUSED */
261 int
262 sys_ktrace(curp, v, retval)
263 struct proc *curp;
264 void *v;
265 register_t *retval;
266 {
267 register struct sys_ktrace_args /* {
268 syscallarg(const char *) fname;
269 syscallarg(int) ops;
270 syscallarg(int) facs;
271 syscallarg(int) pid;
272 } */ *uap = v;
273 register struct vnode *vp = NULL;
274 register struct proc *p;
275 struct pgrp *pg;
276 int facs = SCARG(uap, facs) & ~((unsigned) KTRFAC_ROOT);
277 int ops = KTROP(SCARG(uap, ops));
278 int descend = SCARG(uap, ops) & KTRFLAG_DESCEND;
279 int ret = 0;
280 int error = 0;
281 struct nameidata nd;
282
283 curp->p_traceflag |= KTRFAC_ACTIVE;
284 if (ops != KTROP_CLEAR) {
285 /*
286 * an operation which requires a file argument.
287 */
288 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
289 curp);
290 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
291 curp->p_traceflag &= ~KTRFAC_ACTIVE;
292 return (error);
293 }
294 vp = nd.ni_vp;
295 VOP_UNLOCK(vp, 0);
296 if (vp->v_type != VREG) {
297 (void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
298 curp->p_traceflag &= ~KTRFAC_ACTIVE;
299 return (EACCES);
300 }
301 }
302 /*
303 * Clear all uses of the tracefile
304 */
305 if (ops == KTROP_CLEARFILE) {
306 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
307 if (p->p_tracep == vp &&
308 !ktrops(curp, p, KTROP_CLEAR, ~0, vp))
309 error = EPERM;
310 }
311 goto done;
312 }
313 /*
314 * need something to (un)trace (XXX - why is this here?)
315 */
316 if (!facs) {
317 error = EINVAL;
318 goto done;
319 }
320 /*
321 * do it
322 */
323 if (SCARG(uap, pid) < 0) {
324 /*
325 * by process group
326 */
327 pg = pgfind(-SCARG(uap, pid));
328 if (pg == NULL) {
329 error = ESRCH;
330 goto done;
331 }
332 for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next)
333 if (descend)
334 ret |= ktrsetchildren(curp, p, ops, facs, vp);
335 else
336 ret |= ktrops(curp, p, ops, facs, vp);
337
338 } else {
339 /*
340 * by pid
341 */
342 p = pfind(SCARG(uap, pid));
343 if (p == NULL) {
344 error = ESRCH;
345 goto done;
346 }
347 if (descend)
348 ret |= ktrsetchildren(curp, p, ops, facs, vp);
349 else
350 ret |= ktrops(curp, p, ops, facs, vp);
351 }
352 if (!ret)
353 error = EPERM;
354 done:
355 if (vp != NULL)
356 (void) vn_close(vp, FWRITE, curp->p_ucred, curp);
357 curp->p_traceflag &= ~KTRFAC_ACTIVE;
358 return (error);
359 }
360
361 int
362 ktrops(curp, p, ops, facs, vp)
363 struct proc *p, *curp;
364 int ops, facs;
365 struct vnode *vp;
366 {
367
368 if (!ktrcanset(curp, p))
369 return (0);
370 if (ops == KTROP_SET) {
371 if (p->p_tracep != vp) {
372 /*
373 * if trace file already in use, relinquish
374 */
375 if (p->p_tracep != NULL)
376 vrele(p->p_tracep);
377 VREF(vp);
378 p->p_tracep = vp;
379 }
380 p->p_traceflag |= facs;
381 if (curp->p_ucred->cr_uid == 0)
382 p->p_traceflag |= KTRFAC_ROOT;
383 } else {
384 /* KTROP_CLEAR */
385 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
386 /* no more tracing */
387 p->p_traceflag = 0;
388 if (p->p_tracep != NULL) {
389 vrele(p->p_tracep);
390 p->p_tracep = NULL;
391 }
392 }
393 }
394
395 /*
396 * Emit an emulation record, every time there is a ktrace
397 * change/attach request.
398 */
399 if (KTRPOINT(p, KTR_EMUL))
400 ktremul(p->p_tracep, p->p_emul->e_name);
401
402 return (1);
403 }
404
405 int
406 ktrsetchildren(curp, top, ops, facs, vp)
407 struct proc *curp, *top;
408 int ops, facs;
409 struct vnode *vp;
410 {
411 register struct proc *p;
412 register int ret = 0;
413
414 p = top;
415 for (;;) {
416 ret |= ktrops(curp, p, ops, facs, vp);
417 /*
418 * If this process has children, descend to them next,
419 * otherwise do any siblings, and if done with this level,
420 * follow back up the tree (but not past top).
421 */
422 if (p->p_children.lh_first)
423 p = p->p_children.lh_first;
424 else for (;;) {
425 if (p == top)
426 return (ret);
427 if (p->p_sibling.le_next) {
428 p = p->p_sibling.le_next;
429 break;
430 }
431 p = p->p_pptr;
432 }
433 }
434 /*NOTREACHED*/
435 }
436
437 void
438 ktrwrite(vp, kth)
439 struct vnode *vp;
440 register struct ktr_header *kth;
441 {
442 struct uio auio;
443 struct iovec aiov[2];
444 struct proc *p = curproc;
445 int error;
446
447 if (vp == NULL)
448 return;
449 auio.uio_iov = &aiov[0];
450 auio.uio_offset = 0;
451 auio.uio_segflg = UIO_SYSSPACE;
452 auio.uio_rw = UIO_WRITE;
453 aiov[0].iov_base = (caddr_t)kth;
454 aiov[0].iov_len = sizeof(struct ktr_header);
455 auio.uio_resid = sizeof(struct ktr_header);
456 auio.uio_iovcnt = 1;
457 auio.uio_procp = (struct proc *)0;
458 if (kth->ktr_len > 0) {
459 auio.uio_iovcnt++;
460 aiov[1].iov_base = kth->ktr_buf;
461 aiov[1].iov_len = kth->ktr_len;
462 auio.uio_resid += kth->ktr_len;
463 }
464 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
465 error = VOP_WRITE(vp, &auio, IO_UNIT|IO_APPEND, p->p_ucred);
466 VOP_UNLOCK(vp, 0);
467 if (!error)
468 return;
469 /*
470 * If error encountered, give up tracing on this vnode.
471 */
472 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
473 error);
474 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
475 if (p->p_tracep == vp) {
476 p->p_tracep = NULL;
477 p->p_traceflag = 0;
478 vrele(vp);
479 }
480 }
481 }
482
483 /*
484 * Return true if caller has permission to set the ktracing state
485 * of target. Essentially, the target can't possess any
486 * more permissions than the caller. KTRFAC_ROOT signifies that
487 * root previously set the tracing status on the target process, and
488 * so, only root may further change it.
489 *
490 * TODO: check groups. use caller effective gid.
491 */
492 int
493 ktrcanset(callp, targetp)
494 struct proc *callp, *targetp;
495 {
496 register struct pcred *caller = callp->p_cred;
497 register struct pcred *target = targetp->p_cred;
498
499 if ((caller->pc_ucred->cr_uid == target->p_ruid &&
500 target->p_ruid == target->p_svuid &&
501 caller->p_rgid == target->p_rgid && /* XXX */
502 target->p_rgid == target->p_svgid &&
503 (targetp->p_traceflag & KTRFAC_ROOT) == 0) ||
504 caller->pc_ucred->cr_uid == 0)
505 return (1);
506
507 return (0);
508 }
509
510 #endif
511