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