1 /* $OpenBSD: kern_ktrace.c,v 1.94 2017/12/30 23:08:29 guenther 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 void 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 vrele(vp); 87 crfree(cred); 88 } 89 } 90 91 /* 92 * Change the trace setting in a correct way (to avoid races). 93 */ 94 void 95 ktrsettrace(struct process *pr, int facs, struct vnode *newvp, 96 struct ucred *newcred) 97 { 98 struct vnode *oldvp; 99 struct ucred *oldcred; 100 101 KASSERT(newvp != NULL); 102 KASSERT(newcred != NULL); 103 104 pr->ps_traceflag |= facs; 105 106 /* nothing to change about where the trace goes? */ 107 if (pr->ps_tracevp == newvp && pr->ps_tracecred == newcred) 108 return; 109 110 vref(newvp); 111 crhold(newcred); 112 113 oldvp = pr->ps_tracevp; 114 oldcred = pr->ps_tracecred; 115 116 pr->ps_tracevp = newvp; 117 pr->ps_tracecred = newcred; 118 119 if (oldvp != NULL) { 120 vrele(oldvp); 121 crfree(oldcred); 122 } 123 } 124 125 void 126 ktrinitheaderraw(struct ktr_header *kth, uint type, pid_t pid, pid_t tid) 127 { 128 memset(kth, 0, sizeof(struct ktr_header)); 129 kth->ktr_type = type; 130 nanotime(&kth->ktr_time); 131 kth->ktr_pid = pid; 132 kth->ktr_tid = tid; 133 } 134 135 void 136 ktrinitheader(struct ktr_header *kth, struct proc *p, int type) 137 { 138 struct process *pr = p->p_p; 139 140 ktrinitheaderraw(kth, type, pr->ps_pid, p->p_tid + THREAD_PID_OFFSET); 141 memcpy(kth->ktr_comm, pr->ps_comm, MAXCOMLEN); 142 } 143 144 void 145 ktrstart(struct proc *p, struct vnode *vp, struct ucred *cred) 146 { 147 struct ktr_header kth; 148 149 ktrinitheaderraw(&kth, htobe32(KTR_START), -1, -1); 150 ktrwriteraw(p, vp, cred, &kth, NULL); 151 } 152 153 void 154 ktrsyscall(struct proc *p, register_t code, size_t argsize, register_t args[]) 155 { 156 struct ktr_header kth; 157 struct ktr_syscall *ktp; 158 size_t len = sizeof(struct ktr_syscall) + argsize; 159 register_t *argp; 160 u_int nargs = 0; 161 int i; 162 163 if (code == SYS_sysctl) { 164 /* 165 * The sysctl encoding stores the mib[] 166 * array because it is interesting. 167 */ 168 if (args[1] > 0) 169 nargs = lmin(args[1], CTL_MAXNAME); 170 len += nargs * sizeof(int); 171 } 172 atomic_setbits_int(&p->p_flag, P_INKTR); 173 ktrinitheader(&kth, p, KTR_SYSCALL); 174 ktp = malloc(len, M_TEMP, M_WAITOK); 175 ktp->ktr_code = code; 176 ktp->ktr_argsize = argsize; 177 argp = (register_t *)((char *)ktp + sizeof(struct ktr_syscall)); 178 for (i = 0; i < (argsize / sizeof *argp); i++) 179 *argp++ = args[i]; 180 if (nargs && copyin((void *)args[0], argp, nargs * sizeof(int))) 181 memset(argp, 0, nargs * sizeof(int)); 182 ktrwrite(p, &kth, ktp, len); 183 free(ktp, M_TEMP, len); 184 atomic_clearbits_int(&p->p_flag, P_INKTR); 185 } 186 187 void 188 ktrsysret(struct proc *p, register_t code, int error, 189 const register_t retval[2]) 190 { 191 struct ktr_header kth; 192 struct ktr_sysret ktp; 193 int len; 194 195 atomic_setbits_int(&p->p_flag, P_INKTR); 196 ktrinitheader(&kth, p, KTR_SYSRET); 197 ktp.ktr_code = code; 198 ktp.ktr_error = error; 199 if (error) 200 len = 0; 201 else if (code == SYS_lseek) 202 /* the one exception: lseek on ILP32 needs more */ 203 len = sizeof(long long); 204 else 205 len = sizeof(register_t); 206 ktrwrite2(p, &kth, &ktp, sizeof(ktp), retval, len); 207 atomic_clearbits_int(&p->p_flag, P_INKTR); 208 } 209 210 void 211 ktrnamei(struct proc *p, char *path) 212 { 213 struct ktr_header kth; 214 215 atomic_setbits_int(&p->p_flag, P_INKTR); 216 ktrinitheader(&kth, p, KTR_NAMEI); 217 ktrwrite(p, &kth, path, strlen(path)); 218 atomic_clearbits_int(&p->p_flag, P_INKTR); 219 } 220 221 void 222 ktrgenio(struct proc *p, int fd, enum uio_rw rw, struct iovec *iov, 223 ssize_t len) 224 { 225 struct ktr_header kth; 226 struct ktr_genio ktp; 227 caddr_t cp; 228 int count; 229 int buflen; 230 231 atomic_setbits_int(&p->p_flag, P_INKTR); 232 233 /* beware overflow */ 234 if (len > PAGE_SIZE) 235 buflen = PAGE_SIZE; 236 else 237 buflen = len + sizeof(struct ktr_genio); 238 239 ktrinitheader(&kth, p, KTR_GENIO); 240 ktp.ktr_fd = fd; 241 ktp.ktr_rw = rw; 242 243 cp = malloc(buflen, M_TEMP, M_WAITOK); 244 while (len > 0) { 245 /* 246 * Don't allow this process to hog the cpu when doing 247 * huge I/O. 248 */ 249 sched_pause(preempt); 250 251 count = lmin(iov->iov_len, buflen); 252 if (count > len) 253 count = len; 254 if (copyin(iov->iov_base, cp, count)) 255 break; 256 257 if (ktrwrite2(p, &kth, &ktp, sizeof(ktp), cp, count) != 0) 258 break; 259 260 iov->iov_len -= count; 261 iov->iov_base = (caddr_t)iov->iov_base + count; 262 263 if (iov->iov_len == 0) 264 iov++; 265 266 len -= count; 267 } 268 269 free(cp, M_TEMP, buflen); 270 atomic_clearbits_int(&p->p_flag, P_INKTR); 271 } 272 273 void 274 ktrpsig(struct proc *p, int sig, sig_t action, int mask, int code, 275 siginfo_t *si) 276 { 277 struct ktr_header kth; 278 struct ktr_psig kp; 279 280 atomic_setbits_int(&p->p_flag, P_INKTR); 281 ktrinitheader(&kth, p, KTR_PSIG); 282 kp.signo = (char)sig; 283 kp.action = action; 284 kp.mask = mask; 285 kp.code = code; 286 kp.si = *si; 287 288 ktrwrite(p, &kth, &kp, sizeof(kp)); 289 atomic_clearbits_int(&p->p_flag, P_INKTR); 290 } 291 292 void 293 ktrstruct(struct proc *p, const char *name, const void *data, size_t datalen) 294 { 295 struct ktr_header kth; 296 297 KERNEL_ASSERT_LOCKED(); 298 atomic_setbits_int(&p->p_flag, P_INKTR); 299 ktrinitheader(&kth, p, KTR_STRUCT); 300 301 if (data == NULL) 302 datalen = 0; 303 ktrwrite2(p, &kth, name, strlen(name) + 1, data, datalen); 304 atomic_clearbits_int(&p->p_flag, P_INKTR); 305 } 306 307 int 308 ktruser(struct proc *p, const char *id, const void *addr, size_t len) 309 { 310 struct ktr_header kth; 311 struct ktr_user ktp; 312 int error; 313 void *memp; 314 #define STK_PARAMS 128 315 long long stkbuf[STK_PARAMS / sizeof(long long)]; 316 317 if (!KTRPOINT(p, KTR_USER)) 318 return (0); 319 if (len > KTR_USER_MAXLEN) 320 return (EINVAL); 321 322 atomic_setbits_int(&p->p_flag, P_INKTR); 323 ktrinitheader(&kth, p, KTR_USER); 324 memset(ktp.ktr_id, 0, KTR_USER_MAXIDLEN); 325 error = copyinstr(id, ktp.ktr_id, KTR_USER_MAXIDLEN, NULL); 326 if (error == 0) { 327 if (len > sizeof(stkbuf)) 328 memp = malloc(len, M_TEMP, M_WAITOK); 329 else 330 memp = stkbuf; 331 error = copyin(addr, memp, len); 332 if (error == 0) 333 ktrwrite2(p, &kth, &ktp, sizeof(ktp), memp, len); 334 if (memp != stkbuf) 335 free(memp, M_TEMP, len); 336 } 337 atomic_clearbits_int(&p->p_flag, P_INKTR); 338 return (error); 339 } 340 341 void 342 ktrexec(struct proc *p, int type, const char *data, ssize_t len) 343 { 344 struct ktr_header kth; 345 int count; 346 int buflen; 347 348 assert(type == KTR_EXECARGS || type == KTR_EXECENV); 349 atomic_setbits_int(&p->p_flag, P_INKTR); 350 351 /* beware overflow */ 352 if (len > PAGE_SIZE) 353 buflen = PAGE_SIZE; 354 else 355 buflen = len; 356 357 ktrinitheader(&kth, p, type); 358 359 while (len > 0) { 360 /* 361 * Don't allow this process to hog the cpu when doing 362 * huge I/O. 363 */ 364 sched_pause(preempt); 365 366 count = lmin(len, buflen); 367 if (ktrwrite(p, &kth, data, count) != 0) 368 break; 369 370 len -= count; 371 data += count; 372 } 373 374 atomic_clearbits_int(&p->p_flag, P_INKTR); 375 } 376 377 void 378 ktrpledge(struct proc *p, int error, uint64_t code, int syscall) 379 { 380 struct ktr_header kth; 381 struct ktr_pledge kp; 382 383 atomic_setbits_int(&p->p_flag, P_INKTR); 384 ktrinitheader(&kth, p, KTR_PLEDGE); 385 kp.error = error; 386 kp.code = code; 387 kp.syscall = syscall; 388 389 ktrwrite(p, &kth, &kp, sizeof(kp)); 390 atomic_clearbits_int(&p->p_flag, P_INKTR); 391 } 392 393 /* Interface and common routines */ 394 395 int 396 doktrace(struct vnode *vp, int ops, int facs, pid_t pid, struct proc *p) 397 { 398 struct process *pr = NULL; 399 struct ucred *cred = NULL; 400 struct pgrp *pg; 401 int descend = ops & KTRFLAG_DESCEND; 402 int ret = 0; 403 int error = 0; 404 405 facs = facs & ~((unsigned)KTRFAC_ROOT); 406 ops = KTROP(ops); 407 408 if (ops != KTROP_CLEAR) { 409 /* 410 * an operation which requires a file argument. 411 */ 412 cred = p->p_ucred; 413 if (!vp) { 414 error = EINVAL; 415 goto done; 416 } 417 if (vp->v_type != VREG) { 418 error = EACCES; 419 goto done; 420 } 421 } 422 /* 423 * Clear all uses of the tracefile 424 */ 425 if (ops == KTROP_CLEARFILE) { 426 LIST_FOREACH(pr, &allprocess, ps_list) { 427 if (pr->ps_tracevp == vp) { 428 if (ktrcanset(p, pr)) 429 ktrcleartrace(pr); 430 else 431 error = EPERM; 432 } 433 } 434 goto done; 435 } 436 /* 437 * need something to (un)trace (XXX - why is this here?) 438 */ 439 if (!facs) { 440 error = EINVAL; 441 goto done; 442 } 443 if (ops == KTROP_SET) { 444 if (suser(p, 0) == 0) 445 facs |= KTRFAC_ROOT; 446 ktrstart(p, vp, cred); 447 } 448 /* 449 * do it 450 */ 451 if (pid < 0) { 452 /* 453 * by process group 454 */ 455 pg = pgfind(-pid); 456 if (pg == NULL) { 457 error = ESRCH; 458 goto done; 459 } 460 LIST_FOREACH(pr, &pg->pg_members, ps_pglist) { 461 if (descend) 462 ret |= ktrsetchildren(p, pr, ops, facs, vp, 463 cred); 464 else 465 ret |= ktrops(p, pr, ops, facs, vp, cred); 466 } 467 } else { 468 /* 469 * by pid 470 */ 471 pr = prfind(pid); 472 if (pr == NULL) { 473 error = ESRCH; 474 goto done; 475 } 476 if (descend) 477 ret |= ktrsetchildren(p, pr, ops, facs, vp, cred); 478 else 479 ret |= ktrops(p, pr, ops, facs, vp, cred); 480 } 481 if (!ret) 482 error = EPERM; 483 done: 484 return (error); 485 } 486 487 /* 488 * ktrace system call 489 */ 490 int 491 sys_ktrace(struct proc *p, void *v, register_t *retval) 492 { 493 struct sys_ktrace_args /* { 494 syscallarg(const char *) fname; 495 syscallarg(int) ops; 496 syscallarg(int) facs; 497 syscallarg(pid_t) pid; 498 } */ *uap = v; 499 struct vnode *vp = NULL; 500 const char *fname = SCARG(uap, fname); 501 struct ucred *cred = NULL; 502 int error; 503 504 if (fname) { 505 struct nameidata nd; 506 507 cred = p->p_ucred; 508 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname, p); 509 nd.ni_pledge = PLEDGE_CPATH | PLEDGE_WPATH; 510 if ((error = vn_open(&nd, FWRITE|O_NOFOLLOW, 0)) != 0) 511 return error; 512 vp = nd.ni_vp; 513 514 VOP_UNLOCK(vp, p); 515 } 516 517 error = doktrace(vp, SCARG(uap, ops), SCARG(uap, facs), 518 SCARG(uap, pid), p); 519 if (vp != NULL) 520 (void)vn_close(vp, FWRITE, cred, p); 521 522 return error; 523 } 524 525 int 526 ktrops(struct proc *curp, struct process *pr, int ops, int facs, 527 struct vnode *vp, struct ucred *cred) 528 { 529 if (!ktrcanset(curp, pr)) 530 return (0); 531 if (ops == KTROP_SET) 532 ktrsettrace(pr, facs, vp, cred); 533 else { 534 /* KTROP_CLEAR */ 535 pr->ps_traceflag &= ~facs; 536 if ((pr->ps_traceflag & KTRFAC_MASK) == 0) { 537 /* cleared all the facility bits, so stop completely */ 538 ktrcleartrace(pr); 539 } 540 } 541 542 return (1); 543 } 544 545 int 546 ktrsetchildren(struct proc *curp, struct process *top, int ops, int facs, 547 struct vnode *vp, struct ucred *cred) 548 { 549 struct process *pr; 550 int ret = 0; 551 552 pr = top; 553 for (;;) { 554 ret |= ktrops(curp, pr, ops, facs, vp, cred); 555 /* 556 * If this process has children, descend to them next, 557 * otherwise do any siblings, and if done with this level, 558 * follow back up the tree (but not past top). 559 */ 560 if (!LIST_EMPTY(&pr->ps_children)) 561 pr = LIST_FIRST(&pr->ps_children); 562 else for (;;) { 563 if (pr == top) 564 return (ret); 565 if (LIST_NEXT(pr, ps_sibling) != NULL) { 566 pr = LIST_NEXT(pr, ps_sibling); 567 break; 568 } 569 pr = pr->ps_pptr; 570 } 571 } 572 /*NOTREACHED*/ 573 } 574 575 int 576 ktrwrite(struct proc *p, struct ktr_header *kth, const void *aux, size_t len) 577 { 578 struct vnode *vp = p->p_p->ps_tracevp; 579 struct ucred *cred = p->p_p->ps_tracecred; 580 struct iovec data[2]; 581 int error; 582 583 if (vp == NULL) 584 return 0; 585 crhold(cred); 586 data[0].iov_base = (void *)aux; 587 data[0].iov_len = len; 588 data[1].iov_len = 0; 589 kth->ktr_len = len; 590 error = ktrwriteraw(p, vp, cred, kth, data); 591 crfree(cred); 592 return (error); 593 } 594 595 int 596 ktrwrite2(struct proc *p, struct ktr_header *kth, const void *aux1, 597 size_t len1, const void *aux2, size_t len2) 598 { 599 struct vnode *vp = p->p_p->ps_tracevp; 600 struct ucred *cred = p->p_p->ps_tracecred; 601 struct iovec data[2]; 602 int error; 603 604 if (vp == NULL) 605 return 0; 606 crhold(cred); 607 data[0].iov_base = (void *)aux1; 608 data[0].iov_len = len1; 609 data[1].iov_base = (void *)aux2; 610 data[1].iov_len = len2; 611 kth->ktr_len = len1 + len2; 612 error = ktrwriteraw(p, vp, cred, kth, data); 613 crfree(cred); 614 return (error); 615 } 616 617 int 618 ktrwriteraw(struct proc *curp, struct vnode *vp, struct ucred *cred, 619 struct ktr_header *kth, struct iovec *data) 620 { 621 struct uio auio; 622 struct iovec aiov[3]; 623 struct process *pr; 624 int error; 625 626 auio.uio_iov = &aiov[0]; 627 auio.uio_offset = 0; 628 auio.uio_segflg = UIO_SYSSPACE; 629 auio.uio_rw = UIO_WRITE; 630 aiov[0].iov_base = (caddr_t)kth; 631 aiov[0].iov_len = sizeof(struct ktr_header); 632 auio.uio_resid = sizeof(struct ktr_header); 633 auio.uio_iovcnt = 1; 634 auio.uio_procp = curp; 635 if (kth->ktr_len > 0) { 636 aiov[1] = data[0]; 637 aiov[2] = data[1]; 638 auio.uio_iovcnt++; 639 if (aiov[2].iov_len > 0) 640 auio.uio_iovcnt++; 641 auio.uio_resid += kth->ktr_len; 642 } 643 vget(vp, LK_EXCLUSIVE | LK_RETRY, curp); 644 error = VOP_WRITE(vp, &auio, IO_UNIT|IO_APPEND, cred); 645 if (!error) { 646 vput(vp); 647 return (0); 648 } 649 /* 650 * If error encountered, give up tracing on this vnode. 651 */ 652 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n", 653 error); 654 LIST_FOREACH(pr, &allprocess, ps_list) 655 if (pr->ps_tracevp == vp && pr->ps_tracecred == cred) 656 ktrcleartrace(pr); 657 658 vput(vp); 659 return (error); 660 } 661 662 /* 663 * Return true if caller has permission to set the ktracing state 664 * of target. Essentially, the target can't possess any 665 * more permissions than the caller. KTRFAC_ROOT signifies that 666 * root previously set the tracing status on the target process, and 667 * so, only root may further change it. 668 * 669 * TODO: check groups. use caller effective gid. 670 */ 671 int 672 ktrcanset(struct proc *callp, struct process *targetpr) 673 { 674 struct ucred *caller = callp->p_ucred; 675 struct ucred *target = targetpr->ps_ucred; 676 677 if ((caller->cr_uid == target->cr_ruid && 678 target->cr_ruid == target->cr_svuid && 679 caller->cr_rgid == target->cr_rgid && /* XXX */ 680 target->cr_rgid == target->cr_svgid && 681 (targetpr->ps_traceflag & KTRFAC_ROOT) == 0 && 682 !ISSET(targetpr->ps_flags, PS_SUGID)) || 683 caller->cr_uid == 0) 684 return (1); 685 686 return (0); 687 } 688