1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. 6 * Copyright (c) 2005 Robert N. M. Watson 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. 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 34 #include "opt_ktrace.h" 35 36 #define EXTERR_CATEGORY EXTERR_KTRACE 37 #include <sys/systm.h> 38 #include <sys/capsicum.h> 39 #include <sys/exterrvar.h> 40 #include <sys/fcntl.h> 41 #include <sys/kernel.h> 42 #include <sys/kthread.h> 43 #include <sys/ktrace.h> 44 #include <sys/lock.h> 45 #include <sys/mutex.h> 46 #include <sys/malloc.h> 47 #include <sys/mount.h> 48 #include <sys/namei.h> 49 #include <sys/priv.h> 50 #include <sys/proc.h> 51 #include <sys/resourcevar.h> 52 #include <sys/socket.h> 53 #include <sys/stat.h> 54 #include <sys/sx.h> 55 #include <sys/sysctl.h> 56 #include <sys/sysent.h> 57 #include <sys/syslog.h> 58 #include <sys/sysproto.h> 59 #include <sys/unistd.h> 60 #include <sys/vnode.h> 61 62 #include <security/mac/mac_framework.h> 63 64 /* 65 * The ktrace facility allows the tracing of certain key events in user space 66 * processes, such as system calls, signal delivery, context switches, and 67 * user generated events using utrace(2). It works by streaming event 68 * records and data to a vnode associated with the process using the 69 * ktrace(2) system call. In general, records can be written directly from 70 * the context that generates the event. One important exception to this is 71 * during a context switch, where sleeping is not permitted. To handle this 72 * case, trace events are generated using in-kernel ktr_request records, and 73 * then delivered to disk at a convenient moment -- either immediately, the 74 * next traceable event, at system call return, or at process exit. 75 * 76 * When dealing with multiple threads or processes writing to the same event 77 * log, ordering guarantees are weak: specifically, if an event has multiple 78 * records (i.e., system call enter and return), they may be interlaced with 79 * records from another event. Process and thread ID information is provided 80 * in the record, and user applications can de-interlace events if required. 81 */ 82 83 static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE"); 84 85 #ifdef KTRACE 86 87 FEATURE(ktrace, "Kernel support for system-call tracing"); 88 89 #ifndef KTRACE_REQUEST_POOL 90 #define KTRACE_REQUEST_POOL 100 91 #endif 92 93 struct ktr_request { 94 struct ktr_header ktr_header; 95 void *ktr_buffer; 96 union { 97 struct ktr_proc_ctor ktr_proc_ctor; 98 struct ktr_cap_fail ktr_cap_fail; 99 struct ktr_syscall ktr_syscall; 100 struct ktr_sysret ktr_sysret; 101 struct ktr_genio ktr_genio; 102 struct ktr_psig ktr_psig; 103 struct ktr_csw ktr_csw; 104 struct ktr_fault ktr_fault; 105 struct ktr_faultend ktr_faultend; 106 struct ktr_struct_array ktr_struct_array; 107 struct ktr_exterr ktr_exterr; 108 } ktr_data; 109 STAILQ_ENTRY(ktr_request) ktr_list; 110 }; 111 112 static const int data_lengths[] = { 113 [KTR_SYSCALL] = offsetof(struct ktr_syscall, ktr_args), 114 [KTR_SYSRET] = sizeof(struct ktr_sysret), 115 [KTR_NAMEI] = 0, 116 [KTR_GENIO] = sizeof(struct ktr_genio), 117 [KTR_PSIG] = sizeof(struct ktr_psig), 118 [KTR_CSW] = sizeof(struct ktr_csw), 119 [KTR_USER] = 0, 120 [KTR_STRUCT] = 0, 121 [KTR_SYSCTL] = 0, 122 [KTR_PROCCTOR] = sizeof(struct ktr_proc_ctor), 123 [KTR_PROCDTOR] = 0, 124 [KTR_CAPFAIL] = sizeof(struct ktr_cap_fail), 125 [KTR_FAULT] = sizeof(struct ktr_fault), 126 [KTR_FAULTEND] = sizeof(struct ktr_faultend), 127 [KTR_STRUCT_ARRAY] = sizeof(struct ktr_struct_array), 128 [KTR_ARGS] = 0, 129 [KTR_ENVS] = 0, 130 [KTR_EXTERR] = sizeof(struct ktr_exterr), 131 }; 132 133 static STAILQ_HEAD(, ktr_request) ktr_free; 134 135 static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 136 "KTRACE options"); 137 138 static u_int ktr_requestpool = KTRACE_REQUEST_POOL; 139 TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool); 140 141 u_int ktr_geniosize = PAGE_SIZE; 142 SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RWTUN, &ktr_geniosize, 143 0, "Maximum size of genio event payload"); 144 145 /* 146 * Allow to not to send signal to traced process, in which context the 147 * ktr record is written. The limit is applied from the process that 148 * set up ktrace, so killing the traced process is not completely fair. 149 */ 150 int ktr_filesize_limit_signal = 0; 151 SYSCTL_INT(_kern_ktrace, OID_AUTO, filesize_limit_signal, CTLFLAG_RWTUN, 152 &ktr_filesize_limit_signal, 0, 153 "Send SIGXFSZ to the traced process when the log size limit is exceeded"); 154 155 static int print_message = 1; 156 static struct mtx ktrace_mtx; 157 static struct sx ktrace_sx; 158 159 struct ktr_io_params { 160 struct vnode *vp; 161 struct ucred *cr; 162 off_t lim; 163 u_int refs; 164 }; 165 166 static void ktrace_init(void *dummy); 167 static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS); 168 static u_int ktrace_resize_pool(u_int oldsize, u_int newsize); 169 static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type); 170 static struct ktr_request *ktr_getrequest(int type); 171 static void ktr_submitrequest(struct thread *td, struct ktr_request *req); 172 static struct ktr_io_params *ktr_freeproc(struct proc *p); 173 static void ktr_freerequest(struct ktr_request *req); 174 static void ktr_freerequest_locked(struct ktr_request *req); 175 static void ktr_writerequest(struct thread *td, struct ktr_request *req); 176 static int ktrcanset(struct thread *,struct proc *); 177 static int ktrsetchildren(struct thread *, struct proc *, int, int, 178 struct ktr_io_params *); 179 static int ktrops(struct thread *, struct proc *, int, int, 180 struct ktr_io_params *); 181 static void ktrprocctor_entered(struct thread *, struct proc *); 182 183 /* 184 * ktrace itself generates events, such as context switches, which we do not 185 * wish to trace. Maintain a flag, TDP_INKTRACE, on each thread to determine 186 * whether or not it is in a region where tracing of events should be 187 * suppressed. 188 */ 189 static void 190 ktrace_enter(struct thread *td) 191 { 192 193 KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set")); 194 td->td_pflags |= TDP_INKTRACE; 195 } 196 197 static void 198 ktrace_exit(struct thread *td) 199 { 200 201 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set")); 202 td->td_pflags &= ~TDP_INKTRACE; 203 } 204 205 static void 206 ktrace_assert(struct thread *td) 207 { 208 209 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set")); 210 } 211 212 static void 213 ast_ktrace(struct thread *td, int tda __unused) 214 { 215 KTRUSERRET(td); 216 } 217 218 static void 219 ktrace_init(void *dummy) 220 { 221 struct ktr_request *req; 222 int i; 223 224 mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET); 225 sx_init(&ktrace_sx, "ktrace_sx"); 226 STAILQ_INIT(&ktr_free); 227 for (i = 0; i < ktr_requestpool; i++) { 228 req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK | 229 M_ZERO); 230 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list); 231 } 232 ast_register(TDA_KTRACE, ASTR_ASTF_REQUIRED, 0, ast_ktrace); 233 } 234 SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL); 235 236 static int 237 sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS) 238 { 239 struct thread *td; 240 u_int newsize, oldsize, wantsize; 241 int error; 242 243 /* Handle easy read-only case first to avoid warnings from GCC. */ 244 if (!req->newptr) { 245 oldsize = ktr_requestpool; 246 return (SYSCTL_OUT(req, &oldsize, sizeof(u_int))); 247 } 248 249 error = SYSCTL_IN(req, &wantsize, sizeof(u_int)); 250 if (error) 251 return (error); 252 td = curthread; 253 ktrace_enter(td); 254 oldsize = ktr_requestpool; 255 newsize = ktrace_resize_pool(oldsize, wantsize); 256 ktrace_exit(td); 257 error = SYSCTL_OUT(req, &oldsize, sizeof(u_int)); 258 if (error) 259 return (error); 260 if (wantsize > oldsize && newsize < wantsize) 261 return (ENOSPC); 262 return (0); 263 } 264 SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, 265 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &ktr_requestpool, 0, 266 sysctl_kern_ktrace_request_pool, "IU", 267 "Pool buffer size for ktrace(1)"); 268 269 static u_int 270 ktrace_resize_pool(u_int oldsize, u_int newsize) 271 { 272 STAILQ_HEAD(, ktr_request) ktr_new; 273 struct ktr_request *req; 274 int bound; 275 276 print_message = 1; 277 bound = newsize - oldsize; 278 if (bound == 0) 279 return (ktr_requestpool); 280 if (bound < 0) { 281 mtx_lock(&ktrace_mtx); 282 /* Shrink pool down to newsize if possible. */ 283 while (bound++ < 0) { 284 req = STAILQ_FIRST(&ktr_free); 285 if (req == NULL) 286 break; 287 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list); 288 ktr_requestpool--; 289 free(req, M_KTRACE); 290 } 291 } else { 292 /* Grow pool up to newsize. */ 293 STAILQ_INIT(&ktr_new); 294 while (bound-- > 0) { 295 req = malloc(sizeof(struct ktr_request), M_KTRACE, 296 M_WAITOK | M_ZERO); 297 STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list); 298 } 299 mtx_lock(&ktrace_mtx); 300 STAILQ_CONCAT(&ktr_free, &ktr_new); 301 ktr_requestpool += (newsize - oldsize); 302 } 303 mtx_unlock(&ktrace_mtx); 304 return (ktr_requestpool); 305 } 306 307 /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */ 308 CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) == 309 (sizeof((struct thread *)NULL)->td_name)); 310 311 static struct ktr_request * 312 ktr_getrequest_entered(struct thread *td, int type) 313 { 314 struct ktr_request *req; 315 struct proc *p = td->td_proc; 316 int pm; 317 318 mtx_lock(&ktrace_mtx); 319 if (!KTRCHECK(td, type)) { 320 mtx_unlock(&ktrace_mtx); 321 return (NULL); 322 } 323 req = STAILQ_FIRST(&ktr_free); 324 if (req != NULL) { 325 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list); 326 req->ktr_header.ktr_type = type; 327 if (p->p_traceflag & KTRFAC_DROP) { 328 req->ktr_header.ktr_type |= KTR_DROP; 329 p->p_traceflag &= ~KTRFAC_DROP; 330 } 331 mtx_unlock(&ktrace_mtx); 332 nanotime(&req->ktr_header.ktr_time); 333 req->ktr_header.ktr_type |= KTR_VERSIONED; 334 req->ktr_header.ktr_pid = p->p_pid; 335 req->ktr_header.ktr_tid = td->td_tid; 336 req->ktr_header.ktr_cpu = PCPU_GET(cpuid); 337 req->ktr_header.ktr_version = KTR_VERSION1; 338 bcopy(td->td_name, req->ktr_header.ktr_comm, 339 sizeof(req->ktr_header.ktr_comm)); 340 req->ktr_buffer = NULL; 341 req->ktr_header.ktr_len = 0; 342 } else { 343 p->p_traceflag |= KTRFAC_DROP; 344 pm = print_message; 345 print_message = 0; 346 mtx_unlock(&ktrace_mtx); 347 if (pm) 348 printf("Out of ktrace request objects.\n"); 349 } 350 return (req); 351 } 352 353 static struct ktr_request * 354 ktr_getrequest(int type) 355 { 356 struct thread *td = curthread; 357 struct ktr_request *req; 358 359 ktrace_enter(td); 360 req = ktr_getrequest_entered(td, type); 361 if (req == NULL) 362 ktrace_exit(td); 363 364 return (req); 365 } 366 367 /* 368 * Some trace generation environments don't permit direct access to VFS, 369 * such as during a context switch where sleeping is not allowed. Under these 370 * circumstances, queue a request to the thread to be written asynchronously 371 * later. 372 */ 373 static void 374 ktr_enqueuerequest(struct thread *td, struct ktr_request *req) 375 { 376 377 mtx_lock(&ktrace_mtx); 378 STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list); 379 mtx_unlock(&ktrace_mtx); 380 ast_sched(td, TDA_KTRACE); 381 } 382 383 /* 384 * Drain any pending ktrace records from the per-thread queue to disk. This 385 * is used both internally before committing other records, and also on 386 * system call return. We drain all the ones we can find at the time when 387 * drain is requested, but don't keep draining after that as those events 388 * may be approximately "after" the current event. 389 */ 390 static void 391 ktr_drain(struct thread *td) 392 { 393 struct ktr_request *queued_req; 394 STAILQ_HEAD(, ktr_request) local_queue; 395 396 ktrace_assert(td); 397 sx_assert(&ktrace_sx, SX_XLOCKED); 398 399 STAILQ_INIT(&local_queue); 400 401 if (!STAILQ_EMPTY_ATOMIC(&td->td_proc->p_ktr)) { 402 mtx_lock(&ktrace_mtx); 403 STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr); 404 mtx_unlock(&ktrace_mtx); 405 406 while ((queued_req = STAILQ_FIRST(&local_queue))) { 407 STAILQ_REMOVE_HEAD(&local_queue, ktr_list); 408 ktr_writerequest(td, queued_req); 409 ktr_freerequest(queued_req); 410 } 411 } 412 } 413 414 /* 415 * Submit a trace record for immediate commit to disk -- to be used only 416 * where entering VFS is OK. First drain any pending records that may have 417 * been cached in the thread. 418 */ 419 static void 420 ktr_submitrequest(struct thread *td, struct ktr_request *req) 421 { 422 423 ktrace_assert(td); 424 425 sx_xlock(&ktrace_sx); 426 ktr_drain(td); 427 ktr_writerequest(td, req); 428 ktr_freerequest(req); 429 sx_xunlock(&ktrace_sx); 430 ktrace_exit(td); 431 } 432 433 static void 434 ktr_freerequest(struct ktr_request *req) 435 { 436 437 mtx_lock(&ktrace_mtx); 438 ktr_freerequest_locked(req); 439 mtx_unlock(&ktrace_mtx); 440 } 441 442 static void 443 ktr_freerequest_locked(struct ktr_request *req) 444 { 445 446 mtx_assert(&ktrace_mtx, MA_OWNED); 447 if (req->ktr_buffer != NULL) 448 free(req->ktr_buffer, M_KTRACE); 449 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list); 450 } 451 452 static void 453 ktr_io_params_ref(struct ktr_io_params *kiop) 454 { 455 mtx_assert(&ktrace_mtx, MA_OWNED); 456 kiop->refs++; 457 } 458 459 static struct ktr_io_params * 460 ktr_io_params_rele(struct ktr_io_params *kiop) 461 { 462 mtx_assert(&ktrace_mtx, MA_OWNED); 463 if (kiop == NULL) 464 return (NULL); 465 KASSERT(kiop->refs > 0, ("kiop ref == 0 %p", kiop)); 466 return (--(kiop->refs) == 0 ? kiop : NULL); 467 } 468 469 void 470 ktr_io_params_free(struct ktr_io_params *kiop) 471 { 472 if (kiop == NULL) 473 return; 474 475 MPASS(kiop->refs == 0); 476 vn_close(kiop->vp, FWRITE, kiop->cr, curthread); 477 crfree(kiop->cr); 478 free(kiop, M_KTRACE); 479 } 480 481 static struct ktr_io_params * 482 ktr_io_params_alloc(struct thread *td, struct vnode *vp) 483 { 484 struct ktr_io_params *res; 485 486 res = malloc(sizeof(struct ktr_io_params), M_KTRACE, M_WAITOK); 487 res->vp = vp; 488 res->cr = crhold(td->td_ucred); 489 res->lim = lim_cur(td, RLIMIT_FSIZE); 490 res->refs = 1; 491 return (res); 492 } 493 494 /* 495 * Disable tracing for a process and release all associated resources. 496 * The caller is responsible for releasing a reference on the returned 497 * vnode and credentials. 498 */ 499 static struct ktr_io_params * 500 ktr_freeproc(struct proc *p) 501 { 502 struct ktr_io_params *kiop; 503 struct ktr_request *req; 504 505 PROC_LOCK_ASSERT(p, MA_OWNED); 506 mtx_assert(&ktrace_mtx, MA_OWNED); 507 kiop = ktr_io_params_rele(p->p_ktrioparms); 508 p->p_ktrioparms = NULL; 509 p->p_traceflag = 0; 510 while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) { 511 STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list); 512 ktr_freerequest_locked(req); 513 } 514 return (kiop); 515 } 516 517 struct vnode * 518 ktr_get_tracevp(struct proc *p, bool ref) 519 { 520 struct vnode *vp; 521 522 PROC_LOCK_ASSERT(p, MA_OWNED); 523 524 if (p->p_ktrioparms != NULL) { 525 vp = p->p_ktrioparms->vp; 526 if (ref) 527 vrefact(vp); 528 } else { 529 vp = NULL; 530 } 531 return (vp); 532 } 533 534 void 535 ktrsyscall(int code, int narg, syscallarg_t args[]) 536 { 537 struct ktr_request *req; 538 struct ktr_syscall *ktp; 539 size_t buflen; 540 char *buf = NULL; 541 542 if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 543 return; 544 545 buflen = sizeof(register_t) * narg; 546 if (buflen > 0) { 547 buf = malloc(buflen, M_KTRACE, M_WAITOK); 548 bcopy(args, buf, buflen); 549 } 550 req = ktr_getrequest(KTR_SYSCALL); 551 if (req == NULL) { 552 if (buf != NULL) 553 free(buf, M_KTRACE); 554 return; 555 } 556 ktp = &req->ktr_data.ktr_syscall; 557 ktp->ktr_code = code; 558 ktp->ktr_narg = narg; 559 if (buflen > 0) { 560 req->ktr_header.ktr_len = buflen; 561 req->ktr_buffer = buf; 562 } 563 ktr_submitrequest(curthread, req); 564 } 565 566 void 567 ktrdata(int type, const void *data, size_t len) 568 { 569 struct ktr_request *req; 570 void *buf; 571 572 if ((req = ktr_getrequest(type)) == NULL) 573 return; 574 buf = malloc(len, M_KTRACE, M_WAITOK); 575 bcopy(data, buf, len); 576 req->ktr_header.ktr_len = len; 577 req->ktr_buffer = buf; 578 ktr_submitrequest(curthread, req); 579 } 580 581 void 582 ktrsysret(int code, int error, register_t retval) 583 { 584 struct ktr_request *req; 585 struct ktr_sysret *ktp; 586 587 if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 588 return; 589 590 req = ktr_getrequest(KTR_SYSRET); 591 if (req == NULL) 592 return; 593 ktp = &req->ktr_data.ktr_sysret; 594 ktp->ktr_code = code; 595 ktp->ktr_error = error; 596 ktp->ktr_retval = ((error == 0) ? retval: 0); /* what about val2 ? */ 597 ktr_submitrequest(curthread, req); 598 } 599 600 /* 601 * When a setuid process execs, disable tracing. 602 * 603 * XXX: We toss any pending asynchronous records. 604 */ 605 struct ktr_io_params * 606 ktrprocexec(struct proc *p) 607 { 608 struct ktr_io_params *kiop; 609 610 PROC_LOCK_ASSERT(p, MA_OWNED); 611 612 kiop = p->p_ktrioparms; 613 if (kiop == NULL || priv_check_cred(kiop->cr, PRIV_DEBUG_DIFFCRED) == 0) 614 return (NULL); 615 616 mtx_lock(&ktrace_mtx); 617 kiop = ktr_freeproc(p); 618 mtx_unlock(&ktrace_mtx); 619 return (kiop); 620 } 621 622 /* 623 * When a process exits, drain per-process asynchronous trace records 624 * and disable tracing. 625 */ 626 void 627 ktrprocexit(struct thread *td) 628 { 629 struct ktr_request *req; 630 struct proc *p; 631 struct ktr_io_params *kiop; 632 633 p = td->td_proc; 634 if (p->p_traceflag == 0) 635 return; 636 637 ktrace_enter(td); 638 req = ktr_getrequest_entered(td, KTR_PROCDTOR); 639 if (req != NULL) 640 ktr_enqueuerequest(td, req); 641 sx_xlock(&ktrace_sx); 642 ktr_drain(td); 643 sx_xunlock(&ktrace_sx); 644 PROC_LOCK(p); 645 mtx_lock(&ktrace_mtx); 646 kiop = ktr_freeproc(p); 647 mtx_unlock(&ktrace_mtx); 648 PROC_UNLOCK(p); 649 ktr_io_params_free(kiop); 650 ktrace_exit(td); 651 } 652 653 static void 654 ktrprocctor_entered(struct thread *td, struct proc *p) 655 { 656 struct ktr_proc_ctor *ktp; 657 struct ktr_request *req; 658 struct thread *td2; 659 660 ktrace_assert(td); 661 td2 = FIRST_THREAD_IN_PROC(p); 662 req = ktr_getrequest_entered(td2, KTR_PROCCTOR); 663 if (req == NULL) 664 return; 665 ktp = &req->ktr_data.ktr_proc_ctor; 666 ktp->sv_flags = p->p_sysent->sv_flags; 667 ktr_enqueuerequest(td2, req); 668 } 669 670 void 671 ktrprocctor(struct proc *p) 672 { 673 struct thread *td = curthread; 674 675 if ((p->p_traceflag & KTRFAC_MASK) == 0) 676 return; 677 678 ktrace_enter(td); 679 ktrprocctor_entered(td, p); 680 ktrace_exit(td); 681 } 682 683 /* 684 * When a process forks, enable tracing in the new process if needed. 685 */ 686 void 687 ktrprocfork(struct proc *p1, struct proc *p2) 688 { 689 690 MPASS(p2->p_ktrioparms == NULL); 691 MPASS(p2->p_traceflag == 0); 692 693 if (p1->p_traceflag == 0) 694 return; 695 696 PROC_LOCK(p1); 697 mtx_lock(&ktrace_mtx); 698 if (p1->p_traceflag & KTRFAC_INHERIT) { 699 p2->p_traceflag = p1->p_traceflag; 700 if ((p2->p_ktrioparms = p1->p_ktrioparms) != NULL) 701 p1->p_ktrioparms->refs++; 702 } 703 mtx_unlock(&ktrace_mtx); 704 PROC_UNLOCK(p1); 705 706 ktrprocctor(p2); 707 } 708 709 /* 710 * When a thread returns, drain any asynchronous records generated by the 711 * system call. 712 */ 713 void 714 ktruserret(struct thread *td) 715 { 716 717 ktrace_enter(td); 718 sx_xlock(&ktrace_sx); 719 ktr_drain(td); 720 sx_xunlock(&ktrace_sx); 721 ktrace_exit(td); 722 } 723 724 void 725 ktrnamei(const char *path) 726 { 727 struct ktr_request *req; 728 int namelen; 729 char *buf = NULL; 730 731 namelen = strlen(path); 732 if (namelen > 0) { 733 buf = malloc(namelen, M_KTRACE, M_WAITOK); 734 bcopy(path, buf, namelen); 735 } 736 req = ktr_getrequest(KTR_NAMEI); 737 if (req == NULL) { 738 if (buf != NULL) 739 free(buf, M_KTRACE); 740 return; 741 } 742 if (namelen > 0) { 743 req->ktr_header.ktr_len = namelen; 744 req->ktr_buffer = buf; 745 } 746 ktr_submitrequest(curthread, req); 747 } 748 749 void 750 ktrsysctl(int *name, u_int namelen) 751 { 752 struct ktr_request *req; 753 u_int mib[CTL_MAXNAME + 2]; 754 char *mibname; 755 size_t mibnamelen; 756 int error; 757 758 /* Lookup name of mib. */ 759 KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long")); 760 mib[0] = 0; 761 mib[1] = 1; 762 bcopy(name, mib + 2, namelen * sizeof(*name)); 763 mibnamelen = 128; 764 mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK); 765 error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen, 766 NULL, 0, &mibnamelen, 0); 767 if (error) { 768 free(mibname, M_KTRACE); 769 return; 770 } 771 req = ktr_getrequest(KTR_SYSCTL); 772 if (req == NULL) { 773 free(mibname, M_KTRACE); 774 return; 775 } 776 req->ktr_header.ktr_len = mibnamelen; 777 req->ktr_buffer = mibname; 778 ktr_submitrequest(curthread, req); 779 } 780 781 void 782 ktrgenio(int fd, enum uio_rw rw, struct uio *uio, int error) 783 { 784 struct ktr_request *req; 785 struct ktr_genio *ktg; 786 int datalen; 787 char *buf; 788 789 if (error != 0 && (rw == UIO_READ || error == EFAULT)) { 790 freeuio(uio); 791 return; 792 } 793 uio->uio_offset = 0; 794 uio->uio_rw = UIO_WRITE; 795 datalen = MIN(uio->uio_resid, ktr_geniosize); 796 buf = malloc(datalen, M_KTRACE, M_WAITOK); 797 error = uiomove(buf, datalen, uio); 798 freeuio(uio); 799 if (error) { 800 free(buf, M_KTRACE); 801 return; 802 } 803 req = ktr_getrequest(KTR_GENIO); 804 if (req == NULL) { 805 free(buf, M_KTRACE); 806 return; 807 } 808 ktg = &req->ktr_data.ktr_genio; 809 ktg->ktr_fd = fd; 810 ktg->ktr_rw = rw; 811 req->ktr_header.ktr_len = datalen; 812 req->ktr_buffer = buf; 813 ktr_submitrequest(curthread, req); 814 } 815 816 void 817 ktrpsig(int sig, sig_t action, sigset_t *mask, int code) 818 { 819 struct thread *td = curthread; 820 struct ktr_request *req; 821 struct ktr_psig *kp; 822 823 req = ktr_getrequest(KTR_PSIG); 824 if (req == NULL) 825 return; 826 kp = &req->ktr_data.ktr_psig; 827 kp->signo = (char)sig; 828 kp->action = action; 829 kp->mask = *mask; 830 kp->code = code; 831 ktr_enqueuerequest(td, req); 832 ktrace_exit(td); 833 } 834 835 void 836 ktrcsw(int out, int user, const char *wmesg) 837 { 838 struct thread *td = curthread; 839 struct ktr_request *req; 840 struct ktr_csw *kc; 841 842 if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 843 return; 844 845 req = ktr_getrequest(KTR_CSW); 846 if (req == NULL) 847 return; 848 kc = &req->ktr_data.ktr_csw; 849 kc->out = out; 850 kc->user = user; 851 if (wmesg != NULL) 852 strlcpy(kc->wmesg, wmesg, sizeof(kc->wmesg)); 853 else 854 bzero(kc->wmesg, sizeof(kc->wmesg)); 855 ktr_enqueuerequest(td, req); 856 ktrace_exit(td); 857 } 858 859 void 860 ktrstruct(const char *name, const void *data, size_t datalen) 861 { 862 struct ktr_request *req; 863 char *buf; 864 size_t buflen, namelen; 865 866 if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 867 return; 868 869 if (data == NULL) 870 datalen = 0; 871 namelen = strlen(name) + 1; 872 buflen = namelen + datalen; 873 buf = malloc(buflen, M_KTRACE, M_WAITOK); 874 strcpy(buf, name); 875 bcopy(data, buf + namelen, datalen); 876 if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) { 877 free(buf, M_KTRACE); 878 return; 879 } 880 req->ktr_buffer = buf; 881 req->ktr_header.ktr_len = buflen; 882 ktr_submitrequest(curthread, req); 883 } 884 885 void 886 ktrstruct_error(const char *name, const void *data, size_t datalen, int error) 887 { 888 889 if (error == 0) 890 ktrstruct(name, data, datalen); 891 } 892 893 void 894 ktrstructarray(const char *name, enum uio_seg seg, const void *data, 895 int num_items, size_t struct_size) 896 { 897 struct ktr_request *req; 898 struct ktr_struct_array *ksa; 899 char *buf; 900 size_t buflen, datalen, namelen; 901 int max_items; 902 903 if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 904 return; 905 if (num_items < 0) 906 return; 907 908 /* Trim array length to genio size. */ 909 max_items = ktr_geniosize / struct_size; 910 if (num_items > max_items) { 911 if (max_items == 0) 912 num_items = 1; 913 else 914 num_items = max_items; 915 } 916 datalen = num_items * struct_size; 917 918 if (data == NULL) 919 datalen = 0; 920 921 namelen = strlen(name) + 1; 922 buflen = namelen + datalen; 923 buf = malloc(buflen, M_KTRACE, M_WAITOK); 924 strcpy(buf, name); 925 if (seg == UIO_SYSSPACE) 926 bcopy(data, buf + namelen, datalen); 927 else { 928 if (copyin(data, buf + namelen, datalen) != 0) { 929 free(buf, M_KTRACE); 930 return; 931 } 932 } 933 if ((req = ktr_getrequest(KTR_STRUCT_ARRAY)) == NULL) { 934 free(buf, M_KTRACE); 935 return; 936 } 937 ksa = &req->ktr_data.ktr_struct_array; 938 ksa->struct_size = struct_size; 939 req->ktr_buffer = buf; 940 req->ktr_header.ktr_len = buflen; 941 ktr_submitrequest(curthread, req); 942 } 943 944 void 945 ktrcapfail(enum ktr_cap_violation type, const void *data) 946 { 947 struct thread *td = curthread; 948 struct ktr_request *req; 949 struct ktr_cap_fail *kcf; 950 union ktr_cap_data *kcd; 951 952 if (__predict_false(td->td_pflags & TDP_INKTRACE)) 953 return; 954 if (type != CAPFAIL_SYSCALL && 955 (td->td_sa.callp->sy_flags & SYF_CAPENABLED) == 0) 956 return; 957 958 req = ktr_getrequest(KTR_CAPFAIL); 959 if (req == NULL) 960 return; 961 kcf = &req->ktr_data.ktr_cap_fail; 962 kcf->cap_type = type; 963 kcf->cap_code = td->td_sa.code; 964 kcf->cap_svflags = td->td_proc->p_sysent->sv_flags; 965 if (data != NULL) { 966 kcd = &kcf->cap_data; 967 switch (type) { 968 case CAPFAIL_NOTCAPABLE: 969 case CAPFAIL_INCREASE: 970 kcd->cap_needed = *(const cap_rights_t *)data; 971 kcd->cap_held = *((const cap_rights_t *)data + 1); 972 break; 973 case CAPFAIL_SYSCALL: 974 case CAPFAIL_SIGNAL: 975 case CAPFAIL_PROTO: 976 kcd->cap_int = *(const int *)data; 977 break; 978 case CAPFAIL_SOCKADDR: { 979 size_t len; 980 981 len = MIN(((const struct sockaddr *)data)->sa_len, 982 sizeof(kcd->cap_sockaddr)); 983 memset(&kcd->cap_sockaddr, 0, 984 sizeof(kcd->cap_sockaddr)); 985 memcpy(&kcd->cap_sockaddr, data, len); 986 break; 987 } 988 case CAPFAIL_NAMEI: 989 strlcpy(kcd->cap_path, data, MAXPATHLEN); 990 break; 991 case CAPFAIL_CPUSET: 992 default: 993 break; 994 } 995 } 996 ktr_enqueuerequest(td, req); 997 ktrace_exit(td); 998 } 999 1000 void 1001 ktrfault(vm_offset_t vaddr, int type) 1002 { 1003 struct thread *td = curthread; 1004 struct ktr_request *req; 1005 struct ktr_fault *kf; 1006 1007 if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 1008 return; 1009 1010 req = ktr_getrequest(KTR_FAULT); 1011 if (req == NULL) 1012 return; 1013 kf = &req->ktr_data.ktr_fault; 1014 kf->vaddr = vaddr; 1015 kf->type = type; 1016 ktr_enqueuerequest(td, req); 1017 ktrace_exit(td); 1018 } 1019 1020 void 1021 ktrfaultend(int result) 1022 { 1023 struct thread *td = curthread; 1024 struct ktr_request *req; 1025 struct ktr_faultend *kf; 1026 1027 if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 1028 return; 1029 1030 req = ktr_getrequest(KTR_FAULTEND); 1031 if (req == NULL) 1032 return; 1033 kf = &req->ktr_data.ktr_faultend; 1034 kf->result = result; 1035 ktr_enqueuerequest(td, req); 1036 ktrace_exit(td); 1037 } 1038 1039 void 1040 ktrexterr(struct thread *td) 1041 { 1042 struct ktr_request *req; 1043 struct ktr_exterr *ktre; 1044 1045 if (!KTRPOINT(td, KTR_EXTERR)) 1046 return; 1047 1048 req = ktr_getrequest(KTR_EXTERR); 1049 if (req == NULL) 1050 return; 1051 ktre = &req->ktr_data.ktr_exterr; 1052 if (exterr_to_ue(td, &ktre->ue) == 0) 1053 ktr_enqueuerequest(td, req); 1054 else 1055 ktr_freerequest(req); 1056 ktrace_exit(td); 1057 } 1058 #endif /* KTRACE */ 1059 1060 #ifndef KTRACE 1061 void 1062 ktrexterr(struct thread *td __unused) 1063 { 1064 } 1065 #endif 1066 1067 /* Interface and common routines */ 1068 1069 #ifndef _SYS_SYSPROTO_H_ 1070 struct ktrace_args { 1071 char *fname; 1072 int ops; 1073 int facs; 1074 int pid; 1075 }; 1076 #endif 1077 /* ARGSUSED */ 1078 int 1079 sys_ktrace(struct thread *td, struct ktrace_args *uap) 1080 { 1081 #ifdef KTRACE 1082 struct vnode *vp = NULL; 1083 struct proc *p; 1084 struct pgrp *pg; 1085 int facs = uap->facs & ~KTRFAC_ROOT; 1086 int ops = KTROP(uap->ops); 1087 int descend = uap->ops & KTRFLAG_DESCEND; 1088 int ret = 0; 1089 int flags, error = 0; 1090 struct nameidata nd; 1091 struct ktr_io_params *kiop, *old_kiop; 1092 1093 /* 1094 * Need something to (un)trace. 1095 */ 1096 if (ops != KTROP_CLEARFILE && facs == 0) 1097 return (EINVAL); 1098 1099 kiop = NULL; 1100 if (ops != KTROP_CLEAR) { 1101 /* 1102 * an operation which requires a file argument. 1103 */ 1104 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname); 1105 flags = FREAD | FWRITE | O_NOFOLLOW; 1106 error = vn_open(&nd, &flags, 0, NULL); 1107 if (error) 1108 return (error); 1109 NDFREE_PNBUF(&nd); 1110 vp = nd.ni_vp; 1111 VOP_UNLOCK(vp); 1112 if (vp->v_type != VREG) { 1113 (void)vn_close(vp, FREAD|FWRITE, td->td_ucred, td); 1114 return (EACCES); 1115 } 1116 kiop = ktr_io_params_alloc(td, vp); 1117 } 1118 1119 /* 1120 * Clear all uses of the tracefile. 1121 */ 1122 ktrace_enter(td); 1123 if (ops == KTROP_CLEARFILE) { 1124 restart: 1125 sx_slock(&allproc_lock); 1126 FOREACH_PROC_IN_SYSTEM(p) { 1127 old_kiop = NULL; 1128 PROC_LOCK(p); 1129 if (p->p_ktrioparms != NULL && 1130 p->p_ktrioparms->vp == vp) { 1131 if (ktrcanset(td, p)) { 1132 mtx_lock(&ktrace_mtx); 1133 old_kiop = ktr_freeproc(p); 1134 mtx_unlock(&ktrace_mtx); 1135 } else 1136 error = EPERM; 1137 } 1138 PROC_UNLOCK(p); 1139 if (old_kiop != NULL) { 1140 sx_sunlock(&allproc_lock); 1141 ktr_io_params_free(old_kiop); 1142 goto restart; 1143 } 1144 } 1145 sx_sunlock(&allproc_lock); 1146 goto done; 1147 } 1148 /* 1149 * do it 1150 */ 1151 sx_slock(&proctree_lock); 1152 if (uap->pid < 0) { 1153 /* 1154 * by process group 1155 */ 1156 pg = pgfind(-uap->pid); 1157 if (pg == NULL) { 1158 sx_sunlock(&proctree_lock); 1159 error = ESRCH; 1160 goto done; 1161 } 1162 1163 /* 1164 * ktrops() may call vrele(). Lock pg_members 1165 * by the proctree_lock rather than pg_mtx. 1166 */ 1167 PGRP_UNLOCK(pg); 1168 if (LIST_EMPTY(&pg->pg_members)) { 1169 sx_sunlock(&proctree_lock); 1170 error = ESRCH; 1171 goto done; 1172 } 1173 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 1174 PROC_LOCK(p); 1175 if (descend) 1176 ret |= ktrsetchildren(td, p, ops, facs, kiop); 1177 else 1178 ret |= ktrops(td, p, ops, facs, kiop); 1179 } 1180 } else { 1181 /* 1182 * by pid 1183 */ 1184 p = pfind(uap->pid); 1185 if (p == NULL) { 1186 error = ESRCH; 1187 sx_sunlock(&proctree_lock); 1188 goto done; 1189 } 1190 if (descend) 1191 ret |= ktrsetchildren(td, p, ops, facs, kiop); 1192 else 1193 ret |= ktrops(td, p, ops, facs, kiop); 1194 } 1195 sx_sunlock(&proctree_lock); 1196 if (!ret) 1197 error = EPERM; 1198 done: 1199 if (kiop != NULL) { 1200 mtx_lock(&ktrace_mtx); 1201 kiop = ktr_io_params_rele(kiop); 1202 mtx_unlock(&ktrace_mtx); 1203 ktr_io_params_free(kiop); 1204 } 1205 ktrace_exit(td); 1206 return (error); 1207 #else /* !KTRACE */ 1208 return (ENOSYS); 1209 #endif /* KTRACE */ 1210 } 1211 1212 /* ARGSUSED */ 1213 int 1214 sys_utrace(struct thread *td, struct utrace_args *uap) 1215 { 1216 1217 #ifdef KTRACE 1218 struct ktr_request *req; 1219 void *cp; 1220 int error; 1221 1222 if (!KTRPOINT(td, KTR_USER)) 1223 return (0); 1224 if (uap->len > KTR_USER_MAXLEN) 1225 return (EINVAL); 1226 cp = malloc(uap->len, M_KTRACE, M_WAITOK); 1227 error = copyin(uap->addr, cp, uap->len); 1228 if (error) { 1229 free(cp, M_KTRACE); 1230 return (error); 1231 } 1232 req = ktr_getrequest(KTR_USER); 1233 if (req == NULL) { 1234 free(cp, M_KTRACE); 1235 return (ENOMEM); 1236 } 1237 req->ktr_buffer = cp; 1238 req->ktr_header.ktr_len = uap->len; 1239 ktr_submitrequest(td, req); 1240 return (0); 1241 #else /* !KTRACE */ 1242 return (ENOSYS); 1243 #endif /* KTRACE */ 1244 } 1245 1246 #ifdef KTRACE 1247 static int 1248 ktrops(struct thread *td, struct proc *p, int ops, int facs, 1249 struct ktr_io_params *new_kiop) 1250 { 1251 struct ktr_io_params *old_kiop; 1252 1253 PROC_LOCK_ASSERT(p, MA_OWNED); 1254 if (!ktrcanset(td, p)) { 1255 PROC_UNLOCK(p); 1256 return (0); 1257 } 1258 if ((ops == KTROP_SET && p->p_state == PRS_NEW) || 1259 p_cansee(td, p) != 0) { 1260 /* 1261 * Disallow setting trace points if the process is being born. 1262 * This avoids races with trace point inheritance in 1263 * ktrprocfork(). 1264 */ 1265 PROC_UNLOCK(p); 1266 return (0); 1267 } 1268 if ((p->p_flag & P_WEXIT) != 0) { 1269 /* 1270 * There's nothing to do if the process is exiting, but avoid 1271 * signaling an error. 1272 */ 1273 PROC_UNLOCK(p); 1274 return (1); 1275 } 1276 old_kiop = NULL; 1277 mtx_lock(&ktrace_mtx); 1278 if (ops == KTROP_SET) { 1279 if (p->p_ktrioparms != NULL && 1280 p->p_ktrioparms->vp != new_kiop->vp) { 1281 /* if trace file already in use, relinquish below */ 1282 old_kiop = ktr_io_params_rele(p->p_ktrioparms); 1283 p->p_ktrioparms = NULL; 1284 } 1285 if (p->p_ktrioparms == NULL) { 1286 p->p_ktrioparms = new_kiop; 1287 ktr_io_params_ref(new_kiop); 1288 } 1289 p->p_traceflag |= facs; 1290 if (priv_check(td, PRIV_KTRACE) == 0) 1291 p->p_traceflag |= KTRFAC_ROOT; 1292 } else { 1293 /* KTROP_CLEAR */ 1294 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) 1295 /* no more tracing */ 1296 old_kiop = ktr_freeproc(p); 1297 } 1298 mtx_unlock(&ktrace_mtx); 1299 if ((p->p_traceflag & KTRFAC_MASK) != 0) 1300 ktrprocctor_entered(td, p); 1301 PROC_UNLOCK(p); 1302 ktr_io_params_free(old_kiop); 1303 1304 return (1); 1305 } 1306 1307 static int 1308 ktrsetchildren(struct thread *td, struct proc *top, int ops, int facs, 1309 struct ktr_io_params *new_kiop) 1310 { 1311 struct proc *p; 1312 int ret = 0; 1313 1314 p = top; 1315 PROC_LOCK_ASSERT(p, MA_OWNED); 1316 sx_assert(&proctree_lock, SX_LOCKED); 1317 for (;;) { 1318 ret |= ktrops(td, p, ops, facs, new_kiop); 1319 /* 1320 * If this process has children, descend to them next, 1321 * otherwise do any siblings, and if done with this level, 1322 * follow back up the tree (but not past top). 1323 */ 1324 if (!LIST_EMPTY(&p->p_children)) 1325 p = LIST_FIRST(&p->p_children); 1326 else for (;;) { 1327 if (p == top) 1328 return (ret); 1329 if (LIST_NEXT(p, p_sibling)) { 1330 p = LIST_NEXT(p, p_sibling); 1331 break; 1332 } 1333 p = p->p_pptr; 1334 } 1335 PROC_LOCK(p); 1336 } 1337 /*NOTREACHED*/ 1338 } 1339 1340 static void 1341 ktr_writerequest(struct thread *td, struct ktr_request *req) 1342 { 1343 struct ktr_io_params *kiop, *kiop1; 1344 struct ktr_header *kth; 1345 struct vnode *vp; 1346 struct proc *p; 1347 struct ucred *cred; 1348 struct uio auio; 1349 struct iovec aiov[3]; 1350 struct mount *mp; 1351 off_t lim; 1352 int datalen, buflen; 1353 int error; 1354 1355 p = td->td_proc; 1356 1357 /* 1358 * We reference the kiop for use in I/O in case ktrace is 1359 * disabled on the process as we write out the request. 1360 */ 1361 mtx_lock(&ktrace_mtx); 1362 kiop = p->p_ktrioparms; 1363 1364 /* 1365 * If kiop is NULL, it has been cleared out from under this 1366 * request, so just drop it. 1367 */ 1368 if (kiop == NULL) { 1369 mtx_unlock(&ktrace_mtx); 1370 return; 1371 } 1372 1373 ktr_io_params_ref(kiop); 1374 vp = kiop->vp; 1375 cred = kiop->cr; 1376 lim = kiop->lim; 1377 1378 KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL")); 1379 mtx_unlock(&ktrace_mtx); 1380 1381 kth = &req->ktr_header; 1382 KASSERT(((u_short)kth->ktr_type & ~KTR_TYPE) < nitems(data_lengths), 1383 ("data_lengths array overflow")); 1384 datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_TYPE]; 1385 buflen = kth->ktr_len; 1386 auio.uio_iov = &aiov[0]; 1387 auio.uio_offset = 0; 1388 auio.uio_segflg = UIO_SYSSPACE; 1389 auio.uio_rw = UIO_WRITE; 1390 aiov[0].iov_base = (caddr_t)kth; 1391 aiov[0].iov_len = sizeof(struct ktr_header); 1392 auio.uio_resid = sizeof(struct ktr_header); 1393 auio.uio_iovcnt = 1; 1394 auio.uio_td = td; 1395 if (datalen != 0) { 1396 aiov[1].iov_base = (caddr_t)&req->ktr_data; 1397 aiov[1].iov_len = datalen; 1398 auio.uio_resid += datalen; 1399 auio.uio_iovcnt++; 1400 kth->ktr_len += datalen; 1401 } 1402 if (buflen != 0) { 1403 KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write")); 1404 aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer; 1405 aiov[auio.uio_iovcnt].iov_len = buflen; 1406 auio.uio_resid += buflen; 1407 auio.uio_iovcnt++; 1408 } 1409 1410 vn_start_write(vp, &mp, V_WAIT); 1411 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1412 td->td_ktr_io_lim = lim; 1413 #ifdef MAC 1414 error = mac_vnode_check_write(cred, NOCRED, vp); 1415 if (error == 0) 1416 #endif 1417 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred); 1418 VOP_UNLOCK(vp); 1419 vn_finished_write(mp); 1420 if (error == 0) { 1421 mtx_lock(&ktrace_mtx); 1422 kiop = ktr_io_params_rele(kiop); 1423 mtx_unlock(&ktrace_mtx); 1424 ktr_io_params_free(kiop); 1425 return; 1426 } 1427 1428 /* 1429 * If error encountered, give up tracing on this vnode on this 1430 * process. Other processes might still be suitable for 1431 * writes to this vnode. 1432 */ 1433 log(LOG_NOTICE, 1434 "ktrace write failed, errno %d, tracing stopped for pid %d\n", 1435 error, p->p_pid); 1436 1437 kiop1 = NULL; 1438 PROC_LOCK(p); 1439 mtx_lock(&ktrace_mtx); 1440 if (p->p_ktrioparms != NULL && p->p_ktrioparms->vp == vp) 1441 kiop1 = ktr_freeproc(p); 1442 kiop = ktr_io_params_rele(kiop); 1443 mtx_unlock(&ktrace_mtx); 1444 PROC_UNLOCK(p); 1445 ktr_io_params_free(kiop1); 1446 ktr_io_params_free(kiop); 1447 } 1448 1449 /* 1450 * Return true if caller has permission to set the ktracing state 1451 * of target. Essentially, the target can't possess any 1452 * more permissions than the caller. KTRFAC_ROOT signifies that 1453 * root previously set the tracing status on the target process, and 1454 * so, only root may further change it. 1455 */ 1456 static int 1457 ktrcanset(struct thread *td, struct proc *targetp) 1458 { 1459 1460 PROC_LOCK_ASSERT(targetp, MA_OWNED); 1461 if (targetp->p_traceflag & KTRFAC_ROOT && 1462 priv_check(td, PRIV_KTRACE)) 1463 return (0); 1464 1465 if (p_candebug(td, targetp) != 0) 1466 return (0); 1467 1468 return (1); 1469 } 1470 1471 #endif /* KTRACE */ 1472