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