1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1999-2005 Apple Inc. 5 * Copyright (c) 2006-2007, 2016-2018 Robert N. M. Watson 6 * All rights reserved. 7 * 8 * Portions of this software were developed by BAE Systems, the University of 9 * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL 10 * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent 11 * Computing (TC) research program. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of Apple Inc. ("Apple") nor the names of 22 * its contributors may be used to endorse or promote products derived 23 * from this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR 29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 34 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 #include <sys/cdefs.h> 39 #include <sys/param.h> 40 #include <sys/condvar.h> 41 #include <sys/conf.h> 42 #include <sys/eventhandler.h> 43 #include <sys/file.h> 44 #include <sys/filedesc.h> 45 #include <sys/fcntl.h> 46 #include <sys/ipc.h> 47 #include <sys/jail.h> 48 #include <sys/kernel.h> 49 #include <sys/kthread.h> 50 #include <sys/malloc.h> 51 #include <sys/mount.h> 52 #include <sys/namei.h> 53 #include <sys/priv.h> 54 #include <sys/proc.h> 55 #include <sys/queue.h> 56 #include <sys/socket.h> 57 #include <sys/socketvar.h> 58 #include <sys/protosw.h> 59 #include <sys/domain.h> 60 #include <sys/sysctl.h> 61 #include <sys/sysproto.h> 62 #include <sys/sysent.h> 63 #include <sys/systm.h> 64 #include <sys/ucred.h> 65 #include <sys/uio.h> 66 #include <sys/un.h> 67 #include <sys/unistd.h> 68 #include <sys/vnode.h> 69 70 #include <bsm/audit.h> 71 #include <bsm/audit_internal.h> 72 #include <bsm/audit_kevents.h> 73 74 #include <netinet/in.h> 75 #include <netinet/in_pcb.h> 76 77 #include <security/audit/audit.h> 78 #include <security/audit/audit_private.h> 79 80 #include <vm/uma.h> 81 82 FEATURE(audit, "BSM audit support"); 83 84 static uma_zone_t audit_record_zone; 85 static MALLOC_DEFINE(M_AUDITCRED, "audit_cred", "Audit cred storage"); 86 MALLOC_DEFINE(M_AUDITDATA, "audit_data", "Audit data storage"); 87 MALLOC_DEFINE(M_AUDITPATH, "audit_path", "Audit path storage"); 88 MALLOC_DEFINE(M_AUDITTEXT, "audit_text", "Audit text storage"); 89 MALLOC_DEFINE(M_AUDITGIDSET, "audit_gidset", "Audit GID set storage"); 90 91 static SYSCTL_NODE(_security, OID_AUTO, audit, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 92 "TrustedBSD audit controls"); 93 94 /* 95 * Audit control settings that are set/read by system calls and are hence 96 * non-static. 97 * 98 * Define the audit control flags. 99 */ 100 int audit_trail_enabled; 101 int audit_trail_suspended; 102 #ifdef KDTRACE_HOOKS 103 u_int audit_dtrace_enabled; 104 #endif 105 bool __read_frequently audit_syscalls_enabled; 106 107 /* 108 * Flags controlling behavior in low storage situations. Should we panic if 109 * a write fails? Should we fail stop if we're out of disk space? 110 */ 111 int audit_panic_on_write_fail; 112 int audit_fail_stop; 113 int audit_argv; 114 int audit_arge; 115 116 /* 117 * Are we currently "failing stop" due to out of disk space? 118 */ 119 int audit_in_failure; 120 121 /* 122 * Global audit statistics. 123 */ 124 struct audit_fstat audit_fstat; 125 126 /* 127 * Preselection mask for non-attributable events. 128 */ 129 struct au_mask audit_nae_mask; 130 131 /* 132 * Mutex to protect global variables shared between various threads and 133 * processes. 134 */ 135 struct mtx audit_mtx; 136 137 /* 138 * Queue of audit records ready for delivery to disk. We insert new records 139 * at the tail, and remove records from the head. Also, a count of the 140 * number of records used for checking queue depth. In addition, a counter 141 * of records that we have allocated but are not yet in the queue, which is 142 * needed to estimate the total size of the combined set of records 143 * outstanding in the system. 144 */ 145 struct kaudit_queue audit_q; 146 int audit_q_len; 147 int audit_pre_q_len; 148 149 /* 150 * Audit queue control settings (minimum free, low/high water marks, etc.) 151 */ 152 struct au_qctrl audit_qctrl; 153 154 /* 155 * Condition variable to signal to the worker that it has work to do: either 156 * new records are in the queue, or a log replacement is taking place. 157 */ 158 struct cv audit_worker_cv; 159 160 /* 161 * Condition variable to flag when crossing the low watermark, meaning that 162 * threads blocked due to hitting the high watermark can wake up and continue 163 * to commit records. 164 */ 165 struct cv audit_watermark_cv; 166 167 /* 168 * Condition variable for auditing threads wait on when in fail-stop mode. 169 * Threads wait on this CV forever (and ever), never seeing the light of day 170 * again. 171 */ 172 static struct cv audit_fail_cv; 173 174 /* 175 * Optional DTrace audit provider support: function pointers for preselection 176 * and commit events. 177 */ 178 #ifdef KDTRACE_HOOKS 179 void *(*dtaudit_hook_preselect)(au_id_t auid, au_event_t event, 180 au_class_t class); 181 int (*dtaudit_hook_commit)(struct kaudit_record *kar, au_id_t auid, 182 au_event_t event, au_class_t class, int sorf); 183 void (*dtaudit_hook_bsm)(struct kaudit_record *kar, au_id_t auid, 184 au_event_t event, au_class_t class, int sorf, 185 void *bsm_data, size_t bsm_lenlen); 186 #endif 187 188 /* 189 * Kernel audit information. This will store the current audit address 190 * or host information that the kernel will use when it's generating 191 * audit records. This data is modified by the A_GET{SET}KAUDIT auditon(2) 192 * command. 193 */ 194 static struct auditinfo_addr audit_kinfo; 195 static struct rwlock audit_kinfo_lock; 196 197 #define KINFO_LOCK_INIT() rw_init(&audit_kinfo_lock, \ 198 "audit_kinfo_lock") 199 #define KINFO_RLOCK() rw_rlock(&audit_kinfo_lock) 200 #define KINFO_WLOCK() rw_wlock(&audit_kinfo_lock) 201 #define KINFO_RUNLOCK() rw_runlock(&audit_kinfo_lock) 202 #define KINFO_WUNLOCK() rw_wunlock(&audit_kinfo_lock) 203 204 /* 205 * Check various policies to see if we should enable system-call audit hooks. 206 * Note that despite the mutex being held, we want to assign a value exactly 207 * once, as checks of the flag are performed lock-free for performance 208 * reasons. The mutex is used to get a consistent snapshot of policy state -- 209 * e.g., safely accessing the two audit_trail flags. 210 */ 211 void 212 audit_syscalls_enabled_update(void) 213 { 214 215 mtx_lock(&audit_mtx); 216 #ifdef KDTRACE_HOOKS 217 if (audit_dtrace_enabled) 218 audit_syscalls_enabled = true; 219 else { 220 #endif 221 if (audit_trail_enabled && !audit_trail_suspended) 222 audit_syscalls_enabled = true; 223 else 224 audit_syscalls_enabled = false; 225 #ifdef KDTRACE_HOOKS 226 } 227 #endif 228 mtx_unlock(&audit_mtx); 229 } 230 231 void 232 audit_set_kinfo(struct auditinfo_addr *ak) 233 { 234 235 KASSERT(ak->ai_termid.at_type == AU_IPv4 || 236 ak->ai_termid.at_type == AU_IPv6, 237 ("audit_set_kinfo: invalid address type")); 238 239 KINFO_WLOCK(); 240 audit_kinfo = *ak; 241 KINFO_WUNLOCK(); 242 } 243 244 void 245 audit_get_kinfo(struct auditinfo_addr *ak) 246 { 247 248 KASSERT(audit_kinfo.ai_termid.at_type == AU_IPv4 || 249 audit_kinfo.ai_termid.at_type == AU_IPv6, 250 ("audit_set_kinfo: invalid address type")); 251 252 KINFO_RLOCK(); 253 *ak = audit_kinfo; 254 KINFO_RUNLOCK(); 255 } 256 257 /* 258 * Construct an audit record for the passed thread. 259 */ 260 static int 261 audit_record_ctor(void *mem, int size, void *arg, int flags) 262 { 263 struct kaudit_record *ar; 264 struct thread *td; 265 struct ucred *cred; 266 struct prison *pr; 267 268 KASSERT(sizeof(*ar) == size, ("audit_record_ctor: wrong size")); 269 270 td = arg; 271 ar = mem; 272 bzero(ar, sizeof(*ar)); 273 ar->k_ar.ar_magic = AUDIT_RECORD_MAGIC; 274 nanotime(&ar->k_ar.ar_starttime); 275 276 /* 277 * Export the subject credential. 278 */ 279 cred = td->td_ucred; 280 cru2x(cred, &ar->k_ar.ar_subj_cred); 281 ar->k_ar.ar_subj_ruid = cred->cr_ruid; 282 ar->k_ar.ar_subj_rgid = cred->cr_rgid; 283 ar->k_ar.ar_subj_egid = cred->cr_groups[0]; 284 ar->k_ar.ar_subj_auid = cred->cr_audit.ai_auid; 285 ar->k_ar.ar_subj_asid = cred->cr_audit.ai_asid; 286 ar->k_ar.ar_subj_pid = td->td_proc->p_pid; 287 ar->k_ar.ar_subj_amask = cred->cr_audit.ai_mask; 288 ar->k_ar.ar_subj_term_addr = cred->cr_audit.ai_termid; 289 /* 290 * If this process is jailed, make sure we capture the name of the 291 * jail so we can use it to generate a zonename token when we covert 292 * this record to BSM. 293 */ 294 if (jailed(cred)) { 295 pr = cred->cr_prison; 296 (void) strlcpy(ar->k_ar.ar_jailname, pr->pr_name, 297 sizeof(ar->k_ar.ar_jailname)); 298 } else 299 ar->k_ar.ar_jailname[0] = '\0'; 300 return (0); 301 } 302 303 static void 304 audit_record_dtor(void *mem, int size, void *arg) 305 { 306 struct kaudit_record *ar; 307 308 KASSERT(sizeof(*ar) == size, ("audit_record_dtor: wrong size")); 309 310 ar = mem; 311 if (ar->k_ar.ar_arg_upath1 != NULL) 312 free(ar->k_ar.ar_arg_upath1, M_AUDITPATH); 313 if (ar->k_ar.ar_arg_upath2 != NULL) 314 free(ar->k_ar.ar_arg_upath2, M_AUDITPATH); 315 if (ar->k_ar.ar_arg_text != NULL) 316 free(ar->k_ar.ar_arg_text, M_AUDITTEXT); 317 if (ar->k_udata != NULL) 318 free(ar->k_udata, M_AUDITDATA); 319 if (ar->k_ar.ar_arg_argv != NULL) 320 free(ar->k_ar.ar_arg_argv, M_AUDITTEXT); 321 if (ar->k_ar.ar_arg_envv != NULL) 322 free(ar->k_ar.ar_arg_envv, M_AUDITTEXT); 323 if (ar->k_ar.ar_arg_groups.gidset != NULL) 324 free(ar->k_ar.ar_arg_groups.gidset, M_AUDITGIDSET); 325 } 326 327 /* 328 * Initialize the Audit subsystem: configuration state, work queue, 329 * synchronization primitives, worker thread, and trigger device node. Also 330 * call into the BSM assembly code to initialize it. 331 */ 332 static void 333 audit_init(void) 334 { 335 336 audit_trail_enabled = 0; 337 audit_trail_suspended = 0; 338 audit_syscalls_enabled = false; 339 audit_panic_on_write_fail = 0; 340 audit_fail_stop = 0; 341 audit_in_failure = 0; 342 audit_argv = 0; 343 audit_arge = 0; 344 345 audit_fstat.af_filesz = 0; /* '0' means unset, unbounded. */ 346 audit_fstat.af_currsz = 0; 347 audit_nae_mask.am_success = 0; 348 audit_nae_mask.am_failure = 0; 349 350 TAILQ_INIT(&audit_q); 351 audit_q_len = 0; 352 audit_pre_q_len = 0; 353 audit_qctrl.aq_hiwater = AQ_HIWATER; 354 audit_qctrl.aq_lowater = AQ_LOWATER; 355 audit_qctrl.aq_bufsz = AQ_BUFSZ; 356 audit_qctrl.aq_minfree = AU_FS_MINFREE; 357 358 audit_kinfo.ai_termid.at_type = AU_IPv4; 359 audit_kinfo.ai_termid.at_addr[0] = INADDR_ANY; 360 361 mtx_init(&audit_mtx, "audit_mtx", NULL, MTX_DEF); 362 KINFO_LOCK_INIT(); 363 cv_init(&audit_worker_cv, "audit_worker_cv"); 364 cv_init(&audit_watermark_cv, "audit_watermark_cv"); 365 cv_init(&audit_fail_cv, "audit_fail_cv"); 366 367 audit_record_zone = uma_zcreate("audit_record", 368 sizeof(struct kaudit_record), audit_record_ctor, 369 audit_record_dtor, NULL, NULL, UMA_ALIGN_PTR, 0); 370 371 /* First initialisation of audit_syscalls_enabled. */ 372 audit_syscalls_enabled_update(); 373 374 /* Initialize the BSM audit subsystem. */ 375 kau_init(); 376 377 audit_trigger_init(); 378 379 /* Register shutdown handler. */ 380 EVENTHANDLER_REGISTER(shutdown_pre_sync, audit_shutdown, NULL, 381 SHUTDOWN_PRI_FIRST); 382 383 /* Start audit worker thread. */ 384 audit_worker_init(); 385 } 386 387 SYSINIT(audit_init, SI_SUB_AUDIT, SI_ORDER_FIRST, audit_init, NULL); 388 389 /* 390 * Drain the audit queue and close the log at shutdown. Note that this can 391 * be called both from the system shutdown path and also from audit 392 * configuration syscalls, so 'arg' and 'howto' are ignored. 393 * 394 * XXXRW: In FreeBSD 7.x and 8.x, this fails to wait for the record queue to 395 * drain before returning, which could lead to lost records on shutdown. 396 */ 397 void 398 audit_shutdown(void *arg, int howto) 399 { 400 401 audit_rotate_vnode(NULL, NULL); 402 } 403 404 /* 405 * Return the current thread's audit record, if any. 406 */ 407 struct kaudit_record * 408 currecord(void) 409 { 410 411 return (curthread->td_ar); 412 } 413 414 /* 415 * XXXAUDIT: Shouldn't there be logic here to sleep waiting on available 416 * pre_q space, suspending the system call until there is room? 417 */ 418 struct kaudit_record * 419 audit_new(int event, struct thread *td) 420 { 421 struct kaudit_record *ar; 422 423 /* 424 * Note: the number of outstanding uncommitted audit records is 425 * limited to the number of concurrent threads servicing system calls 426 * in the kernel. 427 */ 428 ar = uma_zalloc_arg(audit_record_zone, td, M_WAITOK); 429 ar->k_ar.ar_event = event; 430 431 mtx_lock(&audit_mtx); 432 audit_pre_q_len++; 433 mtx_unlock(&audit_mtx); 434 435 return (ar); 436 } 437 438 void 439 audit_free(struct kaudit_record *ar) 440 { 441 442 uma_zfree(audit_record_zone, ar); 443 } 444 445 void 446 audit_commit(struct kaudit_record *ar, int error, int retval) 447 { 448 au_event_t event; 449 au_class_t class; 450 au_id_t auid; 451 int sorf; 452 struct au_mask *aumask; 453 454 if (ar == NULL) 455 return; 456 457 ar->k_ar.ar_errno = error; 458 ar->k_ar.ar_retval = retval; 459 nanotime(&ar->k_ar.ar_endtime); 460 461 /* 462 * Decide whether to commit the audit record by checking the error 463 * value from the system call and using the appropriate audit mask. 464 */ 465 if (ar->k_ar.ar_subj_auid == AU_DEFAUDITID) 466 aumask = &audit_nae_mask; 467 else 468 aumask = &ar->k_ar.ar_subj_amask; 469 470 if (error) 471 sorf = AU_PRS_FAILURE; 472 else 473 sorf = AU_PRS_SUCCESS; 474 475 /* 476 * syscalls.master sometimes contains a prototype event number, which 477 * we will transform into a more specific event number now that we 478 * have more complete information gathered during the system call. 479 */ 480 switch(ar->k_ar.ar_event) { 481 case AUE_OPEN_RWTC: 482 ar->k_ar.ar_event = audit_flags_and_error_to_openevent( 483 ar->k_ar.ar_arg_fflags, error); 484 break; 485 486 case AUE_OPENAT_RWTC: 487 ar->k_ar.ar_event = audit_flags_and_error_to_openatevent( 488 ar->k_ar.ar_arg_fflags, error); 489 break; 490 491 case AUE_SYSCTL: 492 ar->k_ar.ar_event = audit_ctlname_to_sysctlevent( 493 ar->k_ar.ar_arg_ctlname, ar->k_ar.ar_valid_arg); 494 break; 495 496 case AUE_AUDITON: 497 /* Convert the auditon() command to an event. */ 498 ar->k_ar.ar_event = auditon_command_event(ar->k_ar.ar_arg_cmd); 499 break; 500 501 case AUE_MSGSYS: 502 if (ARG_IS_VALID(ar, ARG_SVIPC_WHICH)) 503 ar->k_ar.ar_event = 504 audit_msgsys_to_event(ar->k_ar.ar_arg_svipc_which); 505 break; 506 507 case AUE_SEMSYS: 508 if (ARG_IS_VALID(ar, ARG_SVIPC_WHICH)) 509 ar->k_ar.ar_event = 510 audit_semsys_to_event(ar->k_ar.ar_arg_svipc_which); 511 break; 512 513 case AUE_SHMSYS: 514 if (ARG_IS_VALID(ar, ARG_SVIPC_WHICH)) 515 ar->k_ar.ar_event = 516 audit_shmsys_to_event(ar->k_ar.ar_arg_svipc_which); 517 break; 518 } 519 520 auid = ar->k_ar.ar_subj_auid; 521 event = ar->k_ar.ar_event; 522 class = au_event_class(event); 523 524 ar->k_ar_commit |= AR_COMMIT_KERNEL; 525 if (au_preselect(event, class, aumask, sorf) != 0) 526 ar->k_ar_commit |= AR_PRESELECT_TRAIL; 527 if (audit_pipe_preselect(auid, event, class, sorf, 528 ar->k_ar_commit & AR_PRESELECT_TRAIL) != 0) 529 ar->k_ar_commit |= AR_PRESELECT_PIPE; 530 #ifdef KDTRACE_HOOKS 531 /* 532 * Expose the audit record to DTrace, both to allow the "commit" probe 533 * to fire if it's desirable, and also to allow a decision to be made 534 * about later firing with BSM in the audit worker. 535 */ 536 if (dtaudit_hook_commit != NULL) { 537 if (dtaudit_hook_commit(ar, auid, event, class, sorf) != 0) 538 ar->k_ar_commit |= AR_PRESELECT_DTRACE; 539 } 540 #endif 541 542 if ((ar->k_ar_commit & (AR_PRESELECT_TRAIL | AR_PRESELECT_PIPE | 543 AR_PRESELECT_USER_TRAIL | AR_PRESELECT_USER_PIPE | 544 AR_PRESELECT_DTRACE)) == 0) { 545 mtx_lock(&audit_mtx); 546 audit_pre_q_len--; 547 mtx_unlock(&audit_mtx); 548 audit_free(ar); 549 return; 550 } 551 552 /* 553 * Note: it could be that some records initiated while audit was 554 * enabled should still be committed? 555 * 556 * NB: The check here is not for audit_syscalls because any 557 * DTrace-related obligations have been fulfilled above -- we're just 558 * down to the trail and pipes now. 559 */ 560 mtx_lock(&audit_mtx); 561 if (audit_trail_suspended || !audit_trail_enabled) { 562 audit_pre_q_len--; 563 mtx_unlock(&audit_mtx); 564 audit_free(ar); 565 return; 566 } 567 568 /* 569 * Constrain the number of committed audit records based on the 570 * configurable parameter. 571 */ 572 while (audit_q_len >= audit_qctrl.aq_hiwater) 573 cv_wait(&audit_watermark_cv, &audit_mtx); 574 575 TAILQ_INSERT_TAIL(&audit_q, ar, k_q); 576 audit_q_len++; 577 audit_pre_q_len--; 578 cv_signal(&audit_worker_cv); 579 mtx_unlock(&audit_mtx); 580 } 581 582 /* 583 * audit_syscall_enter() is called on entry to each system call. It is 584 * responsible for deciding whether or not to audit the call (preselection), 585 * and if so, allocating a per-thread audit record. audit_new() will fill in 586 * basic thread/credential properties. 587 * 588 * This function will be entered only if audit_syscalls_enabled was set in the 589 * macro wrapper for this function. It could be cleared by the time this 590 * function runs, but that is an acceptable race. 591 */ 592 void 593 audit_syscall_enter(unsigned short code, struct thread *td) 594 { 595 struct au_mask *aumask; 596 #ifdef KDTRACE_HOOKS 597 void *dtaudit_state; 598 #endif 599 au_class_t class; 600 au_event_t event; 601 au_id_t auid; 602 int record_needed; 603 604 KASSERT(td->td_ar == NULL, ("audit_syscall_enter: td->td_ar != NULL")); 605 KASSERT((td->td_pflags & TDP_AUDITREC) == 0, 606 ("audit_syscall_enter: TDP_AUDITREC set")); 607 608 /* 609 * In FreeBSD, each ABI has its own system call table, and hence 610 * mapping of system call codes to audit events. Convert the code to 611 * an audit event identifier using the process system call table 612 * reference. In Darwin, there's only one, so we use the global 613 * symbol for the system call table. No audit record is generated 614 * for bad system calls, as no operation has been performed. 615 */ 616 if (code >= td->td_proc->p_sysent->sv_size) 617 return; 618 619 event = td->td_proc->p_sysent->sv_table[code].sy_auevent; 620 if (event == AUE_NULL) 621 return; 622 623 /* 624 * Check which audit mask to use; either the kernel non-attributable 625 * event mask or the process audit mask. 626 */ 627 auid = td->td_ucred->cr_audit.ai_auid; 628 if (auid == AU_DEFAUDITID) 629 aumask = &audit_nae_mask; 630 else 631 aumask = &td->td_ucred->cr_audit.ai_mask; 632 633 /* 634 * Determine whether trail or pipe preselection would like an audit 635 * record allocated for this system call. 636 */ 637 class = au_event_class(event); 638 if (au_preselect(event, class, aumask, AU_PRS_BOTH)) { 639 /* 640 * If we're out of space and need to suspend unprivileged 641 * processes, do that here rather than trying to allocate 642 * another audit record. 643 * 644 * Note: we might wish to be able to continue here in the 645 * future, if the system recovers. That should be possible 646 * by means of checking the condition in a loop around 647 * cv_wait(). It might be desirable to reevaluate whether an 648 * audit record is still required for this event by 649 * re-calling au_preselect(). 650 */ 651 if (audit_in_failure && 652 priv_check(td, PRIV_AUDIT_FAILSTOP) != 0) { 653 cv_wait(&audit_fail_cv, &audit_mtx); 654 panic("audit_failing_stop: thread continued"); 655 } 656 record_needed = 1; 657 } else if (audit_pipe_preselect(auid, event, class, AU_PRS_BOTH, 0)) { 658 record_needed = 1; 659 } else { 660 record_needed = 0; 661 } 662 663 /* 664 * After audit trails and pipes have made their policy choices, DTrace 665 * may request that records be generated as well. This is a slightly 666 * complex affair, as the DTrace audit provider needs the audit 667 * framework to maintain some state on the audit record, which has not 668 * been allocated at the point where the decision has to be made. 669 * This hook must run even if we are not changing the decision, as 670 * DTrace may want to stick event state onto a record we were going to 671 * produce due to the trail or pipes. The event state returned by the 672 * DTrace provider must be safe without locks held between here and 673 * below -- i.e., dtaudit_state must must refer to stable memory. 674 */ 675 #ifdef KDTRACE_HOOKS 676 dtaudit_state = NULL; 677 if (dtaudit_hook_preselect != NULL) { 678 dtaudit_state = dtaudit_hook_preselect(auid, event, class); 679 if (dtaudit_state != NULL) 680 record_needed = 1; 681 } 682 #endif 683 684 /* 685 * If a record is required, allocate it and attach it to the thread 686 * for use throughout the system call. Also attach DTrace state if 687 * required. 688 * 689 * XXXRW: If we decide to reference count the evname_elem underlying 690 * dtaudit_state, we will need to free here if no record is allocated 691 * or allocatable. 692 */ 693 if (record_needed) { 694 td->td_ar = audit_new(event, td); 695 if (td->td_ar != NULL) { 696 td->td_pflags |= TDP_AUDITREC; 697 #ifdef KDTRACE_HOOKS 698 td->td_ar->k_dtaudit_state = dtaudit_state; 699 #endif 700 } 701 } else 702 td->td_ar = NULL; 703 } 704 705 /* 706 * audit_syscall_exit() is called from the return of every system call, or in 707 * the event of exit1(), during the execution of exit1(). It is responsible 708 * for committing the audit record, if any, along with return condition. 709 */ 710 void 711 audit_syscall_exit(int error, struct thread *td) 712 { 713 int retval; 714 715 /* 716 * Commit the audit record as desired; once we pass the record into 717 * audit_commit(), the memory is owned by the audit subsystem. The 718 * return value from the system call is stored on the user thread. 719 * If there was an error, the return value is set to -1, imitating 720 * the behavior of the cerror routine. 721 */ 722 if (error) 723 retval = -1; 724 else 725 retval = td->td_retval[0]; 726 727 audit_commit(td->td_ar, error, retval); 728 td->td_ar = NULL; 729 td->td_pflags &= ~TDP_AUDITREC; 730 } 731 732 void 733 audit_cred_copy(struct ucred *src, struct ucred *dest) 734 { 735 736 bcopy(&src->cr_audit, &dest->cr_audit, sizeof(dest->cr_audit)); 737 } 738 739 void 740 audit_cred_destroy(struct ucred *cred) 741 { 742 743 } 744 745 void 746 audit_cred_init(struct ucred *cred) 747 { 748 749 bzero(&cred->cr_audit, sizeof(cred->cr_audit)); 750 } 751 752 /* 753 * Initialize audit information for the first kernel process (proc 0) and for 754 * the first user process (init). 755 */ 756 void 757 audit_cred_kproc0(struct ucred *cred) 758 { 759 760 cred->cr_audit.ai_auid = AU_DEFAUDITID; 761 cred->cr_audit.ai_termid.at_type = AU_IPv4; 762 } 763 764 void 765 audit_cred_proc1(struct ucred *cred) 766 { 767 768 cred->cr_audit.ai_auid = AU_DEFAUDITID; 769 cred->cr_audit.ai_termid.at_type = AU_IPv4; 770 } 771 772 void 773 audit_thread_alloc(struct thread *td) 774 { 775 776 td->td_ar = NULL; 777 } 778 779 void 780 audit_thread_free(struct thread *td) 781 { 782 783 KASSERT(td->td_ar == NULL, ("audit_thread_free: td_ar != NULL")); 784 KASSERT((td->td_pflags & TDP_AUDITREC) == 0, 785 ("audit_thread_free: TDP_AUDITREC set")); 786 } 787 788 void 789 audit_proc_coredump(struct thread *td, char *path, int errcode) 790 { 791 struct kaudit_record *ar; 792 struct au_mask *aumask; 793 struct ucred *cred; 794 au_class_t class; 795 int ret, sorf; 796 char **pathp; 797 au_id_t auid; 798 799 ret = 0; 800 801 /* 802 * Make sure we are using the correct preselection mask. 803 */ 804 cred = td->td_ucred; 805 auid = cred->cr_audit.ai_auid; 806 if (auid == AU_DEFAUDITID) 807 aumask = &audit_nae_mask; 808 else 809 aumask = &cred->cr_audit.ai_mask; 810 /* 811 * It's possible for coredump(9) generation to fail. Make sure that 812 * we handle this case correctly for preselection. 813 */ 814 if (errcode != 0) 815 sorf = AU_PRS_FAILURE; 816 else 817 sorf = AU_PRS_SUCCESS; 818 class = au_event_class(AUE_CORE); 819 if (au_preselect(AUE_CORE, class, aumask, sorf) == 0 && 820 audit_pipe_preselect(auid, AUE_CORE, class, sorf, 0) == 0) 821 return; 822 823 /* 824 * If we are interested in seeing this audit record, allocate it. 825 * Where possible coredump records should contain a pathname and arg32 826 * (signal) tokens. 827 */ 828 ar = audit_new(AUE_CORE, td); 829 if (ar == NULL) 830 return; 831 if (path != NULL) { 832 pathp = &ar->k_ar.ar_arg_upath1; 833 *pathp = malloc(MAXPATHLEN, M_AUDITPATH, M_WAITOK); 834 audit_canon_path(td, AT_FDCWD, path, *pathp); 835 ARG_SET_VALID(ar, ARG_UPATH1); 836 } 837 ar->k_ar.ar_arg_signum = td->td_proc->p_sig; 838 ARG_SET_VALID(ar, ARG_SIGNUM); 839 if (errcode != 0) 840 ret = 1; 841 audit_commit(ar, errcode, ret); 842 } 843