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