kern_ktrace.c revision 1.1.1.3 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.5 (Berkeley) 5/14/95
34 */
35
36 #ifdef KTRACE
37
38 #include <sys/param.h>
39 #include <sys/systm.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 #include <sys/mount.h>
49 #include <sys/syscallargs.h>
50
51 struct ktr_header *
52 ktrgetheader(type)
53 int type;
54 {
55 register struct ktr_header *kth;
56 struct proc *p = curproc; /* XXX */
57
58 MALLOC(kth, struct ktr_header *, sizeof (struct ktr_header),
59 M_TEMP, M_WAITOK);
60 kth->ktr_type = type;
61 microtime(&kth->ktr_time);
62 kth->ktr_pid = p->p_pid;
63 bcopy(p->p_comm, kth->ktr_comm, MAXCOMLEN);
64 return (kth);
65 }
66
67 void
68 ktrsyscall(vp, code, argsize, args)
69 struct vnode *vp;
70 int code, argsize;
71 register_t args[];
72 {
73 struct ktr_header *kth;
74 struct ktr_syscall *ktp;
75 register len = sizeof(struct ktr_syscall) + argsize;
76 struct proc *p = curproc; /* XXX */
77 register_t *argp;
78 int i;
79
80 p->p_traceflag |= KTRFAC_ACTIVE;
81 kth = ktrgetheader(KTR_SYSCALL);
82 MALLOC(ktp, struct ktr_syscall *, len, M_TEMP, M_WAITOK);
83 ktp->ktr_code = code;
84 ktp->ktr_argsize = argsize;
85 argp = (register_t *)((char *)ktp + sizeof(struct ktr_syscall));
86 for (i = 0; i < (argsize / sizeof *argp); i++)
87 *argp++ = args[i];
88 kth->ktr_buf = (caddr_t)ktp;
89 kth->ktr_len = len;
90 ktrwrite(vp, kth);
91 FREE(ktp, M_TEMP);
92 FREE(kth, M_TEMP);
93 p->p_traceflag &= ~KTRFAC_ACTIVE;
94 }
95
96 void
97 ktrsysret(vp, code, error, retval)
98 struct vnode *vp;
99 int code, error, retval;
100 {
101 struct ktr_header *kth;
102 struct ktr_sysret ktp;
103 struct proc *p = curproc; /* XXX */
104
105 p->p_traceflag |= KTRFAC_ACTIVE;
106 kth = ktrgetheader(KTR_SYSRET);
107 ktp.ktr_code = code;
108 ktp.ktr_error = error;
109 ktp.ktr_retval = retval; /* what about val2 ? */
110
111 kth->ktr_buf = (caddr_t)&ktp;
112 kth->ktr_len = sizeof(struct ktr_sysret);
113
114 ktrwrite(vp, kth);
115 FREE(kth, M_TEMP);
116 p->p_traceflag &= ~KTRFAC_ACTIVE;
117 }
118
119 void
120 ktrnamei(vp, path)
121 struct vnode *vp;
122 char *path;
123 {
124 struct ktr_header *kth;
125 struct proc *p = curproc; /* XXX */
126
127 p->p_traceflag |= KTRFAC_ACTIVE;
128 kth = ktrgetheader(KTR_NAMEI);
129 kth->ktr_len = strlen(path);
130 kth->ktr_buf = path;
131
132 ktrwrite(vp, kth);
133 FREE(kth, M_TEMP);
134 p->p_traceflag &= ~KTRFAC_ACTIVE;
135 }
136
137 void
138 ktrgenio(vp, fd, rw, iov, len, error)
139 struct vnode *vp;
140 int fd;
141 enum uio_rw rw;
142 register struct iovec *iov;
143 int len, error;
144 {
145 struct ktr_header *kth;
146 register struct ktr_genio *ktp;
147 register caddr_t cp;
148 register int resid = len, cnt;
149 struct proc *p = curproc; /* XXX */
150
151 if (error)
152 return;
153 p->p_traceflag |= KTRFAC_ACTIVE;
154 kth = ktrgetheader(KTR_GENIO);
155 MALLOC(ktp, struct ktr_genio *, sizeof(struct ktr_genio) + len,
156 M_TEMP, M_WAITOK);
157 ktp->ktr_fd = fd;
158 ktp->ktr_rw = rw;
159 cp = (caddr_t)((char *)ktp + sizeof (struct ktr_genio));
160 while (resid > 0) {
161 if ((cnt = iov->iov_len) > resid)
162 cnt = resid;
163 if (copyin(iov->iov_base, cp, (unsigned)cnt))
164 goto done;
165 cp += cnt;
166 resid -= cnt;
167 iov++;
168 }
169 kth->ktr_buf = (caddr_t)ktp;
170 kth->ktr_len = sizeof (struct ktr_genio) + len;
171
172 ktrwrite(vp, kth);
173 done:
174 FREE(kth, M_TEMP);
175 FREE(ktp, M_TEMP);
176 p->p_traceflag &= ~KTRFAC_ACTIVE;
177 }
178
179 void
180 ktrpsig(vp, sig, action, mask, code)
181 struct vnode *vp;
182 int sig;
183 sig_t action;
184 int mask, code;
185 {
186 struct ktr_header *kth;
187 struct ktr_psig kp;
188 struct proc *p = curproc; /* XXX */
189
190 p->p_traceflag |= KTRFAC_ACTIVE;
191 kth = ktrgetheader(KTR_PSIG);
192 kp.signo = (char)sig;
193 kp.action = action;
194 kp.mask = mask;
195 kp.code = code;
196 kth->ktr_buf = (caddr_t)&kp;
197 kth->ktr_len = sizeof (struct ktr_psig);
198
199 ktrwrite(vp, kth);
200 FREE(kth, M_TEMP);
201 p->p_traceflag &= ~KTRFAC_ACTIVE;
202 }
203
204 void
205 ktrcsw(vp, out, user)
206 struct vnode *vp;
207 int out, user;
208 {
209 struct ktr_header *kth;
210 struct ktr_csw kc;
211 struct proc *p = curproc; /* XXX */
212
213 p->p_traceflag |= KTRFAC_ACTIVE;
214 kth = ktrgetheader(KTR_CSW);
215 kc.out = out;
216 kc.user = user;
217 kth->ktr_buf = (caddr_t)&kc;
218 kth->ktr_len = sizeof (struct ktr_csw);
219
220 ktrwrite(vp, kth);
221 FREE(kth, M_TEMP);
222 p->p_traceflag &= ~KTRFAC_ACTIVE;
223 }
224
225 /* Interface and common routines */
226
227 /*
228 * ktrace system call
229 */
230 /* ARGSUSED */
231 int
232 ktrace(curp, uap, retval)
233 struct proc *curp;
234 register struct ktrace_args /* {
235 syscallarg(char *) fname;
236 syscallarg(int) ops;
237 syscallarg(int) facs;
238 syscallarg(int) pid;
239 } */ *uap;
240 register_t *retval;
241 {
242 register struct vnode *vp = NULL;
243 register struct proc *p;
244 struct pgrp *pg;
245 int facs = SCARG(uap, facs) & ~KTRFAC_ROOT;
246 int ops = KTROP(SCARG(uap, ops));
247 int descend = SCARG(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, SCARG(uap, fname),
258 curp);
259 if (error = vn_open(&nd, FREAD|FWRITE, 0)) {
260 curp->p_traceflag &= ~KTRFAC_ACTIVE;
261 return (error);
262 }
263 vp = nd.ni_vp;
264 VOP_UNLOCK(vp, 0, p);
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 = allproc.lh_first; p != 0; p = p->p_list.le_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 (SCARG(uap, pid) < 0) {
299 /*
300 * by process group
301 */
302 pg = pgfind(-SCARG(uap, pid));
303 if (pg == NULL) {
304 error = ESRCH;
305 goto done;
306 }
307 for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next)
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(SCARG(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 ktrsetchildren(curp, top, ops, facs, vp)
374 struct proc *curp, *top;
375 int ops, facs;
376 struct vnode *vp;
377 {
378 register struct proc *p;
379 register int ret = 0;
380
381 p = top;
382 for (;;) {
383 ret |= ktrops(curp, p, ops, facs, vp);
384 /*
385 * If this process has children, descend to them next,
386 * otherwise do any siblings, and if done with this level,
387 * follow back up the tree (but not past top).
388 */
389 if (p->p_children.lh_first)
390 p = p->p_children.lh_first;
391 else for (;;) {
392 if (p == top)
393 return (ret);
394 if (p->p_sibling.le_next) {
395 p = p->p_sibling.le_next;
396 break;
397 }
398 p = p->p_pptr;
399 }
400 }
401 /*NOTREACHED*/
402 }
403
404 ktrwrite(vp, kth)
405 struct vnode *vp;
406 register struct ktr_header *kth;
407 {
408 struct uio auio;
409 struct iovec aiov[2];
410 register struct proc *p = curproc; /* XXX */
411 int error;
412
413 if (vp == NULL)
414 return;
415 auio.uio_iov = &aiov[0];
416 auio.uio_offset = 0;
417 auio.uio_segflg = UIO_SYSSPACE;
418 auio.uio_rw = UIO_WRITE;
419 aiov[0].iov_base = (caddr_t)kth;
420 aiov[0].iov_len = sizeof(struct ktr_header);
421 auio.uio_resid = sizeof(struct ktr_header);
422 auio.uio_iovcnt = 1;
423 auio.uio_procp = (struct proc *)0;
424 if (kth->ktr_len > 0) {
425 auio.uio_iovcnt++;
426 aiov[1].iov_base = kth->ktr_buf;
427 aiov[1].iov_len = kth->ktr_len;
428 auio.uio_resid += kth->ktr_len;
429 }
430 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
431 error = VOP_WRITE(vp, &auio, IO_UNIT|IO_APPEND, p->p_ucred);
432 VOP_UNLOCK(vp, 0, p);
433 if (!error)
434 return;
435 /*
436 * If error encountered, give up tracing on this vnode.
437 */
438 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
439 error);
440 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
441 if (p->p_tracep == vp) {
442 p->p_tracep = NULL;
443 p->p_traceflag = 0;
444 vrele(vp);
445 }
446 }
447 }
448
449 /*
450 * Return true if caller has permission to set the ktracing state
451 * of target. Essentially, the target can't possess any
452 * more permissions than the caller. KTRFAC_ROOT signifies that
453 * root previously set the tracing status on the target process, and
454 * so, only root may further change it.
455 *
456 * TODO: check groups. use caller effective gid.
457 */
458 ktrcanset(callp, targetp)
459 struct proc *callp, *targetp;
460 {
461 register struct pcred *caller = callp->p_cred;
462 register struct pcred *target = targetp->p_cred;
463
464 if ((caller->pc_ucred->cr_uid == target->p_ruid &&
465 target->p_ruid == target->p_svuid &&
466 caller->p_rgid == target->p_rgid && /* XXX */
467 target->p_rgid == target->p_svgid &&
468 (targetp->p_traceflag & KTRFAC_ROOT) == 0) ||
469 caller->pc_ucred->cr_uid == 0)
470 return (1);
471
472 return (0);
473 }
474
475 #endif
476