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/sysproto.h> 42 #include <sys/kernel.h> 43 #include <sys/proc.h> 44 #include <sys/fcntl.h> 45 #include <sys/lock.h> 46 #include <sys/namei.h> 47 #include <sys/vnode.h> 48 #include <sys/ktrace.h> 49 #include <sys/malloc.h> 50 #include <sys/syslog.h> 51 52 53 #include <stddef.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_buf = (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_buf = (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_buf = 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_buf = (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_buf = (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_buf = (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 LIST_FOREACH(p, &allproc, p_list) { 285 if (p->p_tracep == vp) { 286 if (ktrcanset(curp, p)) { 287 p->p_tracep = NULL; 288 p->p_traceflag = 0; 289 (void) vn_close(vp, FREAD|FWRITE, 290 p->p_ucred, p); 291 } else 292 error = EPERM; 293 } 294 } 295 goto done; 296 } 297 /* 298 * need something to (un)trace (XXX - why is this here?) 299 */ 300 if (!facs) { 301 error = EINVAL; 302 goto done; 303 } 304 /* 305 * do it 306 */ 307 if (uap->pid < 0) { 308 /* 309 * by process group 310 */ 311 pg = pgfind(-uap->pid); 312 if (pg == NULL) { 313 error = ESRCH; 314 goto done; 315 } 316 LIST_FOREACH(p, &pg->pg_members, p_pglist) 317 if (descend) 318 ret |= ktrsetchildren(curp, p, ops, facs, vp); 319 else 320 ret |= ktrops(curp, p, ops, facs, vp); 321 322 } else { 323 /* 324 * by pid 325 */ 326 p = pfind(uap->pid); 327 if (p == NULL) { 328 error = ESRCH; 329 goto done; 330 } 331 if (descend) 332 ret |= ktrsetchildren(curp, p, ops, facs, vp); 333 else 334 ret |= ktrops(curp, p, ops, facs, vp); 335 } 336 if (!ret) 337 error = EPERM; 338 done: 339 if (vp != NULL) 340 (void) vn_close(vp, FWRITE, curp->p_ucred, curp); 341 curp->p_traceflag &= ~KTRFAC_ACTIVE; 342 return (error); 343 #else 344 return ENOSYS; 345 #endif 346 } 347 348 /* 349 * utrace system call 350 */ 351 /* ARGSUSED */ 352 int 353 utrace(curp, uap) 354 struct proc *curp; 355 register struct utrace_args *uap; 356 { 357 #ifdef KTRACE 358 struct ktr_header *kth; 359 struct proc *p = curproc; /* XXX */ 360 register caddr_t cp; 361 362 if (!KTRPOINT(p, KTR_USER)) 363 return (0); 364 p->p_traceflag |= KTRFAC_ACTIVE; 365 kth = ktrgetheader(KTR_USER); 366 MALLOC(cp, caddr_t, uap->len, M_KTRACE, M_WAITOK); 367 if (!copyin(uap->addr, cp, uap->len)) { 368 kth->ktr_buf = cp; 369 kth->ktr_len = uap->len; 370 ktrwrite(p->p_tracep, kth, NULL); 371 } 372 FREE(kth, M_KTRACE); 373 FREE(cp, M_KTRACE); 374 p->p_traceflag &= ~KTRFAC_ACTIVE; 375 376 return (0); 377 #else 378 return (ENOSYS); 379 #endif 380 } 381 382 #ifdef KTRACE 383 static int 384 ktrops(curp, p, ops, facs, vp) 385 struct proc *p, *curp; 386 int ops, facs; 387 struct vnode *vp; 388 { 389 390 if (!ktrcanset(curp, p)) 391 return (0); 392 if (ops == KTROP_SET) { 393 if (p->p_tracep != vp) { 394 /* 395 * if trace file already in use, relinquish 396 */ 397 if (p->p_tracep != NULL) 398 vrele(p->p_tracep); 399 VREF(vp); 400 p->p_tracep = vp; 401 } 402 p->p_traceflag |= facs; 403 if (curp->p_ucred->cr_uid == 0) 404 p->p_traceflag |= KTRFAC_ROOT; 405 } else { 406 /* KTROP_CLEAR */ 407 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) { 408 /* no more tracing */ 409 p->p_traceflag = 0; 410 if (p->p_tracep != NULL) { 411 vrele(p->p_tracep); 412 p->p_tracep = NULL; 413 } 414 } 415 } 416 417 return (1); 418 } 419 420 static int 421 ktrsetchildren(curp, top, ops, facs, vp) 422 struct proc *curp, *top; 423 int ops, facs; 424 struct vnode *vp; 425 { 426 register struct proc *p; 427 register int ret = 0; 428 429 p = top; 430 for (;;) { 431 ret |= ktrops(curp, p, ops, facs, vp); 432 /* 433 * If this process has children, descend to them next, 434 * otherwise do any siblings, and if done with this level, 435 * follow back up the tree (but not past top). 436 */ 437 if (!LIST_EMPTY(&p->p_children)) 438 p = LIST_FIRST(&p->p_children); 439 else for (;;) { 440 if (p == top) 441 return (ret); 442 if (LIST_NEXT(p, p_sibling)) { 443 p = LIST_NEXT(p, p_sibling); 444 break; 445 } 446 p = p->p_pptr; 447 } 448 } 449 /*NOTREACHED*/ 450 } 451 452 static void 453 ktrwrite(vp, kth, uio) 454 struct vnode *vp; 455 register struct ktr_header *kth; 456 struct uio *uio; 457 { 458 struct uio auio; 459 struct iovec aiov[2]; 460 struct proc *p = curproc; /* XXX */ 461 struct mount *mp; 462 int error; 463 464 if (vp == NULL) 465 return; 466 auio.uio_iov = &aiov[0]; 467 auio.uio_offset = 0; 468 auio.uio_segflg = UIO_SYSSPACE; 469 auio.uio_rw = UIO_WRITE; 470 aiov[0].iov_base = (caddr_t)kth; 471 aiov[0].iov_len = sizeof(struct ktr_header); 472 auio.uio_resid = sizeof(struct ktr_header); 473 auio.uio_iovcnt = 1; 474 auio.uio_procp = curproc; 475 if (kth->ktr_len > 0) { 476 auio.uio_iovcnt++; 477 aiov[1].iov_base = kth->ktr_buf; 478 aiov[1].iov_len = kth->ktr_len; 479 auio.uio_resid += kth->ktr_len; 480 if (uio != NULL) 481 kth->ktr_len += uio->uio_resid; 482 } 483 vn_start_write(vp, &mp, V_WAIT); 484 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 485 (void)VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); 486 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, p->p_ucred); 487 if (error == 0 && uio != NULL) { 488 (void)VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); 489 error = VOP_WRITE(vp, uio, IO_UNIT | IO_APPEND, p->p_ucred); 490 } 491 VOP_UNLOCK(vp, 0, p); 492 vn_finished_write(mp); 493 if (!error) 494 return; 495 /* 496 * If error encountered, give up tracing on this vnode. 497 */ 498 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n", 499 error); 500 LIST_FOREACH(p, &allproc, p_list) { 501 if (p->p_tracep == vp) { 502 p->p_tracep = NULL; 503 p->p_traceflag = 0; 504 vrele(vp); 505 } 506 } 507 } 508 509 /* 510 * Return true if caller has permission to set the ktracing state 511 * of target. Essentially, the target can't possess any 512 * more permissions than the caller. KTRFAC_ROOT signifies that 513 * root previously set the tracing status on the target process, and 514 * so, only root may further change it. 515 * 516 * TODO: check groups. use caller effective gid. 517 */ 518 static int 519 ktrcanset(callp, targetp) 520 struct proc *callp, *targetp; 521 { 522 register struct pcred *caller = callp->p_cred; 523 register struct pcred *target = targetp->p_cred; 524 525 if (!PRISON_CHECK(callp, targetp)) 526 return (0); 527 if ((caller->pc_ucred->cr_uid == target->p_ruid && 528 target->p_ruid == target->p_svuid && 529 caller->p_rgid == target->p_rgid && /* XXX */ 530 target->p_rgid == target->p_svgid && 531 (targetp->p_traceflag & KTRFAC_ROOT) == 0) || 532 caller->pc_ucred->cr_uid == 0) 533 return (1); 534 535 return (0); 536 } 537 538 #endif /* KTRACE */ 539