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