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$ 35 */ 36 37 #include "opt_ktrace.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/lock.h> 42 #include <sys/mutex.h> 43 #include <sys/sysproto.h> 44 #include <sys/kernel.h> 45 #include <sys/proc.h> 46 #include <sys/fcntl.h> 47 #include <sys/namei.h> 48 #include <sys/vnode.h> 49 #include <sys/ktrace.h> 50 #include <sys/malloc.h> 51 #include <sys/sx.h> 52 #include <sys/syslog.h> 53 #include <sys/jail.h> 54 55 static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE"); 56 57 #ifdef KTRACE 58 static struct ktr_header *ktrgetheader __P((int type)); 59 static void ktrwrite __P((struct vnode *, struct ktr_header *, struct uio *)); 60 static int ktrcanset __P((struct proc *,struct proc *)); 61 static int ktrsetchildren __P((struct proc *,struct proc *,int,int,struct vnode *)); 62 static int ktrops __P((struct proc *,struct proc *,int,int,struct vnode *)); 63 64 65 static struct ktr_header * 66 ktrgetheader(type) 67 int type; 68 { 69 register struct ktr_header *kth; 70 struct proc *p = curproc; /* XXX */ 71 72 MALLOC(kth, struct ktr_header *, sizeof (struct ktr_header), 73 M_KTRACE, M_WAITOK); 74 kth->ktr_type = type; 75 microtime(&kth->ktr_time); 76 kth->ktr_pid = p->p_pid; 77 bcopy(p->p_comm, kth->ktr_comm, MAXCOMLEN + 1); 78 return (kth); 79 } 80 81 void 82 ktrsyscall(vp, code, narg, args) 83 struct vnode *vp; 84 int code, narg; 85 register_t args[]; 86 { 87 struct ktr_header *kth; 88 struct ktr_syscall *ktp; 89 register int len = offsetof(struct ktr_syscall, ktr_args) + 90 (narg * sizeof(register_t)); 91 struct proc *p = curproc; /* XXX */ 92 register_t *argp; 93 int i; 94 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_buffer = (caddr_t)ktp; 104 kth->ktr_len = len; 105 ktrwrite(vp, kth, NULL); 106 FREE(ktp, M_KTRACE); 107 FREE(kth, M_KTRACE); 108 p->p_traceflag &= ~KTRFAC_ACTIVE; 109 } 110 111 void 112 ktrsysret(vp, code, error, retval) 113 struct vnode *vp; 114 int code, error; 115 register_t retval; 116 { 117 struct ktr_header *kth; 118 struct ktr_sysret ktp; 119 struct proc *p = curproc; /* XXX */ 120 121 p->p_traceflag |= KTRFAC_ACTIVE; 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_buffer = (caddr_t)&ktp; 128 kth->ktr_len = sizeof(struct ktr_sysret); 129 130 ktrwrite(vp, kth, NULL); 131 FREE(kth, M_KTRACE); 132 p->p_traceflag &= ~KTRFAC_ACTIVE; 133 } 134 135 void 136 ktrnamei(vp, path) 137 struct vnode *vp; 138 char *path; 139 { 140 struct ktr_header *kth; 141 struct proc *p = curproc; /* XXX */ 142 143 p->p_traceflag |= KTRFAC_ACTIVE; 144 kth = ktrgetheader(KTR_NAMEI); 145 kth->ktr_len = strlen(path); 146 kth->ktr_buffer = path; 147 148 ktrwrite(vp, kth, NULL); 149 FREE(kth, M_KTRACE); 150 p->p_traceflag &= ~KTRFAC_ACTIVE; 151 } 152 153 void 154 ktrgenio(vp, fd, rw, uio, error) 155 struct vnode *vp; 156 int fd; 157 enum uio_rw rw; 158 struct uio *uio; 159 int error; 160 { 161 struct ktr_header *kth; 162 struct ktr_genio ktg; 163 struct proc *p = curproc; /* XXX */ 164 165 if (error) 166 return; 167 p->p_traceflag |= KTRFAC_ACTIVE; 168 kth = ktrgetheader(KTR_GENIO); 169 ktg.ktr_fd = fd; 170 ktg.ktr_rw = rw; 171 kth->ktr_buffer = (caddr_t)&ktg; 172 kth->ktr_len = sizeof(struct ktr_genio); 173 uio->uio_offset = 0; 174 uio->uio_rw = UIO_WRITE; 175 176 ktrwrite(vp, kth, uio); 177 FREE(kth, M_KTRACE); 178 p->p_traceflag &= ~KTRFAC_ACTIVE; 179 } 180 181 void 182 ktrpsig(vp, sig, action, mask, code) 183 struct vnode *vp; 184 int sig; 185 sig_t action; 186 sigset_t *mask; 187 int code; 188 { 189 struct ktr_header *kth; 190 struct ktr_psig kp; 191 struct proc *p = curproc; /* XXX */ 192 193 p->p_traceflag |= KTRFAC_ACTIVE; 194 kth = ktrgetheader(KTR_PSIG); 195 kp.signo = (char)sig; 196 kp.action = action; 197 kp.mask = *mask; 198 kp.code = code; 199 kth->ktr_buffer = (caddr_t)&kp; 200 kth->ktr_len = sizeof (struct ktr_psig); 201 202 ktrwrite(vp, kth, NULL); 203 FREE(kth, M_KTRACE); 204 p->p_traceflag &= ~KTRFAC_ACTIVE; 205 } 206 207 void 208 ktrcsw(vp, out, user) 209 struct vnode *vp; 210 int out, user; 211 { 212 struct ktr_header *kth; 213 struct ktr_csw kc; 214 struct proc *p = curproc; /* XXX */ 215 216 p->p_traceflag |= KTRFAC_ACTIVE; 217 kth = ktrgetheader(KTR_CSW); 218 kc.out = out; 219 kc.user = user; 220 kth->ktr_buffer = (caddr_t)&kc; 221 kth->ktr_len = sizeof (struct ktr_csw); 222 223 ktrwrite(vp, kth, NULL); 224 FREE(kth, M_KTRACE); 225 p->p_traceflag &= ~KTRFAC_ACTIVE; 226 } 227 #endif 228 229 /* Interface and common routines */ 230 231 /* 232 * ktrace system call 233 */ 234 #ifndef _SYS_SYSPROTO_H_ 235 struct ktrace_args { 236 char *fname; 237 int ops; 238 int facs; 239 int pid; 240 }; 241 #endif 242 /* ARGSUSED */ 243 int 244 ktrace(curp, uap) 245 struct proc *curp; 246 register struct ktrace_args *uap; 247 { 248 #ifdef KTRACE 249 register struct vnode *vp = NULL; 250 register struct proc *p; 251 struct pgrp *pg; 252 int facs = uap->facs & ~KTRFAC_ROOT; 253 int ops = KTROP(uap->ops); 254 int descend = uap->ops & KTRFLAG_DESCEND; 255 int ret = 0; 256 int flags, error = 0; 257 struct nameidata nd; 258 259 curp->p_traceflag |= KTRFAC_ACTIVE; 260 if (ops != KTROP_CLEAR) { 261 /* 262 * an operation which requires a file argument. 263 */ 264 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, curp); 265 flags = FREAD | FWRITE | O_NOFOLLOW; 266 error = vn_open(&nd, &flags, 0); 267 if (error) { 268 curp->p_traceflag &= ~KTRFAC_ACTIVE; 269 return (error); 270 } 271 NDFREE(&nd, NDF_ONLY_PNBUF); 272 vp = nd.ni_vp; 273 VOP_UNLOCK(vp, 0, curp); 274 if (vp->v_type != VREG) { 275 (void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp); 276 curp->p_traceflag &= ~KTRFAC_ACTIVE; 277 return (EACCES); 278 } 279 } 280 /* 281 * Clear all uses of the tracefile 282 */ 283 if (ops == KTROP_CLEARFILE) { 284 sx_slock(&allproc_lock); 285 LIST_FOREACH(p, &allproc, p_list) { 286 if (p->p_tracep == vp) { 287 if (ktrcanset(curp, p)) { 288 p->p_tracep = NULL; 289 p->p_traceflag = 0; 290 (void) vn_close(vp, FREAD|FWRITE, 291 p->p_ucred, p); 292 } else 293 error = EPERM; 294 } 295 } 296 sx_sunlock(&allproc_lock); 297 goto done; 298 } 299 /* 300 * need something to (un)trace (XXX - why is this here?) 301 */ 302 if (!facs) { 303 error = EINVAL; 304 goto done; 305 } 306 /* 307 * do it 308 */ 309 if (uap->pid < 0) { 310 /* 311 * by process group 312 */ 313 pg = pgfind(-uap->pid); 314 if (pg == NULL) { 315 error = ESRCH; 316 goto done; 317 } 318 LIST_FOREACH(p, &pg->pg_members, p_pglist) 319 if (descend) 320 ret |= ktrsetchildren(curp, p, ops, facs, vp); 321 else 322 ret |= ktrops(curp, p, ops, facs, vp); 323 } else { 324 /* 325 * by pid 326 */ 327 p = pfind(uap->pid); 328 if (p == NULL) { 329 error = ESRCH; 330 goto done; 331 } 332 PROC_UNLOCK(p); 333 if (descend) 334 ret |= ktrsetchildren(curp, p, ops, facs, vp); 335 else 336 ret |= ktrops(curp, p, ops, facs, vp); 337 } 338 if (!ret) 339 error = EPERM; 340 done: 341 if (vp != NULL) 342 (void) vn_close(vp, FWRITE, curp->p_ucred, curp); 343 curp->p_traceflag &= ~KTRFAC_ACTIVE; 344 return (error); 345 #else 346 return ENOSYS; 347 #endif 348 } 349 350 /* 351 * utrace system call 352 */ 353 /* ARGSUSED */ 354 int 355 utrace(curp, uap) 356 struct proc *curp; 357 register struct utrace_args *uap; 358 { 359 #ifdef KTRACE 360 struct ktr_header *kth; 361 struct proc *p = curproc; /* XXX */ 362 register caddr_t cp; 363 364 if (!KTRPOINT(p, KTR_USER)) 365 return (0); 366 if (uap->len > KTR_USER_MAXLEN) 367 return (EINVAL); 368 p->p_traceflag |= KTRFAC_ACTIVE; 369 kth = ktrgetheader(KTR_USER); 370 MALLOC(cp, caddr_t, uap->len, M_KTRACE, M_WAITOK); 371 if (!copyin(uap->addr, cp, uap->len)) { 372 kth->ktr_buffer = cp; 373 kth->ktr_len = uap->len; 374 ktrwrite(p->p_tracep, kth, NULL); 375 } 376 FREE(kth, M_KTRACE); 377 FREE(cp, M_KTRACE); 378 p->p_traceflag &= ~KTRFAC_ACTIVE; 379 380 return (0); 381 #else 382 return (ENOSYS); 383 #endif 384 } 385 386 #ifdef KTRACE 387 static int 388 ktrops(curp, p, ops, facs, vp) 389 struct proc *p, *curp; 390 int ops, facs; 391 struct vnode *vp; 392 { 393 394 if (!ktrcanset(curp, p)) 395 return (0); 396 if (ops == KTROP_SET) { 397 if (p->p_tracep != vp) { 398 /* 399 * if trace file already in use, relinquish 400 */ 401 if (p->p_tracep != NULL) 402 vrele(p->p_tracep); 403 VREF(vp); 404 p->p_tracep = vp; 405 } 406 p->p_traceflag |= facs; 407 if (curp->p_ucred->cr_uid == 0) 408 p->p_traceflag |= KTRFAC_ROOT; 409 } else { 410 /* KTROP_CLEAR */ 411 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) { 412 /* no more tracing */ 413 p->p_traceflag = 0; 414 if (p->p_tracep != NULL) { 415 vrele(p->p_tracep); 416 p->p_tracep = NULL; 417 } 418 } 419 } 420 421 return (1); 422 } 423 424 static int 425 ktrsetchildren(curp, top, ops, facs, vp) 426 struct proc *curp, *top; 427 int ops, facs; 428 struct vnode *vp; 429 { 430 register struct proc *p; 431 register int ret = 0; 432 433 p = top; 434 sx_slock(&proctree_lock); 435 for (;;) { 436 ret |= ktrops(curp, p, ops, facs, vp); 437 /* 438 * If this process has children, descend to them next, 439 * otherwise do any siblings, and if done with this level, 440 * follow back up the tree (but not past top). 441 */ 442 if (!LIST_EMPTY(&p->p_children)) 443 p = LIST_FIRST(&p->p_children); 444 else for (;;) { 445 if (p == top) { 446 sx_sunlock(&proctree_lock); 447 return (ret); 448 } 449 if (LIST_NEXT(p, p_sibling)) { 450 p = LIST_NEXT(p, p_sibling); 451 break; 452 } 453 p = p->p_pptr; 454 } 455 } 456 /*NOTREACHED*/ 457 } 458 459 static void 460 ktrwrite(vp, kth, uio) 461 struct vnode *vp; 462 register struct ktr_header *kth; 463 struct uio *uio; 464 { 465 struct uio auio; 466 struct iovec aiov[2]; 467 struct proc *p = curproc; /* XXX */ 468 struct mount *mp; 469 int error; 470 471 if (vp == NULL) 472 return; 473 auio.uio_iov = &aiov[0]; 474 auio.uio_offset = 0; 475 auio.uio_segflg = UIO_SYSSPACE; 476 auio.uio_rw = UIO_WRITE; 477 aiov[0].iov_base = (caddr_t)kth; 478 aiov[0].iov_len = sizeof(struct ktr_header); 479 auio.uio_resid = sizeof(struct ktr_header); 480 auio.uio_iovcnt = 1; 481 auio.uio_procp = curproc; 482 if (kth->ktr_len > 0) { 483 auio.uio_iovcnt++; 484 aiov[1].iov_base = kth->ktr_buffer; 485 aiov[1].iov_len = kth->ktr_len; 486 auio.uio_resid += kth->ktr_len; 487 if (uio != NULL) 488 kth->ktr_len += uio->uio_resid; 489 } 490 vn_start_write(vp, &mp, V_WAIT); 491 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 492 (void)VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); 493 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, p->p_ucred); 494 if (error == 0 && uio != NULL) { 495 (void)VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); 496 error = VOP_WRITE(vp, uio, IO_UNIT | IO_APPEND, p->p_ucred); 497 } 498 VOP_UNLOCK(vp, 0, p); 499 vn_finished_write(mp); 500 if (!error) 501 return; 502 /* 503 * If error encountered, give up tracing on this vnode. 504 */ 505 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n", 506 error); 507 sx_slock(&allproc_lock); 508 LIST_FOREACH(p, &allproc, p_list) { 509 if (p->p_tracep == vp) { 510 p->p_tracep = NULL; 511 p->p_traceflag = 0; 512 vrele(vp); 513 } 514 } 515 sx_sunlock(&allproc_lock); 516 } 517 518 /* 519 * Return true if caller has permission to set the ktracing state 520 * of target. Essentially, the target can't possess any 521 * more permissions than the caller. KTRFAC_ROOT signifies that 522 * root previously set the tracing status on the target process, and 523 * so, only root may further change it. 524 * 525 * XXX: These checks are stronger than for ptrace() 526 * XXX: This check should be p_can(... P_CAN_DEBUG ...); 527 * 528 * TODO: check groups. use caller effective gid. 529 */ 530 static int 531 ktrcanset(callp, targetp) 532 struct proc *callp, *targetp; 533 { 534 struct ucred *callcr = callp->p_ucred; 535 struct ucred *targetcr = targetp->p_ucred; 536 537 if (prison_check(callcr, targetcr)) 538 return (0); 539 if ((callcr->cr_uid == targetcr->cr_ruid && 540 targetcr->cr_ruid == targetcr->cr_svuid && 541 callcr->cr_rgid == targetcr->cr_rgid && /* XXX */ 542 targetcr->cr_rgid == targetcr->cr_svgid && 543 (targetp->p_traceflag & KTRFAC_ROOT) == 0) || 544 !suser_xxx(callcr, NULL, PRISON_ROOT)) 545 return (1); 546 547 return (0); 548 } 549 550 #endif /* KTRACE */ 551