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