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