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