xref: /openbsd-src/sys/kern/kern_ktrace.c (revision a0747c9f67a4ae71ccb71e62a28d1ea19e06a63c)
1 /*	$OpenBSD: kern_ktrace.c,v 1.104 2020/09/13 09:48:39 claudio Exp $	*/
2 /*	$NetBSD: kern_ktrace.c,v 1.23 1996/02/09 18:59:36 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)kern_ktrace.c	8.2 (Berkeley) 9/23/93
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/proc.h>
38 #include <sys/sched.h>
39 #include <sys/fcntl.h>
40 #include <sys/namei.h>
41 #include <sys/vnode.h>
42 #include <sys/lock.h>
43 #include <sys/ktrace.h>
44 #include <sys/malloc.h>
45 #include <sys/syslog.h>
46 #include <sys/sysctl.h>
47 #include <sys/pledge.h>
48 
49 #include <sys/mount.h>
50 #include <sys/syscall.h>
51 #include <sys/syscallargs.h>
52 
53 #include <uvm/uvm_extern.h>
54 
55 void	ktrinitheaderraw(struct ktr_header *, uint, pid_t, pid_t);
56 void	ktrinitheader(struct ktr_header *, struct proc *, int);
57 int	ktrstart(struct proc *, struct vnode *, struct ucred *);
58 int	ktrops(struct proc *, struct process *, int, int, struct vnode *,
59 	    struct ucred *);
60 int	ktrsetchildren(struct proc *, struct process *, int, int,
61 	    struct vnode *, struct ucred *);
62 int	ktrwrite(struct proc *, struct ktr_header *, const void *, size_t);
63 int	ktrwrite2(struct proc *, struct ktr_header *, const void *, size_t,
64 	    const void *, size_t);
65 int	ktrwriteraw(struct proc *, struct vnode *, struct ucred *,
66 	    struct ktr_header *, struct iovec *);
67 int	ktrcanset(struct proc *, struct process *);
68 
69 /*
70  * Clear the trace settings in a correct way (to avoid races).
71  */
72 void
73 ktrcleartrace(struct process *pr)
74 {
75 	struct vnode *vp;
76 	struct ucred *cred;
77 
78 	if (pr->ps_tracevp != NULL) {
79 		vp = pr->ps_tracevp;
80 		cred = pr->ps_tracecred;
81 
82 		pr->ps_traceflag = 0;
83 		pr->ps_tracevp = NULL;
84 		pr->ps_tracecred = NULL;
85 
86 		vp->v_writecount--;
87 		vrele(vp);
88 		crfree(cred);
89 	}
90 }
91 
92 /*
93  * Change the trace setting in a correct way (to avoid races).
94  */
95 void
96 ktrsettrace(struct process *pr, int facs, struct vnode *newvp,
97     struct ucred *newcred)
98 {
99 	struct vnode *oldvp;
100 	struct ucred *oldcred;
101 
102 	KASSERT(newvp != NULL);
103 	KASSERT(newcred != NULL);
104 
105 	pr->ps_traceflag |= facs;
106 
107 	/* nothing to change about where the trace goes? */
108 	if (pr->ps_tracevp == newvp && pr->ps_tracecred == newcred)
109 		return;
110 
111 	vref(newvp);
112 	crhold(newcred);
113 	newvp->v_writecount++;
114 
115 	oldvp = pr->ps_tracevp;
116 	oldcred = pr->ps_tracecred;
117 
118 	pr->ps_tracevp = newvp;
119 	pr->ps_tracecred = newcred;
120 
121 	if (oldvp != NULL) {
122 		oldvp->v_writecount--;
123 		vrele(oldvp);
124 		crfree(oldcred);
125 	}
126 }
127 
128 void
129 ktrinitheaderraw(struct ktr_header *kth, uint type, pid_t pid, pid_t tid)
130 {
131 	memset(kth, 0, sizeof(struct ktr_header));
132 	kth->ktr_type = type;
133 	nanotime(&kth->ktr_time);
134 	kth->ktr_pid = pid;
135 	kth->ktr_tid = tid;
136 }
137 
138 void
139 ktrinitheader(struct ktr_header *kth, struct proc *p, int type)
140 {
141 	struct process *pr = p->p_p;
142 
143 	ktrinitheaderraw(kth, type, pr->ps_pid, p->p_tid + THREAD_PID_OFFSET);
144 	memcpy(kth->ktr_comm, pr->ps_comm, MAXCOMLEN);
145 }
146 
147 int
148 ktrstart(struct proc *p, struct vnode *vp, struct ucred *cred)
149 {
150 	struct ktr_header kth;
151 
152 	ktrinitheaderraw(&kth, htobe32(KTR_START), -1, -1);
153 	return (ktrwriteraw(p, vp, cred, &kth, NULL));
154 }
155 
156 void
157 ktrsyscall(struct proc *p, register_t code, size_t argsize, register_t args[])
158 {
159 	struct	ktr_header kth;
160 	struct	ktr_syscall *ktp;
161 	size_t len = sizeof(struct ktr_syscall) + argsize;
162 	register_t *argp;
163 	u_int nargs = 0;
164 	int i;
165 
166 	if (code == SYS_sysctl) {
167 		/*
168 		 * The sysctl encoding stores the mib[]
169 		 * array because it is interesting.
170 		 */
171 		if (args[1] > 0)
172 			nargs = lmin(args[1], CTL_MAXNAME);
173 		len += nargs * sizeof(int);
174 	}
175 	atomic_setbits_int(&p->p_flag, P_INKTR);
176 	ktrinitheader(&kth, p, KTR_SYSCALL);
177 	ktp = malloc(len, M_TEMP, M_WAITOK);
178 	ktp->ktr_code = code;
179 	ktp->ktr_argsize = argsize;
180 	argp = (register_t *)((char *)ktp + sizeof(struct ktr_syscall));
181 	for (i = 0; i < (argsize / sizeof *argp); i++)
182 		*argp++ = args[i];
183 	if (nargs && copyin((void *)args[0], argp, nargs * sizeof(int)))
184 		memset(argp, 0, nargs * sizeof(int));
185 	ktrwrite(p, &kth, ktp, len);
186 	free(ktp, M_TEMP, len);
187 	atomic_clearbits_int(&p->p_flag, P_INKTR);
188 }
189 
190 void
191 ktrsysret(struct proc *p, register_t code, int error,
192     const register_t retval[2])
193 {
194 	struct ktr_header kth;
195 	struct ktr_sysret ktp;
196 	int len;
197 
198 	atomic_setbits_int(&p->p_flag, P_INKTR);
199 	ktrinitheader(&kth, p, KTR_SYSRET);
200 	ktp.ktr_code = code;
201 	ktp.ktr_error = error;
202 	if (error)
203 		len = 0;
204 	else if (code == SYS_lseek)
205 		/* the one exception: lseek on ILP32 needs more */
206 		len = sizeof(long long);
207 	else
208 		len = sizeof(register_t);
209 	ktrwrite2(p, &kth, &ktp, sizeof(ktp), retval, len);
210 	atomic_clearbits_int(&p->p_flag, P_INKTR);
211 }
212 
213 void
214 ktrnamei(struct proc *p, char *path)
215 {
216 	struct ktr_header kth;
217 
218 	atomic_setbits_int(&p->p_flag, P_INKTR);
219 	ktrinitheader(&kth, p, KTR_NAMEI);
220 	ktrwrite(p, &kth, path, strlen(path));
221 	atomic_clearbits_int(&p->p_flag, P_INKTR);
222 }
223 
224 void
225 ktrgenio(struct proc *p, int fd, enum uio_rw rw, struct iovec *iov,
226     ssize_t len)
227 {
228 	struct ktr_header kth;
229 	struct ktr_genio ktp;
230 	caddr_t cp;
231 	int count, error;
232 	int buflen;
233 
234 	atomic_setbits_int(&p->p_flag, P_INKTR);
235 
236 	/* beware overflow */
237 	if (len > PAGE_SIZE)
238 		buflen = PAGE_SIZE;
239 	else
240 		buflen = len + sizeof(struct ktr_genio);
241 
242 	ktrinitheader(&kth, p, KTR_GENIO);
243 	ktp.ktr_fd = fd;
244 	ktp.ktr_rw = rw;
245 
246 	cp = malloc(buflen, M_TEMP, M_WAITOK);
247 	while (len > 0) {
248 		/*
249 		 * Don't allow this process to hog the cpu when doing
250 		 * huge I/O.
251 		 */
252 		sched_pause(preempt);
253 
254 		count = lmin(iov->iov_len, buflen);
255 		if (count > len)
256 			count = len;
257 		if (copyin(iov->iov_base, cp, count))
258 			break;
259 
260 		KERNEL_LOCK();
261 		error = ktrwrite2(p, &kth, &ktp, sizeof(ktp), cp, count);
262 		KERNEL_UNLOCK();
263 		if (error != 0)
264 			break;
265 
266 		iov->iov_len -= count;
267 		iov->iov_base = (caddr_t)iov->iov_base + count;
268 
269 		if (iov->iov_len == 0)
270 			iov++;
271 
272 		len -= count;
273 	}
274 
275 	free(cp, M_TEMP, buflen);
276 	atomic_clearbits_int(&p->p_flag, P_INKTR);
277 }
278 
279 void
280 ktrpsig(struct proc *p, int sig, sig_t action, int mask, int code,
281     siginfo_t *si)
282 {
283 	struct ktr_header kth;
284 	struct ktr_psig kp;
285 
286 	atomic_setbits_int(&p->p_flag, P_INKTR);
287 	ktrinitheader(&kth, p, KTR_PSIG);
288 	kp.signo = (char)sig;
289 	kp.action = action;
290 	kp.mask = mask;
291 	kp.code = code;
292 	kp.si = *si;
293 
294 	KERNEL_LOCK();
295 	ktrwrite(p, &kth, &kp, sizeof(kp));
296 	KERNEL_UNLOCK();
297 	atomic_clearbits_int(&p->p_flag, P_INKTR);
298 }
299 
300 void
301 ktrstruct(struct proc *p, const char *name, const void *data, size_t datalen)
302 {
303 	struct ktr_header kth;
304 
305 	atomic_setbits_int(&p->p_flag, P_INKTR);
306 	ktrinitheader(&kth, p, KTR_STRUCT);
307 
308 	if (data == NULL)
309 		datalen = 0;
310 	KERNEL_LOCK();
311 	ktrwrite2(p, &kth, name, strlen(name) + 1, data, datalen);
312 	KERNEL_UNLOCK();
313 	atomic_clearbits_int(&p->p_flag, P_INKTR);
314 }
315 
316 int
317 ktruser(struct proc *p, const char *id, const void *addr, size_t len)
318 {
319 	struct ktr_header kth;
320 	struct ktr_user ktp;
321 	int error;
322 	void *memp;
323 #define	STK_PARAMS	128
324 	long long stkbuf[STK_PARAMS / sizeof(long long)];
325 
326 	if (!KTRPOINT(p, KTR_USER))
327 		return (0);
328 	if (len > KTR_USER_MAXLEN)
329 		return (EINVAL);
330 
331 	atomic_setbits_int(&p->p_flag, P_INKTR);
332 	ktrinitheader(&kth, p, KTR_USER);
333 	memset(ktp.ktr_id, 0, KTR_USER_MAXIDLEN);
334 	error = copyinstr(id, ktp.ktr_id, KTR_USER_MAXIDLEN, NULL);
335 	if (error == 0) {
336 		if (len > sizeof(stkbuf))
337 			memp = malloc(len, M_TEMP, M_WAITOK);
338 		else
339 			memp = stkbuf;
340 		error = copyin(addr, memp, len);
341 		if (error == 0)
342 			ktrwrite2(p, &kth, &ktp, sizeof(ktp), memp, len);
343 		if (memp != stkbuf)
344 			free(memp, M_TEMP, len);
345 	}
346 	atomic_clearbits_int(&p->p_flag, P_INKTR);
347 	return (error);
348 }
349 
350 void
351 ktrexec(struct proc *p, int type, const char *data, ssize_t len)
352 {
353 	struct ktr_header kth;
354 	int count;
355 	int buflen;
356 
357 	assert(type == KTR_EXECARGS || type == KTR_EXECENV);
358 	atomic_setbits_int(&p->p_flag, P_INKTR);
359 
360 	/* beware overflow */
361 	if (len > PAGE_SIZE)
362 		buflen = PAGE_SIZE;
363 	else
364 		buflen = len;
365 
366 	ktrinitheader(&kth, p, type);
367 
368 	while (len > 0) {
369 		/*
370 		 * Don't allow this process to hog the cpu when doing
371 		 * huge I/O.
372 		 */
373 		sched_pause(preempt);
374 
375 		count = lmin(len, buflen);
376 		if (ktrwrite(p, &kth, data, count) != 0)
377 			break;
378 
379 		len -= count;
380 		data += count;
381 	}
382 
383 	atomic_clearbits_int(&p->p_flag, P_INKTR);
384 }
385 
386 void
387 ktrpledge(struct proc *p, int error, uint64_t code, int syscall)
388 {
389 	struct ktr_header kth;
390 	struct ktr_pledge kp;
391 
392 	atomic_setbits_int(&p->p_flag, P_INKTR);
393 	ktrinitheader(&kth, p, KTR_PLEDGE);
394 	kp.error = error;
395 	kp.code = code;
396 	kp.syscall = syscall;
397 
398 	KERNEL_LOCK();
399 	ktrwrite(p, &kth, &kp, sizeof(kp));
400 	KERNEL_UNLOCK();
401 	atomic_clearbits_int(&p->p_flag, P_INKTR);
402 }
403 
404 /* Interface and common routines */
405 
406 int
407 doktrace(struct vnode *vp, int ops, int facs, pid_t pid, struct proc *p)
408 {
409 	struct process *pr = NULL;
410 	struct ucred *cred = NULL;
411 	struct pgrp *pg;
412 	int descend = ops & KTRFLAG_DESCEND;
413 	int ret = 0;
414 	int error = 0;
415 
416 	facs = facs & ~((unsigned)KTRFAC_ROOT);
417 	ops = KTROP(ops);
418 
419 	if (ops != KTROP_CLEAR) {
420 		/*
421 		 * an operation which requires a file argument.
422 		 */
423 		cred = p->p_ucred;
424 		if (!vp) {
425 			error = EINVAL;
426 			goto done;
427 		}
428 		if (vp->v_type != VREG) {
429 			error = EACCES;
430 			goto done;
431 		}
432 	}
433 	/*
434 	 * Clear all uses of the tracefile
435 	 */
436 	if (ops == KTROP_CLEARFILE) {
437 		LIST_FOREACH(pr, &allprocess, ps_list) {
438 			if (pr->ps_tracevp == vp) {
439 				if (ktrcanset(p, pr))
440 					ktrcleartrace(pr);
441 				else
442 					error = EPERM;
443 			}
444 		}
445 		goto done;
446 	}
447 	/*
448 	 * need something to (un)trace (XXX - why is this here?)
449 	 */
450 	if (!facs) {
451 		error = EINVAL;
452 		goto done;
453 	}
454 	if (ops == KTROP_SET) {
455 		if (suser(p) == 0)
456 			facs |= KTRFAC_ROOT;
457 		error = ktrstart(p, vp, cred);
458 		if (error != 0)
459 			goto done;
460 	}
461 	/*
462 	 * do it
463 	 */
464 	if (pid < 0) {
465 		/*
466 		 * by process group
467 		 */
468 		pg = pgfind(-pid);
469 		if (pg == NULL) {
470 			error = ESRCH;
471 			goto done;
472 		}
473 		LIST_FOREACH(pr, &pg->pg_members, ps_pglist) {
474 			if (descend)
475 				ret |= ktrsetchildren(p, pr, ops, facs, vp,
476 				    cred);
477 			else
478 				ret |= ktrops(p, pr, ops, facs, vp, cred);
479 		}
480 	} else {
481 		/*
482 		 * by pid
483 		 */
484 		pr = prfind(pid);
485 		if (pr == NULL) {
486 			error = ESRCH;
487 			goto done;
488 		}
489 		if (descend)
490 			ret |= ktrsetchildren(p, pr, ops, facs, vp, cred);
491 		else
492 			ret |= ktrops(p, pr, ops, facs, vp, cred);
493 	}
494 	if (!ret)
495 		error = EPERM;
496 done:
497 	return (error);
498 }
499 
500 /*
501  * ktrace system call
502  */
503 int
504 sys_ktrace(struct proc *p, void *v, register_t *retval)
505 {
506 	struct sys_ktrace_args /* {
507 		syscallarg(const char *) fname;
508 		syscallarg(int) ops;
509 		syscallarg(int) facs;
510 		syscallarg(pid_t) pid;
511 	} */ *uap = v;
512 	struct vnode *vp = NULL;
513 	const char *fname = SCARG(uap, fname);
514 	struct ucred *cred = NULL;
515 	int error;
516 
517 	if (fname) {
518 		struct nameidata nd;
519 
520 		cred = p->p_ucred;
521 		NDINIT(&nd, 0, 0, UIO_USERSPACE, fname, p);
522 		nd.ni_pledge = PLEDGE_CPATH | PLEDGE_WPATH;
523 		nd.ni_unveil = UNVEIL_CREATE | UNVEIL_WRITE;
524 		if ((error = vn_open(&nd, FWRITE|O_NOFOLLOW, 0)) != 0)
525 			return error;
526 		vp = nd.ni_vp;
527 
528 		VOP_UNLOCK(vp);
529 	}
530 
531 	error = doktrace(vp, SCARG(uap, ops), SCARG(uap, facs),
532 	    SCARG(uap, pid), p);
533 	if (vp != NULL)
534 		(void)vn_close(vp, FWRITE, cred, p);
535 
536 	return error;
537 }
538 
539 int
540 ktrops(struct proc *curp, struct process *pr, int ops, int facs,
541     struct vnode *vp, struct ucred *cred)
542 {
543 	if (!ktrcanset(curp, pr))
544 		return (0);
545 	if (ops == KTROP_SET)
546 		ktrsettrace(pr, facs, vp, cred);
547 	else {
548 		/* KTROP_CLEAR */
549 		pr->ps_traceflag &= ~facs;
550 		if ((pr->ps_traceflag & KTRFAC_MASK) == 0) {
551 			/* cleared all the facility bits, so stop completely */
552 			ktrcleartrace(pr);
553 		}
554 	}
555 
556 	return (1);
557 }
558 
559 int
560 ktrsetchildren(struct proc *curp, struct process *top, int ops, int facs,
561     struct vnode *vp, struct ucred *cred)
562 {
563 	struct process *pr;
564 	int ret = 0;
565 
566 	pr = top;
567 	for (;;) {
568 		ret |= ktrops(curp, pr, ops, facs, vp, cred);
569 		/*
570 		 * If this process has children, descend to them next,
571 		 * otherwise do any siblings, and if done with this level,
572 		 * follow back up the tree (but not past top).
573 		 */
574 		if (!LIST_EMPTY(&pr->ps_children))
575 			pr = LIST_FIRST(&pr->ps_children);
576 		else for (;;) {
577 			if (pr == top)
578 				return (ret);
579 			if (LIST_NEXT(pr, ps_sibling) != NULL) {
580 				pr = LIST_NEXT(pr, ps_sibling);
581 				break;
582 			}
583 			pr = pr->ps_pptr;
584 		}
585 	}
586 	/*NOTREACHED*/
587 }
588 
589 int
590 ktrwrite(struct proc *p, struct ktr_header *kth, const void *aux, size_t len)
591 {
592 	struct vnode *vp = p->p_p->ps_tracevp;
593 	struct ucred *cred = p->p_p->ps_tracecred;
594 	struct iovec data[2];
595 	int error;
596 
597 	if (vp == NULL)
598 		return 0;
599 	crhold(cred);
600 	data[0].iov_base = (void *)aux;
601 	data[0].iov_len = len;
602 	data[1].iov_len = 0;
603 	kth->ktr_len = len;
604 	error = ktrwriteraw(p, vp, cred, kth, data);
605 	crfree(cred);
606 	return (error);
607 }
608 
609 int
610 ktrwrite2(struct proc *p, struct ktr_header *kth, const void *aux1,
611     size_t len1, const void *aux2, size_t len2)
612 {
613 	struct vnode *vp = p->p_p->ps_tracevp;
614 	struct ucred *cred = p->p_p->ps_tracecred;
615 	struct iovec data[2];
616 	int error;
617 
618 	if (vp == NULL)
619 		return 0;
620 	crhold(cred);
621 	data[0].iov_base = (void *)aux1;
622 	data[0].iov_len = len1;
623 	data[1].iov_base = (void *)aux2;
624 	data[1].iov_len = len2;
625 	kth->ktr_len = len1 + len2;
626 	error = ktrwriteraw(p, vp, cred, kth, data);
627 	crfree(cred);
628 	return (error);
629 }
630 
631 int
632 ktrwriteraw(struct proc *curp, struct vnode *vp, struct ucred *cred,
633     struct ktr_header *kth, struct iovec *data)
634 {
635 	struct uio auio;
636 	struct iovec aiov[3];
637 	struct process *pr;
638 	int error;
639 
640 	KERNEL_ASSERT_LOCKED();
641 
642 	auio.uio_iov = &aiov[0];
643 	auio.uio_offset = 0;
644 	auio.uio_segflg = UIO_SYSSPACE;
645 	auio.uio_rw = UIO_WRITE;
646 	aiov[0].iov_base = (caddr_t)kth;
647 	aiov[0].iov_len = sizeof(struct ktr_header);
648 	auio.uio_resid = sizeof(struct ktr_header);
649 	auio.uio_iovcnt = 1;
650 	auio.uio_procp = curp;
651 	if (kth->ktr_len > 0) {
652 		aiov[1] = data[0];
653 		aiov[2] = data[1];
654 		auio.uio_iovcnt++;
655 		if (aiov[2].iov_len > 0)
656 			auio.uio_iovcnt++;
657 		auio.uio_resid += kth->ktr_len;
658 	}
659 	error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
660 	if (error)
661 		goto bad;
662 	error = VOP_WRITE(vp, &auio, IO_UNIT|IO_APPEND, cred);
663 	vput(vp);
664 	if (error)
665 		goto bad;
666 
667 	return (0);
668 
669 bad:
670 	/*
671 	 * If error encountered, give up tracing on this vnode.
672 	 */
673 	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
674 	    error);
675 	LIST_FOREACH(pr, &allprocess, ps_list) {
676 		if (pr == curp->p_p)
677 			continue;
678 		if (pr->ps_tracevp == vp && pr->ps_tracecred == cred)
679 			ktrcleartrace(pr);
680 	}
681 	ktrcleartrace(curp->p_p);
682 	return (error);
683 }
684 
685 /*
686  * Return true if caller has permission to set the ktracing state
687  * of target.  Essentially, the target can't possess any
688  * more permissions than the caller.  KTRFAC_ROOT signifies that
689  * root previously set the tracing status on the target process, and
690  * so, only root may further change it.
691  *
692  * TODO: check groups.  use caller effective gid.
693  */
694 int
695 ktrcanset(struct proc *callp, struct process *targetpr)
696 {
697 	struct ucred *caller = callp->p_ucred;
698 	struct ucred *target = targetpr->ps_ucred;
699 
700 	if ((caller->cr_uid == target->cr_ruid &&
701 	    target->cr_ruid == target->cr_svuid &&
702 	    caller->cr_rgid == target->cr_rgid &&	/* XXX */
703 	    target->cr_rgid == target->cr_svgid &&
704 	    (targetpr->ps_traceflag & KTRFAC_ROOT) == 0 &&
705 	    !ISSET(targetpr->ps_flags, PS_SUGID)) ||
706 	    caller->cr_uid == 0)
707 		return (1);
708 
709 	return (0);
710 }
711