Fix kernel panic on NULL pointer deref w/ certain ktrace commands, allow -c -f.
[dragonfly.git] / sys / kern / kern_ktrace.c
CommitLineData
984263bc
MD
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 * $FreeBSD: src/sys/kern/kern_ktrace.c,v 1.35.2.6 2002/07/05 22:36:38 darrenr Exp $
9fb04d14 35 * $DragonFly: src/sys/kern/kern_ktrace.c,v 1.30 2008/04/14 12:01:50 dillon Exp $
984263bc
MD
36 */
37
38#include "opt_ktrace.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/sysproto.h>
43#include <sys/kernel.h>
44#include <sys/proc.h>
45#include <sys/fcntl.h>
46#include <sys/lock.h>
fad57d0e 47#include <sys/nlookup.h>
984263bc
MD
48#include <sys/vnode.h>
49#include <sys/ktrace.h>
50#include <sys/malloc.h>
51#include <sys/syslog.h>
52#include <sys/sysent.h>
53
54#include <vm/vm_zone.h>
55static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
56
57#ifdef KTRACE
402ed7e1 58static struct ktr_header *ktrgetheader (int type);
a9b80e23 59static void ktrwrite (struct proc *, struct ktr_header *, struct uio *);
402ed7e1 60static int ktrcanset (struct proc *,struct proc *);
29f58392
MD
61static int ktrsetchildren (struct proc *,struct proc *,int,int, ktrace_node_t);
62static int ktrops (struct proc *,struct proc *,int,int, ktrace_node_t);
984263bc
MD
63
64static struct ktr_header *
2b9ccab7 65ktrgetheader(int type)
984263bc 66{
1fd87d54 67 struct ktr_header *kth;
984263bc 68 struct proc *p = curproc; /* XXX */
2af36526 69 struct lwp *lp = curthread->td_lwp;
984263bc
MD
70
71 MALLOC(kth, struct ktr_header *, sizeof (struct ktr_header),
72 M_KTRACE, M_WAITOK);
73 kth->ktr_type = type;
2af36526
MD
74 /* XXX threaded flag is a hack at the moment */
75 kth->ktr_flags = (p->p_nthreads > 1) ? KTRH_THREADED : 0;
984263bc
MD
76 microtime(&kth->ktr_time);
77 kth->ktr_pid = p->p_pid;
2af36526 78 kth->ktr_tid = lp->lwp_tid;
984263bc
MD
79 bcopy(p->p_comm, kth->ktr_comm, MAXCOMLEN + 1);
80 return (kth);
81}
82
83void
9fb04d14 84ktrsyscall(struct lwp *lp, int code, int narg, register_t args[])
984263bc
MD
85{
86 struct ktr_header *kth;
87 struct ktr_syscall *ktp;
a9b80e23 88 int len;
984263bc
MD
89 register_t *argp;
90 int i;
91
a9b80e23
MD
92 len = offsetof(struct ktr_syscall, ktr_args) +
93 (narg * sizeof(register_t));
29f58392
MD
94
95 /*
96 * Setting the active bit prevents a ktrace recursion from the
97 * ktracing op itself.
98 */
9fb04d14 99 lp->lwp_traceflag |= KTRFAC_ACTIVE;
984263bc
MD
100 kth = ktrgetheader(KTR_SYSCALL);
101 MALLOC(ktp, struct ktr_syscall *, len, M_KTRACE, M_WAITOK);
102 ktp->ktr_code = code;
103 ktp->ktr_narg = narg;
104 argp = &ktp->ktr_args[0];
105 for (i = 0; i < narg; i++)
106 *argp++ = args[i];
107 kth->ktr_buf = (caddr_t)ktp;
108 kth->ktr_len = len;
9fb04d14 109 ktrwrite(lp->lwp_proc, kth, NULL);
984263bc
MD
110 FREE(ktp, M_KTRACE);
111 FREE(kth, M_KTRACE);
9fb04d14 112 lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
984263bc
MD
113}
114
115void
9fb04d14 116ktrsysret(struct lwp *lp, int code, int error, register_t retval)
984263bc
MD
117{
118 struct ktr_header *kth;
119 struct ktr_sysret ktp;
984263bc 120
9fb04d14 121 lp->lwp_traceflag |= KTRFAC_ACTIVE;
984263bc
MD
122 kth = ktrgetheader(KTR_SYSRET);
123 ktp.ktr_code = code;
124 ktp.ktr_error = error;
125 ktp.ktr_retval = retval; /* what about val2 ? */
126
127 kth->ktr_buf = (caddr_t)&ktp;
128 kth->ktr_len = sizeof(struct ktr_sysret);
129
9fb04d14 130 ktrwrite(lp->lwp_proc, kth, NULL);
984263bc 131 FREE(kth, M_KTRACE);
9fb04d14 132 lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
984263bc
MD
133}
134
135void
9fb04d14 136ktrnamei(struct lwp *lp, char *path)
984263bc
MD
137{
138 struct ktr_header *kth;
984263bc 139
9fb04d14 140 lp->lwp_traceflag |= KTRFAC_ACTIVE;
984263bc
MD
141 kth = ktrgetheader(KTR_NAMEI);
142 kth->ktr_len = strlen(path);
143 kth->ktr_buf = path;
144
9fb04d14 145 ktrwrite(lp->lwp_proc, kth, NULL);
984263bc 146 FREE(kth, M_KTRACE);
9fb04d14 147 lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
984263bc
MD
148}
149
150void
9fb04d14 151ktrgenio(struct lwp *lp, int fd, enum uio_rw rw, struct uio *uio, int error)
984263bc
MD
152{
153 struct ktr_header *kth;
154 struct ktr_genio ktg;
984263bc
MD
155
156 if (error)
157 return;
9fb04d14 158 lp->lwp_traceflag |= KTRFAC_ACTIVE;
984263bc
MD
159 kth = ktrgetheader(KTR_GENIO);
160 ktg.ktr_fd = fd;
161 ktg.ktr_rw = rw;
162 kth->ktr_buf = (caddr_t)&ktg;
163 kth->ktr_len = sizeof(struct ktr_genio);
164 uio->uio_offset = 0;
165 uio->uio_rw = UIO_WRITE;
166
9fb04d14 167 ktrwrite(lp->lwp_proc, kth, uio);
984263bc 168 FREE(kth, M_KTRACE);
9fb04d14 169 lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
984263bc
MD
170}
171
172void
9fb04d14 173ktrpsig(struct lwp *lp, int sig, sig_t action, sigset_t *mask, int code)
984263bc
MD
174{
175 struct ktr_header *kth;
176 struct ktr_psig kp;
984263bc 177
9fb04d14 178 lp->lwp_traceflag |= KTRFAC_ACTIVE;
984263bc
MD
179 kth = ktrgetheader(KTR_PSIG);
180 kp.signo = (char)sig;
181 kp.action = action;
182 kp.mask = *mask;
183 kp.code = code;
184 kth->ktr_buf = (caddr_t)&kp;
185 kth->ktr_len = sizeof (struct ktr_psig);
186
9fb04d14 187 ktrwrite(lp->lwp_proc, kth, NULL);
984263bc 188 FREE(kth, M_KTRACE);
9fb04d14 189 lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
984263bc
MD
190}
191
192void
9fb04d14 193ktrcsw(struct lwp *lp, int out, int user)
984263bc
MD
194{
195 struct ktr_header *kth;
196 struct ktr_csw kc;
984263bc 197
9fb04d14 198 lp->lwp_traceflag |= KTRFAC_ACTIVE;
984263bc
MD
199 kth = ktrgetheader(KTR_CSW);
200 kc.out = out;
201 kc.user = user;
202 kth->ktr_buf = (caddr_t)&kc;
203 kth->ktr_len = sizeof (struct ktr_csw);
204
9fb04d14 205 ktrwrite(lp->lwp_proc, kth, NULL);
984263bc 206 FREE(kth, M_KTRACE);
9fb04d14 207 lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
984263bc
MD
208}
209#endif
210
211/* Interface and common routines */
212
0e5797fe 213#ifdef KTRACE
984263bc
MD
214/*
215 * ktrace system call
216 */
0e5797fe
MD
217struct ktrace_clear_info {
218 ktrace_node_t tracenode;
219 int rootclear;
220 int error;
221};
222
223static int ktrace_clear_callback(struct proc *p, void *data);
224
225#endif
226
984263bc 227int
753fd850 228sys_ktrace(struct ktrace_args *uap)
984263bc
MD
229{
230#ifdef KTRACE
0e5797fe 231 struct ktrace_clear_info info;
dadab5e9
MD
232 struct thread *td = curthread;
233 struct proc *curp = td->td_proc;
41c20dac 234 struct proc *p;
984263bc
MD
235 struct pgrp *pg;
236 int facs = uap->facs & ~KTRFAC_ROOT;
237 int ops = KTROP(uap->ops);
238 int descend = uap->ops & KTRFLAG_DESCEND;
239 int ret = 0;
240 int error = 0;
fad57d0e 241 struct nlookupdata nd;
29f58392 242 ktrace_node_t tracenode = NULL;
984263bc
MD
243
244 curp->p_traceflag |= KTRFAC_ACTIVE;
245 if (ops != KTROP_CLEAR) {
246 /*
247 * an operation which requires a file argument.
248 */
fad57d0e
MD
249 error = nlookup_init(&nd, uap->fname,
250 UIO_USERSPACE, NLC_LOCKVP);
251 if (error == 0)
252 error = vn_open(&nd, NULL, FREAD|FWRITE|O_NOFOLLOW, 0);
253 if (error == 0 && nd.nl_open_vp->v_type != VREG)
254 error = EACCES;
984263bc
MD
255 if (error) {
256 curp->p_traceflag &= ~KTRFAC_ACTIVE;
fad57d0e 257 nlookup_done(&nd);
984263bc
MD
258 return (error);
259 }
29f58392
MD
260 MALLOC(tracenode, ktrace_node_t, sizeof (struct ktrace_node),
261 M_KTRACE, M_WAITOK | M_ZERO);
262 tracenode->kn_vp = nd.nl_open_vp;
263 tracenode->kn_refs = 1;
fad57d0e
MD
264 nd.nl_open_vp = NULL;
265 nlookup_done(&nd);
a11aaa81 266 vn_unlock(tracenode->kn_vp);
984263bc
MD
267 }
268 /*
29f58392
MD
269 * Clear all uses of the tracefile. Not the most efficient operation
270 * in the world.
984263bc
MD
271 */
272 if (ops == KTROP_CLEARFILE) {
0e5797fe
MD
273 info.tracenode = tracenode;
274 info.error = 0;
275 info.rootclear = 0;
276 allproc_scan(ktrace_clear_callback, &info);
277 error = info.error;
984263bc
MD
278 goto done;
279 }
280 /*
281 * need something to (un)trace (XXX - why is this here?)
282 */
283 if (!facs) {
284 error = EINVAL;
285 goto done;
286 }
287 /*
288 * do it
289 */
290 if (uap->pid < 0) {
291 /*
292 * by process group
293 */
294 pg = pgfind(-uap->pid);
295 if (pg == NULL) {
296 error = ESRCH;
297 goto done;
298 }
fad57d0e 299 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
984263bc 300 if (descend)
29f58392 301 ret |= ktrsetchildren(curp, p, ops, facs, tracenode);
984263bc 302 else
29f58392 303 ret |= ktrops(curp, p, ops, facs, tracenode);
fad57d0e 304 }
984263bc
MD
305 } else {
306 /*
307 * by pid
308 */
309 p = pfind(uap->pid);
310 if (p == NULL) {
311 error = ESRCH;
312 goto done;
313 }
314 if (descend)
29f58392 315 ret |= ktrsetchildren(curp, p, ops, facs, tracenode);
984263bc 316 else
29f58392 317 ret |= ktrops(curp, p, ops, facs, tracenode);
984263bc
MD
318 }
319 if (!ret)
320 error = EPERM;
321done:
29f58392
MD
322 if (tracenode)
323 ktrdestroy(&tracenode);
984263bc
MD
324 curp->p_traceflag &= ~KTRFAC_ACTIVE;
325 return (error);
326#else
327 return ENOSYS;
328#endif
329}
330
0e5797fe
MD
331#ifdef KTRACE
332
333/*
334 * NOTE: NOT MPSAFE (yet)
335 */
336static int
337ktrace_clear_callback(struct proc *p, void *data)
338{
339 struct ktrace_clear_info *info = data;
340
a4a63985
MD
341 if (p->p_tracenode) {
342 if (info->rootclear) {
343 if (p->p_tracenode == info->tracenode) {
0e5797fe
MD
344 ktrdestroy(&p->p_tracenode);
345 p->p_traceflag = 0;
a4a63985
MD
346 }
347 } else {
348 if (p->p_tracenode->kn_vp == info->tracenode->kn_vp) {
349 if (ktrcanset(curproc, p)) {
350 ktrdestroy(&p->p_tracenode);
351 p->p_traceflag = 0;
352 } else {
353 info->error = EPERM;
354 }
0e5797fe
MD
355 }
356 }
357 }
358 return(0);
359}
360
361#endif
362
984263bc
MD
363/*
364 * utrace system call
365 */
366/* ARGSUSED */
367int
753fd850 368sys_utrace(struct utrace_args *uap)
984263bc
MD
369{
370#ifdef KTRACE
371 struct ktr_header *kth;
dadab5e9
MD
372 struct thread *td = curthread; /* XXX */
373 struct proc *p = td->td_proc;
1fd87d54 374 caddr_t cp;
984263bc 375
dadab5e9 376 if (!KTRPOINT(td, KTR_USER))
984263bc 377 return (0);
ab2eb4eb 378 if (uap->len > KTR_USER_MAXLEN)
984263bc 379 return (EINVAL);
9fb04d14 380 td->td_lwp->lwp_traceflag |= KTRFAC_ACTIVE;
984263bc
MD
381 kth = ktrgetheader(KTR_USER);
382 MALLOC(cp, caddr_t, uap->len, M_KTRACE, M_WAITOK);
383 if (!copyin(uap->addr, cp, uap->len)) {
384 kth->ktr_buf = cp;
385 kth->ktr_len = uap->len;
a9b80e23 386 ktrwrite(p, kth, NULL);
984263bc 387 }
984263bc
MD
388 FREE(kth, M_KTRACE);
389 FREE(cp, M_KTRACE);
9fb04d14 390 td->td_lwp->lwp_traceflag &= ~KTRFAC_ACTIVE;
984263bc
MD
391
392 return (0);
393#else
394 return (ENOSYS);
395#endif
396}
397
29f58392
MD
398void
399ktrdestroy(struct ktrace_node **tracenodep)
400{
401 ktrace_node_t tracenode;
402
403 if ((tracenode = *tracenodep) != NULL) {
404 *tracenodep = NULL;
405 KKASSERT(tracenode->kn_refs > 0);
406 /* XXX not MP safe yet */
407 --tracenode->kn_refs;
408 if (tracenode->kn_refs == 0) {
29f58392
MD
409 vn_close(tracenode->kn_vp, FREAD|FWRITE);
410 tracenode->kn_vp = NULL;
411 FREE(tracenode, M_KTRACE);
412 }
413 }
414}
415
0e5797fe
MD
416/*
417 * This allows a process to inherit a ref on a tracenode and is also used
418 * as a temporary ref to prevent a tracenode from being destroyed out from
419 * under an active operation.
420 */
29f58392
MD
421ktrace_node_t
422ktrinherit(ktrace_node_t tracenode)
423{
8a1a197f
MD
424 if (tracenode) {
425 KKASSERT(tracenode->kn_refs > 0);
426 ++tracenode->kn_refs;
427 }
29f58392
MD
428 return(tracenode);
429}
430
984263bc
MD
431#ifdef KTRACE
432static int
29f58392
MD
433ktrops(struct proc *curp, struct proc *p, int ops, int facs,
434 ktrace_node_t tracenode)
984263bc 435{
29f58392 436 ktrace_node_t oldnode;
984263bc
MD
437
438 if (!ktrcanset(curp, p))
439 return (0);
440 if (ops == KTROP_SET) {
8a1a197f 441 if ((oldnode = p->p_tracenode) != tracenode) {
29f58392
MD
442 p->p_tracenode = ktrinherit(tracenode);
443 ktrdestroy(&oldnode);
984263bc
MD
444 }
445 p->p_traceflag |= facs;
446 if (curp->p_ucred->cr_uid == 0)
447 p->p_traceflag |= KTRFAC_ROOT;
448 } else {
449 /* KTROP_CLEAR */
450 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
984263bc
MD
451 /* no more tracing */
452 p->p_traceflag = 0;
29f58392 453 ktrdestroy(&p->p_tracenode);
984263bc
MD
454 }
455 }
456
457 return (1);
458}
459
460static int
2b9ccab7 461ktrsetchildren(struct proc *curp, struct proc *top, int ops, int facs,
29f58392 462 ktrace_node_t tracenode)
984263bc 463{
1fd87d54
RG
464 struct proc *p;
465 int ret = 0;
984263bc
MD
466
467 p = top;
468 for (;;) {
29f58392 469 ret |= ktrops(curp, p, ops, facs, tracenode);
984263bc
MD
470 /*
471 * If this process has children, descend to them next,
472 * otherwise do any siblings, and if done with this level,
473 * follow back up the tree (but not past top).
474 */
475 if (!LIST_EMPTY(&p->p_children))
476 p = LIST_FIRST(&p->p_children);
477 else for (;;) {
478 if (p == top)
479 return (ret);
480 if (LIST_NEXT(p, p_sibling)) {
481 p = LIST_NEXT(p, p_sibling);
482 break;
483 }
484 p = p->p_pptr;
485 }
486 }
487 /*NOTREACHED*/
488}
489
490static void
a9b80e23 491ktrwrite(struct proc *p, struct ktr_header *kth, struct uio *uio)
984263bc 492{
0e5797fe 493 struct ktrace_clear_info info;
984263bc
MD
494 struct uio auio;
495 struct iovec aiov[2];
984263bc 496 int error;
29f58392 497 ktrace_node_t tracenode;
984263bc 498
d681a0bb 499 /*
29f58392
MD
500 * We have to ref our tracenode to prevent it from being ripped out
501 * from under us while we are trying to use it. p_tracenode can
502 * go away at any time if another process gets a write error.
a9b80e23
MD
503 *
504 * XXX not MP safe
d681a0bb 505 */
29f58392 506 if (p->p_tracenode == NULL)
984263bc 507 return;
29f58392 508 tracenode = ktrinherit(p->p_tracenode);
984263bc
MD
509 auio.uio_iov = &aiov[0];
510 auio.uio_offset = 0;
511 auio.uio_segflg = UIO_SYSSPACE;
512 auio.uio_rw = UIO_WRITE;
513 aiov[0].iov_base = (caddr_t)kth;
514 aiov[0].iov_len = sizeof(struct ktr_header);
515 auio.uio_resid = sizeof(struct ktr_header);
516 auio.uio_iovcnt = 1;
dadab5e9 517 auio.uio_td = curthread;
984263bc
MD
518 if (kth->ktr_len > 0) {
519 auio.uio_iovcnt++;
520 aiov[1].iov_base = kth->ktr_buf;
521 aiov[1].iov_len = kth->ktr_len;
522 auio.uio_resid += kth->ktr_len;
523 if (uio != NULL)
524 kth->ktr_len += uio->uio_resid;
525 }
29f58392
MD
526 vn_lock(tracenode->kn_vp, LK_EXCLUSIVE | LK_RETRY);
527 error = VOP_WRITE(tracenode->kn_vp, &auio,
528 IO_UNIT | IO_APPEND, p->p_ucred);
984263bc 529 if (error == 0 && uio != NULL) {
29f58392
MD
530 error = VOP_WRITE(tracenode->kn_vp, uio,
531 IO_UNIT | IO_APPEND, p->p_ucred);
984263bc 532 }
a11aaa81 533 vn_unlock(tracenode->kn_vp);
8a1a197f
MD
534 if (error) {
535 /*
536 * If an error occured, give up tracing on all processes
537 * using this tracenode. This is not MP safe but is
538 * blocking-safe.
539 */
540 log(LOG_NOTICE,
541 "ktrace write failed, errno %d, tracing stopped\n", error);
0e5797fe
MD
542 info.tracenode = tracenode;
543 info.error = 0;
544 info.rootclear = 1;
545 allproc_scan(ktrace_clear_callback, &info);
984263bc 546 }
29f58392 547 ktrdestroy(&tracenode);
984263bc
MD
548}
549
550/*
551 * Return true if caller has permission to set the ktracing state
552 * of target. Essentially, the target can't possess any
553 * more permissions than the caller. KTRFAC_ROOT signifies that
554 * root previously set the tracing status on the target process, and
555 * so, only root may further change it.
556 *
557 * TODO: check groups. use caller effective gid.
558 */
559static int
41c20dac 560ktrcanset(struct proc *callp, struct proc *targetp)
984263bc 561{
41c20dac
MD
562 struct ucred *caller = callp->p_ucred;
563 struct ucred *target = targetp->p_ucred;
984263bc 564
41c20dac 565 if (!PRISON_CHECK(caller, target))
984263bc 566 return (0);
41c20dac
MD
567 if ((caller->cr_uid == target->cr_ruid &&
568 target->cr_ruid == target->cr_svuid &&
569 caller->cr_rgid == target->cr_rgid && /* XXX */
570 target->cr_rgid == target->cr_svgid &&
984263bc
MD
571 (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
572 (targetp->p_flag & P_SUGID) == 0) ||
41c20dac 573 caller->cr_uid == 0)
984263bc
MD
574 return (1);
575
576 return (0);
577}
578
579#endif /* KTRACE */